Followers 0

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

## 6 posts in this topic

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 this post

##### 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 this post

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

lewster32 likes this

#### Share this post

##### 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 this post

##### 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 this post

##### Share on other sites

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

lewster32 likes this

## Create an account or sign in to comment

You need to be a member in order to leave a comment

## Create an account

Sign up for a new account in our community. It's easy!

Register a new account

## Sign in

Already have an account? Sign in here.

Sign In Now
Followers 0

• ### Recently Browsing   0 members

No registered users viewing this page.