Search code examples
javascriptrotationdegreesradians

Is it bad practice to keep increasing rotational degrees (over 360)


I am experimenting with a creative javascript framework P5.js and often times I'm using degrees to rotate a sphere. However, I do this by continuously increasing a variable and basing the rotation off of that variable. Is it a bad practice to infinitely increase a variable? Should I reset the rotation to 0 when it hits 360?

Example:

this.deg = 0;

this.show = function(){
    rotateY(radians(this.deg));
    sphere(this.x, this.y, this.r);
    this.deg++; // Continuously increasing the deg :(
}

Solution

  • Well, it depends.

    If you talking about wether it affects any performance of p5.js then No, as it most probably already do something like this.deg%=360 on degree anyways.

    However you should be careful with very large integers in JavaScript, as you might lose precision for very large integers or might overflow the size.

    Besides that, you should really keep the things under 360 so to avoid any confusion during debugging.

    Any Easy way of doing this is using modulus operator in your code, like so

    this.deg = 0;
    
    this.show = function(){
    rotateY(radians(this.deg));
    sphere(this.x, this.y, this.r);
    this.deg++; 
    this.deg%=360; // keep it under 360 deg , always
    }
    

    Read More about Safe Limit for Integers in JavaScript : https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER and this stackoverflow question : What is JavaScript's highest integer value that a Number can go to without losing precision?