如何在 python opengl 中使用键输入旋转两个单独的对象

How to rotate two separate objects with key input in python opengl

我想画两个矩形作为“机械臂”。这些应该能够围绕自己的轴单独旋转。矩形应位于坐标系的原点。使用键 'a' 和 'd' 绿色手臂应向左和向右旋转,使用键 'w' 和 's' 蓝色手臂应向左旋转和权利。 这是它应该如何旋转的直观表示: rotating robot arms

两个矩形我都画好了,还可以用按键移动。 但是,我无法正确旋转。我不能单独旋转矩形,它们总是同时旋转,也不围绕它们自己的轴旋转。 有谁知道我该怎么做以及我的代码中缺少什么? 我只发现了有关如何旋转单个对象的问题,但从来没有发现两个单独的对象合二为一 window。 (尤其不适用于 python + opengl)

到目前为止,这是我的代码:

import pygame
from OpenGL.GL import *
from OpenGL.GLU import *
from pygame.locals import *


def makeQuad(x, y, width, height):
glBegin(GL_QUADS)
glVertex2f(0, 0)  # x,y
glVertex2f(0, 50)
glVertex2f(50, 50)
glVertex2f(50, 0)
glEnd()


def main():
pygame.init()
window = (1200, 800)
display = pygame.display.set_mode(window, DOUBLEBUF | OPENGL)
pygame.display.set_caption('Roboterarm')
gluOrtho2D(0, 400, 0, 300)

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_a:  # green arm left
                glRotatef(180, 0, 0, 1)  # angle, x,y,z
                glTranslatef(-1, 0, 0)
            if event.key == pygame.K_d:  # green arm right
                glRotatef(-180, 0, 0, 1)
                glTranslatef(1, 0, 0)
            if event.key == pygame.K_w:  # blue arm left
                glTranslatef(0, 1, 0)
            if event.key == pygame.K_s:  # blue arm right
                glTranslatef(0, -1, 0)

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

    # glMultMatrixf(modelMatrix)
    # modelMatrix = glGetFloatv(GL_MODELVIEW_MATRIX)

    # glLoadIdentity()
    # glTranslatef(0, 0, -5)
    # glMultMatrixf(modelMatrix)

    # blue arm
    glPushMatrix()
    glTranslated(150, 100, 0)
    # glRotated(5, 0, 0, 1)
    glColor3fv((0, 0, 1))  # blue
    makeQuad(0, 0, 300, 100)
    glPopMatrix()

    # green arm
    glPushMatrix()
    glTranslated(250, 100, 0)
    # glRotated(10, 0, 0, 1)
    # glScale()
    glColor3fv((0, 1, 0))  # green
    makeQuad(0, 0, 300, 100)
    glPopMatrix()

    # a1 += 1
    # a2 += 0.5

    pygame.display.flip()

if __name__ == "__main__":
main()

有些行被注释掉了,因为我用这些贡献尝试了一些东西:

参见:

  • How can I make a sprite move when key is held down

您需要在按下按钮时更改角度 a1a2。按角度旋转几何体:

import pygame
from OpenGL.GL import *
from OpenGL.GLU import *
from pygame.locals import *

def makeQuad():
    glBegin(GL_QUADS)
    glVertex2f(-1, -1)
    glVertex2f(-1, 1)
    glVertex2f(1, 1)
    glVertex2f(1, -1)
    glEnd()

def main():
    pygame.init()
    window = (400, 300)
    display = pygame.display.set_mode(window, DOUBLEBUF | OPENGL)
    pygame.display.set_caption('Roboterarm')
    clock = pygame.time.Clock()
    gluOrtho2D(0, 400, 0, 300)

    a1, a2 = 30, -30
    while True:
        clock.tick(100)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()

        keys = pygame.key.get_pressed()
        a1 += (keys[pygame.K_d] - keys[pygame.K_a]) * 2
        a2 += (keys[pygame.K_w] - keys[pygame.K_s]) * 2

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

        # blue arm
        glPushMatrix()
        glTranslated(150, 100, 0)
        glRotated(a1, 0, 0, 1)
        glTranslated(-20, 0, 0)
        glScale(20, 5, 1)
        glColor3fv((0, 0, 1))  # blue
        makeQuad()
        glPopMatrix()

        # green arm
        glPushMatrix()
        glTranslated(200, 100, 0)
        glRotated(a2, 0, 0, 1)
        glTranslated(20, 0, 0)
        glScale(20, 5, 1)
        glColor3fv((0, 1, 0))  # green
        makeQuad()
        glPopMatrix()

        pygame.display.flip()

if __name__ == "__main__":
    main()