applyForce(0, 400) - SpriteKit inconsistency

前端 未结 2 1514
长情又很酷
长情又很酷 2020-12-08 22:46

So I have an object that has a physicsBody and gravity affects it. It is also dynamic.

Currently, when the users touches the screen, I run the code:

applyFor

2条回答
  •  自闭症患者
    2020-12-08 23:02

    Suggested Solution

    If you're trying to implement a jump feature, I suggest you look at applyImpulse instead of applyForce. Here's the difference between the two, as described in the Sprite Kit Programming Guide:

    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.

    A jump is really an instantaneous change to a body's velocity, meaning that you should apply an impulse instead of a force. To use the applyImpulse: method, figure out the desired instantaneous change in velocity, multiply by the body's mass, and use that as the impulse parameter into the function. I think you'll see better results.

    Explanation for Unexpected Behavior

    If you're calling applyForce: outside of your update: function, what's happening is that your force is being multiplied by the amount of time passed between when you apply the force and when the next frame of the simulation is processed. This multiplier is not a constant, so you're seeing a different change in velocity every time you call applyForce: in this manner.

提交回复
热议问题