// return a list of all potential collision you may be going through public static CollisionInfo[] BroadPhase(Entity owner, Box2D sweep) { m_cachedList.Clear(); // first, increase the sweep box so that it covers all the tile it is touching Box2D fullCover = sweep; fullCover.Left = Mathf.Floor(fullCover.Left); fullCover.Right = Mathf.Ceil(fullCover.Right); fullCover.Top = Mathf.Ceil(fullCover.Top); fullCover.Bottom = Mathf.Floor(fullCover.Bottom); // 0.9 is not a typo, it's just using a large epsilon which is currently fine because our sweep box is currently always at least one tile large DebugUtils.Assert(fullCover.Width >= 0.99); DebugUtils.Assert(fullCover.Height >= 0.99); if (Input.GetKeyDown(KeyCode.F10)) { DrawCollision = !DrawCollision; } // check against world for (float i = fullCover.Left; i + 0.9f < fullCover.Right; i += 1.0f) { for (float j = fullCover.Bottom; j + 0.9f < fullCover.Top; j += 1.0f) { if (DrawCollision) { DebugUtils.DrawRect(new Vector2(i, j), new Vector2(i + 1.0f, j + 1.0f), Color.blue); } Vector2 checkVector = new Vector2(i + 0.5f, j + 0.5f); TileInfo ti = GameManager.Instance.GetTileFromWorldPos(checkVector); ETile tile = ti.Tile; if (WorldTile.IsCollision(tile)) { Box2D box2d = new Box2D(i, j + 1.0f, i + 1.0f, j); m_cachedList.Add(new CollisionInfo(null, box2d, CollisionFlags.Wall)); } // check entity as well // TODO: This does a double lookup from above List <Entity> entities = GameManager.Instance.GetEntitiesFromWorldPos(checkVector); if (entities == null) { continue; } foreach (Entity entity in entities) { // pair-wise check must be done once only to avoid obtaining multiple collisions if (entity.Id <= owner.Id) { continue; } Box2D box2d = entity.Box; if (entity is ItemInstance) { m_cachedList.Add(new CollisionInfo(entity, box2d, CollisionFlags.Item)); } else { m_cachedList.Add(new CollisionInfo(entity, box2d, CollisionFlags.None)); } } } } // let's draw all collisions if (DrawCollision) { foreach (var ci in m_cachedList) { ci.Box.Draw(Color.white); } } return(m_cachedList.ToArray()); }