Rotating a square to point at a Vector2 gets completely messed up at 0 degrees

Viewed 26

This is the code that rotates the boat to follow the line. A joint is just a Vector2 on the line. To the left is 0 degrees. And as you can see, when the boat is going from 0 to 360 or vice versa degrees, it glitches out.

float LookAt(Vector2 joint)
{
     float deltaY = rect.y - joint.Y; // Calculate Delta y

     float deltaX = joint.X - rect.x; // Calculate delta x

     float angle = (float)(Math.Atan2(deltaY, deltaX) * 180.0 / Math.PI) + 90; // Find angle

     float amountToRotate = angle - rotation;

     amountToRotate *= 0.05f;

     Console.WriteLine($"Rotation: {rotation} Angle: {angle} Amount: {amountToRotate}");

     return rotation + amountToRotate;
 }

I'm using an amountToRotate variable because I want the rotation to be a little smooth (doesn't show well on the GIF).

https://gyazo.com/cd907763665ac41a2c8f8e5d246ab292

Any help is much appreciated.

(I'm also doing this in Raylib if that makes any difference).

0 Answers
Related