/// <summary> /// Search for all objects that are or can be in collision with each other. /// Must be called when step was already performed, so collisions are already there. /// If there is no collision, returns empty list. /// </summary> /// <returns>List of list of all potentially colliding objects.</returns> public List<List<IPhysicalEntity>> CollisionGroups() { List<HashSet<IPhysicalEntity>> listOfSets = new List<HashSet<IPhysicalEntity>>(); foreach (IPhysicalEntity physicalEntity in m_physicalEntities) { if (Collides(physicalEntity)) { var circle = new Circle(physicalEntity.Position, 2 * MaximumGameObjectRadius + MaximumGameObjectSpeed); var physicalEntities = new HashSet<IPhysicalEntity>(m_objectLayer.GetPhysicalEntities(circle)); listOfSets.Add(physicalEntities); } } // consolidation for (int i = 0; i < listOfSets.Count - 1; i++) { HashSet<IPhysicalEntity> firstList = listOfSets[i]; for (int j = i + 1; j < listOfSets.Count; j++) { HashSet<IPhysicalEntity> secondList = listOfSets[j]; if (firstList.Overlaps(secondList)) { firstList.UnionWith(secondList); listOfSets.RemoveAt(j); j--; } } } // To List List<List<IPhysicalEntity>> collisionGroups = new List<List<IPhysicalEntity>>(); foreach (HashSet<IPhysicalEntity> hashSet in listOfSets) { collisionGroups.Add(hashSet.ToList()); } return collisionGroups; }
public bool CollidesWithPhysicalEntity(IPhysicalEntity physicalEntity) { var circle = new Circle(physicalEntity.Position, 2 * MaximumGameObjectRadius); List<IPhysicalEntity> physicalEntities = m_objectLayer.GetPhysicalEntities(circle); return physicalEntities.Any(physicalEntity.CollidesWith); }