Correct use of GluLookAt?

Started by
14 comments, last by SuperVGA 12 years, 11 months ago
Ah guys it will not work... <_<

At first I got a code for nomalize:
float Normalize(float v1, float v2, float v3)
float m = sqrt(v1 * v1 + v2 * v2 + v3 * v3);
return (v1/m), (v2/m), (v3/m);
}

I think (hope) this is right. (Not the real code but same math calculations).

Then I got my movement pos with this:
case GLUT_KEY_LEFT:
Move_x -= 0.5f;
break;
case GLUT_KEY_RIGHT:
Move_x += 0.5f;
break;
case GLUT_KEY_DOWN:
Move_y -= 0.5f;
break;
case GLUT_KEY_UP:
Move_y += 0.5f;
break;

Not best but enough for testing.

Then I get the mousecoords with the glutPassiveMotionFunc and calculate the diffrence. (Mouse old pos - new pos).
But I dont know how to calculate now it with the camera.

This my current code:
gluLookAt(Move_x, Move_y, 1.8f, Move_x + 1.0f, Move_y + 1.0f, 1.8f, 1, 1, 1);
It not include the mousemovements because it looks really buggy...
Can someone show me the solution pls? I hope I understand then the calculation/prob :unsure:
Advertisement
You're writing in c++, right? Could you explain what the normalize function does? Your movement code causes movement along x and y axed only. Unaffected by view direction? I think you should try simplifying a bit..
1. Yes C++

2. I send 3 float variables to the function, with the m var I calculate the the length of the vector and then I return 3 new values (v1/m v2/m v3/m). ?! The function return me 3 values and they looks good... all under 1 :rolleyes:

3. I know that my move is only along x and y and is really unaffected by view direction but I dont know how I include the math calculation correct into the gluLookat! I try your "tutorial" from post #3 but it work not really... :huh:
1: Great. Keep doing that!

2: Ok, your normalize function sounds correct.

3: That "tutorial" assumes you orient the camera at a point in space, to look at. If you want to have a 4DOF-freeroam sort of behavior, you need angles for pitch and yaw.
You sin and cos these, and add them to the position of the camera. The returned position is then the point to look at.
If the point to look at remains stationary, you get that "spherical joystick"-sorta behavior. Great for orbiting and examining stuff.
Aeh... sorry but this doesn´t help me now :unsure:
I cant understand the theory with words.. I think I need a math calculation to understand this... I can set in values and calculate myself.

Mycurrent progress is to get the sin and cos values:
xm = sin(((Mouse_x_calc / 2)) / raddeg);
ym = cos(((Mouse_x_calc / 2)) / raddeg);

The raddeg is 57.2957! So I have the value not in rad but in deg (rad really sucks -_-).

if now for example arrow key up:
Move_x += ym;
Move_y += xm;

it works good! I have now an other angle the camera turn in the new direction! :) But the Movement from the camera is now with glrotatef and this is only possible with one direction (left and right or top and down).
So the solution are the normalize values! But I dunno how to use it now!

My Glulookat:
gluLookAt(Move_x, Move_y, Move_z, Move_x + 1, Move_y + 1, Move_z, 1, 1, 1);

Works good for walking but not rotating! :mellow:

Hope you can help me that this problem nearly done...

Sry for bad english <_<

Aeh... sorry but this doesn´t help me now :unsure:
I cant understand the theory with words.. I think I need a math calculation to understand this... I can set in values and calculate myself.

Mycurrent progress is to get the sin and cos values:
xm = sin(((Mouse_x_calc / 2)) / raddeg);
ym = cos(((Mouse_x_calc / 2)) / raddeg);

The raddeg is 57.2957! So I have the value not in rad but in deg (rad really sucks -_-).

if now for example arrow key up:
Move_x += ym;
Move_y += xm;

it works good! I have now an other angle the camera turn in the new direction! :) But the Movement from the camera is now with glrotatef and this is only possible with one direction (left and right or top and down).
So the solution are the normalize values! But I dunno how to use it now!

My Glulookat:
gluLookAt(Move_x, Move_y, Move_z, Move_x + 1, Move_y + 1, Move_z, 1, 1, 1);

Works good for walking but not rotating! :mellow:

Hope you can help me that this problem nearly done...

Sry for bad english <_<


Ok, let me get this straight: You can now move in a plane. You can only change the yaw angle, but not the pitch? So you have 2 degrees of freedom.
Alright, the most straightforward way (to understand), i can think of, would be to do add a pitch angle for starters. The pitch angle should be altered
the same way you change the yaw angle, ((Mouse_y_calc / 2)) / raddeg
Next, you'd want the cosine of the pitch value to multiply with your xm and ym. Note how a pitch of +pi/2 or -pi/2 causes xm and ym to be 0.0.
Alright, now make zm = sin(pitch), and add this to move_z.

That's just about it. But it probably won't be completely satisfying in the longer run...
Also, it's faster to multiply than divide. Remember that when you need to do something often, like dividing by two or converting from degs to rads.
(Then again, if you don't like converting, just stick to rads. -they ain't that bad, really) ;)

I guess your English is alright, I just hope i understood you.

This topic is closed to new replies.

Advertisement