
Box2D is an Open Source 2D physics library for game makers. This library take care about friction, gravity, body mass, colissions and much more. Using this library you can create your own phisics based games, shooting games like Angry Birds, for example.
I will show you how it works on the small, basic example, writting with Qt. Why Qt - because this is a crossplatform library, write once use anywere, said on Qt main page. And this is a true.
Here is you can download Box2D library for your platform.
Let see how it works. First we need to create the "world".
b2World* m_Box2DWorld;// Gravity vector. X and Y gravity could be setup here
b2Vec2 gravity(0.0f, -10.0f);// Do we want to let bodies sleep? This is CPU saving.
bool doSleep = true;// Create the world
m_Box2DWorld = new b2World(gravity, doSleep);
World is done. Now we need to add some game objects. Let add the ground object. This object will be a static object (we do not need to move it or recalculate it position).
// Gound body definition
b2BodyDef groundBodyDef;
// Ground position, in this example - the bottom part of the screen
groundBodyDef.position.Set(0.0f, 0.0f);// World is create the body, you do not need to care about memory allocation
// for Box2D objects, library takes care about it
b2Body* groundBody = m_Box2DWorld->CreateBody(&groundBodyDef);// Every body must be linked to some shape
// in this case this is a rectangle
b2PolygonShape groundBox;
groundBox.SetAsBox(640.0f, 20.0f);
// This fuction will create our share and link it
// to our body definition
groundBody->CreateFixture(&groundBox, 0.0f);
We have the ground, now lets add the cannon body
// Cannon
b2BodyDef cannonDef;
cannonDef.position.Set(40.0f, 45.0f);
// Rotation angle of cannon
cannonDef.angle = (-45 * (2 * PI)) / 360.0;b2Body* cannonBody = m_Box2DWorld->CreateBody(&cannonDef);
b2PolygonShape cannonBox;
cannonBox.SetAsBox(10.0f, 20.0f);
cannonBody->CreateFixture(&cannonBox, 0.0f);
All of the object above (ground and cannon) is the static objects. Lets add some dynamic objects. This will be a row of the 5 blocks.
for(int i = 0; i < 5; ++i)
{
b2BodyDef boxDef;
// Object is a dynamic object
boxDef.type = b2_dynamicBody;
boxDef.position.Set(400.0, 30.0f + i * 20);b2Body* boxBody = m_Box2DWorld->CreateBody(&boxDef);
b2PolygonShape boxBox;
boxBox.SetAsBox(10.0f, 10.0f);b2FixtureDef fixtureDef;
fixtureDef.shape = &boxBox;
// Mass of the object
fixtureDef.density = -0.1f;
// Friction powr of the object
// Object with high friction will slide more easily than
// object with less friction
fixtureDef.friction = 0.1f;boxBody->CreateFixture(&fixtureDef);
}
The world is done. Now we going to add the bullets. In the key down handler we will add code for shoot the bullets.
// Bullet
b2BodyDef bulletDef;
bulletDef.type = b2_dynamicBody;
bulletDef.position.Set(63.0f, 68.0f);b2Body* bulletBody = m_Box2DWorld->CreateBody(&bulletDef);
b2PolygonShape bulletBox;
bulletBox.SetAsBox(10.0f, 10.0f);b2FixtureDef fixtureDef;
fixtureDef.shape = &bulletBox;
fixtureDef.density = -0.9f;
fixtureDef.friction = 0.1f;bulletBody->CreateFixture(&fixtureDef);
// Power of the initial impulse
float power = 100.0f;
// Alngel of the impulse
float angle = (-45 * (2 * PI)) / 360.0;bulletBody->ApplyLinearImpulse(b2Vec2(cos(angle) * power, -sin(angle) * power), bulletBody->GetWorldCenter());
Use this sample app as a playground for your experiments. The potential of the Box2D library is a huge.
Want to see more examples? Check out the world construction kit. This is a really amazing. Or play Angry Birds game (available in the AppUp Store). Did you know - this game is uses Box2D library too.
All source code for this article available here.
Black Belt (App Development)
Comments
small typo on great article well done.
The worls is done.
to
The world is done.
Good Article.
Regards,
Nayan
Very cool! How was the library used for Angry Birds on iOS? As I understand it (and I'm not a mobile developer, so could just be my ignorance), you have to code in Objective C.
Seems to be a trend with more of these physics-based games appearing. There's a Wii game I have called Gravity, which is a fantastically entertaining problem solver using a gravity engine. the interactions have a very real feel to them. Also an Androind one call Space Physics.
@Don McIntosh Box2D has a number of ports for various platforms including iOS, Flash, C#
Interesting Post, learnt new things. Very informative
hi ,
i want to develop game like Angry Bird Using QML and BOX2D 2.2.1 lib can any one of you help me on this or provide me the sample example code for it ,so i will very thank full to you
well I have been looking box2d with respect to Windows Phone support and its really cool to get some more info on that here.
I can use it in my project now.
Post new comment