2015-03-31 20 views
1

我試圖轉換Unity和Threejs之間的歐拉角度旋轉。這有兩個主要問題。使用不同的座標軸(Unity和Threejs)來轉換系統之間的歐拉角度

問題1:

統一和Threejs具有不同的座標系統

統一:

enter image description here

Threejs

enter image description here

問題2:

Unity按照ZXY的順序確定euler數學,而Threejs默認爲XYZ。我在Threejs方面發現了一些使用不同乘法次序創建歐拉角的公式,但是我想知道這個數學背後的數學問題,所以我可以在兩個系統之間來回切換。我也不確定不同的座標系如何在這個轉換數學中起作用。

編輯1

我發現這個堆棧溢出後有關轉換爲Unity四元數Threejs:

Convert Unity transforms to THREE.js rotations

但是,我沒能得到這個代碼來換去工作Threejs到Unity的相反方向,這是我需要的。

+0

是座標系統之間翻轉一樣簡單Z * -1? – Donnelle 2015-03-31 21:49:16

回答

0

我終於找到了一個解決方案,使用下面的鏈接。可能有一個更簡單的解決方案,但沒有別的我試過給了我預期的效果。值得注意的是,這是用一個Threejs相機測試的,該相機是-z面向+ y的位置。我的團結相機是-z面對+ y面朝上。如果你有一個+ z面向攝像頭,這在Unity中很常見,只需將GameObject設爲一個空的GameObject,然後將180度歐拉旋轉應用到空的GameObject。這也假設Threejs Euler旋轉是默認的XYZ排序。

http://answers.unity3d.com/storage/temp/12048-lefthandedtorighthanded.pdf

http://en.wikipedia.org/wiki/Euler_angles

http://forum.unity3d.com/threads/how-to-assign-matrix4x4-to-transform.121966/

/// <summary> 
    /// Converts the given XYZ euler rotation taken from Threejs to a Unity Euler rotation 
    /// </summary> 
    public static Vector3 ConvertThreejsEulerToUnity(Vector3 eulerThreejs) 
    { 
     eulerThreejs.x *= -1; 
     eulerThreejs.z *= -1; 
     Matrix4x4 threejsMatrix = CreateRotationalMatrixThreejs(ref eulerThreejs); 

     Matrix4x4 unityMatrix = threejsMatrix; 
     unityMatrix.m02 *= -1; 
     unityMatrix.m12 *= -1; 
     unityMatrix.m20 *= -1; 
     unityMatrix.m21 *= -1; 

     Quaternion rotation = ExtractRotationFromMatrix(ref unityMatrix); 
     Vector3 eulerRotation = rotation.eulerAngles; 
     return eulerRotation; 
    } 

    /// <summary> 
    /// Creates a rotation matrix for the given threejs euler rotation 
    /// </summary> 
    private static Matrix4x4 CreateRotationalMatrixThreejs(ref Vector3 eulerThreejs) 
    { 
     float c1 = Mathf.Cos(eulerThreejs.x); 
     float c2 = Mathf.Cos(eulerThreejs.y); 
     float c3 = Mathf.Cos(eulerThreejs.z); 
     float s1 = Mathf.Sin(eulerThreejs.x); 
     float s2 = Mathf.Sin(eulerThreejs.y); 
     float s3 = Mathf.Sin(eulerThreejs.z); 
     Matrix4x4 threejsMatrix = new Matrix4x4(); 
     threejsMatrix.m00 = c2 * c3; 
     threejsMatrix.m01 = -c2 * s3; 
     threejsMatrix.m02 = s2; 
     threejsMatrix.m10 = c1 * s3 + c3 * s1 * s2; 
     threejsMatrix.m11 = c1 * c3 - s1 * s2 * s3; 
     threejsMatrix.m12 = -c2 * s1; 
     threejsMatrix.m20 = s1 * s3 - c1 * c3 * s2; 
     threejsMatrix.m21 = c3 * s1 + c1 * s2 * s3; 
     threejsMatrix.m22 = c1 * c2; 
     threejsMatrix.m33 = 1; 
     return threejsMatrix; 
    } 

    /// <summary> 
    /// Extract rotation quaternion from transform matrix. 
    /// </summary> 
    /// <param name="matrix">Transform matrix. This parameter is passed by reference 
    /// to improve performance; no changes will be made to it.</param> 
    /// <returns> 
    /// Quaternion representation of rotation transform. 
    /// </returns> 
    public static Quaternion ExtractRotationFromMatrix(ref Matrix4x4 matrix) 
    { 
     Vector3 forward; 
     forward.x = matrix.m02; 
     forward.y = matrix.m12; 
     forward.z = matrix.m22; 

     Vector3 upwards; 
     upwards.x = matrix.m01; 
     upwards.y = matrix.m11; 
     upwards.z = matrix.m21; 

     return Quaternion.LookRotation(forward, upwards); 
    } 
0

問題1:

授予我只輔修數學,但我相信你應該僅僅是能夠做到的直線映射要點如下:

(X,Y,Z)=>(X ,Y,-Z)

而且這應該兩種方式。

據我記得,一旦你在座標之間進行轉換,數學應該是相同的,只要確保你在一個系統或另一個系統中工作,讓你的生活更輕鬆。然後,您可以根據需要將結果導回。

+0

因爲我們在談論這兩個系統中的旋轉,所以這不起作用。如果我們在談論位置差異,這可能會起作用,但它不會用於歐拉角旋轉。 – Matt 2015-03-31 21:50:59

+0

你看過旋轉矩陣嗎?我認爲這可能是解決兩個衝突的好方法,因爲它們可以用來抽象循環的順序。然後,您應該留下簡單的座標轉換。 – CShannon 2015-03-31 21:55:03

+0

我一直在研究旋轉矩陣。我可以使用這個wiki將Threejs的歐拉角變成旋轉矩陣: http://en.wikipedia.org/wiki/Euler_angles 在這一點上,我試圖將矩陣從右手轉換爲左手使用雙手座標系統: http://stackoverflow.com/questions/1263072/changing-a-matrix-from-right-handed-to-left-handed-coordinate-system 在這一點上,我試圖提取旋轉作爲四元數返回矩陣。然而,最後2個步驟中的其中一步沒有起作用,因爲旋轉不對齊。如果可以,請檢查我的編輯 – Matt 2015-04-01 21:22:02

相關問題