Andengine - Always keep the same speed of the body

≡放荡痞女 提交于 2019-12-12 04:33:21

问题


I am developing a game where you are on side scroll on screen middle (player) and enemyes appears from left or right continusly.

I create a enemy, set a Linearvelocity to right (or left) on this way: getBody.setLinearVelocity(v*this.getDireccion(), 0);

Then, when the enemy collides with another enemy or player, sometimes change direction (he slides back) or get more slow or fast. I need that always have the same LinearVelocity, and if this enemy is colliding with a player or another monster, dont stop, dont slow, dont fast... same velocity and direction.

Anybody can help me with this? Sorry my english.


回答1:


FixtureDef FIXDEF = PhysicsFactory.createFixtureDef(final float pDensity, final float pElasticity, final float pFriction);

1) To avoid "change direction" set friction to 0.

2) To get always the same LinearVelocity, set Elasticity to 1, and after that in your game loop, check if velocity is changed, than change it back to needed. Or you can use "applyForce", you can see an applyForce example here

P.S. You can use something like that .createFixtureDef(1.0f, 1.0f, 0.0f)

I had similar problem. I needed ball to bounce back to same height. What I did? I remembered previous velocity. And when ball hits the ground (and velocity was changed). I set previous remembered velocity (but with opposite sign prevVel *(-1)), which was before bouncing.




回答2:


How about create the ContactListener, and use the postSolve callback method to reset your body velocity.
After collision, Box2D will generate impulse to avoid fixtures overlap, that's why the speed of your body is changed. Here is a good tutorials describe the collision, hope this help.



来源:https://stackoverflow.com/questions/12998860/andengine-always-keep-the-same-speed-of-the-body

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