Moving/Aiming The Camera

Started by
7 comments, last by GameDev.net 18 years, 8 months ago
Hi. I'm trying to make an FPS engine using DirectX 8 in VB6, and I'm having problems trying to move the camera appropriately. I guess this is due to my lack of understanding of matrices. I think I have managed to get the camera to move back and forth, and strafe correctly. Here's what I have: If DIState.Key(DIK_W) <> 0 Then z = z - 0.5 If DIState.Key(DIK_S) <> 0 Then z = z + 0.5 If DIState.Key(DIK_D) <> 0 Then x = x - 0.5 If DIState.Key(DIK_A) <> 0 Then x = x + 0.5 D3DXMatrixTranslation matView, x, y, z D3DDevice.SetTransform D3DTS_VIEW, matView Is this right? My main problem is trying to get the camera to look around. I have tried many different things, with none of them appearing to work. I'm finding it impossible. How am I supposed to do it? Any code would be more than helpful. :) (I have kinda started a topic on this before, but it was more to do with matrices than my actual problem. I thought by starting a more specific one, I could get more responses). Thanks, Luke
luke88
Advertisement
Quote:If DIState.Key(DIK_W) <> 0 Then z = z - 0.5
If DIState.Key(DIK_S) <> 0 Then z = z + 0.5
If DIState.Key(DIK_D) <> 0 Then x = x - 0.5
If DIState.Key(DIK_A) <> 0 Then x = x + 0.5


I see you've already counteracted what seemed like "mirrored" movement.

I'm assuming you already know about world matrices, and that you create a world matrix and set it to the device before you render an object. Well, the view matrix can be calculated just like a world matrix - with one exception: you invert the matrix after you calculate it.

So, some sample code might look like (I'm doing the best I can, I've never used VB):

Dim matPos As D3DXMATRIXDim matRot As D3DXMATRIXDim matView As D3DXMATRIXD3DXMatrixTranslation matView,x,y,z'xang, yang, and zang are in radiansD3DXMatrixRotationYawPitchRoll matRot,yang,xang,zangD3DXMatrixMultiply matView,matRot,matPosD3DXMatrixInverse matView,0,matViewD3DDevice.SetTransform D3DTS_VIEW,matView


You'll also want to change the signs in your movement code - you won't have to mirror the movement anymore.
_______________________________________________________________________Hoo-rah.
When using that code, instead of the camera staying in one spot, looking around, the camera kind of rotates around the whole world. What am I doing wrong?
(I'm tearing my hair out over this. I've been stuck on it for far too long. Who would have thought that aiming a camera could be so damn difficult. D3DRM was soo much simpler.)

[Edited by - luke88 on August 11, 2005 1:31:23 PM]
luke88
Aha. By switching matView and matRot in your code, it now works! But with this, a new problem arises. Instead of the camera moving in the direction its facing, it would just run along its axis. For example, if I turn left by 90 degrees, and press W (to walk forward), the camera would strafe right. If I turn left by another 90 degrees, and press W, the camera would move backwards.
luke88
Ook, there's a typo in my last post that caused that problem. I'll change it all here.

Ooh boy, moving objects in the direction they're facing :) That's fun.

Dim matPos as D3DXMATRIXDim matRot as D3DXMATRIXDim matView as D3DXMATRIXIf DIState.Key(DIK_W) <> 0 Then 	zmove = 0.5Else If DIState.Key(DIK_S) <> 0 Then	zmove = -0.5Else	zmove = 0End IfIf DIState.Key(DIK_D) <> 0 Then	xmove = 0.5Else If DIState.Key(DIK_A) <> 0 Then	xmove = -0.5Else	xmove = 0End If'whatever your rotation code is goes hereD3DXMatrixRotationYawPitchRoll matRot,yang,xang,zangDim vec As D3DXVECTOR4vec.x = xmovevec.y = 0vec.z = zmovevec.w = 1D3DXVec4Transform vec,vec,matRotx = x + vec.xz = z + vec.z'this is where I messed up before - I put the position in matView instead of matPosD3DXMatrixTranslation matPos,x,y,zD3DXMatrixMultiply matView,matRot,matPosD3DXMatrixInverse matView,0,matViewD3DDevice.SetTransform D3DTS_VIEW,matView


Hope that works out better.
_______________________________________________________________________Hoo-rah.
YAY! It works perfectly. Thank you soo much! I can now finally sleep at night.
I really got learn how to use matrices. Meh.
Again, thank you :)
luke88
Sure! Linear algebra (vectors and matrices and stuff) is one of the hardest parts of learning how to do 3D graphics IMO, but after a while, it just becomes sort of a "oh, duh" kind of thing.
_______________________________________________________________________Hoo-rah.
Just to complement:

You can get more information about camera settings at:
http://www.toymaker.info/Games/html/camera.html

Also, there is a very flexible camera function at:
http://www.adrianojmr.ubbi.com.br
(take a look at D3DSmartCamera function in TRICICLO sample)

Although they have a C++ code, it's easy to convert it to VB.
Just to complement:

You can get more information about camera settings at:
http://www.toymaker.info/Games/html/camera.html

Also, there is a very flexible camera function at:
http://www.adrianojmr.ubbi.com.br
(take a look at D3DSmartCamera function in TRICICLO sample)

Although they have a C++ code, it's easy to convert them to VB.

This topic is closed to new replies.

Advertisement