I'm having a problem not with the collision detection itself, but with the check order loop.
For the detection of the collisions I'm using AABB.
Visual representation for reference:
https://i.imgur.com/7SaeUjX.png
Scenario 1:
- I have three square objects; [player], [enemy1] and [enemy2].
- [enemy1] and [enemy2] are close to each other horizontally and the [player] is above [enemy1].
- The player then starts moving with DownArrow + RightArrow constantly.
- So now, the [player] is over [enemy1] and [enemy 2] going right.
- Because the first element to be checked is [enemy1], the collision system corrects the y position of the [player] and because the right is not considered to be colliding, it can go right.
- RESULT: all good. The [player] can keep going on to the right without problem.
Scenario 2:
- I have three square objects; player, enemy1 and enemy2.
- [enemy1] and [enemy2] are close to each other horizontally and the [player] is above [enemy2].
- The player then starts moving with DownArrow + LeftArrow constantly.
- So now, the [player] is over [enemy2] and [enemy 1] going left.
- Because the first element to be checked is [enemy1], the collision system corrects the y position, and the x position of the [player] and, of course, [enemy2] collision aren't important at this point.
- RESULT: of course, [player] gets blocked by [enemy1] and can not keep going left.
Explanation of the problem that I'm having in Scenario 2:
The problem lies in the check order of the collisions. The loop starts checking if the player cololides with [enemy1] directly because is the first to be checked, so the [player], of course, detects that is colliding with [enemy1] and solves the 2 collisions giving the bad result.
If you change the order of the list, starting from [enemy2], of course the problem comes in the other way around.
What should I do to avoid this situation?
Ok, after looking and thinking for a solution, I ended up with this:
My collider manager now works this way (3 steps):
- Finds the overlaps
- Colects the info of the collision
- Sorts the collisions by overlaping area (collected in the info) and resolutes
So, the main difference now, is that I first analyze all the overlaps/collisions, and after that I treat them, in this case sorting by overlaping area
I hope that helps