/// <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 CollisionChecker(IAtlas atlas) { m_atlas = atlas; m_objectLayer = atlas.GetLayer(LayerType.Object) as SimpleObjectLayer; if (m_objectLayer != null) { m_physicalEntities = m_objectLayer.GetPhysicalEntities(); } else { throw new ArgumentException("ObjectLayer not found."); } }
public bool Add(GameActorPosition gameActorPosition, bool collidesWithObstacles = false) { if (collidesWithObstacles) { IObjectLayer gameObjectLayer = GetLayer(LayerType.Object) as IObjectLayer; Debug.Assert(gameObjectLayer != null, "gameObjectLayer != null"); Vector2 position = gameActorPosition.Position; GameActor actor = gameActorPosition.Actor; IPhysicalEntity physicalEntity; if (actor is IGameObject) { physicalEntity = (actor as IGameObject).PhysicalEntity; physicalEntity.Position = position; } else if (actor is Tile) { physicalEntity = (actor as Tile).GetPhysicalEntity(new Vector2I(gameActorPosition.Position)); } else { throw new ArgumentException("actor is unknown Type"); } bool anyCollision = gameObjectLayer.GetPhysicalEntities().Any(x => x.CollidesWith(physicalEntity)); if (anyCollision) { return(false); } bool anyObstacleOnPosition = GetObstaceLayers().Any(x => x.GetActorAt(new Vector2I(gameActorPosition.Position)) != null); if (anyObstacleOnPosition) { return(false); } } ILayer <GameActor> layer = GetLayer(gameActorPosition.Layer); return(layer.Add(gameActorPosition)); }