Working with collisions
Causing an effect to occur from the collisions between bodies, whether it is the playing of sound or the disposal of a body, is often a necessary part of a game based on a physics simulation. Handling collisions seems like an intimidating task at first, but it will become second nature after we learn how each part of the ContactListener
interface functions. In this recipe, we will demonstrate how to handle collisions between fixtures.
Getting ready...
Follow the steps in the Introduction to the Box2D physics extension section at the beginning of the chapter to create a new activity that will facilitate the creation of our simulation in which we will control collision behavior.
How to do it...
Follow these steps to demonstrate our control of collisions:
Place the following definitions at the beginning of the activity class:
public Rectangle dynamicRect; public Rectangle staticRect; public Body dynamicBody; public Body staticBody; public boolean setFullAlphaForDynamicBody...