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:
146 def rotate_around(self: Vertex, angle_in_radians: float, center: Vertex) -> Vertex:
147 translate_to_center: Vertex = self.translate(-center)
148 rotated_around_origin: Vertex = translate_to_center.rotate(angle_in_radians)
149 back_to_position: Vertex = rotated_around_origin.translate(center)
150 return back_to_position
Within the event loop, this seems quite reasonable
219while not glfw.window_should_close(window):
240 glColor3f(paddle1.r, paddle1.g, paddle1.b)
241
242 glBegin(GL_QUADS)
243 rotatePoint: Vertex = paddle1.position
244 for paddle1_vertex_in_model_space in paddle1.vertices:
245 paddle1_vertex_in_world_space: Vertex = paddle1_vertex_in_model_space.translate(paddle1.position)
246 paddle1_vertex_in_world_space: Vertex = paddle1_vertex_in_world_space.rotate_around(paddle1.rotation,
247 rotatePoint)
248 paddle1_vertex_in_ndc_space: Vertex = paddle1_vertex_in_world_space.uniform_scale(scalar=1.0/10.0)
249 glVertex2f(paddle1_vertex_in_ndc_space.x, paddle1_vertex_in_ndc_space.y)
256 # draw paddle 2
257 glColor3f(paddle2.r, paddle2.g, paddle2.b)
258
259 glBegin(GL_QUADS)
260 rotatePoint: Vertex = paddle2.position
261 for paddle2_vertex_model_space in paddle2.vertices:
262 paddle2_vertex_world_space: Vertex = paddle2_vertex_model_space.translate(paddle2.position)
263 paddle2_vertex_world_space: Vertex = paddle2_vertex_world_space.rotate_around(paddle2.rotation,
264 rotatePoint)
265 paddle2_vertex_ndc_space: Vertex = paddle2_vertex_world_space.uniform_scale(scalar=1.0/10.0)
266 glVertex2f(paddle2_vertex_ndc_space.x, paddle2_vertex_ndc_space.y)
267 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.