From b13ccf720302965ec011252b5adfdbc249c5cbfe Mon Sep 17 00:00:00 2001 From: Luke Murphy Date: Sun, 9 Feb 2020 12:29:21 +0100 Subject: [PATCH] Add vectors and poly collisions --- voicegardens/static/voicegardens.js | 34 +++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/voicegardens/static/voicegardens.js b/voicegardens/static/voicegardens.js index bc56515..abef40f 100644 --- a/voicegardens/static/voicegardens.js +++ b/voicegardens/static/voicegardens.js @@ -202,6 +202,11 @@ class GeneratedShape { this.xs = []; this.ys = []; + // vector listing of the above xs, ys so that we can pass these lists to + // the collidePolyPoly function for collision detection which expects + // vector objects, not plain x, y coordinates as in xs, ys. + this.vectors = []; + // ??? this.angles = []; @@ -243,8 +248,9 @@ class GeneratedShape { for (let i = 0; i < this.edges; i++) { this.startXs[i] = 0; this.startYs[i] = 0; - this.ys[i] = 0; this.xs[i] = 0; + this.ys[i] = 0; + this.vectors[i] = createVector(this.xs[i], this.ys[i]); this.angles[i] = 0; // this directly influences the shape of the size alongside the @@ -262,7 +268,30 @@ class GeneratedShape { /** * Detect if the shape collides with another shape. **/ - return false; // TODO: implement once again + if (shapes.length === 1) { + return false; + } + + for (let i = 0; i < shapes.length; i++) { + let shape = shapes[i]; + + if (this === shape) { + continue; + } + + // don't detect if one shape is fully inside another + let interiorCollision = false; + + var collision = collidePolyPoly( + this.vectors, + shape.vectors, + interiorCollision + ); + + if (collision === true) { + return true; + } + } } sound() { @@ -367,6 +396,7 @@ class GeneratedShape { this.startXs[i] + sin(radians(this.angles[i])) * (this.accelX * 2); this.ys[i] = this.startYs[i] + sin(radians(this.angles[i])) * (this.accelY * 2); + this.vectors[i] = createVector(this.xs[i], this.ys[i]); this.angles[i] += this.frequencies[i]; } }