PDA

View Full Version : Camera view math...



M109uk
19-01-2005, 04:16 PM
Hi again all,

I have a question on how to set the position of the camera behind the players actor, at the moment i am simply using the following code:

var
p,v,u: Array [0..2] Of Single;
begin
....
p := Cameras.PlayerCam.Position; {Position of the Camera}
v := Players[0].Position; {Position of the player/model}
u := Vertex3f(0, 4, 0); {Up vector}
glLoadIdentity;
gluLookat(p, v, u);
....
end;


The model uses a position and rotation vertex to set its position and rotation and neither of the Y axis are used!

Of course at the moment the camera doesnt rotate around the player depending on the players rotation, i have tried adding the players rotation to the v vertex and/or to the p vertex but this only effects the X axis and not the Y axis.
I want the camera to stay behind the players model.
For an idea of what i mean i have drawn a diagram:

Figs 1 and 2 are what i want but fig 3 is what i get :(
[Note: the diagram is crap, hope its understandble]

http://www.biocoders.org/camDiagrams.png

Here is a link to a current build of my app so show whats happening:
http://www.biocoders.org/XShell.zip - (4505kb)
Controls:
Up,Down,Left and Right to move the players model around,
Numpad + and -,
Use ESC to skip through each intro scene and to exit app.

The app requires some registry settings which i have included in the zip, theses settings are stored in the software/biocoders section.

A key will also need to be updated:
HKEY_CURRENT_USER\Software\BioCoders\XShell\Direct ory
Set this value you to the directory where XShell.exe has been extracted.

Also the app takes a while to load the actor model and skins after the 3rd intro screen..

Be warned its only a test app and hasnt got anything special!

Thanx for any help

{MSX}
19-01-2005, 06:42 PM
I have a question on how to set the position of the camera behind the players actor, at the moment i am simply using the following code:


Umm in that way you have the camera right above the player, direct in down direction.. not that nice :P



Of course at the moment the camera doesnt rotate around the player depending on the players rotation, i have tried adding the players rotation to the v vertex and/or to the p vertex but this only effects the X axis and not the Y axis.
I want the camera to stay behind the players model.
For an idea of what i mean i have drawn a diagram:


This is a typical problem.. The solution with matrix is, for example:
- select a vector X that define the distance from the player to the camera (example 10 unit behind and 4 above)
- translate the vector to the orientation of the player (this is what you miss)
- sum the translated X to the player position.
- now you can use lookAt with the calculated position as eye and the player position as target.

Alternatively, you can calculate the camera orientation as well, instead of using the lookat.

Also, depending on your project, you could want the camera to be a little "late" about the player.. so that you can see it turning a bit (like in Tomb Raider for example). This is a little more triky.

I've choosed to use Quaternions for my game instead of matrices for all orientations (camera, player, etc). It's more difficoult at first but then it's cleaner in the whole and you have less problems. You could consider using them.



Figs 1 and 2 are what i want but fig 3 is what i get :(
[Note: the diagram is crap, hope its understandble]


I found it nice, instead :P



The app requires some registry settings which i have included in the zip, theses settings are stored in the software/biocoders section.
A key will also need to be updated:
HKEY_CURRENT_USER\Software\BioCoders\XShell\Direct ory
Set this value you to the directory where XShell.exe has been extracted.


Sincerely, what the hell is the registry stuff useful for? Do you hope people will take all that pain to run a game?
Anyway, i'm on linux so i didn't tryed out your game.

Now i think i'm going to write my article "1000 and 1 reason why the windows registry system is completely unnecessary and stupid", also called "tell me one thing that you can do with registry that you can't do with a regular filesystem" :mrgreen:

M109uk
19-01-2005, 06:57 PM
Thanx {MSX}, il give it a go :)
Iv been messing around with the code, i sort of managed to get it to go round the actor, except for of an oval shape :?

Thanx, my artistc skills are VERY limited :lol:



Sincerely, what the hell is the registry stuff useful for? Do you hope people will take all that pain to run a game?
Anyway, i'm on linux so i didn't tryed out your game.

Now i think i'm going to write my article "1000 and 1 reason why the windows registry system is completely unnecessary and stupid", also called "tell me one thing that you can do with registry that you can't do with a regular filesystem"


Im using the registry as a temporary store for the settings, i will be using an encrypted config file at some point when i have finished the routines.

My app is more of a Shell replacement for windows, simply put, the user can explore their system in 3D with customizable themes, with support for 3D websites (similar to VRML, but with more modern game feel) will also include plugin games and apps to run with in the environment. kinda a brief description if it makes any sense :roll:

At some point i hope to add support for linux but i have no idea where to start to get system information and file information with linux :? also with internet support :?

If you have any idea where or how i can do this it will be great!?

Thanx

{MSX}
19-01-2005, 07:14 PM
Thanx {MSX}, il give it a go :)
Iv been messing around with the code, i sort of managed to get it to go round the actor, except for of an oval shape :?


Umm i suggest you to get the camera (and the basic 3d system as a whole) working perfecly as you expect, becouse it will have a huge impact on the rest of the program. If something doesn't work as expected, and even if something works but you don't know how, you'll better fix it.

What tecnique did you used to get the actual "oval" system ?



Im using the registry as a temporary store for the settings, i will be using an encrypted config file at some point when i have finished the routines.

My app is more of a Shell replacement for windows, simply put, the user can explore their system in 3D with customizable themes, with support for 3D websites (similar to VRML, but with more modern game feel) will also include plugin games and apps to run with in the environment. kinda a brief description if it makes any sense :roll:


Ah, a nice project.



At some point i hope to add support for linux but i have no idea where to start to get system information and file information with linux :? also with internet support :?
If you have any idea where or how i can do this it will be great!?


First, if you plan to use DirectX, change your plans :P Use SDL and OpenGL instead (see JEDI-SDL).
About the file information, the best thing is to use delphi functions, like findFirst and findNext function, that are possibly cross-platform.

Anyway the project you're working is more system-dependant than a game.. you'll have hard work on porting it on linux :P

M109uk
19-01-2005, 07:40 PM
Umm i suggest you to get the camera (and the basic 3d system as a whole) working perfecly as you expect, becouse it will have a huge impact on the rest of the program. If something doesn't work as expected, and even if something works but you don't know how, you'll better fix it.

What tecnique did you used to get the actual "oval" system ?


Yeah its something i really want to get sorted and im not continuing with the rest until i know it is working perfectly!

I also want to add different styles of camera, behind the actor, FPS style, Isometric style, and top view, maybe others if and when i work out how to do them :roll:

I managed to get the oval system via:

var
p,v,u,tmpP: TVertex3f;
begin
p := Player.Position;
tmpP := p;
p[1] := 4;
v := RotatePoint(Player.Position, Player.Rotation);
u := Vertex3f(0,4,0);
gluLookat(p, v, u);
end;


Of course i know this is wrong..
From what im guessing id say that p should be the position of the camera, v of the target (Actor) and u is the tilt?



This is a typical problem.. The solution with matrix is, for example:
- select a vector X that define the distance from the player to the camera (example 10 unit behind and 4 above)
- translate the vector to the orientation of the player (this is what you miss)
- sum the translated X to the player position.
- now you can use lookAt with the calculated position as eye and the player position as target.

Im not too sure on what you mean, i came up with the following and of course it doesnt work lol (im no good at theory :roll:)

var
p,Distance: Array [0..2] of Single;
begin
Distance := Vertex3f(0, 4, 6);
p := VertexAdd(Player.Position, Distance);
glLookat(p, Player.Position, Vertex3f(0,4,0));
end;




Ah, a nice project.

First, if you plan to use DirectX, change your plans :P Use SDL and OpenGL instead (see JEDI-SDL).
About the file information, the best thing is to use delphi functions, like findFirst and findNext function, that are possibly cross-platform.

Anyway the project you're working is more system-dependant than a game.. you'll have hard work on porting it on linux :P


Thanx, iv been working on it for ages and starting to get it going nicly, just need to put it all together :roll:

I have decided to use OpenGL mostly because i see it simpler to understand than DX, and i have majority of the engine designed for OpenGL. Amoungst that i thought it would be easier to use OpenGL for if i decide to make anything cross-platform in the future.

{MSX}
20-01-2005, 08:48 AM
I managed to get the oval system via:

var
p,v,u,tmpP: TVertex3f;
begin
p := Player.Position;
tmpP := p;
p[1] := 4;
v := RotatePoint(Player.Position, Player.Rotation);
u := Vertex3f(0,4,0);
gluLookat(p, v, u);
end;


Of course i know this is wrong..
From what im guessing id say that p should be the position of the camera, v of the target (Actor) and u is the tilt?


Umm, yes, the parameter to gluLookAt are in fact camera position, target position and up vector (that defines the tilt).
Now, the problem is: how do you define the rotation of the player ? With an angle, matrix, quaternion ?

The code above is not correct: the RotatePoint(Player.Position, Player.Rotation) doesn't give you anything useful.
You just get the position of the player rotated of that rotation around the origin, which is not what you want.
What you want is: take a distance vector (as below), rotate it around the player (not the origin) and then add it to
the player position
Something like this:


begin
Distance := Vertex3f(0, 4, 6);
v := RotatePoint(Distance, Player.Rotation);
v := VectorAdd(Distance, Player.position);
u := Vertex3f(0,1,0);
gluLookat(p, v, u);
end;

Depending on your setup, you could have to rotate by -Player.rotation or subtract instead of add.



Im not too sure on what you mean, i came up with the following and of course it doesnt work lol (im no good at theory :roll:)

var
p,Distance: Array [0..2] of Single;
begin
Distance := Vertex3f(0, 4, 6);
p := VertexAdd(Player.Position, Distance);
glLookat(p, Player.Position, Vertex3f(0,4,0));
end;



No, this way you just add the distance and the position, without first orienting the direction (see above).

I know that this things are hard to visualize.. :P

M109uk
20-01-2005, 11:38 AM
Umm, yes, the parameter to gluLookAt are in fact camera position, target position and up vector (that defines the tilt).
Now, the problem is: how do you define the rotation of the player ? With an angle, matrix, quaternion ?

Originaly i was moving the camera around and then applying the position to the player and add 6 and to the positions z value.
Now i have changed it so that the players position is moved:

procedure TPlayer.moveforward(Value: Single);
var
Dir,Rot: TVertex3f;
begin
Rot := Rotation;
Rot[0] := 0;
Dir := RotatePoint(Vertex3f(0, 0, -1), Rot);
VertexAdd(Position, Vertex3f(Dir[0]*Value, Dir[1]*Value, Dir[2]*Value));
end;

procedure TPlayer.movebackward(Value: Single);
var
Dir,Rot: TVertex3f;
begin
Rot := Rotation;
Rot[0] := 0;
Dir := RotatePoint(Vertex3f(0, 0, -1), Rot);
VertexAdd(Position, Vertex3f(-Dir[0]*Value, -Dir[1]*Value, -Dir[2]*Value));
end;

procedure TPlayer.turnleft(Value: Single);
begin
Rotation := AddVertex(Rotation, Vertex3f(0, -Value, 0));
Rotation := NormalizeAngle(Rotation);
end;

procedure TPlayer.turnleft(Value: Single);
begin
Rotation := AddVertex(Rotation, Vertex3f(0, Value, 0));
Rotation := NormalizeAngle(Rotation);
end;


I added the following code in to the render function:

Distance := Vertex3f(0, 4, 6);
v := RotatePoint(Distance, Player.Rotation);
p := VectorAdd(Distance, Player.Position);
u := Vertex3f(0,1,0);
glLoadIdentity;
gluLookat(p, v, u);

//Render player
glTranslatef(Player.Position[0], Player.Position[1], Player.Position[2]);
glRotatef(Player.Rotation[0], 1, 0, 0);
glRotatef(Player.Rotation[1], 0, 1, 0);
glRotatef(Player.Rotation[2], 0, 0, 1);
Player.Render;

The above code gives to very bezier problems :roll: iv played around with the v value to try and get it the way i want. The closest i have got is the camera does rotate around the player how ever the problem now is the model rotates around one direction on the Y axis but the camera rotates around the opposite direction on the y axis. should i just inverse the cameras rotation?

I had a look at some documents and sources i found an delphi3d.net on Quaternions but it all just flew over my head :lol:

Thanx for your help so far, and sorry for the stupid questions :roll: