casey Posted February 8, 2018 Share Posted February 8, 2018 I'm thoroughly confused about radians and degrees! Been a long time since high school math I'd like to get the rotation of an object (currently being returned in radians), convert that to degrees, and allow for a variance of a few degrees. So a player is rotating the object to line it up with another object, and I'm checking that the rotation is correct. The rotation should be 180 deg, give or take a few degs (so 175 to 185 deg). However, my rotation is converting as where approx 185 is shows up as -175... Basically once it hits 180 it converts into a negative number, backwards from 180. I've got this.rot = rotationOfSprite; var degs = this.getDegrees(this.rot); console.log(degs); getDegrees: function(){ var degs = this.rot * 180/Math.PI; return degs; } SOLVED. Figured it out. Thanks (future ref: if(this.rot<0) this.rot +=2*Math.PI. Quote Link to comment Share on other sites More sharing options...
sebas86pl Posted February 23, 2018 Share Posted February 23, 2018 Most game engines have somewhere in code constants and just multiply values to perform conversion: var rad2ang = 180/Math.PI; var ang2rad = Math.PI/180; /* [...] */ var angle = someObject.rotation /* in radians */ * rad2ang; In this case you can easily avoid function call and code is still perfectly readable. Quote Link to comment Share on other sites More sharing options...
casey Posted February 23, 2018 Author Share Posted February 23, 2018 Thank you: but does that account for the fact that the degrees here aren't 360, but 180 and -180 (which is why I had to resort to if(this.rot<0) this.rot +=2*Math.PI.)? Quote Link to comment Share on other sites More sharing options...
sebas86pl Posted February 26, 2018 Share Posted February 26, 2018 It doesn't matter if it's range of <-180,180> or <-360-360> or even bigger values, it just works because trigonometric functions are periodic functions and math lib can handle it. You should just avoid extremely big values to avoid precision loose. Quote Link to comment Share on other sites More sharing options...
mattstyles Posted February 28, 2018 Share Posted February 28, 2018 Most libraries also cache values (at least integer rotational values) and save themselves some processing cycles. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.