Jason (jcreed) wrote,

Today after a long day of annoying twelf hacking (for "implicit index variables polluting the subsumption relation and subsequently causing spurious coverage errors" values of "annoying") I sat down to learn me some Lagrangian mechanics from this book I mentioned before.

And can you tell me what classical mechanics is good for, boys and girls? That's right! Implementing silly flash games. (see http://www.cs.cmu.edu/~jcreed/flash/balance.html) I mean, if you want to design a game around relativistic and/or quantum physics, be my guest. I'm stickin' with old-school for now.

What's neat and surprising about Lagrangian mechanics (well, surprsing at least if you are a total physics nub such as me) is that you can formulate the setup of constrained systems (like the simple externally-driven weight-attached-to-rigid-rod system in the game) in whatever coordinate system is convenient for you, figure out what the kinetic and potential energy formulas are, and then derive ordinary differential equations of motion from them in that coordinate system.

So I was like, okay, my mass is at a fixed length L from the mouse at angle θ. My (single) coordinate is just θ, not the (redundant by virtue of the constraint) x and y coordinates of the weight. Kinetic energy is (1/2)m(L2(θ')2 + x'mouse2 + y'mouse2 + 2Lθ'(ymousecos θ - xmousesin θ)), potential energy is mg(L sin θ + ymouse). Now I plug these into the Euler-Lagrange equations and proceed to crunch through a bunch of algebra and differentiation, and out pops

&theta'' = (1/L)(x''mousesin θ - (y''mouse + g)cos θ)

which is now in a form that's trivial to implement numerically.

Not once did I have to think about newton's laws or the weird tension or compression forces in the rigid rod that keep it having the same length. Lagrange just takes care of all that BS somehow.

(PS my best score is 4783 5192 milliseconds)
Tags: flash, games, math, physics, twelf, work
  • Post a new comment


    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded