Guest User

Untitled

a guest
Sep 7th, 2011
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.73 KB | None | 0 0
  1. ...
  2.     Point direction = new Point(Math.Sign(velocity.X), Math.Sign(velocity.Y));
  3.     Vector2 nextPosition = this.position + velocity;
  4.  
  5.     Int32 startx = (Int32)((Single)(position.X) / Tile.Size) - 1;
  6.     Int32 endx = (Int32)((Single)(position.X + this.Width) / Tile.Size) + 1;
  7.     Int32 starty = (Int32)((Single)(position.Y) / Tile.Size) - 1;
  8.     Int32 endy = (Int32)((Single)(position.Y + this.Height) / Tile.Size) + 1;
  9.  
  10.     List<Point> horizontalWall = new List<Point>();
  11.     List<Point> verticalWall = new List<Point>();
  12.  
  13.     if (direction.X == 1) // moving right
  14.     {
  15.         for (Int32 y = starty + 1; y < endy; y += 1)
  16.             verticalWall.Add(new Point(endx, y));
  17.     }
  18.     else if (direction.X == -1) // moving left
  19.     {
  20.         for (Int32 y = starty + 1; y < endy; y += 1)
  21.             verticalWall.Add(new Point(startx, y));
  22.     }
  23.  
  24.     if (direction.Y == 1) // moving down
  25.     {
  26.         for (Int32 x = startx + 1; x < endx; x += 1)
  27.             horizontalWall.Add(new Point(x, endy));
  28.     }
  29.     else if (direction.Y == -1) // moving up
  30.     {
  31.         for (Int32 x = startx + 1; x < endx; x += 1)
  32.             horizontalWall.Add(new Point(x, starty));
  33.     }
  34.  
  35.     foreach (Point point in verticalWall)
  36.         if (realm.TileAt(point.X, point.Y).IsSolid)
  37.         {
  38.             // if our next position would collide with the tile, we fix velocity.
  39.             if (nextPosition.X + this.Width > point.X * Tile.Size &&
  40.                 nextPosition.X < point.X * Tile.Size + Tile.Size)
  41.             {
  42.                 if (direction.X == 1) // moving right
  43.                 {
  44.                     velocity.X = point.X * Tile.Size - (position.X + this.Width) - 0.0001f;
  45.                 }
  46.                 else if (direction.X == -1) // moving left
  47.                 {
  48.                     velocity.X = (point.X * Tile.Size + Tile.Size) - position.X;
  49.                 }
  50.             }
  51.             realm.Tiles[point.X, point.Y].Debug = true;
  52.         }
  53.  
  54.     foreach (Point point in horizontalWall)
  55.         if (realm.TileAt(point.X, point.Y).IsSolid)
  56.         {
  57.             // if our next position would collide with the tile, we fix velocity.
  58.             if (nextPosition.Y + this.Height > point.Y * Tile.Size &&
  59.                 nextPosition.Y < point.Y * Tile.Size + Tile.Size)
  60.             {
  61.                 if (direction.Y == 1) // moving down
  62.                 {
  63.                     velocity.Y = point.Y * Tile.Size - (position.Y + this.Height) - 0.0001f;
  64.                 }
  65.                 else if (direction.Y == -1) // moving up
  66.                 {
  67.                     velocity.Y = (point.Y * Tile.Size + Tile.Size) - position.Y;
  68.                 }
  69.             }
  70.             realm.Tiles[point.X, point.Y].Debug = true;
  71.         }
  72. ...
Advertisement
Add Comment
Please, Sign In to add comment