SFML 2.0 Collision not working

I don't understand why this doesn't work.
please help

(relevant) Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//collision function
bool Collision(sf::RectangleShape &paddle, sf::RectangleShape &ball)
{
    float pbottom = paddle.getPosition().y, pleft = paddle.getPosition().x - (paddle.getSize().x),
    pright = paddle.getPosition().x, ptop = paddle.getPosition().y - (paddle.getSize().y);

    float bbottom = ball.getPosition().y, bleft = ball.getPosition().x - (ball.getSize().x),
    bright = ball.getPosition().x, btop = ball.getPosition().y - (ball.getSize().y);

    if (pright < bleft || pleft > bright || ptop > bbottom || pbottom < btop)
            {
                return false;
            }
            return true;
}
closed account (o1vk4iN6)
 
ptop > bbottom || pbottom < btop


You have these backwards, if top is greater than bottom collision is still possible.
changed it but collision still isn't registered

New code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//collision function
bool Collision(sf::RectangleShape &paddle, sf::RectangleShape &ball)
{
    float pbottom = paddle.getPosition().y, pleft = paddle.getPosition().x - (paddle.getSize().x),
    pright = paddle.getPosition().x, ptop = paddle.getPosition().y - (paddle.getSize().y);

    float bbottom = ball.getPosition().y, bleft = ball.getPosition().x - (ball.getSize().x),
    bright = ball.getPosition().x, btop = ball.getPosition().y - (ball.getSize().y);

    if (pright < bleft || pleft > bright || ptop < bbottom || pbottom > btop)
            {
                return false;
            }
            return true;
}
Last edited on
Because at least one of those four statements will be true.

Try using and instead (&& instead of ||).
closed account (o1vk4iN6)
^ thats the point, if one of those are true then it is know there won't be a collision. If the min of object X is greater than the max of object Y then they won't intersect.

You should follow the coordinate system:

For example if paddle position.y is 0 then ptop is negative. That is backwards to the norm. Normally "up" is positive y.
 
ptop = paddle.getPosition().y - (paddle.getSize().y)


Sfml does come with a class sf::Rect<>. If you are not going to rotate the RectShape you can just use getGlobalBounds() with intersects().

http://www.sfml-dev.org/documentation/2.0/classsf_1_1Shape.php#a5257341fe832884dbba6b9dc855e33cc
http://www.sfml-dev.org/documentation/2.0/classsf_1_1Rect.php#a566740c8f58e01bb052266f47e7e1011



Last edited on
Ah well I've got it inverted.
i don't understand the syntax of the intersect function
closed account (N36fSL3A)
Check out lazyfoo's collision detection tutorial.

EDIT: (omg 1024 posts!)
Last edited on
congrats lumpkin

i figured it out
@Cronnoc
Even though you figured it out, lazyfoo's tutorials are still a decent set of tutorials to look at if you haven't already. Then of course the documentation is always best for reference.

[EDIT]Even though you are using SFML, the underlying concepts are the same so I still think it is beneficial just to glance at lazyfoo's stuff.
Last edited on by closed account z6A9GNh0
closed account (3qX21hU5)
Btw SFML provides contains() function for sf::Rect objects which can simplify your collision test. For example your whole collision function can be reduced to this.

1
2
3
4
bool Collision(sf::FloatRect &lhs, sf::FloatRect &rhs)
{
    return lhs.contains(rhs);
}


You shouldn't really be testing your sf::RectangleShapes directly either instead use sf::Rect objects. For example to call the collision function on one of the paddles and the ball you could do something like this.

1
2
3
4
if (collision(paddle.getGlobalBounds(), ball.getGlobalBounds()))
{
    // Do whatever you want to do to handle the collision
}


getGlobalBounds() will return a sf::FloatRect object that represents that objects global bounds (IE all transformations will be taken into account).

http://www.sfml-dev.org/documentation/2.1/classsf_1_1Shape.php#a5257341fe832884dbba6b9dc855e33cc
http://www.sfml-dev.org/documentation/2.1/classsf_1_1Rect.php#details
Topic archived. No new replies allowed.