Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

BadMark

Pong - Collision detection between the paddle and the ball

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

Hi. I am making a pong-clone game in OpenGL(with GLUT).I have a working collision detection between the ball and the wall(s) - it the ball bounces just fine. But I do not know how to do a collision detection between the paddle and the ball.I have the following informations: ball (x,y) position, velocity and two paddle (x,y) positions - px1,px2 and py1,py2..and also paddle size... How can I from those informations calculate if the paddle collides with the ball. Thanks. [edited by - BadMark on June 7, 2002 11:23:00 AM]

Share this post


Link to post
Share on other sites
Advertisement
I would imagien it wouldbe something likethis:


    

//I am assuming that p1 is the face of the paddle,

//or the face that is closest to the play field.


if (ball.x == p1.x)
{
//The p1 values are the upper and lower rightside corners of the paddle

if(ball.y <p1.y && ball.y > p2.y)
{
//Do your stuff here.

}
}


_____________________________
Refried beans are good.

[edited by - Mrbeaner on June 7, 2002 11:44:10 AM]

Share this post


Link to post
Share on other sites
Then all you would haft to do to fix that problem is add or subtract the radius of the ball in your collision math.

Share this post


Link to post
Share on other sites
if (ball.x == p1.x)

haha... if it moves more than 1 pixel per frame it might go straight
through...

something like this should work:

paddle on left side, ball.x center, and radius... well it''s the
ball''s radius.

if (ball.x - radius < p1.x)

and for the right one:

if (ball.x + radius > p1.x)

Joakim Asplund
http://megajocke.cjb.net

Share this post


Link to post
Share on other sites
Hmm, ball.x - radius < px2 or px1 does not work.
radius = 0.2

I draw the ball with: glutSolidSphere(0.2,30,2);
Other variables:
bx - ball x position
by - ball y position
px1 - left paddle(rectangle) vertex x position
px2 - right paddle(rectangle) vertex x position
bx_speed - ball velocity....

Now, how do i calculate if the glutSolidSphere collides with the rectangle?

Share this post


Link to post
Share on other sites
Umm, there is just one paddle in the game - it moves left or right on the screen...the ball just bounces when it hits the edge of the game window...
Now, i have tried something like this:


if(((bx-radius) >= px1) && ((bx+radius) <= px1+PADDLE_SIZE)) {
do somethings
}

Why this doesn''t work for X axe collision detection..?? I mean it works in some positions but not all...

Share this post


Link to post
Share on other sites
Was it one paddle?? Moving left andr right?? I thought yo meant two paddles moving up and down =)

The plus and minus are mixed up, try using this instead:
if((bx+radius >= px1) && (bx-radius <= px1+PADDLE_SIZE)) {
do somethings
}


Joakim Asplund
http://megajocke.cjb.net

Share this post


Link to post
Share on other sites

  • 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!