From 4d27f83dd79033466fd88c7d700f3a46a425fc17 Mon Sep 17 00:00:00 2001 From: John Breaux Date: Fri, 1 Jul 2022 06:11:20 -0500 Subject: [PATCH] Real Pendulum Mode --- Pendulum/Pendulum.html | 1 + Pendulum/Pendulum.js | 4 ++++ Pendulum/PendulumComponents.js | 23 ++++++++++------------- Pendulum/common.js | 3 +++ 4 files changed, 18 insertions(+), 13 deletions(-) diff --git a/Pendulum/Pendulum.html b/Pendulum/Pendulum.html index 2b5e41a..427108c 100644 --- a/Pendulum/Pendulum.html +++ b/Pendulum/Pendulum.html @@ -16,6 +16,7 @@

+

diff --git a/Pendulum/Pendulum.js b/Pendulum/Pendulum.js index 2877493..8811ee5 100644 --- a/Pendulum/Pendulum.js +++ b/Pendulum/Pendulum.js @@ -89,3 +89,7 @@ function up() { function down() { A_VELOCITY -= 10; } + +function real() { + REAL_PENDULUM_MODE = ! REAL_PENDULUM_MODE; +} diff --git a/Pendulum/PendulumComponents.js b/Pendulum/PendulumComponents.js index 60046aa..61c56e4 100644 --- a/Pendulum/PendulumComponents.js +++ b/Pendulum/PendulumComponents.js @@ -8,7 +8,6 @@ // Classes // - // Polygon class: Construct and display a regular polygon in 2D class Polygon extends ShapedObject { // constructor: Make a new regular polygon @@ -58,8 +57,8 @@ class Anchor extends ShapedObject { class Pendulum extends ShapedObject { constructor({ angle = 60, length = PEN_LENGTH, radius = BOB_RADIUS } = {}) { super(); - length = 0.994 this.angle_initial = angle; + this.angle = angle; this.period = Math.sqrt(9.81 / length); // define where anchor and bob are var anchor = { x: 0, y: 0 }, bob = { x: 0, y: -length }; @@ -67,7 +66,7 @@ class Pendulum extends ShapedObject { this.components = [ new Rod({ anchor: anchor, bob: bob }), new Anchor({ center: anchor }), - new Polygon({ radius: radius, center: bob }) + new Polygon({ sides: 4, radius: radius, center: bob }) ]; } @@ -85,7 +84,6 @@ class Pendulum extends ShapedObject { draw(gl, modelMatrix, u_ModelMatrix) { // Rotate the pendulum modelMatrix.setIdentity(); - modelMatrix.translate(0, 0.5, 0, 1); modelMatrix.rotate(this.angle, 0, 0, 1); // Draw each component for (var component of this.components) { @@ -93,16 +91,15 @@ class Pendulum extends ShapedObject { } } - tick_tock_clock_pendulum_simulation() { - var now = Date.now(), elapsed = (now - this.t_prev) / 1000 * this.period; - this.angle = this.angle_initial * Math.cos(elapsed); - } - // Tick the pendulum (update and perform movement) tick() { - this.tick_tock_clock_pendulum_simulation(); - //var now = Date.now(), elapsed = now - this.t_prev; - //this.t_prev = now; - //this.angle = (this.angle + (A_VELOCITY * elapsed / 1000)) % 360; + 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; + } } } \ No newline at end of file diff --git a/Pendulum/common.js b/Pendulum/common.js index 3e316f4..f7d0274 100644 --- a/Pendulum/common.js +++ b/Pendulum/common.js @@ -17,6 +17,9 @@ var PEN_LENGTH = 0.8; // Rotation angle (degrees/second) var A_VELOCITY = 45.0; +// Enable or disable real pendulum mode +var REAL_PENDULUM_MODE = false; + // // Common functions: //