Game Development Reference
In-Depth Information
Setting up Box2D simulations
Box2D is a pure C++ library with no dependencies on the CPU architecture, so a simple
makefile and Android.mk script, similar to those found in the previous sections, would
sufice to build the library. Using the techniques described in the previous section, we set up a
simulation. We also have the frame buffer from the previous chapter, and we only render the
boxes using 2D lines.
Getting ready
As a bonus, Erin Catto—the library author—provides a simpliied version of Box2D. Once you
are happy with just the boxes available, you can restrict yourself to using the BoxLite version.
Download the most recent source code from the library home page: http://box2d.org .
How to do it...
1. To start with Box2D, we adapt the standard sample for a slightly modiied BoxLite
version, which is included in this topic's materials. First, we declare the global
World object:
World* g_World = NULL;
2.
Initialize it at the end of the OnStartup() routine:
g_World = new World(Vec2(0,0), 10);
Setup(g_World);
3.
The OnTimer() callback (those used in the previous recipes) updates the g_World
object using the TIME_STEP constant by calling the Step() method.
4.
The OnDrawFrame() callback passes the parameters of each body to the
DrawBody() function, which renders the body bounding box:
void OnDrawFrame()
{
Clear(0xFFFFFF);
for (auto b = g_World->bodies.begin();
b !=g_World->bodies.end(); b++ )
{
DrawBody(*b);
}
5.
Render each joint:
for ( auto j = g_World->joints.begin() ;
j != g_World->joints.end() ; j++ )
{
DrawJoint(*j);
}
 
Search WWH ::




Custom Search