#### Archived

This topic is now archived and is closed to further replies.

# Camera control

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

## Recommended Posts

How can i control my camera like in an ego-shooter.I want to write functions,to walk with the cursor keys and to rotate or move the cam with the mouse.But i don''t know the math for that Can somebody help me or does anybody know a good(and probably german ) turorial about that theme? greez,Neo

##### Share on other sites
Well, since you are using DirectX, you can use the D3DXLookAtLH and D3DXLookAt functions for building a matrix using the position of your camera, an up vector, and a point that you are looking at. Look up those functions in the documents.

Use the functions to build the matrix, then use this function to convert it to a matrix the SetTransform() method can use.

  void D3DXMatrixToD3DMatrix( D3DMATRIX* pHeraus, D3DXMATRIX* pIn){	(*pHeraus)._11 = (*pIn).m[0][0];	(*pHeraus)._12 = (*pIn).m[0][1];	(*pHeraus)._13 = (*pIn).m[0][2];	(*pHeraus)._14 = (*pIn).m[0][3];	(*pHeraus)._21 = (*pIn).m[1][0];	(*pHeraus)._22 = (*pIn).m[1][1];	(*pHeraus)._23 = (*pIn).m[1][2];	(*pHeraus)._24 = (*pIn).m[1][3];	(*pHeraus)._31 = (*pIn).m[2][0];	(*pHeraus)._32 = (*pIn).m[2][1];	(*pHeraus)._33 = (*pIn).m[2][2];	(*pHeraus)._34 = (*pIn).m[2][3];	(*pHeraus)._41 = (*pIn).m[3][0];	(*pHeraus)._42 = (*pIn).m[3][1];	(*pHeraus)._43 = (*pIn).m[3][2];	(*pHeraus)._44 = (*pIn).m[3][3];}[source]Ala Babble fish @ www.altavista.com:Gut da Sie DirectX verwenden, können Sie D3DXLookAtLH und D3DXLookAt Funktionen für das Errichten einer Matrix mit der Position Ihrer Kamera, des hohen Vektors und des Punktes benutzen, der Sie betrachten. Schauen Sie oben jene Funktionen in den Dokumenten. Verwenden Sie die Funktionen, um die Matrix zu errichten, verwenden Sie dann diese Funktion, um sie in eine Matrix umzuwandeln, welche die SetTransform() Methode benutzen kann.

##### Share on other sites
Using the lookat fuction is your best solution, by far.

As you want to do a first person view, I recommend something like this, it might sound a bit long and complicated, but it gives a guaranteed result:

- keep the camera position.

- keep 2 floats, one for up/down rotation and another one for left/right rotation, obviously you will add or subtract to these values when you detect a rotation command, and don''t forget to initialize the values or you might get an error...

- then when you need the camera matrix you pick up the rotation values and you make a rotation matrix. This matrix should be assembled with the multiplication of two matrix rotations, don''t use the D3DX function which takes x y and z values in the beginning. Idealy you''d rotate first around the x axis (up/down) and then you''d rotate around the y axis (left/right), here is where matrix multiplication enters, you make the 2 separate matrices and then multiply them noting that matrixA*matrixB is different from matrixB*matrixA, one will perform the above mentioned task the other will rotate around the y axis and then around the x axis (so the order is important when performing the multiply).

- with this matrix you can then transform 2 vectores, the up and forward vector (with the above example up would be (0,1,0) and forward would by (0,0,1)). These transformed vectores are exactly the vectores you want.

- then you just need to translate them into position, in this case you just need to simply add the postion vector to them.

- supply the lookat founction with the position, forward and up vector.

Ofcourse, I must point out that I mention all these steps but in the middle you''ll already practically have the view matrix, you just need to calculate another value and feed it into a matrix struct... But this method is intuitive, gives you space to add some extra ideas which comes along later, provides a forward (and up) vector which you can use later on and, principally (as some of these objectives can still be obtained with a shorter approach) these steps are easy to understand which is important to devise your approach and will greatly simplify debugging (I made an object that did all this treatment, and kept some extra values and etc, and I got good results from the start with no need to debug, while with other approaches I sometimes had to go into the code and find out what the hell was going on).

##### Share on other sites
I personally don''t like the lookat function, since all it does it use your position and the lookat point to create a facing vector anyway, and cross that with the up vector to get the right vector and then use those in the view matrix.

I''m currently writing a tutorial on making a camera system with matrices using d3dx, which talks both about full 6 degrees of freedom camera needed in a space shooter, and the classic firstperson shooter style camera as well.

it''ll be finished in a couple days.

Toby

Gobsmacked - by Toby Murray

1. 1
2. 2
3. 3
Rutin
19
4. 4
khawk
14
5. 5
frob
12

• 9
• 11
• 11
• 23
• 12
• ### Forum Statistics

• Total Topics
633659
• Total Posts
3013207
×

## Important Information

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!