Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rotating an object around a fixed point in opengl

I have a problem with this openGL code:

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glPushMatrix(); // put current matrix on stack

//glTranslatef(0.0f, 0.0f, 0.0f);   
//glTranslatef(-4*1.5, 0.0, 4*1.5);

glRotatef(rotationAngle, 0.0f, 1.0f, 0.0f); // rotate the robot on its y-axis
glTranslatef(xpos, ypos, zpos);
DrawRobot(xpos, ypos, zpos); // draw the robot
glPopMatrix();

What should I do to make my robot turn around the point at which it is currently situated and not around the origin? I think the problem lies in this snippet.

like image 833
user2388112 Avatar asked May 16 '13 02:05

user2388112


1 Answers

Example of rotating an object around its centre along the z-axis:

glPushMatrix();

glTranslatef(250,250,0.0); // 3. Translate to the object's position.

glRotatef(angle,0.0,0.0,1.0); // 2. Rotate the object.

glTranslatef(-250,-250,0.0); // 1. Translate to the origin.

// Draw the object
glPopMatrix();
like image 75
samsad beagum Avatar answered Sep 24 '22 03:09

samsad beagum