aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/common/math.hpp2
-rw-r--r--src/games/breakout/Breakout.cpp25
-rw-r--r--src/games/breakout/Breakout.hpp4
3 files changed, 26 insertions, 5 deletions
diff --git a/src/common/math.hpp b/src/common/math.hpp
index 48a5869..cad4606 100644
--- a/src/common/math.hpp
+++ b/src/common/math.hpp
@@ -60,7 +60,7 @@ struct Color {
inline bool
-Intersect_Rectangle_Circle(Rectangle rect, Circle circle)
+Intersect_AABB_Circle(Rectangle rect, Circle circle)
{
float xmin = std::min(rect.x0, rect.x1);
float xmax = std::max(rect.x0, rect.x1);
diff --git a/src/games/breakout/Breakout.cpp b/src/games/breakout/Breakout.cpp
index 62ee17c..38d356c 100644
--- a/src/games/breakout/Breakout.cpp
+++ b/src/games/breakout/Breakout.cpp
@@ -3,6 +3,7 @@
#include "games/Game.hpp"
#include "common/shapes.hpp"
#include "renderer/Renderer.hpp"
+#include <cmath>
void
@@ -17,7 +18,7 @@ Breakout::Start()
m_ball.circle.y = 2.0f;
m_ball.circle.r = 0.05f;
m_ball.dx = 0.0f;
- m_ball.dy = 1.0f;
+ m_ball.dy = BALL_SPEED;
float brickmap_w = 1.00f * MAP_WIDTH;
@@ -126,14 +127,32 @@ Breakout::MoveBall(float dt)
}
// collision paddle
+ // Todo: Handle collisions on the side of the paddle near the bottom.
+ // Those should end up in game over somehow.
Rectangle paddle_rect = {
m_paddle.x,
0.0f,
m_paddle.x + PADDLE_WIDTH,
PADDLE_HEIGHT
};
- if (Intersect_Rectangle_Circle(paddle_rect, m_ball.circle)) {
- m_ball.dy = std::abs(m_ball.dy);
+ if (Intersect_AABB_Circle(paddle_rect, m_ball.circle)) {
+ // Todo: find a better name than 'percent' (which represents [0.0, 1.0] here instead of the expected [0, 100]).
+ float rect_half_width = (paddle_rect.x1 - paddle_rect.x0) / 2;
+ float rect_center_x = paddle_rect.x0 + rect_half_width;
+ float dist_x = m_ball.circle.x - rect_center_x;
+ float dist_x_percent = dist_x / rect_half_width;
+
+ float max_dx_percent = 0.7f;
+ float dx_percent = dist_x_percent * max_dx_percent;
+ float dy_percent = 1.0f - dx_percent;
+
+ float length = std::sqrt(dx_percent*dx_percent + dy_percent*dy_percent);
+ float dx = BALL_SPEED * (dx_percent / length);
+ float dy = BALL_SPEED * (dy_percent / length);
+
+ m_ball.circle.y += paddle_rect.y1 -(m_ball.circle.y - m_ball.circle.r);
+ m_ball.dx = dx;
+ m_ball.dy = dy;
}
}
diff --git a/src/games/breakout/Breakout.hpp b/src/games/breakout/Breakout.hpp
index 2b7c9b3..60cd648 100644
--- a/src/games/breakout/Breakout.hpp
+++ b/src/games/breakout/Breakout.hpp
@@ -22,8 +22,10 @@ class Breakout : public Game {
static constexpr uint32_t BRICK_ROWS = 8;
static constexpr uint32_t BRICK_COLS = 14;
- static constexpr float PADDLE_HEIGHT = 0.1f;
+ static constexpr float BALL_SPEED = 2.0f;
+
static constexpr float PADDLE_WIDTH = 0.6f;
+ static constexpr float PADDLE_HEIGHT = 0.1f;
static constexpr float PADDLE_SPEED = 1.0f;