5

I can't seem to 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 doesn't move when I apply a force to its physicsBody. Any help would be greatly appreciated.

1
  • This helped me. Although, I first tried Anthony Taylor's suggestion. That got me some sort of jitter on screen. Using what you had above for a minimum z-height constraint worked for me. Thanks...
    – xBACP
    Nov 15, 2016 at 3:10

2 Answers 2

4

Yeah. This one stumped me for a bit, too.

The issue is with the matrix. According the the Developer documentation concerning the SCNMatrix4 (matrix) argument:

If the node is affected by an in-progress animation, this value reflects the currently visible state of the node during the animation (rather than its target state that will be visible when the animation completes).

Instead of this:

var newMatrix = matrix

You really want:

var newMatrix = node.transform

which appears to be the current transform about to be applied to the node.

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

2

This worked for me to constrain a SCNCameraController to the boundaries of a grid:

    let constraint = SCNTransformConstraint.positionConstraint(inWorldSpace: false, with: { (node, position) -> SCNVector3 in
        var constrainedPosition = position
        if position.x < gridMinX {constrainedPosition.x = gridMinX; node.position.x = gridMinX}
        if position.x > gridMaxX {constrainedPosition.x = gridMaxX; node.position.x = gridMaxX}
        if position.z < gridMinZ {constrainedPosition.z = gridMinZ; node.position.z = gridMinZ}
        if position.z > gridMaxZ {constrainedPosition.z = gridMaxZ; node.position.z = gridMaxZ}
        return constrainedPosition

    })
    sceneView.pointOfView?.constraints = [constraint]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.