在 OpenGL 中随着对象移动改变旋转点?
Changing the rotation point along with object moving in OpenGL?
我正在尝试构建一个可以沿 X 轴移动并绕 Z 轴旋转的锤子武器。现在我的锤子有问题。锤子可以在一个固定的支点上绕 Z 轴旋转,但是当我将锤子移动到一个新位置然后旋转锤子时,锤子仍然围绕旧的支点旋转。
我尝试将我移动的距离添加到旧轴心点,但它不起作用。我该如何解决这个问题?感谢您的帮助!
这是我的代码:
glPushMatrix();
//the rotation angle of Z-axis
glTranslatef(0.5f,1.0f,-1.0f); //Back to original point
glRotatef(zr, 0.0f, 0.0f, 1.0f); //Rotating
glTranslatef(-0.5f,-1.0f,1.0f); //The rotation piovt point
//build weapon base
//the moving distant on X-axis
glPushMatrix();
glColor3f(1, 0, 0);
glTranslatef(0.5f+xr, 1.0f, -1.0f);
glRotatef(-90.0, 1.0, 0.0, 0.0);
quadratic = gluNewQuadric();
gluCylinder(quadratic, 0.2f, 0.2f, 2.0f, 50, 50);
glPopMatrix();
//build hammer
glPushMatrix();
glTranslatef(0.0+xr, 3.0f, -1.0f);
glRotatef(90.0, 0.0, 1.0, 0.0);
glColor3f(0, 1, 0);
quadratic = gluNewQuadric();
gluCylinder(quadratic, 0.2f, 0.2f, 1.0f, 50, 50);
glPopMatrix();
glPopMatrix();
你也必须移动枢轴:
float pivot_x = 0.5f + xr;
glTranslatef(pivot_x, 1.0f, -1.0f); //Back to original point
glRotatef(zr, 0.0f, 0.0f, 1.0f); //Rotating
glTranslatef(-pivot_x, -1.0f, 1.0f); //The rotation piovt point
我正在尝试构建一个可以沿 X 轴移动并绕 Z 轴旋转的锤子武器。现在我的锤子有问题。锤子可以在一个固定的支点上绕 Z 轴旋转,但是当我将锤子移动到一个新位置然后旋转锤子时,锤子仍然围绕旧的支点旋转。
我尝试将我移动的距离添加到旧轴心点,但它不起作用。我该如何解决这个问题?感谢您的帮助!
这是我的代码:
glPushMatrix();
//the rotation angle of Z-axis
glTranslatef(0.5f,1.0f,-1.0f); //Back to original point
glRotatef(zr, 0.0f, 0.0f, 1.0f); //Rotating
glTranslatef(-0.5f,-1.0f,1.0f); //The rotation piovt point
//build weapon base
//the moving distant on X-axis
glPushMatrix();
glColor3f(1, 0, 0);
glTranslatef(0.5f+xr, 1.0f, -1.0f);
glRotatef(-90.0, 1.0, 0.0, 0.0);
quadratic = gluNewQuadric();
gluCylinder(quadratic, 0.2f, 0.2f, 2.0f, 50, 50);
glPopMatrix();
//build hammer
glPushMatrix();
glTranslatef(0.0+xr, 3.0f, -1.0f);
glRotatef(90.0, 0.0, 1.0, 0.0);
glColor3f(0, 1, 0);
quadratic = gluNewQuadric();
gluCylinder(quadratic, 0.2f, 0.2f, 1.0f, 50, 50);
glPopMatrix();
glPopMatrix();
你也必须移动枢轴:
float pivot_x = 0.5f + xr;
glTranslatef(pivot_x, 1.0f, -1.0f); //Back to original point
glRotatef(zr, 0.0f, 0.0f, 1.0f); //Rotating
glTranslatef(-pivot_x, -1.0f, 1.0f); //The rotation piovt point