0
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);

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

0
146 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);

0
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.

0
176 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.

0
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