Duncan Groenewald
Duncan Groenewald

Reputation: 9018

Smooth animation with SceneKit on OS X

I am trying to simulate the orientation of a body using accelerometer data and am having difficulty getting a smooth animation.

I have a large file of accelerometer data and want to animate the orientation using the pitch and roll values calculated from the accelerometer data. The data is sampled at constant time intervals and I figured I could approximate that by setting the duration = to the sample time which could vary between 80ms and 640ms.

The code below does not seem to work and changing the duration seems to have no effect on the time taken to animate the transition to the new orientation.

What would the correct way be to animate the orientation changes of the body and also be able to adjust the simulation speed such that it approximates the actual time or some multiple thereof?

func animate(scan: ScanData) {

    if let accel = scan.next() {
        let orientation = accel.orientation
        let start = scan.startTimeString
        let time = StringFromDate(scan.dateFromTime(orientation.ms)!)        

        let pitch = Double(DegreesToRadians(0.0))
        let yaw = Double(DegreesToRadians(CGFloat(orientation.roll)))
        let roll = Double(DegreesToRadians(CGFloat(orientation.pitch)))

        CATransaction.begin()
            CATransaction.setCompletionBlock({
                animate(scan)
            })

            bodyNode.eulerAngles = SCNVector3(pitch, yaw, roll)

            CATransaction.setAnimationDuration(0.16)
        CATransaction.commit()

    } else {
        print("No more data")
        return
    }
}

Upvotes: 0

Views: 211

Answers (1)

Toyos
Toyos

Reputation: 4064

For SceneKit implicit animations use SCNTransaction (not CATransaction).

Upvotes: 2

Related Questions