public bool CollidesWith(CollisionBody other) { if (this.Shape == ShapeType.Circle && other.Shape == ShapeType.Circle) { CircleBody a = (CircleBody)this; CircleBody b = (CircleBody)other; return(Vector2.Distance(this.Position, other.Position) <= a.Radius + b.Radius); } else if (this.Shape == ShapeType.Circle && other.Shape == ShapeType.Rectangle) { return(RectCircCollision((CircleBody)this, (RectangleBody)other)); } else if (this.Shape == ShapeType.Rectangle && other.Shape == ShapeType.Circle) { return(RectCircCollision((CircleBody)other, (RectangleBody)this)); } else if (this.Shape == ShapeType.Rectangle && other.Shape == ShapeType.Rectangle) { RectangleBody a = (RectangleBody)this; RectangleBody b = (RectangleBody)other; return((a.Position.X <= b.Position.X + b.Size.X && a.Position.X + a.Size.X >= b.Position.X) && (a.Position.Y <= b.Position.Y + b.Size.Y && a.Position.Y + a.Size.Y >= b.Position.Y)); } else { throw new NotImplementedException("Attempted collision with a shape that doesn't exist"); } }
private bool RectCircCollision(CircleBody a, RectangleBody b) { float nearestX = Clamp(a.Position.X, b.Position.X, b.Position.X + b.Size.X); float nearestY = Clamp(a.Position.Y, b.Position.Y, b.Position.Y + b.Size.Y); Vector2 nearestPoint = new Vector2(nearestX, nearestY); return(Vector2.Distance(nearestPoint, a.Position) <= a.Radius); }
public Dictionary <uint, CollisionBody> GetPotentialCollisions(CollisionBody toCheck) { int xMin = 0; int xMax = 0; int yMin = 0; int yMax = 0; if (toCheck.Shape == CollisionBody.ShapeType.Circle) { CircleBody body = (CircleBody)toCheck; xMin = ((int)(Math.Max(body.Position.X - body.Radius / 2, 0)) / tileSize); xMax = (int)(Math.Min(body.Position.X + body.Radius / 2, maxWidth - 1)) / tileSize; yMin = ((int)(Math.Max(body.Position.Y - body.Radius / 2, 0)) / tileSize); yMax = (int)(Math.Min(body.Position.Y + body.Radius / 2, maxHeight - 1)) / tileSize; } else if (toCheck.Shape == CollisionBody.ShapeType.Rectangle) { RectangleBody body = (RectangleBody)toCheck; xMin = ((int)(Math.Max(body.Position.X, 0)) / tileSize); xMax = (int)(Math.Min(body.Position.X + body.Size.X, 49)) / tileSize; yMin = ((int)(Math.Max(body.Position.Y, 0)) / tileSize); yMax = (int)(Math.Min(body.Position.Y + body.Size.Y, 49)) / tileSize; } // Get every possible CollisionBody Dictionary <uint, CollisionBody> potentials = new Dictionary <uint, CollisionBody>(); for (int y = yMin; y <= yMax; y++) { for (int x = xMin; x <= xMax; x++) { foreach (CollisionBody c in Tiles[y * columns + x].Data.Values) { if (!potentials.ContainsKey(c.id)) { potentials.Add(c.id, c); } } } } return(potentials); }
private List <PhysicsGridTile> GetCoveredTiles(CollisionBody toAdd) { int xMin = 0; int xMax = 0; int yMin = 0; int yMax = 0; if (toAdd.Shape == CollisionBody.ShapeType.Circle) { CircleBody body = (CircleBody)toAdd; xMin = ((int)(Math.Max(body.Position.X - body.Radius / 2, 0)) / tileSize); xMax = (int)(Math.Min(body.Position.X + body.Radius / 2, maxWidth - 1)) / tileSize; yMin = ((int)(Math.Max(body.Position.Y - body.Radius / 2, 0)) / tileSize); yMax = (int)(Math.Min(body.Position.Y + body.Radius / 2, maxHeight - 1)) / tileSize; } else if (toAdd.Shape == CollisionBody.ShapeType.Rectangle) { RectangleBody body = (RectangleBody)toAdd; xMin = ((int)(Math.Max(body.Position.X, 0)) / tileSize); xMax = (int)(Math.Min(body.Position.X + body.Size.X, maxWidth - 1)) / tileSize; yMin = ((int)(Math.Max(body.Position.Y, 0)) / tileSize); yMax = (int)(Math.Min(body.Position.Y + body.Size.Y, maxWidth - 1)) / tileSize; } List <PhysicsGridTile> toReturn = new List <PhysicsGridTile>(); for (int y = yMin; y <= yMax; y++) { for (int x = xMin; x <= xMax; x++) { toReturn.Add(Tiles[y * columns + x]); } } return(toReturn); }