Thursday, 26 April 2007

Explosions and Fireworks

Explosions and Particle Animation

Explosions are common in games. In this workshop you will develop an applet which demonstrates simple fireworks as an example of an explosion.

An explosion (and a firework being no exception) involves a high number of small objects called particles, moving very fast. In computer animation the proper technique applied here is called particle animation and in fact is one of the most common animation techniques. Small objects are controlled using some rather elementary physics; good results are obtained mostly thanks to the really high number of particles involved than any complexity of a single object. So, we can think about particles as very simple physics-based objects that reveal their power when act in a large flock. Besides explosions, the technique is commonly used to model smoke, fog, fire and even water, depending on appearance and behaviour of the particles.

Fireworks: a Very Simple Model

First, a payload is shot to some altitude; often the payload is a rocket to get higher altitudes. As this initial stage is just a flight, and not an explosion, we will ignore it and start our simulation from the moment when the payload is fired and explodes into a plethora of particles. In the most classical type of fireworks these particles are distributed evenly in all directions, forming a quickly expanding sphere. The particles concentrate near the surface of this sphere. Seen from a distance, particles simply seem to fly with diverse velocity: the highest in case of those flying perpendicular to your optical axis, and the lowest in case of those flying along the axis. A very simplified model can therefore assume particles flying from a designated point on a 2D cast surface with random velocity.

Initial Implementation

Please download Kingston University Games Fundamental Classes (see the link on the right). Be careful not to overwrite anything valuable when unpacking the archive. Instead you can download this to get only the files involved in this project. The applet is a framework solution based on simplified and somewhat naive assumptions listed below:

  • There is just one firing, executed at the start-up of the program
  • All particles start from the centre of the window, i.e. point (400, 300)
  • All particles are of the same size (2)
  • The colour hue of the particles is random
  • 500 particles are created
  • All of them immediately start to travel with a random velocity and in a random direction.

See FireworksGame.onStart function, where the particles are created.

The FireworksGame class incorporates a list of particles and also controls their updating and drawing. Any particle added to m_particles will be properly processed.

The Particle class incorporates a simple color scheme in which you can separately set the hue and the brightness (this is HSB or Hue-Saturation-Brightness colour scheme, used as an option in most serious graphical packages). This allows for simple controlling of the brightness of a particle.

Run the program, observe, think why the overall look-and-feel is far from satisfactory.

Getting a better geometrical model

What is wrong, is the distribution of velocities of our particles. The model applied in the first, naive solution assumes that all the observed velocities have the same probability. It is not true. There is just a small fraction of particles flying straight towards you or straight away of you (along the optical axis). However there is significantly more of them flying to the sides, perpendicualar to the axis; they create a visible front, or a circular wave, which makes the cloud of particles looking spherical. Relatively more particles should therefore get higher velocities and less of them - lower. Without this, instead of an elegant "expanding sphere" effect, what we get is a just shapeless flock of flying elements.

Tasks to do:

Task for everyone:

  • Find out what should be the distribution of (random) velocity of the particles to get a natural look and feel and apply it (see FireworksGame.onUpdate function where the velocity is calculated).

Hint: The surface area of a spherical dome's ceiling is 2 pi r h, where r is the sphere radius and h is the position of the dome's cutting plane...

Tasks to choose (do as many as you can):

  • Change the place where the fireworks are fired from the centre of the window to a random position (very easy)
  • Change the program so that to get a series of explosions instead of a just one (easy)
  • Apply various (random) sizes of particles (very easy)
  • Update particles so that they fade after a time (easy)
  • Play with the previous task, try out several non-linear modes in which they may fade (medium)
  • Update particles so that they shrink after a time (medium)
  • Add gravitation: particles not only fly, but also slowly fall down (medium)
  • Delete the particles from the particle list when they are no longer visible (medium)
  • Change the shape and kind of fireworks (difficult)
  • Show particle traces - a recent part of trajectory glows forming a "tail" (difficult)
  • Add secondary explosions: after a time the particles may explode once again (difficult)
  • Add rotational motion (particles flying along spiral trajectories) (very difficult)