Jump to content


third person view in opengl


4 replies to this topic

#1 nabla

    New Member

  • Members
  • Pip
  • 2 posts

Posted 02 January 2012 - 03:45 AM

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!

#2 Stainless

    Member

  • Members
  • PipPipPipPip
  • 582 posts
  • LocationSouthampton

Posted 02 January 2012 - 11:54 AM

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

#3 nabla

    New Member

  • Members
  • Pip
  • 2 posts

Posted 02 January 2012 - 03:17 PM

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

#4 TheNut

    Senior Member

  • Moderators
  • 1701 posts
  • LocationCyberspace

Posted 03 January 2012 - 03:36 AM

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.
http://www.nutty.ca - Being a nut has its advantages.

#5 MarekKnows.com

    Valued Member

  • Members
  • PipPipPip
  • 190 posts
  • LocationOntario, Canada

Posted 06 February 2012 - 10:57 AM

For some sample code showing 3rd person view working in OpenGL, have a look at this video tutorial: http://www.marek-kno...?vmk=gameEng44a
C++, 3D OpenGL and Game Programming video tutorials:
www.MarekKnows.com
Play my free games: Ghost Toast, Zing





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users