Jittery Movement and Uncontrollable Rotating

Started by
19 comments, last by vipar 10 years, 8 months ago
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 I've added + 90 to the above calculation so that it reads:
sp.Rotation = (float)Math.Atan2(directionV.Y, directionV.X) * 180 / PI + 90;
It will now face the mouse correctly every time. (This is different since the video was made)
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:
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?
The code is as follows:

CEO of Dynamic Realities

Advertisement
Think about it. When the mouse point is equal to the rotation point, exactly what orientation should the sprite have? A better way to look at it is this: Imagine a point in the exact center of your body. Now, orient yourself to face that point. The results from atan2 at this point are undefined.

Dunno what Atan2 returns for 0, 0 in Java (EDIT: C# even ;)). In C/C++ it returns 0 and sets errno I think. You have to set it to some value, usually the last valid rotation is the best. Better still is to restrict the cursor so it can't get too close to the rotation centre.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

I've tried to stop the sprite when it was very close to the cursor to make it stop rotating like that. I know what is causing it. I just don't know how to really fix it.

CEO of Dynamic Realities

Keep track of the mouse position. If the new mouse position is viable, save it and use it to determine the rotation. If it is not viable, discard it and use the stored position to determine rotation.

Keep track of the mouse position. If the new mouse position is viable, save it and use it to determine the rotation. If it is not viable, discard it and use the stored position to determine rotation.

I don't think you read the post or the code :/

I am already doing that.

CEO of Dynamic Realities

Keep track of the mouse position. If the new mouse position is viable, save it and use it to determine the rotation. If it is not viable, discard it and use the stored position to determine rotation.


I don't think you read the post or the code :/
I am already doing that.


No, you are tracking the rotation. Track the mouse position.


if fabs(old_pos - mouse_pos) > epsilon then
old_pos = mouse_pos
directionV = Normalize(mouse_pos- sp.Position);
else
directionV = Normalize(old_pos- sp.Position);
endif

newRot = (float)Math.Atan2(directionV.Y, directionV.X) * 180 / (float)Math.PI + 90;

Keep track of the mouse position. If the new mouse position is viable, save it and use it to determine the rotation. If it is not viable, discard it and use the stored position to determine rotation.


I don't think you read the post or the code :/
I am already doing that.


No, you are tracking the rotation. Track the mouse position.


if fabs(old_pos - mouse_pos) > epsilon then
old_pos = mouse_pos
directionV = Normalize(mouse_pos- sp.Position);
else
directionV = Normalize(old_pos- sp.Position);
endif

newRot = (float)Math.Atan2(directionV.Y, directionV.X) * 180 / (float)Math.PI + 90;

Can't really see how I should translate this into C# given a mouse position is given by a Vector2i and a position is given by a Vector2f and by subtracting them you can't get a single number you can compare to Epsilon. Epsilon would have to be either vector itself to test for "is greater than" or the subtractions of the vectors have to be made into a number.

CEO of Dynamic Realities

That is just pseudo-code. Find the distance between the two points and compare the distance to epsilon.

distance = sqrt(((old_pos.x - mouse_pos.x) * (old_pos.x - mouse_pos.x)) + ((old_pos.y - mouse_pos.y) * (old_pos.y - mouse_pos.y)));
if fabs(distance) > epsilon then
...

Yeah, you meant "length" or "magnitude" rather than fabs.

And you want to use the square of the distance in your new example (rather than call sqrt, for efficiency reasons) and compare it to epsilon * epsilon, since if a >= 0 and b >= 0 then a >= b if and only if a2 >= b2

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

This topic is closed to new replies.

Advertisement