diff --git a/vocoder/static/p5js/p5.collide2d.min.js b/vocoder/static/p5js/p5.collide2d.min.js new file mode 100644 index 0000000..263ec76 --- /dev/null +++ b/vocoder/static/p5js/p5.collide2d.min.js @@ -0,0 +1,9 @@ +/* +Repo: https://github.com/bmoren/p5.collide2D/ +Created by http://benmoren.com +Some functions and code modified version from http://www.jeffreythompson.org/collision-detection +Version 0.6 | Nov 28th, 2018 +CC BY-NC-SA 4.0 +*/ + +console.log("### p5.collide ###"),p5.prototype._collideDebug=!1,p5.prototype.collideDebug=function(i){_collideDebug=i},p5.prototype.collideRectRect=function(i,t,e,o,r,l,n,c){return i+e>=r&&i<=r+n&&t+o>=l&&t<=l+c},p5.prototype.collideRectCircle=function(i,t,e,o,r,l,n){var c=r,p=l;return ri+e&&(c=i+e),lt+o&&(p=t+o),this.dist(r,l,c,p)<=n/2},p5.prototype.collideCircleCircle=function(i,t,e,o,r,l){return this.dist(i,t,o,r)<=e/2+l/2},p5.prototype.collidePointCircle=function(i,t,e,o,r){return this.dist(i,t,e,o)<=r/2},p5.prototype.collidePointEllipse=function(i,t,e,o,r,l){var n=r/2,c=l/2;if(i>e+n||io+c||t=-d},p5.prototype.collidePointRect=function(i,t,e,o,r,l){return i>=e&&i<=e+r&&t>=o&&t<=o+l},p5.prototype.collidePointLine=function(i,t,e,o,r,l,n){var c=this.dist(i,t,e,o),p=this.dist(i,t,r,l),s=this.dist(e,o,r,l);return void 0===n&&(n=.1),c+p>=s-n&&c+p<=s+n},p5.prototype.collideLineCircle=function(i,t,e,o,r,l,n){var c=this.collidePointCircle(i,t,r,l,n),p=this.collidePointCircle(e,o,r,l,n);if(c||p)return!0;var s=i-e,d=t-o,u=this.sqrt(s*s+d*d),h=((r-i)*(e-i)+(l-t)*(o-t))/this.pow(u,2),y=i+h*(e-i),f=t+h*(o-t);return!!this.collidePointLine(y,f,i,t,e,o)&&(this._collideDebug&&this.ellipse(y,f,10,10),s=y-r,d=f-l,this.sqrt(s*s+d*d)<=n/2)},p5.prototype.collideLineLine=function(i,t,e,o,r,l,n,c,p){var s=((n-r)*(t-l)-(c-l)*(i-r))/((c-l)*(e-i)-(n-r)*(o-t)),d=((e-i)*(t-l)-(o-t)*(i-r))/((c-l)*(e-i)-(n-r)*(o-t));if(s>=0&&s<=1&&d>=0&&d<=1){if(this._collideDebug||p)var u=i+s*(e-i),h=t+s*(o-t);return this._collideDebug&&this.ellipse(u,h,10,10),!p||{x:u,y:h}}return!!p&&{x:!1,y:!1}},p5.prototype.collideLineRect=function(i,t,e,o,r,l,n,c,p){var s,d,u,h,y;return p?(s=this.collideLineLine(i,t,e,o,r,l,r,l+c,!0),d=this.collideLineLine(i,t,e,o,r+n,l,r+n,l+c,!0),u=this.collideLineLine(i,t,e,o,r,l,r+n,l,!0),h=this.collideLineLine(i,t,e,o,r,l+c,r+n,l+c,!0),y={left:s,right:d,top:u,bottom:h}):(s=this.collideLineLine(i,t,e,o,r,l,r,l+c),d=this.collideLineLine(i,t,e,o,r+n,l,r+n,l+c),u=this.collideLineLine(i,t,e,o,r,l,r+n,l),h=this.collideLineLine(i,t,e,o,r,l+c,r+n,l+c)),!!(s||d||u||h)&&(!p||y)},p5.prototype.collidePointPoly=function(i,t,e){for(var o=!1,r=0,l=0;lt&&c.yt)&&i<(c.x-n.x)*(t-n.y)/(c.y-n.y)+n.x&&(o=!o)}return o},p5.prototype.collideCirclePoly=function(i,t,e,o,r){void 0==r&&(r=!1);for(var l=0,n=0;n0&&y<=n/2&&y>=-n/2)return!0}return!1}; diff --git a/vocoder/static/vocoder.js b/vocoder/static/vocoder.js index a068af4..87b06b6 100644 --- a/vocoder/static/vocoder.js +++ b/vocoder/static/vocoder.js @@ -120,8 +120,28 @@ class GeneratedShape { this.xSpeed = 1.3; this.ySpeed = 0.7; - this.xDirection = 1; // left or right - this.yDirection = 1; // top or bottom + this.xDirection = 1; + this.yDirection = 1; + } + + collide() { + /** + * Detect if the shape collides with another shape. + **/ + // TODO: here's what I think we need to do here: there are multiple shapes + // on the canvas so we need to take each shape (that isn't this shape) and + // find out if we're colliding. If so, then we return a "true" value which + // can then be used for generating sounds + return false; // TODO: placeholder for now ... + } + + sound() { + /** + * Play a sound after a collision is detected. + **/ + // TODO: play some nice sounds in response to a collision + // thinking about https://github.com/Tonejs/Tone.js + return; // TODO: placeholder for now ... } move() { @@ -144,6 +164,7 @@ class GeneratedShape { /** * Show the shape on the canvas. **/ + // TODO: use getSoundInfo function to influence how shape is drawn ellipse(this.x, this.y, this.w, this.h); // TODO: experimenting for now ... } } @@ -171,6 +192,12 @@ function draw() { for (var i = 0; i < shapes.length; i++) { var shape = shapes[i]; shape.move(); + + // TODO: need to implement this + if (shape.collide()) { + shape.sound(); + } + shape.display(); } } diff --git a/vocoder/templates/index.html b/vocoder/templates/index.html index 5d73192..74ab25b 100644 --- a/vocoder/templates/index.html +++ b/vocoder/templates/index.html @@ -27,6 +27,7 @@ +