Added radius compensation to collision detection with paddle, within updateBall function

This commit is contained in:
Joe Adams 2017-12-14 10:05:58 +00:00
parent 96b2380036
commit a3017b0774
2 changed files with 4 additions and 3 deletions

View File

@ -290,9 +290,9 @@ void updateBall(Uint32 tickrate, ball *theBall, paddle *thePaddle, int winWidth,
double paddleRX = thePaddle->x - (double)(winWidth / 2); double paddleRX = thePaddle->x - (double)(winWidth / 2);
double paddleRY = 20 - (double)(winHeight / 2); double paddleRY = 20 - (double)(winHeight / 2);
if (ballNX > paddleRX && ballNY<paddleRY && // If to the right and below the top left corner of paddle if (ballNX+theBall->radius > paddleRX && ballNY-theBall->radius < paddleRY && // If to the right and below the top left corner of paddle
ballNX < paddleRX + (double)thePaddle->width && // ...and to the left of the right side of the paddle (i.e. about to go inside inside the paddle) ballNX-theBall->radius < paddleRX + (double)thePaddle->width && // ...and to the left of the right side of the paddle
ballNY > (paddleRY-thePaddle->height)) { // ...and above the bottom of the paddle ballNY > (paddleRY-thePaddle->height)) { // ...and above the bottom of the paddle (all now compensating for ball radius)
theBall->vY *= -1.0; theBall->vY *= -1.0;
theBall->vX = ((ballNX - paddleRX - (double)(thePaddle->width/2))/((double)thePaddle->width/2))*0.5; // Sets X velocity to be proportional to the relative position of the ball and the paddle on collision theBall->vX = ((ballNX - paddleRX - (double)(thePaddle->width/2))/((double)thePaddle->width/2))*0.5; // Sets X velocity to be proportional to the relative position of the ball and the paddle on collision
} }

View File

@ -2,3 +2,4 @@ breakout_s = breakout.c
breakout_c = breakout breakout_c = breakout
all: breakout.c all: breakout.c
clang $(breakout_s) -l SDL2 -lGLU -lGL -o $(breakout_c) clang $(breakout_s) -l SDL2 -lGLU -lGL -o $(breakout_c)
./breakout