public void GetClosestCell(Vector3 pos, out Cell cell, out bool outsideCell, out Vector3 closestPoint)
        {
            cell         = null;
            closestPoint = Vector3.zero;
            outsideCell  = true;

            //if true then no result
            if (empty || _map == null)
            {
                return;
            }

            float       s        = PathFinder.gridSize / PathFinder.CELL_GRID_SIZE;
            List <Cell> mapChunk = _map
                                   [Mathf.Clamp((int)((pos.x - chunk.realX) / s), 0, PathFinder.CELL_GRID_SIZE - 1)]
                                   [Mathf.Clamp((int)((pos.z - chunk.realZ) / s), 0, PathFinder.CELL_GRID_SIZE - 1)];

            float sqrDist = float.MaxValue;

            //Debug.Log(mapChunk.Count);
            //search cell in chunk are inside chunk
            if (mapChunk.Count > 0)
            {
                for (int i = 0; i < mapChunk.Count; i++)
                {
                    Vector3 currentClosestToCell;
                    //see if this point are inside cell and get position if are
                    if (mapChunk[i].GetPointInsideCell(pos, out currentClosestToCell))
                    {
                        float curSqrDist = SomeMath.SqrDistance(pos, currentClosestToCell);
                        if (curSqrDist < sqrDist)
                        {
                            sqrDist      = curSqrDist;
                            cell         = mapChunk[i];
                            outsideCell  = false;
                            closestPoint = currentClosestToCell;
                        }
                    }
                }
            }

            //if there was result then here it is
            if (cell != null)
            {
                return;
            }

            //if there is no result then search it by checking all contours that represent hole in graph
            foreach (var pair in _contourLib)
            {
                Vector3 curNearest = SomeMath.NearestPointOnSegment(pair.Key.a, pair.Key.b, pos);
                float   curSqrDist = SomeMath.SqrDistance(curNearest, pos);

                if (curSqrDist < sqrDist)
                {
                    sqrDist      = curSqrDist;
                    cell         = pair.Value;
                    closestPoint = curNearest;
                }
            }
        }
Beispiel #2
0
 /// <summary>
 /// return nearest point on this segment Vector2 by XZ
 /// </summary>
 public Vector2 NearestPointXZ(Vector2 position)
 {
     return(SomeMath.NearestPointOnSegment(xLeft, zLeft, xRight, zRight, position.x, position.y));
 }
Beispiel #3
0
 /// <summary>
 /// return nearest point on this segment Vector2 by XZ
 /// </summary>
 public Vector2 NearestPointXZ(float positionX, float positionZ)
 {
     return(SomeMath.NearestPointOnSegment(xLeft, zLeft, xRight, zRight, positionX, positionZ));
 }
Beispiel #4
0
 /// <summary>
 /// return nearest point on this segment Vector3
 /// </summary>
 public Vector3 NearestPoint(Vector3 position)
 {
     return(SomeMath.NearestPointOnSegment(xLeft, yLeft, zLeft, xRight, yRight, zRight, position.x, position.y, position.z));
 }