Rotate the Square - Demo 12¶
Purpose¶
Rotate the square around its origin.
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 its center |
Description¶
Cayley Graph¶
Code¶
Make a variable to determine the angle that the square will be rotated.
193square_rotation: float = 0.0
When ‘q’ is pressed, increase the angle.
198def handle_inputs() -> None:
199 global square_rotation
200 if glfw.get_key(window, glfw.KEY_Q) == glfw.PRESS:
201 square_rotation += 0.1
...
Event Loop¶
In the previous chapter, this was the rendering code for the square.
268 glColor3f(0.0, 0.0, 1.0)
269 glBegin(GL_QUADS)
270 for model_space in square:
271 paddle1space: Vertex = model_space.translate(Vertex(x=2.0, y=0.0))
272 world_space: Vertex = paddle1space.rotate(paddle1.rotation) \
273 .translate(paddle1.position)
274 camera_space: Vertex = world_space.translate(translate_amount=-camera.position_worldspace)
275 ndc_space: Vertex = camera_space.uniform_scale(scalar=1.0/10.0)
276 glVertex2f(ndc_space.x, ndc_space.y)
277 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.
268 glColor3f(0.0, 0.0, 1.0)
269 glBegin(GL_QUADS)
270 for model_space in square:
271 paddle_1_space: Vertex = model_space.rotate(square_rotation) \
272 .translate(Vertex(x=2.0, y = 0.0))
273 world_space: Vertex = paddle_1_space.rotate(paddle1.rotation) \
274 .translate(paddle1.position)
275 camera_space: Vertex = world_space.translate(-camera.position_worldspace)
276 ndc_space: Vertex = camera_space.uniform_scale(scalar=1.0/10.0)
277 glVertex2f(ndc_space.x, ndc_space.y)
278 glEnd()
The author is getting really tired of having to look at all the different transformation functions repeatedly defined for each object being drawn.