Public Group

# Pong Colision

This topic is 4892 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I am working on my first game, pong. And I need help with colision detection. Here is my colision detection function...
//paddle1 check
void checkPaddleColision(int x,int y,int p1x,int p1y,int p2x,int p2y,int& dir){
//P1 colision
//If ball x == paddle1x and

if(x==p1x+6 && y > p1y && y < p1y+28){
dir = 2;
}
if(x==p2x && y > p2y && y < p2y+28){
dir = 1;
}

}
but its not working.

##### Share on other sites

#define PADDLE1_WIDTH 30#define PADDLE1_HIGHT 10#define PADDLE1_HALFWIDTH  ( PADDLE1_WIDTH/2 )#define PADDLE1_HALFHEIGHT ( PADDLE1_HIGHT/2 )#define BALL_WIDTH 30#define BALL_HIGHT 10#define BALL_HALFWIDTH  ( BALL_WIDTH/2 )#define BALL_HALFHEIGHT ( BALL_HIGHT/2 )//assuming that these coords are the center positionsbool CheckPaddleCollision( float x, float y, float paddlex, float paddley, int &dir ){    if( (x + paddlex) > (BALL_HALFWIDTH + PADDLE1_HALFWIDTH) )    {        return false;    }    if( (y + paddley) > (BALL_HALFHEIGHT + PADDLE1_HALFHEIGHT) )    {        return false;    }    if( (y + paddley) > (x + paddlex) )    {        dir = 1;    }    else    {        dir = 2;    }}

~guyaton

##### Share on other sites
The way I did it was as follows (pseudocode):
If ball's top-left x-coordinate is between (paddle's top-left x coordinate) and (paddle's top-left x-coordinate + the paddle's width){if ball's top left y-coordinate is between (paddle's top-left y-coordinate) and (paddle's top-left y-coordinate + paddle height)            {COLLISION OCCURS            }}

The whole top-left corner thing assumes you're using SDL, but the principles are the same.

HTH,

ukdeveloper.

##### Share on other sites
that is what I have...
Breakdown of my if()
	x==px && //if  the ball's x == paddle x and         y > py && //if the ball's y > paddle y (from the top of the paddle down) and        y < py+28 //if the ball's y < paddle y +28(paddle height) so between paddleY and paddley +28.

**EDIT**

I got it working, my final colision checking function.

void checkPaddleColision(int x,int y,int p1x,int p1y,int p2x,int p2y,int& dir){	//P1 colision	if(x==p1x+6 && y >= p1y && y <= p1y+28){		dir = 2;	}	if(x==p2x-6 && y >= p2y && y <= p2y+28){		dir = 1;	}		}

All I added was for the Y checking, I put a = after the < > oporaters

1. 1
2. 2
Rutin
20
3. 3
khawk
16
4. 4
A4L
14
5. 5

• 11
• 16
• 26
• 10
• 11
• ### Forum Statistics

• Total Topics
633755
• Total Posts
3013706
×