1 / 19

Collision handling

Collision handling. If you want to receive collision callbacks, you need to subclass b2ContactListener (in C++, rename implementation file to .mm) BeginContact and EndContact will be called automatically; they are a good place to implement game logic, sounds, ..

lesley
Download Presentation

Collision handling

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Collision handling • If you want to receive collision callbacks, you need to subclass b2ContactListener (in C++, rename implementation file to .mm) • BeginContact and EndContact will be called automatically; they are a good place to implement game logic, sounds, .. • Note: you cannot create or destroy Box2D entities inside these methods

  2. Collision handling # import “Box2D.h” class ContactListener : public b2ContactListener { // … }; // do not forget ; here

  3. Collision ! (BeginContact) • When a collision between 2 bodies is detected, BeginContact is called • void BeginContact( b2Contact *contact); • Override it if you want to execute some code at that time

  4. Collision ! (BeginContact) • In ContactListener.mm void ContactListener::BeginContact( b2Contact *contact ) { // update sprites, game logic, play sound, … }

  5. Collision ! (BeginContact) • The class b2Contact encapsulates a contact between 2 (and only 2) entities, A and B • GetFixtureA( ) and GetFixtureB( ) will give you references to the 2 fixtures • Then you can call GetBody( ) to have references to the 2 bodies • Then you can call GetUserData( ) to get references to the 2 sprites

  6. Sprites that collided b2Body *bodyA = contact -> GetFixtureA( ) -> GetBody( ); b2Body *bodyB = contact -> GetFixtureB( ) -> GetBody( ); CCSprite *spriteA = (CCSprite *) bodyA -> GetUserData( ); CCSprite *spriteB = (CCSprite *) bodyB -> GetUserData( );

  7. Sprites that collided if( spriteA != NULL && spriteB != NULL ) { // color sprites in red spriteA.color = ccMAGENTA; spriteB.color = ccMAGENTA; }

  8. Sprites that collided • What if we want to know which sprite is what? • One could be a bullet, the other could be an enemy can tag the sprites and check the tag property for that

  9. End of collision (EndContact) • When a collision between 2 bodies ends, EndContact is called • void EndContact( b2Contact *contact); • Override it if you want to execute some code at that time

  10. Using ContactListener • We need to do 2 things in the layer class • Instantiate a ContactListener object • Set the contact listener of the world to be that object

  11. Using ContactListener • In .h file #import “ContactListener” // ContactListener instance variable ContactListener *contactListener;

  12. Using ContactListener • In .mm file contactListener = new ContactListener( ); world->SetContactListener( contactListener ); • And inside dealloc delete contactListener;

  13. Collision ! (BeginContact) • We could also play a sound when 2 sprites collide • We could also have a state variable so that we play a sound on the first collision between 2 sprites, but not the secondary collisions • Since we can get the bodies, we could retrieve the linear velocity, and depending on its value, adjust the intensity of the sound

  14. Collision ! A vs B • Does A (or B ) have a dynamic or static body? int typeOfBodyA = bodyA -> GetFixtureList( )->GetType( ); • // 1  static body • // 2 dynamic body

  15. Collision ! A vs B • If A and B both have a dynamic body, then what are they? Spider? Ship? Bullet? Player? … • We can retrieve the sprite based on the body • If the sprite has been tagged earlier, we can retrieve the tag, that can identify the type of sprite

  16. Collision ! A vs B • We will tag the blocks to identify them • 4 blocks: A, B, C, D •  4 different tags

  17. Collision ! A vs B

  18. Collision ! A vs B • Convert idx idy binary to decimal • int tag = 2 * idx + idy; •  0, 1, 2, or 3

  19. Collision ! A vs B CCSprite * spriteA = ( CCSprite *) bodyA -> GetUserData( ); // could be null if the body is static if( spriteA != NULL ) int tagOfA = spriteA.tag; • Now we can decide what to do baed on the tag value

More Related