任意の軸を中心に行列の回転を試行していますが、私は近いと思いますが、バグがあります。私は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度の回転を見てみましょう:私たちはATAN(0.5/0.707)を取るとき
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;
我々は代わりに45度回転の35.28度回転を持っていることがわかります。また、ベクトルの長さは1から866に変化しています。誰かが私が間違っていることについてのヒントを持っていますか?
です。代わりに 'Atan2((u.Cross(v))。Magnitude、u.Dot(v))'を使用してください。 – ja72