Gaps and overlaps in object collision

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
lieudusty
Prole
Posts: 14
Joined: Thu Feb 21, 2013 5:19 am

Gaps and overlaps in object collision

Post 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
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: Gaps and overlaps in object collision

Post 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).
lieudusty
Prole
Posts: 14
Joined: Thu Feb 21, 2013 5:19 am

Re: Gaps and overlaps in object collision

Post 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.
User avatar
Inny
Party member
Posts: 652
Joined: Fri Jan 30, 2009 3:41 am
Location: New York

Re: Gaps and overlaps in object collision

Post 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.
User avatar
ivan
Party member
Posts: 1912
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Gaps and overlaps in object collision

Post 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.
User avatar
Inny
Party member
Posts: 652
Joined: Fri Jan 30, 2009 3:41 am
Location: New York

Re: Gaps and overlaps in object collision

Post 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.
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: Gaps and overlaps in object collision

Post 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).
lieudusty
Prole
Posts: 14
Joined: Thu Feb 21, 2013 5:19 am

Re: Gaps and overlaps in object collision

Post 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 :(
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: Gaps and overlaps in object collision

Post by micha »

Can you upload a .love-file. That makes it much easier for us to help you. Otherwise, we can only guess.
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot], Semrush [Bot] and 6 guests