Added some comments and changed X velocity post paddle-collision to be proportional to the relative position of the ball and paddle
This commit is contained in:
parent
54b9eb14f0
commit
f08b0d5b75
@ -234,24 +234,25 @@ int main(void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void updateBall(double *ballX, double *ballY, double *ballVX, double *ballVY, double paddleX, int paddleWidth, int winWidth, int winHeight) {
|
void updateBall(double *ballX, double *ballY, double *ballVX, double *ballVY, double paddleX, int paddleWidth, int winWidth, int winHeight) {
|
||||||
double ballNX = *ballX + *ballVX;
|
double ballNX = *ballX + *ballVX; // Calculates the position of the ball on the next step
|
||||||
double ballNY = *ballY + *ballVY;
|
double ballNY = *ballY + *ballVY;
|
||||||
double paddleRX = paddleX - (double)(winWidth / 2);
|
double paddleRX = paddleX - (double)(winWidth / 2);
|
||||||
double paddleRY = 20 - (double)(winHeight / 2);
|
double paddleRY = 20 - (double)(winHeight / 2);
|
||||||
|
|
||||||
if (ballNX > paddleRX && ballNY<paddleRY) {
|
if (ballNX > paddleRX && ballNY<paddleRY) { // If to the right or below the top left corner of paddle
|
||||||
if (ballNX < paddleRX + paddleWidth) {
|
if (ballNX < paddleRX + (double)paddleWidth) { // ...and to the left of the right side of the paddle (i.e. about to go inside inside the paddle)
|
||||||
*ballVY *= -1.0;
|
*ballVY *= -1.0;
|
||||||
|
*ballVX = ((ballNX - paddleRX - (double)(paddleWidth/2))/((double)paddleWidth/2))*0.5; // Sets X velocity to be proportional to the relative position of the ball and the paddle on collision
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ballNX > winWidth / 2 || ballNX < (-1.0*winWidth/2)) {
|
if (ballNX > winWidth / 2 || ballNX < (-1.0*winWidth/2)) { // Collision with walls
|
||||||
*ballVX *= -1.0;
|
*ballVX *= -1.0;
|
||||||
}
|
}
|
||||||
if (ballNY > winHeight / 2) {
|
if (ballNY > winHeight / 2) { // Collision with ceiling
|
||||||
*ballVY *= -1.0;
|
*ballVY *= -1.0;
|
||||||
}
|
}
|
||||||
if (ballNY < (-1.0*winHeight/2)) {
|
if (ballNY < (-1.0*winHeight/2)) { // Collision with floor
|
||||||
*ballX = 0;
|
*ballX = 0;
|
||||||
*ballY = 0;
|
*ballY = 0;
|
||||||
*ballVX = -0.1;
|
*ballVX = -0.1;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user