private void RunHash(TimeStep step) { foreach (List <Body> list in hash.Values) { for (int index1 = 0; index1 < list.Count - 1; index1++) { Body body1 = list[index1]; for (int index2 = index1 + 1; index2 < list.Count; index2++) { Body body2 = list[index2]; if ((body1.Mass.MassInv != 0 || body2.Mass.MassInv != 0) && Body.CanCollide(body1, body2) && body1.Rectangle.Intersects(body2.Rectangle)) { long key = PairID.GetHash(body1.ID, body2.ID); if (!filter.ContainsKey(key)) { filter.Add(key, null); OnCollision(step, body1, body2); } } } } list.Clear(); } filter.Clear(); }
private void FillHash() { Scalar average = 0; for (int index = 0; index < Bodies.Count; index++) { Body body = this.Bodies[index]; if (!body.IsCollidable) { continue; } BoundingRectangle rect = body.Rectangle; average += Math.Max(rect.Max.X - rect.Min.X, rect.Max.Y - rect.Min.Y); int minX = (int)(rect.Min.X * cellSizeInv); int maxX = (int)(rect.Max.X * cellSizeInv) + 1; int minY = (int)(rect.Min.Y * cellSizeInv); int maxY = (int)(rect.Max.Y * cellSizeInv) + 1; for (int x = minX; x < maxX; x++) { for (int y = minY; y < maxY; y++) { long key = PairID.GetHash(x, y); List <Body> list; if (!hash.TryGetValue(key, out list)) { list = new List <Body>(); hash.Add(key, list); } list.Add(body); } } } if (autoAdjustCellSize) { CellSize = 2 * average / (Bodies.Count); } }