Skip to content

Instantly share code, notes, and snippets.

@anuraghazra
Created May 16, 2019 15:05
Show Gist options
  • Save anuraghazra/6fcfe9ceb205c3dab93cdc01422d8be2 to your computer and use it in GitHub Desktop.
Save anuraghazra/6fcfe9ceb205c3dab93cdc01422d8be2 to your computer and use it in GitHub Desktop.
class Entity {
constructor(iterations) {
this.dots = [];
this.sticks = [];
this.iterations = iterations || 16;
}
addDot(x, y, vx, vy) {
this.dots.push(new Dot(x, y, vx, vy));
}
addStick(p1, p2, length) {
this.sticks.push(new Stick(this.dots[p1], this.dots[p2], length));
}
updatePoints() {
for (let i = 0; i < this.dots.length; i++) {
this.dots[i].update();
}
}
updateSticks() {
for (let i = 0; i < this.sticks.length; i++) {
this.sticks[i].update();
}
}
updateContrains() {
for (let i = 0; i < this.dots.length; i++) {
this.dots[i].constrain();
}
}
renderPoints(ctx) {
for (let i = 0; i < this.dots.length; i++) {
this.dots[i].render(ctx);
}
}
renderSticks(ctx) {
for (let i = 0; i < this.sticks.length; i++) {
this.sticks[i].render(ctx);
}
}
update(ctx) {
this.updatePoints();
for (let j = 0; j < this.iterations; j++) {
this.updateSticks();
this.updateContrains();
}
this.renderPoints(ctx);
this.renderSticks(ctx);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment