I would like to have the opposite of RotateVector, that takes two vectors as input and returns a rotator. So not UnrotateVector.
In the following example, I know the return value, the input value A, and I want to know the B value. I think it’s basic maths, but I got a bit lost in the C++ code and the rotation/transformation matrix. Can someone help me ?
Thanks.
Motanum
(Motanum)
April 3, 2017, 2:56pm
2
Hi,
I think you want Find Look At Rotator. It finds the rotation of going from Point A, to Point B.
Then there is also Make Rotator from Axis, which takes 3 vectors and gets one rotator.
http://puu.sh/v7Xps/6cda995306.png
I tried these, and while they might be useful for what I need, they aren’t enough.
I tried to explore the C++ source code, but I’m giving up. It’s very complicated behind RotateVector
…
FVector FRotator::RotateVector(const FVector& V) const
{
return FRotationMatrix(*this).TransformVector( V );
}
FRotationMatrix(const FRotator& Rot)
: FRotationTranslationMatrix(Rot, FVector::ZeroVector)
{ }
FRotationTranslationMatrix::FRotationTranslationMatrix(const FRotator& Rot, const FVector& Origin)
{
float SP, SY, SR;
float CP, CY, CR;
FMath::SinCos(&SP, &CP, FMath::DegreesToRadians(Rot.Pitch));
FMath::SinCos(&SY, &CY, FMath::DegreesToRadians(Rot.Yaw));
FMath::SinCos(&SR, &CR, FMath::DegreesToRadians(Rot.Roll));
M[0][0] = CP * CY;
M[0][1] = CP * SY;
M[0][2] = SP;
M[0][3] = 0.f;
M[1][0] = SR * SP * CY - CR * SY;
M[1][1] = SR * SP * SY + CR * CY;
M[1][2] = - SR * CP;
M[1][3] = 0.f;
M[2][0] = -( CR * SP * CY + SR * SY );
M[2][1] = CY * SR - CR * SP * SY;
M[2][2] = CR * CP;
M[2][3] = 0.f;
M[3][0] = Origin.X;
M[3][1] = Origin.Y;
M[3][2] = Origin.Z;
M[3][3] = 1.f;
}
FVector4 FMatrix::TransformVector(const FVector& V) const
{
return TransformFVector4(FVector4(V.X,V.Y,V.Z,0.0f));
}
// Homogeneous transform.
FVector4 FMatrix::TransformFVector4(const FVector4 &P) const
{
FVector4 Result;
VectorRegister VecP = VectorLoadAligned(&P);
VectorRegister VecR = VectorTransformVector(VecP, this);
VectorStoreAligned(VecR, &Result);
return Result;
}
/**
* Calculate Homogeneous transform.
*
* @param VecP VectorRegister
* @param MatrixM FMatrix pointer to the Matrix to apply transform
* @return VectorRegister = VecP*MatrixM
/
FORCEINLINE VectorRegister VectorTransformVector(const VectorRegister& VecP, const void MatrixM )
{
const VectorRegister *M = (const VectorRegister *) MatrixM;
VectorRegister VTempX, VTempY, VTempZ, VTempW;
// Splat x,y,z and w
VTempX = VectorReplicate(VecP, 0);
VTempY = VectorReplicate(VecP, 1);
VTempZ = VectorReplicate(VecP, 2);
VTempW = VectorReplicate(VecP, 3);
// Mul by the matrix
VTempX = VectorMultiply(VTempX, M[0]);
VTempY = VectorMultiply(VTempY, M[1]);
VTempZ = VectorMultiply(VTempZ, M[2]);
VTempW = VectorMultiply(VTempW, M[3]);
// Add them all together
VTempX = VectorAdd(VTempX, VTempY);
VTempZ = VectorAdd(VTempZ, VTempW);
VTempX = VectorAdd(VTempX, VTempZ);
return VTempX;
}
Motanum
(Motanum)
April 4, 2017, 2:08pm
6
The math’s behind rotators are quaternions IIRC. Quaternion - Wikipedia .
Mahmood215
(Mahmood215)
September 30, 2018, 1:48pm
7
Hi, You may want this:
FRotator::UnRotateVector
Mahmood215
(Mahmood215)
September 30, 2018, 1:48pm
8
Hi, You may want this:
FRotator::UnRotateVector
FRotator VecRotDiff(const FVector& A, const FVector& Target)
{
// GetNormalized is optional. It gets the “shortest rotation path” (e.g. not turning 270° but -90° instead)
return (Target.Rotation() - A.Rotation()).GetNormalized();
}
Is this what you looking for?