Created
September 16, 2012 15:44
-
-
Save akashkgarg/3732906 to your computer and use it in GitHub Desktop.
Avoiding the Spiral of Death in Flick a' Fruit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (void) step:(ccTime)elapsedTime | |
{ | |
// Fix the time elapsed to avoid the spiral of death | |
if (elapsedTime > 0.2) | |
elapsedTime = 0.2 | |
// Do physics time-stepping. | |
_dtAccumulator += elapsedTime; | |
while (_dtAccumulator >= _dt) { | |
// Take the actual physics step with step-size _dt | |
cpSpaceStep(_space, _dt); | |
_dtAccumulator -= _dt; | |
} | |
// Note that accumulator will not be zero at the end of this, it will | |
// most likely be < FIXED_TIMESTEP. This indicates that the Physics is | |
// ahead of the rendered frame. To fix this, we need to interpolate | |
// the sprites for each object between their previous positions (before the | |
// timestep) and their current position based on the fractional value | |
// stored in the accumulator. This is only for display purposes so that we | |
// don't get temporal aliasing. | |
float alpha = _dtAccumulator / _dt; | |
// The following function will for each physical object, | |
// interpolate the position of the sprite based on: | |
// previousPhysicsPosition * (1 - alpha) + currentPhysicsPosition * (alpha) | |
cpSpaceEachBody(_space, interpolateSprites, &alpha); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (void) step:(ccTime)elapsedTime | |
{ | |
// Do physics time-stepping. | |
_dtAccumulator += elapsedTime; | |
// Avoid the spiral of death by fixing maximum steps to take. | |
int stepsTaken = 0; | |
while (_dtAccumulator >= _dt && stepsTaken++ < MAX_PHYSICS_STEPS) { | |
// Take the actual physics step with step-size _dt | |
cpSpaceStep(_space, _dt); | |
_dtAccumulator -= _dt; | |
} | |
// Note that accumulator will not be zero at the end of this, it will | |
// most likely be < FIXED_TIMESTEP. This indicates that the Physics is | |
// ahead of the rendered frame. To fix this, we need to interpolate | |
// the sprites for each object between their previous positions (before the | |
// timestep) and their current position based on the fractional value | |
// stored in the accumulator. This is only for display purposes so that we | |
// don't get temporal aliasing. | |
float alpha = _dtAccumulator / _dt; | |
// The following function will for each physical object, | |
// interpolate the position of the sprite based on: | |
// previousPhysicsPosition * (1 - alpha) + currentPhysicsPosition * (alpha) | |
cpSpaceEachBody(_space, interpolateSprites, &alpha); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment