How do I get a body to bounce around the screen wh

2019-08-16 07:33发布

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?

2条回答
Lonely孤独者°
2楼-- · 2019-08-16 07:42

The collisions in Box2D are never really elastic, there are some little errors in computations that tend to pile up and the body loses energy. There are two approaches that come to mind.

a) You can set the elasticity a little bit above 1

b) You can monitor the body's kinetic energy and if it drops below a certain threshold, give it a little nudge

查看更多
爷、活的狠高调
3楼-- · 2019-08-16 07:48

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)?

查看更多
登录 后发表回答