Rotate the Square - Demo 12

Purpose

Rotate the square around its origin.

Demo 12

Demo 12

How to Execute

On Linux or on MacOS, in a shell, type “python src/demo12/demo.py”. On Windows, in a command prompt, type “python src\demo12\demo.py”.

Move the Paddles using the Keyboard

Keyboard Input

Action

w

Move Left Paddle Up

s

Move Left Paddle Down

k

Move Right Paddle Down

i

Move Right Paddle Up

d

Increase Left Paddle’s Rotation

a

Decrease Left Paddle’s Rotation

l

Increase Right Paddle’s Rotation

j

Decrease Right Paddle’s Rotation

UP

Move the camera up, moving the objects down

DOWN

Move the camera down, moving the objects up

LEFT

Move the camera left, moving the objects right

RIGHT

Move the camera right, moving the objects left

q

Rotate the square around it’s center

Description

Cayley Graph

Demo 12

Demo 12

Code

Make a variable to determine the angle that the square will be rotated.

square_rotation: float = 0.0

When ‘q’ is pressed, increase the angle.

def handle_inputs() -> None:
    global square_rotation
    if glfw.get_key(window, glfw.KEY_Q) == glfw.PRESS:
        square_rotation += 0.1
...

Event Loop

In the previous chapter, this was the rendering code for the square.

    glColor3f(0.0, 0.0, 1.0)
    glBegin(GL_QUADS)
    for model_space in square:
        paddle1space: Vertex = model_space.translate(tx=20.0,
                                                     ty=0.0)
        world_space: Vertex = paddle1space.rotate(paddle1.rotation) \
                                          .translate(tx=paddle1.position.x,
                                                     ty=paddle1.position.y)
        camera_space: Vertex = world_space.translate(tx=-camera.position_worldspace.x,
                                                     ty=-camera.position_worldspace.y)
        ndc_space: Vertex = camera_space.scale(scale_x=1.0 / 100.0,
                                               scale_y=1.0 / 100.0)
        glVertex2f(ndc_space.x, ndc_space.y)
    glEnd()

Since we just want to add one rotation at the end of the sequence of transformations from paddle 1 space to square space, just add a rotate call at the top.

    glColor3f(0.0, 0.0, 1.0)
    glBegin(GL_QUADS)
    for model_space in square:
        paddle_1_space: Vertex = model_space.rotate(square_rotation) \
                                            .translate(tx=20.0,
                                                       ty=0.0)
        world_space: Vertex = paddle_1_space.rotate(paddle1.rotation) \
                                            .translate(tx=paddle1.position.x,
                                                       ty=paddle1.position.y)
        camera_space: Vertex = world_space.translate(tx=-camera.position_worldspace.x,
                                                     ty=-camera.position_worldspace.y)
        ndc_space: Vertex = camera_space.scale(scale_x=1.0 / 100.0,
                                               scale_y=1.0 / 100.0)
        glVertex2f(ndc_space.x, ndc_space.y)
    glEnd()

The author is getting really tired of having to look at all the different transformation functions repeatedly defined for each object being drawn.