SFML community forums

Help => General => Topic started by: Viperz012 on February 13, 2015, 05:07:43 am

Title: Collision
Post by: Viperz012 on February 13, 2015, 05:07:43 am
I have a collision map with 0 for no collision and 1 for collision I go through the file find 1 and get its position such as

0 1 0
top = 0
bottom = 32
left = 32
right = 64

    for ( int i = 0; i < mapCollision.size(); i++ )
    {
        for ( int j = 0; j < mapCollision.size(); j++ )
        {
            if ( mapCollision[j] == 1)
            {
                sf::Vector2i findSquarePosition = sf::Vector2i ( 1 * j, 1 * i );
                int topOfTile, bottomOfTile, leftOfTile, rightOfTile;
                topOfTile    = findSquarePosition.y * 32;
                bottomOfTile = findSquarePosition.y * 32 + 32;
                leftOfTile   = findSquarePosition.x * 32;
                rightOfTile  = findSquarePosition.x * 32 + 32;

                int playerTopOfBox, playerBottomOfBox, playerLeftOfBox, playerRightOfBox;
                playerTopOfBox = player.getPosition().y;
                playerBottomOfBox = player.getPosition().y + player.getSize().y;
                playerLeftOfBox = player.getPosition().x;
                playerRightOfBox = player.getPosition().x + player.getSize().x;

                if ( playerRightOfBox < leftOfTile || playerLeftOfBox > rightOfTile || playerTopOfBox > bottomOfTile || playerBottomOfBox < topOfTile )
                {
                }
                else
                {
                    if ( playerTopOfBox < bottomOfTile )
                    {
                        cout << "Collision!" << endl;
                    }
                }

            }
        }
    }

issue I am having is that I hit the block however if I move over to go around it I get blocked also.
I am trying to make a collision detection that knows if I hit the left side of the block or top or bottom or right and pushing away accordingly to that detection.
Say my left side of my player hits the right wall I want to push him right
Say my right side of my player hits the left wall I want to push him left

Can anyone help thank you!
Title: Re: Collision
Post by: Ricky on February 13, 2015, 06:39:14 am
There is a code tag on the forum you could use to make your code look nicer.
char* str = "Something like this\n";

I'd also recommend you completely scratch your method of doing things and use SFML's built in classes (which are nice).

I think you want something like sf::IntRect (http://www.sfml-dev.org/documentation/2.2/classsf_1_1Rect.php) or something in that family. It has a function that checks the intersections for you and then from there you can check the top/bottom/left/right sides etc. There is more information in the documentation.
Title: Re: Collision
Post by: AlexxanderX on February 13, 2015, 07:31:33 am
As Ricky said, you should use sf::Rect. The intersects function also have a second parameter which return a sf::Rect with the intersecion zone.
I also have a tutorial on how to do this: http://alexanderx.net/how-apply-collision/ (http://alexanderx.net/how-apply-collision/)