Public Group

#### Archived

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

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

## 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 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 on other sites
That would work if the ball is moved in 1-pixel increments, and the ball is a point...

Joakim Asplund
http://megajocke.cjb.net

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

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

and for the right one:

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

Joakim Asplund
http://megajocke.cjb.net

##### Share on other sites
Hmm, ball.x - radius < px2 or px1 does not work.

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

do somethings
}

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

##### 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:
do somethings
}

Joakim Asplund
http://megajocke.cjb.net

1. 1
2. 2
Rutin
22
3. 3
JoeJ
18
4. 4
5. 5

• 15
• 40
• 23
• 13
• 13
• ### Forum Statistics

• Total Topics
631724
• Total Posts
3001903
×