Pong collision detection

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
Lividpayment
Posts: 2
Joined: April 13th, 2020, 8:47 pm

Pong collision detection

Post by Lividpayment » May 21st, 2020, 10:34 pm

int BallWidth = xPos + width;
int BallHeight = yPos + height;

if (BallWidth >= PaddleX && xPos <= PaddleX + PaddleWidth &&
BallHeight >= PaddleY && yPos <= PaddleY + PaddleHeight)
{
vx + 1;
BallWidth = PaddleX - 3;
}

if (BallWidth >= PaddleX && xPos <= PaddleX + PaddleWidth &&
BallHeight >= PaddleY && BallHeight <= PaddleY + PaddleHeight)
{
vy - 1;
BallHeight = PaddleY - 3;
}

if (BallWidth >= PaddleX && xPos <= PaddleX + PaddleWidth && yPos <= PaddleY + PaddleHeight && yPos >= PaddleY)
{
vy + 1;
yPos = PaddleY + PaddleHeight + 3;
}



====


Hmmm I can't seem to get the top paddle or the bottom paddle to collide and bounce off correctly. Would someone mind giving me a few pointers in this direction? Thank you!

albinopapa
Posts: 4145
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Pong collision detection

Post by albinopapa » May 22nd, 2020, 3:26 am

First off, the names of the variables are a bit confusing and should probably be more descriptively named, though that is hardly the issue I'm sure.

Second,

Code: Select all

if (BallWidth >= PaddleX && xPos <= PaddleX + PaddleWidth &&
BallHeight >= PaddleY && yPos <= PaddleY + PaddleHeight)
and

Code: Select all

if (BallWidth >= PaddleX && xPos <= PaddleX + PaddleWidth &&
BallHeight >= PaddleY && BallHeight <= PaddleY + PaddleHeight)
looks to be the same code and I'm pretty sure this is not the desirable thing you want.

For determining which way to reflect the ball, you should only be testing for one axis at a time:

Code: Select all

if( xPos <= PaddleX + PaddleWidth && BallWidth >= PaddleX ) 
    // adjust the X axis of velocity and position
if( yPos <= PaddleY + PaddleHeight && BallHeight >= PaddleY )
    // adjust the Y axis of velocity and position
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

albinopapa
Posts: 4145
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Pong collision detection

Post by albinopapa » May 22nd, 2020, 3:31 am

As for my comment about names:
BallWidth and BallHeight describes a bounding box around the ball and these represent the width and height of that bounding box, whereas BallRight and BallBottom would tell me the right and bottom side of that bounding box.

Changing the names:

Code: Select all

int BallRight = xPos + width;
int BallBottom = yPos + height;
int PaddleRight = PaddleX + PaddleWidth;
int PaddleBottom = PaddleY + PaddleHeight;

if( xPos <= PaddleRight && BallRight >= PaddleX )
    // adjust the X axis of velocity and position
if( yPos <= PaddleBottom && BallBottom >= PaddleY )
    // adjust the Y axis of velocity and position
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

Post Reply