I'm implementing a 360 video viewer on iOS using the gyroscope, I need to get two rotations for the video, I used this;
self.motionManager.deviceMotionUpdateInterval = 1.0/60.0;
if (self.motionManager.isDeviceMotionAvailable) {
CMDeviceMotion *deviceMotion = self.motionManager.deviceMotion;
CMAttitude *currentAttitude = deviceMotion.attitude;
//[self.motionManager startDeviceMotionUpdates];
[self.motionManager startDeviceMotionUpdatesUsingReferenceFrame:CMAttitudeReferenceFrameXArbitraryZVertical];
}
Then I do:
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
CMDeviceMotion *currentDeviceMotion = self.motionManager.deviceMotion;
CMAttitude *currentAttitude = currentDeviceMotion.attitude;
float roll = currentAttitude.roll;
float pitch = currentAttitude.pitch;
float yaw = currentAttitude.yaw;
if (isLandscape)
{
_player.rotation = -yaw + currentLocation.x;
// 0 roll is phone flat, so we need to rotate -90 degrees to adjust vertical phone
if (orientation == UIDeviceOrientationLandscapeLeft) // Invert roll and add 180 degrees offset
_player.pitch = (roll - RADIAN_90_DEGREES + RADIAN_180_DEGREES) + currentLocation.y;
else
_player.pitch = -(roll - RADIAN_90_DEGREES) + currentLocation.y;
} else {
if (orientation == UIDeviceOrientationFaceDown)
_player.rotation = -yaw - currentLocation.x;
else
_player.rotation = -yaw + currentLocation.x;
if (orientation == UIDeviceOrientationFaceUp)
_player.pitch = (roll) + currentLocation.y;
else if (orientation == UIDeviceOrientationFaceDown)
_player.pitch = (roll) + currentLocation.y;
else
_player.pitch = -(roll) + currentLocation.y;
}
This works great on landscape mode (left and right) and does exactly what I need, I had to do some adjustments depending of orientation, but works absolute fine, it's on portrait I'm lost, all values I get are so different from landscape and depending if Portrait, FaceUp, FaceDown... I'm confused and what to do, I've researched and found references to rotation matrix, quaternions... honestly I've tried lot of things and got no good results on portrait. Any help appreciated.