1
0
mirror of https://github.com/osmarks/random-stuff synced 2024-11-09 22:09:55 +00:00
random-stuff/square-law gravity - physics test.html

77 lines
3.0 KiB
HTML
Raw Normal View History

2022-02-05 14:45:04 +00:00
<!DOCTYPE html>
<meta charset="utf8">
<canvas id="canvas-but-good" width=768 height=768></canvas>
<div id="info"></div>
<script>
const canv = document.getElementById("canvas-but-good")
const info = document.getElementById("info")
const centerX = canv.width / 2
const centerY = canv.height / 2
const scale = 0.5
const G = 0.1
2023-02-05 21:59:30 +00:00
const stepsPerSecond = 1000
2022-02-05 14:45:04 +00:00
const vzero = [0, 0]
const vadd = ([a, b], [c, d]) => [a + c, b + d]
const vscale = (a, [b, c]) => [a * b, a * c]
const vsub = (a, b) => vadd(vscale(-1, a), b)
const vmag = ([a, b]) => Math.hypot(a, b)
const vnorm = v => vscale(1 / vmag(v), v)
const vsum = vs => vs.reduce(vadd, vzero)
const objects = [] /*[
{ x: [0.5, 0.25], v: vzero, m: 1 },
{ x: [-0.25, -0.5], v: vzero, m: 0.5 },
{ x: [0.25, 0.75], v: vzero, m: 2 }
]*/
for (let i = 0; i < (4 * Math.PI); i += Math.PI / 12) {
objects.push({ x: [ Math.cos(i), Math.sin(i) ], v: vzero, m: Math.exp(Math.random() - 0.5) })
}
const ctx = canv.getContext("2d")
let previousTimestamp = null
function step(timestamp) {
const previousPreviousTimestamp = previousTimestamp
previousTimestamp = timestamp
if (!timestamp) {
return
}
2023-02-05 21:59:30 +00:00
const deltaT = (timestamp - previousPreviousTimestamp) / 1000
const stepCount = Math.min(Math.ceil(deltaT * stepsPerSecond), 20)
const timestep = deltaT / stepCount
console.log(stepCount, deltaT)
for (let j = 0; j < stepCount; j++) {
for (const object of objects) {
object.x = vadd(object.x, vscale(timestep, object.v))
const F = vsum(objects.filter(x => x !== object).map(x =>
vscale(G * (object.m * x.m) * (vmag(vsub(object.x, x.x)) ** 2), vnorm(vsub(object.x, x.x)))
))
object.v = vadd(object.v, vscale(timestep, vscale(1 / object.m, F)))
//console.log(F, object.x, object.v)
}
}
2022-02-05 14:45:04 +00:00
ctx.fillStyle = "black"
ctx.fillRect(0, 0, canv.width, canv.height)
var i = 0
for (const object of objects) {
//console.log(F, object.x, object.v)
ctx.beginPath()
const disp = vscale(scale, object.x)
ctx.arc(centerX + disp[0] * centerX, centerY + disp[1] * centerY, 4 * Math.cbrt(object.m), 0, 2 * Math.PI, false)
ctx.fillStyle = `hsl(${i * 73}deg, 100%, 60%)`
ctx.fill()
i++
}
2022-11-23 17:03:26 +00:00
const E_k = objects.map(x => 1/2 * x.m * vmag(x.v) ** 2).reduce((a, b) => a + b)
// extra division by 2 due to double-counting
const E_p = objects.map(o1 => objects.filter(o2 => o2 !== o1).map(o2 => 1/6 * o1.m * o2.m * G * (vmag(vsub(o1.x, o2.x)) ** 3)).reduce((a, b) => a + b)).reduce((a, b) => a + b)
info.innerHTML = `E<sub>k</sub>=${E_k.toFixed(2)}<br>E<sub>p</sub>=${E_p.toFixed(2)}<br>sum=${E_k+E_p}`
2022-02-05 14:45:04 +00:00
requestAnimationFrame(step)
}
requestAnimationFrame(step)
</script>