The units in my game have two rotations, one actual and one target, in order to have smooth rotation transmissions. The angle spans from -180 to 180. In order to move from the real rotation to the target, I had a system of if's that added or subtracted from the real rotation based on which was greater. The problem is, if the unit starts at -179 and moves to 179 instead of just moving the 2 degrees, it moves all the way around the circle. I've been trying to think of a workaround, but everything just ends up being a huge system of inefficient if's. What would be the easiest way to make the units reach their new rotation as quickly as possible?
Thanks for your time.
Rotation moves to another rotation
What about:
?
or if for some reason you fear that values can get too far:
This would keep rotation in 0-360 range.
Code: Select all
if(rotation < 0) rotation +=360;
if(rotation >= 360) rotation -= 360;or if for some reason you fear that values can get too far:
Code: Select all
while(rotation < 0) rotation +=360;
while(rotation >= 360) rotation -= 360;Code: Select all
CurrentAngle_converted=CurrentAngle+180; // set your angle system to range from 0-360
GoalAngle_converted=GoalAngle+180; // same for the goal angle
now, if the currentangle is -179 ( 1, converted )
and the goal angle is 179, ( 359, converted )
DeltaAngleRotateRight=fabs(CurrentAngle_converted-GoalAngle_converted); // gives 1-359=358
DeltaAngleRotateLeft=360-GoalAngle_converted; // gives 360-359=1
if(DeltaAngleRotateRight<DeltaAngleRotateLeft)
{
CurrentAngle_converted++;
if(CurrentAngle_converted>360)CurrentAngle_converted=0;
}
else
{
CurrentAngle_converted--;
if(CurrentAngle_converted<0)CurrentAngle_converted=360;
}
CurrentAngle=CurrentAngle_converted-180; // set the real anglevariable back to your -180 180 system
example:
current: -10 goal: 170
-10 becomes 170, 170 becomes 350
170-350=-180 but with fabs() it gives 180
360-350=10 ( shortest route )[code]
Last edited by VPB_Enge on Mon Mar 17, 2008 8:50 pm, edited 2 times in total.