Browse Source

Added ball physics

master
Kenneth Bruen 3 years ago
parent
commit
77426a0b63
Signed by: kbruen
GPG Key ID: C1980A470C3EE5B1
  1. 47
      src/main.cpp

47
src/main.cpp

@ -1,3 +1,4 @@
#include <iostream>
#include <functional> #include <functional>
#include <string> #include <string>
#include <cmath> #include <cmath>
@ -42,11 +43,13 @@ public:
class Ball : public Drawable { class Ball : public Drawable {
public: public:
static constexpr GLfloat defaultVeloX = 0.5;
GLfloat radius; GLfloat radius;
GLfloat x, y; GLfloat x, y;
GLfloat veloX, veloY; GLfloat veloX, veloY;
Ball(GLfloat x, GLfloat y) : x(x), y(y), radius(0.025), veloX(0.4) { Ball(GLfloat x, GLfloat y) : x(x), y(y), radius(0.025), veloX(defaultVeloX) {
} }
@ -121,7 +124,7 @@ void display(void) {
int gameTime; int gameTime;
const GLfloat paddleMoveSpeed = 0.75; const GLfloat paddleMoveSpeed = 0.8;
void idle() { void idle() {
int newTime = glutGet(GLUT_ELAPSED_TIME); int newTime = glutGet(GLUT_ELAPSED_TIME);
@ -131,6 +134,45 @@ void idle() {
if (gameRunning) { if (gameRunning) {
ball.update(); ball.update();
// Detect ball collission
if (leftPaddle.x <= (ball.x + ball.radius) && (ball.x - ball.radius) <= (leftPaddle.x + leftPaddle.width)) {
// Ball inside left-right area of left paddle
if (leftPaddle.y <= ball.y && ball.y <= leftPaddle.y + leftPaddle.height) {
// Ball inside up-down area of left paddle
ball.veloX = ball.defaultVeloX;
ball.veloY = Utils::nummap(ball.y, leftPaddle.y, leftPaddle.y + leftPaddle.height, (GLfloat)-1, (GLfloat)1);
}
}
else if (rightPaddle.x <= (ball.x + ball.radius) && (ball.x - ball.radius) <= (rightPaddle.x + rightPaddle.width)) {
// Ball inside left-right area of right paddle
if (rightPaddle.y <= ball.y && ball.y <= rightPaddle.y + rightPaddle.height) {
// Ball inside up-down area of right paddle
ball.veloX = -ball.defaultVeloX;
ball.veloY = Utils::nummap(ball.y, rightPaddle.y, rightPaddle.y + rightPaddle.height, (GLfloat)-1, (GLfloat)1);
}
}
else if (ball.x + ball.radius < -1) {
// Ball exited board on the left side
rightScore += 1;
ball.x = ball.y = 0;
gameRunning = false;
}
else if (ball.x - ball.radius > 1) {
// Ball exited board on the right side
leftScore += 1;
ball.x = ball.y = 0;
gameRunning = false;
}
else if (ball.y - ball.radius < -1) {
// Ball touched bottom side
ball.veloY *= -1;
}
else if (ball.y + ball.radius > 1) {
// Ball touched top side
ball.veloY *= -1;
}
}
// Move paddle // Move paddle
leftPaddle.y += leftMove * paddleMoveSpeed * deltaTime; leftPaddle.y += leftMove * paddleMoveSpeed * deltaTime;
rightPaddle.y += rightMove * paddleMoveSpeed * deltaTime; rightPaddle.y += rightMove * paddleMoveSpeed * deltaTime;
@ -140,7 +182,6 @@ void idle() {
if (rightPaddle.y < -1) rightPaddle.y = -1; if (rightPaddle.y < -1) rightPaddle.y = -1;
if (leftPaddle.y > 1 - leftPaddle.height) leftPaddle.y = 1 - leftPaddle.height; if (leftPaddle.y > 1 - leftPaddle.height) leftPaddle.y = 1 - leftPaddle.height;
if (rightPaddle.y > 1 - rightPaddle.height) rightPaddle.y = 1 - rightPaddle.height; if (rightPaddle.y > 1 - rightPaddle.height) rightPaddle.y = 1 - rightPaddle.height;
}
glutPostRedisplay(); glutPostRedisplay();
} }

Loading…
Cancel
Save