FLookAtMatrix erroneous rotation?

I have some very basic code that aims to create a matrix, at a location, looking in a direction:

HitPosition = FVector(526.0f, 284.0f, 150.0f);
HitNormal = FVector(0.0f, 1.0f, 0.0f);

// Set the component to be at the shatter location
FLookAtMatrix lMatrix = FLookAtMatrix(HitPosition, HitPosition + HitNormal, FVector::UpVector);

FVector zero = FVector::ZeroVector;
zero = lMatrix.TransformPosition(zero);

I would’ve expected that the zero vector, after the transform, should be at 526.0f,284.0f,150.0f

However when I run that code, and look at the result, I see that zero is actually 526.0f,-150.0f,-284.0f

In effect it has rotated it.

Why is this when a transform of a zero vector should always be to the origin of the transform?

Hello PompeyPaul,

It is a view matrix. The main idea is to create such transform that changes axis (Z – is points from camera (screen), X – from left to right (relative to screen), Y – up axis) and set your camera position (EyePosition) to origin. In most cases it uses to change position from world to view system. That’s why in your example zero vector is behind the camera.
For more details check this article. The difference only that view matrix in UE4 is transposed cause different order of multiplication, but result is same.

Best regards,

Oh I see. How do you set a matrix / transform to be at a location and point in a direction? I have been looking at FVector.Rotation() which returns a rotator but doesn’t take a vector Up to maintain.

I could build my own function, doing the cross products etc but it seems like something pretty standard that would be in UDK that I must have missed somewhere.

You can use FRotationTranslationMatrix::Make

static FMatrix Make(const FRotator& Rot, const FVector& Origin)
{
	return FRotationTranslationMatrix(Rot, Origin);
}

Of course during conversion from vector to rotator you will lost ‘roll’.

Is there no function to make the matrix honouring an up vector? I.e.

Make(FVector lLocation, FVector lLookAt, FVector lLookUp)

I.e. to point one actor at another actor that still is upwards?

Hi PompeyPaul,

I think you are looking for that function

FRotationMatrix.h

static CORE_API FMatrix MakeFromXZ(FVector const& XAxis, FVector const& ZAxis);

X it is you looking direction (lLookAt - lLocation), Z it is your up vector (lLookUp), it will be ortogonolized, Y axis will be calculated.

Also, if you are looking for another vectors, there are additional functions, such as MakeFromX, MakeFromXY, MakeFromYZ

Best regards,

Thanks . Having come from Unity land it is still confusing to think of Z as up. Awesome answer =D

Hi. I have same issue. When I learned view matrix in Direct X. I could make matrix for new axis using view matrix. For example, when I made space ship’s turret that attached each side. I made each view matrix for tracking enemy space ship.
And now I want make view matrix for tracking enemy in UE4. But when I made FLookAtMatrix it is not work well.
I saw you guys answer, but I’m not good at English and I can’t understand your answer easily.
You guys mean FLookAtMatrix function is not work well. Then if I make new axis matrix. I should make MakeFromXZ function for rotation?. And how can I make matrix for eye location transform. And what is relationship between matrix for new axis rotation and matrix for eye’s transform. Could you explain in detail?