Skip to content

Commit 86d7634

Browse files
author
Steve Krouse
committed
## Math and physics as code
I've had this intuition for a while now: wouldn't it be neat if you could somehow "copy-and-paste" your physics equations from your textbook and drop them into your computer and that be the code? Let's describe a bouncing ball. Works great to start: ```javascript const a = (t) => -9.8 /* meters per second sq */ * 10 /* px per meter */ const v0 = 0 const v1 = (t) => v0 + a(t)*t const p0 = 0 const p1 = (t) => p0 + (((v1(t)+v0)/2)*t) ``` But what about the bounce? There are two problems: 1) We need to reverse the velocity (multiply it by -1) 2) We need to know at what time the bounce occurs How about a piece-wise defined function? For starters, let's just hardcode the bounce intervals at 3, and 9: ```javascript const t_bottom1 = 3 // p1(t) === 0, solve for t const v2 = (t) => -v1(t_bottom1) + a(t)*(t - t_bottom1) const p2 = (t) => p1(t_bottom1) + ((v2(t) + v2(t_bottom1))/2)*(t - t_bottom1) const t_bottom2 = 9 // p2(t) === 0, solve for t const v3 = (t) => -v2(t_bottom2) + a(t)*(t - t_bottom2) const p3 = (t) => p2(t_bottom2) + ((v3(t) + v3(t_bottom2))/2)*(t - t_bottom2) const p = (t) => { if (t < t_bottom1) { return p1(t) } else if (t < t_bottom2) { return p2(t) } else { return p3(t) } } ``` Ok, let's abstract this so the ball can bounce forever-ish: ```javascript const eq0 = { t0: -1, v: (t) => 0, p: (t) => 0 } var equations = [eq0] const intervals = [0].concat(range(3,1000, 6)) // [0, 3, 9, 15, ...] intervals.forEach((t0, i) => { var eq = {} eq_last = equations[i] eq.v_last = eq_last.v(t0) eq.p_last = eq_last.p(t0) eq.t0 = t0 eq.a = (t) => -9.8 /* meters per second sq */ * 10 /* px per meter */ eq.v = (t) => -eq.v_last + eq.a(t)*(t - t0) // NEXT: can't decrease by 0.9 because then 6 second hardcoded intervals are fucked up // THUS: how do make dynamic // MAYBE use something like http://algebra.js.org/ to solve the equations // WHICH would eventually lead to allowing mouse click generated invervals eq.p = (t) => eq.p_last + (((eq.v(t)+eq.v(t0))/2)*(t - t0)) equations.push(eq) }) function eqI(t) { const eqNext = equations.find(eq => t < eq.t0) if (!eqNext) { return equations.length-1 } const eqNextIndex = equations.indexOf(eqNext) return eqNextIndex - 1 } const p = (t) => { const eq = equations[eqI(t)] return eq.p(t) } ``` Full code to be found [here](http://woofjs.com/create.html#physics-and-math/6). As said in the code, next steps are: 1) make the intervals dynamic (so we can decrease the speed by 10% each bounce), maybe using something like http://algebra.js.org/ to solve the equations 2) see if we can additionally generate intervals by non-predictable user-input, such as mouse clicks 3) ...turn into flappy bird? Some notes: * It was really annoying to code this, because math can be annoying especially inside JS. I wonder how much better it could be in the right kind of environemnt that supports this kind of maths with rapid feedback. I worry that expressing things in this explicit, closed form, with partial equations will be too awkward for people and the gains of mathmatical purity, expresivity won't be worth it. Regardless, I want to follow down this rabbit hole and see where it goes. * To really show why it's neat to do things in this closed form, I should probably add a time travel debugger thing, that also shows the positions and velocities and accelerations at different points in time, and change when you tweak the params. Probably would be a neat demo.
1 parent 0ef7c35 commit 86d7634

File tree

0 file changed

+0
-0
lines changed

    0 file changed

    +0
    -0
    lines changed

    0 commit comments

    Comments
     (0)