Punchausen Posted October 9, 2016 Share Posted October 9, 2016 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 Link to comment Share on other sites More sharing options...
spencerTL Posted October 9, 2016 Share Posted October 9, 2016 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. Link to comment Share on other sites More sharing options...
samme Posted October 10, 2016 Share Posted October 10, 2016 You can use Phaser.Math.wrapAngle. But I think you can set `body.angle` outside [-180, 180] and Phaser will wrap it itself. lewster32 1 Link to comment Share on other sites More sharing options...
lewster32 Posted October 11, 2016 Share Posted October 11, 2016 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. Link to comment Share on other sites More sharing options...
samme Posted October 12, 2016 Share Posted October 12, 2016 `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. Link to comment Share on other sites More sharing options...
Punchausen Posted October 13, 2016 Author Share Posted October 13, 2016 Thanks very much guys, the solution of <value>-180 is a D'oh moment for me lewster32 1 Link to comment Share on other sites More sharing options...
Recommended Posts