1

I want to draw two rectangles that serve as "robot arms". These should be able to rotate separately around their own axis. The rectangles should lie at the origin of the coordinate system. With the keys 'a' and 'd' the green arm shall be rotated to the left and right, and with the keys 'w' and 's' the blue arm shall be rotated to the left and right. Here is a visual representation of how it should rotate: rotating robot arms

I have already drawn both rectangles and can also move them with the keys. However, I can't get the rotation right. I can't rotate the rectangles separately, they always rotate at the same time and also not around their own axis. Does anyone know how I can do this and what is missing in my code? I have only found questions about how to rotate individual objects, but never two separate objects in one window. (especially not for python + opengl)

Here's my code so far:

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()

there are some lines commented out because I tried some things with these contributions: How can I rotate multiple quads separately? (Pygame, PyOpengl) How to rotate a cube using mouse in pyopengl

genpfault
  • 51,148
  • 11
  • 85
  • 139
gxma
  • 25
  • 4

1 Answers1

1

See:

You need to change the angles a1 and a2 when the buttons are pressed. Rotate the geometry by the angles:

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()
Rabbid76
  • 202,892
  • 27
  • 131
  • 174