I need to understand WHY

Please explain the logic behind this code:

rock.position = CELL_SIZE * cell + random_offset

Why do we have to multiply CELL_SIZE by cell? I got the idea of adding the offset, but I didn't understand the reason for the multiplication.

Thank you in advance

  • godoterfr replied

    func add_rocks_on_grid(columns: int, rows: int) -> void:
        for column in range(columns):
            for row in range(rows):
                var cell := Vector2(column, row)

    For example, the 1st rock in the 1st column is :
    cell = Vector2(0, 0)

    The 2nd rock in the 1st column :
    cell = Vector2(0, 1)

    This is their position achieved by the nested for loops.
    We want each cell to be a dimension of Vector(128, 128). So we multiply the cell position by its size.

    (128, 128) * (0, 1) + random_offset = (0, 128) + random_offset

    1 love
  • Nathan Lovato replied

    To add to godoterfr's explanation, the variable cell represents the cell's coordinates on a virtual game board. These coordinates are not screen coordinates in pixels. By multiplying the cell by CELL_SIZE, we convert the coordinates from the virtual game board to a screen position in pixels.

    1 love