tile map collision detection

Hey guys!

I know there are A LOT of collision detection topics all around here and the internet, but I am really struggling to get my head around it even though there are so many resources (I’m not a very clever guy).

I think my main issue is that I’m not understanding the logic behind collision detection with tiles. From what I’ve read so far, it seems as though what I want to do is:
-get the players current position,
-check if the next position will be a solid block,
-move the player if it isn’t.

Is this understanding correct?
Sorry about another collision detection topic!

yeah, you’ve got the concept there. I see collision detection as just a check before you move the object.

Ok cool! :slight_smile: thank you.

Just another question regarding the same thing. When it comes to the players ‘bounding box’ how should i go about setting that up so that it works with the collision detection?
Should I just be testing collisions with the players position +/- his height or width?

I usually use Rectangles. But that would work if you want.

Depends on the shape of the character, and how you encode the position (e.g., whether you use the top-left corner, or the middle of the graphic, etc).

It is possible to also create a rectangle or shape and store it with the character, and use that for your collision tests. Many shapes have a contains() function.

Wow! so I can just apply a Rectangle() to my player, and use that for my collisions?
With my previous game all collisions were handled with Rectangle’s .intersects() method, but for obvious reasons this wont work with a platformer which requires collisions for specific sides of the player.
So then how would I separate the players left, right, top and bottom with a Rectangle?
This is so magical.

thanks for the help! It is amazing getting some sort of direction after weeks of trying to get this working!

Well what I do is to avoid having to loop through every single other tile, just get the tile that the player is currently located at and work with that. I also make getters for tiles around it like getNorthTile, east, west, south, and of course the tile the player is currently on.

[quote]So then how would I separate the players left, right, top and bottom with a Rectangle?
[/quote]
Sub-rectangles?

How about Polygon triangles that meet in the center but have a broad side on each relevant front? Just throwing out an idea, one that I haven’t seen used but seems like it should work.

I’d have to know more about your specific intended use to better understand the tradeoffs of the different possibilities.

You sort of have the right idea. As for collision I do a comparison between the location of all four sides. Like if the right side of block a is greater than the left side of block and the left side of block a is less than the right side of block b : there will be a collision on the x axis. But that just gives you x and it would collide anywhere on the y axis so you do something very similar to the y a is just with the top and bottom.

Then in the update method have something like if (!solid){x+= xvelocity; y+= yvelocity }; and if x+xvelocity intersects another x of a tile solid = true so the it dosent update

Hope this helps.

How to till what side there is a collision on using rectangles.

Get the intersection using the oh so convenient getIntersection method in Rectangle/Rectanle2D. This will give the rectangle that is the intersection of the 2 rectangles.

If the width is less then height, then it is a collision on either the right or left. See whether or not the x vel is positive or negative to find which side.

If the width is greater then height, then you have collision on either the top or bottom. Again, check y vel to see which one.

You can then add the intersection width or height to the player to move them up properly. This will not work for all cases (equal width and height or vel of 0) but is a good starting point.


Rectangle2D rect = this.getIntersection(block.entities.get(i));
				if(trect.getWidth() < rect.getHeight())
				{
					if(vel.x < 0)
					{
                   //blah blah
					}
					else
					{
						
						 //blah blah
					}
				}
				else
				{
					if(vel.y < 0)
					{
						
						 //blah blah
					}
					else
					{
						
						 //blah blah
					}
				}

You guys are absolute legends!
Thanks for all the useful help. I decided to go for Rectangles for my player and the tile map. From my understanding I can use the methods getMinX, getMaxY ect to my advantage for checking collisions, ie. if (player.getMinY < block.getMaxY) I can move.

Now I’m just stuck on figuring out which Rectangles in my tile map I’m testing against :S is this what getIntersection() is used to figure out? or should I use a function which returns the players current position in the tile map?

Sorry about all the questions! For some reason this is a tough concept for me to get my head around. Honestly you guys must be actual geniuses for understanding all this!