1
\$\begingroup\$

I've tried, but I cannot understand angle_difference.

I have an object, oTurretDetTurret, which turns to face its Target, which lies at the angle DirectionOfTarget from it.

It does this by updating its direction thus:

direction = direction + SpinDirection

SpinDirection can be either +3 or -3, meaning it can turn in either direction at a rate of 3 degrees per step.

What determines the direction it turns in is the angle between its current direction and the `DirectionToTarget. I want the turret to turn in the direction with the shortest number of degrees to travel.

I've been using this:

if angle_difference(direction, DirectionToTarget) > 0 {
direction = direction + 3
} else if angle_difference(direction, DirectionToTarget <= 0 {
direction = direction - 3
}

However, in game this just means that the turret faces the opposite direction to its target at all times, oscillating back and forth slightly.

In my understanding, angle_difference returns the smaller of the two angles between two directions.

For example, if the turret's direction is 300 degrees and the DirectionToTarget is 90 degrees, angle_difference should return +150, the smaller of the two angles, and thus my above code should move the turret counter-clockwise towards 90 degrees.

Can anybody help me?

\$\endgroup\$

2 Answers 2

1
\$\begingroup\$

angle_difference is calculated with atan2 function which returns -PI to +PI (-180 deg to +180 deg). That is useful because if angle_difference > 0 you know it is clockwise from you and < 0 counterclockwise. your else if statement with <= should only be <, which is causing your oscillation.

\$\endgroup\$
2
  • \$\begingroup\$ Thank you very much! I'm so used to using <- in Gamemaker to cover the boundary number itself, not just less or greater than it, that I didn't consider that angle_difference would not return 0. The other issue in my code is that I had the +3 and -3 in the wrong order, as illuminated by your answer. So now if angle_difference > 0 , the direction changes by -3 (moving clockwise) and if < 0 it changes by +3 (anti-clockwise). Thank you for the explanation! \$\endgroup\$ Jul 9, 2016 at 7:52
  • \$\begingroup\$ You are welcome. Happy coding! \$\endgroup\$
    – Chris
    Jul 11, 2016 at 2:09
0
\$\begingroup\$

I think probably your directToTarget is off by 180 degrees somehow. Maybe post that code. That could explain why your turret is always pointing away from the target. The next thing to consider is the oscillation. This is probably happening because you increment by 3 even if the difference between the angle and the desired angle is only 1 - so you'll get some overshoot over and over again. So instead, write in some logic that basically says if difference is more than 3, increment by 3, else increment by the difference

\$\endgroup\$
1
  • \$\begingroup\$ Thank you for taking the time to answer: I did think that my DirectionToTarget was off by 180 as you say, but in the end it turned out to be because I included 0 in the angle_difference calculation, which it seems not to count. As for the increment, I tested it with +1 and -1 and it didn't seem to make a difference, and the code now works with any increment I choose. \$\endgroup\$ Jul 9, 2016 at 7:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .