Question

I have an animation using the spritekit physics world. I have been looking to see if there is a way to "fast forward" through the physics and just get the result or path without displaying the animation. I looked at physicsWorld and didn't see anything for this? Any ideas of how I could do this?

Basically I want to show the user the resulting location of the animation before they decide to execute.

Was it helpful?

Solution

Apparently you missed the speed property in SKPhysicsWorld:

The default value is 1.0, which means the simulation runs at normal speed. A value other than the default changes the rate at which time passes in the physics simulation. For example, a speed value of 2.0 indicates that time in the physics simulation passes twice as fast as the scene’s simulation time. A value of 0.0 pauses the physics simulation.

Keep in mind that this will affect all bodies, and there's no built-in way to reset the system to a previous state. Also the simulation has to advance by at least 1 step in order for the change to become visible, so you'd have to set the speed property to, say, 30 in the update or didEvaluateActions method and reset it back to 1 in didSimulatePhysics. That's also the method where you would check the result of the fast-forward step.

If the body travels at a fixed velocity and won't be affected by anything like collisions or gravity, you can use pure trigonometry (and won't need the speed property) to find where the body will be in n steps, or how many steps it will take the body to travel at least n distance.

I can't think of a way to advance until a body reaches a certain destination where the body doesn't travel at a fixed velocity and you have no easy way to predict the time of arrival without actually running the physics simulation. That would require being able to advance the world independently from the rest of the scene (and safeguarding with a time-out against the body never actually getting there), which would only be possible with a 3rd party physics engine. You can still use Box2D or Chipmunk with Sprite Kit though.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top