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

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

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.

| improve this answer | |
  • \$\begingroup\$ Yes it was in a loop, i remove it from there and problem resolved, thanks. \$\endgroup\$ – Rudy_TM Feb 25 '12 at 0:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.