Calculating enemy sprite direction vector to face player

• 06-01-2011
Eman
Calculating enemy sprite direction vector to face player
lol this is part 2 of my trig confusion..please feel free to engage :D

Right, to calculate the look vector I was using an original arrow sprite
that looked like this:
Attachment 10627
green arrow, get it? :D
em, anyways it was supposed to track the mouse as i moved it..
and this simple formula worked to calculate the angle of rotation:
Code:

`rotAngle = Math.Atan2(Mouse.GetState().X - arrowPos.X, Mouse.GetState().Y-arrowPos.Y);`
and it worked perfectly..
so I decided to use it with my ship.
Attachment 10628 (hello again)
the initial heading I used was 90 degrees (the right ship)...and when I used the above code..it wasn't working :( (oh, the horror!)
adding +90 to the rotAngle made it only slightly better..but it wasn't looking at the freaking player..
so I looked at the arrow sprite again and saw that the heading was different (hence why I was asking how to calculate the new x and y positions, thinking about it I should probably have asked this question first)

so I made the ship face 3 o'clock and re ran the code..and wow it tracked the player perfectly..oh the joy!
So my question is why did the atan2 formula not working for the 90 degree heading but it worked for the 0 degree heading?
I hope what I asked is clear..I like to tell stories a lot :(

Thanks
• 06-01-2011
tabstop
Nothing wrong with atan2, and everything wrong with you not drawing your sprite very well. Your ship has been looking at the player, except you drew the eyes on the side of the ship as opposed to the front of the ship.

(Also, if you had started out 90 degrees too high, shouldn't you have subtracted 90 degrees to fix it?)
• 06-01-2011
Eman
Quote:

Originally Posted by tabstop
Nothing wrong with atan2, and everything wrong with you not drawing your sprite very well. Your ship has been looking at the player, except you drew the eyes on the side of the ship as opposed to the front of the ship.

I know it was the way I drew the sprite..but what do you mean to the side of the ship...
both ships are looking upwards at 90 deg..
both ships are looking sideways at 0 deg..
but initially they look in the same direction just different positions..
so for atan2 to give me the correct results it has to look at 0 degrees..why? :S
Quote:

(Also, if you had started out 90 degrees too high, shouldn't you have subtracted 90 degrees to fix it?)
i did -90 it still wasn't looking at the enemy..i even did -89.5 but that makes it look the other way
• 06-02-2011
VirtualAce
90 degress is only correct for the right ship if you are using sin(), cos() in a specific order and on specific axes. It does not have to be 90 degrees and can be any orientation you want so long as you remember what frame of reference you are using. Usually right is zero degrees.