Problems with rotation around 0-360 transition

Hello, I am having a simple problem I cant quite sort out. I have a sprite that is being rotated to point towards the mouse position. I have this all working so far with degrees, 0-360 for both the mouse and object. My problem is when I come to the transition, when the angle changes abruptly from 0 to 360 degrees.

Here is an image I made of the problem

I’m pretty sure that I need to add another condition to both if statements, probably an OR, but I cannot seem to figure out the correct condition.


        if (degreesToTheMouse > ship.angleInDegrees) {
            ship.turnRight();
        }
        if (degreesToTheMouse < ship.angleInDegrees) {
            ship.turnLeft();
        }


    public void turnRight() {
        angleInDegrees += rotationSpeed * Gdx.graphics.getDeltaTime();
        if (angleInDegrees >= 360) {
            angleInDegrees = 0;
        }
    }

    public void turnLeft() {
        angleInDegrees -= rotationSpeed * Gdx.graphics.getDeltaTime();
        if (angleInDegrees <= 0) {
            angleInDegrees = 360;
        }
    }

I have tried working with difference between the two angles, rather then working with the comparision of them, but I run into the same problem. (mouseAngle - shipAngle) and (shipAngle - mouseAngle) both have the same problem when the angle measurement flips from 0 to 360, or vice versa.

Can anyone offer any tips to help me? I’d greatly appreciate it.

Try this:

if ((mouse - ship) > 180 || mouse < ship) {
	if ((ship - mouse) > 180) {
		rotateRight
	} else {		
		rotateLeft
	}
} else ((ship - mouse) > 180 || mouse > ship) {    
	if ((mouse - ship) > 180) {
		rotateLeft
	} else {		
		rotateRight
	}
}

Meh it’s sloppy. Time for bed.

It is pretty messy, but it works, thank a lot.

How would you suggest I do things to avoid such confusing series of if statements? Should I try to work in radians instead?

Get back to you later. Too tired.

You could work in radians. However if you judge a game that has a lot of rotating objects just tuck that horrible code away deep inside a method you will never look at again.

How I would handle this like so:

Implement a mouse movement listener. And handle the input:


Math.atan2(ship.y - mouse.y, ship.x - mouse.x)

Maybe interpolate too.