/// <summary> /// Inserts an object into the grid, expanding the cell to fit the element /// </summary> /// <param name="obj">The object to insert</param> public void Insert(IGridElt obj) { int xIdx = GridLocalToCellCol(obj.Xf); int yIdx = GridLocalToCellRow(obj.Yf); InsertToCell(obj, xIdx, yIdx); }
/// <summary> /// Removes an element from the grid. The <see cref="IUGridElt"/> of the object must give /// the same position where the element is currently in the grid /// </summary> /// <param name="obj">The object to remove</param> public void Remove(IGridElt obj) { int xIdx = GridLocalToCellCol(obj.Xf); int yIdx = GridLocalToCellRow(obj.Yf); RemoveFromCell(obj, xIdx, yIdx); }
/// <summary> /// Moves an element in the grid from the former position to the new one. /// </summary> /// <param name="obj">The object to move</param> /// <param name="fromX">The current position of the object in the grid</param> /// <param name="fromY">The current position of the object in the grid</param> /// <param name="toX">The new position of the object in the grid</param> /// <param name="toY">The new position of the object in the grid</param> public void Move(IGridElt obj, float fromX, float fromY, float toX, float toY) { int oldCol = GridLocalToCellCol(fromX); int oldRow = GridLocalToCellRow(fromY); int newCol = GridLocalToCellCol(toX); int newRow = GridLocalToCellRow(toY); ref LooseGridRow row = ref rows[oldRow];
/// <summary> /// Inserts an object into a grid cell at the given index /// </summary> /// <param name="elt">The object to insert</param> /// <param name="xIdx">The column index of the cell</param> /// <param name="yIdx">The row index of the cell</param> private void InsertToCell(IGridElt elt, int xIdx, int yIdx) { LooseCell cell = rows[yIdx].cells[xIdx]; //if the cell is empty, initialize the bounds to match the element if (cell.FirstElt == null) { cell.Push(elt); cell.lft = (int)elt.Xf; cell.btm = (int)elt.Yf; cell.rgt = cell.lft + elt.Width; cell.top = cell.btm + elt.Height; //insert into the tight cells it overlaps InsertToCoarseGrid(cell); } else //otherwise, see if the bounds need to change to fit the element { cell.Push(elt); ExpandCell(cell, elt); } }
private void RemoveFromCell(IGridElt obj, int xIdx, int yIdx) { LooseCell cell = rows[yIdx].cells[xIdx]; IGridElt elt = cell.FirstElt; IGridElt prevElt = null; while (elt.ID != obj.ID) { prevElt = elt; elt = elt.NextElt; } if (prevElt == null) { cell.Pop(); } else { prevElt.NextElt = elt.NextElt; } }
public void LooseCell(IGridElt firstElt, AABB bounds) { GizmoHelp.DrawAABB(Color.yellow, bounds); }