# How Would I Assign Collisions To Tiled Map?

Hi everyone,

I’m relatively new to Monogame Extended. So lets say I have a tilemap ready. I sort of understand the logic of assigning collisions to each tile; you call every tile and assign certain ones to return a boolean value.

What I’m unsure of is how I exactly do the above (or if theres a better way). I’m not sure what Extended functions/commands to use and also the most efficient way of doing so. I am aware there are a few guides on doing this but they all are made in a different language or are a bit confusing.

Sorry for the dumb question but could someone give me a basis of what I should be doing or aiming for because I am quite lost at the moment.

I’m ready to do this too, having come from using Cocos2d, which has a class for working with Tiled maps. I saw how it was done in the Platformer demo, which is basically he added static bodies to each tile that had a certain property, then made a resolve collisions function, which would check collisions between static and dynamic bodies, that he passed in the player’s dynamic body in as a parameter.

What would be nice for me is to have the Tiled class return a tile and its properties by either passing in a row and column, or a Point, like a player position.

Even cooler would be a list of tiles/properties if I pass in a rectangle. This would give me all the tiles that are touching a player at a given frame.

I’m happy with the Tiled class so far, but I’m curious to see how other people are handling collisions without these helpful features.

To get the tile the players is on
int TileX = (int)(player.position.x / tileWidth);
int TileY = (int)(player.position.y / tileWidth);

var tile = TileGrid[TileX ,TileY];

if (tile.HasCollision())
{

3 Likes

Nonsense! That’s too simple. Oh, wait. It works. Hey, thanks, man!

Sorry to bother you, but how did you get your code to work? I believe we were both looking into a similar problem originally and if you have solved it, could you send me some more details as to how you solved it.

Would you mind explaining more what the code does. Why is the y value divided by width? I’m guessing this is sort of a pseudocode representation, what would the actual commands look like, I’m really confused about what the commands are for MonoGame Extended and which ones to use for this problem.

Thanks

I guess he uses width again because the tiles are square, width = height, and he was a little lazy

What helped me with movement restriction based on tiles is to look into if you can move BEFORE you move.
So you are at x=3 and Y = 3, and you plan to move downward, so you want to do y+1. Now first have a “CanThisMovementWork(x,y)”, and when that returns true, then do the actual movement. How to do it with Monogame extended would be intereseting to see too, but i fear monogame extended is dead. There has never been a documentation.

I know nothing about MonoGame Extended. Its just general code you can use for any grid based collisions.

So what you’re saying is that MonoGame Extended might not be the best method for implementing this? If so, what are some other methods to add maps with tiles to your game that are easier to understand especially for newer users? I dont mind if it takes a while to make but whare are some good ways?

I had a quick look at the source code. It seems like a pretty messy project, would not suggest to use it as a beginner.

Tiles are pretty easy to design, its just squares in a 2d grid.

1. Figure out how to render a square image on the screen.
2. Try render several squares in a row with a loop, add to position x with each loop.
3. Do the same in columns with a second loop, add to position y with each loop.

You got to learn to split your problem down into small steps - don’t do all at once and test often.

1 Like

Take a gander at the source code. If you are looking for tile collisions, an easy solution is just to have an array of collision information corresponding to the tiles.

e.g.

``````private TileCollisionType[] _tileCollisions

...

public enum TileCollisionType : byte
{
Passable,
Blocked
}

...

private bool CanMoveTo(int x, int y)
{
if (_tileCollisions[x + y * mapWidth] == TileCollisionType.Blocked) return false;
return true;
}
``````

How you fill the collision information is up to you. You could build it from the graphical data of the tiles, such as this building graphic is always “blocked”. Or you could make it from using a particular Tile layer which uses a unique tileset to represent the collision information. Of course, in that case, the tile layer is never meant to be rendered in-game; it’s just for encoding the collision information.

2 Likes

This is a very good reply! Thats the most performance efficient approach i know. okay they already have that? Good to know!

Its again this question: going through their sourcecode, finding out if they have something, find out how to use it or implement it yourselve from the beginning. Documentation please

Thanks to everyone for their contributions.

I might take @ByteCaptain’s and @vikingfabian’s word and try to manually implement this. Not that I have hatred against MonoGame Extended, its more that I’d rather understand the code than have me copy someone else’s code for MonoGame Extended after being frustrated at the lack of user friendly documentation. Thanks again.

MonoGame.Extended is not dead. It’s actually more active than it has ever been

The real problem with Extended is that it has become quite a big project and there’s only a few people who work on it. I started the project and I still work on it as often as I can. Unfortunately, even when I dedicate all of my spare time to it, there’s still a limit to how much I much can get done. There’s always loads of things on the to-do list (documentation included).

I completely understand that the lack of documentation can be frustrating. I would love to be able to wave my magic wand and solve this problem. One day I might get around to it.

The good news is that Extended is completely free and open source. If you want to make your own fork of the project you may. If you want to copy and paste code out of it go ahead. You’re welcome to do pretty much anything you like if it helps you make your game. You have my permission to borrow anything that makes your life easier.

2 Likes

I opted for a manual implementation since it’s convenient for me as I use an orthogonal map with only rectangular shapes as collisions, with separate maps for each level.

I have a dedicated collision layer, called Collision, and a wrapper class called CollisionObject. Each level has a list of CollisionObjects; I can modify this list to add/remove collision at will. One of the constructors takes in a TiledMapObject, which allows me to easily create collision from the Tiled map data.