Java.next -- Four languages that represent the future of Java
Blogger Stuart Halloway has begun a series of posts on trends that point to the future of the Java platform. In his first post, he compares Clojure, Groovy, JRuby, and Scala -- four wildly different languages that nonetheless all play together in the JRE. Find out what unites these languages and what they can tell us about the future of Java-based development ...

Newsletter sign-up

Sign up for our technology specific newsletters.

Enterprise Java
View all newsletters

Email Address:

Simulate fuzzy phenomena with particle systems

Use particle systems to simulate explosion rings, fireworks explosions, vapor trails, and more

Computer graphics (CG) is important to the games we play, the movies we watch, the design of our vehicles, and more. CG uses traditional polygon-based techniques to model and render classical geometry (cones, spheres, cubes, etc.). In contrast, the geometry of natural phenomena—such as fire and flowing water—needs a different modeling, rendering, and animation technique: particle systems.

After introducing you to the particle system concept and presenting particle systems in a historical context, this Java Fun and Games installment takes you on a tour of Java-based software that I created to build and play with particle systems. The article then reveals a demo applet that uses this software to simulate explosion rings, fireworks explosions, and vapor trails.

Particle system basics

A particle system is a CG technique that combines modeling, rendering, and animation to simulate fuzzy phenomena (also known as fuzzy objects); examples are various kinds of explosions, flowing water, clouds, fire, meteor trails, snow, sparks, fog, and falling leaves. These phenomena are called fuzzy because, due to the absence of straight edges, they appear blurred when rendered: they are composed of particles.

Particle systems manipulate collections of particles. Each particle has various attributes that affect the particle's behavior, along with where and how (as a point composed of one or more pixels, a line, an image, and so on) the particle is rendered. Common attributes include position, velocity (speed and direction), color, lifetime, age, shape, size, and transparency. Most of these attributes vary their values during the particle's existence:

  • Birth: Particles are generated somewhere in the fuzzy object's vicinity. Each of the particle's attributes is given an initial value. This value may be fixed or determined by some stochastic process.
  • Life: Various particle attributes (like position and age) tend to vary over time. For an explosion, each particle's color may darken as the particle moves further away from the explosion's center. This signifies an energy decrease (the particle is dying).
  • Death: Particles typically have two attributes that determine the length of the particle's life: age and lifetime. Age determines the amount of time the particle has already lived. It typically is initialized to 0 and is measured in animation frames (iterations of an animation loop). Lifetime is the maximum amount of time the particle can live and also is measured in frames. The particle is destroyed when its age reaches its lifetime. Various factors may result in a premature death, including a particle moving out of the viewing area and not returning to that area, and a particle's color fading to black prior to its age reaching its lifetime. In this case, we could assume that a black particle indicates zero energy (think explosion particles).


Particle systems have been in use for several decades. Perhaps their earliest use dates back to 1961-1962. According to "Welcome to PONG-Story," in 1961, three MIT students created a video game called Spacewar for the PDP-1 minicomputer (released by Digital Equipment Corporation in November 1960). This game featured a particle system to simulate explosions.

Spacewar involves a pair of spaceships battling around a star whose gravity pulls them in. While trying to avoid the star, these spaceships fire torpedoes at each other. As Figure 1 reveals, the particle system rips apart a spaceship when the spaceship is hit by a torpedo.

Figure 1. The lower-right spaceship's breakup is generated by a particle system



The Spacewar particle system also generates explosions whenever both spaceships collide. Despite a low resolution and the absence of color, Figure 2 indicates a degree of realism that the particle system achieves when simulating explosions.

Figure 2. The particle system manages two explosions when both spaceships collide



Note
If you would like to play the original Spacewar game (which is in the public domain), point your Web browser to the Spacewar instructions page and follow the instructions. After you download the Perl and Java source files, you will need to install a copy of Perl on your system (if you don't already have Perl installed) and execute the following commands to create spacewar.bin:

  • perl macro.pl spacewar.mac >out expands macros in spacewar.mac's PDP-1 assembly language and stores the expanded assembly language in a file named out
  • perl pass12.pl out >out1 performs a two-pass assembly of out's expanded assembly language and stores a binary listing in a file named out1
  • perl tape.pl out1 extracts the object code from out1's binary listing and stores it in spacewar.bin


After you create spacewar.bin, compile pdp1b.java, which also compiles all of the associated Java source files. Execute java pdp1b and click the resulting GUI's Run button to play. The pdp1b program loads spacewar.bin and starts a PDP-1 emulator to interpret this file's contents.



Subsequent to Spacewar, particle systems were used in other games (such as Asteroids). But their potential was not realized until 1983. In that year, Lucasfilm's William T. Reeves published his paper "Particle Systems: A Technique for Modeling a Class of Fuzzy Objects." This paper formalized the particle system concept. It was well received because it described the particle system that generated the Genesis Effect's planetary wall of fire in the movie Star Trek II: The Wrath of Kahn. If you have an account with the Association for Computing Machinery, you can read Reeves's paper, a link to which appears in Resources.

Particle system software

Particle system software lets you simulate fuzzy phenomena by supporting the modeling, rendering, and animation of particles. My Java-based particle system software provides the PS class to support modeling and facilitate animation. It also provides the Display class to support rendering. I first discuss Display because PS depends on this class.

The renderer

The Display class is a java.awt.Canvas component that renders particles. It renders particles as individual pixels, although there is no reason why Display could not render them as groups of pixels, images, and so on.

Discuss

Start a new discussion or jump into one of the threads below:

Subject Replies Last post
. Simulate fuzzy phenomena with particle systems
By JavaWorld
0 07/23/07 06:25 AM
by Anonymous


Resources