summaryrefslogtreecommitdiff
path: root/tests/Collision
diff options
context:
space:
mode:
Diffstat (limited to 'tests/Collision')
-rw-r--r--tests/Collision/CollisionTest.php364
1 files changed, 364 insertions, 0 deletions
diff --git a/tests/Collision/CollisionTest.php b/tests/Collision/CollisionTest.php
new file mode 100644
index 0000000..d6af549
--- /dev/null
+++ b/tests/Collision/CollisionTest.php
@@ -0,0 +1,364 @@
+<?php
+
+namespace Tests\Collision;
+
+use PHPUnit\Framework\TestCase;
+use App\Collision\Collision;
+
+class CollisionTest extends TestCase
+{
+ /**
+ * @dataProvider circleRectangleData
+ */
+ public function testCircleRectangle(int $c_radius, int $c_x, int $c_y, int $r_w, int $r_h, int $r_x, int $r_y, bool $expected)
+ {
+ $result = Collision::circleRectangle($c_radius, $c_x, $c_y, $r_w, $r_h, $r_x, $r_y);
+
+ $this->assertEquals($expected, $result);
+ }
+
+ public function circleRectangleData()
+ {
+ return [
+ [
+ 1, // circle radius
+ 0, // circle x
+ 0, // circle y
+ 1, // rectangle width
+ 1, // rectangle height
+ 100, // rectangle x
+ 100, // rectangle y,
+ false // expected collison
+ ],
+ [
+ 10, // circle radius
+ 250, // circle x
+ 250, // circle y
+ 200, // rectangle width
+ 200, // rectangle height
+ 0, // rectangle x
+ 0, // rectangle y,
+ false // expected collison
+ ],
+ [
+ 10, // circle radius
+ 10, // circle x
+ 10, // circle y
+ 10, // rectangle width
+ 10, // rectangle height
+ 0, // rectangle x
+ 0, // rectangle y,
+ true // expected collison
+ ],
+ [
+ 10, // circle radius
+ 200, // circle x
+ 50, // circle y
+ 200, // rectangle width
+ 200, // rectangle height
+ 0, // rectangle x
+ 0, // rectangle y,
+ true // expected collison
+ ],
+ ];
+ }
+
+ /**
+ * @dataProvider circleWorldData
+ */
+ public function testCircleWorld(int $c_radius, int $c_x, int $c_y, int $w_w, int $w_h, int $w_x, int $w_y, bool $expected)
+ {
+ $result = Collision::circleWorld($c_radius, $c_x, $c_y, $w_w, $w_h, $w_x, $w_y);
+
+ $this->assertEquals($expected, $result);
+ }
+
+ public function circleWorldData()
+ {
+ return [
+ [ // inside
+ 1, // circle radius
+ 50, // circle x
+ 50, // circle y
+ 100, // world width
+ 100, // world height
+ 0, // world x
+ 0, // world y,
+ false // expected collison
+ ],
+ [ // outside to left
+ 1, // circle radius
+ -100, // circle x
+ 50, // circle y
+ 100, // world width
+ 100, // world height
+ 0, // world x
+ 0, // world y,
+ true // expected collison
+ ],
+ [ // clipping left
+ 1, // circle radius
+ 0, // circle x
+ 50, // circle y
+ 100, // world width
+ 100, // world height
+ 0, // world x
+ 0, // world y,
+ true // expected collison
+ ],
+ [ // outside to right
+ 1, // circle radius
+ 200, // circle x
+ 50, // circle y
+ 100, // world width
+ 100, // world height
+ 0, // world x
+ 0, // world y,
+ true // expected collison
+ ],
+ [ // clipping right
+ 1, // circle radius
+ 100, // circle x
+ 50, // circle y
+ 100, // world width
+ 100, // world height
+ 0, // world x
+ 0, // world y,
+ true // expected collison
+ ],
+ [ // outside to top
+ 1, // circle radius
+ 50, // circle x
+ -100, // circle y
+ 100, // world width
+ 100, // world height
+ 0, // world x
+ 0, // world y,
+ true // expected collison
+ ],
+ [ // clipping top
+ 1, // circle radius
+ 50, // circle x
+ 0, // circle y
+ 100, // world width
+ 100, // world height
+ 0, // world x
+ 0, // world y,
+ true // expected collison
+ ],
+ [ // outside to bottom
+ 1, // circle radius
+ 50, // circle x
+ 200, // circle y
+ 100, // world width
+ 100, // world height
+ 0, // world x
+ 0, // world y,
+ true // expected collison
+ ],
+ [ // clipping bottom
+ 1, // circle radius
+ 50, // circle x
+ 100, // circle y
+ 100, // world width
+ 100, // world height
+ 0, // world x
+ 0, // world y,
+ true // expected collison
+ ],
+ ];
+ }
+
+ /**
+ * @dataProvider circleOutsideRectangleData
+ */
+ public function testCircleOutsideRectangle(int $c_radius, int $c_x, int $c_y, int $w_w, int $w_h, int $w_x, int $w_y, bool $expected)
+ {
+ $result = Collision::circleOutsideRectangle($c_radius, $c_x, $c_y, $w_w, $w_h, $w_x, $w_y);
+
+ $this->assertEquals($expected, $result);
+ }
+
+ public function circleOutsideRectangleData()
+ {
+ return [
+ [ // inside
+ 1, // circle radius
+ 50, // circle x
+ 50, // circle y
+ 100, // world width
+ 100, // world height
+ 0, // world x
+ 0, // world y,
+ false // expected collison
+ ],
+ [ // outside to left
+ 1, // circle radius
+ -100, // circle x
+ 50, // circle y
+ 100, // world width
+ 100, // world height
+ 0, // world x
+ 0, // world y,
+ true // expected collison
+ ],
+ [ // clipping left
+ 1, // circle radius
+ 0, // circle x
+ 50, // circle y
+ 100, // world width
+ 100, // world height
+ 0, // world x
+ 0, // world y,
+ false // expected collison
+ ],
+ [ // outside to right
+ 1, // circle radius
+ 200, // circle x
+ 50, // circle y
+ 100, // world width
+ 100, // world height
+ 0, // world x
+ 0, // world y,
+ true // expected collison
+ ],
+ [ // clipping right
+ 1, // circle radius
+ 100, // circle x
+ 50, // circle y
+ 100, // world width
+ 100, // world height
+ 0, // world x
+ 0, // world y,
+ false // expected collison
+ ],
+ [ // outside to top
+ 1, // circle radius
+ 50, // circle x
+ -100, // circle y
+ 100, // world width
+ 100, // world height
+ 0, // world x
+ 0, // world y,
+ true // expected collison
+ ],
+ [ // clipping top
+ 1, // circle radius
+ 50, // circle x
+ 0, // circle y
+ 100, // world width
+ 100, // world height
+ 0, // world x
+ 0, // world y,
+ false // expected collison
+ ],
+ [ // outside to bottom
+ 1, // circle radius
+ 50, // circle x
+ 200, // circle y
+ 100, // world width
+ 100, // world height
+ 0, // world x
+ 0, // world y,
+ true // expected collison
+ ],
+ [ // clipping bottom
+ 1, // circle radius
+ 50, // circle x
+ 100, // circle y
+ 100, // world width
+ 100, // world height
+ 0, // world x
+ 0, // world y,
+ false // expected collison
+ ],
+
+ ];
+ }
+
+ /**
+ * @dataProvider lineCircleData
+ */
+ public function testLineCircle(int $x_1, int $x_2, int $y_1, int $y_2, int $c_x, int $c_y, int $c_radius, bool $expected)
+ {
+ $result = Collision::lineCircle($x_1, $x_2, $y_1, $y_2, $c_x, $c_y, $c_radius);
+
+ $this->assertEquals($expected, $result);
+ }
+
+ public function lineCircleData()
+ {
+ return [
+ [0, 0, 0, 10, 0, 0, 1, true],
+ [0, 0, 0, 10, 0, 4, 1, true],
+ [0, 0, 0, 10, 4, 0, 1, false],
+ [0, 10, 0, 10, 0, 0, 1, true],
+ [0, 10, 0, 10, 5, 5, 1, true],
+ [0, 10, 0, 10, 11, 11, 1, false],
+ [0, 10, 0, 10, 12, 12, 1, false],
+ ];
+ }
+
+ /**
+ * @dataProvider linePointData
+ */
+ public function testLinePoint(int $x_1, int $x_2, int $y_1, int $y_2, int $p_x, int $p_y, bool $expected)
+ {
+ $result = Collision::linePoint($x_1, $x_2, $y_1, $y_2, $p_x, $p_y);
+
+ $this->assertEquals($expected, $result);
+ }
+
+ public function linePointData()
+ {
+ return [
+ [0, 0, 0, 10, 0, 0, true],
+ [0, 0, 0, 10, 0, 4, true],
+ [0, 0, 0, 10, 4, 0, false],
+ [0, 10, 0, 10, 0, 0, true],
+ [0, 10, 0, 10, 5, 5, true],
+ [0, 10, 0, 10, 11, 11, false],
+ ];
+ }
+
+ /**
+ * @dataProvider pointCircleData
+ */
+ public function testPointCirlce(int $p_x, int $p_y, int $c_x, int $c_y, int $c_radius, bool $expected)
+ {
+ $result = Collision::pointCircle($p_x, $p_y, $c_x, $c_y, $c_radius);
+
+ $this->assertEquals($expected, $result);
+ }
+
+ public function pointCircleData()
+ {
+ return [
+ [0, 0, 0, 0, 1, true],
+ [0, 0, 10, 10, 1, false],
+ [0, 0, 5, 5, 11, true],
+ [0, 0, 4, 0, 1, false],
+ [0, 10, 4, 0, 1, false],
+ ];
+ }
+
+ /**
+ * @dataProvider distanceData
+ */
+ public function testDistance(int $x_1, int $x_2, int $y_1, int $y_2, float $expected)
+ {
+ $distance = Collision::distance($x_1, $x_2, $y_1, $y_2);
+
+ $this->assertEquals($expected, $distance);
+ }
+
+ public function distanceData()
+ {
+ return [
+ [1, 1, 1, 1, 0],
+ [0, 1, 0, 1, 1.41],
+ [0, 2, 1, 3, 2.83],
+ [0, 13, 0, 3, 13.34],
+ ];
+ }
+}