Newtonian gravity demo

Introduction

This is part one of a series of three pages describing my Newtonian gravity demo program.

A Newtonian gravity demo (that is, a simulation of the laws of gravity ignoring the effects of relativity) is pretty neat.  My first Hohmann transfer orbit demo  and the Earth orbit demo  were both driven by constant calculations of orbital position based on hard-coded orbital properties.  Think of them as puppet shows; they need the constant attention of a guiding hand who knows what to put where.  The gravity demo, on the other hand, needs just an initial state with four parameters:

  • The position of each body in the model
  • The mass of each body
  • The radius of each body (if you want to detect collisions)
  • The initial velocity of each body

Think of the gravity demo as a perpetual motion machine.  You set it up and give it a push and thereafter it runs forever.  After the initial push the scene unfolds according to the Newtonian laws of gravity:

  • Every body in the model attracts every other body with a force that is proportional to the product of their masses and inversely proportional to the square of the distance between them.
  • The acceleration of a body at any instant is equal to the sum of the gravitational forces at that instant divided by the body's mass.
  • Velocities do not change except as a result of these gravitational forces.

I have tried to use IoC (inversion of control) to make it easy to plug in a different scene.

Run it!

This application requires .NET Framework 3.5 SP1 and Windows Installer 3.1. If you have these components then click here to run the demo.  If you don't have them (or you're not sure) then click here for the full installation screen.

The demo includes three scenarios:

  1. Binary star - two stars with identical masses and velocities orbit their comon centre of gravity.
  2. Inner solar system - Mercury, Venus, Earth and Mars orbit the sun.
  3. Hohmann transfer orbit - a satellite is launched from Earth and flies to Mars on a Hohmann transfer orbit.

The demo provides a "trackball" with which you can manipulate the camera.  Clicking and dragging with the left mouse button will swing around the scene.  Clicking and dragging up and down with the right button will zoom in and out.

The source code

Click here to download the source code.

As usual, in writing this program I have stood on the shoulders of giants.  As well as Castle Windsor, I made big use of 3DTools for WPF (the trackball and "ScreenSpaceLines3D" features).  I made a couple of small modifications, so I have included my 3DTools project in the gravity project download.

If you are looking through the source code, here are some files to note:

Next page: The inner solar system demo.

©2009 Carl Johansen