Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions tenMinutePhysics/contribs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,18 @@
</td>
</tr>

<tr>
<td>
<img class = "paperImage" src = "newtonsCradle.jpg" >
</td>
<td>
<div class = "tutorialTitle">Emlyn Corrin, UK</div>
<div class = "description">Newton's Cradle</div>
<a href = "https://github.com/matthias-research/pages/blob/master/tenMinutePhysics/contribs/newtonsCradle.html" target="_blank" class="paperButton">Code</a>
<a href = "https://matthias-research.github.io/pages/tenMinutePhysics/contribs/newtonsCradle.html" target="_blank" class="paperButton">Demo</a>
</td>
</tr>

</table>

</body>
Expand Down
328 changes: 328 additions & 0 deletions tenMinutePhysics/contribs/newtonsCradle.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,328 @@
<!--
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>
<meta name="viewport" content="width=device-width, initial-scale=1.0">

<head>
<title>Constrained Dynamics</title>
<style>
body {
font-family: verdana;
font-size: 15px;
}
.button {
background-color: #606060;
border: none;
color: white;
padding: 15px 32px;
font-size: 16px;
margin: 4px 2px;
cursor: pointer;
}
</style>
</head>

<body>


<button class="button" onclick="setupScene()">Restart</button>
<button class="button" onclick="run()">Run</button>
<p>Number of balls lifted: <input type = "range" min = "0" max = "5" value = "1" id = "ballsSlider" class = "slider"> <span id = "balls">1</span></p>
<br>
<canvas id="myCanvas"></canvas>


<script>

// drawing -------------------------------------------------------

var canvas = document.getElementById("myCanvas");
var c = canvas.getContext("2d");

canvas.width = window.innerWidth - 20;
canvas.height = window.innerHeight - 100;

var simMinWidth = 2.0;
var cScale = Math.min(canvas.width, canvas.height) / simMinWidth;
var simWidth = canvas.width / cScale;
var simHeight = canvas.height / cScale;

function cX(pos) {
return pos.x * cScale;
}

function cY(pos) {
return canvas.height - pos.y * cScale;
}

// vector math -------------------------------------------------------

class Vector2 {
constructor(x = 0.0, y = 0.0) {
this.x = x;
this.y = y;
}

set(v) {
this.x = v.x; this.y = v.y;
}

clone() {
return new Vector2(this.x, this.y);
}

add(v, s = 1.0) {
this.x += v.x * s;
this.y += v.y * s;
return this;
}

addVectors(a, b) {
this.x = a.x + b.x;
this.y = a.y + b.y;
return this;
}

subtract(v, s = 1.0) {
this.x -= v.x * s;
this.y -= v.y * s;
return this;
}

subtractVectors(a, b) {
this.x = a.x - b.x;
this.y = a.y - b.y;
return this;
}

length() {
return Math.sqrt(this.x * this.x + this.y * this.y);
}

scale(s) {
this.x *= s;
this.y *= s;
return this;
}

dot(v) {
return this.x * v.x + this.y * v.y;
}

perp() {
return new Vector2(-this.y, this.x);
}
}

// scene -------------------------------------------------------

class Bead {
constructor(radius, mass, pos) {
this.radius = radius;
this.mass = mass;
this.pos = pos.clone();
this.prevPos = pos.clone();
this.vel = new Vector2();
}
startStep(dt, gravity) {
this.vel.add(gravity, dt);
this.prevPos.set(this.pos);
this.pos.add(this.vel, dt);
}
keepOnWire(center, radius) {
var dir = new Vector2();
dir.subtractVectors(this.pos, center);
var len = dir.length();
if (len == 0.0)
return;
dir.scale(1.0 / len);
var lambda = physicsScene.wireRadius - len;
this.pos.add(dir, lambda);
return lambda;
}
endStep(dt) {
this.vel.subtractVectors(this.pos, this.prevPos);
this.vel.scale(1.0 / dt);
}

}

var physicsScene =
{
gravity : new Vector2(0.0, -10.0),
dt : 1.0 / 60.0,
numSteps : 100,
wireCenters : [],
wireRadius : 0.0,
beads : [],
startBeads : 1,
paused : true,
};

// -----------------------------------------------------

function setupScene()
{
var numBeads = 5;

physicsScene.beads = [];
physicsScene.wireCenters = [];
physicsScene.wireRadius = simMinWidth * 0.4;

var mass = 1.0;

var r = 0.1;
var angle = - Math.PI / 2.0;
for (i = 0; i < numBeads; i++) {
var c = new Vector2(simWidth / 2.0 + (2 * i - numBeads + 1) * r, simHeight / 2.0);
physicsScene.wireCenters.push(c);
var mass = Math.PI * r * r;
if (i >= (numBeads - physicsScene.startBeads)) {
angle = - Math.PI / 4.0;
}
var pos = new Vector2(
c.x + physicsScene.wireRadius * Math.cos(angle),
c.y + physicsScene.wireRadius * Math.sin(angle));

physicsScene.beads.push(new Bead(r, mass, pos));
}
physicsScene.paused = true;
}

// draw -------------------------------------------------------

function drawCircle(pos, radius, filled)
{
c.beginPath();
c.arc(
cX(pos), cY(pos), cScale * radius, 0.0, 2.0 * Math.PI);
c.closePath();
if (filled)
c.fill();
else
c.stroke();
}

function drawLine(a, b)
{
c.beginPath();
c.moveTo(cX(a), cY(a));
c.lineTo(cX(b), cY(b));
c.stroke();
}

function draw()
{
c.clearRect(0, 0, canvas.width, canvas.height);

c.lineWidth = 1.0;
c.strokeStyle = "#f4f4f4";
for (var i = 0; i < physicsScene.wireCenters.length; i++) {
drawCircle(physicsScene.wireCenters[i], physicsScene.wireRadius, false);
}

c.fillStyle = "#888";
c.strokeStyle = "#000";
for (var i = 0; i < physicsScene.beads.length; i++) {
var bead = physicsScene.beads[i];
drawLine(physicsScene.wireCenters[i], bead.pos);
drawCircle(bead.pos, bead.radius, true);
}

c.lineCap = "round";
c.strokeStyle = "#ccc";
c.lineWidth = 20.0;
drawLine(physicsScene.wireCenters[0], physicsScene.wireCenters[physicsScene.wireCenters.length - 1]);
}

// --- collision handling -------------------------------------------------------

function handleBeadBeadCollision(bead1, bead2)
{
var restitution = 1.0;
var dir = new Vector2();
dir.subtractVectors(bead2.pos, bead1.pos);
var d = dir.length();
if (d == 0.0 || d > bead1.radius + bead2.radius)
return;

dir.scale(1.0 / d);

var corr = (bead1.radius + bead2.radius - d) / 2.0;
bead1.pos.add(dir, -corr);
bead2.pos.add(dir, corr);

var v1 = bead1.vel.dot(dir);
var v2 = bead2.vel.dot(dir);

var m1 = bead1.mass;
var m2 = bead2.mass;

var newV1 = (m1 * v1 + m2 * v2 - m2 * (v1 - v2) * restitution) / (m1 + m2);
var newV2 = (m1 * v1 + m2 * v2 - m1 * (v2 - v1) * restitution) / (m1 + m2);

bead1.vel.add(dir, newV1 - v1);
bead2.vel.add(dir, newV2 - v2);
}

// ------------------------------------------------

function simulate()
{
if (physicsScene.paused)
return;
var sdt = physicsScene.dt / physicsScene.numSteps;

for (var step = 0; step < physicsScene.numSteps; step++) {
for (var i = 0; i < physicsScene.beads.length; i++)
physicsScene.beads[i].startStep(sdt, physicsScene.gravity);

for (var i = 0; i < physicsScene.beads.length; i++) {
physicsScene.beads[i].keepOnWire(
physicsScene.wireCenters[i], physicsScene.wireRadius);
}

for (var i = 0; i < physicsScene.beads.length; i++)
physicsScene.beads[i].endStep(sdt);

for (var i = 0; i < physicsScene.beads.length; i++) {
for (var j = 0; j < i; j++) {
handleBeadBeadCollision(
physicsScene.beads[i], physicsScene.beads[j]);
}
}
}
}

document.getElementById("ballsSlider").oninput = function() {
physicsScene.startBeads = this.value;
document.getElementById("balls").innerHTML = this.value;
if (physicsScene.paused)
setupScene();
}
// --------------------------------------------------------

function update() {
simulate();
draw();
requestAnimationFrame(update);
}
function run() {
physicsScene.paused = false;
}

setupScene();
update();

</script>
</body>
</html>
Binary file added tenMinutePhysics/contribs/newtonsCradle.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.