A mess of a 3rd person camera attempt

Started by
3 comments, last by gluefactory 17 years, 1 month ago
Hi all, I'm new here so I'll just introduce myself, my name is rob i'm a games development student based in the uk. I'm currently working on a 3rd person camera (in 'c' and OpenGL) using some lecture notes for guidance. However It's proving rather fidly and if I'm honest im a little lost. I was wondering if anyone on here could shed some light on this for me. The set-by-step I have been given is as follows: 1) Get transform matrix to move avatar (car) (same as camera transform in 1st person, minus the inversion) 2) Save this matrix as car matrix 3) Use this to find a camera position n units directly behind the car 4) Load identity matrix (start again) 5) Use gluLookAt() to build camera view matrix --(Eye position found behind car) --(Target is position of car (xpos, ypos, zpos)) 4) Get and save camera view matrix 5) Add car transformations onto camera matrix and save as new car matrix 6) Use camera matrix to transform terrain and other static objects 7) Use car matrix to transform car Now i've had a go at this, but it is somewhat messy and a lil incomplete, ill attatch my code with notes of what section is which step:

void move_car(char direction[], int scalar)
{
	static float scale = 10.0;
	static float heading, pitch, roll;

	float m[16];
	float cmatrix[16];
	float cam[16];
	float xpos, ypos, zpos;
	float camx, camy, camz;
	float carx, cary, carz;
	float nextcamx, nextcamy, nextcamz;
	float xinc, yinc, zinc ;
	int i = 0;
	
	xpos = 1280.0f ;
	zpos = -1280.0f;
	ypos = 200.0f;

// Step 1: the directions etc. are imputs from keyboard or gamepad and are
// to move the car around

	glLoadIdentity() ;

	if (direction[2] == 'a') heading = heading + 3 * scalar;
	if (direction[3] == 'd') heading = heading - 3 * scalar;
	if (direction[5] == 'f') pitch = pitch - 1;
	if (direction[4] == 'r') pitch = pitch + 1;
	if (direction[10] == 'z') roll = roll - 1;
	if (direction[11] == 'x') roll = roll + 1;

	glPushMatrix();
	glLoadIdentity();
	glRotatef(heading, 0.0f, 1.0f, 0.0f);
	glRotatef(pitch, 1.0f, 0.0f, 0.0f);
	glRotatef(roll, 0.0f, 0.0f, 1.0f);
	glGetFloatv(GL_MODELVIEW_MATRIX, m);
	glPopMatrix();

	xinc = -m[8] * scale;
	yinc = -m[9] * scale;
	zinc = -m[10] * scale;
 
	if(direction[0] == 'w')
	{
		xpos = xpos + xinc;
		ypos = ypos + yinc;
		zpos = zpos + zinc;
	}
	if(direction[1] == 's')
	{
		xpos = xpos - xinc;
		ypos = ypos - yinc;
		zpos = zpos - zinc;	  
	}

	xinc = -m[0] * scale;
	yinc = -m[1] * scale;
	zinc = -m[2] * scale;

	if(direction[6] == 'q')
	{
		xpos = xpos - xinc;
		ypos = ypos - yinc;
		zpos = zpos - zinc;	  
	}
	if(direction[7] == 'e')
	{
		xpos = xpos + xinc;
		ypos = ypos + yinc;
		zpos = zpos + zinc; 
	}

	xinc = -m[4]  * scale;
	yinc = -m[5]  * scale;
	zinc = -m[6] * scale;

	if(direction[8] == 'p')
	{
		xpos = xpos - xinc;
		ypos = ypos - yinc;
		zpos = zpos - zinc;	  
	}
	if(direction[9] == 'o')
	{
		xpos = xpos + xinc;
		ypos = ypos + yinc;
		zpos = zpos + zinc; 
	}

	glLoadIdentity();
	glRotatef(roll, 0.0f, 0.0f, 1.0f);
	glRotatef(pitch, 1.0f, 0.0f, 0.0f);
	glRotatef(heading, 0.0f,1.0f, 0.0f);
	glTranslatef(xpos, ypos, zpos);

// Step 2: saving the matrix as car matrix - cmatrix

	glGetFloatv(GL_MODELVIEW_MATRIX, cmatrix);

// Step 3: This is supposed to calculate the cam position behind the car
// I've added in a little bit to program some kind of camera lag

	nextcamx = (-18 * cmatrix[8] + cmatrix[12]);
	nextcamy = (-18 * cmatrix[9] + cmatrix[13]);
	nextcamz = (-18 * cmatrix[10] + cmatrix[14]);
	
        //camera lag
	camx = camx + ((nextcamx - camx) * 0.1f);
	camy = camy + ((nextcamy - camy) * 0.1f);
	camz = camz + ((nextcamz - camz) * 0.1f);

// Step 4: Load the identity

	glLoadIdentity();

// Step 5: the gluLookAt command

	gluLookAt(camx, camy, camz, xpos, ypos, zpos, 0, 1, 0);

// Step 6: geting the camera matrix - cam

	glGetFloatv(GL_MODELVIEW_MATRIX, cam);

// Step 7: Adding the car transformations onto the camera matrix and saving
// as a new car matrix... I'm really not sure this is right at all

	for(i = 0;i<=15;i++)
	{
		cmatrix = cam + cmatrix;
	}

// Step 8: Using the camera matrix to transform terrain and static objects
// again I'm not sure if this is right

	glLoadIdentity();
	glRotatef(-roll, 0.0f, 0.0f, 1.0f);
	glRotatef(-pitch, 1.0f, 0.0f, 0.0f);
	glRotatef(-heading, 0.0f,1.0f, 0.0f);
	glTranslatef(-camx, -camy, -camz);

// Step 9: Use car matrix to transform car... now I'm really lost here and 
// havent attempted much. rendercar() is where I render the car using an md2
// model which is working fine.

	carx = cmatrix[8];
	cary = cmatrix[9];
	carz = cmatrix[10];

	//glTranslatef(carx, cary, carz);
	//rendercar(carx, cary, carz);	
}
As you can see from the comments, I'm a little lost, any help at all is greatly appreciated. Thank you
Advertisement
I think you're making it more complicated than you need.

Is something like this what you want?

Multiperson camera

Here's my code:
vpos = car pos
vtarget = car heading
vup = up vector (you can use (0,1,0))

void CCamera::update(const CVector3 &vpos, const CVector3 &vtar, const CVector3 &vup ){ 	//set target as player pos	target = vpos;	position = vpos;	up = vup;    	CVector3 f, r, u;	CVector3 temp(vtar);	//forward = target - pos	//r = f x u	f = temp - vpos;	u = vup;	r = vcross(f, u);       //normalize 'em	f.normalize();	u.normalize();	r.normalize();	//rotate on right axis to set cam just above the player	Matrix.rotate_on_axis (-15*PI/180.0f, r.x, r.y, r.z);	Matrix.transform_vector (f);	//adjust camera's pos just behind the player 6 units	position -= (f * 5);	target += (f * 5);}


What this does basically is to get the right angle from your forward vector, move the camera 5 units from the car, and rotate the camera on the right axis.

The effect is looking "just above and behind the car"


Oops.

Here's how you use it...

glPushMatrix();    glLoadIdentity();    //set the player view vectors and update the cam depending on    //what the viewer sees.    set_view(Object,Camera);        //Apply lookat transform    gluLookAt ( Camera.position.x,Camera.position.y, Camera.position.z,                Camera.target.x,Camera.target.y,Camera.target.z,                Camera.up.x,Camera.up.y,Camera.up.z );    //draw some noce grids    draw_terrain()    //draw and orient the object, note there's a glpushmatrix in the sub draw_player    draw_player (Object);        glPopMatrix();


The link I posted has the whole source and exe.
Hi.
relsoft,

Thanks for that, your right it is much simpler to impliment that way, however as this is lab work for my course I need to stick to the original method :(

thanks again,
Oh sorry 'bout that.

Did you try this?

// Step 9: Use car matrix to transform car... now I'm really lost here and // havent attempted much. rendercar() is where I render the car using an md2// model which is working fine.	carx = cmatrix[8];	cary = cmatrix[9];	carz = cmatrix[10];        glPushMatrix();   	   glTranslatef(carx, cary, carz);	   rendercar(carx, cary, carz);	        glPopMatrix();}

Hi.
Cheers :)

Doesn't work but is along the right lines, using the push and pop will stop it moving all the static objects again when I want it to just move the car. Think the answer lies in using the car matrix to transform the car, feeling rather 'thick' but I just can't get my head around how I'd do that :P

I'm sure once I get the answer it'll be a "duh!" moment for me :)

thanks,

This topic is closed to new replies.

Advertisement