Rotation Fix Attempt 1 - Demo 08

Objective

Fix the rotation problem from the previous demo in a seemingly intuitive way, but do it inelegantly.

Demo 08

Demo 08

How to Execute

Load src/modelviewprojection/demo08.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

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.

src/modelviewprojection/mathutils2d.py
28@dataclass
29class Vector2D:
30    x: float  #: The x-component of the 2D Vector
31    y: float  #: The y-component of the 2D Vector
src/modelviewprojection/mathutils2d.py
188def rotate_around(
189    angle_in_radians: float, center: Vector2D
190) -> InvertibleFunction:
191    translation_to_origin: InvertibleFunction[Vector2D] = translate(-center)
192    rotation: InvertibleFunction[Vector2D] = rotate(angle_in_radians)
193    translation_back: InvertibleFunction[Vector2D] = translate(center)
194
195    return compose(translation_back, rotation, translation_to_origin)
196
197

Within the event loop, this seems quite reasonable

src/modelviewprojection/demo08.py
171while not glfw.window_should_close(window):
src/modelviewprojection/demo08.py
192    glColor3f(*astuple(paddle1.color))
193
194    glBegin(GL_QUADS)
195    rotatePoint: Vector2D = paddle1.position
196    for p1_v_ms in paddle1.vertices:
197        fn: InvertibleFunction[Vector2D] = compose(
198            uniform_scale(1.0 / 10.0),
199            rotate_around(paddle1.rotation, rotatePoint),
200            translate(paddle1.position),
201        )
202        paddle1_vector_ndc: Vector2D = fn(p1_v_ms)
203        glVertex2f(paddle1_vector_ndc.x, paddle1_vector_ndc.y)
src/modelviewprojection/demo08.py
208    # draw paddle 2
209    glColor3f(*astuple(paddle2.color))
210
211    glBegin(GL_QUADS)
212    rotatePoint: Vector2D = paddle2.position
213    for p2_v_ms in paddle2.vertices:
214        fn: InvertibleFunction[Vector2D] = compose(
215            uniform_scale(1.0 / 10.0),
216            rotate_around(paddle2.rotation, rotatePoint),
217            translate(paddle2.position),
218        )
219        paddle2_vector_ndc: Vector2D = fn(p2_v_ms)
220        glVertex2f(paddle2_vector_ndc.x, paddle2_vector_ndc.y)
221    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.

Demo 08

translating back to the origin

Demo 08

resetting the coordinate system

Demo 08

rotating

Demo 08

resetting the coordinate system

Demo 08

and them translating them back to the paddle space origin

Demo 08

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.

Demo 08