-1
\$\begingroup\$

I finally found a tutorial on tile maps and loading them in from a file that I understand (http://circlessuck.blogspot.co.uk/2012/09/xna-tutorial-basic-tile-engine-part-2.html), however the tutorial doesn't show teach you how to do collision. I have vague idea in mind of what I might have to do from other tile map collisions I've seen, but I can't get it into code that will fit this style of loading tile maps. My tile map loadin:

private List<Texture2D> tiles;
    private int[,] tileMap;

    private const int tileWidth = 32;
    private const int tileHeight = 32;

    public TileMap(string name, ContentManager Content) 
    {
        LoadTiles(Content);
        LoadMapData(name);
    }

    public void Draw(SpriteBatch spriteBatch) 
    {
        for (int y = 0; y < tileMap.GetLength(0); y++)
        {
            for (int x = 0; x < tileMap.GetLength(1); x++)
            {
                spriteBatch.Draw(
                   tiles[tileMap[y, x]], new Vector2(x * tileWidth, y * tileHeight),
                   Color.White);
            }
        }
    }

    private void LoadTiles(ContentManager Content) 
    {
        tiles = new List<Texture2D>();
        tiles.Add(Content.Load<Texture2D>("sky"));
        tiles.Add(Content.Load<Texture2D>("grass"));
        tiles.Add(Content.Load<Texture2D>("dirt"));
    }



    private void LoadMapData(string name) 
    {
        string path = "Maps/" + name + ".txt";

        int width = 0;
        int height = File.ReadLines(path).Count();

        StreamReader sReader = new StreamReader(path);
        string line = sReader.ReadLine();
        string[] tileNo = line.Split(',');

        width = tileNo.Count();
        tileMap = new int[height, width];
        sReader.Close();


        sReader = new StreamReader(path);

        for (int y = 0; y < height; y++) 
        {
            line = sReader.ReadLine();
            tileNo = line.Split(',');

            for (int x = 0; x < width; x++) 
            {
                tileMap[y, x] = Convert.ToInt32(tileNo[x]);
            }
        }

        sReader.Close();
    }

(I have a player class with basic physics that all work fine)

\$\endgroup\$
  • \$\begingroup\$ There's no question here. If you're more specific in what you want, you're more likely to get an answer that suits you. \$\endgroup\$ – MichaelHouse Aug 18 '13 at 14:04
  • \$\begingroup\$ tonypa.pri.ee/tbw/tut05.html - Great tutorial on tile-map collision. Its flash but the code is easily made workable in C# \$\endgroup\$ – dan369 Aug 18 '13 at 14:06
  • \$\begingroup\$ Mark tiles that should collide with a flag/bool. Check tiles in vicinity of player if there respective triangles intersect. \$\endgroup\$ – Madmenyo Aug 23 '13 at 12:59
  • \$\begingroup\$ Check out this link, I suspect it will be helpful : gamedev.stackexchange.com/questions/84208/… \$\endgroup\$ – Shroeder Oct 2 '14 at 16:34
0
\$\begingroup\$

You will either need a another flag to decide if the tile is solid or not, or you will need to decide this based on the type of tile / texture. For the first option, you'll need to include a piece of data in the txt that tells you if the tile is solid or not. Just a second digit could work in this case. Other, decide based on the tile type. So, if you get grass, that may not be solid, if you get a rock, for example, that might be solid.

Another option could be to include another txt file that maps digit to digit with the map txt file, except you have 0 or 1 for each tile, indicating what is solid and what is not.

If you mean how do you actually implement collision, then look into the "Rectangle" class, and specifically the .Intersect method.

\$\endgroup\$
  • \$\begingroup\$ Well in my map file, I have the digits 0, 1 & 2 and I want the digit 1 to be the one that player collides with. But wouldn't I need to somehow get the digit 1 and assign it to a Rectangle variable so I can collide with it? If so I would have no idea how to do that \$\endgroup\$ – Dan Scott Aug 18 '13 at 15:41
  • \$\begingroup\$ Well, right now your map is just a int[,]. This means you have no map data besides what the tile is. What I would recommend is having either another two-dimensional array, this time of rectangles that covers the whole map, or make a tile struct / class and give it both a type and a rectangle member. Now, based on this you just have to check if the player rectangle intersects the tile rectangle, and if that tile is a solid type. \$\endgroup\$ – Ben Aug 19 '13 at 6:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.