How would you locate collisions to a 2D map?

Hello there!

I’ve got a question. I’ve been making simple games with MonoGame and now I was wondering, how do I locate the places on my map where the collisions have to be? Or is there another place to assign the collisions and import the map afterward?

Those are multiple questions up here, but my main question is what the title says.

Regards,

DefiniteGoose

Build a collision layer that consists of Rectangle objects in code. You can then check for for collisions like such:

If (playerRectangle.Intersects(CollisionRectangle)) {
//do something
}

That’s a simple example. Really your collision layer should contain a list of all the collision rectangles for the map. You can then call a method that accepts the player rectangle and returns a list of rectangles that the player rectangle collides with.

Beyond that, you could actually create an object that contains the collision rectangle as well as other properties (perhaps type? wall, ground, ceiling, etc…). This will make it easy to determine what should happen for each collision type. Your collision layer would contain a list of these objects.

Hey and thanks for your clear explanation! This really helped me, but what I was looking for is, how do I know which position I should put a certain Rectangle, for example on a wall?

Can you try to elaborate on what you want to achieve? It’s not clear to me. :stuck_out_tongue:

I mean, you know what places you want the player to collide with, so that’s where you should put your rectangles.

Are we talking about one big map or a tile map? Because the big map is what I’m talking about. With this I mean how do I know where to put the rectangles? Or is a map that consists out of tiles more common for MonoGame?

The collision rectangles should have the same world coordinates as the floors, walls, etc… in your map. Think of it as an overlay.

Which method do you use to create a game map?

It doesn’t matter if you are using tiles or one large image. Either way, the collision rectangles should have the same world coordinates as the pixels/sections of your image(s) for the map floors, walls, celings, etc…

Work through this. It will help you:
http://xbox.create.msdn.com/en-US/education/catalog/tutorial/collision_2d_rectangle