SceneKit 相机,如何补偿欧拉规则(方向)变化 [英] SceneKit camera, how to compensate for euler rules (orientation) changes
问题描述
也许这更像是一道数学题,但我希望有人能帮助我了解如何补偿相机方向的变化.
Maybe this is more of a math question, but I am hoping somebody can help me understand how I would compensate for changes to my camera's orientation.
我想做的是能够使用类似游戏的控件在场景中移动我的相机.我设置了 W S A D 键来分别向前、向后、向左和向右移动相机.我通过更改相机节点的位置向量的值来做到这一点.我还有右左上和下箭头键映射到转动和倾斜相机.我通过调整相机节点的欧拉规则(俯仰和偏航)来做到这一点.
What I would like to do is to be able to move my camera around the scene using game-like controls. I have W S A D keys set up to move the camera forward, backward, left and right respectively. I do this by changing the values of the camera node's position vector. I also have the right left up and down arrow keys mapped to turning and tilting the camera. I do this by adjusting the camera node's euler rules (pitch and yaw).
在我用左右箭头转动相机之前,一切正常.在此之后按 W 将相机移动到它在我对其应用偏航之前所面对的方向.这种行为对我来说很有意义,但我无法弄清楚我需要对位置矢量调整做什么来补偿我应用的偏航.
Things work well until I turn the camera with the right and left arrows. Pressing W after this moves the camera in the direction it was facing before I applied yaw to it. The behavior makes sense to me, but I cannot figure out what I need to do to my position-vector tweaking to compensate for the yaw I've applied.
我是否需要以某种方式将我的位置向量乘以新的变换?或者更好的是还有其他一些属性,比如我可以更改的枢轴,以便我可以保持我的位置代码相同?
Do I need to multiply my position vector somehow by new transform? Or better yet is there some other property like the pivot that I can change, so that I can keep my position code the same?
感谢您的任何指点.
这里更新的是我目前正在使用的代码:
Update here is the code I'm working with so far:
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
}
}
我有两个计时器,一个更新相机位置,另一个更新其方向.
I have two timers, one that updates the cameras position, and the other that updates its orientation.
我坚持的部分是我知道我的新变换(旧位置加上 ASDW 的移动增量),并且我知道相机的旋转,但我不知道如何将这两者结合到我真正的新变换中转变.Transform 是 SCNMatrix4,rotation 是 SCNVector4.
The part I'm stuck on is that I know my new transform (old position plus the move increment from A S D W), and I know the camera's rotation, but I don't know how to combine those two into my true new transform. Transform is a SCNMatrix4, and rotation is a SCNVector4.
更新 2
这是代码的修改版本.我改为使用转换.但是,我仍然找不到正确的操作来解释旋转.
Here is a reworked version of the code. I switched to working with the transform instead. I still cannot find the right manipulation to account for the rotation however.
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)
}
<小时>
上次更新
Last Update
感谢您的建议.这是对我有用的实现
Thanks for the suggestions. Here is the implementation that is working for me
我需要这个 objc 助手,因为其中一些函数在 Swift 中似乎不可用:
I needed this objc helper, as some of these functions to not seem to be available in 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
if t
is the translation you compute from the WSAD keys, then you should not simply add it (node.position += t
) but first apply a rotation and then add it : node.position += node.rotation * t
这篇关于SceneKit 相机,如何补偿欧拉规则(方向)变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!