camera view

Started by
5 comments, last by steg 22 years, 3 months ago
Hi, I''ve got a world in which objects are translated to world space and then to view/camera space, all the objects thus far are static and hence the camera is used to ''walk'' around this world. If I want to have moving objects I only have to update the objects x,y,z values before applying it to its world transformation matrix, I guess this is correct so far ? The camera code I use was one out of Zen of Direct3D, the code works well but isn''t explained clearly - can anyone please help me with this code I''m using ?! I understand the vector regeneration part, the velocity part I''m not sure of - I take it the code is making sure the LookAt, Up and Right vectors are always perpendicular to each other (Cross product) ? Here is the code : void ViewTrans() { D3DXMATRIX matPitch, matYaw, matRoll; vPos.x += vVelocity.x * vRight.x; vPos.y += vVelocity.x * vRight.y; vPos.z += vVelocity.x * vRight.z; vPos.x += vVelocity.y * vUp.x; vPos.y += vVelocity.y * vUp.y; vPos.z += vVelocity.y * vUp.z; vPos.x += vVelocity.z * vLook.x; vPos.y += vVelocity.z * vLook.y; vPos.z += vVelocity.z * vLook.z; // Base vector regeneration D3DXVec3Normalize(&vLook, &vLook); D3DXVec3Cross(&vRight,&vUp,&vLook); // make vRight D3DXVec3Normalize(&vRight, &vRight); D3DXVec3Cross(&vUp,&vLook,&vRight); // make vUp D3DXVec3Normalize(&vUp, &vUp); D3DXMatrixRotationAxis(&matPitch,&vRight,fPitch); D3DXMatrixRotationAxis(&matYaw,&vUp,fYaw); D3DXMatrixRotationAxis(&matRoll,&vLook,fRoll); D3DXVec3TransformCoord(&vLook,&vLook,&matYaw); D3DXVec3TransformCoord(&vRight,&vRight,&matYaw); D3DXVec3TransformCoord(&vLook,&vLook,&matPitch); D3DXVec3TransformCoord(&vUp,&vUp,&matPitch); D3DXVec3TransformCoord(&vRight,&vRight,&matRoll); D3DXVec3TransformCoord(&vUp,&vUp,&matRoll); D3DXMATRIX view; D3DXMatrixIdentity(&view); view._11 = vRight.x; view._12 = vUp.x; view._13 = vLook.x; view._21 = vRight.y; view._22 = vUp.y; view._23 = vLook.y; view._31 = vRight.z; view._32 = vUp.z; view._33 = vLook.z; view._41 = -D3DXVec3Dot(&vPos, &vRight); view._42 = -D3DXVec3Dot(&vPos, &vUp); view._43 = -D3DXVec3Dot(&vPos, &vLook); g_pDevice->SetTransform( D3DTS_VIEW, &view ); } This is called every frame, to move the camera left is easy, just vPos.x += leftStep; right,up,down,in,out are obvious too. Please can someone explain this to me though ? As anyone got any camera tutorials ? Kind regards, sTeVe

If it isn't working, take a bath, have a think and try again...

Advertisement
This is a small tutorial on FPS camera manipulation in OpenGL
but the theory is the same...

There USED to be a kick-ass d3d7 tutorial back when MrGamemaker.com was still around..

I thought I had them archived somewhere, but I can''t find them at the moment..argh!

But yeah you''re right. By taking the cross products and normalizing the vectors, you''re ensuring that your lookat, right and up vectors are all perpendicular to each other, giving a "proper" FPS look...

hth,



Learn about game programming!Games Programming in C++: Start to Finish
I take it the below code is confusing you as it did me. The below may seem at first over the top for just moving the camera any which way/direction, and it is. The reason for this code becomes clear when you have to attach your camera to follow and object. You don''t need half as much code to just move the camera moing. But did Mr.Walsh tell us that, NO THE GIT.

vPos.x += vVelocity.x * vRight.x;
vPos.y += vVelocity.x * vRight.y;
vPos.z += vVelocity.x * vRight.z;

vPos.x += vVelocity.y * vUp.x;
vPos.y += vVelocity.y * vUp.y;
vPos.z += vVelocity.y * vUp.z;

vPos.x += vVelocity.z * vLook.x;
vPos.y += vVelocity.z * vLook.y;
vPos.z += vVelocity.z * vLook.z;
steg:

1.
An objects "world matrix" would be better named "object to world matrix". It transforms the points and vectors from the space the object or model was defined in into world space. If the world matrix contains a rotation, then the object will rotate (change orientation). To change the position of the object you can apply a "translation" to the world matrix.
You could alternatively update the x,y,z coordinates of every vertex in the object, but usually a translation in the matrix is more suitable.

2.
when your copy of Real-Time Rendering arrives, look up "orthonormal basis" and "orthogonal matrix".

Some of that code is superfluous for the purposes of setting a view matrix.

The cross products and normalisation are as you deduce to make unit length vectors perpendicular to each other, each one represents each major axis in the "camera space" coordinate system (an orthonormal basis).

The velocity stuff has nothing to do with the view matrix calculation - it looks like its for the movement of the camera "look from" position (would have been cleaner if the author had split it into a SetViewMatrix and MoveCameraFromPosition functions).

The D3DXMatrixRotationAxis and D3DXVec3TransformCoord also has nothing to do with a usual view matrix setup. From a quick glance, that appears to add pitch, yaw and roll Euler rotation angles to the rotation from the orthonormal basis - I don''t really see the point of having two ways of specifying the rotation. I''ll assume it has something to do with other code in the book.

The view._11, view._21, view._31 etc sets the rotation part of the matrix.

The view._41, view._42, view.43 thing is a translation to the position of the camera. This has the effect of "pulling" the world towards the camera so that it sticks to the lens. The lens of the camera becomes the new 0,0,0 position.

Since the code is using D3DX, its strange that the ready made D3DXMatrixLookAtLH() function wasn''t used instead.


--
Simon O''''Connor
Creative Asylum Ltd
www.creative-asylum.com

Simon O'Connor | Technical Director (Newcastle) Lockwood Publishing | LinkedIn | Personal site

Here is a Great tutorial on rotating the Camera within your 3d world.

Go here to download this group of tutorials:

http://64.33.37.114/Mr-Gamemaker%20Tutorials.zip

Unzip and double click on "tutorials.html"

Then click the "Rotating the Camera" link for DX7 (it's almost setup the same way for DX8)

That should do it

Edited by - JohnG on December 30, 2001 8:29:42 PM
JohnG: Thanks man, I''ve been looking for those for a while now!



"Hey dude, that nurse has no arms!"

"We''re an equal opportunity employer here son."

Learn about game programming!Games Programming in C++: Start to Finish
Thanks all,
As usual some great replies !-) I guess I will disect
that code into seperate methods - the velocity defo was for
moving the camera, I think Mr Walsh uses it in a space type scenario where the ship is always moving.

Simon - How do you use the D3DXMatrixLookAtLH() to create the
same effect ?

Kind regards to all,
sTeVe

If it isn't working, take a bath, have a think and try again...

This topic is closed to new replies.

Advertisement