Recently, I’m trying to do something needs to convert a 4x4 matrix to FTransform. Of course, FMatrix is the right class to use, and I make a simple translate matrix like this:
1, 0, 0 10
0, 1, 0, 20
0, 0, 1, 30
0, 0, 0, 1
Generally, this matrix means translate by a Vector(10, 20, 30), at least that’s my understanding, but, I got Vector(0, 0, 0) for translate, I’m confused, so I go though relevant source code. I found this function FMatrix::SetFromMatrix
// /Engine/Source/Runtime/Core/Public/Math/TransformVectorized.h#L1209
void SetFromMatrix(const FMatrix& InMatrix)
{
FMatrix M = InMatrix;
// Get the 3D scale from the matrix
FVector InScale = M.ExtractScaling();
Scale3D = VectorLoadFloat3_W0(&InScale);
// If there is negative scaling going on, we handle that here
if(InMatrix.Determinant() < 0.f)
{
// Assume it is along X and modify transform accordingly.
// It doesn't actually matter which axis we choose, the 'appearance' will be the same
Scale3D = VectorMultiply(Scale3D, GlobalVectorConstants::FloatMinus1_111 );
M.SetAxis(0, -M.GetScaledAxis( EAxis::X ));
}
FQuat InRotation = FQuat(M);
Rotation = VectorLoadAligned(&InRotation);
FVector InTranslation = InMatrix.GetOrigin(); // this line for translation so let's go to GetOrigin
Translation = VectorLoadFloat3_W0(&InTranslation);
// Normalize rotation
Rotation = VectorNormalizeQuaternion(Rotation);
}
and FMatrix::GetOrigin
// /Engine/Source/Runtime/Core/Public/Math/Matrix.inl#L525
inline FVector FMatrix::GetOrigin() const
{
return FVector(M[3][0],M[3][1],M[3][2]);
}
Is that right? If I remembered clearly, M[0][3],M[1][3],M[2][3]
is the right vector for translation, acoording to wikipedia, am I right?