Ios 锁定相机的旋转方向

Ios 锁定相机的旋转方向,ios,swift,scenekit,Ios,Swift,Scenekit,我正在创建一个使用SceneKit的应用程序。模型位于固定位置,用户可以围绕场景平移和旋转摄影机 平移可以很好地工作,旋转相机也可以工作——只要只旋转一个轴。 当相机面朝下或向上,相机向左或向右旋转时,它不仅会围绕该轴旋转,还会围绕第二个看起来很奇怪的轴旋转 我试着移动轴心点,但没用 以下是我用于旋转和移动相机的代码: fileprivate func translateCamera(_ x: Float, _ y: Float) { if let cameraNode = self.c

我正在创建一个使用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)
    }
}
fileprivate func translateCamera(x:Float,y:Float)
{
如果让cameraNode=self.cameraNode
{
让moveX=x*2//TODO Settings.speed
让moveY=-y*2//TODO Settings.speed
让位置=SCInvector3Make(moveX,0,moveY)
设rotatedPosition=self.position(位置,cameraNode.rotation)
让translated=SCNMatrix4Translate(cameraNode.transform,rotatedPosition.x,rotatedPosition.y,rotatedPosition.z)
cameraNode.transform=已翻译
如果摄像机节点位置y<25
{
cameraNode.position.y=25
}
}
}
fileprivate func位置(uPosition:SCNVector3,uRotation:SCNVector4)->SCNVector3
{
如果旋转.w==0
{
返回位置
}
让gPosition:GLKVector3=scinvector3toglkvector3(位置)
设gRotation=GLKMatrix4MakeRotation(rotation.w、rotation.x、rotation.y、rotation.z)
设r=GLKMATRIX4多射面3(gRotation,gPosition)
从GLKVector3(r)返回SCInvector3
}
fileprivate func rotateCamera(x:Float,y:Float)
{
如果让cameraNode=self.cameraNode
{
设moveX=x/50.0
设moveY=y/50.0
让旋转=SCNMatrix4Rotate(SCNMatrix4Identity,-moveX,0,1,0)
cameraNode.transform=SCNMatrix4Mult(旋转,cameraNode.transform)
让rotated2=SCNMatrix4Rotate(SCNMatrix4Identity,moveY,1,0,0)
cameraNode.transform=SCNMatrix4Mult(旋转2,cameraNode.transform)
}
}
什么是正确的方法来“锁定”相机,使其仅围绕所需的轴移动?我制作了一个小视频来展示这种行为:

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

创建空节点并添加cameraNode作为其子节点。旋转x的cameraNode和y的emptyNode