矩阵围绕任意轴旋转问题
问题描述:
我一直试图围绕任意轴工作得到矩阵旋转,我想我很接近但是我有一个bug。我对3D旋转比较陌生,对发生的事情有基本的了解。矩阵围绕任意轴旋转问题
public static Matrix4D Rotate(Vector3D u, Vector3D v)
{
double angle = Acos(u.Dot(v));
Vector3D axis = u.Cross(v);
double c = Cos(angle);
double s = Sin(angle);
double t = 1 - c;
return new Matrix4D(new double [,]
{
{ c + Pow(axis.X, 2) * t, axis.X * axis.Y * t -axis.Z * s, axis.X * axis.Z * t + axis.Y * s, 0 },
{ axis.Y * axis.X * t + axis.Z * s, c + Pow(axis.Y, 2) * t, axis.Y * axis.Z * t - axis.X * s, 0 },
{ axis.Z * axis.X * t - axis.Y * s, axis.Z * axis.Y * t + axis.X * s, c + Pow(axis.Z, 2) * t, 0 },
{ 0, 0, 0, 1 }
});
}
上面的代码是矩阵旋转的算法。当我用单位向量测试算法时,我得到以下结果:
Matrix4D rotationMatrix = Matrix4D.Rotate(new Vector3D(1, 0, 0), new Vector3D(0, 0, 1));
Vector4D vectorToRotate = new Vector4D(1,0,0,0);
Vector4D result = rotationMatrix * vectorToRotate;
//Result
X = 0.0000000000000000612;
Y = 0;
Z = 1;
Length = 1;
随着90度的旋转,我发现它几乎完美的工作。现在,让我们来看看一个45度旋转:
Matrix4D rotationMatrix = Matrix4D.Rotate(new Vector3D(1, 0, 0), new Vector3D(1, 0, 1).Normalize());
Vector4D vectorToRotate = new Vector4D(1,0,0,0);
Vector4D result = rotationMatrix * vectorToRotate;
//Result
X = .70710678118654746;
Y = 0;
Z = .5;
Length = 0.8660254037844386;
当我们乘坐ATAN(0.5/0.707),我们发现,我们有35.28度的旋转,而不是45度旋转。矢量的长度也从1变为.866。有没有人有任何提示我做错了什么?
答
你的矩阵代码看起来是正确的,但你需要正常化轴太(只是因为u
和v
归一化并不意味着u cross v
也是)。
(我也建议使用一个简单的乘法,而不是Pow
,性能和精度的原因,但这是次要的,而不是你的问题的来源)
答
只是给后人,这是我使用的代码对于这个确切的事情。我总是推荐使用Atan2(dy,dx)
而不是Acos(dx)
,因为它在接近90°的角度更稳定。
public static class Rotations
{
public static Matrix4x4 FromTwoVectors(Vector3 u, Vector3 v)
{
Vector3 n = Vector3.Cross(u, v);
double sin = n.Length(); // use |u×v| = |u||v| SIN(θ)
double cos = Vector3.Dot(u, v); // use u·v = |u||v| COS(θ)
// what if u×v=0, or u·v=0. The full quadrant arctan below is fine with it.
double angle = Atan2(sin, cos);
n = Vector3.Normalize(n);
return FromAxisAngle(n, angle);
}
public static Matrix4x4 FromAxisAngle(Vector3 n, double angle)
{
// Asssume `n` is normalized
double x = n.X, y = n.Y, z = n.Z;
double sin = Sin(angle);
double vcos = 1.0-Cos(angle);
return new Matrix4x4(
1.0-vcos*(y*y+z*z), vcos*x*y-sin*z, vcos*x*z+sin*y, 0,
vcos*x*y+sin*z, 1.0-vcos*(x*x+z*z), vcos*y*z-sin*x, 0,
vcos*x*z-sin*y, vcos*y*z+sin*x, 1.0-vcos*(x*x+y*y), 0,
0, 0, 0, 1.0);
}
}
代码为C#
让我补充一点,让使用'ACOS(u.Dot(V))'是不是很好的角度,因为如果两个向量是几乎垂直的结果是不稳定。使用'Atan2((u.Cross(v))。Magnitude,u.Dot(v))'代替。 – ja72