c++/ SFML Collision Handling: get back on "ground"

  • Mudzii
    Likes 0

    Problem Description

    Hi! 

    I have been working on a game in SFML for a school project and I’ve run into some trouble with collision handling. I can get the character to get up on the ledges in the game, but the problem is that they will stay on that ledges y position untill you jump (and you’re not colliding).  Here’s a sniplet of code from my game (please tell me if you need anything else) : 



    bool Game::checkCollisionWithLedge(const sf::RectangleShape &object, float push) {
        bool collision = false;

        //Body variables 
        sf::Vector2f boxPosition = object.getPosition();
        sf::Vector2f boxHalfSize = (object.getSize() / 2.f);

        sf::Vector2f playerPosition = ((Player*)this->mPlayer)->getPlayerPosition();
        sf::Vector2f playerHalfSize(40.f, 30.f);

        float deltaX = boxPosition.x - playerPosition.x;
        float deltaY = boxPosition.y - playerPosition.y;

        float intersectX = abs(deltaX) - (boxHalfSize.x + playerHalfSize.x);
        float intersectY = abs(deltaY) - (boxHalfSize.y + playerHalfSize.y);

        //check collision
        if (intersectX < 0.0f && intersectY < 0.0f) {
            
            push = std::min(std::max(push, 0.f), 1.f); 

            if (deltaY > 0.f) {
                ((Player*)this->mPlayer)->move(0.0f, intersectY * 1.f * push);
        
                ((Player*)this->mPlayer)->setJumpable(false);
                }


            collision = true;

        }


        return collision;
    }


    I appreciate all help!

    Thank you C: 

  • Sonar Systems admin
    Likes 0

    What do you mean it’s not colliding?

  • Mudzii
    Likes 0

    The collision works, but once you’re off the ledge (no collision) the character stays at the same height as the ledge untill you jump up. That’s when the “gravity” starts working and the caracter lands back on ground level (if there’s nothing else to collide with). 

    (I did just managed to make some kind of “gravity” work by constantly pulling the character down with ((Player*)this->mPlayer)->move(0.0f, gravity) if it isn’t colliding. But that makes it either float slowly to the ground (low value for gravity) or impossible to jump (higher value for gravity) )

     

  • Sonar Systems admin
    Likes 0

    Could you show us a video of exactly what is happening

  • Mudzii
    Likes 0

    Yes C: I really hope these will suffice ^^ 

    https://mega.nz/#F!15FXFQ6L!U0fuNmP-gTfRaXmYjFBu1w

  • Sonar Systems admin
    Likes 0

    Are you using Box2d?

  • Mudzii
    Likes 0

    No, I am not. I’ve only used SFML for the project

  • Sonar Systems admin
    Likes 0

    Take a look at these videos to integrate Box2D as it will serve your project better https://www.youtube.com/playlist?list=PLRtjMdoYXLf5VvlkjDmXUn5CPmfQoJf8Z 

  • Mudzii
    Likes 0

    OK C: Thank you for the help ^^ 

  • Sonar Systems admin
    Likes 1

    Your welcome.

     

    Feel free to post any other questions you have.

Login to reply