Graphics Reference
In-Depth Information
//update all the shapes
cpSpaceEachShape ( self . space , & updateShape , NULL );
}
Avoiding the Spiral of Death
One thing you must ensure when using fixed simulation time steps is that the real-world
time taken to perform the physics calculations does not exceed the simulated time step. In
our example, we've chosen the arbitrary time step of 1/120th of a second for our physics
simulation. Chipmunk is fast and our example is simple, so our cpSpaceStep() should
complete well within that time and is unlikely to delay our frame update.
But suppose we had a more complex scene with hundreds of objects all interacting: The
physics calculations would become more complex and the cpSpaceStep() might actually
take more than 1/120th of a second to complete. We aren't measuring the time for our
physics step because we are assuming that it will be trivial compared to the frame update,
but if our simulation steps take longer to execute than the time period they are simulating,
they will delay the frame update.
It gets worse: If the frame takes longer to update, our simulation will need to perform more
steps to keep the simulated time in sync with real time. Those additional steps will then
delay the frame update further, and so on. This is known as the spiral of death because the
result is that the frame rate becomes slower and slower until the application effectively
freezes.
We could add code to measure the real-world time for our physics step on a given device
and adjust the fixed time step automatically, but in practice this is probably overkill. Just
ensure that you leave a generous margin for error and test on the slowest device you intend
to support. If the physics calculations take more than ~50% of the simulated time, consider
increasing your simulation time step (or simplifying your scene). If your simulation time
step increases to the point that it takes more than one-sixtieth of a second (an entire screen
frame update), you will need to reduce your animation frame rate to 30 frames per second
or less by increasing the CADisplayLink frameInterval so that you don't start randomly
skipping frames, which will make your animation look unsmooth.
Summary
In this chapter, you learned how to create animations on a frame-by-frame basis using a
timer combined with a variety of animation techniques including easing, physics
simulation, and user input (via the accelerometer).
In Part III, we look at how animation performance is impacted by the constraints of the
hardware, and learn how to tune our code to get the best frame rate possible.
 
Search WWH ::




Custom Search