2022-07-01 05:38:54 +00:00
|
|
|
// PendulumComponents.js
|
|
|
|
// John Breaux 2022-06-30
|
2022-07-01 08:45:53 +00:00
|
|
|
// Classes used by Pendulum.js
|
2022-07-01 05:38:54 +00:00
|
|
|
|
2022-07-01 08:49:08 +00:00
|
|
|
"use strict";
|
2022-07-01 05:38:54 +00:00
|
|
|
|
2022-07-01 08:45:53 +00:00
|
|
|
//
|
|
|
|
// Classes
|
|
|
|
//
|
2022-07-01 05:50:21 +00:00
|
|
|
|
2022-07-01 08:45:53 +00:00
|
|
|
// Polygon class: Construct and display a regular polygon in 2D
|
|
|
|
class Polygon extends ShapedObject {
|
|
|
|
// constructor: Make a new regular polygon
|
|
|
|
constructor({ sides = 6, radius = 1, center = { x: 0, y: 0 }, color = { r: 0, g: 0, b: 1, a: 1 } } = {}) {
|
|
|
|
super({ color: color });
|
|
|
|
// Create vertices
|
|
|
|
this.vertices = [];
|
|
|
|
for (var i = 0; i <= sides; i++) {
|
|
|
|
// Create vertices by adding center-point in Homogeneous coordinates to an offset vector generated from polar coordinates.
|
2022-07-01 10:50:27 +00:00
|
|
|
this.vertices[i] = new Homogeneous2D().from_polar(radius, 2 * Math.PI * i / sides, 0).add_m(new Homogeneous2D(center.x, center.y, 1));
|
2022-07-01 05:38:54 +00:00
|
|
|
}
|
|
|
|
}
|
2022-07-01 08:45:53 +00:00
|
|
|
// Wrap the init function to set the draw_primitive type to TRIANGLE_FAN
|
|
|
|
init(gl) {
|
|
|
|
this.draw_primitive = gl.TRIANGLE_FAN;
|
|
|
|
super.init(gl);
|
2022-07-01 05:38:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-01 08:45:53 +00:00
|
|
|
class Rod extends ShapedObject {
|
2022-07-01 06:48:56 +00:00
|
|
|
constructor({ anchor = { x: 0, y: 0 }, bob = { x: 0, y: 0 }, color = { r: 1.0, g: 0.0, b: 0.0, a: 1.0 } } = {}) {
|
2022-07-01 08:45:53 +00:00
|
|
|
super({ color: color });
|
2022-07-01 06:48:56 +00:00
|
|
|
// Set the object's vertices
|
2022-07-01 08:45:53 +00:00
|
|
|
this.vertices = [new Homogeneous2D(anchor.x, anchor.y, 1), new Homogeneous2D(bob.x, bob.y, 1)]
|
2022-07-01 06:48:56 +00:00
|
|
|
}
|
2022-07-01 08:45:53 +00:00
|
|
|
// Wrap the init function to set the draw_primitive type to LINES
|
2022-07-01 06:48:56 +00:00
|
|
|
init(gl) {
|
2022-07-01 08:45:53 +00:00
|
|
|
this.draw_primitive = gl.LINES;
|
|
|
|
super.init(gl);
|
2022-07-01 06:48:56 +00:00
|
|
|
}
|
|
|
|
}
|
2022-07-01 05:38:54 +00:00
|
|
|
|
2022-07-01 08:45:53 +00:00
|
|
|
class Anchor extends ShapedObject {
|
2022-07-01 06:48:56 +00:00
|
|
|
constructor({ center = { x: 0, y: 0 }, color = { r: 0.0, g: 1.0, b: 0.0, a: 1.0 } } = {}) {
|
2022-07-01 08:45:53 +00:00
|
|
|
super({ color: color });
|
|
|
|
// Set the object's vertices
|
|
|
|
this.vertices = [new Homogeneous2D(center.x, center.y, 1)];
|
2022-07-01 05:38:54 +00:00
|
|
|
}
|
2022-07-01 08:45:53 +00:00
|
|
|
// Wrap the init function to set the draw_primitive type to POINTS
|
2022-07-01 05:38:54 +00:00
|
|
|
init(gl) {
|
2022-07-01 08:45:53 +00:00
|
|
|
this.draw_primitive = gl.POINTS;
|
|
|
|
super.init(gl);
|
2022-07-01 05:38:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-01 08:45:53 +00:00
|
|
|
// This doesn't need to extend ShapedObject, but I love the symmetry
|
|
|
|
class Pendulum extends ShapedObject {
|
2022-07-01 10:50:27 +00:00
|
|
|
constructor({ angle = 60, length = PEN_LENGTH, radius = BOB_RADIUS } = {}) {
|
2022-07-01 08:45:53 +00:00
|
|
|
super();
|
2022-07-01 10:50:27 +00:00
|
|
|
this.angle_initial = angle;
|
2022-07-01 11:11:20 +00:00
|
|
|
this.angle = angle;
|
2022-07-01 10:50:27 +00:00
|
|
|
this.period = Math.sqrt(9.81 / length);
|
|
|
|
// define where anchor and bob are
|
2022-07-01 08:45:53 +00:00
|
|
|
var anchor = { x: 0, y: 0 }, bob = { x: 0, y: -length };
|
2022-07-01 10:50:27 +00:00
|
|
|
// create the components
|
2022-07-01 05:38:54 +00:00
|
|
|
this.components = [
|
2022-07-01 08:45:53 +00:00
|
|
|
new Rod({ anchor: anchor, bob: bob }),
|
2022-07-01 05:38:54 +00:00
|
|
|
new Anchor({ center: anchor }),
|
2022-07-11 02:09:08 +00:00
|
|
|
new Polygon({ sides: 6, radius: radius, center: bob })
|
2022-07-01 05:38:54 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize the pendulum
|
|
|
|
init(gl) {
|
|
|
|
// initialize the components
|
2022-07-01 06:48:56 +00:00
|
|
|
for (var component of this.components) {
|
2022-07-01 05:38:54 +00:00
|
|
|
component?.init(gl);
|
|
|
|
}
|
|
|
|
// start the clock
|
|
|
|
this.t_prev = Date.now();
|
|
|
|
}
|
2022-07-01 08:45:53 +00:00
|
|
|
|
2022-07-01 05:38:54 +00:00
|
|
|
// Draw the pendulum
|
|
|
|
draw(gl, modelMatrix, u_ModelMatrix) {
|
|
|
|
// Rotate the pendulum
|
2022-07-01 08:45:53 +00:00
|
|
|
modelMatrix.setIdentity();
|
|
|
|
modelMatrix.rotate(this.angle, 0, 0, 1);
|
2022-07-01 05:38:54 +00:00
|
|
|
// Draw each component
|
|
|
|
for (var component of this.components) {
|
|
|
|
component?.draw(gl, modelMatrix, u_ModelMatrix);
|
|
|
|
}
|
|
|
|
}
|
2022-07-01 08:45:53 +00:00
|
|
|
|
|
|
|
// Tick the pendulum (update and perform movement)
|
|
|
|
tick() {
|
2022-07-01 11:11:20 +00:00
|
|
|
if (REAL_PENDULUM_MODE) {
|
|
|
|
var now = Date.now(), elapsed = (now - this.t_prev) / 1000 * this.period;
|
|
|
|
this.angle = this.angle_initial * Math.cos(elapsed);
|
|
|
|
} else {
|
|
|
|
var now = Date.now(), elapsed = now - this.t_prev;
|
|
|
|
this.t_prev = now;
|
|
|
|
this.angle = (this.angle + (A_VELOCITY * elapsed / 1000)) % 360;
|
|
|
|
}
|
2022-07-01 08:45:53 +00:00
|
|
|
}
|
2022-07-01 05:38:54 +00:00
|
|
|
}
|