Added radius compensation to collision detection with paddle, within updateBall function
This commit is contained in:
parent
96b2380036
commit
a3017b0774
@ -290,9 +290,9 @@ void updateBall(Uint32 tickrate, ball *theBall, paddle *thePaddle, int winWidth,
|
||||
double paddleRX = thePaddle->x - (double)(winWidth / 2);
|
||||
double paddleRY = 20 - (double)(winHeight / 2);
|
||||
|
||||
if (ballNX > paddleRX && ballNY<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)
|
||||
ballNY > (paddleRY-thePaddle->height)) { // ...and above the bottom of the paddle
|
||||
if (ballNX+theBall->radius > paddleRX && ballNY-theBall->radius < paddleRY && // If to the right and below the top left corner of 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 (all now compensating for ball radius)
|
||||
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
|
||||
}
|
||||
|
||||
@ -2,3 +2,4 @@ breakout_s = breakout.c
|
||||
breakout_c = breakout
|
||||
all: breakout.c
|
||||
clang $(breakout_s) -l SDL2 -lGLU -lGL -o $(breakout_c)
|
||||
./breakout
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user