xxxxxxxxxx
81
class Pendulum {
constructor(length_arm, bob_borderColor, bob_fillColor, clockwise) {
// Properties of the pendulum's ARM:
this.arm_length = length_arm;
this.arm_startPos = createVector(0, 0);
this.arm_endPos = createVector(0, this.arm_length);
this.arm_mass = 7;
// Properties of the pendulum's BOB:
this.bob_pos = createVector(0, this.arm_length);
this.bob_mass = 40;
this.bob_borderColor = bob_borderColor;
this.bob_fillColor = bob_fillColor;
// Properties of BOTH pendulum's PARTS:
// Property to set the position
this.origin = createVector(width/2, 3);
this.angle = 47;
// Property to designate whether the current pendulum will be rotated clockwise or anticlokwise:
this.clockwise = clockwise;
if(!this.clockwise) {
this.angle = -this.angle;
}
this.angleVel = 0;
this.angleAccel = 0;
// Associating the value of the damping force to the length of the arms of the two pendulums, so the larger this length is, the smaller the value of the damping force will be:
this.damping = map(this.arm_length, 130, 467, 0.998, 0.97);
}
applyForce(force) {
this.angleAccel = (-force / this.arm_length) * sin(this.angle);
}
swing() {
this.angleVel += this.angleAccel;
this.angle += this.angleVel;
this.angleAccel *= 0;
this.angleVel *= this.damping;
}
render() {
push();
// Drawing the pendulum's ARM:
translate(this.origin.x, this.origin.y);
rotate(this.angle);
strokeWeight(this.arm_mass);
stroke(110, 54, 2);
line(this.arm_startPos.x, this.arm_startPos.x, this.arm_endPos.x, this.arm_endPos.y);
// Drawing the pendulum's BOB:
strokeWeight(6.4);
stroke(this.bob_borderColor);
fill(this.bob_fillColor);
ellipse(this.bob_pos.x, this.bob_pos.y, this.bob_mass);
pop();
// Drawing the pendulum's PIVOT:
strokeWeight(5);
stroke(160);
point(this.origin.x, this.origin.y + 1, 6);
}
// Pendulum's method to rest the length of its arm and other of its properties that depends on this measure of this length:
reset(length_arm) {
this.arm_length = length_arm;
this.arm_endPos = createVector(0, this.arm_length);
this.bob_pos = createVector(0, this.arm_length);
this.angle = 47;
if(!this.clockwise) {
this.angle = -this.angle;
}
}
}