Archived

This topic is now archived and is closed to further replies.

Bart-Man

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

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
This will not do it, you need to relate your camera position to
a variable. Your camera position needs to be executed per frame.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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;

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
Or you could swap X/Y and flip Z, should be faster, right?


--
MFC is sorta like the swedish police... It''''s full of crap, and nothing can communicate with anything else.

Share this post


Link to post
Share on other sites
If all else fails, take a look at the code from my (Moe''s) 4 Elements 3 contest entry. It is quite well commented and it should be fairly easy to figure out how it works.

Moe''s site

Share this post


Link to post
Share on other sites
Based on the camera class from www.gametutorials.com, I have a very decent working first person shooter style camera that uses D3DXMatrixLookAtLH. I highly recommend using the camera from they''re OpenGL tutorials to create your own that''s specific to Direct3D.

---
My Site-My Tetris Clone w/Source
Come join us on IRC in #directxdev @ irc.afternet.org

Share this post


Link to post
Share on other sites