Applying anti-gravity to a specific body
In the previous recipe, we looked at how forces affect bodies. Using a constant force that opposes gravity, we can release a body from the gravity of the physics world. If the force that opposes gravity is great enough, the body will even float away! In this recipe, we will create a body that counteracts the force of gravity.
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 a body that has a constant force applied that opposes gravity.
How to do it...
For this recipe, follow these steps to create a body that opposes gravity:
Place the following definitions in the activity:
Body gravityBody; Body antigravityBody; final FixtureDef boxFixtureDef = PhysicsFactory.createFixtureDef(2f, 0.5f, 0.9f);
Next, create a rectangle and body that will demonstrate the normal effects of gravity on a body. Place the following code...