【问题标题】:Lock camera rotation around锁定相机旋转
【发布时间】:2018-01-24 19:48:09
【问题描述】:

我正在创建一个使用 SceneKit 的应用程序。模型位于固定位置,用户可以围绕场景平移和旋转相机。

平移可以正常工作,旋转相机也可以 - 只要只旋转一个轴。 当相机朝下或朝上并且相机向左或向右旋转时,它不仅围绕该轴旋转,而且还围绕第二个轴旋转,这看起来很奇怪。

我尝试移动枢轴点,但没有帮助。

这是我用于旋转和移动相机的代码:

fileprivate func translateCamera(_ x: Float, _ y: Float)
{
    if let cameraNode = self.cameraNode
    {
        let moveX = x * 2 // TODO Settings.speed
        let moveY = -y * 2 // TODO Settings.speed

        let position = SCNVector3Make(moveX, 0, moveY)
        let rotatedPosition = self.position(position, cameraNode.rotation)
        let translated = SCNMatrix4Translate(cameraNode.transform, rotatedPosition.x, rotatedPosition.y, rotatedPosition.z)

        cameraNode.transform = translated

        if cameraNode.position.y < 25
        {
            cameraNode.position.y = 25
        }
    }
}

fileprivate func position(_ position: SCNVector3, _ rotation: SCNVector4) -> SCNVector3
{
    if rotation.w == 0
    {
        return position
    }

    let gPosition: GLKVector3 = SCNVector3ToGLKVector3(position)
    let gRotation = GLKMatrix4MakeRotation(rotation.w, rotation.x, rotation.y, rotation.z)
    let r = GLKMatrix4MultiplyVector3(gRotation, gPosition)

    return SCNVector3FromGLKVector3(r)
}

fileprivate func rotateCamera(_ x: Float, _ y: Float)
{
    if let cameraNode = self.cameraNode
    {
        let moveX = x / 50.0
        let moveY = y / 50.0

        let rotated = SCNMatrix4Rotate(SCNMatrix4Identity, -moveX, 0, 1, 0)
        cameraNode.transform = SCNMatrix4Mult(rotated, cameraNode.transform)

        let rotated2 = SCNMatrix4Rotate(SCNMatrix4Identity, moveY, 1, 0, 0)
        cameraNode.transform = SCNMatrix4Mult(rotated2, cameraNode.transform)
    }
}

“锁定”相机以使其仅围绕所需轴移动的正确方法是什么?我制作了一个小视频来展示这种行为:

https://www.youtube.com/watch?v=ctK-hnw7JxY

  • 只要只旋转一个轴就可以正常工作。
  • 但一旦第二个轴旋转,它也会向一侧倾斜。

【问题讨论】:

    标签: ios swift scenekit


    【解决方案1】:

    创建空节点并将 cameraNode 添加为其子节点。为 x 旋转 cameraNode,为 y 旋转 emptyNode。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-02-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-09-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多