Search code examples
swiftskspritenodeskphysicsbody

How can I get the trajectory of a SKSpriteNode's physicsBody?


How can I get the trajectory (of which I will derive a point) of a moving SKSpriteNode ? Would I use its physicsBody.velocity value ? Or is there another variable I can use ? Basically, I'm trying to get a few points that the moving node will pass through.

Thanks!


Solution

  • If you are using physics to move it you should be able to use the velocity value of the physics body plus the position of the node it is attached to. If you want to get points on the same line as the velocity vector, you take the x position and the y position, and you add t * velocity.dx to the x position, and t * velocity.dy to the y position where t is just an arbitrary time when the node would reach that point.

    Probably worth noting that this is the point velocity of the node. If it is set up to move in a path other than a straight line, such as a parabolic arc, this will take the straight line velocity at the given point, and return points on that line, as opposed to lines on the parabolic arc that it will travel. If you want points on a complex path that it will travel due to many different or changing forces, you will need to model those forces into an equation and plug your t into that.