# Maintain speed in box2D body

I want to delimit my speed and to maintain that speed constantly, but it gets getting higher and if i apply the linear velocity it doesnt bounce :/, Here is te part of my code

double fx = .25f*Math.cos(Math.toRadians(rotation));
double fy = .25f*Math.sin(Math.toRadians(rotation));
this.getBody().applyLinearImpulse(World.toGameCoordinates((float)fx, (float)fy), this.bodydef.position);
this.getBody().setLinearVelocity(World.toGameCoordinates((float)fx, (float)fy));


What can i do to maintain the speed constant and it keeps bouncing

• I'm confused at to what you are trying to achieve. Do you want to set a max velocity for a body? – ClassicThunder Feb 24 '12 at 22:45
• It was my error, i was calling the method in the render, and thats why the speed was increasing. – Rudy_TM Feb 25 '12 at 0:02

## 1 Answer

I am assuming that your code above is in an update loop (or in a callback)

In a physics engine you should never use anything similar to set. This is because the solver can no longer give you proper collisions (especially for a Continuous collision solver like Box2D). So your line:

this.getBody().setLinearVelocity(World.toGameCoordinates((float)fx, (float)fy));


Should not be there. That sets the velocity immediately and the ship will essentially teleport to the new position. As far as maintaining the current velocity goes, if you don't have any drag and you are not losing any energy in collisions, then you should never lose your initial velocity.

• Yes it was in a loop, i remove it from there and problem resolved, thanks. – Rudy_TM Feb 25 '12 at 0:03