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!











