Beispiel #1
0
        /*public IEnumerable<Cell> CellsFrame (int minX, int minZ, int maxX, int maxZ)
         * {
         *      for (int x=minX+1; x<=maxX; x++) yield return cells[
         *      {
         *              List<Coord> objs = cells[num].objs;
         *              for (int i=0; i<objs.Count; i++)
         *                      yield return objs[i];
         *      }
         * }*/

        #endregion

        public void Add(SpatialObject obj, Vector2 pos, float extend)
        {
            Vector2 min = pos - new Vector2(extend, extend);
            Vector2 max = pos + new Vector2(extend, extend);

            foreach (int cellNum in CellNumsInRect(min, max))
            {
                cells[cellNum].objs.Add(obj);
            }
            Count++;
        }
Beispiel #2
0
        public float MinDist(Vector2 p, bool skipSelf = true)
        {
            SpatialObject closest = Closest(p, skipSelf);

            if (closest == null)
            {
                return(20000000);
            }
            else
            {
                return((p - closest.pos).magnitude);
            }
        }
Beispiel #3
0
        public SpatialObject Closest_outdated(Vector2 p)
        {
            float         minDist = 20000000;
            SpatialObject closest = null;

            foreach (SpatialObject coord in this)
            {
                float dist = (coord.pos - p).sqrMagnitude;
                if (dist < 0.00001f)
                {
                    continue;                                  //if itself
                }
                if (dist < minDist)
                {
                    minDist = Mathf.Min(dist, minDist);
                    closest = coord;
                }
            }
            return(closest);
        }
Beispiel #4
0
        }                                                                                                     //no extend, non-generic

        //outdated, use Closest with bo rect instead
        public SpatialObject Closest(Vector2 pos, float range, bool skipSelf = true)
        {
            float         minDist = 20000000;
            SpatialObject closest = null;

            Vector2 min = new Vector2(pos.x - range, pos.y - range);
            Vector2 max = new Vector2(pos.x + range, pos.y + range);

            foreach (SpatialObject coord in ObjsInRect(min, max))
            {
                float dist = (coord.pos - pos).sqrMagnitude;
                if (dist < minDist)
                {
                    if (skipSelf && dist < 0.00001f)
                    {
                        continue;                                                  //if itself
                    }
                    minDist = Mathf.Min(dist, minDist);
                    closest = coord;
                }
            }
            return(closest);
        }
Beispiel #5
0
        public SpatialObject Closest(Vector2 pos, bool skipSelf = true)
        {
            float         minDist = 20000000;
            SpatialObject closest = null;

            float cellSize      = size / resolution;
            float searchDist    = 0.0001f;
            float maxSearchDist = size * 1.415f;

            bool closestFound = false;             //iterating one circle after closest was found

            //Gizmos.color = Color.green;

            while (searchDist <= maxSearchDist)
            {
                Vector2 min = new Vector2(pos.x - searchDist, pos.y - searchDist);
                Vector2 max = new Vector2(pos.x + searchDist, pos.y + searchDist);

                foreach (int cellNum in CellNumsInRect(min, max, inCenter:false))
                {
                    Cell cell          = cells[cellNum];
                    int  cellObjsCount = cell.objs.Count;
                    for (int i = 0; i < cellObjsCount; i++)
                    {
                        SpatialObject obj  = cell.objs[i];
                        float         dist = (obj.pos - pos).sqrMagnitude;
                        if (dist < minDist)
                        {
                            if (skipSelf && dist < 0.00001f)
                            {
                                continue;                                                          //if itself
                            }
                            minDist = dist;
                            closest = obj;
                        }
                    }

                    //Gizmos.DrawWireCube((cell.min+Vector2.one*cellSize/2).V3(), (Vector2.one*cellSize).V3());
                }

                //Gizmos.color = new Color( Gizmos.color.r+0.2f, Gizmos.color.g-0.2f, 0, 1);

                searchDist += cellSize;
                if (closestFound)
                {
                    break;
                }
                if (closest != null)
                {
                    closestFound = true;                                  //iterating one circle after closest was found
                }
            }

            return(closest);

            /*searchDist = cellSize;
             * while (searchDist <= maxSearchDist)
             * {
             *      SpatialObject closest1 = Closest(pos, searchDist, skipSelf);
             *      if (closest1 != null) return closest1;
             *      searchDist *= 2;
             * }
             * return null;*/
        }
Beispiel #6
0
        }                                                                                                              //no extend

        public void Remove(SpatialObject obj)
        {
            GetCellByPoint(obj.pos).objs.Remove(obj); Count--;
        }                                                                                                     //no extend, non-generic
Beispiel #7
0
 //public void Remove (Coord obj) { for (int i=0; i<cells.Length; i++) cells[i].objs.Remove(obj); }
 public void Remove(SpatialObject obj, Vector2 pos)
 {
     GetCellByPoint(pos).objs.Remove(obj); Count--;
 }                                                                                                              //no extend
Beispiel #8
0
        }         //no extend, non-generic

        public void Add(SpatialObject obj)
        {
            GetCellByPoint(obj.pos).objs.Add(obj); Count++;
        }                                                                                               //no extend, non-generic
Beispiel #9
0
 public void Add(SpatialObject obj, Vector2 pos)
 {
     GetCellByPoint(pos).objs.Add(obj); Count++;
 }                                                                                                        //no extend