xxxxxxxxxx
// jWilliamDunn forked from:
// github.com/matthias-research/pages/blob/master/tenMinutePhysics/05-manyBeads.html
// see also: graphics.pixar.com/pbm2001/pdf/notesf.pdf
// this new version uses p5.js drawing and an independent loop for the physics simulation.
// it could be improved by removing the vector math code and using p5.js vectors instead
var // adjustables
numBeads=5,
friction=0.0003,
restitution=0.9, //en.wikipedia.org/wiki/Coefficient_of_restitution
g=30, //gravity
delay=30, //delay in ms for physics loop
steps=2; //steps in physics calculations
let // fixed
simMinWidth=2.0,
cScale, simWidth, simHeight;
var mouseClicked=()=>setupScene(),
keyPressed=()=>setupScene();
function setup() {
createCanvas(windowWidth, windowHeight);
cScale = Math.min(width, height) / simMinWidth;
simWidth = canvas.width / cScale;
simHeight = canvas.height / cScale;
background(100);
frameRate(60);
setupScene();
setInterval(simulate,delay); // loop for physics
}
function draw() {
background(0);
noFill();
stroke(255);
strokeWeight(2); // ring
circle(cX(physicsScene.wireCenter), cY(physicsScene.wireCenter), 2*cScale*physicsScene.wireRadius);
noStroke();
fill(180,0,0); // beads
for (var i = 0; i < physicsScene.beads.length; i++) {
var bead = physicsScene.beads[i];
circle(cX(bead.pos),cY(bead.pos), 2*cScale*bead.radius);
}
}
function cX(pos) {
return pos.x * cScale;
}
function cY(pos) {
return 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-friction) / dt);
}
}
var physicsScene = {
gravity : new Vector2(0.0, -g),
dt : 1.0 / 60.0,
numSteps : steps,
wireCenter : new Vector2(),
wireRadius : 0.0,
beads : [],
};
// -----------------------------------------------------
function setupScene() {
physicsScene.beads = [];
physicsScene.wireCenter.x = simWidth / 2.0;
physicsScene.wireCenter.y = simHeight / 2.0;
physicsScene.wireRadius = simMinWidth * 0.4;
// numBeads is global
var mass = 1.0;
var r = 0.1;
var angle = 0.0;
for (i = 0; i < numBeads; i++) {
mass = Math.PI * r * r;
var pos = new Vector2(
physicsScene.wireCenter.x + physicsScene.wireRadius * Math.cos(angle),
physicsScene.wireCenter.y + physicsScene.wireRadius * Math.sin(angle));
physicsScene.beads.push(new Bead(r, mass, pos));
angle += Math.PI / numBeads +0.1;
r = 0.05 + Math.random() * 0.1;
}
}
// --- collision handling -------------------------------------------------------
function handleBeadBeadCollision(bead1, bead2) {
//restitution is global
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() {
var step, i, sdt = physicsScene.dt / physicsScene.numSteps;
for (step = 0; step < physicsScene.numSteps; step++) {
for (i = 0; i < physicsScene.beads.length; i++)
physicsScene.beads[i].startStep(sdt, physicsScene.gravity);
for (i = 0; i < physicsScene.beads.length; i++) {
physicsScene.beads[i].keepOnWire(
physicsScene.wireCenter, physicsScene.wireRadius);
}
for (i = 0; i < physicsScene.beads.length; i++)
physicsScene.beads[i].endStep(sdt);
for (i = 0; i < physicsScene.beads.length; i++) {
for (var j = 0; j < i; j++) {
handleBeadBeadCollision(
physicsScene.beads[i], physicsScene.beads[j]);
}
}
}
}
/*
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.
*/