I want to rotate a node around it's x-axis (more specifically, I want to turn a wheel).
The wheel is a subnode of a car. I found a couple of threads regarding this problem on stackoverflow, e.g. this but non of the solutions worked for me:
I tried the following code:
if let wheel = sceneView.scene.rootNode.childNode(withName: "WheelRearL", recursively: true) {
// also tried:
// wheel.pivot = SCNMatrix4MakeTranslation(0.5, 0.5, 0.5)
let (minVec, maxVec) = (wheel.boundingBox)
wheel.pivot = SCNMatrix4MakeTranslation(-25, (maxVec.y - minVec.y) / 2 + minVec.y, (maxVec.z - minVec.z) / 2 + minVec.z)
}
But the wheel still turns around the center of the whole car.
The following code is used for the rotation itself, but should not be relevant to the solution I think:
let spin = CABasicAnimation.init(keyPath: "rotation")
spin.fromValue = NSValue(scnVector4: SCNVector4(x: 25, y: 0, z: 0, w: 0))
spin.toValue = NSValue(scnVector4: SCNVector4(x:25, y: 0, z: 0, w: Float(CGFloat(2 * M_PI))))
spin.duration = 3
spin.repeatCount = .infinity
let wheel = sceneView.scene.rootNode.childNode(withName: "WheelRearL", recursively: true)
wheel?.addAnimation(spin, forKey: "spin around")
(btw: I need to change the pivot in code, without setting an additional container node in SceneKit interface builder and moving it to the nodes center)
SCNAction.rotate(by: value around: SCNVector3Make(1,0 , 0), duration: 0.2)
try the answer I post and let me know but please give it a try - Reinier Melian