Advertisement

Rotation angle problem

Started by January 16, 2003 03:12 PM
9 comments, last by Raduprv 22 years, 1 month ago
I never asked such a stupid question, but this one is killing me, I just have a mind block, my brain ''resets'' whenever I am thinking of it. Anyway, here is my problem: My characters can move in 8 directions: N,NE,E, etc. Each direction has a 45 degree angle. That is, N is 0, E is 90, S is 180, etc. Now, if my character is facing N, and I want to move to SE, then the angle the char. has to rotate would be 45*3=135 But, if it faces N and I want it to move SW, then the angle would be -135 (I want the characters to turn around CW, or CCW, depending on which s the smallest angle). Now, what is the formula to do this? It is killing me, any help would be greatly appreciated.
if (angle > 180)
angle = -(360-angle)

-- Exitus Acta Probat --
Advertisement
Won''t work, because sometimes the angle can be negative, etc.
It''s not that simple.
I made a game 2 years ago in highschool and i had that problem but i fixed it..Im in class right now so i cant give you the code. If you have AIM, orgeeizm is my SN and IM me later on and ill show you how to fix it.
Actually nvm, I looked at my ancient site, heres an example program i wrote long ago about that angle problem.

http://www.angelfire.com/wa2/x0skelet0n/v2/fil3z/faceit.c

IM me if you have questions.
Thanks for the code, but... umm... that code finds the angle between 2 points, right?
I want even something simple, I have the angles already, I just want to know the shortest angle between them...
Advertisement
Did you want something like this?
float unwindAngle_Degrees( float fAngle ){	fAngle -= 360.0f * (int)( fAngle / 360.0f ); 	if( fAngle < 0.0f )	{		fAngle += 360.0f;	}	return fAngle;} 

No, i don''t want something like that...
I actually want my characters to turn... umm...
If you are looking forward (N) and you want to turn SW, you normally turn:
N->NW->W->SW
Instead of:
N->NE->E->SE->S->SW

I am lloking for a way to turn as less as possible.
As in:

bool isCWFaster( float fStartAngle, float fEndAngle ){   float ccw = unwindAngle_Degrees( fStartAngle - fEndAngle );   float cw = unwindAngle_Degrees( fEndAngle - fStartAngle );   return( cw < ccw );} 


?

Example runs:
If start angle = 0.0f (N) and dest angle = 225 (SW):
ccw = 135.0f
cw = 225.0f
function returns false

If start angle = 180.0f (S) and dest angle = 225 (SW):
ccw = 315.0f
cw = 45.0f
function returns true

standard disclaimers apply - i''m tired.
I'm not familiar with this programming language, but I know what you mean. Maybe something like

if (absolute value of (angle-turntoangle) < absolute value of (angle-360)-turntoangle)
subtract from the angle
else
add to the angle
end

[edited by - ghotistix on January 16, 2003 7:50:30 PM]

This topic is closed to new replies.

Advertisement