Table of Contents:

Introduction

I recently wrote a small Silverlight game for an internal competition and a friend asked me how I did the projectile trajectories. Turns out that something which looks cool in practice is one of the simplest algorithms of all time (two lines of code!), and which I guarantee you learned in highschool science class; although they didn't call it an "algorithm", they called it a "formula". The cool thing is that it can be used to generate fairly complex visuals, by adding just a little extra spice.

I'm going to write up a short series demonstrating how to simulate fireworks in Silverlight using a very basic physics engine. At the end, we should have a nice interactive simulation to play with.

To start off, let me get back to the basic physics equation which underlies this whole thing:

v = u + at

To paraphrase this: "The velocity (v) a after some time (t) will equal the initial velocity (u), plus the acceleration (a) multiplied by the time (t)." Alternatively, to really get to the heart of the matter:

"Acceleration makes things go faster [or slower!] over time."

Now let's look at the units of measurement:

  • Time: seconds
  • Acceleration: pixels per second per second
  • Velocity: pixels per second
  • Distance: pixels

I think Newton may have been using the outmoded "meters" rather than pixels when he came up with this stuff; but it won't matter as long as the unit is consistent throughout.

Now, when you think about those units of measurement above, you notice that acceleration is just a measure of how velocity changes, and velocity is just a measure of how distance changes. So the whole physics simulation, at its core, is this set of two lines:

   1: while ( true )
   2: {
   3:     position += velocity;
   4:     velocity += acceleration;
   5: }

In other words:

"Every frame, the position of the object changes based on its velocity, and the velocity changes based on the acceleration."

Run the contents of that loop for each frame, and you have yourself a simple physics simulation.

One big thing that seems to be missing above is the direction of motion. It actually isn't missing - It's hidden in the type of those variables:

   1: Vector2D acceleration = new Vector2D( 0, 10 );
   2: Vector2D velocity = new Vector2D( 50, -50 );
   3: Vector2D position = new Vector2D( 0, 0 );

What's a Vector2D? It's a custom class that stores both a X component [of motion] and a Y component, and knows how to add/subtract them. Turns out that the WPF Vector class was taken out of Silverlight, so we need to write our own. Also turns out that this is trivial.

With this simple algorithm, by merely changing the initial velocity, you can simulate lots of simple phenomena:

  • A ballistic trajectory, by giving a high initial velocity in the direction the gun is pointed.
  • An explosion, by creating lots of fragments that have large initial velocities radiating in a rough circle from an origin.
  • A fountain, by continuously creating particles that radiate roughly upwards from a single point.

Stay tuned... Next up; the Vector2D class.

Avi