I am trying to scale some x and y velocity values to be between -MAX and MAX and maintain their proportions. The numbers can be negative, zero, or positive. This is being used to enforce a speed limit on x and y velocities. Here’s what I’ve got:
if(abs(velocities.x) <= MAX_TRANSLATIONAL_VELOCITY && abs(velocities.y) <= MAX_TRANSLATIONAL_VELOCITY)
return;
float higher = max(abs(velocities.x), abs(velocities.y));
velocities.x = (velocities.x / higher) * MAX_TRANSLATIONAL_VELOCITY;
velocities.y = (velocities.y / higher) * MAX_TRANSLATIONAL_VELOCITY;
This is not really working and the robots I’m applying it to are kind of spazzing out. Is there a standard way to accomplish this?
Thanks.
To normalize a vector you shouldn’t divide its components by the maximum of any of them but by their magnitude which is the euclidean norm of the vector.
Actually you shouldn’t check a single component, first you calculate magnitude, then if it’s over MAX_MAGNITUDE, you normalize the vector and multiply it by MAX_MAGNITUDE.