Search code examples
swiftsprite-kitgame-physicsskphysicsbodyskphysicsworld

How to make a force that doesn't accelerate objects constantly SpriteKit Swift


So I want to make a force that moves a physics object in the scene without accelerating them constantly. Just like in geometry dash. So when I use gravity it is like falling , but I don't want it to accelerate like that.

Hope you understood, this was a bit tricky to explain. Thank you


Solution

  • In SKPhysicsBody terminology this is the difference between a force (constantly applied) and an impulse (instantaneously applied):

    You can choose to apply either a force or an impulse:

    • A force is applied for a length of time based on the amount of simulation time that passes between when you apply the force and when the next frame of the simulation is processed. So, to apply a continuous force to an body, you need to make the appropriate method calls each time a new frame is processed. Forces are usually used for continuous effects

    • An impulse makes an instantaneous change to the body’s velocity that is independent of the amount of simulation time that has passed. Impulses are usually used for immediate changes to a body’s velocity.

    (From 'Making Physics Bodies Move' from Apple documentation of SKPhysicsBody.)

    So to apply an impulse to your body and thus avoid constant acceleration, use the method applyImpulse.