Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ...
- Point direction = new Point(Math.Sign(velocity.X), Math.Sign(velocity.Y));
- Vector2 nextPosition = this.position + velocity;
- Int32 startx = (Int32)((Single)(position.X) / Tile.Size) - 1;
- Int32 endx = (Int32)((Single)(position.X + this.Width) / Tile.Size) + 1;
- Int32 starty = (Int32)((Single)(position.Y) / Tile.Size) - 1;
- Int32 endy = (Int32)((Single)(position.Y + this.Height) / Tile.Size) + 1;
- List<Point> horizontalWall = new List<Point>();
- List<Point> verticalWall = new List<Point>();
- if (direction.X == 1) // moving right
- {
- for (Int32 y = starty + 1; y < endy; y += 1)
- verticalWall.Add(new Point(endx, y));
- }
- else if (direction.X == -1) // moving left
- {
- for (Int32 y = starty + 1; y < endy; y += 1)
- verticalWall.Add(new Point(startx, y));
- }
- if (direction.Y == 1) // moving down
- {
- for (Int32 x = startx + 1; x < endx; x += 1)
- horizontalWall.Add(new Point(x, endy));
- }
- else if (direction.Y == -1) // moving up
- {
- for (Int32 x = startx + 1; x < endx; x += 1)
- horizontalWall.Add(new Point(x, starty));
- }
- foreach (Point point in verticalWall)
- if (realm.TileAt(point.X, point.Y).IsSolid)
- {
- // if our next position would collide with the tile, we fix velocity.
- if (nextPosition.X + this.Width > point.X * Tile.Size &&
- nextPosition.X < point.X * Tile.Size + Tile.Size)
- {
- if (direction.X == 1) // moving right
- {
- velocity.X = point.X * Tile.Size - (position.X + this.Width) - 0.0001f;
- }
- else if (direction.X == -1) // moving left
- {
- velocity.X = (point.X * Tile.Size + Tile.Size) - position.X;
- }
- }
- realm.Tiles[point.X, point.Y].Debug = true;
- }
- foreach (Point point in horizontalWall)
- if (realm.TileAt(point.X, point.Y).IsSolid)
- {
- // if our next position would collide with the tile, we fix velocity.
- if (nextPosition.Y + this.Height > point.Y * Tile.Size &&
- nextPosition.Y < point.Y * Tile.Size + Tile.Size)
- {
- if (direction.Y == 1) // moving down
- {
- velocity.Y = point.Y * Tile.Size - (position.Y + this.Height) - 0.0001f;
- }
- else if (direction.Y == -1) // moving up
- {
- velocity.Y = (point.Y * Tile.Size + Tile.Size) - position.Y;
- }
- }
- realm.Tiles[point.X, point.Y].Debug = true;
- }
- ...
Advertisement
Add Comment
Please, Sign In to add comment