Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Collision  (Read 1294 times)

0 Members and 1 Guest are viewing this topic.

Viperz012

  • Newbie
  • *
  • Posts: 2
    • View Profile
Collision
« 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!
« Last Edit: February 13, 2015, 05:20:57 am by Viperz012 »

Ricky

  • Jr. Member
  • **
  • Posts: 99
    • View Profile
    • Tejada
    • Email
Re: Collision
« Reply #1 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 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.
Wilt thou yet say before him that slayeth thee, I am God? but thou shalt be a man, and no God, in the hand of him that slayeth thee.

AlexxanderX

  • Full Member
  • ***
  • Posts: 128
    • View Profile
    • AlexanderX
Re: Collision
« Reply #2 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/
Here you can find my blog and tutorials about SFML - http://alexanderx.net/ (died...) - http://web.archive.org/web/20160110002847/http://alexanderx.net/