SCNVector4: Rotate on x-axis

1k Views Asked by At

I have a sphere and managed to rotate it. But unfortunately along the wrong axis.

My goal is a rotation like the earth along the x axis. Can you help me to apply this?

Here is my existing code:

let spin = CABasicAnimation(keyPath: "rotation")
        // Use from-to to explicitly make a full rotation around z
        spin.fromValue = NSValue(scnVector4: SCNVector4(x: 1, y: 0, z: 0, w: 0))
        spin.toValue = NSValue(scnVector4: SCNVector4(x: 1, y: 0, z: 0, w: Float(CGFloat(-2 * Double.pi))))
        spin.duration = 30
        spin.repeatCount = .infinity
        sphereNode.addAnimation(spin, forKey: "spin around")
3

There are 3 best solutions below

2
On

You're already have working solution:

spin.fromValue = NSValue(scnVector4: SCNVector4(x: 1, y: 0, z: 0, w: 0))
spin.toValue = NSValue(scnVector4: SCNVector4(x: 1, y: 0, z: 0, w: CGFloat.pi * 2))

If you wanna change direction, just change vectors :)

spin.fromValue = NSValue(scnVector4: SCNVector4(x: 1, y: 0, z: 0, w: CGFloat.pi * 2))
spin.toValue = NSValue(scnVector4: SCNVector4(x: 1, y: 0, z: 0, w: 0))
2
On

It will be something like this

self.imageView.layer.transform = CATransform3DConcat(self.imageView.layer.transform, CATransform3DMakeRotation(M_PI,1.0,0.0,0.0));
0
On

You can rotate your Earth model about X-axis using SceneKit's Transaction:

let scene = SCNScene(named: "art.scnassets/model.scn")!
let modelEarth = scene.rootNode.childNode(withName: "model", 
                                          recursively: true)!

SCNTransaction.begin()
SCNTransaction.animationDuration = 500           // animation in seconds
SCNTransaction.animationTimingFunction = .init(name: .default)
modelEarth.rotation.x = 1
modelEarth.rotation.y = 0
modelEarth.rotation.z = 0
modelEarth.rotation.w = 100 * Float.pi           // fourth component
SCNTransaction.commit()