Jump to content

radians and degrees conversion


casey
 Share

Recommended Posts

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.

Link to comment
Share on other sites

  • 2 weeks later...

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.

Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...