Scenekit PhysicEngine after a rolling ball

I want to keep track of a rotating sphere in Apple SceneKit. I added a LookAt Constraint to the camera, and when the sphere falls down, the cam points towards it, but if the sphere moves away, the camera remains at its current position. I want the cam to follow this sphere like in a third handgun with a predetermined distance to it. If I make the cam a childNode of the sphere, the cam "spins" around it as the ball rolls. Any ideas how I can follow the ball using the camera?

+3


source to share


1 answer


It's pretty simple. You just need to change the position of the camera node on every frame on the presentationNode ball plus the offset to avoid it inside.

I'm not very familiar with Swift, but the code would look something like this:



func renderer(aRenderer: SCNSceneRenderer, didSimulatePhysicsAtTime time: NSTimeInterval){
    var ballP = ballNode.presentationNode.position
    // Offset the camera up and on X:
    var cameraP = SCNVector3(x: ballP.x+5, y: ballP.y+10, z: ballP.z)
    cameraNode.position = cameraP       
}

      

+1


source







All Articles