Rotation Fix Attempt 1 - Demo 08¶
Purpose¶
Fix the rotation problem from the previous demo in a seemingly intuitive way, but do it inelegantly.
How to Execute¶
On Linux or on MacOS, in a shell, type “python src/demo08/demo.py”. On Windows, in a command prompt, type “python src\demo08\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 |
Description¶
The problem in the last demo is that all rotations happen relative to World Space’s (0,0) and axes. By translating our paddles to their position before rotating, they are rotated around World Space’s origin, instead of being rotated around their modelspace’s center.
In this demo, we try to solve the problem by making a method to rotate around a given point in world space, in this case, the paddle’s center.
109class Vertex:
143 def rotate_around(self: Vertex, angle_in_radians: float, center: Vertex) -> Vertex:
144 translate_to_center: Vertex = self.translate(-center)
145 rotated_around_origin: Vertex = translate_to_center.rotate(angle_in_radians)
146 back_to_position: Vertex = rotated_around_origin.translate(center)
147 return back_to_position
Within the event loop, this seems quite reasonable
218while not glfw.window_should_close(window):
237 glColor3f(paddle1.r, paddle1.g, paddle1.b)
238
239 glBegin(GL_QUADS)
240 rotatePoint: Vertex = paddle1.position
241 for paddle1_vertex_in_model_space in paddle1.vertices:
242 paddle1_vertex_in_world_space: Vertex = paddle1_vertex_in_model_space.translate(paddle1.position)
243 paddle1_vertex_in_world_space: Vertex = paddle1_vertex_in_world_space.rotate_around(paddle1.rotation,
244 rotatePoint)
245 paddle1_vertex_in_ndc_space: Vertex = paddle1_vertex_in_world_space.uniform_scale(scalar=1.0/10.0)
246 glVertex2f(paddle1_vertex_in_ndc_space.x, paddle1_vertex_in_ndc_space.y)
253 # draw paddle 2
254 glColor3f(paddle2.r, paddle2.g, paddle2.b)
255
256 glBegin(GL_QUADS)
257 rotatePoint: Vertex = paddle2.position
258 for paddle2_vertex_model_space in paddle2.vertices:
259 paddle2_vertex_world_space: Vertex = paddle2_vertex_model_space.translate(paddle2.position)
260 paddle2_vertex_world_space: Vertex = paddle2_vertex_world_space.rotate_around(paddle2.rotation,
261 rotatePoint)
262 paddle2_vertex_ndc_space: Vertex = paddle2_vertex_world_space.uniform_scale(scalar=1.0/10.0)
263 glVertex2f(paddle2_vertex_ndc_space.x, paddle2_vertex_ndc_space.y)
264 glEnd()
All we did was add a rotate around method, and call it, with the paddle’s center as the rotate point.
Although this works for now and looks like decent code, this is extremely sloppy, and not thought out well at all. We apply a transformation from paddle space to world space, then do the inverse, then rotate, and then do the first transformation from paddle space to world space again.
The images of the transformation sequence below should show how brain-dead it is, and the Cayley graph is gross.
But from this we will learn something important.
translating back to the origin
resetting the coordinate system
rotating
resetting the coordinate system
and them translating them back to the paddle space origin
Cayley Graph¶
Note, this is gross, and the edge from the paddlespace to itself doesn’t even make any sense, but the author did not know how else to represent this code.