Spin the bottle: Comparing 2 Euler angles

257 Views Asked by At

Angle A is a Euler angle that keeps increasing by increments and Angle B is the stopping point of Angle A (think 'Spin-the-bottle' where Angle A is the current angle of the bottle Angle B is the angle needed to point to your crush and the bottle spins at a fixed rate clock-wise). To do this, I've been trying to compare Angle A and Angle B with inequalities. If Angle A is 60 degrees and Angle B is 80 degrees, this is no problem. If Angle A is 350 degrees and Angle B is 10 degrees however, the inequality thinks that Angle A has crossed Angle B even though it hasn't. How do I compare angles when they wraparound at 360 degrees?

This is some pseudocode for what I have so far:

int AngleB = 30;

int AngleA = 300;

void Update() {

AngleA += 13;

if (AngleA > AngleB) {

AngleA = AngleB;

}

}

1

There are 1 best solutions below

3
On BEST ANSWER

Here's a kind of generic solution:

while (a < (b-180)) {
   a = a + 360;
}
a = a - 360;
while ((b-180) > a) {
   b = b - 360;
}
b = b + 360

If you run this, you'll end up with an angle $a$, and an angle $b$ that's numerically greater than $a$, but not by more than 360. It's not very efficient code if $a$ and $b$ differ by $100,000$ degrees, for instance, but for the cases you care about, it works surprisingly well in practice.

What do you do with these values? Well, you might say that if $b$ is more than 180 degrees above $a$, then $a$ is greater than $b$, but if $b$ is less than 180 degrees more than $a$, then $b$ is greater than $a$. if $b - a = 180$, all bets are off. :)