Scenekit PhysicEngine follow rolling ball

2019-05-11 03:48发布

问题:

I want to follow a rotating sphere in Apple's SceneKit. I've added an LookAt Constraint to the camera and as the sphere falls down the cam aleays points to it but if the sphere rolls away the camera stays at its current position. I want the cam to follow this sphere like in a third persond shooter with a predefined distance to it. If I make the cam a childNode of the sphere the cam "orbits" around it as the ball is rolling. Any ideas how I can follow the ball with the cam?

回答1:

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

I'm not too 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       
}