Sign in to follow this  

Pong Collision Detection

This topic is 3934 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

Hey all, it's been a while since I've posted, and I am starting back up on programming. Well I was in school and was thinking about giving my Pong clone another go. I was thinking of different ways to do the collision detection and need a confirmation to see if this would work (my theory). P.S This is done in C++ using SDL. Ok, so now.. I haven't actually started on the clone yet, but I was thinking of this.. I should create Player 1's Paddle on the left side with the coordinates of 36x100 (just an example with random numbers) now, should I create something like this..
if(ball_left >= paddle_y && ball_left >= paddle_x)
{
   ball_x++;
}



So saying when the left side of the ball hits the paddle then it bounces back, and now for the movement..(NOTE: this is not the right SDL functions, just an example)
if(key == ARROWUP)
{
   paddle_y--;
}
if(key == ARROWDOWN)
{
   paddle_y++;
}



Now to check if the paddles Y coordinates are matching the screen..
if(paddle_top <= SCREEN_TOP)
{
   paddle_y++;
}
else if(paddle_bottom >= SCREEN_BOTTOM)
{
   paddle_y--;
}



Again, I thought of that collision detection in about 30 minutes at school, but not sure if it works or what. Thanks in advanced for the replies :D. EDIT: But if I made it checking to see if the ball hits the paddles X then won't it bounce back every time because the X coordinate never changes...

Share this post


Link to post
Share on other sites
Your ball struct should have absolute coordinates (x,y) and velocity to control the movement of those coordinates.

Example from own Pong clone:


void update_sprite(SPRITE *param) {
++param->xcount; ++param->ycount;
if (param->xcount > param->xdelay || param->ycount > param->ydelay) {
erase_sprite(param);
if(param->xcount > param->xdelay) {
param->xcount = 0;
param->x += param->velx;
}
if(param->ycount > param->ydelay) {
param->ycount = 0;
param->y += param->vely;
}
}
}



This handles the movement of the sprites (paddles and ball). When you detect a collision, you simply change the proper velocity.

Like so...

rebound_sprite(SPRITE* spr) {
if (spr->x < 0 || spr->x+spr->width > 640) {
spr->vel_x *= -1;
else if (spr->y < 0 || spr->y+spr->height > 480) {
spr->vel_y *= -1;
}



With this method, you're limited to vertical and horizontal faces for your paddles and your playing field, but it certainly gets the job done.

Share this post


Link to post
Share on other sites
what you really want to look into is velocity, and don't just increatment the values. What do I mean?



//the velocity. use any number, but I will use 2 here
float yVel=2.0f;
//switch statement for the keypresses, or w/e you want use. Maybe keystates
switch(event.key.keysym.sym)
{
case SDLK_w: yVel-=sourceRect.h/speed; break;
case SDLK_s: yVel+=sourceRect.h/speed; break;
}



where sourceRect is the rect that holds the dimensions of the paddle.

What this is, is this in words: you just take the sourceRect height value, and divide it by the speed (pixels to move each frame) and subtract it from the yVel. Do just the opposite for the s (down) key.

now for collision. Collision is pretty easy for pong


if(destRect.x+destRect.w==WINDOW_HEIGHT)
{
//oops...we need to take the last move back now!!
destRect.x-=yVel;
}


it's really that simple. Here's a SDL tutorial explaining it more, if you need more help.

LazyFoo's motion tutorial.

I hope that helps dude. If need more help, then don't be afraid to post back here.

Chad.

EDIT: Beaten, but will keep it here, just to clairify more.
Changed xVel to yVel.

EDIT 2: If you don't believe me, or want to know why the paddle moves, then lets do math.

EDIT 3: lol...last edit...hopefully. Just back here if you need help with the ball. It's basically just the same, except one little tiny bitty difference, but the logic is the same...so you might get it.

yVel=2.0, speed=10 (for right now it is), image size=32x32 (any size will work in SDL...I am just acting like we are moving a tile

so...first: 32/10=3.2
next: 2.0-3.2=-1.2
so, you can see that we are indeed moving it up, due to the cordinate system.

Share this post


Link to post
Share on other sites

This topic is 3934 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this