From 494c9e9e46d5600bc180211ac28f9a68118531e0 Mon Sep 17 00:00:00 2001 From: En Yi Date: Sun, 13 Aug 2023 12:34:27 +0800 Subject: [PATCH] Fix incorrect line test By this project convention, the line extend will start from p1 and end at p2, but not including p2 [p1, p2). Thus, existing tests are testing a zero-length line, which should assert no collision. Add test to properly test AABB edge collision with a line Also update existing collision function to account for this --- CMakeLists.txt | 2 +- scenes/engine/collisions.c | 2 +- tests/test_AABB.c | 5 +++++ 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index aab8df5..7582bb2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -111,7 +111,7 @@ target_link_libraries(assets_test ${GAME_LIBS} ) -if (UNIT_TESTING) +if (BUILD_TESTING) find_package(cmocka 1.1.0 REQUIRED) add_subdirectory(tests) endif() diff --git a/scenes/engine/collisions.c b/scenes/engine/collisions.c index 820433b..6bef940 100644 --- a/scenes/engine/collisions.c +++ b/scenes/engine/collisions.c @@ -93,7 +93,7 @@ uint8_t check_collision_line(const CollideEntity_t* ent, TileGrid_t* grid, bool unsigned int tile_y2 = (ent->area.tile_y2 >= grid->height) ? grid->height - 1 : ent->area.tile_y2; Vector2 p1 = {ent->bbox.x, ent->bbox.y}; - Vector2 p2 = {ent->bbox.x + ent->bbox.width - 1, ent->bbox.y + ent->bbox.height - 1}; + Vector2 p2 = {ent->bbox.x + ent->bbox.width, ent->bbox.y + ent->bbox.height}; for(unsigned int tile_y = tile_y1; tile_y <= tile_y2; tile_y++) { if (tile_y >= grid->height) return 0; diff --git a/tests/test_AABB.c b/tests/test_AABB.c index 3c0e391..35eda63 100644 --- a/tests/test_AABB.c +++ b/tests/test_AABB.c @@ -1,4 +1,5 @@ #include "AABB.h" +#include #include #include @@ -25,10 +26,14 @@ static void test_line_AABB(void **state) p1.y = 0; p2.y = 0; + assert_false(line_in_AABB(p1, p2, box)); + p2.y = 1; assert_true(line_in_AABB(p1, p2, box)); p1 = (Vector2){5, 0}; p2 = (Vector2){5, 10}; + assert_false(line_in_AABB(p1, p2, box)); + p2 = (Vector2){6, 10}; assert_true(line_in_AABB(p1, p2, box)); p1 = (Vector2){14, 0};