swift - How to move a rotated SCNNode in SceneKit? -


the image below shows rotated box should moved horizontally on x , z axes. y should stay unaffected simplify scenario. box scnnode of camera, guess projection not make sense @ point.

so lets want move box in direction of red arrow. how achieve using scenekit?

the red arrow indicates -z direction of box. shows not parallel camera's projection or global axes shown dark grey lines of grid.

my last approach product of translation matrix , rotation matrix results in new transformation matrix. have add current transform new transform then?

if yes, scenekit function addition of matrices scnmatrix4mult multiplication or have write myself using metal?

if no, i'm missing out matrix calculations?

i don't want make use of glkit.

enter image description here

so understanding want move box node along own x axis (not it's parents x axis). , because box node rotated, x axis not aligned parent's one, have problem convert translation between 2 coordinate systems.

the node hierarchy is

parentnode     |     |----boxnode // rotated around y (vertical) axis 

using transformation matrices

to move boxnode along its own x axis

// first let's current boxnode transformation matrix scnmatrix4 boxtransform = boxnode.transform;  // let's make new matrix translation +2 along x axis scnmatrix4 xtranslation = scnmatrix4maketranslation(2, 0, 0);  // combine 2 matrices, order matters ! // if swap parameters move in parent's coord system scnmatrix4 newtransform = scnmatrix4mult(xtranslation, boxtransform);  // allply newly generated transform boxnode.transform = newtransform; 

please note: order matters when multiplying matrices

another option:

using scnnode coordinate conversion functions, looks more straight forward me

// boxnode current position in parent's coord system scnvector3 positioninparent = boxnode.position;  // convert coordinate boxnode's own coord system scnvector3 positioninself = [boxnode convertposition:positioninparent fromnode:parentnode];  // translate along own x axis +2 points positioninself = scnvector3make(positioninself.x + 2,                                 positioninself.y,                                 positioninself.z);  // convert parent's coord system positioninparent = [parentnode convertposition: positioninself fromnode:boxnode];  // apply new position boxnode.position = positioninparent; 

Comments