Skip to content

Instantly share code, notes, and snippets.

@bcrist
Last active March 10, 2016 14:31
Show Gist options
  • Save bcrist/3c15d27fd51d7c7c43b1 to your computer and use it in GitHub Desktop.
Save bcrist/3c15d27fd51d7c7c43b1 to your computer and use it in GitHub Desktop.
Limiting Velocity
MAXIMUM_VELOCITY = <any number>;
SQUARED_MAXIMUM_VELOCITY = MAXIMUM_VELOCITY * MAXIMUM_VELOCITY;
function animate(){
var squared_horizontal_velocity = (x_velocity * x_velocity) + (z_velocity * z_velocity);
if( squared_horizontal_velocity > SQUARED_MAXIMUM_VELOCITY ){
var velocity_magnitude = sqrt(squared_horizontal_velocity);
var scalar = MAXIMUM_VELOCITY / velocity_magnitude;
x_velocity = x_velocity * scalar;
z_velocity = z_velocity * scalar;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment