//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.
Pong Colision
I am working on my first game, pong. And I need help with colision detection. Here is my colision detection function...
instead try something like this:
~guyaton
#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
The way I did it was as follows (pseudocode):
The whole top-left corner thing assumes you're using SDL, but the principles are the same.
HTH,
ukdeveloper.
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.
that is what I have...
Breakdown of my if()
**EDIT**
I got it working, my final colision checking function.
All I added was for the Y checking, I put a = after the < > oporaters
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
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement