How to limit object speed?

I'am trying to make an Air Disc game, but the disc get too fast! How can i limit its speed?

Thanks
This code works good


//onUpdate.. ( step )
if (s is Disco) {
var v:b2Vec2 = body.GetLinearVelocity().Copy();
if (v.Length() > 8) {
v.Multiply(.8);
}
body.SetLinearVelocity(v);
}

but i think its not the best way.. .any suggestions?
Try to use angular damping? It's more realistic 🙂
Quote:
if (s is Disco) {
var v:b2Vec2 = body.GetLinearVelocity().Copy();
if (v.Length() > 8) {
v.Multiply(.8);
}
body.SetLinearVelocity(v);
}



if you are willing to treat max velocity as X and Y separately,
b2Vec2 has a built in function MaxV, which you would call on enter frame sending it a b2Vec2, with appropriate x and y

or you could do the function your self i suppose, MaxV is a simple (but condenced) if statement to either use the speed that exsists or the max speed
x = x > b.x ? x : b.x;
y = y > b.y ? y : b.y;

to apply the same sort of formula to both X and Y combined velocity, would be something like this.
assuming that you wanted to limit it to keep going at the max speed after it reaches the max speed, this would be most accurate.

Code:
var v:b2Vec2 = body.GetLinearVelocity().Copy();
var maxSpeed:Number = 8
if (v.Length() > 8) {
var percentage = 8/v.Length();
v.x *= percentage
v.y *= percentage
body.SetLinearVelocity(v);
}


what i find interesting is that you use .Copy();
it doesn't seem necessary in this case.
so if you are looking for a bit more speed, consider taking that off.

if that's preventing what i think its preventing, you may not need SetLinearVelocity(v), once Copy() is removed.
seems like the reason to copy the variable is to not edit the original.

good luck.
one thing i just noticed on this topic

Code:
public function MinV(b:b2Vec2) : void
{
   x = x < b.x ? x : b.x;
   y = y < b.y ? y : b.y;
}
   
public function MaxV(b:b2Vec2) : void
{
   x = x > b.x ? x : b.x;
   y = y > b.y ? y : b.y;
}


doesn't use absolute value.

in most games i make i program Max-Velocity, as the same positive or negative as a speed limit
and Min-Velocity as preventing values that continuously approach 0 (like with friction), and appear to drift at near 0, so for example
Code:
if( Math.abs(xvel)<2 ){ 
   xvel=0
}


but in b2Vec2.as MaxV and MinV do the same speed limiting function for separate directions, its not often a game needs these separated.
just thought it was interesting. I'm sure they have their reasons.
for another speed boost to the equation you are using.

use LengthSquared > 64
instead of Length > 8

this will dodge an unnecessary Math.sqrt()

even if this isn't hard coded in the future

var _maxSpeed:int = 8
LengthSquared > _maxSpeed*_maxSpeed

would still be faster than a Math.sqrt() i think.

Original thread: https://box2d.org/forum/viewtopic.php?t=2611