Trivial Physics Simulations (…in Silverlight) – Part 1

Table of Contents:


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.


Comments (9)
  1. Questa mattina ho notato che sono state aggiunte delle nuove applicazioni Silverlight 2 alla gallery

  2. Justo ahora que estoy llevando a cabo un pequeño proyecto de carácter abierto y del cual no quiero desvelar

  3. L-Systems We’ve established that drawing the tree will use the concept of self-similarity. To put this

  4. You’ve been kicked (a good thing) – Trackback from

  5. Will says:

    I think, if Newton was using meters in his calculations, that he was WAAAAAAY ahead of his time.  

  6. Justo ahora que estoy llevando a cabo un pequeño proyecto de carácter abierto y del cual no quiero desvelar

  7. georgea67 says:

    The past, present and future of computer simulation of real-time physical events, or simply computer-based simulations that involve highly accurate representations of things moving/changing in space and time that are precisely affected by multiple variables like wind, rain, gravity, mud, oil, planets, waves, etc are very fascinating topics for gamers(many may not realize this explicitly, but they sure experience it!), mathematicians, programmers and physicists alike. Heck, any body who thinks about the thinking behind things that they experience in a simulated environment should watch/listen to this interview (available in podcast form as well as video)


    <a href="/"rel=nofollow></a>.

  8. Generale says:

    I know this is an old entry, but I just wanted to thank you for it.

    I have been having a massive mental block on inertial coding as it has been a long time for me. As soon as I saw this it came back to me. Great explanations. Thank you.

Comments are closed.

Skip to main content