Public Group

# Is this how I should do it?

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

## Recommended Posts

Hello, I'm STILL working on Pong, and finally got some ball/paddle collisions. However, in an attempt to refine it further (so that the ball deflected up when it hit the top of the paddle and down when it hit the bottom), I ended up deleting my code and forgetting how I did it :(. What I think should happen is this: I've got a bitmap blitted onto a rectangle, called element_paddle_player (there's another one called element_paddle_computer, but that's not important). A collision should occur if the ball's x coordinate == the x co-ordinate of the paddle AND is within the height of the rectangle. Trouble is, the way I had it, when you move the paddle to the top of the screen, the ball bounced backwards and forwards endlessly off of an invisible line, but when it was moved down it worked fine (except the deflection didn't work correctly). I don't have any code to hand. Is the way I suggested up there the right way to do it? If so, how exactly would I achieve the detection for the y-range (i.e. the paddle height), and code it? I've been stuck on this for days. I've taken a look at code by Aaron Cox and Steve, which partly made sense and worked to an extent but didn't get the desired effect. Any suggestions? I've posted different problems surrounding this of late, but I'm getting pretty desperate. ukdeveloper.

##### Share on other sites
I am assuming you are using bitmaps for your ball and paddles, so we'll just use rectangle collision detection.

So you use rectangles for blitting your sprites, these rectangles have four points/corners/vertices and four sides (left,right,top,bottom). Two rectangles collide if at least one point of one of the rectangles is inside another rectangle. This is the pseudocode:
for each point   if rect2.left < point.x < rect2.right AND      rect2.top < point.y < rect2.bottom      // object collides

But of course this method isn't at all accurate for round objects such as a ball.

##### Share on other sites
Quote:
 Original post by alniteI am assuming you are using bitmaps for your ball and paddles, so we'll just use rectangle collision detection.

Yes. The "ball" is also in a square with the black pixels round the edges made transparent.

I'm not understanding this, what do rect2.left and point.x etc. refer to?

Please explain more clearly, I'm using SDL_Rect to blit them onto the screen if that helps you to know. I therefore don't really have a way of working out the "left".

##### Share on other sites
Quote:
Original post by ukdeveloper
Quote:
 Original post by alniteI am assuming you are using bitmaps for your ball and paddles, so we'll just use rectangle collision detection.

Yes. The "ball" is also in a square with the black pixels round the edges made transparent.

I'm not understanding this, what do rect2.left and point.x etc. refer to?

Please explain more clearly, I'm using SDL_Rect to blit them onto the screen if that helps you to know. I therefore don't really have a way of working out the "left".

Sorry, this is the better pseudocode:
for each point in rect1   if rect2.left < point.x < rect2.right AND      rect2.top < point.y < rect2.bottom      // object collides

I am not familiar with SDL_Rect. But this is basically what a rectangle is:
 (x,y)   +-----+   |     |   |     |   +-----+

The left side of the rectangle is basically just x, and the top side is just y. The right side is x + width of the rectangle, and the bottom side is y + height of the rectangle.

Or you can think of them as four points: (x,y), (x+width,y), (x,y+height), (x+width,y+height). What you need to do is to check if any point of rectangle 1 is inside rectangle 2. If so, collision occurs.

1. 1
2. 2
Rutin
21
3. 3
4. 4
frob
16
5. 5

• 9
• 13
• 9
• 33
• 13
• ### Forum Statistics

• Total Topics
632593
• Total Posts
3007279

×