我正在创建一个使用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)
}
}
&#34;锁定&#34;正确的方法是什么?相机所以它只围绕所需的轴移动?我制作了一个显示行为的小视频:
https://www.youtube.com/watch?v=ctK-hnw7JxY
答案 0 :(得分:1)
创建空节点并将cameraNode添加为其子节点。旋转cameraNode为x,emptyNode为y。