Methods for accurately detecting collisions in a platformer?

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
dichotommy
Prole
Posts: 4
Joined: Thu Nov 22, 2018 11:09 am

Methods for accurately detecting collisions in a platformer?

Post by dichotommy »

Hi everybody. I recently got it in my head to recreate the movement/mechanics of platformer Celeste in Love as a coding exercise (and yes, before you say it, I know the source is available). This is my first time ever trying to code this type of game. I didn't have too much trouble implementing movement, jumping, and dashes, but have been absolutely struggling with detecting/handling collisions between the player and the platforms.

The solution I came to on my own was giving the platform object four rectangular "hitboxes," each one representing a side of the platform. This generally gives me the ability to tell which side the player is colliding with, and thus whether he should be treated as if hitting a wall, a ceiling, or landing on a platform. But! What about when the player is in one of the platform's corners, colliding with two hitboxes? How do I know if the player is jumping from underneath or coming in from the side?

My janky answer: making the platform's side colliders really narrow and removing any overlap w/ the top and bottom colliders. It works pretty well, but the unfortunate result is that sometimes the player jumps from underneath the platform and slides very slightly to the left or right, snapping to the platform's side when he should really hit the bottom.

So, this is long-winded, but my question is: what is the best way to handle player-platform collisions in a platformer-type game? I suspect the answer involves checking velocity vectors or keeping track of the player's previous position (both things I've never tried before) but I'm curious what approaches exist—or if there's something really fundamental I'm not thinking of.

Many thanks for your help,
newbie coder
User avatar
ivan
Party member
Posts: 1915
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Methods for accurately detecting collisions in a platformer?

Post by ivan »

Collision detection and response is a difficult problem.
Collisions are usually resolved in pairs. With non-continuous collision detection, you want to resolve based on the "shortest separation vector". So it doesn't matter where the player is coming from, the important thing is, what is the shortest/most efficient way to separate the two overlapping shapes. You'll need to find the collision normal and penetration depth.
There is no "best" way to handle collisions. Personally I use the Box2D library (love.physics). I have done a collision library in pure Lua but it's too much work and it's not as good. This is a difficult problem and you're not going to come up with anything better than Box2D.
Last edited by ivan on Sat Dec 11, 2021 8:25 am, edited 1 time in total.
User avatar
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

Re: Methods for accurately detecting collisions in a platformer?

Post by tentus »

Something that has worked for me in the past is the concept of the "white cane" sensor objects. Basically, you can position sensors around your player and use them to tell "am I adjacent to something," much like how a person with limited or no vision can use a cane to feel the space ahead of them. An advantage to this method is that you can have many of these objects surrounding the player, (like, say, 8 in a loose square arrangement) and by cross-referencing them you can determine if the player is clinging to just a wall, standing on a corner, etc. Because they're objects you can fudge the sizing of them a bit, depending on the levels you're making.
Kurosuke needs beta testers
karolek
Prole
Posts: 13
Joined: Sun Jul 16, 2017 8:36 pm

Re: Methods for accurately detecting collisions in a platformer?

Post by karolek »

Actually, the author of Celeste – Matt Makes Games, has written and article that just answers some of your questions:
https://mattmakesgames.tumblr.com/post/ ... ll-physics

That system was used in his previous game, but I think it may teach you something. It is not a standard method of dealing with physics though (or it is just that I would do it differently).
There is clearly a separation between collisions with active objects like player and enemy and collisions with static level. You will likely handle them differently.

As ivan said, it can be easier to use Box2D library, but in pixel-perfect platformers that approach may be worse than writing your own imperfect physic engine. Shortest separation vector is also a way and might work in your case.

If you fill into reading more on topic, I really liked this, quite comprehensive article:
https://www.gamedev.net/articles/progra ... onse-r3084

All in all, there is no silver bullet (how different it was done in old Mario Bros for example), so inspire yourself with those and experiment a little.
User avatar
pgimeno
Party member
Posts: 3672
Joined: Sun Oct 18, 2015 2:58 pm

Re: Methods for accurately detecting collisions in a platformer?

Post by pgimeno »

I'm surprised no one has mentioned Kikito's bump.lua yet.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 11 guests