Creating destructible objects
Using the impulse data from the postSolve()
method in the physics world's contact listener gives us a force of impact for each collision. Extending that data to cause a multiple-body object to break apart simply involves determining which body collided and if the force was great enough to break the body from the multiple-body object. In this recipe, we will demonstrate the creation of a destructible object made from bodies.
Getting ready...
Create an activity by following the steps in the Introduction to the Box2D physics extension section at the beginning of the chapter. This activity will facilitate the creation of the destructible body groups that we will use in this section.
How to do it...
Follow these steps to create a destructible object that breaks apart when it collides with a large force:
Add the following definitions to the activity class:
public Body box1Body; public Body box2Body; public Body box3Body; public boolean breakOffBox1 = false; public boolean...