SceneKit 中的相机旋转
Camera Rotation in SceneKit
我已经 post 提出了与此类似的问题
here ,但这不同之处在于它处理欧拉角。
考虑到我在另一个 post 中的设置。屏幕上有一个简单的板,还有一个正在注视它的摄像头,我想旋转摄像头。为简单起见,我在代码中完成了整个相机。对于上下文,根据其他问题和答案,我已经确定电路板在 Z 轴上运行较长,在 X 轴上较短,高度为 Y 轴。
将此代码添加到场景中,我们可以在 Z 轴上看到我的板 运行。我在 Y 轴上稍微抬高了相机以获得更好的视野。我的最终目标是让棋盘 运行 长途穿过相机。
SCNNode *cameraNode = [SCNNode node];
cameraNode.camera = [SCNCamera camera];
cameraNode.camera.zFar = 200;
cameraNode.camera.zNear = 0.1;
[scene.rootNode addChildNode:cameraNode];
cameraNode.position = SCNVector3Make(0, 5, 0);
cameraNode.eulerAngles = SCNVector3Make(0, 0, 0);
给予
很好的开始。然后我试着旋转相机,让它从上往下看板子。我的理解是我需要围绕 X-axis 旋转才能完成此操作。我通过尝试以下方法做到了这一点。
SCNNode *cameraNode = [SCNNode node];
cameraNode.camera = [SCNCamera camera];
cameraNode.camera.zFar = 200;
cameraNode.camera.zNear = 0.1;
[scene.rootNode addChildNode:cameraNode];
cameraNode.position = SCNVector3Make(0, 50, 0);
cameraNode.eulerAngles = SCNVector3Make(0, 0, -M_PI/2);
iOS 文档指出 eulars 中的旋转是用 (z, y, x) 设置的。然而,这没有用,只给出了一个空白屏幕。然后我开始试验,发现绕 Z 轴旋转会让我朝着正确的方向前进。
SCNNode *cameraNode = [SCNNode node];
cameraNode.camera = [SCNCamera camera];
cameraNode.camera.zFar = 200;
cameraNode.camera.zNear = 0.1;
[scene.rootNode addChildNode:cameraNode];
cameraNode.position = SCNVector3Make(0, 50, 0);
cameraNode.eulerAngles = SCNVector3Make(-M_PI/2, 0, 0);
这呈现了这个屏幕。
这没有意义,但我继续进行并最终发现通过绕 Y 轴旋转我可以获得我想要的屏幕。
SCNNode *cameraNode = [SCNNode node];
cameraNode.camera = [SCNCamera camera];
cameraNode.camera.zFar = 200;
cameraNode.camera.zNear = 0.1;
[scene.rootNode addChildNode:cameraNode];
cameraNode.position = SCNVector3Make(0, 50, 0);
cameraNode.eulerAngles = SCNVector3Make(-M_PI/2, -M_PI/2, 0);
y 旋转也有点莫名其妙,因为我原以为必须绕 Z 轴旋转。
虽然这段代码有效,但我有一个与我的另一个问题类似的问题 post。为什么第一次旋转时必须绕 Z 轴而不是 X 轴旋转。我想我可能只是不太了解欧拉角。
提前致谢
编辑:
正如@mnuages 所指出的,在线文档指出该向量位于 (x,y,z) 中。这与我通常使用的 header 文档相反,可以在此处看到
看来这只是 Apple 代码中的一个 bug/typo。感谢您清理它。
[评论不适合评论]
请注意 documentation 指出
The order of components in this vector matches the axes of rotation:
- Pitch (the x component) is the rotation about the node’s x-axis.
- Yaw (the y component) is the rotation about the node’s y-axis.
- Roll (the z component) is the rotation about the node’s z-axis.
我已经 post 提出了与此类似的问题 here ,但这不同之处在于它处理欧拉角。
考虑到我在另一个 post 中的设置。屏幕上有一个简单的板,还有一个正在注视它的摄像头,我想旋转摄像头。为简单起见,我在代码中完成了整个相机。对于上下文,根据其他问题和答案,我已经确定电路板在 Z 轴上运行较长,在 X 轴上较短,高度为 Y 轴。
将此代码添加到场景中,我们可以在 Z 轴上看到我的板 运行。我在 Y 轴上稍微抬高了相机以获得更好的视野。我的最终目标是让棋盘 运行 长途穿过相机。
SCNNode *cameraNode = [SCNNode node];
cameraNode.camera = [SCNCamera camera];
cameraNode.camera.zFar = 200;
cameraNode.camera.zNear = 0.1;
[scene.rootNode addChildNode:cameraNode];
cameraNode.position = SCNVector3Make(0, 5, 0);
cameraNode.eulerAngles = SCNVector3Make(0, 0, 0);
给予
很好的开始。然后我试着旋转相机,让它从上往下看板子。我的理解是我需要围绕 X-axis 旋转才能完成此操作。我通过尝试以下方法做到了这一点。
SCNNode *cameraNode = [SCNNode node];
cameraNode.camera = [SCNCamera camera];
cameraNode.camera.zFar = 200;
cameraNode.camera.zNear = 0.1;
[scene.rootNode addChildNode:cameraNode];
cameraNode.position = SCNVector3Make(0, 50, 0);
cameraNode.eulerAngles = SCNVector3Make(0, 0, -M_PI/2);
iOS 文档指出 eulars 中的旋转是用 (z, y, x) 设置的。然而,这没有用,只给出了一个空白屏幕。然后我开始试验,发现绕 Z 轴旋转会让我朝着正确的方向前进。
SCNNode *cameraNode = [SCNNode node];
cameraNode.camera = [SCNCamera camera];
cameraNode.camera.zFar = 200;
cameraNode.camera.zNear = 0.1;
[scene.rootNode addChildNode:cameraNode];
cameraNode.position = SCNVector3Make(0, 50, 0);
cameraNode.eulerAngles = SCNVector3Make(-M_PI/2, 0, 0);
这呈现了这个屏幕。
这没有意义,但我继续进行并最终发现通过绕 Y 轴旋转我可以获得我想要的屏幕。
SCNNode *cameraNode = [SCNNode node];
cameraNode.camera = [SCNCamera camera];
cameraNode.camera.zFar = 200;
cameraNode.camera.zNear = 0.1;
[scene.rootNode addChildNode:cameraNode];
cameraNode.position = SCNVector3Make(0, 50, 0);
cameraNode.eulerAngles = SCNVector3Make(-M_PI/2, -M_PI/2, 0);
y 旋转也有点莫名其妙,因为我原以为必须绕 Z 轴旋转。
虽然这段代码有效,但我有一个与我的另一个问题类似的问题 post。为什么第一次旋转时必须绕 Z 轴而不是 X 轴旋转。我想我可能只是不太了解欧拉角。
提前致谢
编辑:
正如@mnuages 所指出的,在线文档指出该向量位于 (x,y,z) 中。这与我通常使用的 header 文档相反,可以在此处看到
看来这只是 Apple 代码中的一个 bug/typo。感谢您清理它。
[评论不适合评论]
请注意 documentation 指出
The order of components in this vector matches the axes of rotation:
- Pitch (the x component) is the rotation about the node’s x-axis.
- Yaw (the y component) is the rotation about the node’s y-axis.
- Roll (the z component) is the rotation about the node’s z-axis.