2017-09-18 52 views
0

我有一個SCNNode - sceneNode - 這是rootNode的孩子,包含我所有的子節點。當用戶點擊一個按鈕時,我想圍繞y軸上的某個點旋轉場景。例如,相機的視角是已知的,我想旋轉相機周圍的所有東西90º。相機不再處於0,0,0。如何繞一個點旋轉SCNNode?

我試過玩SCNMatrix4MakeTranslation函數,然後改變歐拉角度上的y值,但我一直沒有能夠得到它的工作果然。

+0

嘗試用'SKAction.rotateToAngle'方法 –

+0

你的意思是像行星圍繞太陽旋轉的方式嗎? –

回答

0

你的問題很模糊,如果你要將整個場景旋轉90度「在相機周圍」,場景最終會在相機旁邊出現,而你不會看到它。

要將SCNNode繞其自身軸心以外的某個點旋轉(如標題中的問題),創建一個將其移至該點的平移矩陣,將旋轉矩陣與SCNNode相乘,然後乘以逆的翻譯矩陣。

但是,您可能想要做的事情與攝像機節點而不是sceneNode基本相同。這將使相機在sceneNode周圍移動,從而使場景旋轉到位。

例如:

//get the current camera's transform and store it in cammat 
GLKMatrix4 cammat = SCNMatrix4ToGLKMatrix4(self.myView.pointOfView.transform); 

//create a rotation matrix of 90 degrees over axis Y 
GLKQuaternion quaty = GLKQuaternionMakeWithAngleAndAxis(M_PI/2, 0, 1, 0); 
GLKMatrix4 rotMat = GLKMatrix4MakeWithQuaternion(quaty); 

//set the camera transform to rotMat * cammat, which basically rotates the camera first, and then moves it back to the same offset it was. 
self.myView.pointOfView.transform = SCNMatrix4FromGLKMatrix4(GLKMatrix4Multiply(rotMat, cammat));