Question.
I am using rotationY to get the rotation of the phone. In portrait, rotationY gives a value between 0 and 360 which is awesome, but when the phone is in landscape the Y values are between 90 to 0, 0 to 90, -90 to 0 and 0 to -90. Is there an easy way to convert these numbers to values between 0 and 360?
Hello @JoeMcKay,
This is not something I can reproduce on my device, an iPhone SE running Safari on iOS 13.5. The value of rotationY
varies between -90 and 90 in both portrait and landscape orientation. Would you mind sharing your phone model and web browser make and version, along with the code you’re using?
To answer the general question, is there an easy way to convert from one range to another: you could use map()
.
map(rotationY, -90, 90, 0, 360)
In this specific case, it doesn’t make much sense doing that, though.
Mapping would make sense but it’s a little more complicated than that. The numbers don’t just go from -90 to 90. An image helps. Behold my Maya skills.
So there’s actually two of every number in a single rotation.
I stripped down the code. I’m on a pixel (first gen) in Chrome.
https://editor.p5js.org/Joemckay/sketches/ij_X-He38
Hehe, yes, your Maya skills are impressive. And the image helps illustrate the fact that rotationY
does indeed vary between -90 and 90. So, when rotationY
return -90, there’s no way for your code to tell which of the orientations below is the phone’s actual orientation (based on solely that value).
Just scaling (mapping) rotationY
is not enough. You have to rely on other sensor data to represent a single full rotation with values between 0–360. Maybe rotationX
could be of assistance?
Thanks for sharing the sketch. I tried it on my iPhone, and rotationY
(the label Y
) does not show values outside the range -90–90 (neither in portrait nor landscape). The documentation for rotationY
states that “the value will be -90 to 90”, so something strange is definitely going on if your Pixel returns values in the range 0–360. Could you borrow a friend’s phone and see if the sketch acts weird there as well?
Yeah, it turns out after I made the clarifying p5 sketch that I realized was wrong. The Y is giving me the same data in both orientations, is just doesn’t get in the way in the verticle view. This is actually good news because it means there’s hopefully some standardization across phones.
Yeah, I agree that I can probably use RotationX data to quantify which quadrant the rotationY is in and then map that data accordingly. I was having one of those moments where you suddenly feel like you’re solving a problem that’s already been solved a thousand times. But if there’s no easy fix I’ll figure it out.
Thanks for the help.
Your Maya skills are awesome. LOL!
But, It gets confusing if there is opposite result for same number!
Let us know when you figure it out.