Skip to content

Instantly share code, notes, and snippets.

@akashkgarg
Created September 16, 2012 00:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akashkgarg/3730592 to your computer and use it in GitHub Desktop.
Save akashkgarg/3730592 to your computer and use it in GitHub Desktop.
Physics time-step function in Flick a' Fruit
- (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