Add test for AABB collision functions
Internal Changelog: - Fix some off-by-one errorsscene_man
parent
ca621bf798
commit
d455cbc6b9
|
@ -0,0 +1 @@
|
|||
ctest --rerun-failed --output-on-failure --test-dir build/tests
|
|
@ -14,7 +14,7 @@ uint8_t find_1D_overlap(Vector2 l1, Vector2 l2, float* overlap)
|
|||
}
|
||||
//Partial overlap
|
||||
// x is p1, y is p2
|
||||
*overlap = (l2.y >= l1.y)? l2.x - l1.y : l2.y - l1.x;
|
||||
*overlap = (l2.y >= l1.y)? l2.x - l1.y - 1 : l2.y - l1.x + 1;
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
@ -25,15 +25,15 @@ uint8_t find_AABB_overlap(const Vector2 tl1, const Vector2 sz1, const Vector2 tl
|
|||
Vector2 l1, l2;
|
||||
uint8_t overlap_x, overlap_y;
|
||||
l1.x = tl1.x;
|
||||
l1.y = tl1.x + sz1.x;
|
||||
l1.y = tl1.x + sz1.x - 1;
|
||||
l2.x = tl2.x;
|
||||
l2.y = tl2.x + sz2.x;
|
||||
l2.y = tl2.x + sz2.x - 1;
|
||||
|
||||
overlap_x = find_1D_overlap(l1, l2, &overlap->x);
|
||||
l1.x = tl1.y;
|
||||
l1.y = tl1.y + sz1.y;
|
||||
l1.y = tl1.y + sz1.y - 1;
|
||||
l2.x = tl2.y;
|
||||
l2.y = tl2.y + sz2.y;
|
||||
l2.y = tl2.y + sz2.y - 1;
|
||||
overlap_y = find_1D_overlap(l1, l2, &overlap->y);
|
||||
|
||||
if (overlap_x == 2 && overlap_y == 2) return 2;
|
||||
|
@ -43,8 +43,8 @@ uint8_t find_AABB_overlap(const Vector2 tl1, const Vector2 sz1, const Vector2 tl
|
|||
bool point_in_AABB(Vector2 point, Rectangle box)
|
||||
{
|
||||
return (
|
||||
point.x > box.x
|
||||
&& point.y > box.y
|
||||
point.x >= box.x
|
||||
&& point.y >= box.y
|
||||
&& point.x < box.x + box.width
|
||||
&& point.y < box.y + box.height
|
||||
);
|
||||
|
|
|
@ -6,19 +6,86 @@
|
|||
#include <setjmp.h>
|
||||
#include <cmocka.h>
|
||||
|
||||
static void test_no_overlap(void **state)
|
||||
static void test_point_AABB(void **state)
|
||||
{
|
||||
(void) state;
|
||||
|
||||
Rectangle box = {0, 0, 20, 20};
|
||||
Vector2 p = {-5, -5};
|
||||
assert_false(point_in_AABB(p, box));
|
||||
|
||||
p.x = 10;
|
||||
p.y = 10;
|
||||
assert_true(point_in_AABB(p, box));
|
||||
|
||||
p.x = 0;
|
||||
p.y = 0;
|
||||
assert_true(point_in_AABB(p, box));
|
||||
|
||||
p.x = 20;
|
||||
p.y = 20;
|
||||
assert_false(point_in_AABB(p, box));
|
||||
|
||||
}
|
||||
|
||||
static void test_AABB_overlap(void **state)
|
||||
{
|
||||
(void) state;
|
||||
Vector2 p1 = {10, 10};
|
||||
Vector2 sz1 = {10, 10};
|
||||
Vector2 p2 = {25, 25};
|
||||
Vector2 sz2 = {20, 20};
|
||||
|
||||
Vector2 overlap = {0};
|
||||
assert_int_equal(find_AABB_overlap(p1, sz1, p2, sz2, &overlap), 0);
|
||||
assert_int_equal(find_AABB_overlap(p2, sz2, p1, sz1, &overlap), 0);
|
||||
|
||||
p2.x = 10;
|
||||
p2.y = 10;
|
||||
assert_int_equal(find_AABB_overlap(p1, sz1, p2, sz2, &overlap), 2); // Smaller one is complete overlap
|
||||
assert_int_equal(find_AABB_overlap(p2, sz2, p1, sz1, &overlap), 1); // Bigger on is not
|
||||
|
||||
p2.x = 15;
|
||||
p2.y = 15;
|
||||
assert_int_equal(find_AABB_overlap(p1, sz1, p2, sz2, &overlap), 1);
|
||||
assert_float_equal(overlap.x, -5, 1e-5);
|
||||
assert_float_equal(overlap.y, -5, 1e-5);
|
||||
assert_int_equal(find_AABB_overlap(p2, sz2, p1, sz1, &overlap), 1);
|
||||
assert_float_equal(overlap.x, 5, 1e-5);
|
||||
assert_float_equal(overlap.y, 5, 1e-5);
|
||||
}
|
||||
|
||||
static void test_1D_overlap(void **state)
|
||||
{
|
||||
(void) state;
|
||||
Vector2 a = {0, 5};
|
||||
Vector2 b = {6, 9};
|
||||
float overlap = 0;
|
||||
assert_int_equal(find_1D_overlap(a, b, &overlap), 0);
|
||||
|
||||
a.y = 6;
|
||||
assert_int_equal(find_1D_overlap(a, b, &overlap), 1);
|
||||
assert_float_equal(overlap, -1, 1e-5);
|
||||
assert_int_equal(find_1D_overlap(b, a, &overlap), 1);
|
||||
assert_float_equal(overlap, 1, 1e-5);
|
||||
|
||||
a.y = 7;
|
||||
assert_int_equal(find_1D_overlap(a, b, &overlap), 1);
|
||||
assert_float_equal(overlap, -2, 1e-5);
|
||||
assert_int_equal(find_1D_overlap(b, a, &overlap), 1);
|
||||
assert_float_equal(overlap, 2, 1e-5);
|
||||
|
||||
a.x = 7;
|
||||
a.y = 9;
|
||||
assert_int_equal(find_1D_overlap(a, b, &overlap), 2);
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
const struct CMUnitTest tests[] = {
|
||||
cmocka_unit_test(test_no_overlap),
|
||||
cmocka_unit_test(test_1D_overlap),
|
||||
cmocka_unit_test(test_AABB_overlap),
|
||||
cmocka_unit_test(test_point_AABB),
|
||||
};
|
||||
|
||||
return cmocka_run_group_tests(tests, NULL, NULL);
|
||||
|
|
Loading…
Reference in New Issue