PDA

View Full Version : Rotating Camera Using GlLookAt()



Winegums
04-06-2006, 10:40 AM
Hi, I did find a thread on this after searching but I wasn't able to see how to apply it to my problem.

I have a camera controlled by this lookat:


gluLookAt(
WWA[0], //Where we're at
WWA[1],
WWA[2],
WWA[0] + sy * cp, //Where we're looking at (based on where we're at)
WWA[1] - sp,
WWA[2] + cy * cp,
0, //Which direction is up
1,
0
); WWA is a 3 element float array (Where We're At). The program checks for mouse movement with the following code:


yaw += (CompareValues(MousePos.oldx, MousePos.x)/100);
pitch += (CompareValues(MousePos.oldy,MousePos.y)/100);This has worked in that I have a camera that rotates using the mouse. However I have a problem with translating the camera. I want to be able to move it in the direction it is facing, but am unable to figure out how to move it in the direction it is facing. I have tried outputting the numerical values for the position and where the camera is looking, but this has not helped at all (they are identical with the exception of a change in the z-axis).

songho
04-06-2006, 01:58 PM
Winegums,
The forward vector (direction it is facing to) can be computed if you know the camera positon and lookat(target). Let's say vector "a" is camera's origin and vector "b" is lookat point. Then the forward vector is b-a.

If you want to move forward, multiply speed(scale) factor and the normalized forward vector, then add up the result into both camera and lookat point.

For example, assume that the camera origin is (1,1,1) and the lookat point is (3,3,3). The forward vector would be (2,2,2), and the normalized forward vector is (.577,.577,.577). And if you want to move 2 units forward at a given frame, then the delta movement would be 2*(.577,.577,.577) = (1.154, 1.154, 1.154).

Therfore, the new camera origin after move will be (2.154, 2.154, 2.154) and the lookat point will be (4.154, 4.154, 4.154).
==song==

Winegums
04-06-2006, 02:59 PM
Hi, thanks for your message. I tried implimenting a translate camera function:


void TranslateCamera(float x, float y, float z, float lx, float ly, float lz)
{
float length; //Length of vector camera is translating along
float vector[3]; //The vector the camera is translating along

vector[0] = lx - x;
vector[1] = ly - y;
vector[2] = lz - z;

length = sqrt(vector[0] * vector[0] + vector[1] * vector[1] + vector[2] * vector [2]);

x /= length;
y /= length;
z /= length;

WWA[0] += x/100;
WWA[1] += y/100;
WWA[2] += z/100;
} I know the code isn't too tidy, but I think my problem is that the coordinates the camera is looking at aren't reasonable. The camera starts at the origin and regardless of where i turn it to face the coordinates of where the camera is facing allways end up as the same as the coordinates of the camera but the Z is one higher.

songho
04-06-2006, 03:42 PM
Winegums,
Please check again your rotation part, because Z-coord of lookat point is NOT always greater than camera origin.

For example, the camera is at (0,0,0) and is looking along +Z axis by default, then the lookat point has greater Z value and X and y are same as the camera point. But, if the camera rotate 180 degree, the z value of the lookat point is not greater anymore. It should be less than the camera point.

BTW, you are dividing by 100 at the bottom of your code. Isn't it too small increment(scaling)?

Winegums
04-07-2006, 08:27 AM
ended up finding another sollution that was somewhat less involved, I just calculated the actual numerical results of

WWA[0] + sy * cp

etc and stored them in other variables. then to translate just do

WWA[0] = WWLA[0]

etc. Thank you very much for your help though. :)