ios - SceneKit 相机,如何补偿欧拉规则(方向)的变化

标签 ios macos camera scenekit

也许这更像是一个数学问题,但我希望有人能帮助我理解如何补偿相机方向的变化。

我想做的是能够使用类似游戏的控件在场景中移动我的相机。我设置了 W S A D 键来分别向前、向后、向左和向右移动相机。我通过更改相机节点的位置向量的值来做到这一点。我还将左右上下箭头键映射到转动和倾斜相机。我通过调整相机节点的欧拉规则(俯仰和偏航)来做到这一点。

一切正常,直到我用左右箭头转动相机。在此之后按 W 会将相机移动到我对其应用偏航之前它所面对的方向。这种行为对我来说很有意义,但我无法弄清楚我需要对我的位置矢量调整做些什么来补偿我应用的偏航。

我是否需要用新变换以某种方式乘以我的位置向量?或者更好的是,是否有一些其他属性(例如我可以更改的枢轴),以便我可以保持我的位置代码不变?

感谢您的指点。


此处更新的是我目前使用的代码:

public func displayTimerDidFire(timer: MyCustomDisplayTimer!) {
    if timer === cameraMoveTimer {

        var cameraTransform = cameraNode.transform
        var x = CGFloat(0.0)
        var y = CGFloat(0.0)
        var z = CGFloat(0.0)

        var step : CGFloat = 10.0

        if moveUpKeyDown {
            y += step
        }
        if moveDownKeyDown {
            y -= step
        }
        if moveLeftKeyDown {
            x -= step
        }
        if moveRightKeyDown {
            x += step
        }
        if moveForwardKeyDown {
            z -= step
        }
        if moveBackwardKeyDown {
            z += step
        }

        cameraTransform = SCNMatrix4Translate(cameraTransform, x, y, z)
        cameraNode.transform = cameraTransform
    }
    else if timer === cameraTiltTimer {
        var angles = cameraNode.eulerAngles
        var stepAngle : CGFloat = CGFloat(M_PI_2 / 90.0)

        if turnLeftKeyDown {
            angles.y += stepAngle
        }
        if turnRightKeyDown {
            angles.y -= stepAngle
        }
        if tiltForwardKeyDown {
            angles.x -= stepAngle
        }
        if tiltBackwardKeyDown {
            angles.x += stepAngle
        }

        cameraNode.eulerAngles = angles
    }
}

我有两个计时器,一个更新相机位置,另一个更新它的方向。

我坚持的部分是我知道我的新变换(旧位置加上 A S D W 的移动增量),我知道相机的旋转,但我不知道如何将这两者结合到我真正的新转变。变换是一个SCNMatrix4,旋转是一个SCNVector4。


更新 2

这是代码的修改版本。我转而使用转换。然而,我仍然找不到正确的操作来解释旋转。

public func displayTimerDidFire(timer: MyDisplayTimer!) {
    var cameraTransform = cameraNode.transform
    var x = CGFloat(0.0)
    var y = CGFloat(0.0)
    var z = CGFloat(0.0)

    var step : CGFloat = 10.0

    if moveUpKeyDown {
        y += step
    }
    if moveDownKeyDown {
        y -= step
    }
    if moveLeftKeyDown {
        x -= step
    }
    if moveRightKeyDown {
        x += step
    }
    if moveForwardKeyDown {
        z -= step
    }
    if moveBackwardKeyDown {
        z += step
    }

    cameraTransform = SCNMatrix4Translate(cameraTransform, x, y, z)
    // Do something with the transform to compensate for rotation..
    // ???

    cameraNode.transform = cameraTransform

    var angles = cameraNode.eulerAngles
    var stepAngle : CGFloat = CGFloat(M_PI_2 / 90.0)

    if turnLeftKeyDown {
        angles.y += stepAngle
    }
    if turnRightKeyDown {
        angles.y -= stepAngle
    }
    if tiltForwardKeyDown {
        angles.x -= stepAngle
    }
    if tiltBackwardKeyDown {
        angles.x += stepAngle
    }

    cameraNode.eulerAngles = angles
   // printNode(cameraNode)
}

最后更新

感谢您的建议。这是对我有用的实现

我需要这个 objc 助手,因为其中一些函数在 Swift 中似乎不可用:

@implementation MySceneKitUtils
+ (SCNVector3)position:(SCNVector3)position multipliedByRotation:(SCNVector4)rotation
{
    if (rotation.w == 0) {
        return position;
    }
    GLKVector3 gPosition = SCNVector3ToGLKVector3(position);
    GLKMatrix4 gRotation = GLKMatrix4MakeRotation(rotation.w, rotation.x, rotation.y, rotation.z);
    GLKVector3 r = GLKMatrix4MultiplyVector3(gRotation, gPosition);
    return SCNVector3FromGLKVector3(r);
}
@end

和实现

public func displayTimerDidFire(timer: MyDisplayTimer!) {
    var x = CGFloat(0.0)
    var y = CGFloat(0.0)
    var z = CGFloat(0.0)

    var step : CGFloat = 10.0

    if moveUpKeyDown {
        y += step
    }
    if moveDownKeyDown {
        y -= step
    }
    if moveLeftKeyDown {
        x -= step
    }
    if moveRightKeyDown {
        x += step
    }
    if moveForwardKeyDown {
        z -= step
    }
    if moveBackwardKeyDown {
        z += step
    }

    var cameraTransform = cameraNode.transform
    var rotation = cameraNode.rotation
    var rotatedPosition = MySceneKitUtils.position(SCNVector3Make(x, y, z), multipliedByRotation: cameraNode.rotation)
    cameraTransform = SCNMatrix4Translate(cameraTransform, rotatedPosition.x, rotatedPosition.y, rotatedPosition.z)

    cameraNode.transform = cameraTransform

    // The rotation

    cameraTransform = cameraNode.transform
    var stepAngle = CGFloat(0.05)
    if turnLeftKeyDown {
        cameraTransform = SCNMatrix4Rotate(cameraTransform, stepAngle, 0.0, 1.0, 0.0);
    }
    if turnRightKeyDown {
        cameraTransform = SCNMatrix4Rotate(cameraTransform, -stepAngle, 0.0, 1.0, 0.0);
    }
    if tiltForwardKeyDown {
        cameraTransform = SCNMatrix4Rotate(cameraTransform, stepAngle, 1.0, 0.0, 0.0);
    }
    if tiltBackwardKeyDown {
        cameraTransform = SCNMatrix4Rotate(cameraTransform, -stepAngle, 1.0, 0.0, 0.0);
    }

    cameraNode.transform = cameraTransform
}

最佳答案

如果 t 是您根据 WSAD 键计算的翻译,那么您不应该简单地添加它 (node.position += t),而是首先应用旋转和然后添加它:node.position += node.rotation * t

关于ios - SceneKit 相机,如何补偿欧拉规则(方向)的变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27738794/

相关文章:

ios - 为了优化目的,iOS 应用程序的 plist 应该被压缩吗?

ios - 解除 View Controller 动画 : completion: has no effect

macos - 如何在 OS X 中使用终端获取进程的 session ID?

Android:使用相机源代码时遇到问题

ios - 以编程方式确定可用的 iPhone 相机分辨率

iOS WebView WTF 崩溃

iOS 7. 仅为一个 View Controller 更改页面方向

python - 如何在 mac osx 上更新 python-sqlite

macos - 在 Applescript 中,如何确定菜单项是否被选中/聚焦?

android - 前置摄像头预览不是全屏