Rotation moves to another rotation

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
Ion Dune
Posts: 453
Joined: Mon Nov 12, 2007 8:29 pm
Location: California, USA
Contact:

Rotation moves to another rotation

Post by Ion Dune »

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.
arras
Posts: 1622
Joined: Mon Apr 05, 2004 8:35 am
Location: Slovakia
Contact:

Post by arras »

What about:

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;
This would keep rotation in 0-360 range.
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

But that doesn't solve the problem, it just moves it around. Instead of having problems with -179 to 179, the problem is now with 1 to 359.

Travis
VPB_Enge
Posts: 17
Joined: Wed Jan 02, 2008 11:35 pm

Post by VPB_Enge »

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]
Didn't compile it, only to show an example on how it can be done. S!
Last edited by VPB_Enge on Mon Mar 17, 2008 8:50 pm, edited 2 times in total.
Ion Dune
Posts: 453
Joined: Mon Nov 12, 2007 8:29 pm
Location: California, USA
Contact:

Post by Ion Dune »

Thanks a lot! :D
VPB_Enge
Posts: 17
Joined: Wed Jan 02, 2008 11:35 pm

Post by VPB_Enge »

Holy *(, that's a fast response ;)
Please note I edited some lines.
Post Reply