Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rotate SCNNode infinitely 360°

Tags:

ios

scenekit

I'm looking for a way to rotate a note 360 degrees around its z-axis. The node holding the camera moves negatively along the z-axis and is not allowed to change its y & z. I've already tried a CABasicAnimation, but without success.

Can anyone point me to a solution?

like image 332
tomalbrc Avatar asked Dec 19 '14 16:12

tomalbrc


2 Answers

How about rotating it by less than 360°? Since your title says "infinitely" it should work just fine, and repeating the animation over and over will justend up being 360° and more.

Else, you can look at Apple's default SceneKit project, which works great for me:

[cameraNode runAction:[SCNAction repeatActionForever:[SCNAction rotateByX:0 y:.5 z:0 duration:1]]];

Or, for OSX:

CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"rotation"];
animation.toValue = [NSValue valueWithSCNVector4:SCNVector4Make(0, 0, 1, M_PI*2)];
animation.duration = 15;
animation.repeatCount = MAXFLOAT; //repeat forever
[YOURNODE addAnimation:animation forKey:nil];
like image 172
Moustach Avatar answered Sep 20 '22 23:09

Moustach


To rotate an object forever along z-axis.

let node = SCNNode()
self.sceneView.scene.rootNode.addChildNode(node)
//creating an box object
node.geometry = SCNBox(width: 0.1, height: 0.1, length: 0.1, chamferRadius: 0)
node.geometry?.firstMaterial?.diffuse.contents = UIColor.blue
node.position = SCNVector3(0,0,0)
//adding a rotation by Z axis        
let action = SCNAction.rotateBy(x: 0, y: 0, z: CGFloat(GLKMathDegreesToRadians(360)), duration: 8)
let forever = SCNAction.repeatForever(action)
node.runAction(forever)
like image 40
Abdul Karim Khan Avatar answered Sep 19 '22 23:09

Abdul Karim Khan