随着iOS设备的移动,cameranode旋转 [英] cameranode rotate as iOS device moving

查看:177
本文介绍了随着iOS设备的移动,cameranode旋转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是360度视频播放器项目。

It's 360 degree video player project.

我将一个cameranode( SCNNode )添加到根节点,将cameranode放在中心位置(0,0 ,0) SCNSphere ,它现在可以播放视频了。

I add a cameranode(SCNNode) to a rootnode, the cameranode was put at the center(0,0,0) of the SCNSphere, It can play video by now.

现在我必须使用devicemotion。设备移动时我需要旋转相机。不只是旋转一定的角度。 (不只是设备移动,当我拿着设备时,我的移动视为设备移动。因为我发现如果我使用 deviceMotion.attitude.roll ,仅限cameranode当设备自行移动时移动,而不是当我使用设备移动时移动)

Now I have to use devicemotion. I need to rotate the camera when device moves. Not just rotate a certain angle. (Not just device move, When I'm holding a device, my moving regard as the device moving. because I found if I use deviceMotion.attitude.roll, the cameranode only moves when device moves by itself, not when I circle around with device)

当设备位于(x1,y1,z1)位置时,cameranode将旋转为设备移动,当设备再次位于(x1,y1,z1)位置时,视图应与我们离开的最后一个视图相同。

When device is located in (x1,y1,z1) position, the cameranode would rotate as the device moving, and when the device located in (x1,y1,z1) position again, the view should be as same as the last we left.

这就是我的意思已完成:

Here's what I've done:

if (self.motionManager.gyroAvailable) {
        self.motionManager.gyroUpdateInterval = 1.0/60.0;
        [self.motionManager startGyroUpdatesToQueue:self.queue withHandler:^(CMGyroData *gyroData, NSError *error){
            if (error) {
                [self.motionManager stopGyroUpdates];
                NSLog(@"Gyroscope encountered error:%@",error);
            }else {
                CGFloat tempX = 0;
                CGFloat tempY = 0;
                CGFloat tempZ = 0;
                if ((fabs(gyroData.rotationRate.y)/60) > 0.002) {
                    tempY = gyroData.rotationRate.y/60;
                }
                tempX = gyroData.rotationRate.x/60;
                tempZ = gyroData.rotationRate.z/60;
                [self.cameraNode runAction:[SCNAction rotateByX:-tempY y:tempX z:tempZ duration:0]];
            }
        }];
    }else {
        NSLog(@"This device has no gyroscope");
    }

数学可能是错误的,我不知道为什么我要划分60 ,但是当我使用 [self.cameraNode runAction:[SCNAction rotateByX:-tempY y:tempX z:tempZ duration:0]]时,它似乎是我需要的最接近的值;

The math could be wrong, I don't know why would I divide 60, but it seems the nearest value I need when I use [self.cameraNode runAction:[SCNAction rotateByX:-tempY y:tempX z:tempZ duration:0]];

这是问题吗?


  1. 我应该使用哪些数据?

CMGyroData CMDeviceMotion 。如果使用 CMDeviceMotion ,我应该使用哪个特定值? deviceMotion.attitude deviceMotion.attitude.quaternion deviceMotion.gravity

CMGyroData or CMDeviceMotion. If use CMDeviceMotion, which specific value should I use? deviceMotion.attitude, deviceMotion.attitude.quaternion, deviceMotion.gravity


  1. 这是旋转cameranode的正确方法吗?有很多方法,我不确定。

+(SCNAction *)rotateByX:(CGFloat) xAngle y:(CGFloat)yAngle z:(CGFloat)zAngle持续时间:(NSTimeInterval)持续时间;

+( SCNAction *)rotateToX:(CGFloat)xAngle y:(CGFloat)yAngle z:(CGFloat)zAngle duration:(NSTimeInterval)duration;

推荐答案

设备运动最佳,因为它连接多个传感器并以有意义的方式将数据融合在一起。最好避免使用欧拉角,因为它们会受到所谓的万向节锁定的影响。相反,请使用四元数并使用它们设置相机方向。

Device motion is best, as it ties in multiple sensors and fuses the data together in a meaningful way. It's also best to avoid using euler angles, as they suffer from what's known as gimbal lock. Instead, use quaternions and set your camera orientation with them.

我创建了一个用于CMDeviceMotion的Swift类扩展,它可以帮助您确定设备正在查看的内容(对于屏幕的任何方向)。

I've created a Swift class extension for CMDeviceMotion that'll let you determine what the device is looking at (for any orientation of the screen).

从那里,旋转相机很简单。首先,设置您的设备动作(在视图中显示,或其他一些适当的位置):

From there, it's straightforward to rotate your camera. First, setup your device motion (in view did appear, or some other appropriate place):

if motionManager.deviceMotionAvailable {

    motionManager.deviceMotionUpdateInterval = 0.017
    motionManager.startDeviceMotionUpdatesToQueue(NSOperationQueue(), withHandler: deviceDidMove)
}

然后使用前面提到的CMDeviceMotion扩展在deviceDidMove实现中处理这些更新:

Then handle those updates in your deviceDidMove implementation, using the previously mentioned CMDeviceMotion extension:

func deviceDidMove(motion: CMDeviceMotion?, error: NSError?) {

    if let motion = motion {

        yourCameraNode.orientation = motion.gaze(atOrientation: UIApplication.sharedApplication().statusBarOrientation)
    }
}

现在你的相机应该关注您的设备在太空中的方向。

And now your camera should follow your devices orientation in space.

这篇关于随着iOS设备的移动,cameranode旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆