1

当我试图围绕一个支点(腿的顶部)进行腿部运动时,反而会发生腿部开始围绕错误的点旋转,而我只想让腿来回摆动。

我该如何解决这个问题?

这是我的代码。

glPushMatrix();

    glTranslatef(x+0.0f, y+6.0f, z-1.5f);
    glRotatef(walk, 1.0f,0.0f,0.0f);
    glBegin(GL_QUADS);
        glTexCoord2f(0.5f,0.0f); glVertex3f(x-1.0f, y-6.0f, z-3.0f);
        glTexCoord2f(1.0f,0.0f); glVertex3f(x-1.0f, y-6.0f, z-0.5f);
        glTexCoord2f(1.0f,0.5f); glVertex3f(x-1.0f, y-18.0f, z-0.5f);
        glTexCoord2f(0.5f,0.5f); glVertex3f(x-1.0f, y-18.0f, z-3.0f);

        glTexCoord2f(0.5f,0.0f); glVertex3f(x+1.0f, y-6.0f, z-3.0f);
        glTexCoord2f(1.0f,0.0f); glVertex3f(x+1.0f, y-6.0f, z-0.5f);
        glTexCoord2f(1.0f,0.5f); glVertex3f(x+1.0f, y-18.0f, z-0.5f);
        glTexCoord2f(0.5f,0.5f); glVertex3f(x+1.0f, y-18.0f, z-3.0f);

        glTexCoord2f(0.5f,0.0f); glVertex3f(x-1.0f, y-6.0f, z-3.0f);
        glTexCoord2f(1.0f,0.0f); glVertex3f(x+1.0f, y-6.0f, z-3.0f);
        glTexCoord2f(1.0f,0.5f); glVertex3f(x+1.0f, y-18.0f, z-3.0f);
        glTexCoord2f(0.5f,0.5f); glVertex3f(x-1.0f, y-18.0f, z-3.0f);

        glTexCoord2f(0.5f,0.0f); glVertex3f(x-1.0f, y-6.0f, z-0.5f);
        glTexCoord2f(1.0f,0.0f); glVertex3f(x+1.0f, y-6.0f, z-0.5f);
        glTexCoord2f(1.0f,0.5f); glVertex3f(x+1.0f, y-18.0f, z-0.5f);
        glTexCoord2f(0.5f,0.5f); glVertex3f(x-1.0f, y-18.0f, z-0.5f);
glEnd();
glPopMatrix();
glPushMatrix();
    glTranslatef(x+0.0f, y+6.0f, z+1.5f);
    glRotatef(-walk, 0.0f,0.0f,1.0f);
    glBegin(GL_QUADS);
        glTexCoord2f(0.5f,0.0f); glVertex3f(x-1.0f, y-6.0f, z+3.0f);
        glTexCoord2f(1.0f,0.0f); glVertex3f(x-1.0f, y-6.0f, z+0.5f);
        glTexCoord2f(1.0f,0.5f); glVertex3f(x-1.0f, y-18.0f, z+0.5f);
        glTexCoord2f(0.5f,0.5f); glVertex3f(x-1.0f, y-18.0f, z+3.0f);

        glTexCoord2f(0.5f,0.0f); glVertex3f(x+1.0f, y-6.0f, z+3.0f);
        glTexCoord2f(1.0f,0.0f); glVertex3f(x+1.0f, y-6.0f, z+0.5f);
        glTexCoord2f(1.0f,0.5f); glVertex3f(x+1.0f, y-18.0f, z+0.5f);
        glTexCoord2f(0.5f,0.5f); glVertex3f(x+1.0f, y-18.0f, z+3.0f);

        glTexCoord2f(0.5f,0.0f); glVertex3f(x-1.0f, y-6.0f, z+3.0f);
        glTexCoord2f(1.0f,0.0f); glVertex3f(x+1.0f, y-6.0f, z+3.0f);
        glTexCoord2f(1.0f,0.5f); glVertex3f(x+1.0f, y-18.0f, z+3.0f);
        glTexCoord2f(0.5f,0.5f); glVertex3f(x-1.0f, y-18.0f, z+3.0f);

        glTexCoord2f(0.5f,0.0f); glVertex3f(x-1.0f, y-6.0f, z+0.5f);
        glTexCoord2f(1.0f,0.0f); glVertex3f(x+1.0f, y-6.0f, z+0.5f);
        glTexCoord2f(1.0f,0.5f); glVertex3f(x+1.0f, y-18.0f, z+0.5f);
        glTexCoord2f(0.5f,0.5f); glVertex3f(x-1.0f, y-18.0f, z+0.5f);
glEnd();

glPopMatrix();
4

1 回答 1

1

所有 OpenGL 旋转都是围绕点 (0, 0, 0) 完成的。因此,如果要围绕特定点(rx、ry、rz)旋转顶点,则必须进行以下转换:

Translate(-rx, -ry, -rz);
Rotate(alpha, beta, gamma);
Translate(rx, ry, rz);
于 2012-04-10T08:15:49.577 回答