Search code examples
andenginegravityjbox2d

How do I get a body to bounce around the screen when there is gravity (Box2D/AndEngine)?


I've done quite a bit of searching and have found multiple suggestions for anti-gravity objects, but nothing seems to work just right. I have a physics world created with Earth's gravity. The user taps the screen and creates sprites/dynamic bodies. These then fall to the bottom of the screen due to the gravity. I have another sprite/body that is supposed to bounce around the screen, ignoring gravity and bouncing off the sprites that have been created. AndEngine's Box2D does not seem to have setGravityScale() so I can't set gravity to 0. The problem with creating fixtures attached to a kinematic body to determine which side is colliding is that the sprite is a circle (which isn't a big deal) and I don't quite understand how they work. I saw that I might be able to create a dynamic body with a constant force equal to body.getMass()*SensorManager.Earth in the -y direction and set the elasticity to 1, but there is no way to get a perfectly elastic collision. Anyone have any perfect suggestions?


Solution

  • JohnEye was correct, just not quite as informative as I liked. In a similar question, I answered this: How do I apply a force to a body in the direction it is traveling (Box2D)?