PDA

View Full Version : Chase Camera Code



technomage
29-07-2005, 01:54 PM
Hi Everyone

I have a little question (as always :wink: ).

I'm trying to implement a chase camera (a very basic one). I figured out the best way was to translate back a bit from the origin and render my model, that was the easy part.

The hard part is figuring out how to rotate the world in such a way that it looks right. I'm using ODE so I get the Position and Rotation of my "object" using

ShipPos := dBodyGetPosition(Ship.Body);
ShipRot := dBodyGetRotation(Ship.Body);


Where ShipPos is a vector 3 and ShipRot is a 4x4 matrix.

My problem is I want to positon my camera using gluLookAt, so I need to calcuate the View and Up vectors from the information I get from ODE.

At this point my brain went to a walk. :wink: . Anyone have any suggestion on where to start?

Cheers

Dean

Paulius
29-07-2005, 02:26 PM
View is the third row and up is second row, or if ODE matrices are mathematically correct, then that would be columns instead of rows.

technomage
29-07-2005, 03:38 PM
I'll give that a go

Thanks :D

technomage
29-07-2005, 04:55 PM
That worked a treat. :D

BTW the ODE matrix seems to be in columns not rows.

WILL
29-07-2005, 08:10 PM
That worked a treat. :D
Ah... another proud PGD member... HELPED! :D

JSoftware
29-07-2005, 09:27 PM
ODE using righthanded matrices?!? weird.. :?

technomage
30-07-2005, 07:51 AM
OK, after a bit more testing it kinda worked, but not fully (rolling works OK, but pitching up or down just looked weird).

So my comment about the matirx being in columns might not be right, I'll do a bit more testing a let you all know. :D

Paulius
30-07-2005, 08:26 AM
Since here camera is of the same orientation as the object this only works if camera is always directly behind the object (cameraPos = ObjectPos ?¢_" something * objectView)
Edit: maybe you?¢_Tre not giving gluLookAt what it wants: Center = CamraPos + ObjectView. Can't seem to come up with a proper way to compute Up with any camera position at the moment, I'll try to do this later

technomage
30-07-2005, 02:54 PM
Hi guys

I thought I'd post some more code.

There is what I'm doing.

Firstly I render the world after setting the camera to the position of the object and using the values from the ShipRot matrix to set the view and up vectors

Then I render the actual object from behind.

glPushMatrix;
ShipPos := dBodyGetPosition(Ship.Body); // get position of ODE body
ShipRot := dBodyGetRotation(Ship.Body); // get rotation of ODE body
// set the camera
UserCam.PositionCamera(ShipPos[0], ShipPos[1], ShipPos[2],
ShipRot[8],ShipRot[9],ShipRot[10],
ShipRot[4],ShipRot[5],ShipRot[6]);
// call gluLookAt
UserCam.Look;
// render world
glPopMatrix;
glPushMatrix;
// set teh camerato be behind the object
UserCam.PositionCamera(0,10,-100,0,0,0,0,1,0);
// call gluLookAt
UserCam.Look;
// render player object
glPopMatrix;


This works for rolling left and right, but not for pitching up or down.

Paulius
30-07-2005, 03:12 PM
if PositionCamera uses data directlly for gluLookAt, then like I said it sould be
UserCam.PositionCamera(ShipPos[0], ShipPos[1], ShipPos[2],
ShipPos[0] + ShipRot[8], ShipPos[1] + ShipRot[9], ShipPos[2] +ShipRot[10],
ShipRot[4],ShipRot[5],ShipRot[6]);


And for any camera position
CamView = ObjectPos - CamPos;
CamRight = CrossProduct((0,1,0), CamView);
CamTop = CrossProduct(CamRight, CamView);

technomage
30-07-2005, 07:30 PM
That worked :D

Thanks for the input everyone :D

While we're here anyone know a good simple intro into Matrix math. :?:

Paulius
30-07-2005, 07:58 PM
http://www.sjbaker.org/steve/omniv/matrices_can_be_your_friends.html a pretty good article to get you started

JSoftware
31-07-2005, 04:02 PM
http://www.euclideanspace.com/

this site is very good! i've got most of my math procedures from here

technomage
31-07-2005, 04:12 PM
After a bit more playing it turns out the ODE matries are not liin rows, but in columns.

elements 2,6 and 10 are for view, 1,5,9 are up vectors.

If you are wondering what I'm working on here is a sneek preview.

http://www.mirthmedia.co.uk/Images/odetest.PNG

:D

savage
31-07-2005, 07:31 PM
Very 8) . Look forward to seeing more.