Glocken/06-pendulumShort.html

126 lines
4.9 KiB
HTML

<!--
Copyright 2021 Matthias Müller - Ten Minute Physics
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-->
<!DOCTYPE html>
<html>
<body>
<canvas id = "myCanvas"></canvas>
<script>
var lengths = [0.2, 0.2, 0.2];
var masses = [1.0, 0.5, 0.3];
var angles = [0.5 * Math.PI, Math.PI, Math.PI];
var canvas = document.getElementById("myCanvas");
var c = canvas.getContext("2d");
canvas.width = window.innerWidth - 20;
canvas.height = window.innerHeight - 20;
var simMinWidth = 1.0;
var cScale = Math.min(canvas.width, canvas.height) / simMinWidth;
function cX(pos) { return canvas.width / 2 + pos.x * cScale; }
function cY(pos) { return 0.4 * canvas.height - pos.y * cScale; }
class Pendulum {
constructor(masses, lengths, angles) {
this.masses = [0.0];
this.lengths = [0.0];
this.pos = [{x:0.0, y:0.0}];
this.prevPos = [{x:0.0, y:0.0}];
this.vel = [{x:0.0, y:0.0}];
var x = 0.0, y = 0.0;
for (var i = 0; i < masses.length; i++) {
this.masses.push(masses[i]);
this.lengths.push(lengths[i]);
x += lengths[i] * Math.sin(angles[i]);
y += lengths[i] * -Math.cos(angles[i]);
this.pos.push({ x:x, y:y});
this.prevPos.push({ x:x, y:y});
this.vel.push({x:0, y:0});
}
}
simulate(dt, gravity)
{
var p = this;
for (var i = 1; i < p.masses.length; i++) {
p.vel[i].y += dt * scene.gravity;
p.prevPos[i].x = p.pos[i].x;
p.prevPos[i].y = p.pos[i].y;
p.pos[i].x += p.vel[i].x * dt;
p.pos[i].y += p.vel[i].y * dt;
}
for (var i = 1; i < p.masses.length; i++) {
var dx = p.pos[i].x - p.pos[i-1].x;
var dy = p.pos[i].y - p.pos[i-1].y;
var d = Math.sqrt(dx * dx + dy * dy);
var w0 = p.masses[i - 1] > 0.0 ? 1.0 / p.masses[i - 1] : 0.0;
var w1 = p.masses[i] > 0.0 ? 1.0 / p.masses[i] : 0.0;
var corr = (p.lengths[i] - d) / d / (w0 + w1);
p.pos[i - 1].x -= w0 * corr * dx;
p.pos[i - 1].y -= w0 * corr * dy;
p.pos[i].x += w1 * corr * dx;
p.pos[i].y += w1 * corr * dy;
}
for (var i = 1; i < p.masses.length; i++) {
p.vel[i].x = (p.pos[i].x - p.prevPos[i].x) / dt;
p.vel[i].y = (p.pos[i].y - p.prevPos[i].y) / dt;
}
}
draw() {
var p = this;
c.strokeStyle = "#303030";
c.lineWidth = 10;
c.beginPath();
c.moveTo(cX(p.pos[0]), cY(p.pos[0]));
for (var i = 1; i < p.masses.length; i++)
c.lineTo(cX(p.pos[i]), cY(p.pos[i]));
c.stroke();
c.lineWidth = 1;
c.fillStyle = "#FF0000";
for (var i = 1; i < p.masses.length; i++) {
var r = 0.05 * Math.sqrt(p.masses[i]);
c.beginPath();
c.arc(
cX(p.pos[i]), cY(p.pos[i]), cScale * r, 0.0, 2.0 * Math.PI);
c.closePath();
c.fill();
}
}
}
var scene = {
gravity : -10.0,
dt : 0.01,
numSubSteps : 100,
pendulum : new Pendulum(masses, lengths, angles)
};
function draw() {
c.fillStyle = "#000000";
c.fillRect(0, 0, canvas.width, canvas.height);
scene.pendulum.draw();
}
function simulate() {
var sdt = scene.dt / scene.numSubSteps;
for (var step = 0; step < scene.numSubSteps; step++)
scene.pendulum.simulate(sdt, scene.gravity);
}
function update() {
simulate();
draw();
requestAnimationFrame(update);
}
update();
</script>
</body>
</html>