# 3D RTS camera , how?

## Recommended Posts

yuppies    116
how to implement a 3D rts camera?.. what i mean is when i move my cursor to the edge of the screen the camera position also moves.. how to do it? right now what i have done is to move the camera to the right if the mouse cursor is x<=0 or x>= window width using the right vector of the camera(that is the cross prod of the forward and the up vector).. but i dont knw how to move the camera position when the mouse move vertically, is this correct or theres another way of doing this? what are the correct way of doing this..? pls enlighten me thanks

##### Share on other sites
Use standard "trigonometry" - math. :)

sin for Y (or Z) and cos for X...

use gluLookAy to position the camera... move target and camera position simultaniously...

p.s I would give you some sourcecode, but my computer is not in my possession for the moment... cheers!

##### Share on other sites
oh, and you need a yaw (direction) angle for the camera, preferably a pitch too..

##### Share on other sites
Opwiz    348
I'm not sure if this is the "correct" way or even a good way but I guess you can get the direction by taking cross product of the right camera vector and the world up vector (probably y-axis unit vector).

##### Share on other sites
evanofsky    2913
I hope you're using DirectX... otherwise I can't help you, so here goes.

You have the right idea with testing whether the mouse is at the edge of the screen, but once you've figured out which direction to move it in, it gets a little harder.

Let's say you want to move to the right. You make a vector in global space that points to the right, like this:
D3DXVECTOR3 toTheRight(1.0f, 0.0f, 0.0f);

Okay, so now all you have to do is add that vector to the camera's position, right? That's only if the camera isn't rotated at all. (this is assuming your game supports camera rotation) Let's say it's rotated 45 degrees to the left. This is real easy, it doesn't involve any math.

Just multiply the "toTheRight" vector by the camera's rotation matrix, like this:
D3DXVec3TransformCoord(&toTheRight, &toTheRight, &cameraRotationMatrix);

This makes the "toTheRight" vector point to the right relative to the camera.

Have fun!