Methods for accurately detecting collisions in a platformer?
Posted: Wed Feb 06, 2019 3:54 pm
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
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