Page 1 of 1

Gaps and overlaps in object collision

Posted: Sat Jun 08, 2013 2:38 pm
by lieudusty
Hello everyone! :)

I've been using the bounding box collision function found on the love2d forums and its working fine but when I try to make walls with it, there are sometimes gaps or overlaps and sometimes not.

Thanks for the help in advanced!

Code: Select all

-- Check Wall returns true if the player's x and y are colliding with a wall
-- Collision gets checked with bounding box collision

if love.keyboard.isDown("up") or love.keyboard.isDown("w") then
	if not checkWall(_, player.y - 1 * dt * player.speed) then
		player.y = math.ceil(player.y - 1 * dt * player.speed)
	end
elseif love.keyboard.isDown("down") or love.keyboard.isDown("s") then
	if not checkWall(_, player.y + 1 * dt * player.speed) then
		player.y = player.y + 1 * dt * player.speed
	end
end

Re: Gaps and overlaps in object collision

Posted: Sat Jun 08, 2013 2:49 pm
by micha
To me, it is not totally clear, what you mean by gaps or overlay, but here is a general comment on collision:
What you do right now is this:
  • Calculate the new position of the player
  • Check if new position causes a collision
  • If it does not, then move to the new position
  • Otherwise, stay at the old position
If now the old position still has some distance to the wall (gap) then the player cannot move any closer to the wall.

So a better method looks like this (only change the last step):
  • Calculate the new position of the player
  • Check if new position causes a collision
  • If it does not, then move to the new position
  • Otherwise, move to the spot closest to the new position, that does not cause collision (touch the wall).

Re: Gaps and overlaps in object collision

Posted: Sat Jun 08, 2013 2:51 pm
by lieudusty
micha wrote:To me, it is not totally clear, what you mean by gaps or overlay, but here is a general comment on collision:
What you do right now is this:
  • Calculate the new position of the player
  • Check if new position causes a collision
  • If it does not, then move to the new position
  • Otherwise, stay at the old position
If now the old position still has some distance to the wall (gap) then the player cannot move any closer to the wall.

So a better method looks like this (only change the last step):
  • Calculate the new position of the player
  • Check if new position causes a collision
  • If it does not, then move to the new position
  • Otherwise, move to the spot closest to the new position, that does not cause collision (touch the wall).
Oh thank you very much for the help I'll try to move it to the closest position to a wall. Also by gaps and overlays, I mean that sometimes the player will have a small gap between a wall and sometimes overlay the wall.

Re: Gaps and overlaps in object collision

Posted: Sat Jun 08, 2013 4:17 pm
by Inny
It sounds like you're experiencing an "Internal Edge", where the collision masks of two side by side objects causes the colliding object (the player) to either stick or get stuck to it, or fall between it. An alternate method that is more closely aligned with how people perceived and expect video game collision to work goes like so:
  1. Generalize the collision function to work on a single axis at a time, and perform them separately (i.e. apply the change in X first, then do Y).
  2. Iterate over all nearby objects with potential collisions. If the Player overlaps a wall, only move them off the wall in the direction they came from, and only in the component direction being tested.
  3. Then, switch to the other component and reiterate over all of those nearby objects again.
  4. The Player's final position is the changed X position combined with the changed Y position.
What you get from doing it this way is, lets say it's a platformer. By performing the Y collision first, the player is moved from inside the floor to on top of the floor in the first pass. Then, when you perform the X movement and collision detection, the player is already just above the floor, so the X can move without problem, meaning regular horizontal movement feels more like regular walking/sliding without the feel that you're stopping every 16 pixels.

Re: Gaps and overlaps in object collision

Posted: Sun Jun 09, 2013 6:12 am
by ivan
Calculate the new position of the player
Check if new position causes a collision
If it does not, then move to the new position
Otherwise, move to the spot closest to the new position, that does not cause collision (touch the wall).
I use a slightly different approach:
- Move the player to a new position
- Find all nearby colliding objects
- If necessary, adjust the player's position so that he's not colliding with those objects
Inny wrote:
  1. Generalize the collision function to work on a single axis at a time, and perform them separately (i.e. apply the change in X first, then do Y).
  2. Iterate over all nearby objects with potential collisions. If the Player overlaps a wall, only move them off the wall in the direction they came from, and only in the component direction being tested.
  3. Then, switch to the other component and reiterate over all of those nearby objects again.
  4. The Player's final position is the changed X position combined with the changed Y position.
What you get from doing it this way is, lets say it's a platformer. By performing the Y collision first, the player is moved from inside the floor to on top of the floor in the first pass. Then, when you perform the X movement and collision detection, the player is already just above the floor, so the X can move without problem, meaning regular horizontal movement feels more like regular walking/sliding without the feel that you're stopping every 16 pixels.
Could you please explain how resolving the collisions one axis at a time would solve the problem altogether?
I mean yes, if you resolve the Y axis first you won't get stuck during horizontal movement.
BUT in that case, you can get stuck during vertical movement - while falling next to walls for example.

Re: Gaps and overlaps in object collision

Posted: Sun Jun 09, 2013 10:26 pm
by Inny
ivan wrote:Could you please explain how resolving the collisions one axis at a time would solve the problem altogether?
I mean yes, if you resolve the Y axis first you won't get stuck during horizontal movement.
BUT in that case, you can get stuck during vertical movement - while falling next to walls for example.
In code that I've written, which works fairly well, I actually do the X first and then the Y, but that's a fine detail, it probably would work either way. However, you have to think about it in terms of the vectors. Lets say the player is moving up and to the right, from a standing position. When you apply the right vector component first, he's moving along the floor, but not colliding with internal edges from the floor beneath him, because he's technically above the floor. If you did the falling against a wall bit: he moves down from the air first, and then the X component checks if he's moving into the wall *after* he's moved through the air. Essentially the player is moving in straight lines along the X and Y axis, and the actually drawing stage only shows the end result giving the appearance of diagonal motion.

Re: Gaps and overlaps in object collision

Posted: Mon Jun 10, 2013 5:26 am
by micha
I do collision detection for the x- and y-axis separately, too. I find it easier to implement, since when a collision happens, it is very easy to resolve the collision. On the other hand, it is only useful, if all objects are axis aligned bounding boxes (AABBs).

Re: Gaps and overlaps in object collision

Posted: Wed Jun 12, 2013 5:24 pm
by lieudusty
Well all I'm trying to find help on is why there is a gap between the player and the walls and a solution to fix that. But sadly I still can't figure it out :(

Re: Gaps and overlaps in object collision

Posted: Thu Jun 13, 2013 6:17 am
by micha
Can you upload a .love-file. That makes it much easier for us to help you. Otherwise, we can only guess.