Using XMMatrixLookAtLH - trying to face a point, but doesn't work/crashes?

1.9k views Asked by At

I'm using this code for my Entity class:

void Entity::Face(FXMVECTOR target)
{
XMVECTOR up = XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f);


up = XMVector3Transform(up, Rotation);


Rotation = XMMatrixLookAtLH(Translation, target, up); //Rotation is a XMMATRIX, Translation is a XMVECTOR
}

And trying to get it to face another entity.Basically I'm doing this:

Entity a = //....
Entity b = //.......
a.Face(b.Translation);

And what happens is one of 3 things: 1.The function crashes, because of an assertion 2.Entity 'a' is misplaced in a weird position 3.Entity 'a' dissapears

This damn function XMMatrixLookAtLH has been the source of problems all over my project, everywhere I used it I had to spend hours to get it to work properly.

If I create the rotation matrix with XMMatrixRotationRollPitchYawFromVector, it works perfectly, however I NEED to get it to face directly at the point it's been given and I don't know how to get it to work.Please someone give me an advice.

1

There are 1 answers

0
Chuck Walbourn On BEST ANSWER

What is the assertion that is failing exactly? That's the clue to what the problem may be.

assert(!XMVector3Equal(EyeDirection, XMVectorZero()));
assert(!XMVector3IsInfinite(EyeDirection));
assert(!XMVector3Equal(UpDirection, XMVectorZero()));
assert(!XMVector3IsInfinite(UpDirection));

Since you are using XMVECTOR and XMMATRIX as class members, have you made sure that your class Entity is always allocated with 16-byte alignment? This is only guaranteed in x64 (64-bit) native programs by default. If you are writing a 32-bit app, you need to use XMLoad*/XMStore* functions and avoid using XMVECTOR and XMMATRIX directly in your class. Make sure you know the difference between when you get a C assertion breakpoint to fire and when you are actually getting an exception thrown like 'access violation' due to alignment.

You may find that using SimpleMath (in the DirectX Tool Kit) more natural to use than directly using DirectXMath, but it is generally designed around RH coordinates.