Jump to content
  • Advertisement
Sign in to follow this  
Mr_Fhqwhgads

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.

If you intended to correct an error in the post then please contact us.

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 ball y > paddle1y(top of the paddle and < paddle1y+28(28 is the height of the paddle) change direction


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


Link to post
Share on other sites
Advertisement
instead try something like this:


#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 positions
bool 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 this post


Link to post
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 this post


Link to post
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!