How to Map a Matrix [x,y] to World coordinates.

Hi guys, I have a problem i cant solve easily it seems.
I did a Pacman grid map using Tiled software. i copied the matrix from the json it produces and i have created a class for this map, it holds the public static in[,] matrix of 28 col X 36 rows representing the map.
Then i did a TileCreator that spawns every 1X1 tile.

    void spawnTiles(){
        for (int Y = 0; Y <28; Y++) {
            for (int X = 0; X < 36; X++) {
                if (map[X,Y] == 2) {
                    GameObject go = Instantiate(VioletCube, new Vector2(Y * 10,-X*10), Quaternion.Euler(-90,0,0));
                    go.transform.parent = this.transform;
                }
                if (map[X,Y] == 1){                      GameObject go=Instantiate(blackCube, new Vector2(Y * 10,- X * 10), Quaternion.Euler(-90, 0, 0));
                    go.transform.parent = this.transform;
                }
            }

        }
    }

IF i want to know if the next tile is valid i have to do this, please not the X,Y inverted.

csharp** ** public bool nextTileIsValid (Vector3 dir){ if (dir.x == 1)return Mapa.map [mYPos, mXPos+1] == 1; if (dir.x == -1)return Mapa.map [mYPos, mXPos-1] == 1; if (dir.z == 1)return Mapa.map [mYPos-1, mXPos] == 1; if (dir.z == -1)return Mapa.map [mYPos+1, mXPos] == 1; return false; }** **

I am trying to map every x,y position in the matrix to an X,Y position in world space, so then i read the pacman´s position for example 120,180 and i know where pacman is, reading the matrix´s position [12,18]. The problem here is that every time i want to read the matrix, i cant do a simple map[12,18], i have to invert the numbers → map[18,12] in order to read the correct tile i am in. I did the movement and it works, i am programming the enemy´s steering behavior and iam struggling really bad to make it work properly, because of this inverted coordinates problem i am most of the time guessing what to code.
I am wondering how should i do this properly, i am using old school coding, nothing of colliders, nothing of nothing basically, and since this game is really old, and the grid system is very comon in old games, i am wondering how is the correct way of doing it.

here the map just for a reference.



páginas de fotos

1 Like

@MaxiARG

Hi, it would have been easier, if you would have just pasted the grid data as text…

but anyway - I’ve done similar thing to learn tile mapping, a couple of times.

What I did;

Abstract the querying of tiles from 2D array.

Your array is visually in text format like we think it; top is top and so on… but in array terms, your line above is before in array, and line below is next… so do a method that gives you “tile above” and “tile below” and tiles to left and right.

This way for your code for enemies and your player related stuff, you can just forget which way is up, and call this method to find out “is tile above free?” or “is there a ghost below player?”.

Also, this way you can also make your array 1D, and calculate the positions in this method, and forget about the implementation of tile map data.

Hi @eses ! thanks for reply.
Actually i did a method named isTileAboveFree, and so on. It works, but the problem is that the matrix is kind of rotated 90 degrees to the right, or at least that is what i think. So if i want to do something like this
if (dir.x == 1)return Mapa.map [XPos, YPos+1] == 1;
It wont work. I have to invert the X and Y coordinate to
if (dir.x == 1)return Mapa.map [mYPos, mXPos+1] == 1;
What i think that is happening is that if i query for the above row tile, it gives me the left column tile and i find myself all the time guessing how should i retrieve positions.
Now that i amdoing the ghost behavior, its becoming a mess to figure it out. The code to do it is really simple but it fails all the time because i am rading the wrong position in the matrix :frowning:
I think this abstraction will help to explain what is going on, If i do a map[X,Y+1] it wont give me the tile below me(Zero is at left upper corner). if i do a map[X-1,Y], it wont give me the tile to the left