Could someone explain how I can use "D3DXMatrixLookAtLH" to move the camera?

Started by
11 comments, last by Bart-Man 21 years, 8 months ago
Greetings, I was wondering if someone could explain how I can use "D3DXMatrixLookAtLH" to move the camera in d3d? I was trying to move the camera postion by just altering the "Camera postion" below. D3DXMATRIX matView; D3DXMatrixLookAtLH(&matView, &D3DXVECTOR3(0.0f, 0.0f,-30.0f) //Camera Position &D3DXVECTOR3(0.0f, 0.0f, 0.0f), //Look At Position &D3DXVECTOR3(0.0f, 1.0f, 0.0f)); //Up Direction I was just changing the camera position to different values, and I assumed that it would move the camera to that position. Is this correct, or is there a better way to do this? For example if I wanted to move the camera left across the X axis would I just change the "Camera Position" to (-1.0f,0.0f,-30.0f)? Thanks in advance for any advice.
Advertisement
Yes you can do it that way, don´t know if there´s a better way, but I sure think so since it´s not very smooth.



Bad Monkey Productions

[edited by - shakazed on August 16, 2002 9:24:07 AM]

[edited by - shakazed on August 16, 2002 9:24:31 AM]

[edited by - shakazed on August 16, 2002 9:25:35 AM]
This will not do it, you need to relate your camera position to
a variable. Your camera position needs to be executed per frame.

I would love going home to M7, but I can't detect black holes.
Yes you need to keep the current x position of the camera in a variable so you can add or subtract to it every frame when moving.



Bad Monkey Productions
Well, I guess since this is the way I move the camera, I''ll take a shot and tell you how to do it.

You are going to need 2 variables: a position variable(point) of the camera and a vector that shows the direction the camera is looking. Both of these variables are stored as D3DXVectors.

You would rotate the view vector to turn the camera and you would move the position vector to change the position. The view vector is relative to the origin.

Then you simply build the parameters for D3DXMatrixLookAtLH using these two bits of info:

position = your position vector
lookat point = position vector plus the view direction vector
up vector = (0,1,0)

D3DXMATRIX* D3DXMatrixLookAtLH(
D3DXMATRIX* pOut, = NULL
const D3DXVECTOR3* pEye, = Position
const D3DXVECTOR3* pAt, = lookat point
const D3DXVECTOR3* pUp = up vector
);

That''s basically it. There is of course more; to move right, you have to build a right vector to you current position and facing to translate the coordinates along that vector instead of just shifting the x coordinates.

Also, to lean you have to rotate the up vector.
Hello all, thanks for taking the time to answer my post!

"Anonymous Poster" approach worked quite nicely. The view and position tracking was a great idea. Thanks!!!
I haven''t tried the "right vector" for movement yet, but the other vectors worked great.
Could you elaborate more on this?

"to move right, you have to build a right vector to you current position and facing to translate the coordinates along that vector instead of just shifting the x coordinates."

Would I do something like this?
vRightPosition = vPosition + vViewDirection;
vRightPosition.x += 1;
Bart-man:

I think he simply means this: if you want to be able to move your camera left and right (e.g. to do "strafing" in first-person shooter games), you have to maintain a vector pointing to the camera''s right at all times.

Then, to compute strafing motion, just compute motion along this vector instead of along the look vector (which would compute forward-backward motion).

Incidentally, the computation for doing so would be:


  //D3DXVECTOR3 m_Pos; (position vector)//D3DXVECTOR3 m_Right; (right vector)//float delta; (motion intensity)m_Pos += delta * m_Right;  


--Hoozit.
----------------------Check out my game demo and resume at www.fivestory.com/projects/game.
Oh, yeah, another thing: you can find your "right" vector every round by taking the cross-product of the up vector (0,1,0) with your look vector.

--Hoozit.
----------------------Check out my game demo and resume at www.fivestory.com/projects/game.
HoozitWhatzit:

Cool, thanks for the info about the left-right strafing. That is good to know. Especially about the "cross-product of the up vector (0,1,0) with your look vector" getting the right vector.

Thanks!

This topic is closed to new replies.

Advertisement