From 02455892dac1d52914e5702fd8e895d5cee1f083 Mon Sep 17 00:00:00 2001 From: FBeans Date: Sat, 21 May 2022 19:07:32 +0100 Subject: Add world, boid and all collision detection --- tests/Collision/CollisionTest.php | 364 ++++++++++++++++++++++++++++++++++++++ tests/World/BoidTest.php | 45 +++++ tests/World/WorldTest.php | 25 +++ tests/bootstrap.php | 3 + 4 files changed, 437 insertions(+) create mode 100644 tests/Collision/CollisionTest.php create mode 100644 tests/World/BoidTest.php create mode 100644 tests/World/WorldTest.php create mode 100644 tests/bootstrap.php (limited to 'tests') 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 @@ +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], + ]; + } +} diff --git a/tests/World/BoidTest.php b/tests/World/BoidTest.php new file mode 100644 index 0000000..1c7202d --- /dev/null +++ b/tests/World/BoidTest.php @@ -0,0 +1,45 @@ +assertEquals(5, $boid->radius()); + $this->assertEquals([1, 2], $boid->position()); + $this->assertEquals('boid', $boid->name()); + + $boid = new Boid(5, 1, 2); + $this->assertEquals(5, $boid->radius()); + $this->assertEquals([1, 2], $boid->position()); + $this->assertTrue(is_string($boid->name())); + $this->assertTrue(strpos($boid->name(), 'boid') === 0); + } + + public function testBoidWorldCollision() + { + $world = new World(200, 200); + + $boid = new Boid(5, 10, 10); + $this->assertEquals(false, $boid->isCollisionWithWorld($world->width(), $world->height(), 0, 0)); + + $boid = new Boid(5, 0, 0); + $this->assertEquals(true, $boid->isCollisionWithWorld($world->width(), $world->height(), 0, 0)); + + $boid = new Boid(5, -100, 100); + $this->assertEquals(true, $boid->isCollisionWithWorld($world->width(), $world->height(), 0, 0)); + + $boid = new Boid(5, 100, -100); + $this->assertEquals(true, $boid->isCollisionWithWorld($world->width(), $world->height(), 0, 0)); + + $boid = new Boid(5, 200, 200); + $this->assertEquals(true, $boid->isCollisionWithWorld($world->width(), $world->height(), 0, 0)); + } +} diff --git a/tests/World/WorldTest.php b/tests/World/WorldTest.php new file mode 100644 index 0000000..c2b117e --- /dev/null +++ b/tests/World/WorldTest.php @@ -0,0 +1,25 @@ +assertEquals('test-world', $world->name()); + $this->assertEquals(1, $world->width()); + $this->assertEquals(2, $world->height()); + + $world = new World(1, 2); + + $this->assertTrue(is_string($world->name())); + $this->assertTrue(strpos($world->name(), 'world') === 0); + $this->assertEquals(1, $world->width()); + $this->assertEquals(2, $world->height()); + } +} diff --git a/tests/bootstrap.php b/tests/bootstrap.php new file mode 100644 index 0000000..991ea43 --- /dev/null +++ b/tests/bootstrap.php @@ -0,0 +1,3 @@ +