iPhone trigonometry for dealing with orientation
I am trying to write an augmented reality application. I am using the compass, gyroscope, and GPS coordinates to create overlays. Everything is working great, except for when the user leaves portrait mode. Should just be a simple trig problem that I can't figure out.
To determine if a coordinate is in the field of view, I use an azimuth and inclination value of the center point of the iPhone that is calculated at the start of the program. Azimuth is defined as left and right movement when the phone is facing up, and inclination is up and down movement when the phone is facing up. I then update this value with gyroscope and compass data as it updates. So if I update the inclination and azimuth of the center point correctly, everything should work.
The problem is that when the iPhone is rotated, its definition of azimuth and inclination stay the same, with respect to the phone. So inclination is still defined as up to the top of the phone, even though the phone is sideways.
What I tried was doing something like this:
self.centerPoint.azimuth = (degToRad(angleX)*cosZ) + (degToRad(angleY)*sinZ);
self.centerPoint.inclination = (degToRad(angleY)*cosZ) + (degToRad(angleX)*sinZ);
where degToRad(x) converts from degrees to radians, cosZ and sinZ are the cos and sin of the z-angle from the gyroscope respectively, and angleX and angleY are the x and y angles from the gyroscope. The X-angle from the gyroscope measures up-down angle, Y-angle measures left-right, and Z-angle measures rotation of the phone from portrait to landscape.
Doesn't seem to be working. When I rotate the phone to the side, the inclin开发者_JAVA百科ation shoots up dramatically. Any trig people out there who can help?
I should add that normally, when only considering portrait orientation, I used: azimuth = degToRad(angleX) and inclination = degToRad(angleY). This works for portrait orientation. I should just have to multiply this by some factor to account for the different angle.
Been a long while since I solved this kind of problem but if its just a question of moving from portrait to landscape, couldn't you just swap azimuth and inclination values as needed?
After all, they are both just angles calculated the same way and it is only the label that really makes them different. You might do better just to label them angle1
and angle2
and then use one or the other in other calculations based on the phone's orientation.
Update:
In looking at this again, I think your problem is that your are using the wrong sensor. The gyroscope data returns radians/sec and is a measure of the rate of rotation which you don't want.
Instead, I think you need to use the attitude measurement provided by CMAttitude. The CMAttitude class provide data for roll, pitch and yaw, which will allow you to calculate the physical orientation of the device. The method multiplyByInverseOfAttitude:
looks like it will provide the change in device orientation between any two samples.
精彩评论