xxxxxxxxxx
51
// The Nature of Code
// Daniel Shiffman
// http://natureofcode.com
// A rectangular boundary
class Surface {
constructor() {
this.surface = [];
// Here we keep track of the screen coordinates of the chain
this.surface.push(Vector.create(0, height / 2));
//this.surface.push(Vector.create(width / 2, height / 2));
this.surface.push(Vector.create(width, height / 2));
this.surface.push(Vector.create(width, height));
this.surface.push(Vector.create(0, height));
this.center = Vector.create(0, 0);
for (let v of this.surface) {
this.center.x += v.x;
this.center.y += v.y;
}
this.center.x /= this.surface.length;
this.center.y /= this.surface.length;
this.body = Bodies.fromVertices(
this.center.x,
this.center.y,
this.surface,
{
isStatic: true,
}
);
console.log(this.body);
Composite.add(engine.world, this.body);
}
// A simple function to just draw the edge chain as a series of vertex points
show() {
let vertices = this.surface;
//let vertices = this.body.vertices;
fill(255);
fill(0);
beginShape();
for (let v of vertices) {
vertex(v.x, v.y);
}
endShape();
}
}