Wolfram Computation Meets Knowledge

Celebrating the Fourth of July with Mathematica Fireworks

What could be a better way to celebrate the Fourth of July than beautiful fireworks in the dark sky?

And what could be a better way to create fireworks on your screen than using Mathematica?

Fireworks

There are a few different ways to create firework “effects” on computers, but it would be a shame if we chose to use just graphical effects with Mathematica. Yes, we are going for the full-scale particle simulation.

Here is the synopsis. We create a firework simulation. With a mouse click, we seed a number of particles on the screen. Each particle has a different initial velocity, and it will follow the projectile motion. The particles spend a limited time on the screen, in which their opacity will diminish gradually. There will be a few customizable effects—colors and trails.

We will start with the projectile motion equation. At a time t, the position of a projected object can be described as the following:

Position of projected object

p0, v0, and a are vectors specifying initial position, initial velocity, and acceleration, respectively. Of course our choice of a is 9.81 m/s2 in negative y direction, the acceleration of gravity.

Since we are simulating many objects, I wrote the formula as a CompiledFunction for better performance.

CompiledFunction formula

Here, v0 is a list of vectors. In this way, posFun can calculate the current positions of multiple particles at once.

Each click generates a group of particles. A group shares the initial time, position, and other properties, such as color. These properties will be maintained in separate queues. The properties will be removed from the queues once the group’s life span is expired.

Now we need to keep track of time. We can try to use AbsoluteTime, but there is one problem. The return value (which is in seconds) is not a machine integer.

Developer`MachineIntegerQ[AbsoluteTime[]]

False

Unfortunately, our compiled function only takes machine-precision numbers as a parameter. There are a few other solutions, but let’s try to use our new timed evaluation functionality. We can create and run a scheduled task, which will simply increase a time variable.

RunScheduledTask[If[timeRun, currentTime += 0.05], 0.05];

In this case, the variable currentTime will be updated every 0.05 seconds. timeRun is a Boolean variable. If it is set to False, it stops the counter, which effectively pauses the simulation. Again, this will be done without delaying the simulation. This will be called at the initialization of the program. And we should not forget to remove the task once the program is done.

RemoveScheduledTask[ScheduledTasks[]];

The main simulation portion will be driven by Dynamic with a certain refresh rate (defined by UpdateInterval). In here, an expired particle group will be removed and the locations of remaining particles will be computed and displayed.

The Dynamic portion will be wrapped by EventHandler, which will add a new group of particles on the “MouseClicked” event. When adding new particles, the current time, mouse position, random velocities, and selected color will be added to the property queues.

The trails of a firework can be created by repeating the position computation while backtracking the time for a few steps.

You can download a notebook of detailed source code with comments here. Also, click here to download the firework simulation in a Computable Document Format (CDF) file, which can be opened with the free Wolfram CDF Player.

The usage is simple. Click anywhere to set off a firework. To set off multiple bursts at once, pause the simulation, click to seed the fireworks, and then click play. Any changes in the number of particles, firework color, or trail length will affect the subsequent clicks.

If you have the free Computable Document Format (CDF) browser plugin or Mathematica 8 installed, then you can set off your fireworks right here in your web browser (if you don’t have the plugin, you’ll just see a placeholder image):

Firework sim

Of course this is very simplified version of firework simulation. There are a lot of parameters that can be adjusted, and fireworks can be more elaborately designed. Maybe I should give a shot at a full-featured “Firework Development Studio” in the future.

I’d like to wish everyone a happy Independence Day, and I hope you enjoy your very personal fireworks presented by Mathematica!

Comments

Join the discussion

!Please enter your comment (at least 5 characters).

!Please enter your name.

!Please enter a valid email address.

2 comments

  1. That’s a really creative demo you have there, I hope you would not mind me taking this and adding to the code.

    I couldn’t be with my boyfriend to watch the fireworks in July and this has given me an idea to layer a cropped picture of us in front of the fireworks for him to set the fireworks off behind us as a present.
    He is no programmer nor does he have Mathematica, I’m glad CDF would give him the ability to interact with it.

    Reply
  2. @Jackie,
    No problem at all. You can do whatever you wish with the code. Yes, CDF is a really neat way to share.

    Reply