Browse Source

Rig up working collision detection

main
Luke Murphy 4 years ago
parent
commit
5ea059dc63
No known key found for this signature in database GPG Key ID: 5E2EF5A63E3718CC
  1. 54
      vocoder/static/vocoder.js

54
vocoder/static/vocoder.js

@ -9,8 +9,8 @@
var archiveUrl = serverUrl + "/add-to-archive/";
var canvasColour = "white";
var canvasHeight = 500;
var canvasWidth = 500;
var canvasHeight = 400;
var canvasWidth = 800;
var frameRate = 30;
var microphone;
var newSoundJustRecorded = false;
@ -111,36 +111,53 @@ class GeneratedShape {
/**
* Initialise the new shape.
**/
this.w = 10;
this.h = 10;
this.w = 20;
this.h = 20;
this.x = random(width);
this.y = random(height);
this.xSpeed = 1.3;
this.ySpeed = 0.7;
this.xSpeed = 1.8;
this.ySpeed = 1.8;
this.xDirection = 1;
this.yDirection = 1;
}
collide() {
collide(shapes) {
/**
* 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 ...
if (shapes.length === 1) {
return false;
}
for (var shape of shapes) {
if (this === shape) {
continue;
}
var collision = collideCircleCircle(
this.x,
this.y,
this.w,
shape.x,
shape.y,
shape.w
);
if (collision === true) {
return true;
}
}
return false;
}
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 ...
}
@ -189,15 +206,12 @@ function draw() {
newSoundJustRecorded = false;
}
for (var i = 0; i < shapes.length; i++) {
var shape = shapes[i];
for (var shape of shapes) {
shape.move();
shape.display();
// TODO: need to implement this
if (shape.collide()) {
if (shape.collide(shapes) === true) {
shape.sound();
}
shape.display();
}
}

Loading…
Cancel
Save