Thread: Calculating enemy sprite direction vector to face player

  1. #1
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629

    Calculating enemy sprite direction vector to face player

    lol this is part 2 of my trig confusion..please feel free to engage

    Right, to calculate the look vector I was using an original arrow sprite
    that looked like this:
    Calculating enemy sprite direction vector to face player-arrow-png
    green arrow, get it?
    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.
    Calculating enemy sprite direction vector to face player-spaceship-png (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
    You ended that sentence with a preposition...Bastard!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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?)

  3. #3
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    Quote Originally Posted by tabstop View Post
    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
    (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
    You ended that sentence with a preposition...Bastard!

  4. #4
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sprite Rotation and in Direction C# and XNA
    By Eman in forum Game Programming
    Replies: 9
    Last Post: 02-21-2011, 11:06 PM
  2. Rotate a direction vector around pitch/yaw
    By Devils Child in forum Game Programming
    Replies: 9
    Last Post: 08-07-2009, 08:27 AM
  3. Moving sprite relative to another sprite
    By Akkernight in forum Game Programming
    Replies: 5
    Last Post: 04-13-2009, 05:00 PM
  4. Replies: 3
    Last Post: 05-12-2003, 05:49 PM
  5. Enemy of the State
    By golfinguy4 in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 05-22-2002, 06:13 PM