Croow gave the way that DirectX doesn't like very much, but it's fun to see. If you play around alot, you can cause movement that's quite wild.
Here's the way using Matrices (which tends to be much nicer).
First you must understand what we're doing here. Here is how the DirectX view matrix is set up. (I think)
Right, Look and Up are axis definitions for orientation (x=Right, y=Up , and z=Look)
Right.x Look.x Up.x 0
Right.y Look.y Up.y 0
Right.z Look.z Up.z 0
Position.x Position.y Position.z 1
You can access these members of your view matrix by using matrix coordinates ( row , column )
In C++ access D3DXMATRIX data members by putting an underscore ( '_' ), the row number, then column number. (example: matrix1._14 is row 1 column 4, which would be zero in a view matrix) .
Now to rotate.
Create 3 variables (float), one for each axis. These will hold the current rotation angles.
Create 3 D3DXVECTOR3 variables. These will be your Right, Up, and Look vectors.
Make sure you initialize them with data such as Right= 1,0,0 , Up = 0,1,0 , Look = 0,0,1 (which are the default values).
Next, you need to make sure these vectors are all perpendicular to eachother. Do this with the following code.
D3DXVec3Normalize(&Look, &Look);
D3DXVec3Cross(&Right, &Up, &Look);
//this function is the Cross product, which creates a perpendicular vector to two vectors (this is also how you calculate polygon normals - except you use normaized vectors formed by edges)
D3DXVec3Normalize(&Right, &Right);
D3DXVec3Cross(&Up, &Look, &Right);
D3DXVec3Normalize(&Up, &Up);
Notice that this makes all the vectors perpendicular to the Look vector. You could make them all perpedicular to another one of the axis if you wish.
Now, create rotation matrices for each axis using D3DXMatrixRotationAxis. Example code:
D3DXMatrixRotationAxis(&ma
D3DXMatrixRotationAxis(&ma
D3DXMatrixRotationAxis(&ma
Now transform the vectors by the matrices.
D3DXVec3TransformCoord(&Lo
D3DXVec3TransformCoord(&Ri
D3DXVec3TransformCoord(&Lo
D3DXVec3TransformCoord(&Up
D3DXVec3TransformCoord(&Ri
D3DXVec3TransformCoord(&Up
In that code you are essentially just rotating one axis about another.
Now, using what I've told you earlier, YOU fill in your view matrix.
By the way, don't forget to create it with D3DXMatrixLookAtLH.
This ONLY covers orientation. I didn't mention much else that you need to know.
(like that if you have position involved here, that you need to make _41, _42, and _43 = to
D3DXVec3Dot(&Position, &(the vector in the above rows of this column (ex: row 1's = Right ) ) ) )
There is also a much simpler way to do this using quaternions, but quaternions, to me, are one of those things that I use, but don't understand.
RageDBL
Main Topics
Browse All Topics





by: CroowPosted on 2004-03-19 at 10:58:51ID: 10635724
... how can i preform camera rotation (like in a first person shooter) ...?
Use a vector to keep track of where you are and another to track where you're looking. Then update the view matrix each frame if the position or orientation have changed.
//////////////////////////
D3DXVECTOR3 vPosition; // position
D3DXVECTOR3 vOrientation; // a look-direction vector of length 1.0f, not a look-at vector
D3DXVECTOR3 vUp; // the up-direction. I use (0,1,0)
float fTurnRate; // turning rate
float fSpeed; // walking speed
float fTime; // elapsed time
...
// Locals
D3DXVECTOR3 vVelocity;
D3DXVECTOR3 vLookAt;
D3DXMATRIX matView;
vVelocity = vOrientation * fSpeed;
vPosition = vPosition + vVelocity * fTime;
// rotating about Y-axis
vOrientation.z = (vOrientation.z * cosf(fTurnRate * fTime))
-(vOrientation.x * sinf(fTurnRate * fTime));
vOrientation.x = (vOrientation.x * cosf(fTurnRate * fTime))
+(vOrientation.z * sinf(fTurnRate * fTime));
vLookAt = vPosition + vOrientation;
D3DXMatrixLookAtLH( &matView, &vPosition, &vLookAt, &vUp );
m_pd3dDevice->SetTransform
/*
walk forward and fSpeed = 10.0
walk backward and fSpeed = -10.0
turn left and fTurnRate = -1.0
turn right and fTurnRate = 1.0
*/
//////////////////////////
Now I know this can be optimized a bit, but generally it's the way to go. As to the second part of your question:
"... based on mouse movment?"
I'd recommend looking at the DirectInput example mouse.cpp that is included in the SDK.
Good luck