# D3DXMatrixLookAtLH() can it be used to rotate an entity?

This topic is 3580 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

So I've got a problem in DirectX in which I have an Entity with it's own World Matrix and the like and this entity also has a target which is expressed as a D3DXVECTOR3. I need my entity to rotate to look at the target and whilst pondering this problem and lamenting my lack of listening in Maths I came across the D3DXMatrixLookAtLH function and thought that this could be the answer to my prayers. Even though it was a camera function I thought i could use this to rotate my Entity by just passing in its position as the 'pEye' value, this did not work and instead of just rotating it the function also transforms it by some bogus amount so my entity is no longer visible! Nightmare! So, to clarify my questions for you knowledgeable folks: 1) is the D3DXMatrixLookAtLH function unusable in this way? 2) What's the proper way to rotate to a point? Here's the code for my function:
void Entity::lookAt(D3DXVECTOR3 _lookHere){
updatePosition();
D3DXMATRIX _rotation;
D3DXMatrixIdentity(&_rotation); //Is this line necessary? I got paranoid and started clutching at straws.
D3DXVECTOR3 up(0.0f, 1.0f,0.0f);
D3DXVECTOR3 _position(getWorldPosition());
D3DXMatrixLookAtLH(&_rotation, &_position, &_lookHere, &up);
m_World=m_World*_rotation;
}


_lookHere is the target vector, getWorldPosition return the actual position of the entity (pulled from its world Matrix (m_World)). Thanks in advance and apologies if this is an unfathomably dumb question.

##### Share on other sites
It should work if you invert the matrix you get from that function.

##### Share on other sites
Thanks for the reply! Do you mean invert the _rotation matrix? I do that and my code looks like this:

void Entity::lookAt(D3DXVECTOR3 _lookHere){updatePosition();D3DXMATRIX _rotation,_irotation;D3DXMatrixIdentity(&_rotation);D3DXVECTOR3 up(0.0f, 1.0f,0.0f);D3DXVECTOR3 _position(getWorldPosition());D3DXMatrixLookAtLH(&_rotation, &_position, &_lookHere, &up);float determinant = D3DXMatrixDeterminant(&_rotation);D3DXMatrixInverse(&_irotation,&determinant,&_rotation);m_World=m_World*_irotation;updatePosition();}

Now my entity appears high up from where it should be and not facing the place I want it to face (not facing anywhere really). Why does the MatrixLookAt function create a matrix that transforms aswell as rotates? This makes no sense to me.

Also, if I don't inverse the rotation matrix I get my entity at 0,0,0 (not where it should be) and it flickers widly about pointing in all different directions.

##### Share on other sites
Ah! Turns out I was being stupid and the MatrixLookAt function actually outputs the whole Matrix, translations and all so there was no need to multiply the m_World matrix by the rotation as the ouput from MatrixLookAt essentially IS the world matrix!

##### Share on other sites
Not sure why you're multiplying by your old world matrix. This will give you a new matrix which can replace your old one.

You don't need all the determinant stuff. Doing an Inverse requires calculating it, and so D3DX takes a pointer to store it if you just happen to want it, saving a costly calculation. You're calculating it once, then Inverse is calculating it again, and overwriting the old value. Kill off you determinant call, and pass 0 to the Inverse function.

The lookat call is intended to be for cameras, so it must include translation to move things from their world space location into view space. After you do the inverse, the matrix will have a translation of your "eye" position, and a rotation that would be looking towards "at" from "eye", while attempting to maintain the correct "up" direction. This should just replace your old world matrix.

edit: And I'm too slow...

1. 1
Rutin
27
2. 2
3. 3
4. 4
5. 5

• 11
• 9
• 9
• 9
• 14
• ### Forum Statistics

• Total Topics
633312
• Total Posts
3011312
• ### Who's Online (See full list)

There are no registered users currently online

×