Game Development Reference
In-Depth Information
So, in BeginContact , we track the collisions between balls and pockets. But we also
track collision between balls. In the first case, the balls are turned invisible when they
touch the pockets. And, in the second case, we play a sound effect whenever two balls
touch each other, but only if they are at a certain speed (we determine that through a
b2Sprite helper method that retrieves the squared magnitude of a sprite's velocity vec-
tor).
The other method in our listener is:
void CollisionListener::PreSolve(b2Contact* contact, const
b2Manifold* oldManifold) {
b2Body * bodyA = contact->GetFixtureA()->GetBody();
b2Body * bodyB = contact->GetFixtureB()->GetBody();
b2Sprite * spriteA = (b2Sprite *) bodyA->GetUserData();
b2Sprite * spriteB = (b2Sprite *) bodyB->GetUserData();
if (spriteA && spriteB) {
//track collision between player and cue ball
if (spriteA->getType() == kSpriteCue &&
spriteA->mag() > 2) {
if (spriteB->getType() == kSpritePlayer &&
spriteA->isVisible()) {
SimpleAudioEngine::getInstance()->playEffect("hit.wav");
spriteA->setVisible(false);
spriteB->getGame()->setCanShoot(false);
}
} else if (spriteB->getType() == kSpriteCue &&
spriteA->mag()> 2) {
if (spriteA->getType() == kSpritePlayer &&
spriteB->isVisible()) {
SimpleAudioEngine::getInstance()->playEffect("hit.wav");
spriteB->setVisible(false);
spriteA->getGame()->setCanShoot(false);
Search WWH ::




Custom Search