I am trying to create a basic side scroll 2D game. There are lots of documentation on collision detection but I find the easiest to use at the moment is just basic AABB Collision. This is my basic outline

Rect old_position = player->rect;
if(key == a) {
    player.x += 5; // move 5 px to right
if ... // same for up and left

if(key == s) {
    player.y += 5; // move 5 px down

player.y += 4; // move player down, gravity

if(collision_detection(player->rect, rectangle->rect)) { // rect = {x pos, y pos, w idth h eight}
    if(player->rect.y - old_position.y > 0) {
        player->rect.y = rectangle->rect.y - player->rect.h; // if player moved down and collided, move the player to the same pos as object + its own height to get directly above the object.
    } else if(player->rect.y - old_position.y < 0) {
        player->rect.y = rectangle->rect.y + rectangle->rect.h; // move to directly under collided object.

this does not work since if I come from the side, falling down, I get teleported up above the object! There are people who write about things such as roleback / moving back before the collision happened, etc. but there is not straight forward way of doing collision resolution. When I try different methods they might work but I always slide into them if I walk straight into a wall or if I jump and move into a wall in the air while jumping I glitch inside the rectangle, etc, etc.

Does anyone have a blueprint for different 2D collision resolution? I am curious what types there are and what pros and cons there is. The above code is just sample dummy code, so there is no need to critique it. Thanks!

  • \$\begingroup\$ Presumably you searched for existing guides and tutorials about AABB collision, and past Q&A in the collision-resolution tag here? This topic already has many hundreds of documented solutions, so I'm not sure what new content you expect us to offer above and beyond what's already at your fingertips via search. \$\endgroup\$ – DMGregory Oct 4 '20 at 12:53
  • \$\begingroup\$ @DMGregory the solutions are "do collision prediction!", "Have you seen how this game does it? This is the theory" "do back stepping!", "relative velocity vector", etc, etc, etc. Nobody has sample code (A BLUEPRINT AS I WROTE IN MY POST, IMPORTANT). Google shows the same results, just a bunch of buzzwords but no practical code! Please provide useful information instead of wasting time with these comments that indicate that you have not read the post. Thanks. \$\endgroup\$ – hahasajk Oct 4 '20 at 13:14
  • \$\begingroup\$ @DMGregory do you have any comment on the fact that every post here on stackexchange containing the tag collision-resolution has a general, text-based solution that is non testable and all theory? By the looks of your profile you should have more to say... \$\endgroup\$ – hahasajk Oct 8 '20 at 16:25
  • 1
    \$\begingroup\$ That suggests to me that most people on this site are comfortable implementing a working code solution from a text description. This is fairly typical of answers here, as this is not a copy-and-paste code on demand service, and each game usually has at least a few idiosyncrasies that require a slightly custom implementation. If you'd like to edit your question to clarify a specific problem you've encountered in implementing one of these text descriptions into your game, you might find that more focused question does better in attracting new, actionable answers. \$\endgroup\$ – DMGregory Oct 8 '20 at 16:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.