How to calculate the collision (if boxes overlap)



  • Dear readers,

    can someone tell me how to write my IF statement on the bottom of my code?
    I want it to return a number i.e. what side there is collision on.

    @#include <SFML/System.hpp>
    #include <SFML/Graphics.hpp>
    #include <SFML/Window.hpp>
    #include <SFML/Audio.hpp>
    #include <SFML/Config.hpp>

    #include "collision.h"

    /*
    Collision Detection Function
    1 = top
    2 = right
    3 = bottom
    4 = left
    */
    int collisionCheck( sf::Sprite player, sf::Sprite object )
    {
    // get the position and dimensions of the player sprite
    int pPosX = player.GetPosition().x;
    int pPosY = player.GetPosition().y;
    int pSizeX = player.GetSize().x;
    int pSizeY = player.GetSize().y;

    // get the position and dimensions of the object sprite
    int oPosX = object.GetPosition().x;
    int oPosY = object.GetPosition().y;
    int oSizeX = object.GetSize().x;
    int oSizeY = object.GetSize().y;
    
    // check for collision on the right side of the player
    if( (pPosX + pSizeX) >= oPosX && (pPosX + pSizeX) <= (oPosX + oSizeX) && (pPosY + pSizeY) >= oPosY && pPosY <= (oPosY + oSizeY ) ){
    

    return 2;
    }

    // check for collision on the left side of the player
    if( (pPosX - 2) <= (oPosX + oSizeX) && (pPosY + pSizeY) >= oPosY && pPosY <= (oPosY + oSizeY ) ){
    

    return 4;
    }

    // check for collision on the top side of the player
    if( ){
    

    return 1;
    }

    // check for collision on the bottom side of the player
    if( ){
    

    return 1;
    }

    }

    int main()
    {
    // create a new window 800x600 resolution 32 bit
    sf::RenderWindow App(sf::VideoMode(800, 600, 32), "LittleBigGame");

    // draw a cube (red)
    sf::Image player;
    player.Create(50, 50, sf::Color::Red);
    
    sf::Texture playerTexture;
    playerTexture.LoadFromImage(player);
    
    sf::Sprite playerSprite(playerTexture);
    playerSprite.SetX(500);
    playerSprite.SetY(410);
    
    // draw a wall (blue)
    sf::Image wall;
    wall.Create(350, 50, sf::Color::Blue);
    
    sf::Texture wallTexture;
    wallTexture.LoadFromImage(wall);
    
    sf::Sprite wallSprite(wallTexture);
    wallSprite.SetX(150);
    wallSprite.SetY(410);
    
    // draw a wall (yellow)
    sf::Image wall2;
    wall2.Create(50, 350, sf::Color::Yellow);
    
    sf::Texture wallTexture2;
    wallTexture2.LoadFromImage(wall2);
    
    sf::Sprite wallSprite2(wallTexture2);
    wallSprite2.SetX(500);
    wallSprite2.SetY(110);
    
    // timer so every computer has the same speed
    sf::Clock clock;
    
    while( App.IsOpened())
    {
        // handle events
        sf::Event Event;
        while (App.PollEvent(Event))
        {
            switch(Event.Type)
            {
                // als het scherm gesloten word
                case sf::Event::Closed:
                    App.Close();
                break;
            }
        }
    
        if(clock.GetElapsedTime() >= 33)
        {
            // execute stuff
            if(sf::Keyboard::IsKeyPressed(sf::Keyboard::Up))
            {
    

    int check = collisionCheck( playerSprite, wallSprite2 );

    if(check != 1){
    playerSprite.Move( 0, -6 );
    }
    }

            if(sf::Keyboard::IsKeyPressed(sf::Keyboard::Right))
            {
    

    int check = collisionCheck( playerSprite, wallSprite2 );

    if(check != 2){
    playerSprite.Move( +6, 0 );
    }
    }

            if(sf::Keyboard::IsKeyPressed(sf::Keyboard::Down))
            {
                playerSprite.Move( 0, +6 );
            }
    
            if(sf::Keyboard::IsKeyPressed(sf::Keyboard::Left))
            {
    

    int check = collisionCheck( playerSprite, wallSprite2 );

    if(check != 4){
    playerSprite.Move( -6, 0 );
    }
    }

            // reset the clock
            clock.Reset();
        }
    
        // clean screen
        App.Clear();
    
        /*
            draw sprites which will be BEHIND of our player
        */
    

    //App.Draw(wallSprite);
    App.Draw(wallSprite2);

        // draw the player sprite
    

    App.Draw(playerSprite);

        /*
            draw sprites which will be IN FRONT of our player
        */
    
    
        // display the window
        App.Display();
    
    } // end while App is opened
    
    return EXIT_SUCCESS;
    

    }
    @

    Hope someone can inform me on how to do this :)

    P.S. I am using SFML but that doesnt matter much, as this is about how to calculate it and the above code should be clear to anyone :)

    The code I have above seems to work for left and right, but I cant get the top and bottom ones working... please help me out ; ;


  • Moderators

    What does this have to do with Qt? Maybe you should try a forum specializing on the library you are actually using?



  • Not much with Qt but its just a common question about how to make a sum, nothing to do with a library or what so ever. I am asking how to make the sum, not how to use a part of the library.

    The sum is just normal C++ like doing math that's all.



  • To get back to Qt: Qt has classes like QRect and QRegion that support methods like intersects() that might be of help.



  • Thank you very much Andre, I will look those functions up, that might be just what I need :)
    Aside from that... my question was just a normal C++ question and yes, I asked it on the forums from the makers
    of this library but all people there are to "cocky" to explain me this simple formula so I tought, lets try the Qt forums
    since I have had a lot of tips, and explenations from nice people on here :)

    Anyways, I will look at the functions but still, can anyone explain me the formula? :P



  • Its not really any sort of formula. If this was in 3D you would need formulas, but this are just basic comparisons. I'd suggest you draw two boxes in a coordinate system on a paper and do the comparisons for left and right (as already exist in the code above) by hand to get a better understanding of what's happening.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.