ios - How to rotate an SCNBox -


i'm trying rotate scnbox created using swipe gestures. example, when swipe right box should rotate 90degs in y-axis , -90degs when swipe left. achieve have been using node's scnaction.rotatebyx method perform rotation animation. problem i'm having when rotating along either x-axis or z-axis after rotation in y-axis , vice-versa positions of axes change.

what have notice rotation perform on either of x,y,z axes changes direction in other axes point.

example: default position

enter image description here

then after rotation in z-axis:

enter image description here

of course pose problem because when swipe left or right no longer desire effect because x-axis , y-axis have swapped positions. know why happen? , there anyway perform rotation animation without affecting other axes?

i apologize lack of understanding on subject first go @ 3d graphics.

solution:

func swiperight(recognizer: uitapgesturerecognizer) {   // rotation animation   let action = scnaction.rotatebyx(0, y: cgfloat(glkmathdegreestoradians(90)), z: 0, duration: 0.5)   boxnode.runaction(action)    //repositoning of x,y,z axes after rotation has been applied   let currentpivot = boxnode.pivot   let changepivot = scnmatrix4invert(boxnode.transform)   boxnode.pivot = scnmatrix4mult(changepivot, currentpivot)   boxnode.transform = scnmatrix4identity } 

i haven't ran problems yet may safer use completion handler ensure changes x,y,z axes done before repositioning them.

i had same issue, here's use give desired behavior:

func pangesture(sender: uipangesturerecognizer) {     let translation = sender.translationinview(sender.view!)      let pan_x = float(translation.x)     let pan_y = float(-translation.y)     let anglepan = sqrt(pow(pan_x,2)+pow(pan_y,2))*(float)(m_pi)/180.0     var rotvector = scnvector4()      rotvector.x = -pan_y     rotvector.y = pan_x     rotvector.z = 0     rotvector.w = anglepan      // apply model container node     boxnode.rotation = rotvector      if(sender.state == uigesturerecognizerstate.ended) {         let currentpivot = boxnode.pivot         let changepivot = scnmatrix4invert(boxnode.transform)         boxnode.pivot = scnmatrix4mult(changepivot, currentpivot)         boxnode.transform = scnmatrix4identity     } } 

Comments