Angle of rotation formula depending on sprite position

Started by
23 comments, last by ElyasMachera 12 years, 10 months ago

[quote name='Elyas_321' timestamp='1307098311' post='4819006']
if the ship is facing right, then its original look vector irrespective of it its position is (1,0) and atan2(0) is always 90

It would be better to say that the original (or default, i.e. unrotated) look vector is [ 0 1 ] (but see below), and that the current look vector is [ 1 0 ] when facing to the right, and all this regardless of the location of the ship. This distinction is necessary because we need the default (or original) vector to be constant. One can say that it is the look direction in the model's local space but expressed in global co-ordinates, and as such it is fixed.

atan2(0) is not defined, because atan2 requires 2 parameters. But atan2(y,0) for any y>0 is always 90°, correct.


if it is up at 90 (0,-1) in reference to your first post should it not be x==0, y<0 for the ship looking up?

Err, does this mean that y increases from top to bottom rather than from bottom to top? Until now I assumed here and in any post above that y increases from the bottom of the screen to its top, because AFAIK it is the way D3D handles this! Please clarify.
[/quote]


If that assumption is wrong, then the vector [ 0 1 ] and its belonging angle +90° will point down, while the vector [ 0 -1 ] and its belonging angle -90° will point up. Moreover, positive angles mean to rotate in CW direction, while negative angles mean to rotate in CCW direction. This can be overcome simply by negating the angle after its computation by atan2, where necessary.
[/quote]
When going up in XNA you need to subtract the y axis, and when going down you need to add to the y axis..yeah it is screwed up..the same with C++ allegro...the graph is upside down...:(

so if the y<0 and x==0 (0,-1) looking up at 90, if its current look vector is (1, 0) then I need to subtract -90 to make it look right...yeah?



so the code
angle = atan2(y2-y1,x2-x1) - Pi/2 ;
Advertisement
Moreover, positive angles mean to rotate in CW direction, while negative angles mean to rotate in CCW direction. [/quote]

This would depend quite a bit on handedness of the system. A positive rotation of 90 on Z should rotate X to Y and Y to -X but whether this is clockwise or not is relative to the handedness of the system and the position of the viewer in respect to the object rotating.
ha ha I'm so lost here..."handedness of he system?" what?! lol...
I think I will just draw an arrow and let it follow the sprite...with different initial directions



When going up in XNA you need to subtract the y axis, and when going down you need to add to the y axis..yeah it is screwed up..the same with C++ allegro...the graph is upside down...:(

so if the y<0 and x==0 (0,-1) looking up at 90, if its current look vector is (1, 0) then I need to subtract -90 to make it look right...yeah?

so the code
angle = atan2(y2-y1,x2-x1) - Pi/2 ;

Yes (with the small mistake that up is at -90°, not at +90°, because atan2(-1,0)==-90°).

However, there is still the possibility that the rotation code uses positive angles for a CCW rotation instead of the CW rotation (as is needed here). If so, then the angle need to be negated before being fed to a rotation matrix generation.


BTW: I'm writing all this from a theoretical point of view. Maybe I'm wrong in the one of other statement, so double check all what I wrote.

[quote name='Elyas_321' timestamp='1307102845' post='4819026']
When going up in XNA you need to subtract the y axis, and when going down you need to add to the y axis..yeah it is screwed up..the same with C++ allegro...the graph is upside down...:(

so if the y<0 and x==0 (0,-1) looking up at 90, if its current look vector is (1, 0) then I need to subtract -90 to make it look right...yeah?

so the code
angle = atan2(y2-y1,x2-x1) - Pi/2 ;

Yes (with the small mistake that up is at -90°, not at +90°, because atan2(-1,0)==-90°).

However, there is still the possibility that the rotation code uses positive angles for a CCW rotation instead of the CW rotation (as is needed here). If so, then the angle need to be negated before being fed to a rotation matrix generation.


BTW: I'm writing all this from a theoretical point of view. Maybe I'm wrong in the one of other statement, so double check all what I wrote.
[/quote]

oh yeah...so I need to subtract -90 from the atan2 results, I am not sure if it takes positive angles
angle = atan2(y2-y1,x2-x1) - (-90); //this doesn't seem right to me
or
angle = atan2(y2-y1,x2-x1) +(-90); //this makes more sense..but the arrow doesn't look at the mouse :(
either way the sprite isn't looking



Moreover, positive angles mean to rotate in CW direction, while negative angles mean to rotate in CCW direction.


This would depend quite a bit on handedness of the system. A positive rotation of 90 on Z should rotate X to Y and Y to -X but whether this is clockwise or not is relative to the handedness of the system and the position of the viewer in respect to the object rotating.
[/quote]
Sorry, but this is IMHO not true. The axes are definitely given so that x increases from left to right, and y increases from top to bottom. Based on a direction vector in this system, the use of atan2(y,x) to compute the orientation angle and a[sub]d[/sub]-a[sub]f[/sub] to compute the rotation (i.e. difference) angle, we have defined that positive angles rotate in CW direction! And it doesn't make sense to look at the screen from behind it to be able to say "it's CCW".

But as I've written in previous posts, when actually doing the rotation, one has 2 possibilities to generate a (usually) rotation matrix. The one rotates in CW direction for positive angles and the other in CCW direction. Again, this is no consequence of the handedness but a convention. Your statement "positive rotation of 90 on Z should rotate ..." (emphasis done by me) already stresses this.
mmn
rotAngle = (float)Math.Atan2(Mouse.GetState().Y-arrowPos.Y, Mouse.GetState().X-arrowPos.X)+90.0f;
odd it worked, I forgot that atan2 returned radians, and rotation parameter takes radians
rotAngle = (float)Math.Atan2(Mouse.GetState().Y-arrowPos.Y, Mouse.GetState().X-arrowPos.X)+Math.Pi/2 did the trick;

EDIT:
I rotated the sprite again at 180 degrees
so its look vector is (-1,0)
hence atan2(0/-1) = 0
using the normal atan2 formula did not work.. although it should work? :S

oh yeah...so I need to subtract -90 from the atan2 results, I am not sure if it takes positive angles
angle = atan2(y2-y1,x2-x1) - (-90); //this doesn't seem right to me
or
angle = atan2(y2-y1,x2-x1) +(-90); //this makes more sense..but the arrow doesn't look at the mouse :(
either way the sprite isn't looking

From said theoretical point of view (;)), the formula to use should be
angle = atan2(y2-y1,x2-x1) - (-90);
what is equivalent to
angle = atan2(y2-y1,x2-x1)+90;

But, as said somewhere above, atan2 usually works with radian (and XNA seems me to do so, too) but "90" is a value in degree! So, if you use the above in actual code, you probably add apples to oranges! Please check whether XNA's atan2 returns radian or degree. In the former case you need to use something like
angle = atan2(y2-y1,x2-x1)+0.5*Math::PI;
or whatever the usual way in XNA is.

Even then, the rotation may be wrong due to the convention XNA uses when generating rotations. After ensuring that the above issue with Math::PI is okay, and the rotation is done so that ship A turns to the left instead of to the right (and vice-versa), please try
angle = -(atan2(y2-y1,x2-x1)+0.5*Math::PI);
(notice the minus in front).

[quote name='Elyas_321' timestamp='1307105886' post='4819035']
oh yeah...so I need to subtract -90 from the atan2 results, I am not sure if it takes positive angles
angle = atan2(y2-y1,x2-x1) - (-90); //this doesn't seem right to me
or
angle = atan2(y2-y1,x2-x1) +(-90); //this makes more sense..but the arrow doesn't look at the mouse :(
either way the sprite isn't looking

From said theoretical point of view (;)), the formula to use should be
angle = atan2(y2-y1,x2-x1) - (-90);
what is equivalent to
angle = atan2(y2-y1,x2-x1)+90;

But, as said somewhere above, atan2 usually works with radian (and XNA seems me to do so, too) but "90" is a value in degree! So, if you use the above in actual code, you probably add apples to oranges! Please check whether XNA's atan2 returns radian or degree. In the former case you need to use something like
angle = atan2(y2-y1,x2-x1)+0.5*Math::PI;
or whatever the usual way in XNA is.

Even then, the rotation may be wrong due to the convention XNA uses when generating rotations. After ensuring that the above issue with Math::PI is okay, and the rotation is done so that ship A turns to the left instead of to the right (and vice-versa), please try
angle = -(atan2(y2-y1,x2-x1)+0.5*Math::PI);
(notice the minus in front).
[/quote]

lol I assume you didn't see my previous post..I noticed that error myself and I didn't have to negate it:(
However, the sprite is now at 180 degrees at 9 o'clock...current look vector is [-1,0]
so atan2(0,-1) returns 0..
which is the original formula..but that didn't work..nor did negating it :S



...
EDIT:
I rotated the sprite again at 180 degrees
so its look vector is (-1,0)
hence atan2(0/-1) = 0
using the normal atan2 formula did not work.. although it should work? :S

I assume you meant atan2(0,-1) instead of atan2(0/-1) ...
atan2(0,-1) returns PI (a.k.a. 180°). That's absolutely okay!

EDIT
[font=arial, verdana, tahoma, sans-serif][size=2]

lol I assume you didn't see my previous post..I noticed that error myself and I didn't have to negate it:(
However, the sprite is now at 180 degrees at 9 o'clock...current look vector is [-1,0]
so atan2(0,-1) returns 0..
which is the original formula..but that didn't work..nor did negating it :S

I saw it, but at that moment my answer was already written and send. Posting is just no realtime communication :([/font]

This topic is closed to new replies.

Advertisement