diff options
author | Fbenas <philbeansburton@gmail.com> | 2020-10-13 22:18:17 +0100 |
---|---|---|
committer | Fbenas <philbeansburton@gmail.com> | 2020-10-13 22:18:17 +0100 |
commit | 2df948ca0509b8bf58d1b7bc3a5ce0840132edd1 (patch) | |
tree | 232f2b84e5ede61c89afca8085c5eab9b65940f1 | |
parent | a44e609b27642afc77495293d975f2ea0bbf6f3e (diff) |
Fully sort detection of boids colliding
-rw-r--r-- | public/js/app.js | 19 | ||||
-rw-r--r-- | resources/js/app.js | 2 | ||||
-rw-r--r-- | resources/js/boid.js | 9 | ||||
-rw-r--r-- | resources/js/scene.js | 8 |
4 files changed, 22 insertions, 16 deletions
diff --git a/public/js/app.js b/public/js/app.js index 8ba4cf8..361321c 100644 --- a/public/js/app.js +++ b/public/js/app.js @@ -70,7 +70,7 @@ class Boid { let rayAngle = tweakAngle + this.direction + rays[i]; // Check against all other boids - if (i == 0 && this.detectBoids(rayAngle, boids)) { + if (this.detectBoids(rayAngle, boids)) { continue; } @@ -81,8 +81,9 @@ class Boid { return rayAngle; } - console.log(this.x, this.y); + console.log('cannot find suitable ray'); + return false; } detectBoids(direction, boids) { @@ -112,10 +113,12 @@ class Boid { if (intersect) { console.log('boids intersect detected'); } + + return intersect; } boidsIntersect(boid) { - this.twoCirclesIntersect(this.radius, boid.radius, this.x, boid.x, this.y, boid.y); + return this.twoCirclesIntersect(this.radius, boid.radius, this.x, boid.x, this.y, boid.y); } twoCirclesIntersect(r1, r2, x1, x2, y1, y2) { @@ -327,10 +330,10 @@ class Scene { boids.push(new Boid( this.boid_radius, "black", - 300, 300, Math.random() * 360, - // Math.random() * (this.gameArea.canvas.width - 100) + 50, - // Math.random() * (this.gameArea.canvas.height - 100) + 50, - // Math.random() * 360, + // 300, 300, Math.random() * 360, + Math.random() * (this.gameArea.canvas.width - 100) + 50, + Math.random() * (this.gameArea.canvas.height - 100) + 50, + Math.random() * 360, i, this.width, this.height @@ -388,7 +391,7 @@ class Scene { } -let scene = new Scene(5, 2); +let scene = new Scene(5, 20); function stop() { scene.stop(); diff --git a/resources/js/app.js b/resources/js/app.js index 752bfc9..47cabd6 100644 --- a/resources/js/app.js +++ b/resources/js/app.js @@ -1,4 +1,4 @@ -let scene = new Scene(5, 2); +let scene = new Scene(5, 20); function stop() { scene.stop(); diff --git a/resources/js/boid.js b/resources/js/boid.js index 85793ff..a15022a 100644 --- a/resources/js/boid.js +++ b/resources/js/boid.js @@ -70,7 +70,7 @@ class Boid { let rayAngle = tweakAngle + this.direction + rays[i]; // Check against all other boids - if (i == 0 && this.detectBoids(rayAngle, boids)) { + if (this.detectBoids(rayAngle, boids)) { continue; } @@ -81,8 +81,9 @@ class Boid { return rayAngle; } - console.log(this.x, this.y); + console.log('cannot find suitable ray'); + return false; } detectBoids(direction, boids) { @@ -112,10 +113,12 @@ class Boid { if (intersect) { console.log('boids intersect detected'); } + + return intersect; } boidsIntersect(boid) { - this.twoCirclesIntersect(this.radius, boid.radius, this.x, boid.x, this.y, boid.y); + return this.twoCirclesIntersect(this.radius, boid.radius, this.x, boid.x, this.y, boid.y); } twoCirclesIntersect(r1, r2, x1, x2, y1, y2) { diff --git a/resources/js/scene.js b/resources/js/scene.js index df456d3..770eb92 100644 --- a/resources/js/scene.js +++ b/resources/js/scene.js @@ -23,10 +23,10 @@ class Scene { boids.push(new Boid( this.boid_radius, "black", - 300, 300, Math.random() * 360, - // Math.random() * (this.gameArea.canvas.width - 100) + 50, - // Math.random() * (this.gameArea.canvas.height - 100) + 50, - // Math.random() * 360, + // 300, 300, Math.random() * 360, + Math.random() * (this.gameArea.canvas.width - 100) + 50, + Math.random() * (this.gameArea.canvas.height - 100) + 50, + Math.random() * 360, i, this.width, this.height |