Friday, August 19, 2016

c# - Jittery Movement, Uncontrollably Rotating + Front of Sprite?


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?



Answer




Only change the rotation whenever your distance is above a certain delta. Also center your sprite, right now you're using the top-left corner of the sprite.


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...