Game Development Reference
In-Depth Information
There's more...
We encourage the reader to ind some free fonts at http://www.1001freefonts.com ,
use the described FreeType font generator to create .bmp iles for those fonts and render the
string using the pre-rendered characters.
Implementing timing in physics
The rest of this chapter is dedicated to two physical simulation libraries, the Box2D (2D
simulation) and Open Dynamics Engine (3D simulations). Building these is not hard, so
we'll focus on making real use of them. The APIs of Box2D and ODE only provide functions
to calculate current positions of the rigid bodies in simulations. First of all, we have to call
the calculation routines. Then, we have to transform the bodies' physical coordinates into a
screen-related coordinate system. Connecting physical simulation with rendering and timing
is the main problem treated in this recipe.
Getting ready
Virtually, every rigid body physics library provides abstractions of the world, object (or body),
constraint (or joint), and shape. The world here is just a collection of bodies and joints
attached to bodies. Shapes deine how bodies collide.
To create a dynamic application based on the physical simulation, we have to be able to
render the physical scene at any moment in time. We also need to convert discrete timer
events into a seemingly continuous process of calculation of the bodies' positions.
Here, we give explanations about the timing and rendering, and then we provide a complete
sample using the Box2D library, the App4 .
How to do it...
1.
In order to animate everything on the screen, we need to set up a timer. In Android,
we perform time stepping as fast as possible, and on each iteration of our rendering
loop, we just call the GetSeconds() function and calculate the difference between
the previous and the current time. The code for GetSeconds() in the Wrappers_
Android.h ile uses the standard POSIX gettimeofday() function:
double GetSeconds()
{
2.
The coeficient to convert time from microseconds into seconds:
const unsigned usec_per_sec = 1000000;
 
Search WWH ::




Custom Search