Game Development Reference
In-Depth Information
Meeting the bodies
The b2Body object is the thing you'll spend most of your time dealing with inside a
Box2D simulation. You have three main types of b2Bodies : dynamic, static, and kin-
ematic. The first two are of greater importance and are the ones we'll use in our game.
Bodies are created by combining a body definition with a body fixture. The body definition
is a structure that holds information about type, position, velocity, and angle, among other
things. The fixture holds information about the shape, including its density, elasticity, and
friction.
So, to create a circle that is 40 pixels wide, you would use the following:
b2BodyDef bodyDef;
bodyDef.type = b2_dynamicBody;
//or make it static bodyDef.type = b2_staticBody;
b2Body * body = world->CreateBody(&bodyDef);
//create circle shape
b2CircleShape circle;
circle.m_radius = 20.0/PTM_RATIO;
//define fixture
b2FixtureDef fixtureDef;
fixtureDef.shape = &circle;
fixtureDef.density = 1;
fixtureDef.restitution = 0.7;
fixtureDef.friction = 0.4;
body->CreateFixture(&fixtureDef);
To create a box that is 40 pixels wide, you would use this:
//create body
b2BodyDef bodyDef;
bodyDef.type = b2_dynamicBody;
b2Body * body = world->CreateBody(&bodyDef);
//define shape
Search WWH ::




Custom Search