Jump to content
  • Advertisement
Sign in to follow this  
ukdeveloper

Is this how I should do it?

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
Share on other sites
Quote:
Original post by alnite
I 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 this post


Link to post
Share on other sites
Quote:
Original post by ukdeveloper
Quote:
Original post by alnite
I 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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!