1 #include <cbehave/cbehave.h>
2 
3 #include <collision/minkowski_hex.h>
4 
5 // Stubs
JoyName(const int deviceIndex)6 const char *JoyName(const int deviceIndex)
7 {
8 	UNUSED(deviceIndex);
9 	return NULL;
10 }
11 
12 
13 FEATURE(minkowski_hex, "Minkowski Hex")
14 	SCENARIO("Same position")
15 		GIVEN("two rectangles with the same position")
16 			const struct vec2 rectPos1 = svec2(3, 4);
17 			const struct vec2 rectVel1 = svec2(-4, 5);
18 			const struct vec2i rectSize1 = svec2i(1, 3);
19 			const struct vec2 rectPos2 = rectPos1;
20 			const struct vec2 rectVel2 = svec2(7, -8);
21 			const struct vec2i rectSize2 = svec2i(2, 1);
22 
23 		WHEN("I check for their collision")
24 			struct vec2 collide1, collide2, normal;
25 			const bool result = MinkowskiHexCollide(
26 				rectPos1, rectVel1, rectSize1,
27 				rectPos2, rectVel2, rectSize2,
28 				&collide1, &collide2, &normal);
29 
30 		THEN("the result should be true");
31 			SHOULD_BE_TRUE(result);
32 		AND("the collision points should be at the start")
33 			SHOULD_INT_EQUAL((int)collide1.x, (int)rectPos1.x);
34 			SHOULD_INT_EQUAL((int)collide1.y, (int)rectPos1.y);
35 			SHOULD_INT_EQUAL((int)collide2.x, (int)rectPos2.x);
36 			SHOULD_INT_EQUAL((int)collide2.y, (int)rectPos2.y);
37 		AND("the normal should be (0, 0)")
38 			SHOULD_INT_EQUAL((int)normal.x, 0);
39 			SHOULD_INT_EQUAL((int)normal.y, 0);
40 	SCENARIO_END
41 
42 	SCENARIO("No overlap, no movement")
43 		GIVEN("two non-overlapping rectangles with no movement")
44 			const struct vec2 rectPos1 = svec2(3, 4);
45 			const struct vec2 rectVel1 = svec2_zero();
46 			const struct vec2i rectSize1 = svec2i(1, 3);
47 			const struct vec2 rectPos2 = svec2(8, 9);
48 			const struct vec2 rectVel2 = svec2_zero();
49 			const struct vec2i rectSize2 = svec2i(2, 1);
50 
51 		WHEN("I check for their collision")
52 			struct vec2 collide1, collide2, normal;
53 			const bool result = MinkowskiHexCollide(
54 				rectPos1, rectVel1, rectSize1,
55 				rectPos2, rectVel2, rectSize2,
56 				&collide1, &collide2, &normal);
57 
58 		THEN("the result should be false");
59 			SHOULD_BE_FALSE(result);
60 	SCENARIO_END
61 
62 	SCENARIO("Single axis single movement")
63 		GIVEN("two rectangles, one moving into the other")
64 			const struct vec2 rectPos1 = svec2_zero();
65 			const struct vec2 rectVel1 = svec2(10, 0);
66 			const struct vec2i rectSize1 = svec2i(2, 2);
67 			const struct vec2 rectPos2 = svec2(5, 0);
68 			const struct vec2 rectVel2 = svec2_zero();
69 			const struct vec2i rectSize2 = svec2i(2, 2);
70 
71 		WHEN("I check for their collision")
72 			struct vec2 collide1, collide2, normal;
73 			const bool result = MinkowskiHexCollide(
74 				rectPos1, rectVel1, rectSize1,
75 				rectPos2, rectVel2, rectSize2,
76 				&collide1, &collide2, &normal);
77 
78 		THEN("the result should be true");
79 			SHOULD_BE_TRUE(result);
80 		AND("the collision point for the moving rectangle should not overlap "
81 			"with the stationary rectangle")
82 			SHOULD_INT_EQUAL((int)collide1.x, 3);
83 			SHOULD_INT_EQUAL((int)collide1.y, 0);
84 		AND("the collision point for the stationary rectangle should be the "
85 			"same as its position")
86 			SHOULD_INT_EQUAL((int)collide2.x, (int)rectPos2.x);
87 			SHOULD_INT_EQUAL((int)collide2.y, (int)rectPos2.y);
88 		AND("the normal should be (-1, 0)")
89 			SHOULD_INT_EQUAL((int)normal.x, -1);
90 			SHOULD_INT_EQUAL((int)normal.y, 0);
91 	SCENARIO_END
92 FEATURE_END
93 
94 CBEHAVE_RUN(
95 	"minkowski_hex features are:",
96 	TEST_FEATURE(minkowski_hex)
97 )
98