/// <summary> /// Check the Segment Collision and Remove colliding Cells with there later Generations /// </summary> /// <param name="_cellsToCheck">The List of Cells to Check the Collision with</param> /// <returns></returns> public HashSet <int> CheckCollision(List <Cell> _cellsToCheck) { List <Cell> PolyPolyCheckList = new List <Cell>(); HashSet <int> StreetIDsToRecreate = new HashSet <int>(); //Sphere Sphere Collsion | fast but not precies | Save the Cells that can possible collide for a more precies check for (int i = 0; i < _cellsToCheck.Count; i++) { if (!_cellsToCheck[i].IsBlocked && MyCollision.SphereSphere(m_Center, m_CollisionRadius, _cellsToCheck[i].m_PosCenter, _cellsToCheck[i].m_Radius)) { PolyPolyCheckList.Add(_cellsToCheck[i]); } } //Poly Poly Collision | slow but more precies for (int i = 0; i < PolyPolyCheckList.Count; i++) { if (!PolyPolyCheckList[i].IsBlocked && MyCollision.PolyPoly(PolyPolyCheckList[i].m_Corner, m_CornerPos)) { PolyPolyCheckList[i].Delete(); //Delete the Colliding Cell and the later Generations int id = PolyPolyCheckList[i].m_Street.ID; StreetIDsToRecreate.Add(id); //Saves the Street ID to recreate the Grid Mesh } } return(StreetIDsToRecreate); }
/// <summary> /// Check für Collider with a OverlapSphere /// </summary> /// <returns>return true if another Cell from a diffrent Street ID collid</returns> public bool CheckForCollision() { List<Cell> cellToCheck = new List<Cell>(); List<StreetSegment> segToCheck = new List<StreetSegment>(); List<Cross> crossToCheck = new List<Cross>(); //Sphere Sphere //Cell Cell foreach (Cell c in GridManager.m_AllCells) if (c.ID != this.ID && MyCollision.SphereSphere(this.m_PosCenter, this.m_Radius, c.m_PosCenter, c.m_Radius)) cellToCheck.Add(c); List<StreetComponent> allComponetns = StreetComponentManager.GetAllStreetComponents(); //Cell Segment for (int i = 0; i < allComponetns.Count; i++) { StreetComponent comp = allComponetns[i]; if (comp.ID == this.ID) continue; if (comp is Street) { Street s = (Street)comp; foreach (StreetSegment seg in s.m_Segments) if (MyCollision.SphereSphere(this.m_PosCenter, this.m_Radius, seg.m_Center, seg.m_CollisionRadius)) segToCheck.Add(seg); } else if (comp is Cross) { Cross c = (Cross)comp; if (MyCollision.SphereSphere(this.m_PosCenter, this.m_Radius, c.m_center, 1.7f)) crossToCheck.Add(c); } } //Poly Poly //Cell Cross foreach (Cross c in crossToCheck) if (MyCollision.PolyPoly(this.m_Corner, c.m_corners)) return true; //Cell Segment foreach (StreetSegment seg in segToCheck) if (MyCollision.PolyPoly(this.m_Corner, seg.m_CornerPos)) return true; //Cell Cell foreach (Cell c in cellToCheck) if (MyCollision.PolyPoly(this.m_Corner, c.m_Corner)) return true; return false; }
public void CheckGridCollision() { m_center = new Vector2(transform.position.x, transform.position.z); m_corners[0] = m_center + new Vector2(1.2f, 1.2f); m_corners[1] = m_center + new Vector2(-1.2f, 1.2f); m_corners[2] = m_center + new Vector2(-1.2f, -1.2f); m_corners[3] = m_center + new Vector2(1.2f, -1.2f); HashSet <int> StreetsToRecreate = new HashSet <int>(); List <Cell> PolyPolyCheckList = new List <Cell>(); List <Cell> CellsToCheck = GridManager.m_AllCells; //Sphere Sphere Coll for (int i = 0; i < CellsToCheck.Count; i++) { if (MyCollision.SphereSphere(m_center, 1.7f, CellsToCheck[i].m_PosCenter, CellsToCheck[i].m_Radius)) { PolyPolyCheckList.Add(CellsToCheck[i]); } } //Poly Poly Coll for (int i = 0; i < PolyPolyCheckList.Count; i++) { if (MyCollision.PolyPoly(PolyPolyCheckList[i].m_Corner, m_corners)) { PolyPolyCheckList[i].Delete(); int id = PolyPolyCheckList[i].m_Street.ID; StreetsToRecreate.Add(id); } } //Recreate Grid foreach (int i in StreetsToRecreate) { Street s = StreetComponentManager.GetStreetByID(i); GridManager.RemoveGridMesh(s); MeshGenerator.CreateGridMesh(s, s.m_GridObj.GetComponent <MeshFilter>(), s.m_GridRenderer); } }