ios - 3rd Person Camera in SceneKit -


i need attach scncamera scnnode whenever node (ball) moves, camera follows it.

so far managed moving camera when physics updated, not efficient , has delay.

    func physicsworld(world: scnphysicsworld, didbegincontact contact: scnphysicscontact)     {         var camerap = scnvector3(x: ball.presentationnode().position.x, y: ball.presentationnode().position.y + 5, z: ball.presentationnode().position.z + 12)         camera.constraints = nil         camera.position = camerap         camera.constraints = [scnlookatconstraint(target: ball)]     } 

i read information scntransformationconstraint "attach" camera node found no working example.

i can't attach camera node want adjust position of camera.

thanks.

edit: (to moustach)

i tried using:

optional func renderer(_ renderer: scnscenerenderer, didsimulatephysicsattime time: nstimeinterval) {     println("called") } 

but error saying: extraneous '_' in parameter: 'rendered' has no keyword argument name

edit 2 (working):

with moustach's advice, able make work efficiently.

the camera position updated on:

func renderer(arenderer: scnscenerenderer, didsimulatephysicsattime time: nstimeinterval) {     println("called") } 

furthermore, new scnnode created rootnode of camera node hold position.

setting camera looks this:

        let lookatballconstraint = scnlookatconstraint(target: ball)         lookatballconstraint.gimballockenabled = true          let tempcam = scncamera()         tempcam.zfar = 5000         camera = scnnode()         camera.camera = tempcam         camera.position = scnvector3(x: 0, y: 0, z: 0)         camera.constraints = [lookatballconstraint]          cameraposition = scnnode()         cameraposition.position = scnvector3(x: 0, y: 5, z: 12)         cameraposition.addchildnode(camera) 

and camera moving function looks this:

func renderer(arenderer: scnscenerenderer, didsimulatephysicsattime time: nstimeinterval) {     var camerap = scnvector3(x: ball.presentationnode().position.x, y: ball.presentationnode().position.y + 5, z: ball.presentationnode().position.z + 12)     cameraposition.position = camerap } 

it seems adding new constraint every time ball touches anything, happens lot every frame!

here's should do:

  • move constraint creation somewhere set once, such init
  • move position update code renderer delegate called once frame, after physics have been calculated.

you should see huge performance update!

edit: try this

func renderer(arenderer: scnscenerenderer, didsimulatephysicsattime time: nstimeinterval){     println("called") } 

Comments