Here's some code snippets to get you thinking.
First, a structure defining a Camera you can move about in 3D space.
I have one of these in my Player structure, but you can do without it.
Then, a function to update the camera and view matrix over time.
Also attatched you will find an update to my LinkedList include.
The update addresses destroying linked 2D object hierarchies.


;---------------------------------------------------------------------------------------------------
CAMERA STRUCT
vPosition D3DXVECTOR3 <> ;3D position of eye of the beholder (where I'm at)
vVelocity D3DXVECTOR3 <> ;3D motion velocity vector (where I'm going)
fYaw FLOAT ? ;current Yaw rotation (left-right)
fPitch FLOAT ? ;fcurrent Pitch rotation (up-down)
fYawVelocity FLOAT ? ;rotation coefficients
fPitchVelocity FLOAT ? ;for angular velocity (how fast I'm turning)
matOrientation D3DXMATRIX <> ;holds the local Orientation Matrix
matView D3DXMATRIX <> ;holds the local View Matrix
CAMERA ENDS
LPCAMERA typedef ptr CAMERA
;---------------------------------------------------------------------------------------------------

Now, here's some code that handles basic physics for the camera.
It doesn't do gravity.
It lets you rotate and move the camera smoothly with velocity and time.
It maintains an orientation matrix.
It create a viewmatrix.


UpdateCamera PROC fElapsed:FLOAT,pCAMERA:LPCAMERA
LOCAL fSpeed:FLOAT ; FLOAT fSpeed = 3 * fElapsedTime
LOCAL fAngularSpeed:FLOAT ; FLOAT fAngularSpeed = 1 * fElapsedTime
LOCAL qR:D3DXQUATERNION
.data
fp09 FLOAT 0.9f
fp0 FLOAT 0.0f
.code
push esi
fld fElapsed ; calculate speed and angular speed
fst fAngularSpeed
fmul fp3
fstp fSpeed

; // De-accelerate the camera movement (for smooth motion)
; vVelocity=vVelocity*0.9
mov esi,pCAMERA
fld [esi].CAMERA.vVelocity.x ;Slow down the XYZ velocity
fmul fp09
fstp [esi].CAMERA.vVelocity.x
fld [esi].CAMERA.vVelocity.y
fmul fp09
fstp [esi].CAMERA.vVelocity.y
fld [esi].CAMERA.vVelocity.z
fmul fp09
fstp [esi].CAMERA.vVelocity.z
fld [esi].CAMERA.fYawVelocity ;slow down the rotational velocity
fmul fp09
fstp [esi].CAMERA.fYawVelocity
fld [esi].CAMERA.fPitchVelocity
fmul fp09
fstp [esi].CAMERA.fPitchVelocity


.data
vT D3DXVECTOR3 <>
.code

; Calculate the Position delta vector (change of position)
; Note this does not take camera orientation into consideration, we do that in a moment...
; D3DXVECTOR3 vT = m_vVelocity * fSpeed;

fld [esi].CAMERA.vVelocity.x
fmul fSpeed
fstp vT.x
fld [esi].CAMERA.vVelocity.y
fmul fSpeed
fstp vT.y
fld [esi].CAMERA.vVelocity.z
fmul fSpeed
fstp vT.z

; Rotate the Position delta vector to suit the current camera orientation
invoke D3DXVec3TransformNormal, addr vT, addr vT, addr [esi].CAMERA.matOrientation

; Add the oriented Position delta vector to the CAMERA Position vector
; (move the camera in the appropriate direction)
; vPosition += vT;
fld vT.x
fadd [esi].CAMERA.vPos.x
fstp [esi].CAMERA.vPos.x
fld vT.y
fadd [esi].CAMERA.vPos.y
fstp [esi].CAMERA.vPos.y
fld vT.z
fadd [esi].CAMERA.vPos.z
fstp [esi].CAMERA.vPos.z

; Make sure that the camera can never go below Y=1 so it can't go underground
mov eax,fp1
.if [esi].CAMERA.vPos.y < eax
m2m [esi].CAMERA.vPos.y, fp1
.endif

; Update the yaw-pitch-rotation vector
fld fAngularSpeed ; fYaw += fAngularSpeed * fYawVelocity;
fmul [esi].CAMERA.fYawVelocity
fadd [esi].CAMERA.fYaw
fstp [esi].CAMERA.fYaw

fld fAngularSpeed ; fPitch += fAngularSpeed * fPitchVelocity;
fmul [esi].CAMERA.fPitchVelocity
fadd [esi].CAMERA.fPitch
fld fp0 ; if( fPitch < 0.0f ) fPitch = 0.0f;
fcomp
jge NoFix1
fstp fptemp
fld fp0
jmp NoFix2
NoFix1: if( fPitch > D3DX_PI/2 ) fPitch = D3DX_PI/2
fld FPPIHALF
fcomp
jge NoFix2
fstp fptemp
fld FPPIHALF
NoFix2:
fstp [esi].CAMERA.fPitch


invoke D3DXQuaternionRotationYawPitchRoll, addr qR, [esi].CAMERA.fYaw, [esi].CAMERA.fPitch, fp0
invoke D3DXMatrixAffineTransformation, addr [esi].CAMERA.matOrientation, fp1pt25, NULL, addr qR, addr [esi].CAMERA.vPosition
invoke D3DXMatrixInverse, addr [esi].CAMERA.matView, NULL, addr [esi].CAMERA.matOrientation
pop esi
ret
UpdateCamera ENDP
Posted on 2003-02-14 23:42:03 by Homer