Public Group

# How to see if two sprites are intersecting in SFML

This topic is 2486 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

What would be the easiest way to have a sprite bounce off another sprite when they intersect.
would it be to find the corners of the sprite and setup an if statement or does sfml offer another way to do this?
I've been stuck on this for awhile now so thanks for your help.

##### Share on other sites
What you would want to do is create a boundary for each sprite. SFML has a defined rectangle class that you can use just for this as it provides functionality for seeing when two rectangles intersect.

##### Share on other sites
Yes, SFML's Rect class will be able to tell you if two rectangles are intersecting. If you need more control or information though (i.e. maybe to know which side of the rectangle, so you know how to change the rectangle's velocity so it bounces in the right direction), you can look into AABB collision detection. The Separating Axis Theorem is really simple for AABBs, should you need to implement it (here's one of my favorite references).

##### Share on other sites
I'm not sure how to use the sfml rectangle class. I'm trying to see if a paddle and a ball intersect.
In my Global.h
 sf::IntRect ppaddleRect( ppaddle.GetPosition().x, ppaddle.GetPosition().y, ppaddle.GetSize().x, ppaddle.GetSize().y); sf::IntRect pballRect( pball.GetPosition().x, pball.GetPosition().y, pball.GetSize().x, pball.GetSize().y); 

In my Update Function
 if(pballRect.Intersects(ppaddleRect)) { Xballspeed = -Xballspeed; } 

However, it seems that the two are always intersecting, so the ball now moves up and down. Does anyone know why. (when I comment out the if statement above the ball moves fine, but obviously doesn't bounce off the paddle either)

##### Share on other sites
Are you sure you want those in Global.h declared like that? Each source file that includes Global.h is going to get its own copy of the variables. If you really are going to use globals like that, you should declare and define them in one source file, and then in the Global.h file put "extern sf::IntRect ppandlRect" and "extern sf::IntRect pballRect".

Not that I recommend using globals.

##### Share on other sites

I'm not sure how to use the sfml rectangle class. I'm trying to see if a paddle and a ball intersect.
In my Global.h
 sf::IntRect ppaddleRect( ppaddle.GetPosition().x, ppaddle.GetPosition().y, ppaddle.GetSize().x, ppaddle.GetSize().y); sf::IntRect pballRect( pball.GetPosition().x, pball.GetPosition().y, pball.GetSize().x, pball.GetSize().y); 

In my Update Function
 if(pballRect.Intersects(ppaddleRect)) { Xballspeed = -Xballspeed; } 

However, it seems that the two are always intersecting, so the ball now moves up and down. Does anyone know why. (when I comment out the if statement above the ball moves fine, but obviously doesn't bounce off the paddle either)

Are you updating the Rectangle's location every frame? Go ahead and print out the X, Y, width and height of the rectangles when they intersect, and you should see what's happening. All an intersection is a check like this (checking Rect1 and Rect2 intersect or not):
 if (Rect1.X <= Rect2.X + Rect2.Width && Rect1.X + Rect1.Width >= Rect2.X && Rect1.Y <= Rect2.Y + Rect2.Height && Rect1.Y + Rect1.Height >= Rect2.Y) 

BTW, I've started using a 2d physics library (chipmunk-physica, see my old blog link in my sig). it's simplifies things so much for me. No longer do I have to worry about collision detection, collision response, object movement, etc. the 2d physics library handles all that for me.

##### Share on other sites
Ok this is what i put
 if (pball.GetPosition().x <= ppaddle.GetPosition().x + ppaddle.GetSize().x && pball.GetPosition().x + pball.GetSize().x >= ppaddle.GetPosition().x && pball.GetPosition().y <= ppaddle.GetPosition().y + ppaddle.GetSize().y && pball.GetPosition().y + pball.GetSize().y >= ppaddle.GetPosition().y) { Xballspeed = -Xballspeed; } 
sometimes the collision happens way to the right of the paddle, other times the collisions happens when the ball is coming from behind the paddle(the ball bounces off all of the walls for now) and other times the ball completely ignores the paddle. I must be missing something vital in my code.

##### Share on other sites

Ok this is what i put
 if (pball.GetPosition().x <= ppaddle.GetPosition().x + ppaddle.GetSize().x && pball.GetPosition().x + pball.GetSize().x >= ppaddle.GetPosition().x && pball.GetPosition().y <= ppaddle.GetPosition().y + ppaddle.GetSize().y && pball.GetPosition().y + pball.GetSize().y >= ppaddle.GetPosition().y) { Xballspeed = -Xballspeed; } 
sometimes the collision happens way to the right of the paddle, other times the collisions happens when the ball is coming from behind the paddle(the ball bounces off all of the walls for now) and other times the ball completely ignores the paddle. I must be missing something vital in my code.

Lets just look at the case where it happens way to the right of the paddle.

The only way a collision is possible is
is true

How can that be true, if the ball is way to the right? That line says, "The left side of the ball is to the left or equal to the right side of the paddle"

So, something is wrong in your code. Is GetSize() returning the right value? Are you drawing the objects based on the GetPosition() values? Print out the values when it collides, and check what it's telling you about the position and size of the objects.

##### Share on other sites
ok i finally got back to programming.(being 14 its hard to find time sometimes) The prints were fine except the sizes of the paddle were mixed up. I believe it is because I rotated the sprite before i drew it so it would be in the right direction. Do you think this is true? When I rotated the sprite back to its original rotation the collision detection worked perfectly. Also, how would i need to change my if statement above to make this work.

##### Share on other sites

ok i finally got back to programming.(being 14 its hard to find time sometimes) The prints were fine except the sizes of the paddle were mixed up. I believe it is because I rotated the sprite before i drew it so it would be in the right direction. Do you think this is true? When I rotated the sprite back to its original rotation the collision detection worked perfectly. Also, how would i need to change my if statement above to make this work.

Well, if getSize() returns the sprite before rotation, you have 2 options:
#1 (easiest) draw the paddle upright, not on it's side or,
#2, create your own GetSize(), which takes into account the current rotation of the sprite.

1. 1
2. 2
Rutin
17
3. 3
4. 4
5. 5

• 13
• 26
• 10
• 11
• 9
• ### Forum Statistics

• Total Topics
633735
• Total Posts
3013593
×