Jittery Movement, Uncontrollably Rotating + Front of Sprite?

Posted by Vipar on Game Development See other posts from Game Development or by Vipar
Published on 2013-07-27T12:11:43Z Indexed on 2013/10/26 4:12 UTC
Read the original article Hit count: 281

Filed under:
|
|
|

So I've been looking around to try and figure out how I make my sprite face my mouse. So far the sprite moves to where my mouse is by some vector math.

Now I'd like it to rotate and face the mouse as it moves. From what I've found this calculation seems to be what keeps reappearing:

Sprite Rotation = Atan2(Direction Vectors Y Position, Direction Vectors X Position)

I express it like so:

sp.Rotation = (float)Math.Atan2(directionV.Y, directionV.X);

If I just go with the above, the sprite seems to jitter left and right ever so slightly but never rotate out of that position. Seeing as Atan2 returns the rotation in radians I found another piece of calculation to add to the above which turns it into degrees:

sp.Rotation = (float)Math.Atan2(directionV.Y, directionV.X) * 180 / PI;

Now the sprite rotates. Problem is that it spins uncontrollably the closer it comes to the mouse. One of the problems with the above calculation is that it assumes that +y goes up rather than down on the screen. As I recorded in these two videos, the first part is the slightly jittery movement (A lot more visible when not recording) and then with the added rotation:

Jittery Movement

So my questions are:

  1. How do I fix that weird Jittery movement when the sprite stands still? Some have suggested to make some kind of "snap" where I set the position of the sprite directly to the mouse position when it's really close. But no matter what I do the snapping is noticeable.
  2. How do I make the sprite stop spinning uncontrollably?
  3. Is it possible to simply define the front of the sprite and use that to make it "face" the right way?

© Game Development or respective owner

Related posts about c#

Related posts about sprites