xxxxxxxxxx
171
//==========================================================
var Particle = function Particle() {
this.px = 0;
this.py = 0;
this.vx = 0;
this.vy = 0;
this.mass = 1.0;
this.damping = 0.96;
this.bFixed = false;
this.bLimitVelocities = true;
this.bPeriodicBoundaries = false;
this.bHardBoundaries = false;
// Initializer for the Particle
this.set = function (x, y) {
this.px = x;
this.py = y;
this.vx = 0;
this.vy = 0;
this.damping = 0.96;
this.mass = 1.0;
};
// Add a force in. One step of Euler integration.
this.addForce = function (fx, fy) {
var ax = fx / this.mass;
var ay = fy / this.mass;
this.vx += ax;
this.vy += ay;
};
this.setPos = function (x, y) {
this.px = x;
this.py = y;
};
// Update the position. Another step of Euler integration.
this.update = function () {
if (this.bFixed == false) {
this.vx *= this.damping;
this.vy *= this.damping;
this.limitVelocities();
this.handleBoundaries();
this.px += this.vx;
this.py += this.vy;
}
};
this.limitVelocities = function () {
if (this.bLimitVelocities) {
var speed = sqrt(this.vx * this.vx + this.vy * this.vy);
var maxSpeed = 10;
if (speed > maxSpeed) {
this.vx *= maxSpeed / speed;
this.vy *= maxSpeed / speed;
}
}
};
this.handleBoundaries = function () {
if (this.bPeriodicBoundaries) {
if (this.px > width) this.px -= width;
if (this.px < 0) this.px += width;
if (this.py > height) this.py -= height;
if (this.py < 0) this.py += height;
} else if (this.bHardBoundaries) {
if (this.px >= width) {
this.vx = abs(this.vx) * -1;
}
if (this.px <= 0) {
this.vx = abs(this.vx);
}
if (this.py >= height) {
this.vy = abs(this.vy) * -1;
}
if (this.py <= 0) {
this.vy = abs(this.vy);
}
}
};
this.render = function () {
fill(255);
ellipse(this.px, this.py, 2, 2);
};
this.spikes = function (centroidVector) {
let normalLinepos = createVector(this.px, this.py);
let normalLine = p5.Vector.sub(centroidVector, normalLinepos);
normalLine.normalize().mult(-10);
line(this.px, this.py, this.px + normalLine.x, this.py + normalLine.y);
};
};
//==========================================================
var Spring = function Spring() {
var p;
var q;
var restLength;
var springConstant;
this.set = function (p1, p2, k) {
p = p1;
q = p2;
var dx = p.px - q.px;
var dy = p.py - q.py;
restLength = sqrt(dx * dx + dy * dy);
springConstant = k;
};
this.update = function () {
var dx = p.px - q.px;
var dy = p.py - q.py;
var dh = sqrt(dx * dx + dy * dy);
if (dh > 1) {
var distention = dh - restLength;
var restorativeForce = springConstant * distention; // F = -kx
var fx = (dx / dh) * restorativeForce;
var fy = (dy / dh) * restorativeForce;
p.addForce(-fx, -fy);
q.addForce(fx, fy);
}
};
this.render = function () {
stroke(255);
line(p.px, p.py, q.px, q.py);
};
};
//==========================================================
var Edge = function Edge() {
var p;
var q;
var restLength;
var springConstant;
this.set = function (p1, p2, k) {
p = p1;
q = p2;
var dx = p.px - q.px;
var dy = p.py - q.py;
restLength = sqrt(dx * dx + dy * dy);
springConstant = k;
};
this.update = function () {
var dx = p.px - q.px;
var dy = p.py - q.py;
var dh = sqrt(dx * dx + dy * dy);
if (dh > 1) {
var distention = dh - restLength;
var restorativeForce = springConstant * distention; // F = -kx
var fx = (dx / dh) * restorativeForce;
var fy = (dy / dh) * restorativeForce;
p.addForce(-fx, -fy);
q.addForce(fx, fy);
}
};
this.render = function () {
stroke(255);
strokeWeight(2);
line(p.px, p.py, q.px, q.py);
};
};