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
135square_rotation: float = 0.0

When ‘q’ is pressed, increase the angle.

src/modelviewprojection/demo12.py
140def handle_inputs() -> None:
141    global square_rotation
142    if glfw.get_key(window, glfw.KEY_Q) == glfw.PRESS:
143        square_rotation += 0.1
...

Event Loop

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

src/modelviewprojection/demo11.py
224    GL.glColor3f(0.0, 0.0, 1.0)
225    GL.glBegin(GL.GL_QUADS)
226    for ms in square:
227        ms_to_ndc: mu.InvertibleFunction[mu2d.Vector2D] = mu.compose(
228            [
229                # camera space to NDC
230                mu.uniform_scale(1.0 / 10.0),
231                # world space to camera space
232                mu.inverse(mu.translate(camera.position_ws)),
233                # model space to world space
234                mu.compose(
235                    [
236                        mu.translate(paddle1.position),
237                        mu2d.rotate(paddle1.rotation),
238                    ]
239                ),
240                # square space to paddle 1 space
241                mu.translate(mu2d.Vector2D(x=2.0, y=0.0)),
242            ]
243        )
244        square_vector_ndc: mu2d.Vector2D = ms_to_ndc(ms)
245        GL.glVertex2f(square_vector_ndc.x, square_vector_ndc.y)
246    GL.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
225    GL.glColor3f(0.0, 0.0, 1.0)
226    GL.glBegin(GL.GL_QUADS)
227    for ms in square:
228        ms_to_ndc: mu.InvertibleFunction[mu2d.Vector2D] = mu.compose(
229            [
230                # camera space to NDC
231                mu.uniform_scale(1.0 / 10.0),
232                # world space to camera space
233                mu.inverse(mu.translate(camera.position_ws)),
234                # model space to world space
235                mu.compose(
236                    [
237                        mu.translate(paddle1.position),
238                        mu2d.rotate(paddle1.rotation),
239                    ]
240                ),
241                # square space to paddle 1 space
242                mu.compose(
243                    [
244                        mu.translate(mu2d.Vector2D(x=2.0, y=0.0)),
245                        mu2d.rotate(square_rotation),
246                    ]
247                ),
248            ]
249        )
250        square_vector_ndc: mu2d.Vector2D = ms_to_ndc(ms)
251        GL.glVertex2f(square_vector_ndc.x, square_vector_ndc.y)
252    GL.glEnd()

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