Archived

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

BadMark

Pong - Collision detection between the paddle and the ball

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