I am writing a game in HTML5 and having problems with a piece of Javascript to do with the conversion of radians to degrees.
Currently my sprite is turned when the left and right keys on the keyboard are pressed/held down. The framework I am using outputs the rotation value in radians however the number can go above/below 1 and -1. Each time the right key applies a +0.05 to the rotation value and -0.05 from the left key.
Everything works fine when I constantly rotate clockwise (right key) using the following code:
game.player.rotationdeg = (ship.rotation * (180/Math.PI)) % 360;
Even if I go through a full rotation over 360 degrees the resulting degrees start back at 0 and continues to provide the correct angle.
When I go counter clockwise and the rotation value goes below 0 then things go out of whack. Instead of 270 degrees I get -90. This is causing me a lot of time and stress and I would appreciate any assistance.
I believe I might be able to check if the result is a negative and then just minus it from 360 but I am unsure of where to put that in the code I have or if it needs a separate function to work it out. I believe it might just be a problem with how I am approaching the situation.