Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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

share|improve this question
    
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 1

up vote 1 down vote accepted

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.

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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