diff options
author | Fbenas <philbeansburton@gmail.com> | 2020-10-13 19:58:47 +0100 |
---|---|---|
committer | Fbenas <philbeansburton@gmail.com> | 2020-10-13 19:58:47 +0100 |
commit | 1df97b3b9d589954d23a02fc2ca2cb5d87133e26 (patch) | |
tree | a269920a273ff16db4e3576443d8a912bc92ca3a /resources/js/boid.js | |
parent | a4908f150053316383fbfaee0477e7b4123ce3ca (diff) |
More refactoring and fixing collisions
Diffstat (limited to 'resources/js/boid.js')
-rw-r--r-- | resources/js/boid.js | 90 |
1 files changed, 38 insertions, 52 deletions
diff --git a/resources/js/boid.js b/resources/js/boid.js index 6075cc2..2ae692d 100644 --- a/resources/js/boid.js +++ b/resources/js/boid.js @@ -1,31 +1,34 @@ class Boid { - constructor(context, radius, color, x, y, direction, id) { - this.rayLength = 40; - this.turnStepAmount = 20; - this.stepAmount = 4; + constructor(radius, color, x, y, direction, id, scene_width, scene_height) { + // Boid properties this.radius = radius; + this.color = color; this.x = x; this.y = y; this.direction = direction; - this.color = color; - this.fieldOfView = 270; this.id = id; - this.boidBuffer = 20; - this.update(context); + // World properties + this.scene_width = scene_width; + this.scene_height = scene_height; + + // Boid "dynamic" properties + this.rayLength = 40; + this.turnStepAmount = 20; + this.stepAmount = 4; + this.fieldOfView = 270; + this.boidBuffer = 20; } - move(context, boids) { + move(boids) { // this.direction += Math.random() * (2 * this.turnStepAmount) - this.turnStepAmount - this.direction = this.findNextRay(context, boids); + this.direction = this.findNextRay(boids); var vector = this.findPoint(this.x, this.y, this.stepAmount, this.direction); this.x = vector.x; this.y = vector.y; - - this.update(context); } buildRays() { @@ -43,7 +46,7 @@ class Boid { return rays; } - findNextRay(context, boids) { + findNextRay(boids) { let rays = this.buildRays(); for (let i = 0; i < rays.length; i++) { @@ -54,11 +57,11 @@ class Boid { let rayAngle = tweakAngle + this.direction + rays[i]; - if (i == 0 && this.detectBoid(context, rayAngle, boids)) { + if (i == 0 && this.detectBoid(rayAngle, boids)) { continue; } - if (this.detectBox(context, context.canvas.width, context.canvas.height, rayAngle)) { + if (this.detectBox(this.scene_width, this.scene_height, rayAngle)) { continue; } @@ -68,7 +71,7 @@ class Boid { console.log('cannot find suitable ray'); } - detectBoid(context, direction, boids) { + detectBoid(direction, boids) { for (let i = 0; i < boids.length; i++) { // rule out ourselves if (this.id == boids[i].id) { @@ -134,13 +137,13 @@ class Boid { } } - detectBox(context, width, height, direction) { - let perceptionVector = this.findPoint(this.x, this.y, this.rayLength, direction); + detectBox(width, height, direction) { + let point = this.findPoint(this.x, this.y, this.rayLength, direction); - if (perceptionVector.x - this.radius < 0 || - perceptionVector.y - this.radius < 0 || - perceptionVector.x + this.radius > width - 0 || - perceptionVector.y + this.radius > height - 0 + if (point.x - this.radius < 0 || + point.y - this.radius < 0 || + point.x + this.radius > width - 0 || + point.y + this.radius > height - 0 ) { return true; } @@ -148,7 +151,19 @@ class Boid { return false; } - update(context) { + findPoint(x1, y1, length, angle) { + angle *= Math.PI / 180; + + var x2 = x1 + length * Math.cos(angle), + y2 = y1 + length * Math.sin(angle); + + return { + x: x2, + y: y2 + }; + } + + draw(context) { this.drawBoid(context); this.drawRay(context, this.x, this.y, this.rayLength, this.direction); } @@ -168,35 +183,6 @@ class Boid { context.moveTo(x, y); context.lineTo(point.x, point.y); context.stroke(); - context.restore(); } - - lineToAngle(context, x1, y1, length, angle) { - angle *= Math.PI / 180; - - var x2 = x1 + length * Math.cos(angle), - y2 = y1 + length * Math.sin(angle); - - context.moveTo(x1, y1); - context.lineTo(x2, y2); - - return { - x: x2, - y: y2 - }; - } - - findPoint(x1, y1, length, angle) { - angle *= Math.PI / 180; - - var x2 = x1 + length * Math.cos(angle), - y2 = y1 + length * Math.sin(angle); - - return { - x: x2, - y: y2 - }; - } - } |