How do I use SCNTransformConstraint to constrain the Z value of a node?

I cannot figure out how to constrain the Z-value of a node using SCNTransformConstraint

. Here's what I have so far.

        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]


With the above constraint ship

does not move when I apply force to her physicsBody

. Any help would be greatly appreciated.


source to share

1 answer

Yes. I scared me a little too.

The problem is with the matrix. As per the Developer documentation regarding the SCNMatrix4 (matrix) argument:

If a node is affected by an animation during runtime, this value reflects the current visible state of the node during animation (rather than its target state, which will be displayed after the animation finishes).

Instead of this:

var newMatrix = matrix


Do you really want:

var newMatrix = node.transform


which appears to be the current transformation to be applied to node.

I know this is an old question, but it was at the top of the search results for SCNTransformConstraint. Hey, better late than never, right?



All Articles