#How To Map A Square Grid (2D Array) To Integer Co-ords When Length is Even Or Odd😵‍💫

1 messages · Page 1 of 1 (latest)

open peak
#

Hello.. I am currently writing code to transform a zero-based, 1D array into a 2D array to visualize my terrain grid.
Currently, it creates a square grid with as many rows/columns equal to grid_size.

while (...)
  var x = index % grid_size
  var z = index / grid_size
  var offset = (grid_size - 1) / 2
  var grid_pos = Vector2i(x - offset, offset - z)

Then I multiply the instances grid_pos by its size to space them evenly ...

instance.position = Vector3(grid_pos.x * CHUNK_SIZE, 0, grid_pos.y * CHUNK_SIZE)

Odd

My current grid is 3x3 (an odd grid_size) and it produces these 2d coordinates:

(-1, 1)
(0, 1)
(1, 1)
(-1, 0)
(0, 0)
(1, 0)
(-1, -1)
(0, -1)
(1, -1)

... This is great, because an odd grid has a center tile which is positioned at 0, 0.

Even

When the grid_size is an EVEN number, no one tile can be at the center. -meaning:
... the 0 coordinate should be taken by the four adjacent tiles that are around the center.
e.g. (-1, 1), (1, 1), (-1, -1), (1, -1)

Instead; I get these 2d coordinates which produces an un-centered grid:
(0, 0)
(1, 0)
(0, -1)
(1, -1)

EDIT:
I am mainly trying to map the grid pos to integer coordinates!
If the grid size is even, no grid_pos output should be at x=0 or y=0.
😵‍💫

#

I understand why the code resolves this way, but I cannot think of an approach which can center a 2D array whether it is even or odd in length.
Your advice is appreciated.

spark lotus
#

I copied your math and idk how you got (0, 0)(1, 0)(0, -1)(1, -1) with a grid size of 2, my values all get rounded down.

    var grid_size = 2
    for index in range(grid_size * grid_size):
        var x = index % grid_size
        var z = index / grid_size
        var offset = (grid_size - 1) / 2
        var grid_pos = Vector2i(x - offset, offset - z)
        print(grid_pos)

prints: (0.0, 0.0), (0.0, 0.0), (0.0, 0.0), (0.0, 0.0)

try change grid_pos from type Vector2i to Vector2 and see what happens

open peak
spark lotus
#

well i'll be damned, I started up my editor again and sure enough it prints what you posted, I think my tool script hadn't updated correctly, either way a simple fix to your problem would be to offset by 0.5 for even numbers.

var x = index % grid_size
var z = index / grid_size
var offset = (grid_size - 1) / 2
if grid_size % 2 == 0:
    offset += 0.5
var grid_pos = Vector2(x - offset, offset - z)
print(grid_pos)

again change from Vector2i to Vector2 to allow float values

open peak
open peak
#

How To Map A Square Grid (2D Array) To Integer Co-ords When Length is Even Or Odd😵‍💫

strange cargo
#

If you really need integers:
Given that your integer coordinates skip (0,0), I assume they don’t need to be spaced by 1? How about 2?
You can double all the coordinates, and then when it’s even, subtract (1,1). This should be always centered on (0,0) I think and have integer coordinates spaced by 2 for both even and odd

cunning bobcat
#

If you skip (0,0) there is a gap in your grid. An even integer size indeed cannot be centred around 0.
My approach would be to orient your integer grid positions top-left -that is, starting at 0,0 and increasing, no negative coordinates, not centred. Only worry about centering the grid when you render it, because you’re not restricted to integers then.

var grid_center = grid_size / 2.0
instance.position = Vector3((grid_pos.x - grid_center) * CHUNK_SIZE, 0.0, (grid_pos.y - grid_center) * CHUNK_SIZE)