gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

OffTopic > D3D Camera

#18843 - StealthElement - Wed Apr 07, 2004 12:25 am

Has anyone tried to move their camera with the mouse? I got the tutorial from GameTutorials.com, but i didn't need a class as big as the ones they used. Actually, they didn't even use all of the functions in CVector. I created my own class and got the camera to rotate on the y-axis, but when i move the mouse up or down, everything is upside down. The cause isn't obvious to me at the moment; I'm still trying to understand the math...if you need some code listings, I'll show some

#18865 - Lupin - Wed Apr 07, 2004 12:23 pm

uhm, i don't think that you'll find a lot of 3D programmers on this board, but I might be able to help you because i once wrote a simple 1st person camera for D3D.

You will have to convert the code to C++, but that's how it's done:
Code:

Public Sub SetCam()
Dim vAxis As D3DVECTOR, vView As D3DVECTOR
Dim MatRot1 As D3DMATRIX, MatRot2 As D3DMATRIX

  If Not g_camera.bMovedSinceLastUpdate Then
    Exit Sub
  End If
  ' To find the axis we need to rotate around for up and down
  ' movements, we need to get a perpendicular vector from the
  ' camera's view vector and up vector.  This will be the axis.
  D3DXVec3Cross vAxis, g_camera.vViewDir, g_camera.vUp
  D3DXVec3Normalize vAxis, vAxis

  D3DXMatrixRotationAxis MatRot1, vAxis, g_camera.fRotAboutUp
  D3DXMatrixRotationAxis MatRot2, MakeVector(0, 1, 0), g_camera.fRotAboutRight
  D3DXMatrixMultiply MatRot1, MatRot1, MatRot2
  D3DXVec3TransformCoord vView, g_camera.vViewDir, MatRot1
  D3DXVec3Normalize vView, vView 'make sure it's normalized
 
  'Initialize a variable for the cross product result
  D3DXVec3Cross g_camera.vStrafe, vView, g_camera.vUp
  D3DXVec3Normalize g_camera.vStrafe, g_camera.vStrafe
 
  g_camera.vViewDir = vView
 
  D3DXVec3Add g_camera.vView, g_camera.vPosition, vView

  D3DXMatrixLookAtLH matView, g_camera.vPosition, g_camera.vView, g_camera.vUp
  D3Ddevice.SetTransform D3DTS_VIEW, matView

  g_camera.fRotAboutRight = 0
  g_camera.fRotAboutUp = 0
End Sub


This code is converted from the OGL camera tutorial of gt.com, they use to put their math routines into their own functions, if you want to use the D3DX functions you just have to know what's going on :)

The math behind it is quite complex (not too much though), I didn't fully understand it either :P
_________________
Team Pokeme
My blog and PM ASM tutorials

#18920 - Cearn - Thu Apr 08, 2004 9:33 am

When you say upside-down, do you mean that the whole image is upside-down, or just that when you expect that you tilt your view up, it goes down and vice verca? In the latter case, this could probably be fixed with inverting your pitch angle. Somewhere, you should have something like
Code:

if(mouse forward)
  pitch += angle_change
else if(mous backward)
  pitch -= angle_change

Switching the -= and += should do the trick.

When in 3D space you have know exactly what axis or angle points in which direction, otherwise you're bound to mess these things up. If I remember correctly, this is especially true for D3D, since it uses a left-handed coordinate system instead of the more usual righthanded one, so sign mix-ups are pretty much guaranteed.

#18979 - StealthElement - Fri Apr 09, 2004 3:19 am

Thanks a lot, really. It finally works now. By the way, the whole image was upside-down, but the tip is useful anyway for inverted preferences and stuff.