From 09ed16029c49aa4747f6334d29a2628efa8e2c65 Mon Sep 17 00:00:00 2001 From: Zack Buhman Date: Fri, 5 Dec 2025 11:56:29 -0600 Subject: [PATCH] initial collision rewrite --- Makefile | 1 + include/collision2.hpp | 5 +++++ include/state.h | 10 ++++++++++ include/update.hpp | 1 + src/collision.cpp | 26 -------------------------- src/collision2.cpp | 34 ++++++++++++++++++++++++++++++++++ src/main.c | 14 +++----------- src/render.cpp | 28 ++++++++++++++-------------- src/update.cpp | 28 +++++++++++++++++++++++++++- 9 files changed, 95 insertions(+), 52 deletions(-) create mode 100644 include/collision2.hpp create mode 100644 src/collision2.cpp diff --git a/Makefile b/Makefile index bd42a52..810db76 100644 --- a/Makefile +++ b/Makefile @@ -104,6 +104,7 @@ MAIN_OBJS = \ src/opengl.o \ src/render.o \ src/collision.o \ + src/collision2.o \ src/update.o \ src/unparse.o \ $(patsubst %.glsl,%.glsl.o,$(wildcard src/shader/*.glsl)) \ diff --git a/include/collision2.hpp b/include/collision2.hpp new file mode 100644 index 0000000..1ca4d5e --- /dev/null +++ b/include/collision2.hpp @@ -0,0 +1,5 @@ +#pragma once + +#include "math/float_types.hpp" + +bool aabb_circle_collision(vec3 aabb_position, vec3 circle_position); diff --git a/include/state.h b/include/state.h index e2510b2..98d7487 100644 --- a/include/state.h +++ b/include/state.h @@ -1,10 +1,20 @@ #pragma once +#include + #ifdef __cplusplus extern "C" { #endif + struct block_state { + float destroyed_time; + }; + struct game_state { + struct block_state blocks[28 * 13]; + const uint8_t * level; + const uint8_t * pal; + float paddle_x; float paddle_y; diff --git a/include/update.hpp b/include/update.hpp index 3f2b825..e57cf72 100644 --- a/include/update.hpp +++ b/include/update.hpp @@ -6,6 +6,7 @@ extern "C" { #endif + void reset_level(struct game_state * state); void update(struct game_state * state); #ifdef __cplusplus diff --git a/src/collision.cpp b/src/collision.cpp index 632be77..b229400 100644 --- a/src/collision.cpp +++ b/src/collision.cpp @@ -260,29 +260,3 @@ vec4 collision(mat4x4 trans, smallest_normal.z, collision ? smallest : -9999.0f); } - -/* -bool collision(float ball_x, float ball_y, - float ball_dx, float ball_dy) -{ - const uint8_t * level = (const uint8_t *)src_level_level1_data_start; - const uint8_t * pal = (const uint8_t *)src_level_level1_data_pal_start; - - for (int y = 0; y < 28; y++) { - for (int x = 0; x < 13; x++) { - char tile = level[y * 13 + x]; - if (tile == 0) - continue; - - //if ( (paddle_x + 1.0) >= ((float)x - 1.0) - //&& (paddle_x - 1.0) <= ((float)x + 1.0) - //&& (paddle_y + 1.0) >= ((float)y - 1.0) - //&& (paddle_y - 1.0) <= ((float)y + 1.0)) { - - collision_inner(translate(vec3(x, y, 0)), - vec3(ball_x, ball_y, 0), - vec3(ball_dx, ball_dy, 0)); - } - } -} -*/ diff --git a/src/collision2.cpp b/src/collision2.cpp new file mode 100644 index 0000000..5cd50e9 --- /dev/null +++ b/src/collision2.cpp @@ -0,0 +1,34 @@ +#include + +#include "collision2.hpp" + +static inline float clamp(float n, float min, float max) +{ + if (n < min) + return min; + if (n > max) + return max; + return n; +} + +static inline vec3 clamp(vec3 v, vec3 min_v, vec3 max_v) +{ + return vec3(clamp(v.x, min_v.x, max_v.x), + clamp(v.y, min_v.y, max_v.y), + clamp(v.z, min_v.z, max_v.z)); +} + +bool aabb_circle_collision(vec3 aabb_position, vec3 circle_position) +{ + const vec3 bounds = vec3(2, 1, 0); + + vec3 distance = circle_position - aabb_position; + vec3 closest_point_pd = clamp(distance, -bounds, bounds); + vec3 closest_point_p = aabb_position + closest_point_pd; + + bool collided = fabsf(magnitude(closest_point_p - circle_position)) < 1; + if (!collided) + return collided; + + return collided; +} diff --git a/src/main.c b/src/main.c index 5dfbe22..86b30cc 100644 --- a/src/main.c +++ b/src/main.c @@ -164,15 +164,7 @@ int main() const char * last_gamepad_name = NULL; struct game_state state; - state.paddle_x = 0.0; - state.paddle_y = 26.0; - - state.ball_x = 0.0; - state.ball_y = 25.0; - - state.ball_dx = 0.1; - state.ball_dy = 0.1; - + reset_level(&state); state.start_time = glfwGetTime(); while(!glfwWindowShouldClose(window)) { @@ -204,8 +196,8 @@ int main() float sensitivity = 0.4f; paddle_dx = (right - left) * sensitivity; - //state.ball_dx = deadzone(axes[0]); - //state.ball_dy = deadzone(axes[1]); + state.ball_dx = deadzone(axes[0]); + state.ball_dy = deadzone(axes[1]); break; } } diff --git a/src/render.cpp b/src/render.cpp index cd7994a..6205e1c 100644 --- a/src/render.cpp +++ b/src/render.cpp @@ -6,13 +6,11 @@ #include "unparse.h" #include "collision.hpp" +#include "collision2.hpp" #include "render.hpp" #include "math/float_types.hpp" #include "math/transform.hpp" -#include "level/level1.data.h" -#include "level/level1.data.pal.h" - #define PI 3.14159274101257324219f extern int vp_width; @@ -131,25 +129,27 @@ void render(mesh paddle_mesh, glEnableVertexAttribArray(attrib_texture); glEnableVertexAttribArray(attrib_normal); - assert(src_level_level1_data_size == 13 * 28); - const uint8_t * level = (const uint8_t *)src_level_level1_data_start; - const uint8_t * pal = (const uint8_t *)src_level_level1_data_pal_start; - vec3 light_pos = normalize(rotate_z(theta) * vec3(1, 1, 1)); for (int y = 0; y < 28; y++) { for (int x = 0; x < 13; x++) { - char tile = level[y * 13 + x]; - if (tile == 0) - continue; + char tile = state->level[y * 13 + x]; + //if (tile == 0) + //continue; const float cs = 1.0f / 255.0f; - vec3 base_color = vec3(((float)pal[tile * 3 + 0]) * cs, - ((float)pal[tile * 3 + 1]) * cs, - ((float)pal[tile * 3 + 2]) * cs); + vec3 base_color = vec3(((float)state->pal[tile * 3 + 0]) * cs, + ((float)state->pal[tile * 3 + 1]) * cs, + ((float)state->pal[tile * 3 + 2]) * cs); + + vec3 block_position = vec3(x * 4.0f, -y * 2.0f, 0.0f); + vec3 ball_position = vec3(state->ball_x * 4.0f, -state->ball_y * 2.0f, 0.0); + bool collided = aabb_circle_collision(block_position, ball_position); + if (collided) + base_color = vec3(1, 0, 0); mat4x4 rx = rotate_x(-PI / 2.0f); - mat4x4 t = translate(vec3(x * 4.0f, -y * 2.0f, 0.0f)); + mat4x4 t = translate(block_position); mat4x4 trans = a * t * rx; diff --git a/src/update.cpp b/src/update.cpp index d05fc25..1084b75 100644 --- a/src/update.cpp +++ b/src/update.cpp @@ -1,5 +1,6 @@ #include #include +#include #include "update.hpp" #include "collision.hpp" @@ -11,7 +12,32 @@ #include "math/float_types.hpp" #include "math/transform.hpp" +void reset_level(struct game_state * state) +{ + state->paddle_x = 0.0; + state->paddle_y = 26.0; + + state->ball_x = 0.0; + state->ball_y = 25.0; + + state->ball_dx = 0.01; + state->ball_dy = 0.01; + + state->start_time = 0.0; + + assert(src_level_level1_data_size == 13 * 28); + const uint8_t * level = (const uint8_t *)src_level_level1_data_start; + const uint8_t * pal = (const uint8_t *)src_level_level1_data_pal_start; + + state->level = level; + state->pal = pal; + for (int i = 0; i < 28 * 13; i++) { + state->blocks[i].destroyed_time = 0.0f; + } +} + void update(struct game_state * state) { - + state->ball_x += state->ball_dx; + state->ball_y += state->ball_dy; }