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.
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.
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.