Get node from position isometric

Hello,

I created a grid on isometric view (in 2D project):

    public static void CreateGrid()
    {
        Debug.Log("Create Grid");
        Node.TypeTerrain type_terrain_node = new Node.TypeTerrain();
        //get the world position of the left of the grid
        Vector3 world_left = transform.position + (Vector3.left * grid_world_size.x * node_diameter);

        for (int y = 0; y < grid_size_y; y++)
        {
            for (int x = 0; x < grid_size_x; x++)
            {
                //get the pos of the current created Node
                Vector3 worldPoint = world_left + new Vector3(node_diameter * (x+y) + node_radius, node_radius * (-x + y) , 0);

                type_terrain_node.Solid = Physics2D.OverlapCircle(worldPoint, node_radius - mini_radius, solid_mask);

                grid[x, y] = new Node(worldPoint, type_terrain_node, x, y);       //create the new node
            }
        }
    }

Then, i want to get a tile from the array “grid” with a world position (which set in tile when the grid was create). For information, here is the Node class:

using UnityEngine;
using System.Collections;

public class Node : IHeapItem<Node>
{

    public Vector3 position;
    public int gridX;
    public int gridY;

    public Node parent;
    public int gCost;
    public int hCost;
    int heap_index;

    public struct TypeTerrain 
    {
        public bool Solid;
        public bool Walk;
    };
    public TypeTerrain type_terrain;

    public bool water;
    public bool waterEnergy;
    public bool windEnergy;

    public Node(Vector2 _position, TypeTerrain _type_terrain, int _gridX, int _gridY)
    {
        position = _position;
        type_terrain = _type_terrain;
        gridX = _gridX;
        gridY = _gridY;
    }

    public int fCost
    {
        get
        {
            return gCost + hCost;
        }
    }

    public int HeapIndex
    {
        get
        {
            return heap_index;
        }
        set
        {
            heap_index = value;
        }
    }

    public int CompareTo(Node node_to_compare)
    {
        int compare = fCost.CompareTo(node_to_compare.fCost);
        if (compare == 0)
        {
            compare = hCost.CompareTo(node_to_compare.hCost);
        }
        return -compare;
    }
}

I found this method which works pretty well in orthogonal view (or carthesian) but not in this isometric view:

    public static Node NodeFromWorldPoint(Vector3 worldPosition)       //Give the grid position of a world position
    {
        float percentX = (worldPosition.x + grid_world_size.x / 2) / grid_world_size.x;
        float percentY = (worldPosition.y + grid_world_size.y / 2) / grid_world_size.y;
        percentX = Mathf.Clamp01(percentX);
        percentY = Mathf.Clamp01(percentY);

        int x = Mathf.RoundToInt((grid_size_x - 1) * percentX);
        int y = Mathf.RoundToInt((grid_size_y - 1) * percentY);

        return grid[x, y];
    }

I was think, it’s a common problem but i don’t find some topic talk about it.

Thanks for your help and your time :slight_smile:

If there are some people interessted, here the code:

 //Give the grid position of a world position    
    public static Node NodeFromWorldPoint(Vector3 world_position)       
        {
            int x = (int)((world_position.x - 2 * world_position.y - node_radius - world_grid_position.x - 2 * world_grid_position.y) / (4 * node_radius));
            int y = (int)((world_position.x + 2 * world_position.y - node_radius - world_grid_position.x - 2 * world_grid_position.y) / (4 * node_radius));
    
            return grid[x, y];
        }

And the function call it for retrive tile from an object that has it x and y size tile:

    public static List<Node> GetNodesGround(GameObject obj, Tiled tiled_obj)
    {
        Debug.Log("Get Nodes Ground");
        List<Node> ground_nodes = new List<Node>();
        Vector3 pos_obj = obj.GetComponent<EditorGrid>().RoundPosition();
        float middle_height_tiles = ((float)(tiled_obj.cell_size_x + tiled_obj.cell_size_y) / 4) * node_diameter;

        Vector2 base_position = new Vector2((pos_obj.x + offset_grid.x - obj.GetComponent<Renderer>().bounds.size.x / 2) + node_radius,
           (pos_obj.y + offset_grid.y - obj.GetComponent<Renderer>().bounds.size.y / 2) + middle_height_tiles);

        Node n = NodeFromWorldPoint(new Vector3(base_position.x, base_position.y, 0));
        ground_nodes.Add(n);
        for (int y = 0; y < tiled_obj.cell_size_y; y++)
        {
            for (int x = 0; x < tiled_obj.cell_size_x; x++)
            {
                ground_nodes.Add(grid[n.gridX + x, n.gridY + y]);
            }
        }

        return ground_nodes;
    }

With all these informations, anyone can (theoretically) create a tile isometric 2d world and get/update tiles.