cor
cor

Reputation: 627

Sprite kit impulse at point

example image

I'm trying to use two points on a spaceship to create a "tank like movement" were the space ship will move forward when both points get an impulse on the physicsBody (using the applyImpulse(CGVector, atPoint: CGPoint) method) and turn left/right when only one point gets an impulse.

I can move the space ship forward using the applyImpulse(CGVector) method without the atPoint parameter and it works, but i have issues using the atPoint parameter.

When trying to use the atPoint parameter the spaceship will move really randomly, even if i apply an impulse to both jet engine 1 and jet engine 2, but i'm not sure if i apply the forces at the right point (on the jet engine spots marked on the image), the documentation is very vague about this and i don't know how to get the position of the impulses right.

Does anyone know how to get the correct positions to apply the impulses to? It should be like the ones on the image. I'm using Swift but i can read Objective C so that doesn't really matter.

EDIT: I have tried the following points:

CGPoint(x: 0, y: 0) and CGPoint(x: 1, y: 0)    
CGPoint(x: 0, y: 0) and CGPoint(x: spaceship.size.width, y: 0)    

Upvotes: 2

Views: 738

Answers (2)

Confused
Confused

Reputation: 6288

A way to do this that's (possibly) going to give you a more "realistic" drag and thrust result is hanging two engine bodies off your fighter jet, and giving them both drag properties, and apply thrust from them when needed, as needed.

In this way you'll get the type of rotation common to one engine being on whilst the other is off, etc.

Upvotes: 0

Andrew Monshizadeh
Andrew Monshizadeh

Reputation: 1794

Reviewing the documentation, the points you apply the impulses at must be in scene coordinates. What that means, is you need to determine the points in the coordinates of your sprite (probably coordinates like CGPoint(x: 0, y: 1) and CGPoint(x: 0, y: -1), but you may need to play with those), then convert those to scene coordinates using convertPoint(point, fromNode: ship).

Upvotes: 5

Related Questions