0

I'm play around with the CAKeyframeAnimation in order to better understanding how this type of animation work.

I want to move my SCNnode in a square shape and at every corner rotate his eulerangle Y of 90 degrees to make it follow the orientation of the track

here my code

    func animatePlaneKey(nodeToAnimate: SCNNode){
//   move forward
        let pos = nodeToAnimate.position
        let animation = CAKeyframeAnimation(keyPath: "position")
        let pos1 = SCNVector3(pos.x, pos.y, pos.z)
        let pos2 = SCNVector3(pos.x + 1 , pos.y, pos.z)
        let pos3 = SCNVector3(pos.x + 1 , pos.y, pos.z + 1) // 1
        let pos4 = SCNVector3(pos.x - 1 , pos.y, pos.z + 1)
        let pos5 = SCNVector3(pos.x, pos.y, pos.z)
        let easeIn  = CAMediaTimingFunction(controlPoints: 0.35, 0.0, 1.0,  1.0)
        animation.values = [pos1,pos2, pos3,pos4,pos5]
        animation.keyTimes = [0,0.25,0.5,0.75,1]
        animation.timingFunctions = [easeIn]
        animation.calculationMode = .cubic
        animation.duration = 12
        animation.repeatCount = 1
        animation.isAdditive = false
        animation.autoreverses = false
     

        // Heading 1st
        let firstTurnAnim = CAKeyframeAnimation(keyPath: "eulerAngles.y")
        let heading = nodeToAnimate.eulerAngles.y

        let rot0heading = heading
        let rot2heading = heading - Float(deg2rad(90))

        firstTurnAnim.values = [rot0heading,rot2heading]
        firstTurnAnim.keyTimes = [0.2,0.3]
        firstTurnAnim.duration = 3
        firstTurnAnim.repeatCount = 1
        firstTurnAnim.isAdditive = true
        firstTurnAnim.autoreverses = false
   
//        // Heading 2st
        let secondTurnAnim = CAKeyframeAnimation(keyPath: "eulerAngles.y")
        let heading2 = nodeToAnimate.eulerAngles.y

        let rot1head0 = heading2
        let rot1head1 = heading2 - Float(deg2rad(180))
        secondTurnAnim.values = [rot1head0,rot1head1]
        secondTurnAnim.keyTimes = [0.45,0.55]
        secondTurnAnim.duration = 6
        secondTurnAnim.repeatCount = 1
        secondTurnAnim.isAdditive = true
        secondTurnAnim.autoreverses = false
        
        nodeToAnimate.addAnimation(animation, forKey: "movement")
        nodeToAnimate.addAnimation(firstTurnAnim, forKey: "turn")
        nodeToAnimate.addAnimation(secondTurnAnim, forKey: "turn2")
        
    }

animationPath

I'm struggling to combine the animation of the y axis at the correct time.

when I add the "turn2" the animation start to mess up everything's, the node appear already rotate in the wrong direction. For my understanding turn2 animation should start at keyframe 0.45 and finish at 0.55, why it start immediately ?

any idea what should be the correct way to combine this animation?

Damiano Miazzi
  • 1,514
  • 1
  • 16
  • 38
  • 1
    Have you tryed to use SCNAction's instead of the CAKeyframeAnimation? Combining them in sequence and using separate sequences to move and rotate your node? (Or is there any need to use explicitly the Keyframe Animation?) – ZAY Nov 17 '21 at 12:08
  • I tried the SCN action but the issue is, I’m not able to combine the animations together at the right time..looks like the action are animated in sequence.. when my SCNnode arrived at the first turn I need to change the rotation on Y and X axis while the node move forward in z axes in simultaneous way to give a better looking of the turn .. if I use SCN action all the movement are in sequence one after the other .. is there any other way I don’t know ‍♂️ to combine scnaction? – Damiano Miazzi Nov 17 '21 at 13:00
  • @DamianoMiazzi SCNAction group will run actions in parallel. https://developer.apple.com/documentation/scenekit/scnaction/1522779-group – James P Nov 17 '21 at 15:52
  • 1
    As far as I know, you can run more than one SCNAction an the node at he same time. Just do node.runAction(ActionOrSequence1); node.runAction(ActionOrSequence2); node.runAction(ActionOrSequence3)- They should just not disturb each other. - or try the other suggestion, using the Group, to pack the Actions together. – ZAY Nov 17 '21 at 17:08
  • Thanks a lot for the advise .. I tried out and looks like working out with action.group and sequence ..-) – Damiano Miazzi Nov 18 '21 at 01:25

0 Answers0