Download

How is "Find Look at Rotation" function is implemented?

Could someone gimme this function without dependence on UnrealEngine. Or write steps how to recreate it. I appreciate any help.

This probably won’t help much (I hope you can read C++).

Under the hood, FindLookAtRotation is:

FRotator UKismetMathLibrary::FindLookAtRotation(const FVector& Start, const FVector& Target)
{
        return MakeRotFromX(Target - Start);
}

Which calls:

FRotator UKismetMathLibrary::MakeRotFromX(const FVector& X)
{
        return FRotationMatrix::MakeFromX(X).Rotator();
}

Which calls:

FMatrix FRotationMatrix::MakeFromX(FVector const& XAxis)
{
        FVector const NewX = XAxis.GetSafeNormal();

        // try to use up if possible
        FVector const UpVector = ( FMath::Abs(NewX.Z) < (1.f - KINDA_SMALL_NUMBER) ) ? FVector(0,0,1.f) : FVector(1.f,0,0);

        const FVector NewY = (UpVector ^ NewX).GetSafeNormal();
        const FVector NewZ = NewX ^ NewY;

        return FMatrix(NewX, NewY, NewZ, FVector::ZeroVector);
}

FMatrix::Rotator is implemented as:

FRotator FMatrix::Rotator() const
{
        const FVector           XAxis   = GetScaledAxis( EAxis::X );
        const FVector           YAxis   = GetScaledAxis( EAxis::Y );
        const FVector           ZAxis   = GetScaledAxis( EAxis::Z );

        FRotator        Rotator = FRotator( 
                                                                        FMath::Atan2( XAxis.Z, FMath::Sqrt(FMath::Square(XAxis.X)+FMath::Square(XAxis.Y)) ) * 180.f / PI, 
                                                                        FMath::Atan2( XAxis.Y, XAxis.X ) * 180.f / PI, 
                                                                        0 
                                                                );
        
        const FVector           SYAxis  = FRotationMatrix( Rotator ).GetScaledAxis( EAxis::Y );
        Rotator.Roll            = FMath::Atan2( ZAxis | SYAxis, YAxis | SYAxis ) * 180.f / PI;

        Rotator.DiagnosticCheckNaN();
        return Rotator;
}

By the way:

+ is addition, - is subtraction, * is multiplication, / is division, ^ is cross product, and | is dot product (with vectors).

You’ll probably want to look into how transformation matrices work.

1 Like