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.
class Vertex:
def rotate_around(self: Vertex, angle_in_radians: float, center: Vertex) -> Vertex:
translate_to_center: Vertex = self.translate(tx=-center.x,
ty=-center.y)
rotated_around_origin: Vertex = translate_to_center.rotate(angle_in_radians)
back_to_position: Vertex = rotated_around_origin.translate(tx=center.x,
ty=center.y)
return back_to_position
Within the event loop, this seems quite reasonable
while not glfw.window_should_close(window):
glColor3f(paddle1.r, paddle1.g, paddle1.b)
glBegin(GL_QUADS)
rotatePoint: Vertex = paddle1.position
for model_space in paddle1.vertices:
world_space: Vertex = model_space.translate(tx=paddle1.position.x,
ty=paddle1.position.y)
world_space: Vertex = world_space.rotate_around(paddle1.rotation,
rotatePoint)
ndc_space: Vertex = world_space.scale(scale_x=1.0 / 100.0,
scale_y=1.0 / 100.0)
glVertex2f(ndc_space.x, ndc_space.y)
# draw paddle 2
glColor3f(paddle2.r, paddle2.g, paddle2.b)
glBegin(GL_QUADS)
rotatePoint: Vertex = paddle2.position
for model_space in paddle2.vertices:
world_space: Vertex = model_space.translate(tx=paddle2.position.x,
ty=paddle2.position.y)
world_space: Vertex = world_space.rotate_around(paddle2.rotation,
rotatePoint)
ndc_space: Vertex = world_space.scale(scale_x=1.0 / 100.0,
scale_y=1.0 / 100.0)
glVertex2f(ndc_space.x, ndc_space.y)
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.