Looking for simple 2D AABB collision tutorial or code

I have tried to create my own 2D AABB collision for ages but i cannot get it to work correctly it has either flew of the map or clips and clings to the side of blocks. Does anyone have some simple collision with sliding?

The simplest form of collision response you can do is to check which edge of the aabb collided and remove all velocity/momentum in that direction.

if(top edge collided) velocityY = min(0,velocityY);
if(bottom edge collided) velocityY = max(0,velocityY);
if(right edge collided) velocityX = min(0,velocityX);
if(left edge collided) velocityX = max(0,velocityX);

This will allow sliding and will not cause 2 interpenetrating objects to get stuck together as they can still move in the opposite direction and either direction perpendicular to the collision. A great thing about aabbs though is that you can determine the exact collision time for two moving aabbs so you should be able to completely eliminate interpenetration with the right code and your code can be very elegant. But if you may decide to add any collision shapes (other than circles) or to allow anything to rotate then things get -really- complicated and you should then use a physics engine like box2d.

Post your physics code if you want help with it.

Please use the search bar (or google):

The linked code is only for stationary AABBs and only gets true/false for the collision so I do not think it will be of much use in this situation.

… Collision detection and “resolvsion” are two different things. First you need to detect the collision, and then you need to handle the collision. You cannot have one without the other. Of course it “returns true/false”, how else would you do it? Return an integer?

What do you even mean by stationary AABBs? That doesn’t make sense at all, the function does not care if the pixels on the screen are moving. Its just math. Math only knows numbers, not whether or not something is stationary.

The linked code was the first occurrence of the word detection in this thread. Yes to do any meaningful collision response you need more info than if a collision happened or not, that is why the linked code will not help him. You need to know which side of the AABB collided and optimally you would determine the exact time. To do this your AABBs will at least need to track their velocity, hence not being stationary.

Here is my issue, currently i am using a tile map and tailored velocity so they don’t cover anything past a cube. I have not used JBox2D because its a bit overkill for just a 2D platformer with just tiles and other entities. First i collect all the tiles intersected tiles from the velocity, next i then use a for loop to check each block by finding the depth of the intersection and then moving the shallow end out and then proceed on with the next block but i have found that this causes issues like clipping to the start of tile when landing and hooking onto walls. I just need a collision and resolver with or without a timestamp based collision.

Your character getting hung up or effected by the internal edges of a “brick wall” when it should have only collided with the outside edges is a problem inherent with a collision detection scheme that allow objects to interpenetrate and/or does not check which order the possible collisions would happen in. When you are dealing with more complex shapes sometime the schemes like that are the best you can come up with (box2d does allow interpenetration and does not perform well on a “brick wall” test). If you are stuck with this type of scheme you will use a hack to work around it like making your players collider a pill shape or something similar. Or combining the colliders of touching tiles in a “brick wall” so there is just one smooth outside edge and no internal edges.

Since you are dealing with AABBs only you have the luxury of doing it the right way, by not allowing interpenetration and ordering your collisions in the correct order. You can look up “swept AABB collision” or post your code if you want help implementing it.