O'Reilly logo

LibGDX Game Development Essentials by Juwal Bose

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Handling collisions

By default, the Box2D world is simulating the right response for our game at this point. The plane will bounce and tumble all over the place if it collides with meteor rocks or pillars. Such a response would have been excellent if our game was Angry Birds or Cut the Rope. If we think about it such a response is fine as our game will end on such a collision. In order to detect collisions, we will use ContactListener. In the initPhysics method, we add the code in the endContact method as follows:

world.setContactListener(new ContactListener() { @Override public void endContact (Contact contact) { bodyA=contact.getFixtureA().getBody(); bodyB=contact.getFixtureB().getBody(); boolean planeFound=false; if(bodyA.equals(planeBody)){ ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required