Ejemplo n.º 1
0
    //based on your position and your linked flowfield, this method returns the vector you need to apply
    public int getValuefromObject(GameObject go)
    {
        /*
         * Q3 | Q0
         * -------
         * Q2 | Q1
         *
         *    +X
         * -Z    +Z
         *    -X
         */

        int id = go.GetInstanceID();

        custom_grid subGrid    = go.GetComponent <flowfield_pathfinding>().subGrid;
        int         grid_ratio = go.GetComponent <flowfield_pathfinding>().grid_ratio;

        int xCell = subGrid.worldToCell(go.transform.position).Item1 % grid_ratio; //10 is the number of cells per supergrid
        int zCell = subGrid.worldToCell(go.transform.position).Item2 % grid_ratio;

        if (xCell < 0) //compensate for negatives
        {
            xCell = grid_ratio + xCell;
        }

        if (zCell < 0)
        {
            zCell = grid_ratio + zCell;
        }

        //we have to transform our world grid coordinates (pos and neg) to the coordinates of the flowfield (pos only)
        switch (type)
        {
        case 0:
            break;

        case 1:
            xCell = xCell + grid_ratio;
            break;

        case 2:
            xCell = xCell + grid_ratio;
            zCell = zCell + grid_ratio;
            break;

        case 3:
            zCell = zCell + grid_ratio;
            break;
        }

        //DEBUG STUFF

        return(grid[xCell, zCell]);
    }
    // Start is called before the first frame update
    void Awake()
    {
        subGrid          = new custom_grid();
        subGrid.cellSize = 5.0f;

        superGrid          = new custom_grid();
        superGrid.cellSize = 50.0f;

        destination = transform.position;

        Path = new Stack <Pair <int, int> >(); //initialize empty path

        djgrid = new DijkstraGrid();           //dijkstra grid is created during flowfield generation, it's used to determine if a cell is blocked or not
    }
Ejemplo n.º 3
0
    float radius = 2.0f; //radius of capsulecast

    //This function will generate a 2D array of either NULL or MAXINT depending on whether there are
    //obstacles in the way or not.
    public int[,] Generate_grid(float cellSize, int row_size, int col_size, int xOff, int zOff)
    {
        xOffset       = xOff;
        zOffset       = zOff;
        grid          = new custom_grid();
        grid.cellSize = cellSize;

        int[,] obstacle_grid = new int[row_size, col_size]; //create an empty grid to load obstacle values into


        //col is col index, row is row index
        for (int col = 0; col < col_size; col++)
        {
            for (int row = 0; row < row_size; row++)
            {
                Vector3 half_offset = (Vector3.right * 0.5f * cellSize) + (Vector3.forward * 0.5f * cellSize);
                //create a new ray from 100 units up pointing down towards the map
                Ray ray = new Ray(grid.cellToWorld(row + xOffset, col + zOffset) + half_offset + (1000.0f * Vector3.up), Vector3.down);

                Vector3 origin = grid.cellToWorld(row + xOffset, col + zOffset) + half_offset + (1000.0f * Vector3.up);

                //if (Physics.Raycast(ray, out hit, 50000.0f,1<<10)) //check a raycast out to infinity
                if (Physics.SphereCast(origin, radius, Vector3.down, out hit, 50000.0f, (1 << 10) | (1 << 14)))
                {
                    //return the GameObject we hit
                    go = hit.transform.gameObject;
                    obstacle_grid[row, col] = Int32.MaxValue;                               //Max value represents impassable object
                }
                else if (Physics.Raycast(origin, -Vector3.up, out hit, 50000.0f, (1 << 8))) //check if it is not an impassable slope
                {
                    if (Vector3.Angle(Vector3.up, hit.normal) > 5.0f)
                    {
                        obstacle_grid[row, col] = Int32.MaxValue; //Max value represents impassable object
                    }
                    else if (hit.point.y < 3.8f)
                    {
                        obstacle_grid[row, col] = Int32.MaxValue; //Max value represents impassable object
                    }
                }
            }
        }

        return(obstacle_grid);
    }