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?