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"); } }
public void Initialize(int width, int height, int typesOfTrash) { Width = width; Height = height; TypesOfTrash = typesOfTrash; float tileWidth = (float)this.Collision.Region().Width / width; float tileHeight = (float)this.Collision.Region().Height / height; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { Vector2 position = new Vector2(this.Collision.Position.X + tileWidth * x, this.Collision.Position.Y + tileHeight * y); Vector2 size = new Vector2(tileWidth, tileHeight); RectangleBody collision = new RectangleBody(position, size); Trash newTrash = new Trash(this.Game, collision, rand.Next() % typesOfTrash); TrashTiles.Add(newTrash); } } RectangleBody rect = new RectangleBody(new Vector2(0, 0), new Vector2(tileWidth * Config.SELECTION_WIDTH, tileHeight * Config.SELECTION_HEIGHT)); Selection = new Selection(Game, rect); ScrambleBoard(); }
protected override void Initialize() { SpriteMapper mapper = new SpriteMapper(); _spriteSheet = mapper.ReadFile(Config.SHEET_CONFIG_FILE_NAME, Content); _trashSheet = mapper.ReadFile(Config.TRASH_SHEET_CONFIG_FILE_NAME, Content); _graphics.PreferredBackBufferWidth = Config.VIEWPORT_WIDTH; _graphics.PreferredBackBufferHeight = Config.VIEWPORT_HEIGHT; _graphics.ApplyChanges(); //RectangleBody collision = new RectangleBody(new Vector2(100, 100), new Vector2(200, 200)); //testObj = new Object(this, collision); //Config.GRID_INFO gridInfo = new Config.GRID_INFO(50, 25, 0f, 0f, 1.0f, 1.0f); //testGrid = new Grid(gridInfo); grid = new Grid(Config.GAME_GRID); RectangleBody size = new RectangleBody(Config.GAME_GRID.Rect); board = new Board(this, size); board.Initialize(Config.GAME_GRID.TilesWide, Config.GAME_GRID.TilesHigh, 16); _overviewSheet = mapper.ReadFile(Config.OVERVIEW_CONFIG_FILE_NAME, Content); RectangleBody fullSize = new RectangleBody(new Vector2(0, 0), new Vector2(Config.VIEWPORT_WIDTH, Config.VIEWPORT_HEIGHT)); overview = new Object(this, fullSize); base.Initialize(); }
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); }
public bool CollidesWith(Vector2 vector) { RectangleBody rect = new RectangleBody(vector, new Vector2(1, 1)); return(CollidesWith(rect)); }