Game Development Reference
In-Depth Information
Creating the cue
The cue ball also extends b2Sprite , and its body is set as a box.
//create body
b2BodyDef bodyDef;
bodyDef.type = b2_dynamicBody;
_body = _game->getWorld()->CreateBody(&bodyDef);
_body->SetLinearDamping(8);
_body->SetAngularDamping(5);
//Define shape
b2PolygonShape box;
box.SetAsBox(BALL_RADIUS * 21 /PTM_RATIO, BALL_RADIUS * 0.2
/ PTM_RATIO);
//Define fixture
b2FixtureDef fixtureDef;
fixtureDef.shape = &box;
fixtureDef.filter.maskBits = 0x0100;
fixtureDef.density = 10;
fixtureDef.restitution = 1;
_body->CreateFixture(&fixtureDef);
_body->SetUserData(this);
It has very high damping values because, in the rare occasions when the player misses the
cue ball, the cue will not fly off the screen but halt a few pixels from the white ball.
If we wanted to create the cue ball as a trapezium or a triangle, we would need to give the
b2PolygonShape option the vertices we want. Here's an example of this:
b2Vec2 vertices[3];
vertices[0].Set(0.0f, 0.0f);
vertices[1].Set(1.0f, 0.0f);
vertices[2].Set(0.0f, 1.0f);
int32 count = 3;
Search WWH ::




Custom Search