Game Development Reference
In-Depth Information
Creating the ball objects
In the game, there is a class called Ball that extends b2Sprite , used for both the target
balls and the cue ball. These objects are also created inside the initPhysics method.
Here is the basic configuration of that object:
//create Box2D body
b2BodyDef bodyDef;
bodyDef.type = b2_dynamicBody;
_body = _game->getWorld()->CreateBody(&bodyDef);
_body->SetLinearDamping(1.2f);
_body->SetAngularDamping(0.2f);
//create circle shape
b2CircleShape circle;
circle.m_radius = BALL_RADIUS/PTM_RATIO;
//define fixture
b2FixtureDef fixtureDef;
fixtureDef.shape = &circle;
fixtureDef.density = 5;
fixtureDef.restitution = 0.7f;
//add collision filters so only white ball can be hit by cue
if (_type == kSpriteBall) {
fixtureDef.filter.categoryBits = 0x0010;
} else if (_type == kSpritePlayer) {
//white ball is tracked as bullet by simulation
_body->SetBullet(true);
fixtureDef.filter.categoryBits = 0x0100;
}
//set sprite texture
switch (_color) {
case kColorBlack:
this->initWithSpriteFrameName("ball_black.png");
break;
Search WWH ::




Custom Search