Jump to content
  • Advertisement
Sign in to follow this  
luke88

Moving/Aiming The Camera

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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 D3DXMATRIX
Dim matRot As D3DXMATRIX
Dim matView As D3DXMATRIX
D3DXMatrixTranslation matView,x,y,z
'xang, yang, and zang are in radians
D3DXMatrixRotationYawPitchRoll matRot,yang,xang,zang
D3DXMatrixMultiply matView,matRot,matPos
D3DXMatrixInverse matView,0,matView
D3DDevice.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.

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites
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 D3DXMATRIX
Dim matRot as D3DXMATRIX
Dim matView as D3DXMATRIX

If DIState.Key(DIK_W) <> 0 Then
zmove = 0.5
Else If DIState.Key(DIK_S) <> 0 Then
zmove = -0.5
Else
zmove = 0
End If

If DIState.Key(DIK_D) <> 0 Then
xmove = 0.5
Else If DIState.Key(DIK_A) <> 0 Then
xmove = -0.5
Else
xmove = 0
End If

'whatever your rotation code is goes here

D3DXMatrixRotationYawPitchRoll matRot,yang,xang,zang
Dim vec As D3DXVECTOR4
vec.x = xmove
vec.y = 0
vec.z = zmove
vec.w = 1
D3DXVec4Transform vec,vec,matRot

x = x + vec.x
z = z + vec.z

'this is where I messed up before - I put the position in matView instead of matPos
D3DXMatrixTranslation matPos,x,y,z

D3DXMatrixMultiply matView,matRot,matPos
D3DXMatrixInverse matView,0,matView
D3DDevice.SetTransform D3DTS_VIEW,matView


Hope that works out better.

Share this post


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

Share this post


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

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!