Skip to content

Instantly share code, notes, and snippets.

@glowcoil
Last active August 3, 2016 13:53
Show Gist options
  • Save glowcoil/71547d4577ced78b1f99026cb8cfc3b8 to your computer and use it in GitHub Desktop.
Save glowcoil/71547d4577ced78b1f99026cb8cfc3b8 to your computer and use it in GitHub Desktop.
// motion_add(dir, spd):
vx += spd * cos(dir);
vy -= spd * sin(dir);
// setting speed to spd:
var current_speed;
current_speed = sqrt(vx*vx + vy*vy);
vx *= spd / current_speed;
vy *= spd / current_speed;
// friction:
var current_speed;
current_speed = sqrt(vx*vx + vy*vy);
if (current_speed > fric) {
vx *= (current_speed - fric) / current_speed;
vy *= (current_speed - fric) / current_speed;
} else if (current_speed < -fric) {
vx *= (current_speed + fric) / current_speed;
vy *= (current_speed + fric) / current_speed;
} else {
vx = 0;
vy = 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment