Oh god this must be so simple. I have a heading in range (0, 2π) and two points from which I get the heading in between. I must compare them to see if one is within a range of the other. What I’ve got so far is.
//get the angle
float angle = atan(here.x - there.x, here.y - there.y);
//atan2 uses (-pi, pi) range, convert to (0, 2pi)
if(angle < 0) angle += 2*pi;
//subtract them pesky headings
float diff = angle - givenAngle;
//a difference of 350 degrees really is a difference of 10 degrees
if(diff > pi) diff = 2*pi - diff;
//a difference of -10 degrees really is a difference of 10 degrees
if(diff < 0) diff *= -1;
//check if the point is in range of givenAngle
if(diff > fov) do_magic(diff - fov);
However, I get all sorts of issues when both angles wrap around to zero and I’ve been wasting way too much brainpower in solving this solved problem.
Where am I doing it wrong? How can I find the difference between two headings correctly?
One of the main problems was in this line:
It was kind of tricky to notice, but that’s not the right argument order for
atan– even the mathematical definition on wikipedia takes theycomponent (sine) before thexcomponent (cosine).A second problem is that, as it turns out,
anglewas off by 180 degrees. In other words, instead of calculatinghere - there, I should be calculatingthere - here.Add in sverre’s observation about the incorrect order of operations, and we have something that works much better: