[LibGDX][Box2D] What method of capping angular velocity/force

I am in the process of creating a basic Asteriods clone and wondering what is the best way to cap forces on the body.

Say I want to cap the ship speed to 1m/s, should I do a simple if statement to achieve this? I notice there is a way to set dampening but this gives less control but a nicer “feel” if that makes sense.

What way should I do it?

I do the following to cap my max speed:


Vector2 velocity = body.getLinearVelocity();
float speed = velocity.len();
if (speed > MAX_SPEED) {
    body.setLinearVelocity(velocity.scl(MAX_SPEED / speed));
}

Edit: Ah sorry, just have seen that you asked for angular velocity in the thread title. The angular velocity defines how fast an object rotates. Not sure if this is really what you want, but if so, you can do basically the same thing, as for the linear velocity. The check needs to take into account, that the velocity can also be negative, but other than that it’s the same logic.

I would want to cap both so your answer applies just fine :smiley:

So using the dampening is a bad idea?

I use the method described above to cap the speed. As I understand the damping is a way to slow bodies (or their rotation) over time. You can check out my LD29 game ( http://www.ludumdare.com/compo/ludum-dare-29/?action=preview&uid=7738 ). I used a very low damping value, so the cells in the game continue to drift in the same direction, even if the player stops pressing the direction keys. It creates quite a floaty feel to the game.

I’m getting to know box2d a bit better by building a pong game. There I also linear use damping, but for the paddles. The damping value is quite high. With this the player can release the direction keys and the paddle will still slide a bit further in the direction instead of stopping instantly.

So I think the damping is a very valuable tool in box2d. You have to try out what parameters work best (keep in mind that the strength of the damping might also be related to the mass. more mass => damping has less effect. I’m not sure about this but it would make sense).

But I don’t use the damping to limit the speed. I think it is the wrong tool for that.

Please take my advice with a grain of salt, because I just started using box2d some weeks ago.

And if you want to stop your bodies from rotating, then you can use body.setFixedRotation