Skip to content

Instantly share code, notes, and snippets.

@brunokruse
Forked from shaunlebron/angleLerp.js
Created March 20, 2016 02:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brunokruse/8d0fd2f2f16dbb8c9b0f to your computer and use it in GitHub Desktop.
Save brunokruse/8d0fd2f2f16dbb8c9b0f to your computer and use it in GitHub Desktop.
The best way to interpolate 2D angles
/*
2D Angle Interpolation (shortest distance)
Parameters:
a0 = start angle
a1 = end angle
t = interpolation factor (0.0=start, 1.0=end)
Benefits:
1. Angles do NOT need to be normalized.
2. Implementation is portable, regardless of how the modulo "%" operator outputs sign (i.e. Python, Ruby, Javascript)
3. Very easy to remember.
Thanks to Trey Wilson for the closed-form solution for shortAngleDist!
*/
function shortAngleDist(a0,a1) {
var max = Math.PI*2;
var da = (a1 - a0) % max;
return 2*da % max - da;
}
function angleLerp(a0,a1,t) {
return a0 + shortAngleDist(a0,a1)*t;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment