ios - Make an object orbit another in SceneKit -


say have 2 nodes in scenekit scene. want 1 rotate around or orbit (like planet orbiting star), other node once in time interval. know can set animations so:

let anim = cabasicanimation(keypath: "rotation") anim.fromvalue = nsvalue(scnvector4: scnvector4(x: 0, y: 1, z: 0, w: 0)) anim.tovalue = nsvalue(scnvector4: scnvector4(x: 0, y: 1, z: 0, w: float(2 * double.pi))) anim.duration = 60      anim.repeatcount = .infinity parentnode.addanimation(aim, forkey: "spin around") 

is there animation "orbiting", , way specify target node?

the way using additional (helper) scnnode. you'll use fact adds own coordinate system , of child nodes move (helper) coordinate system. child nodes off-centre orbiting if view them world coordinate system.

  1. you add helpernode @ centre of fixedplanetnode (orbited planet), perhaps child, @ same position

  2. you add orbitingplanetnode child helpernode, offset on 1 of axes, e.g. 10 points on x axis

  3. you start helpernode rotating (together coordinate system) around different axis, e.g. y axis

  4. this result in orbitingplanetnode orbiting around y axis of helpernode orbit radius of 10 points.

example

earthnode - fixed orbited planet

moonnode - orbiting planet

helpernode - helper node added provide coordinate system

// assuming planet geometry @ centre of corresponding nodes // helpernode.position set (0, 0, 0) [earthnode addchildnode:helpernode]; moonnode.position = scnvector3make(10, 0, 0); [helpernode addchildnode:moonnode];  // set helpernode rotate forever scnaction * rotation = [scnaction rotatebyx:0 y:3 z:0]; scnaction * infiniterotation = [scnaction repeatactionforever:rotation]; [helpernode runaction:infiniterotation]; 

i used actions , objective-c familiar with, should doable in swift , animations.


Comments

Popular posts from this blog

javascript - Clear button on addentry page doesn't work -

c# - Selenium Authentication Popup preventing driver close or quit -

tensorflow when input_data MNIST_data , zlib.error: Error -3 while decompressing: invalid block type -