Augmented Reality App in Landscape Mode

2019-06-04 18:19发布

问题:

I am trying to build an augmented reality app using the CoreMotion framework. I have tried to go off of Apple's pARk sample code project, but it only works in portrait mode. I need it to work in landscape. When switched to landscape mode the subviews in the overlay view move in the opposite directions and at the wrong rate (they either move too fast or too slow across screen)

I have read other postings that provide two solutions:

  • Create a reference attitude and apply the inverse of that attitude to the current attitude, as suggested in the CoreMotion Tea Pot Example.

  • Rotate the quaternion representation of the attitude 90 degrees

I do not think that the first will work because my augmented reality app requires that it be referenced to true north.

I also do not understand the math required to do the second.

Any suggestions on how to accomplish this complex problem I welcome.

回答1:

How far are you now in your augmented reality app? I think beginning by taking a look at PARk from Apple is harsh. You need to have some advanced mathematical understanding. But if you do, why not!

you can take a look at this repository, this an augmented reality project working on Portrait and Landscape mode. Here is how the rotation is handled:

- (void)deviceOrientationDidChange:(NSNotification *)notification {

prevHeading = HEADING_NOT_SET; 

[self currentDeviceOrientation];

UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];

// Later we may handle the Orientation of Faceup to show a Map.  For now let's ignore it.
if (orientation != UIDeviceOrientationUnknown && orientation != UIDeviceOrientationFaceUp && orientation != UIDeviceOrientationFaceDown) {

    CGAffineTransform transform = CGAffineTransformMakeRotation(degreesToRadian(0));
    CGRect bounds = [[UIScreen mainScreen] bounds];

    switch (orientation) {
        case UIDeviceOrientationLandscapeLeft:
            transform          = CGAffineTransformMakeRotation(degreesToRadian(90));
            bounds.size.width  = [[UIScreen mainScreen] bounds].size.height;
            bounds.size.height = [[UIScreen mainScreen] bounds].size.width;
            break;
        case UIDeviceOrientationLandscapeRight:
            transform          = CGAffineTransformMakeRotation(degreesToRadian(-90));
            bounds.size.width  = [[UIScreen mainScreen] bounds].size.height;
            bounds.size.height = [[UIScreen mainScreen] bounds].size.width;
            break;
        case UIDeviceOrientationPortraitUpsideDown:
            transform = CGAffineTransformMakeRotation(degreesToRadian(180));
            break;
        default:
            break;
    }


    [displayView setTransform:CGAffineTransformIdentity];
    [displayView setTransform: transform];
    [displayView setBounds:bounds];  

    degreeRange = [self displayView].bounds.size.width / ADJUST_BY;

}
}

You have to rotate all your annotations and then your overlay view after the device rotation!



回答2:

If you're really stuck, and are willing to use another toolkit, try iPhone-AR-Toolkit. It works in both portrait and landscape.