SDL hacks |
|
||||||||||||||||||||||||||||||||||
|
(Summer 2009) I want to "lure" my nephews and nieces towards science and engineering, and one of the things I've done towards that goal is to code some real-time physics simulators.
For the specifics of each "demo", check the README inside each source package. DownloadExplosion/fireworks simulatorDownload source code, and Win32 binary.Click and hold-down the left mouse button to create a never ending supply of "debris". One-dimensional wave simulatorDownload source code, and Win32 binary.Hit SPACE to pull down the string at a distance of 1/4 its full length (from the left). See if you can hit SPACE at the proper frequency (natural structural frequency) to maximize the wave amplitude... Can you make it reach the right side? Two-dimensional waves simulator - a.k.a. "water"Download source code, and Win32 binary.Click (and hold) the left mouse button to "pull" the water at the point where you click. Draging the mouse makes for nice waves that reflect around the borders of the "pool"... Snow "generator"Download the Win32 binary, the code was too embarassing to share :-)And yes, what you see there getting covered in snow is my... name... in Greek :-) Hey, my nephews loved it! :-) TheoryThe "snow" and "fireworks" simulations are easy to implement: we just add random horizontal noise to the falling snow flakes, and we make sure every firework particle is falling with a constant vertical acceleration.The theory behind real-time wave simulation is a little more complex.
Assume we have a wave,
Consider the individual pixels to represent the water molecules, and
assume that each molecule is influenced only by its two neighbours
(left and right), as if they are connected to it with springs. That is,
assume that a given pixel's value,
With First: the pixel's position in the new frame is equal to the position in this frame, plus the vertical velocity: The velocity is equal to the old velocity plus the acceleration:
These equations can be used to perform the simulation, and they would work fine. There is one problem, though: the simulation would be slow, since there's quite a lot of calculations going on per pixel...
Instead, we will use the acceleration at time
Replacing So the end result is amazingly simple: we add the
And that is the only equation we need, to simulate one-dimensional waves in real-time. For the corresponding two-dimensional problem, we just average the effects of the X- and Y- coordinate waves: So, for real-time simulation of two-dimensional waves, we only need this equation:
Hmm, Coding "semi-scientific" models of natural processes can quickly become addictive... I better stop now... :-)
|
|
||||||||||||||||||||||||||||||||||