Reputation: 7216
I'm trying to design an app that makes it appear as if you can look around an object. I've gotten pretty far, but I can't figure out how to make the object appear floating in the air like a picture frame (as opposed to flat on the ground).
Here is my code:
CMMotionManager *motionManager = [[CMMotionManager alloc] init];
[motionManager setDeviceMotionUpdateInterval:0.1];
CMDeviceMotionHandler motionHandler = ^ (CMDeviceMotion *motion, NSError *error)
{
[xLabel setText:[NSString stringWithFormat:@"X: %f, Yaw: %f", motion.userAcceleration.x, motion.attitude.yaw]];
[yLabel setText:[NSString stringWithFormat:@"Y: %f, Pitch: %f", motion.userAcceleration.y, motion.attitude.pitch]];
[zLabel setText:[NSString stringWithFormat:@"Z: %f, Roll: %f", motion.userAcceleration.z, motion.attitude.roll]];
CATransform3D transform;
transform = CATransform3DMakeRotation(motion.attitude.pitch, 1, 0, 0);
transform = CATransform3DRotate(transform, motion.attitude.roll, 0, 1, 0);
transform = CATransform3DRotate(transform, motion.attitude.yaw, 0, 0, 1);
myView.layer.sublayerTransform = transform;
//[myImage setNeedsDisplay];
};
[motionManager startDeviceMotionUpdatesUsingReferenceFrame:CMAttitudeReferenceFrameXTrueNorthZVertical toQueue:[NSOperationQueue currentQueue] withHandler:motionHandler];
This will make it look like the picture is flat on a desk, but it should instead look like it's hung up on a wall. Any ideas on how to do that?
Upvotes: 3
Views: 1742
Reputation: 585
If it looks flat you just need to add the correct rotation to the pitch rotation code to make it stand upright by default. Try changing your pitch rotation to the code below
transform = CATransform3DMakeRotation(motion.attitude.pitch + M_PI_2, 1, 0, 0);
if its upside down try doing a subtraction instead.
Upvotes: 1