Not sure if this belongs here or on the Math Exchange site; it's probably a simple math concept that I'm missing.
I have a 2D sprite with a rotation (let's call it A), and I want to slowly rotate it until it's at a new rotation (B). Depending on where A is, I'll rotate right or left, which ever gets me to B faster. A is constantly changing as it rotates towards B, and B is also able to change.
I'm currently taking the difference of B - A, and rotating left (CCW) if it's positive, and rotating right (CW) if it's negative. This almost works, but breaks when the target crosses the boundary at 0/2π and changes signs.
In the first image, you'll see how my current logic works as expected:
B - A ≈ 5.76 - 5.23 = 0.53 = turn CCW.
But, if B continues to rotate left, it will rotate past 2π and result in the sprite turning CW, which is slower than turning CCW:
B - A ≈ 0.17 - 5.50 = -5.33 = turn CW.
Any ideas would be appreciated, thanks!
NOTES:
- I'm using Bevy + Rust, but this seems like a generalized math question so not sure if that's important
- I've tried this with angles ranging from [-π, π] (instead of [0, 2π]), but it has the same issue when the angle crosses the boundary between π and -π.