Overuse of % 360 when working with angles in tutorials and examples

ORBZORBZ Member Posts: 1,304
edited June 2012 in Working with GS (Mac)
Guys, I just wanted to let you know that all your math involving angles does not need %360 in the equation. It's totally redundant.

First of all if you are assigning to an angle variable, or setting a value in any behavior that expects an angle (such as Move's "direction" parameter) GS automatically converts the input number to it's 360 degree counterpart. Basically GS does %360 for you in the background. This isn't really GS, it's just the nature of angles. They loop automatically every 360 degrees because circles are just like that.

Secondly if you are using any function that expects an angle it will work just fine with any number because of the nature of circles all of these are equal: 1 == cos(0) == cos(360) == cos(720) ==cos(1080) ... etc. It doesn't matter how big the value the functions all work the same way. You can imagine, if you like, that % 360 happens automatically inside the function.

This means that these two equations are equal:

cos(self.Time) == cos(self.Time % 360)

That doesn't mean that you NEVER need % 360, there are perfectly good reasons for it. But most of the time .. I'd say 99% of the time when I see tutorials or examples it's unnecessary. In fact, the only time I can think it may be needed is if you wanted to display the angle but all you had was an integer. Remember: integer % 360 == angle


This isn't an optimization or anything. Your game will work the same with our without the % 360, but there really is no reason to add the extra complexity. It's akin to saying: take your number and add 0. Or it's like saying: multiply by 1. It does nothing but it doesn't hurt either. It just violates KISS.

Actually, using integers instead of angles gives the nice property of being able to calculate the number of revolutions too since integer / 360 == revolutions


Comments

Sign In or Register to comment.