PDA

View Full Version : Camera positioning....



M109uk
09-01-2006, 02:17 AM
Hi all, been a while since i posted here lol 351 unread topics :s

Anyway... i'v started a new project, and im trying to work out how to position my camera behind my player character..

the code i use to position my players character is as below:

procedure MoveForwards(const Run: Boolean = True);
var
Xtra: Single;
begin
If Run Then Xtra := 2 Else Xtra := 0;
Player.Position[0] := (Player.Position[0]-Sin(Player.Heading*Pi/180)*Xngine.GlobalTime/600)-Xtra;
Player.Position[2] := (Player.Position[2]+Cos(Player.Heading*Pi/180)*Xngine.GlobalTime/600)+Xtra;
Player.HeadMovAngle := Player.HeadMovAngle+5;
Player.HeadMovement := 0.025*Sin(Player.HeadMovAngle*Pi/180);
end;

procedure MoveBackwards(const Run: Boolean = True);
var
Xtra: Single;
begin
If Run Then Xtra := 2 Else Xtra := 0;
Player.Position[0] := (Player.Position[0]+Sin(Player.Heading*Pi/180)*Xngine.GlobalTime/600)+Xtra;
Player.Position[2] := (Player.Position[2]-Cos(Player.Heading*Pi/180)*Xngine.GlobalTime/600)-Xtra;
Player.HeadMovAngle := Player.HeadMovAngle-5;
Player.HeadMovement := 0.025*Sin(Player.HeadMovAngle*Pi/180);
end;

procedure FlyHigher;
begin
Player.Position[1] := Player.Position[1]+(Xngine.GlobalTime/600);
end;

procedure FlyLower;
begin
Player.Position[1] := Player.Position[1]-(Xngine.GlobalTime/600);
end;

procedure TurnLeft;
begin
Player.Heading := Player.Heading+5;
If Player.Heading > 360 Then Player.Heading := 0;
end;

procedure TurnRight;
begin
Player.Heading := Player.Heading-5;
If Player.Heading < 0 Then Player.Heading := 360;
end;

procedure TiltUp;
begin
Player.Tilt := Player.Tilt+5;
If Player.Tilt > 60 Then Player.Tilt := 60;
end;

procedure TiltDown;
begin
Player.Tilt := Player.Tilt-5;
If Player.Tilt < -60 Then Player.Tilt := -60;
end;

procedure StepLeft;
begin
Player.Position[0] := Player.Position[0]+Sin((Player.Heading+90)*Pi/180)*Xngine.GlobalTime/900;
Player.Position[2] := Player.Position[2]-Cos((Player.Heading+90)*Pi/180)*Xngine.GlobalTime/900;
end;

procedure StepRight;
begin
Player.Position[0] := Player.Position[0]-Sin((Player.Heading+90)*Pi/180)*Xngine.GlobalTime/900;
Player.Position[2] := Player.Position[2]+Cos((Player.Heading+90)*Pi/180)*Xngine.GlobalTime/900;
end;


I thought that i could use gluLookAt, but i cant seem to grasp how to use it correctly :s

The rotation and positions get randomly messed up when i move my character..

Thanx for any help :)

User137
09-01-2006, 03:10 AM
Well.. assume you have
0: X
1: Y
2: Z
Then those coordinates are a bit wrong. Also the run calculation is bugging. Here's how i'd fix first function:


procedure MoveForwards&#40;const Run&#58; Boolean = True&#41;;
var Xtra&#58; Single;
begin
If Run Then Xtra &#58;= 2 Else Xtra &#58;= 1;
Player.Position&#91;0&#93; &#58;= &#40;Player.Position&#91;0&#93;+Cos&#40;Player.Heading*Pi/180&#41;*Xngine.GlobalTime*Xtra/600&#41;;
Player.Position&#91;2&#93; &#58;= &#40;Player.Position&#91;2&#93;+Sin&#40;Player.Heading*Pi/180&#41;*Xngine.GlobalTime*Xtra/600&#41;;
Player.HeadMovAngle &#58;= Player.HeadMovAngle+5;
Player.HeadMovement &#58;= 0.025*Sin&#40;Player.HeadMovAngle*Pi/180&#41;;
end;

M109uk
09-01-2006, 04:24 AM
I got the translation and rotation coordinates ok for rendering the world, and the general movements, its just when i render the character, it moves in the opposite direction than the camera and twice as fast :(

its like it stays in the same position as the camera moves around it (but i know it doesnt stay in the same position)..

Now i am rendering the view like:

glPushMatrix;
glRotatef(Player.Tilt, 1, 0, 0);
glRotatef(Player.Heading, 0, 1, 0);
glTranslatef(Player.Position[0], Player.Position[1]-50, Player.Position[2]);

glPushMatrix;
glTranslatef(0, -500, 0);
glScalef(0.02, 0.02, 0.02);
Models['Houses'].Render(False, False);
glPopMatrix;

glPushMatrix;
glRotatef(Player.Heading/180, 0, 1, 0);
glTranslatef(0, 50, -20);
Player.Model.Draw(Xngine.GlobalTime);
glPopMatrix;
glPopMatrix;


Just a question... if i used glMultf within the glPushMatrix/glPopMatrix would it mess up the view matrix?

Im asking because the actor model renders the vertices by using glMulti!?

User137:
Thanx for the reply, i tried your suggestion.. how ever it seems to change the axis between X and Z :s.. e.g. i move forwards but instead the cameras moves left.. Maybe i mis-print?

M109uk
09-01-2006, 05:09 AM
P.S.

Its probably a really stupid question but how exactly do you use gluLookAt?
Does it effect the translation and rotation of the world?

i tried the cod below, im probably using it wrong.. but the view just doesnt show any part of my world :(


LookAt(Vertex3f(0, 0, 0), Vertex3f(-Player.Position[0], -Player.Position[1], -Player.Position[2]), Vertex3f(0, 0, 0));
glPushMatrix;
glRotatef(Player.Tilt, 1, 0, 0);
glRotatef(Player.Heading, 0, 1, 0);
glTranslatef(Player.Position[0], Player.Position[1]-50, Player.Position[2]);

glPushMatrix;
glTranslatef(0, -400, 0);
glScalef(0.05, 0.05, 0.05);
Models['Houses'].Render(False, False);
// glScalef(1, 1, 1);
// glTranslatef(0, 400, 0);
glPopMatrix;

// glPushMatrix;
glTranslatef(0, 20, 0);
Player.Model.Draw(Xngine.GlobalTime);
// glPopMatrix;
glPopMatrix;

JSoftware
09-01-2006, 11:01 AM
one of the vectors need to be a up vector, a normalized vector pointing up relating to the camera (most likely 0,1,0)

M109uk
09-01-2006, 08:57 PM
Ok, thanx for the replys :)

I think i have my camera almost sorted, the only problem is thats its to stiff... i mean there is no natural movement at all...

How would i go about doing this?

I was thinking maybe some thing like putting a delay on the movement of the camera and the players model??

Thanx for any help :)

User137
10-01-2006, 10:56 AM
For the player model, you should freeze camera in 1 spot above the scene and see if player moves the way you want.

And for the camera, there is many ways but i'd start by totally separating it from player. There is ways to make camera stick to the player like a "sphere on a spring". You can calculate the supposed camera position behind the player and then move camera towards it.

This calculation as vectors:
TC = Target Camera , C = Camera
Variable S = Speed (closer to 0 slower, 1 has no delay)

C = C+(TC-C)*S
And same formula for camera angle :)

Also note that you can change TC on fly! It very smoothly transports camera to anywhere in the world.

M109uk
13-01-2006, 04:20 PM
Cool thanx, i will give that a ry when i get back :)

I have been working on my camera class, and it is now seperate from the player, so works a little better now :)
Just have loads of problems with the frustum :(

Nitrogen
16-01-2006, 08:02 AM
Here's how I did it:

The camera has 4 variables.
A position (P) and a point in the level where it's looking (Tar)
Then for smooth interpolation, there is a Target Position (TP)
And a Target Tar variable (TTar).

These are where the camera *wants* to look..


Procedure TLevel.TickCamera;
begin
//Do the interpolation
//T is just a timing variable.
with Camera do
begin
P[0] := P[0]-(P[0]-TP[0])*0.005*T;
P[1] := P[1]-(P[1]-TP[1])*0.005*T;
P[2] := P[2]-(P[2]-TP[2])*0.005*T;

Tar[0] := Tar[0]-(Tar[0]-Ttar[0])*0.005*T;
Tar[1] := Tar[1]-(Tar[1]-Ttar[1])*0.005*T;
Tar[2] := Tar[2]-(Tar[2]-Ttar[2])*0.005*T;
end;
end;

Procedure TLevel.LookFromCamera;
begin
glLoadIdentity;
glscalef(0.1, 0.1, 0.1);
with Camera do
gluLookAt(P[0], P[1], P[2], Tar[0], Tar[1], Tar[2], 0, 1, 0);
Camera.Frustum.CalculateFrustum;
end;


The format of the gluLookAt function is 3 coordinates for where the camera should be placed (same as glTranslatef())
Then 3 coordinates of the object you want to look at,
Then your Up vector. Which is usually in the Y direction, but depends on your engine.