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!
Pong collision detection
-
- Posts: 4373
- Joined: February 28th, 2013, 3:23 am
- Location: Oklahoma, United States
Re: Pong collision detection
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,
and
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:
Second,
Code: Select all
if (BallWidth >= PaddleX && xPos <= PaddleX + PaddleWidth &&
BallHeight >= PaddleY && yPos <= PaddleY + PaddleHeight)
Code: Select all
if (BallWidth >= PaddleX && xPos <= PaddleX + PaddleWidth &&
BallHeight >= PaddleY && BallHeight <= PaddleY + PaddleHeight)
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
-
- Posts: 4373
- Joined: February 28th, 2013, 3:23 am
- Location: Oklahoma, United States
Re: Pong collision detection
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:
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