main.js entry 4.1 KB
Copy main.js
SimKit.define({
title: "Aerodynamics: free fall with air drag and a parachute",
dt: 1e-3, speed: 3, height: 340,
params: {
m: { label: "Mass", unit: "kg", min: 1, max: 200, value: 80, log: true },
Cd: { label: "Drag coefficient (body)", unit: "", min: 0.2, max: 1.5, value: 1.0, step: 0.05 },
A: { label: "Frontal area (body)", unit: "m²", min: 0.01, max: 2, value: 0.7, log: true },
rho: { label: "Air density", unit: "kg/m³", min: 0.02, max: 1.5, value: 1.225, log: true },
chute: { label: "Parachute open (1) / closed (0)", unit: "", min: 0, max: 1, value: 0, step: 1 },
Ac: { label: "Parachute area", unit: "m²", min: 5, max: 60, value: 25, step: 1 },
h0: { label: "Drop height", unit: "m", min: 100, max: 5000, value: 1500, log: true }
},
init(P, S) {
S.g = 9.81;
S.y = P.h0; S.v = 0; // object with air drag
S.yv = P.h0; S.vv = 0; // reference object in vacuum
S.fd = 0; S.hold = 0; S.done = false;
S.vt = 0; S.h0 = P.h0;
},
step(dt, P, S, t) {
const open = P.chute >= 0.5;
const cda = P.Cd * P.A + (open ? 1.5 * P.Ac : 0); // effective drag area Cd·A
S.vt = Math.sqrt(2 * P.m * S.g / (P.rho * cda)); // terminal speed
if (S.done) { // pause on the ground, then drop again
S.hold += dt;
if (S.hold > 2) {
S.y = P.h0; S.v = 0; S.yv = P.h0; S.vv = 0;
S.h0 = P.h0; S.hold = 0; S.done = false;
}
return;
}
S.fd = 0.5 * P.rho * cda * S.v * S.v; // drag force
const a = S.g - S.fd / P.m;
S.v += a * dt;
S.y -= S.v * dt;
if (S.yv > 0) { S.vv += S.g * dt; S.yv -= S.vv * dt; }
S.a = a; S.t = (S.t || 0) + dt;
if (S.y <= 0) { S.y = 0; S.done = true; S.hold = 0; S.t = 0; }
S.open = open;
},
draw(g, W, H, P, S) {
const top = 40, gnd = H - 60;
const py = y => top + (1 - Math.max(y, 0) / S.h0) * (gnd - top);
const xa = W * 0.32, xv = W * 0.68;
const n = (x, d) => x.toFixed(d);
// sky and ground
g.save();
g.fillStyle = "rgba(90,160,255,0.10)"; g.fillRect(0, 0, W, gnd);
g.fillStyle = "#6b8e4e"; g.fillRect(0, gnd, W, H - gnd);
g.restore();
// parachute
if (P.chute >= 0.5) {
const y = py(S.y), r = 14 + Math.sqrt(P.Ac) * 4;
g.save(); g.strokeStyle = "#d03030"; g.fillStyle = "rgba(208,48,48,0.25)"; g.lineWidth = 2;
g.beginPath(); g.arc(xa, y - 30, r, Math.PI, 0); g.closePath(); g.fill(); g.stroke();
g.beginPath(); g.moveTo(xa - r, y - 30); g.lineTo(xa, y - 6); g.lineTo(xa + r, y - 30); g.stroke();
g.restore();
}
// objects: with drag (left) and in vacuum (right)
const drawBall = (x, y, color) => {
g.save(); g.fillStyle = color; g.beginPath(); g.arc(x, y, 9, 0, 2 * Math.PI); g.fill(); g.restore();
};
drawBall(xa, py(S.y), "#3070c0");
drawBall(xv, py(S.yv), "#888888");
// forces on the object with drag: weight down (red), drag up (blue)
const w = P.m * S.g, y0 = py(S.y);
const lw = 50, ld = Math.min(50 * S.fd / w, 110);
SimKit.sch.arrow(g, xa + 22, y0, xa + 22, y0 + lw, "#d03030");
if (ld > 2) SimKit.sch.arrow(g, xa + 36, y0, xa + 36, y0 - ld, "#3070c0");
SimKit.sch.label(g, "Weight " + n(w, 0) + " N", xa + 78, y0 + lw - 4, "#d03030");
if (ld > 2) SimKit.sch.label(g, "Drag " + n(S.fd, 0) + " N", xa + 74, y0 - ld + 4, "#3070c0");
// titles and readouts
SimKit.sch.label(g, "With air", xa, gnd + 16, "#3070c0");
SimKit.sch.label(g, "Vacuum (no drag)", xv, gnd + 16, "#666");
SimKit.sch.label(g, "v = " + n(S.v, 1) + " m/s a = " + n(S.a || 0, 2) + " m/s²", xa, gnd + 34, "#3070c0");
SimKit.sch.label(g, "v = " + n(S.vv, 1) + " m/s", xv, gnd + 34, "#666");
SimKit.sch.label(g, "Terminal speed vt = √(2mg / ρ·Cd·A) = " + n(S.vt, 1) + " m/s", W / 2, 16, "#444");
},
scope: {
span: P => 40, unit: "m/s",
traces: [
{ label: "With air", color: "#5aa0ff", get: S => S.v },
{ label: "Vacuum", color: "#888888", get: S => S.vv },
{ label: "Terminal speed", color: "#ff6060", get: S => S.vt }
]
}
});