Created
September 16, 2012 00:46
-
-
Save akashkgarg/3730592 to your computer and use it in GitHub Desktop.
Physics time-step function 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 | |
{ | |
// 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); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment