SCNNode Animation Forever - SceneKit, Swift

I want to animate 2 cubes forever until the user hits one of them. The animation is supposed to be similar to ∞.

These are my cubes in sceneView that I want to animate: cubes to be animated

I am trying to do this with nested animations, and this partially works. Now, since animations always wait until they end before starting a new one, it doesn't look smooth.

Before proceeding with the development of ∞ animation, I would like to know if there is a better (and actually working) way. This is my current code:

SCNTransaction.begin() SCNTransaction.setAnimationDuration(2) cube1.position = SCNVector3Make(3, 0, 0) cube2.position = SCNVector3Make(-3, 0, 0) println("log 0") SCNTransaction.setCompletionBlock { println("log 1") while (animationKey != 1) { //initially animationKey = 0 SCNTransaction.begin() SCNTransaction.setAnimationDuration(2) cube1.position.x += 1 cube1.position.y += 1 cube2.position.x += 1 cube2.position.y += 1 println("log 2") SCNTransaction.setCompletionBlock { SCNTransaction.begin() SCNTransaction.setAnimationDuration(2) cube1.position.x -= 1 cube1.position.y -= 1 cube2.position.x -= 1 cube2.position.y -= 1 println("log 3") SCNTransaction.setCompletionBlock { animationKey = 1 } SCNTransaction.commit() } println("log 4") SCNTransaction.commit() } } println("log 5") SCNTransaction.commit() println("log 6") } 

The output shows the following things: log 0, log 5, log 6, log 1, log 2, log 4, log 2 ... and then it just continues alternating between log 4 and log 2 .

Can anybody help me?

Note

  • I thought a recursive approach would be better, so I replaced animationKey = 1 with a call to the method it is in ... didn't work anymore.

  • I got the animation to work once, but now it just stops after the very first animation is completed and completely ignores the second and third animation blocks ...

+6
source share
2 answers

If you are going to animate SCNNode , your nodes will trigger SCNAction .

Swift:

 let moveUp = SCNAction.moveBy(x: 0, y: 1, z: 0, duration: 1) moveUp.timingMode = .easeInEaseOut; let moveDown = SCNAction.moveBy(x: 0, y: -1, z: 0, duration: 1) moveDown.timingMode = .easeInEaseOut; let moveSequence = SCNAction.sequence([moveUp,moveDown]) let moveLoop = SCNAction.repeatForever(moveSequence) myNode.runAction(moveLoop) 

Objective-C:

 SCNAction *moveUp = [SCNAction moveByX:0 Y:1 Z:0 duration:1]; moveUp.timingMode = SCNActionTimingModeEaseInEaseOut; SCNAction *moveDown = [SCNAction moveByX:0 Y:-1 Z:0 duration:1]; moveDown.timingMode = SCNActionTimingModeEaseInEaseOut; SCNAction *moveSequence = [SCNAction sequence:@[moveUp,moveDown]]; SCNAction *moveLoop = [SCNAction repeatActionForever:moveSequence]; [myNode runAction:moveLoop]; 

The above code will animate myNode up and down forever.

+16
source

you can create a CAKeyframeAnimation animation from a Bezier path (see the path property). This way you get a very smooth animation.

To make it repeat forever, set its repeatCount to repeatCount , and you can adjust the speed using the timingFunction property.

+2
source

Source: https://habr.com/ru/post/985210/


All Articles