Trivial Physics Simulations (…in Silverlight) – Part 5


The finished product:

I'm having trouble finding a way to embed the SL app inside this page, so let's see if this works (c'mon, skydrive!):

CLICK HERE TO SEE THE APP IN ACTION.

And download the solution from here:

 

Taking it Further

Particle systems are the basis for lots of very high tech graphics effects such as fire, smoke, water, and explosions. Perhaps that's no longer the state of the art - but it used to be 🙂 Anyway, with extra work, you can simulate the above to some degree of realism.

Aside from that, the physics we've created here aren't just for particles - you can use them in games for any simple physics-based motion (space ships, cars, projectiles). I say "simple" because things start to change if you want realistic physics that take into account the shape of the object and how it reacts to its environment.

Here are some ideas:

Direction: Currently, our particles are drawn as simple circles. Let's say we wanted to upgrade them to space ships. In that case, we'd get a terrible effect where the spaceship changes direction without actually rotating - nowhere in our code do we rotate the visual based on the direction (which is a function of its velocity). This could be fixed by adding a RenderTransform at the FireworksRenderingHelper level.

If you do that, you'll be 80% of the way there, but you'll notice that as the direction of the ship changes, the rotation can "flip" suddenly, which is unnatural. The reason for this is because the code we've written simulates a body being acted upon by simple outside forces; but a spaceship actually introduces its own forces (thrust). See more below...

Forces: We haven't talked about the concept of "force", but it's at the heart of real physics. We've hard-coded the acceleration to simulate gravity; but in reality, gravity applies a force, which causes acceleration. If you wanted to simulate a spaceship, you'd be better off altering the acceleration based on the force created by its thrusters, rather than having a fixed acceleration.

Introducing forces to the animation can make the realism really jump to the next level, because you start "feeling" inertia. But keep in mind that there's no force without mass, so bust out this equation:

F = ma

Or for our purposes:

a = F/m

Where "a" is the acceleration, "F" is the force being applied, and "m" is the mass of the object being pushed. The heavier the object, the slower the acceleration.

Obviously, this would add to the inner loop for the particle - but the basics are the same: modify the acceleration based on the force, the velocity based on the acceleration, and the position based on the velocity.

Friction: The current simulation has no mention of friction. The most obvious one is air resistance, but depending on what you're simulating, there could be others. I won't go into detail here because friction can be complex depending on the scenario; but it's essentially another force acting on the object. It can be a matter of reducing the acceleration, it could be tied to velocity, it could be tied to mass, materials, etc.

Angular Momentum: Getting fancier, you might want to give particles an angular velocity (or acceleration) - rotation. Let's say you smack a cube on the corner; it's going to both move and rotate. If your particles are being rendered as anything but tiny dots, it makes sense to add a component for that. In 2D, it would probably be of type double, units "degrees per second", or something like that.

Visuals: This is where you can really go nuts. In my youth (on my beloved Amiga, RIP) I wrote a program which rendered animations of explosions, smoke and lightning. All used a particle system like this, but altered the way that individual particles were rendered. All used a particle whose transparency fell off near the edges - you can probably do this in SL with a RadialGradientBrush - and whose colour changed over age. For example, an explosion particle went from bright orange to gray (as it turned into smoke). It actually turned out very beautiful and quite realistic (well, the 2nd version, anyway).

 

As you can see, there's lots of room for improvement and creativity. If you want to get into a more detailed physics simulation, I recommend this book:

http://www.amazon.com/Physics-Game-Developers-David-Bourg/dp/0596000065

Once you get into collisions between oddly shaped bodies and working out how they come to rest; that's when things start getting really complex.

 

Avi

Skip to main content