Rotate 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 so that my robot spins around the point at which it is now, and not around the source? I think the problem is this fragment.

+6
source share
4 answers

Just make a turn after the transfer. Order matters.

 glTranslatef(xpos, ypos, zpos); glRotatef(rotationAngle, 0.0f, 1.0f, 0.0f); 
+10
source

An example of the rotation of an object around its center along the z axis:

 glPushMatrix(); glTranslatef(250,250,0.0); // 3. Translate to the object 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(); 
+10
source

try turning after translation:

  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); glTranslatef(xpos, ypos, zpos); glRotatef(rotationAngle, 0.0f, 1.0f, 0.0f); // rotate the robot on its y-axis DrawRobot(xpos, ypos, zpos); // draw the robot glPopMatrix(); 
+3
source

Use this

 house(); glTranslatef(x, y, 0.0); // 3. Translate back to original glRotatef(theta, 0.0, 0.0, 1.0); // 2. Rotate the object around angle glTranslatef(-m, -n, 0.0); // 1. Move to origin house(); 

where m and n are the point on the object that you want to rotate around , and x and y are the <strong> points that you want to rotate around.

0
source

Source: https://habr.com/ru/post/945116/


All Articles