third person view in opengl

Dca655285fd2b7b378345c0769bf36b8
0
nabla 101 Jan 02, 2012 at 03:45

Hi all,

I am trying to programm a thrid person view in opengl for weeks. Unfortunately it still doesnt work as I expected. My idea was to use the normal rotate and translate functions and calculate the direction vector and
the new position from the camera. With my code i get the following behavior the object is rotating around its center and the camera is rotating around the object but with a different speed. And if you move the object along the direction vector it disappears (this was working in a earlier version I changed something and… ).

float lx=1,lz=0;
// Set coordinates for the camera and for the direction vector of the object
void set_richtung()
{
koord.x=0;koord.y=0;koord.z=0;
set_lookat_eye.x=koord.x-8;
set_lookat_eye.y=10;
set_lookat_eye.x=koord.z;
}
//the new eye coordinates for the lookat function
void calc_ang()
{
set_lookat_eye.x=koord.x+lx;
set_lookat_eye.z=koord.z+lz;
}
//set lookat function
void update_cam()
{
gluLookAt(set_lookat_eye.x,set_lookat_eye.y,set_lookat_eye.z, koord.x,0 ,koord.z,0.0f,1.0f,0.0f);
}
//rotate object and cam
void side(float ang)
{
punkt a,b;
lx=sin(ang);
lz=cos(ang);
calc_ang();
update_cam();
}
//move the object along the direction vector
void forw(int a)
{
set_lookat_eye.x=koord.x + a*lx*MOVE;
set_lookat_eye.z=koord.z + a*lz*MOVE;
koord.x = koord.x + a*lx*MOVE;
koord.z = koord.z + a*lz*MOVE;
update_cam();

}

and the display function

void display(){
kfunk();
glEnable(GL_TEXTURE_2D) ;
glClear(GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT);
glClearColor(1,1,1,1);
glLoadIdentity();

update_cam();

glPushMatrix();

glTranslatef(koord.x,0.0f,koord.z);
glRotatef(-ang,0.0f,1.0f,0.0f);
draw_sk(); //draw the object
glPopMatrix() ;
//draw the rest
drawKoordinatensystem();
Draw_Grid();
glutSwapBuffers();
glutPostRedisplay();
glDisable(GL_TEXTURE_2D);
glFlush ();
}

Thanks for looking at my code!

4 Replies

Please log in or register to post a reply.

B5262118b588a5a420230bfbef4a2cdf
0
Stainless 151 Jan 02, 2012 at 11:54

Try changing this

glTranslatef(koord.x,0.0f,koord.z);
glRotatef(-ang,0.0f,1.0f,0.0f);

to this

glRotatef(-ang,0.0f,1.0f,0.0f);

glTranslatef(koord.x,0.0f,koord.z);

Dca655285fd2b7b378345c0769bf36b8
0
nabla 101 Jan 02, 2012 at 15:17

Thanks for your suggestion but it doesnt work. I think its the math in here unfortunately i dont get the problem.

6837d514b487de395be51432d9cdd078
0
TheNut 179 Jan 03, 2012 at 03:36

Your camera is looking down the Y-axis at the object, but you are setting the Y-axis as the up-vector in your gluLookAt function. Set your up-vector along the X or Z axis instead.

500367065665a05a847242a39a0bc69e
0
mmakrzem 101 Feb 06, 2012 at 10:57

For some sample code showing 3rd person view working in OpenGL, have a look at this video tutorial: http://www.marek-knows.com/downloads.php5?vmk=gameEng44a