Animate SCNBox height by keeping base in the same y

scenekit, swift

Solution

Yes, you're correct that the `pivot` is what you need to change. The pivot is a transform — meaning it can encompass scaling and rotation for setting the "base" orientation of a node — but all you need is a translation. To anchor the box at the bottom, translate the pivot by half the box's height:

boxNode.pivot = SCNMatrix4MakeTranslation(0, -(box.height/2), 0)

However, this isn't enough to keep it anchored if you change the height — the pivot will stay at half the old height, so your box will still grow in both directions. So you'll need to change the pivot as well as the height when animating:

SCNTransaction.begin()
SCNTransaction.setAnimationDuration(5)
box.height = 20
boxNode.pivot = SCNMatrix4MakeTranslation(0, -(box.height/2), 0) // new height
SCNTransaction.commit()

Since you're animating both `box.height` and `boxNode.pivot` together, it'll stay anchored at the bottom through the animation.

Problem

I have a SCNBox object added to a SCNScene through a SCNNode... ``` let box:SCNBox = SCNBox(width: 4, height: 4, length: 4, chamferRadius: 0.1) box.firstMaterial?.diffuse.contents = UIColor.greenColor() let boxNode:SCNNode = SCNNode(geometry: box) boxNode.position = SCNVector3(x: 2, y: 2, z: 2) scene.rootNode.addChildNode(boxNode) ``` My question is how can I animate the height (lets say to 40) and at the same time have the box not grow downward too? I think I need to change the pivot point to go at the bottom of box? I am not sure how to do this if that is a solution. How can I do this correctly?

Original source