Jittery Movement and Uncontrollable Rotating
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.
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.
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.
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.
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