2016-02-13 60 views
1

假設在三維空間中有兩個點。請撥打第一個o作爲來源,其他t作爲目標。每個旋轉軸都與世界/父母座標系(以及彼此)進行了對齊。將第三個點r與原點,相同的位置和旋轉重合。swift:使y軸朝向三維空間中的另一個點

如何在Swift中旋轉r,使其y軸指向t?如果指出z軸更容易,我會採取這種做法。其他兩個軸的結果取向對我的需求來說並不重要。

enter image description here

我經歷過與此相關的許多討論,但沒有滿足。我從閱讀和經驗中瞭解到,歐拉角度可能不是要走的路。無論如何,我們並沒有在微積分中包括這一點,那是50年前的事情。

+0

好吧,我很困惑。 3D點不具有旋轉,它們具有沿給定軸的x/y/z點。你正在繪製'r',就好像它是一個矢量。你是否在'y'軸上尋找原點和目標之間的梯度? – Michael

+0

對不起。這些意味着是向量表示。我試圖定位的是SCNNode。我不知道術語梯度如何應用於此。謝謝,Byrne – bpedit

回答

2

Got it!添加容器節點非常簡單。以下似乎適用於任何象限中的任何位置。

// pointAt_c is a container node located at, and child of, the originNode 
    // pointAtNode is its child, position coincident with pointAt_c (and originNode) 

    // get deltas (positions of target relative to origin) 
    let dx = targetNode.position.x - originNode.position.x 
    let dy = targetNode.position.y - originNode.position.y 
    let dz = targetNode.position.z - originNode.position.z 

    // rotate container node about y-axis (pointAtNode rotated with it) 
    let y_angle = atan2(dx, dz) 
    pointAt_c.rotation = SCNVector4(0.0, 1.0, 0.0, y_angle) 

    // now rotate the pointAtNode about its z-axis 
    let dz_dx = sqrt((dz * dz) + (dx * dx)) 
     // (due to rotation the adjacent side of this angle is now a hypotenuse) 
    let x_angle = atan2(dz_dx, dy) 
    pointAtNode.rotation = SCNVector4(1.0, 0.0, 0.0, x_angle) 

我需要這個替換lookAt約束,不管怎麼說,這個約束都不能輕易歸檔到節點樹中。我指的是Y軸,因爲這就是SCN圓柱體和膠囊的導向。

如果有人知道如何避免容器節點請告訴。每次我嘗試將順序旋轉應用於單個節點時,最後一次會覆蓋前一個節點。我不知道如何制定一個旋轉表達式來一次完成它。

+0

真棒。這對我來說太棒了! –

相關問題