# Converting a 360 Degree Angle Value to the 180/-180 value Phaser Uses

## Recommended Posts

Hi folks,

I'm working on a project (or continuing on I should say) where I'm now wanting to use my device accelerometer to determine the angle of my sprite.  The only issue I have however, is that the accelerometer outputs a degree angle from 0 to 360, while object.body.angle uses a -180 to 180, and object.body.rotation uses some odd 3.something to -2.something value.

Is there any way I can convert a 360 value to one of the others that Phaser uses?

Many thanks!

Punchausen

##### Share on other sites

I think the body rotation is in radians.  I can't check exactly what at the moment as I hate browsing on my phone, but in Phaser maths utils there are methods for switching between rads and degs and also normalising angles which will do what you want but maybe only in rads. Using these methods and switching to the units you need at the end should do what you want.

##### Share on other sites

You can use Phaser.Math.wrapAngle. But I think you can set `body.angle` outside [-180, 180] and Phaser will wrap it itself.

##### Share on other sites

Converting 0 - 360 to -180 - 180 is just a matter of subtracting 180 from the value. Handily, as @samme mentions, wrapAngle can do this for you, and also if you pass true as the second parameter, you can put radians in there too.

##### Share on other sites

`sprite.body.rotation` mirrors `sprite.angle` and both are in degrees.

`sprite.body.angle` is the angle of the body's velocity vector (in radians), not its orientation.

I'd guess you should just set `sprite.angle` to the angle you want. In Phaser an angle of 0 points to the right but the accelerometer's 0 may be different.

##### Share on other sites

Thanks very much guys, the solution of <value>-180 is a D'oh moment for me

## 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.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.

×   Your previous content has been restored.   Clear editor

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