diff --git a/engine/AABB.c b/engine/AABB.c new file mode 100644 index 0000000..01edd1e --- /dev/null +++ b/engine/AABB.c @@ -0,0 +1,46 @@ +#include "AABB.h" + +bool find_1D_overlap(const Vector2 l1, const Vector2 l2, float* overlap) +{ + // No Overlap + if (l1.y < l2.x || l2.y < l1.x) return false; + + if (l1.x >= l2.x && l1.y <= l2.y) + { + // Complete Overlap, not sure what to do tbh + *overlap = l2.y-l2.x + l1.y-l1.x; + } + else + { + //Partial overlap + // x is p1, y is p2 + *overlap = (l2.y >= l1.y)? l2.x - l1.y : l2.y - l1.x; + } + if (fabs(*overlap) < 0.01) // Use 2 dp precision + { + *overlap = 0; + return false; + } + return true; +} + +bool find_AABB_overlap(const Vector2 tl1, const Vector2 sz1, const Vector2 tl2, const Vector2 sz2, Vector2 * const overlap) +{ + // Note that we include one extra pixel for checking + // This avoid overlapping on the border + Vector2 l1, l2; + bool overlap_x, overlap_y; + l1.x = tl1.x; + l1.y = tl1.x + sz1.x; + l2.x = tl2.x; + l2.y = tl2.x + sz2.x; + + overlap_x = find_1D_overlap(l1, l2, &overlap->x); + l1.x = tl1.y; + l1.y = tl1.y + sz1.y; + l2.x = tl2.y; + l2.y = tl2.y + sz2.y; + overlap_y = find_1D_overlap(l1, l2, &overlap->y); + + return overlap_x && overlap_y; +} diff --git a/engine/AABB.h b/engine/AABB.h new file mode 100644 index 0000000..085cdf4 --- /dev/null +++ b/engine/AABB.h @@ -0,0 +1,7 @@ +#ifndef __AABB_H +#define __AABB_H +#include "raylib.h" +#include "raymath.h" +bool find_1D_overlap(const Vector2 l1, const Vector2 l2, float* overlap); +bool find_AABB_overlap(const Vector2 tl1, const Vector2 sz1, const Vector2 tl2, const Vector2 sz2, Vector2 * const overlap); +#endif // __AABB_H diff --git a/engine/CMakeLists.txt b/engine/CMakeLists.txt index 45d7943..d2e8283 100644 --- a/engine/CMakeLists.txt +++ b/engine/CMakeLists.txt @@ -3,6 +3,7 @@ add_library(lib_scenes STATIC scene.c scene_impl.c game_systems.c + AABB.c ) target_include_directories(lib_scenes PUBLIC diff --git a/engine/game_systems.c b/engine/game_systems.c index ce68682..b4287fc 100644 --- a/engine/game_systems.c +++ b/engine/game_systems.c @@ -1,7 +1,6 @@ #include "game_systems.h" +#include "AABB.h" #include "constants.h" -#include "raylib.h" -#include "raymath.h" static const Vector2 TILE_SZ = {TILE_SIZE, TILE_SIZE}; static const Vector2 GRAVITY = {0, GRAV_ACCEL}; @@ -26,51 +25,6 @@ static inline unsigned int get_tile_idx(int x, int y, unsigned int tilemap_width return tile_y * tilemap_width + tile_x; } -static bool find_1D_overlap(const Vector2 l1, const Vector2 l2, float* overlap) -{ - // No Overlap - if (l1.y < l2.x || l2.y < l1.x) return false; - - if (l1.x >= l2.x && l1.y <= l2.y) - { - // Complete Overlap, not sure what to do tbh - *overlap = l2.y-l2.x + l1.y-l1.x; - } - else - { - //Partial overlap - // x is p1, y is p2 - *overlap = (l2.y >= l1.y)? l2.x - l1.y : l2.y - l1.x; - } - if (fabs(*overlap) < 0.01) // Use 2 dp precision - { - *overlap = 0; - return false; - } - return true; -} - -static bool find_AABB_overlap(const Vector2 tl1, const Vector2 sz1, const Vector2 tl2, const Vector2 sz2, Vector2 * const overlap) -{ - // Note that we include one extra pixel for checking - // This avoid overlapping on the border - Vector2 l1, l2; - bool overlap_x, overlap_y; - l1.x = tl1.x; - l1.y = tl1.x + sz1.x; - l2.x = tl2.x; - l2.y = tl2.x + sz2.x; - - overlap_x = find_1D_overlap(l1, l2, &overlap->x); - l1.x = tl1.y; - l1.y = tl1.y + sz1.y; - l2.x = tl2.y; - l2.y = tl2.y + sz2.y; - overlap_y = find_1D_overlap(l1, l2, &overlap->y); - - return overlap_x && overlap_y; -} - static bool check_collision_at(Vector2 pos, Vector2 bbox_sz, TileGrid_t* grid, Vector2 point) { Vector2 new_pos = Vector2Add(pos, point);