4

I've been trying to rotate my view based on the CMAttitude returned from CMMotionManager specifically the pitch=x and roll=y. I'm using a reference attitude to set my horizon. This works great for portrait mode but the minute i try to do it for a landscape view it goes wrong. As the phone is now rotated 90 ccw I was hoping that coremotion would know landscape was in place and keep the pitch and roll useful. Instead I still have the axis pointing their original way. To try and compensate I simply changed the sign on roll=x and switched pitch=y. This appeared to work till I held the device in front of me and the turned around 180 degrees. The view spun upside down and inverted.

My spidy sense is telling me I need to apply a proper transformation on the pitch roll and yaw to reorientate the attitude

I'm hoping some geniuses or genii can help me. Maths is obviously not a strong point of mine.

Svenissimo
  • 41
  • 2

1 Answers1

3

Your are right, changing pitch and roll will lead into serious trouble. The simplest way seems to work with a new reference attitude like in CoreMotionTeapot sample. Just when the orientation change is detected, you have to grab the current attitude before multiplying it with your former reference attitude and set it as new reference attitude.

Kay
  • 12,918
  • 4
  • 55
  • 77
  • Thanks I'm already using a reference attitude in the manor that is in the teapot example. I'm getting a relative attitude from core motion – Svenissimo Feb 23 '11 at 09:46
  • but I think my issue stems from the fact that the axis continue to be aligned in the same way.. ie Y is always the length of the phone. Adding a new reference attitude doenst fix that. I think I need to clamp or selectively invert the pitch and roll..hmm – Svenissimo Feb 23 '11 at 09:56
  • Another approach that I used for getting coordinates transformed is to take the quaternion representation and then multiply it with a quaternion q for the rotation of 90° around z-Axis (for rotating to landscape mode) i.e. q={cos(90/2); 0; 0; sin(90/2)}. The resulting quaternion can now be used for extrating the new roll, pitch and yaw angles. The drawback is that you have to write your own Quaternion class because iOS SDK does not provide it. There is an open source project at code.google.com which cann be a good starting point. – Kay Feb 23 '11 at 13:36
  • Kay thanks, I'm using Cocos2d and the new extension Cocos3d which has some reasonable matrix and quaternion support. I'll give it a try and hopefully that should do the trick.. i'll let you know and thanks for the suggestions – Svenissimo Feb 24 '11 at 08:14
  • You are welcome. If you never heard from quaternions before (like me) spend some time for reading tutorials out there. It will help you to find bugs. If you ever struggle with the abstractness of quaternions, take a look at axis-angle pairs: the (x,y,z) represents the vector and w as cos(phi/2) stands for the amount of rotation. Helped me to clear things up. New here to stackoverflow? Don't forget voting up :-) – Kay Feb 24 '11 at 22:17