Game Development Reference
In-Depth Information
13. Repaint everything since we're about to change the state:
InvalidateRect(h, NULL, 1);
14. Recalculate everything using the time slice of 0.01 seconds:
OnTimer(0.01);
break;
As in Chapter 2 , Porting Common Libraries , the new OnTimer() callback function is
independent of the Windows or Android speciics.
How it works...
Now, when we have timer events generated for us, we may proceed to the calculation of rigid
bodies' positions. This is a somewhat complicated process of solving the equations of motion.
In simple terms, given current positions and orientations, we want to calculate new positions
and orientations of all the bodies in the scene:
positions_new = SomeFunction(positions_old, time_step);
In this pseudo code, the positions_new and positions_old are the arrays with new and
old rigid body positions and orientations, and time_step is the value in seconds, by which
we should advance our time counter. Typically, we need to update everything using the time
step of 0.05 of a second or lower, to ensure we calculate positions and orientations with
high enough accuracy. For each logical timer event, we may need to perform one or more
calculation steps. To that end, we introduce the TimeCounter variable and implement the
so-called time slicing:
const float TIME_STEP = 1.0f / 60.0f;
float TimeCounter = 0;
void OnTimer (float Delta)
{
g_ExecutionTime += Delta;
while (g_ExecutionTime > TIME_STEP)
{
Call the Box2D's method Step() to recalculate positions of rigid bodies and decrement the
time counter for one step:
g_World->Step(Delta);
g_ExecutionTime -= TIME_STEP;
}
}
 
Search WWH ::




Custom Search