
Rotation, atan2, headaches
Okay, so I''m using atan2(var,var) to get the angle between two points. No problem there. However, atan2 returns a value between -pi and pi. When converted to degrees it''s between -180 and 180 degrees. The problem is that I have been using the gained angle to slowly rotate an object to face another. I simply use:
rotation+=desiredRotation-rotation*(deltaTime*8);
desiredRotation being the angle gained from atan2 and the dT*8 being to limit the speed to 8 degrees of rotation per second. The problem arises in that when the angle from atan goes around a full circle, it leaps from 180 to -180 and the object has to do a complete spin in the wrong direction to match up.
At first I figured it would be a simple case of using:
rotation+=(desiredRotation+360)-(rotation+360)*(deltaTime*8);
Alas no. And indeed, alas no to all my other attempts to circumvent this problem. So now I officially hand in the towel and am begging for the solution to what seems like a simple problem. I do hate it when I can''t figure out these simple things.....I wonder if I could sue my high school maths teacher

------------------------------------------[New Delta Games] | [Sliders]
You need to check whether abs( desiredRotation ) >= 180 degrees and go the other way in that case.
"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley
well, if you know vectors then, ill tell you how you can find if the current rotation is Clock-wise or counter-clockwise of the objective. I know the problem is 2D but we will pass through 3D space to solve it:
v=(cos(rotation),0,(sin(rotation))
w=(cos(desiredRotation),0,(sin(desiredRotation))... note that cos and sin are passed values in radians.
now let u=v x w (3D crossproduct operator)...
if u.y >0, then v is on one side of w (not sure which) and
if u.y <0, then v is on other side of w
and depending on the side, you either add or subtract angle by a constant amount*dt, until you reach within some tolerence of the objective. (this method ensures constant angular velocity)
you can also stop using atan2 if you wanted to...then w would be the vector between the seeker and the objective.
SporadicFire
v=(cos(rotation),0,(sin(rotation))
w=(cos(desiredRotation),0,(sin(desiredRotation))... note that cos and sin are passed values in radians.
now let u=v x w (3D crossproduct operator)...
if u.y >0, then v is on one side of w (not sure which) and
if u.y <0, then v is on other side of w
and depending on the side, you either add or subtract angle by a constant amount*dt, until you reach within some tolerence of the objective. (this method ensures constant angular velocity)
you can also stop using atan2 if you wanted to...then w would be the vector between the seeker and the objective.
SporadicFire
Thanks, the abs(desiredRotation) was the right way to go. Although, I''m not sure if you misunderstood my post or just made a small mistake - but it should have been:
abs(desiredRotation-rotation)>180
At first I was thinking - hmm...that shouldn''t work but maybe he knows something I don''t, and tried it anyway. Then I realised what you meant.
And it''s at times likethese that I wonder if I''m cut out for programming games
I really should have been able to see that solution myself
abs(desiredRotation-rotation)>180
At first I was thinking - hmm...that shouldn''t work but maybe he knows something I don''t, and tried it anyway. Then I realised what you meant.

And it''s at times likethese that I wonder if I''m cut out for programming games

------------------------------------------[New Delta Games] | [Sliders]
I realised after I posted that I had made a mistake... figured you''d suss it though.
"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley
"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley
"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement