Relative Objects - Demo 11

Purpose

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

Demo 11

How to Execute

On Linux or on MacOS, in a shell, type “python src/demo11/demo.py”. On Windows, in a command prompt, type “python src\demo11\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

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

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

Coordinate Frames

Code

Define the geometry of the square in it’s own modelspace.

square: Paddle = [
    Vertex(x=-5.0, y=-5.0),
    Vertex(x=5.0, y=-5.0),
    Vertex(x=5.0, y=5.0),
    Vertex(x=-5.0, y=5.0),
]

Event Loop

while not glfw.window_should_close(window):
...

Draw paddle 1, just as before.

    glColor3f(paddle1.r, paddle1.g, paddle1.b)

    glBegin(GL_QUADS)
    for model_space in paddle1.vertices:
        world_space: Vertex = model_space.rotate(paddle1.rotation) \
                                         .translate(tx=paddle1.position.x,
                                                    ty=paddle1.position.y)
        camera_space: Vertex = world_space.translate(tx=-camera.position_worldspace.x,
                                                     ty=-camera.position_worldspace.y)
        ndc_space: Vertex = camera_space.scale(scale_x=1.0 / 100.0,
                                               scale_y=1.0 / 100.0)
        glVertex2f(ndc_space.x, ndc_space.y)
    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 cameraspace to NDC

New part! Draw the square relative to the first paddle! Translate the square to the right by 20 units. We are dealing with a -100 to 100 world space, which later gets scaled down to NDC.

    glColor3f(0.0, 0.0, 1.0)
    glBegin(GL_QUADS)
    for model_space in square:
        paddle1space: Vertex = model_space.translate(tx=20.0,
                                                     ty=0.0)
        world_space: Vertex = paddle1space.rotate(paddle1.rotation) \
                                          .translate(tx=paddle1.position.x,
                                                     ty=paddle1.position.y)
        camera_space: Vertex = world_space.translate(tx=-camera.position_worldspace.x,
                                                     ty=-camera.position_worldspace.y)
        ndc_space: Vertex = camera_space.scale(scale_x=1.0 / 100.0,
                                               scale_y=1.0 / 100.0)
        glVertex2f(ndc_space.x, ndc_space.y)
    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 it’s own space!

  • Read from worldspace to cameraspace, knowing that camera transformations are implemented as the inverse of placing the camera space in world space.

  • Reset the coordinate system

  • Read cameraspace to NDC

Draw paddle 2 just like before.

    glColor3f(paddle2.r, paddle2.g, paddle2.b)

    glBegin(GL_QUADS)
    for model_space in paddle2.vertices:
        world_space: Vertex = model_space.rotate(paddle2.rotation) \
                                         .translate(tx=paddle2.position.x,
                                                    ty=paddle2.position.y)
        camera_space: Vertex = world_space.translate(tx=-camera.position_worldspace.x,
                                                     ty=-camera.position_worldspace.y)
        ndc_space: Vertex = camera_space.scale(scale_x=1.0 / 100.0,
                                               scale_y=1.0 / 100.0)
        glVertex2f(ndc_space.x, ndc_space.y)
    glEnd()