2014-01-17 43 views
2

我正嘗試旋轉的Vector3D這樣的:旋轉的Vector3D

Vector3D i = new Vector3D(1, 1, 0); 
i.Normalize(); 

Matrix3D m = Matrix3D.Identity; 
Quaternion rot = GetShortestRotationBetweenVectors(i, new Vector3D(1, 0, 0)); 
m.Rotate(rot); 

Vector3D j = new Vector3D(0, 1, 0); 
Vector3D jRotated = m.Transform(j); 
// j should be equal to i 

public static Quaternion GetShortestRotationBetweenVectors(Vector3D vector1, Vector3D vector2) 
{ 
vector1.Normalize(); 
vector2.Normalize(); 
float angle = (float)Math.Acos(Vector3D.DotProduct(vector1, vector2)); 
Vector3D axis = Vector3D.CrossProduct(vector2, vector1); 

// Check to see if the angle is very small, in which case, the cross product becomes unstable, 
// so set the axis to a default. It doesn't matter much what this axis is, as the rotation angle 
// will be near zero anyway. 
if (angle < 0.001f) 
{ 
    axis = new Vector3D(0.0f, 0.0f, 1.0f); 
} 

if (axis.Length < .001f) 
{ 
    return Quaternion.Identity; 
} 

axis.Normalize(); 
Quaternion rot = new Quaternion(axis, angle); 

return rot; 
} 

我得到的旋轉矩陣來傳遞從i =(0.77,0.77,0)到(1,0,0)=> 45°。所以(0,1,0)旋轉45°,結果應該是(0.77,0.77,0)。但結果幾乎是相同的原始矢量(0,1,0),所以沒有進行轉換。

如何旋轉矢量?我有一個向量(x,y,z),這個向量應該旋轉到(1,0,0)。對於這個操作,假設我們必須旋轉30º。那麼如何旋轉我擁有這30º的所有矢量?

回答

1

最後我發現了這個問題。

​​

給出弧度的角度。

Quaternion rot = new Quaternion(axis, angle); 

期待度

角度,使該解決方案很簡單:

float angle = (float)(Math.Acos(Vector3D.DotProduct(vector1, vector2)) * (180/Math.PI)); 

這意味着在Avateering-XNA(微軟官方軟件)中的錯誤。