Last active
March 10, 2016 14:31
-
-
Save bcrist/3c15d27fd51d7c7c43b1 to your computer and use it in GitHub Desktop.
Limiting Velocity
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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