如何使用SCNTransformConstraint来限制节点的Z值?

Eps*_*lon 5 ios scenekit swift

我似乎无法弄清楚如何使用 来约束节点的 Z 值SCNTransformConstraint。这是我到目前为止所拥有的。

        let constraint = SCNTransformConstraint(inWorldSpace: true, withBlock:{
            node, matrix in

            var newMatrix = matrix
            let currentNode = node as SCNNode
            if (currentNode.presentationNode().position.z > 0.0) {
                newMatrix.m43 = 0.0
            }

            return newMatrix
        })

    ship.constraints = [constraint]
Run Code Online (Sandbox Code Playgroud)

有了上述约束,ship当我对其施加力时,它不会移动physicsBody。任何帮助将不胜感激。

小智 4

是的。这个也让我有点难受。

问题出在矩阵上。根据有关 SCNMatrix4(矩阵)参数的开发人员文档:

如果节点受到正在进行的动画的影响,则该值反映动画期间节点当前可见的状态(而不是动画完成时可见的目标状态)。

而不是这个:

var newMatrix = matrix
Run Code Online (Sandbox Code Playgroud)

你真的想要:

var newMatrix = node.transform
Run Code Online (Sandbox Code Playgroud)

这似乎是要应用于节点的当前变换。

我知道这是一个老问题,但它位于 SCNTransformConstraint 搜索结果的顶部附近。嘿,迟到总比不到好,对吧?