angle icon indicating copy to clipboard operation
angle copied to clipboard

shortestAngle: why adding start to delta?

Open mborecki opened this issue 4 years ago • 2 comments

https://github.com/davidfig/angle/blob/7984eb858db8c360a98bfa427ac85048af97d91a/index.ts#L112

Hi.

I tried to use your lib, but shortestAngle returns something streange. THere is error or I dont understand your intentions for this function.

mborecki avatar Jul 26 '21 15:07 mborecki

ShortestAngle finds the best angle to move from your start angle to your end angle. In effect it gives you a better end angle.

For example, if you have a start angle of 10deg and want to go to 350deg, then it will return -10deg. (As opposed to iterating from 10 through 350deg). I use it mostly in ease functions to choose the fastest way to get between two angles.

davidfig avatar Jul 26 '21 15:07 davidfig

I too found this confusing until I realized the angle it's returning is not the smallest difference between the two angles -- instead it's returning the target angle that is sort of "locally normalized" to be numerically closest to the starting angle. Indeed very useful for tweening object rotations.

To get the shortest difference, use differenceAngles. This will always be an absolute value though, so if you want it signed in the direction needed to go from A to B, multiply by the result of differenceAnglesSign. Or, just subtract your starting angle from the result of shortestAngle 🙃

vegeta897 avatar Oct 08 '21 03:10 vegeta897