Relative Objects - Demo 11¶
Objective¶
Introduce relative objects, by making a small blue square that is defined relative to the left paddle, but offset some in the x direction. When the paddle on the left moves or rotates, the blue square moves with it, because it is defined relative to it.

Demo 11¶
How to Execute¶
Load src/modelviewprojection/demo11.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 |
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 |
Description¶
Cayley Graph¶
In the graph below, all we have added is “Square space”, relative to paddle 1 space.

Demo 11¶
In the picture below, in 3D space, we see that the square has its own modelspace (as evidenced by the 3 arrows), and we are going to define its position and orientation relative to paddle 1.

Coordinate Frames¶
Code¶
Define the geometry of the square in its own modelspace.
128square: list[mu2d.Vector2D] = [
129 mu2d.Vector2D(x=-0.5, y=-0.5),
130 mu2d.Vector2D(x=0.5, y=-0.5),
131 mu2d.Vector2D(x=0.5, y=0.5),
132 mu2d.Vector2D(x=-0.5, y=0.5),
133]
Event Loop¶
176while not glfw.window_should_close(window):
...
Draw paddle 1, just as before.
197 GL.glColor3f(*iter(paddle1.color))
198
199 GL.glBegin(GL.GL_QUADS)
200 for p1_v_ms in paddle1.vertices:
201 ms_to_ndc: mu.InvertibleFunction[mu2d.Vector2D] = mu.compose(
202 [
203 # camera space to NDC
204 mu.uniform_scale(1.0 / 10.0),
205 # world space to camera space
206 mu.inverse(mu.translate(camera.position_ws)),
207 # model space to world space
208 mu.compose(
209 [
210 mu.translate(paddle1.position),
211 mu2d.rotate(paddle1.rotation),
212 ]
213 ),
214 ]
215 )
216
217 paddle1_vector_ndc: mu2d.Vector2D = ms_to_ndc(p1_v_ms)
218
219 GL.glVertex2f(paddle1_vector_ndc.x, paddle1_vector_ndc.y)
220 GL.glEnd()
As a refresher, the author recommends reading the code from modelspace to worldspace from the bottom up, and from worldspace to NDC from top down.
Read from modelspace to world space, bottom up
Reset the coordinate system
Read from world space to camera space, knowing that camera transformations are implemented as the inverse of placing the camera space in world space.
Reset the coordinate system
Read camera-space to NDC
New part! Draw the square relative to the first paddle! Translate the square to the right by 2 units. We are dealing with a -1 to 1 world space, which later gets scaled down to NDC.
224 GL.glColor3f(0.0, 0.0, 1.0)
225 GL.glBegin(GL.GL_QUADS)
226 for ms in square:
227 ms_to_ndc: mu.InvertibleFunction[mu2d.Vector2D] = mu.compose(
228 [
229 # camera space to NDC
230 mu.uniform_scale(1.0 / 10.0),
231 # world space to camera space
232 mu.inverse(mu.translate(camera.position_ws)),
233 # model space to world space
234 mu.compose(
235 [
236 mu.translate(paddle1.position),
237 mu2d.rotate(paddle1.rotation),
238 ]
239 ),
240 # square space to paddle 1 space
241 mu.translate(mu2d.Vector2D(x=2.0, y=0.0)),
242 ]
243 )
244 square_vector_ndc: mu2d.Vector2D = ms_to_ndc(ms)
245 GL.glVertex2f(square_vector_ndc.x, square_vector_ndc.y)
246 GL.glEnd()
Towards that, we need to do all of the transformations to the square that we would to the paddle, and then do any extra transformations afterwards.
As such, read
Read paddle1space to world space, from bottom up
If we were to plot the square now, it would be in paddle 1’s space. We don’t want that, we want in to be moved in the X direction some units. Therefore
Read modelspace to paddle1space, from bottom up
Reset the coordinate system.
Now the square’s geometry will be in its own space!
Read from worldspace to camera-space, knowing that camera transformations are implemented as the inverse of placing the camera space in world space.
Reset the coordinate system
Read camera-space to NDC
Draw paddle 2 just like before.
250 GL.glColor3f(*iter(paddle2.color))
251
252 GL.glBegin(GL.GL_QUADS)
253 for p2_v_ms in paddle2.vertices:
254 ms_to_ndc: mu.InvertibleFunction[mu2d.Vector2D] = mu.compose(
255 [
256 # camera space to NDC
257 mu.uniform_scale(1.0 / 10.0),
258 # world space to camera space
259 mu.inverse(mu.translate(camera.position_ws)),
260 # model space to world space
261 mu.compose(
262 [
263 mu.translate(paddle2.position),
264 mu2d.rotate(paddle2.rotation),
265 ]
266 ),
267 ]
268 )
269
270 paddle2_vector_ndc: mu2d.Vector2D = ms_to_ndc(p2_v_ms)
271
272 GL.glVertex2f(paddle2_vector_ndc.x, paddle2_vector_ndc.y)
273 GL.glEnd()