Problem with collision (AABB)

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
User avatar
Tilur
Prole
Posts: 4
Joined: Mon Dec 02, 2019 8:11 pm
Contact:

Problem with collision (AABB)

Post by Tilur »

Hello people,

I wanna know if some of you actually see where my problem is with my function AABB, because when my player goes into the side of a platform, it doesn't block him and let him fall, but it actually let him go into the platform and reach the top of it.

Here is the code and one picture to make it more clear (player is in yellow, platform is in white) :

AABB Function code :

Code: Select all

local function checkCollision(x1, y1, w1, h1, x2, y2, w2, h2)
  return x1 < x2 + w2 and
  x1 + w1 > x2 and
  y1 < y2 + h2 and
  y1 + h1 > y2
end

Collision Player Platform code :

Code: Select all

for platformNum, platform in ipairs(platformList) do
    if checkCollision(player.x, player.y, player.w, player.h, platform.x, platform.y + player.h, platform.w, platform.h) then
      player.vY = 0       
    end
  end
Image :

Image

Thanks for your help !

See ya later,

Tilur
User avatar
pgimeno
Party member
Posts: 3544
Joined: Sun Oct 18, 2015 2:58 pm

Re: Problem with collision (AABB)

Post by pgimeno »

Tilur wrote: Mon Dec 02, 2019 8:35 pm

Code: Select all

    if checkCollision(player.x, player.y, player.w, player.h, platform.x, platform.y + player.h, platform.w, platform.h) then
Why platform.y + player.h?
User avatar
Tilur
Prole
Posts: 4
Joined: Mon Dec 02, 2019 8:11 pm
Contact:

Re: Problem with collision (AABB)

Post by Tilur »

It's because if i don't add the height of the player, the collision make him like 50 pixels above the platform :/ , i'm not sure this is the good way to do that but it actually worked as well so i let it like that ^^
User avatar
pgimeno
Party member
Posts: 3544
Joined: Sun Oct 18, 2015 2:58 pm

Re: Problem with collision (AABB)

Post by pgimeno »

Hard to tell without having the rest of the code. Maybe you can attach a .love file?

All I can say is that generally, setting the vertical speed to 0 is not enough to guarantee that the entity will be on the platform, because the Y coordinate will already be wrong by the time a collision is detected. The Y coordinate needs to be fixed up as well.

Welcome to the forums, by the way.
User avatar
Tilur
Prole
Posts: 4
Joined: Mon Dec 02, 2019 8:11 pm
Contact:

Re: Problem with collision (AABB)

Post by Tilur »

Oh well, i'm not sure if it will work, but here is it ! :)

I already tried to look for the velocity Y but i didn't found anything, so actually i really don't know :/
Attachments
World of Til'ur.rar
(2.27 KiB) Downloaded 136 times
User avatar
pgimeno
Party member
Posts: 3544
Joined: Sun Oct 18, 2015 2:58 pm

Re: Problem with collision (AABB)

Post by pgimeno »

I've looked into your program a bit, and it's now clear why you need to do platform.y + player.h. It's because the player position is given as the bottom left corner of the player. So, technically the proper thing to do would be to use checkCollision(player.x, player.y - player.h, player.w, player.h, platform.x, platform.y, platform.w, platform.h) so that you use the top left corner of both the player and the platform, but the result is the same so it's OK anyway.

Regarding your problem, it's fixable but it would take some code reorganization and rethinking some things. My impression is that handling collisions yourself may be too much at this point, as they are a complicated subject, and that you should instead resort to using a collision library like bump, which handles collision resolution for axis-aligned rectangles like the ones you're using. Here's bump: https://github.com/kikito/bump.lua

I also recommend you to switch to some other method for detecting if the player is on the platform, other than checking if the vertical speed is zero. I remember assisting someone who had a problem because sometimes, their player could jump while at the cusp of the jump, and that happened because the vertical velocity became zero at that point. If you follow my advice of using bump, you can detect if the player is on the platform by checking the normal of the collision when you perform the bump movement of the player to the new position.
User avatar
Tilur
Prole
Posts: 4
Joined: Mon Dec 02, 2019 8:11 pm
Contact:

Re: Problem with collision (AABB)

Post by Tilur »

Honestly, Thank you,

I didn't expect such message, i just read this, and i'll applicate tomorrow. By the way, i'll try to simplify my code and reorganize it, to make it more clear, i think i'll use the "require" system.

If you want to know, i actually got many problems with table too, and the game is about killing the white rectangles above the player in the right order, but for that you need to listen to a melody of 3 musical tones.

And that will be great i guess for a first prototype, i made this for an entrance exam for Game Design, so i'm trying to be as fast as possible.

Thank you again for your help ! I'll check anything to see what i can improve from your advices !

Have a nice night pgimeno ^^ !
Post Reply

Who is online

Users browsing this forum: No registered users and 54 guests