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

Author Topic: Methods for Collsion Detection  (Read 967 times)

0 Members and 1 Guest are viewing this topic.

Clockwork

  • Newbie
  • *
  • Posts: 47
    • View Profile
Methods for Collsion Detection
« on: June 19, 2014, 08:37:24 pm »
Hi everybody.

Right now I'm just working on a pong game.  So far I have the ball bouncing off the top and bottom of the screen implemented, along with scoring for the player and opponent.  But I'm having some trouble with the paddle collision.  I tried this:

if (ball.getPosition().x > player.getPosition().x &&
                ball.getPosition().x < (player.getPosition().x + PADDLE_WIDTH) &&
                ball.getPosition().y > player.getPosition().y &&
                ball.getPosition().y < (player.getPosition().y + PADDLE_HEIGHT))
        {
                ballDirX = 1;
                if (ballDirY == 1)
                        ballDirY = -1;
                else
                        ballDirY = 1;
        }

But I realized that this only checks if ONE POINT on the ball is colliding with the player's paddle, which causes problems when the player only hits part of the ball that isn't being checked for collisions.  How could I implement a collision system that doesn't have these problems?

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
« Last Edit: June 19, 2014, 08:47:15 pm by Ixrec »

Clockwork

  • Newbie
  • *
  • Posts: 47
    • View Profile
Re: Methods for Collsion Detection
« Reply #2 on: June 19, 2014, 08:46:13 pm »
Awesome, that's just what I needed!

Thanks.

Clockwork

  • Newbie
  • *
  • Posts: 47
    • View Profile
Re: Methods for Collsion Detection
« Reply #3 on: June 19, 2014, 10:08:45 pm »
Quick question actually, how do I use this?

sf::Vector2f pos = Object.TransformToGlobal(sf::Vector2f(0, 0));

I can't find a transformToGlobal function anywhere.  Am I missing something?

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Methods for Collsion Detection
« Reply #4 on: June 19, 2014, 10:23:28 pm »
I forgot there were two versions of that page and linked you the one for SFML 1.  The link should be correct now.

Clockwork

  • Newbie
  • *
  • Posts: 47
    • View Profile
Re: Methods for Collsion Detection
« Reply #5 on: June 22, 2014, 01:42:02 am »
Ok, so I implemented the bounding box collision system and it works, but I want to split it up so that I can tell whether the ball collides with the top or bottom half of the paddle.  This is the boundingboxtest function, how can i modify it to check if it's colliding with the top or bottom half?

int boundingBoxTest(const sf::RectangleShape& object1, const sf::RectangleShape& object2)
        {
                // 0 = not colliding
                // 1 = colliding with top
                // 2 = colliding with bottom

                BoundingBox obj1 = object1;
                BoundingBox obj2 = object2;

                // Create the four distinct axes that are perpindicular to the edjes of the two rectangles
                sf::Vector2f axes[4] =
                {
                        sf::Vector2f(obj1.points[1].x - obj1.points[0].x, obj1.points[1].y - obj1.points[0].y),
                        sf::Vector2f(obj1.points[1].x - obj1.points[2].x, obj1.points[1].y - obj1.points[2].y),
                        sf::Vector2f(obj2.points[0].x - obj2.points[3].x, obj2.points[0].y - obj2.points[3].y),
                        sf::Vector2f(obj2.points[0].x - obj2.points[1].x, obj2.points[0].y - obj2.points[1].x)
                };

                for (int i = 0; i < 4; i++)
                {
                        float minOBJ1, maxOBJ1, minOBJ2, maxOBJ2;

                        obj1.projectOntoAxis(axes[i], minOBJ1, maxOBJ1);
                        obj2.projectOntoAxis(axes[i], minOBJ2, maxOBJ2);

                        if (!((minOBJ2 <= maxOBJ1) && (maxOBJ2 >= minOBJ1)))
                                return 0;
                }

                return 1;
        }

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Methods for Collsion Detection
« Reply #6 on: June 22, 2014, 03:14:47 pm »
That is an extremely simple problem to solve, so please sit down and think about it on your own for a bit.

If you really can't figure it out:
(click to show/hide)