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

梦想与她 提交于 2019-12-11 16:22:01

问题


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?


回答1:


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




回答2:


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



来源:https://stackoverflow.com/questions/11257197/how-do-i-get-a-body-to-bounce-around-the-screen-when-there-is-gravity-box2d-and

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!