title: XNA Isometric tile collision
body: <p>Im a C#/XNA student and I've recently been working on an isometric tile engine and so far it works fairly well. But im having problem trying to figure out on how to do collision, this is what my tile engine does at the moment:</p>
  
  <ul>
  <li><p>Draws the world from an image and place a tile depending on what color is on my image. For instance color red would draw a grass tile. (Tiles are 64x32)</p></li>
  <li><p>Camera following player, and my draw loop only draws what the camera sees.</p></li>
  </ul>
  
  <p>This is how my game looks if that would be of any help: 
  <img src="https://i.stack.imgur.com/ApRlZ.png" alt="enter image description here"></p>
  
  <p>I don't know what sort of collision would work best. Should i do collision points, or intersects or any other sort of collision. I've read somewhere that you could do Worldtoscreen/Screentoworld but im far to inexperienced and don't know how that works nor how the code would look like.</p>
  
  <p>Here is my code drawing tiles etc:</p>
  
  <pre><code>class MapRow
      {
          public List&lt;MapCell&gt; Columns = new List&lt;MapCell&gt;();
      }
  
  
  
      class TileMap
      {
          public List&lt;MapRow&gt; Rows = new List&lt;MapRow&gt;();
  
          public static Texture2D image;
          Texture2D tileset;
  
  
          TileInfo[,] tileMap;
  
  
          Color[] pixelColor;
  
  
          public TileMap(string TextureImage, string Tileset)
          {
              tileset = Game1.Instance.Content.Load&lt;Texture2D&gt;(Tileset);
  
              image = Game1.Instance.Content.Load&lt;Texture2D&gt;(TextureImage);
              pixelColor = new Color[image.Width * image.Height]; // pixelColor array that is holding all pixel in the image
              image.GetData&lt;Color&gt;(pixelColor); // Save all the pixels in image to the array pixelColor
  
              tileMap = new TileInfo[image.Height, image.Width];
  
              int counter = 0;
              for (int y = 0; y &lt; image.Height; y++)
              {
                  MapRow thisRow = new MapRow();
                  for (int x = 0; x &lt; image.Width; x++)
                  {
                      tileMap[y, x] = new TileInfo();
  
  
                      if (pixelColor[counter] == new Color(0, 166, 81))
                      {
                          tileMap[y, x].cellValue = 1;//grass
  
                      }
                      if (pixelColor[counter] == new Color(0, 74, 128))
                      {
  
                          tileMap[y, x].cellValue = 2;//water
                      }
  
                      if (pixelColor[counter] == new Color(255, 255, 0))
                      {
                          tileMap[y, x].cellValue = 3;//Sand
                      }
  
  
                      tileMap[y, x].LoadInfoFromCellValue();//determine what tile it should draw depending on cellvalue
                      thisRow.Columns.Add(new MapCell(tileMap[y, x]));
  
  
                      counter++;
                  }
                  Rows.Add(thisRow);
              }
          }
  
  
          public static int printx;
          public static int printy;
  
          public static int squaresAcross = Settings.screen.X / Tile.TileWidth;
          public static int squaresDown = Settings.screen.Y / Tile.TileHeight;
  
          int baseOffsetX = -32;
          int baseOffsetY = -64;
  
              public void draw(SpriteBatch spriteBatch)
          {
              printx = (int)Camera.Location.X / Tile.TileWidth;
              printy = (int)Camera.Location.Y / Tile.TileHeight;
  
              squaresAcross = (int)Camera.Location.X / Tile.TileWidth + Settings.screen.X / Tile.TileWidth;
              squaresDown = 2*(int)Camera.Location.Y / Tile.TileHeight + Settings.screen.Y / Tile.TileHeight + 7;
  
  
              for (printy = (int)Camera.Location.Y / Tile.TileHeight; printy &lt; squaresDown; printy++)
              {
                  int rowOffset = 0;
                  if ((printy) % 2 == 1)
                      rowOffset = Tile.OddRowXOffset;
  
                  for (printx = (int)Camera.Location.X / Tile.TileWidth; printx &lt; squaresAcross; printx++)
                  {
                      if (tileMap[printy, printx].Collides(MouseCursor.mousePosition))
                          Console.WriteLine(tileMap[printy, printx].tileRect);
  
                      foreach (TileInfo tileID in Rows[printy].Columns[printx].BaseTiles)
                      {
  
                          spriteBatch.Draw(
  
                              tileset,
                              tileMap[printy, printx].tileRect = new Rectangle(
                                  (printx * Tile.TileStepX) + rowOffset + baseOffsetX,
                                  (printy * Tile.TileStepY) + baseOffsetY,
                                  Tile.TileWidth, Tile.TileHeight),
                              Tile.GetSourceRectangle(tileID.cellValue),
                              Color.White,
                              0.0f,
                              Vector2.Zero,
                              SpriteEffects.None,
                              tileID.drawDepth);
  
  
  
  
  
                      }
  
  
  
                  }
  
              }
  
              }
  
          }
  </code></pre>