I implemented some basic collision detection using AABBs and it mostly works, except for the fact that when I move down towards a collidable object, the player can pass through, but only when approaching the object after moving to the left. Here's a picture of what I mean:

If you can see, the player (the white square), is intersecting a tile. The player can only pass through a tile when there is no tiles under the player, and you are moving left and then down. Here's my collision detection code:

I construct a new Vector (tempPos) that is the center of the entity in question. Then I update the AABB with the center of the entity, and then I add either sx or sy, which is the movespeed, generally speaking this is usually set to 8, so I move 8 pixels every update, and the tiles are 32 pixels in size. Then I get the tile immediately to the wherever the player is moving using the x and y variables.

Then the collision works correctly, but now it doesn't detect collisions on the y axis at all. x and y are constrained between 1 and -1, as you can see in the keyboard update code. It's used to look up the tile where the player wants to move, so I don't know why only the x axis is working, and not the y.

Sorry if I didn't explain this well enough, ask me and I'll clarify anything!

So I figured out my problem was that when I move onto two tiles at once, I get the tile that I am standing on, but I actually round down so that the tile I get is actually the incorrect one. My question is, how do I get the tiles surrounding the player, and then check to see if any of them collide with the player? My player is one tile tall/wide so I only need to get the four tiles around him. But I think the way I am doing it right now is wrong because it's so hard to implement, and it's such an easy idea. Any help?

Since all tiles are of uniform size you can store all collidable tiles in a 2d array by their column and row. That would mean tile[8][5] would be column 8 and row 5 and their world position 8 * 32, 5 *32. Now if you divide the player center coordinates by it´s tile size you´ll get an approximate position in tile coordinates and then you can check against the surrounding tiles simply by looping those tiles.

Let´s say the player coordinates are y100, x100. Divide by 32 and you get column 3, row 3. Now you just need to check the tiles in column 2, 3, 4 and row 2, 3, 4 since those are the only possible tiles that can collide with the player.

But my main issue is that the move method does not work at all, and I have no idea why. Can anyone please just point me to a link or show me some code that selects the tiles around the player, and then checks to see if the player is colliding with them?

No offense, but that really doesn't help as you're using the Rectangle class, and I'm using my own AABB class. If I was using the Rectangle.intersects method, I would already be done. But I don't want to, I want to figure it out my own way.

The issue is most likely due to you only detecting the 4 neighbouring tiles. You should check all 8 around you. The way you described it sounds like your player's centre is too high(or too far left) for it to check the tile it is going through. Until it gets halfway through the tile, you are checking the empty tile next to it for collision.

I'd suggest checking the 8 tiles around the player, instead of just 4.

Correct, but I'm simply trying to get the four tiles around the player. Your method is far to advanced for what I need to do, and it's driving me crazy that no one can help me.

You should really check all 8 surrounding tiles like I and RawringNymNym pointed out. I don´t know if you read my earlier post but here´s one simple way: a double for-loop, getting the x/y tilecoordinates of the tiles and then check if there is an overlap. Here´s some psudocode I threw together:

If you´re having problems then try to simplify it so you just try out the core function. For example, forget about the color for now, just use a simple System.out.println("Collision") and what tilecoordinates are colliding. Another thing that can cause trouble is stuff like this: "(pos.getX() + (pos.getX() + 32)) / 2". Calcuations like has caused me a lot of headache, ´cause it´s so easy to slip in an error and they can be a pain to find.

The method getMapOverlap would return all the tiles overlapping the bounding box. You could easily change this to fit your code. If for example you have an array of ints just change the getMapOverlap() method.

As you can see in the second code, I add the xSpeed/ySpeed to the obj's x/y, and then I check if it bumps any obj, if it does, I subtract the xSpeed/ySpeed from the obj's x/y, if it doesn't bump anything, the method terminates.

I don't think this is a good idea for collision detection, but I don't know other way, so, I hope it helped you.

java-gaming.org is not responsible for the content posted by its members, including references to external websites,
and other references that may or may not have a relation with our primarily
gaming and game production oriented community.
inquiries and complaints can be sent via email to the info‑account of the
company managing the website of java‑gaming.org