Rotate the Square - Demo 12

Objective

Rotate the square around its origin.

Demo 12

Demo 12

How to Execute

Load src/modelviewprojection/demo12.py in Spyder and hit the play button.

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 its center

Description

Cayley Graph

Demo 12

Demo 12

Code

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

src/modelviewprojection/demo12.py
163square_rotation: float = 0.0

When ‘q’ is pressed, increase the angle.

src/modelviewprojection/demo12.py
168def handle_inputs() -> None:
169    global square_rotation
170    if glfw.get_key(window, glfw.KEY_Q) == glfw.PRESS:
171        square_rotation += 0.1
...

Event Loop

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

src/modelviewprojection/demo11.py
243    glColor3f(0.0, 0.0, 1.0)
244    glBegin(GL_QUADS)
245    for ms in square:
246        ms_to_ndc: InvertibleFunction[Vector2D] = compose(
247            # camera space to NDC
248            uniform_scale(1.0 / 10.0),
249            # world space to camera space
250            inverse(translate(camera.position_ws)),
251            # model space to world space
252            compose(translate(paddle1.position), rotate(paddle1.rotation)),
253            # square space to paddle 1 space
254            translate(Vector2D(x=2.0, y=0.0)),
255        )
256        square_vector_ndc: Vector2D = ms_to_ndc(ms)
257        glVertex2f(square_vector_ndc.x, square_vector_ndc.y)
258    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.

src/modelviewprojection/demo12.py
246    glColor3f(0.0, 0.0, 1.0)
247    glBegin(GL_QUADS)
248    for ms in square:
249        ms_to_ndc: InvertibleFunction[Vector2D] = compose(
250            # camera space to NDC
251            uniform_scale(1.0 / 10.0),
252            # world space to camera space
253            inverse(translate(camera.position_ws)),
254            # model space to world space
255            compose(translate(paddle1.position), rotate(paddle1.rotation)),
256            # square space to paddle 1 space
257            compose(translate(Vector2D(x=2.0, y=0.0)), rotate(square_rotation)),
258        )
259        square_vector_ndc: Vector2D = ms_to_ndc(ms)
260        glVertex2f(square_vector_ndc.x, square_vector_ndc.y)
261    glEnd()

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