Pong - Collision detection between the paddle and the ball

Started by
6 comments, last by BadMark 21 years, 10 months ago
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]
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]
------------------------------------------VOTE Patrick O'GradyWrite in Presidential CandidateThe Candidate who Cares.
That would work if the ball is moved in 1-pixel increments, and the ball is a point...

Joakim Asplund
http://megajocke.cjb.net
Joakim Asplundhttp://megajocke.y2.org
Then all you would haft to do to fix that problem is add or subtract the radius of the ball in your collision math.
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
Joakim Asplundhttp://megajocke.y2.org
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?

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...
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
Joakim Asplundhttp://megajocke.y2.org

This topic is closed to new replies.

Advertisement