XNA Creators Club Online
Page 1 of 1 (8 items)
Sort Posts: Previous Next

Make a sprite face the mouse

Last post 11/18/2009 9:32 AM by TheHomicidal. 7 replies.
  • 11/15/2009 4:11 AM

    Make a sprite face the mouse

    Howdy,
    Me and a friend have this idea to make a top-down horror game... thing...

    Anyway, I want to make the sprite face the mouse if that makes sense. If it doesn't, like it does in this: Left 4K Dead. I have no idea as to how I'd go about doing that though... Any help would be great.

    Thanks,
    Homicidal
    A forum dedicated to creating XNA games for PC: http://www.xna-pc.tk
  • 11/15/2009 4:17 AM In reply to

    Re: Make a sprite face the mouse

  • 11/15/2009 5:18 AM In reply to

    Re: Make a sprite face the mouse

    I forgot to mention that this would be 2D, sorry
    A forum dedicated to creating XNA games for PC: http://www.xna-pc.tk
  • 11/15/2009 7:29 AM In reply to

    Re: Make a sprite face the mouse

    It's quite simply. You just need to know difference of mouse position and you player position.
    Vector2 d = mouseScreenPosition - manScreenPosition;  
    float angle = (float)Math.Atan2((double)d.X, (double)d.Y);  
     
    Remember this returns angle in radians.
    FontBuilder TrueType Font Editor
  • 11/15/2009 8:20 PM In reply to

    Re: Make a sprite face the mouse

    TheHomicidal:
    I forgot to mention that this would be 2D, sorry


    2D is just 3D with a constant Z value.  You can use the above technique and just assume 0 for all initial Z values.
  • 11/16/2009 3:59 PM In reply to

    Re: Make a sprite face the mouse

    ButcherBolzi:
    It's quite simply. You just need to know difference of mouse position and you player position.
    Vector2 d = mouseScreenPosition - manScreenPosition;  
    float angle = (float)Math.Atan2((double)d.X, (double)d.Y);  
     
    Remember this returns angle in radians.
    Doesn't Atan2 take in (Y,X), not (X,Y)?
  • 11/16/2009 5:00 PM In reply to

    Re: Make a sprite face the mouse

    Check out this sample.
    XNA Framework Developer - blog - homepage
  • 11/18/2009 9:32 AM In reply to

    Re: Make a sprite face the mouse

    Thanks guys!
    Shawn, I completely forgot the samples, thanks!

    Homicidal
    A forum dedicated to creating XNA games for PC: http://www.xna-pc.tk
Page 1 of 1 (8 items) Previous Next