绕一整圈时节点的方向是错误的

Node's orientation is wrong when I make a full circle

我需要那个节点总是看着相机,我通过

func renderer(_ renderer: SCNSceneRenderer, updateAtTime time: TimeInterval) {
    DispatchQueue.main.async {
        self.node.orientation.y = sceneView.pointOfView?.orientation.y
    }
}

但是当我尝试做一个完整的圆圈(将相机的方向改为相反的方向)时,这个值就变得错误了。我尝试使用 SCNBillboardConstraint(),但它 orientation 的节点保持 0 值,我需要知道角度,所以,这不适合我。

我只需要改变Y旋转,X和Z需要是0

编辑:我注意到我在使用约束时需要使用 node.presentation.orientation.y,但这仍然无法正确处理 PI

附近的起始角度

首先,尝试使用 SCNLookAtConstraint,而不是 SCNBillboardConstraint

SCNLookAtConstraint 对象自动调整节点的方向,使其始终指向另一个节点。

如果SCNLookAtConstraint 没有用,那么这是万向节锁定问题。 Gimbal lock 是三维机制中一个自由度的损失。 SceneKit 中有两个变量:eulerAngles(固有欧拉角,以弧度表示,表示围绕 x-y-z 轴的 3 次旋转序列,顺序如下:z-y-x 或滚动、偏航、俯仰)和 orientation (节点的方向,表示为四元数)。你用最后一个但是...

SceneKit使用单位四元数,其分量满足方程:

x*x + y*y + z*z + w*w == 1

我想你的相机没有正确旋转,因为你只使用了四元数结构的一个分量(没有矢量中的第四个重要分量,w)。

To get rid of incorrect camera's behaviour use all components of quaternion (expressed in SCNVector4, where x-y-z values are normalized components, and the w field contains the rotation angle, in radians, or torque magnitude, in newton-meters).

self.node.orientation = (sceneView.pointOfView?.orientation)!

所有对应的节点必须是同一类型。