PDA

View Full Version : Yet some gluLookAt trouble



mathx
09-01-2001, 05:12 AM
Hello,

Okay, I'm getting really agry about this thing. Here´s the code I'm trying to make work onto a way of walking like a first person shooter. But when I'm aligned to the x-axis of the world, some strange things happen. Take a look and see if you find any errors (delphi code, shouldn't be difficult for you guys to understand though)

(the setup procedure)
procedure TForm1.FormCreate(Sender: TObject);
begin
InitGl;

upve[1]:= 1;

posi[0]:= 0;
posi[1]:= 0;
posi[2]:= -5;

vect[0]:= 0;
vect[1]:= 0;
vect[2]:= 1;

cent[0]:= posi[0] + (vect[0] * 1);
cent[1]:= posi[1] + (vect[1] * 1);
cent[2]:= posi[2] + (vect[2] * 1);
end;

(the movement procedure)
procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
var
u: single;
begin
//walk front
if key = VK_UP then begin
u:= 0.1;

posi[0]:= posi[0] + (vect[0] * u);
posi[1]:= posi[1] + (vect[1] * u);
posi[2]:= posi[2] + (vect[2] * u);

cent[0]:= posi[0] + (vect[0] * 1);
cent[1]:= posi[1] + (vect[1] * 1);
cent[2]:= posi[2] + (vect[2] * 1);
end;

//walk back
if key = VK_DOWN then begin
u:= 0.1;

posi[0]:= posi[0] - (vect[0] * u);
posi[1]:= posi[1] - (vect[1] * u);
posi[2]:= posi[2] - (vect[2] * u);

cent[0]:= posi[0] + (vect[0] * 1);
cent[1]:= posi[1] + (vect[1] * 1);
cent[2]:= posi[2] + (vect[2] * 1);
end;

//look left
if key = VK_F1 then begin
u:= 5;

RotateY(u, vect);
cent[0]:= posi[0] + (vect[0] * 1);
cent[1]:= posi[1] + (vect[1] * 1);
cent[2]:= posi[2] + (vect[2] * 1);
end;

//look rigth
if key = VK_F2 then begin
u:= -5;

RotateY(u, vect);
cent[0]:= posi[0] + (vect[0] * 1);
cent[1]:= posi[1] + (vect[1] * 1);
cent[2]:= posi[2] + (vect[2] * 1);
end;

//look up
if key = VK_F3 then begin
u:= -5;

RotateX(u, vect);
cent[0]:= posi[0] + (vect[0] * 1);
cent[1]:= posi[1] + (vect[1] * 1);
cent[2]:= posi[2] + (vect[2] * 1);
end;

//Look Down
if key = VK_F4 then begin
u:= 5;

RotateX(u, vect);
cent[0]:= posi[0] + (vect[0] * 1);
cent[1]:= posi[1] + (vect[1] * 1);
cent[2]:= posi[2] + (vect[2] * 1);
end;

normalize(vect);
end;

(the draw procedure)
procedure TForm1.WMPaint(var message: TWMPaint);
begin
glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);

glLoadIdentity;

gluLookat(posi[0], posi[1], posi[2], //position
cent[0], cent[1], cent[2], //center
upve[0], upve[1], upve[2]); //up

//draw some objects
end;

(the normalize, rotateY and RotateX functions)
procedure Normalize(var dest : TVector);
var
leng : Single;
scaleFactor : Single;
begin
leng := Len(dest);
if (leng = 0.0) then
Exit;

scaleFactor := 1.0/leng;

dest[0] := dest[0] * scaleFactor;
dest[1] := dest[1] * scaleFactor;
dest[2] := dest[2] * scaleFactor;
end;

procedure RotateX(ang : Single; var dest : TVector);
var
y0, z0 : Single;
radAng : Single;
begin
y0 := dest[1];
z0 := dest[2];
radAng := DegToRad(ang);

dest[1] := (y0 * cos(radAng)) - (z0 * sin(radAng));
dest[2] := (y0 * sin(radAng)) + (z0 * cos(radAng));
end;

procedure RotateY(ang : Single; var dest : TVector);
var
x0, z0 : Single;
radAng : Single;
begin
x0 := dest[0];
z0 := dest[2];
radAng := DegToRad(ang);

dest[0] := (x0 * cos(radAng)) + (z0 * sin(radAng));
dest[2] := (z0 * cos(radAng)) - (x0 * sin(radAng));
end;

Okay, let me explain the variables: posi is an array that sets the position of the camera, center is the center point of the scene, up is the up-vector and vect is the vector to where I'm pointing.

Sorry for the long post, but I'm busting my brains out of this.

Thanks,
Matheus.