swift - SceneKit physics add velocity in local space -


i trying manipulate player node using physicsbody.velocity adding or subtracting velocity axis different directions. problem i'm having trouble finding method applying local space or @ least applying velocity in relation direction object facing. in other words, works fine if have not rotated object's node. if still add velocity unrotated space. know there way add velocity current scnvector3, cannot figure out.

if iszthrustpositive {         if let velocity = self.physicsbody?.velocity {             if velocity.z * 100 <= 8000 {                 thrustdirection = scnvector3(                     x: velocity.x,                     y: velocity.y,                     z: velocity.z + kplayershipmainthrust)                 self.physicsbody?.velocity = thrustdirection             }         }     } 

i trying rotate node using angularvelocity in similar fashion. works fine long node has not moved or rotated. if has, seems using world space well.

if isyrotatingpositive {         if let angularvel = self.physicsbody?.angularvelocity {             self.physicsbody?.angularvelocity = scnvector4(                 x: angularvel.x,                 y: angularvel.y + kplayershiprotationspeed,                 z: angularvel.z,                 w: angularvel.w + kplayershiprotationmagnitude)         }     } 

after physics simulated, updating node's position , rotation. have tried convertposition, not figure out way make work. (i got crazy results this). appreciated.

update got playership object add velocity in proper directions when rotated, still unable perform multiple rotations without not turning in correct direction. ship velocity did this:

if isxthrustpositive {         thrustdirection = self.convertposition(scnvector3(x: kplayershipmainthrust, y: 0.0, z: 0.0), tonode: self.parentnode!)         thrustdirection.x = thrustdirection.x - self.position.x         thrustdirection.y = thrustdirection.y - self.position.y         thrustdirection.z = thrustdirection.z - self.position.z         self.physicsbody?.applyforce(thrustdirection, impulse: true)     } 

when trying use similar method rotation (i know doomed), resulting scnvector3 position, not direction node facing. there information on converttransform , how might use that?

as turns out, had proper position rootnode of scene perform proper rotation based on current orientation of scnnode.

xaxis = self.convertposition(scnvector3make(1.0, 0.0, 0.0), tonode: self.parentnode!) yaxis = self.convertposition(scnvector3make(0.0, 1.0, 0.0), tonode: self.parentnode!) zaxis = self.convertposition(scnvector3make(0.0, 0.0, 1.0), tonode: self.parentnode!) if isxrotatingpositive {     self.physicsbody?.applytorque(         scnvector4(             x: sin(kplayershiprotationspeed/2.0) * (xaxis.x - self.position.x),             y: sin(kplayershiprotationspeed/2.0) * (xaxis.y - self.position.y),             z: sin(kplayershiprotationspeed/2.0) * (xaxis.z - self.position.z),             w: cos(kplayershiprotationspeed/2.0) * kplayershiprotationmagnitude),         impulse: true)     } 

then used standard quaternion rotation formula rotation based on new axes current position. hope helps else (and more information on scenekit forthcoming) if scenekit experts want comment on or offer suggestions, appreciated. :)


Comments

Popular posts from this blog

php - Admin SDK -- get information about the group -

dns - How To Use Custom Nameserver On Free Cloudflare? -

Python Error - TypeError: input expected at most 1 arguments, got 3 -