File size: 6,805 Bytes
ef987bb | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 | <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Two-Object Drop Experiment</title>
<style>
* { box-sizing: border-box; }
body { font-family: system-ui, sans-serif; margin: 0; background: #eef3f8; color: #1a2230; }
h1 { text-align: center; }
.lab { display: flex; gap: 16px; max-width: 960px; margin: 0 auto; padding: 16px; flex-wrap: wrap; }
.panel { width: 280px; background: #fff; border: 1px solid #d6dde6; border-radius: 10px; padding: 16px; }
.panel label { display: block; margin: 10px 0 4px; font-size: 14px; font-weight: bold; }
.panel select, .panel input[type=range] { width: 100%; }
.panel .val { float: right; font-weight: normal; color: #2563eb; }
.stage { flex: 1; min-width: 320px; }
#scene { background: linear-gradient(#cfe8ff, #f7fbff); border: 1px solid #b8c6d6; border-radius: 10px; display: block; width: 100%; }
.buttons { margin-top: 10px; text-align: center; }
button { padding: 10px 22px; font-size: 15px; border: none; border-radius: 8px; cursor: pointer; margin: 0 6px; }
#start { background: #16a34a; color: #fff; }
#reset { background: #6b7280; color: #fff; }
#result { margin-top: 10px; padding: 12px; background: #fff; border: 1px solid #d6dde6; border-radius: 8px; }
</style>
</head>
<body>
<h1>Two-Object Drop Experiment</h1>
<div class="lab">
<aside class="panel">
<label>Object 1 Type
<select id="typeA"><option value="ball">Ball</option><option value="feather">Feather</option></select>
</label>
<label>Object 2 Type
<select id="typeB"><option value="feather">Feather</option><option value="ball">Ball</option></select>
</label>
<label><input type="checkbox" id="vacuum"> Vacuum (no air resistance)</label>
<label>Drag / Air Resistance <span class="val" id="dragVal">0.02</span>
<input type="range" id="drag" min="0" max="0.1" step="0.005" value="0.02">
</label>
<label>Gravity (m/s²) <span class="val" id="gravityVal">9.8</span>
<input type="range" id="gravity" min="1" max="20" step="0.1" value="9.8">
</label>
<label>Object Weight (kg) <span class="val" id="weightVal">1.0</span>
<input type="range" id="weight" min="0.1" max="5" step="0.1" value="1.0">
</label>
</aside>
<section class="stage">
<canvas id="scene" width="560" height="420"></canvas>
<div class="buttons">
<button id="start">Start</button>
<button id="reset">Reset</button>
</div>
<div id="result">Result: Ready…</div>
</section>
</div>
<script>
const canvas = document.getElementById('scene');
const ctx = canvas.getContext('2d');
const groundY = canvas.height - 30;
const els = {
vacuum: document.getElementById('vacuum'),
drag: document.getElementById('drag'),
gravity: document.getElementById('gravity'),
weight: document.getElementById('weight'),
typeA: document.getElementById('typeA'),
typeB: document.getElementById('typeB'),
};
['drag', 'gravity', 'weight'].forEach(id => {
document.getElementById(id).addEventListener('input', (e) => {
document.getElementById(id + 'Val').textContent = e.target.value;
});
});
let balls = [];
let running = false;
let raf = null;
let remainder = 0;
let previousFrame = null;
function makeBall(x, type) {
// feathers have a larger drag area, balls fall more cleanly
return { x, y: 30, v: 0, landed: false, time: 0,
type, area: type === 'feather' ? 6 : 1, color: type === 'feather' ? '#c084fc' : '#ef4444' };
}
function reset() {
running = false;
if (raf) cancelAnimationFrame(raf);
remainder = 0;
previousFrame = null;
balls = [makeBall(canvas.width * 0.35, els.typeA.value),
makeBall(canvas.width * 0.65, els.typeB.value)];
document.getElementById('result').textContent = 'Result: Ready…';
draw();
}
function start() {
reset();
running = true;
raf = requestAnimationFrame(step);
}
function advancePhysics(dt) {
const g = parseFloat(els.gravity.value);
const dragK = els.vacuum.checked ? 0 : parseFloat(els.drag.value);
const mass = parseFloat(els.weight.value);
for (const b of balls) {
if (b.landed) continue;
const resistance = dragK * b.area * b.v * b.v / mass;
b.v += (g - resistance) * dt;
b.y += b.v * dt * 12;
b.time += dt;
if (b.y >= groundY) { b.y = groundY; b.landed = true; }
}
}
function advanceTime(milliseconds) {
remainder += Math.max(0, milliseconds);
while (remainder >= 50) {
advancePhysics(0.05);
remainder -= 50;
}
draw();
if (balls.every(b => b.landed)) {
running = false;
const t = balls.map((b, i) => 'Object ' + (i + 1) + ': ' + b.time.toFixed(2) + 's').join(', ');
document.getElementById('result').textContent = 'Result: ' + t +
(els.vacuum.checked ? ' (both objects land together in a vacuum)' : '');
}
}
function step(timestamp) {
if (!running) return;
if (previousFrame !== null) advanceTime(timestamp - previousFrame);
previousFrame = timestamp;
if (running) raf = requestAnimationFrame(step);
}
function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = '#8b5a2b';
ctx.fillRect(0, groundY + 10, canvas.width, 20);
for (const b of balls) {
ctx.fillStyle = b.color;
ctx.beginPath();
if (b.type === 'feather') ctx.ellipse(b.x, b.y, 6, 18, Math.PI / 5, 0, Math.PI * 2);
else ctx.arc(b.x, b.y, 12, 0, Math.PI * 2);
ctx.fill();
}
}
document.getElementById('start').addEventListener('click', start);
document.getElementById('reset').addEventListener('click', reset);
reset();
window.gameTest = {
reset(config={}) {
if (config.types) { els.typeA.value=config.types[0]; els.typeB.value=config.types[1]; }
if (config.vacuum !== undefined) els.vacuum.checked=!!config.vacuum;
if (config.drag !== undefined) els.drag.value=String(config.drag);
if (config.gravity !== undefined) els.gravity.value=String(config.gravity);
if (config.mass !== undefined) els.weight.value=String(config.mass);
for (const id of ['drag', 'gravity', 'weight']) document.getElementById(id + 'Val').textContent = els[id].value;
reset(); return this.state();
},
step(milliseconds) {
advanceTime(milliseconds);
return this.state();
},
state() { return { objects:balls.map(b=>({height:groundY-b.y,velocity:b.v,landed:b.landed,landingTime:b.landed?b.time:null})), config:{vacuum:els.vacuum.checked,drag:+els.drag.value,gravity:+els.gravity.value,mass:+els.weight.value} }; }
};
</script>
</body>
</html>
|