Welcome!

Java Authors: Elizabeth White, Liz McMillan, Roger Strukhoff, Pat Romanski, Yeshim Deniz

Related Topics: Java

Java: Article

Star Trek Technology for Java3D

Building a particle system for Java3D

We Need More Power, Scotty!
Let's extend our particle system design to incorporate Shape3D particles. We still want to easily add the particle system to the Java3D scene, and reuse as much of what we've done up to this point. Recall that our particle systems use an emitter to control the initial position and velocity of the particles. The point and motion blurred particle systems deal with pixel size particles so we'll have to create a new type of particle system to handle Java3D shapes. So far, the particle systems have been Shape3D objects, so how can particles be any Shape3D object? Java3D supports the aggregation of shapes into a Group.

As you can see from Figure 6, the Shape3DParticle-System is a subclass of the Java3D Group class to allow shapes to be grouped together into a particle system. By implementing the IParticleSystem interface, the Shape3DParticleSystem can use the particle emitter unchanged. To help organize and control the shapes, the shape particle system maintains a scene graph segment for each shape in the particle system. Each shape has a scene graph segment consisting of a branch group to maintain membership in the particle system and a transform group to control the location, scale, and rotation of the shape.

Because particles are born and die during the particle system life cycle, shapes must be added and removed from the scene during the animation. Java3D limits the changes to the content of live scene graphs to branch groups. Provided the group (the particle system) has the ALLOW_CHILDREN_EXTEND and the ALLOW_CHILDREN_WRITE capabilities set and the branch group has the ALLOW_DETACH capability set, the branch group and its children can be added or removed from the scene. For our purposes, the only child of the branch group is a transform group. The transform group maintains the standard Java3D translation, scale, and rotation attributes of its child shape in a Transform3D object. With this structure in place, let's briefly review the Reeves life cycle for our new shape particle system.

Emit New Particles
From Figure 6 you can see that the shape particle system implements the IParticleLifeCycleListener interface. The particle emitter notifies listeners as particles evolve through their lifetime. This notification can be used to create additional effects such as spawning additional particle systems. Just before particles are emitted, the aboutToEmit() method is called on the listener, passing a list of particles to be emitted. The shape particle system reflects the initial particle position in the transform group and adds the branch group, transform group, and shape to the scene.

Bury the Dead Particles
When the particle emitter is ready to bury dead particles, it notifies listeners of their impending demise by calling the aboutToDie() method. The shape particle system takes the news pretty well by removing the branch group for the particles from the particle system, removing the shapes from the scene. To reduce object creation, the shape, transform group, and branch group are recycled for a future reincarnation.

Update the Surviving Particles
While particles are alive, the particle emitter applies the influences and the particles are moved, scaled, and rotated. After the particles have been updated by the particle emitter, it notifies the listeners by calling the updated() method. The shape particle system reflects the new particle position, scale, and rotation in the transform group. We discussed how linear acceleration and velocity of a particle can affect its new position, but how about rotation?

Slicker Than Euler
Realistic rotation of Shape3D particles with Euler angles can be mathematically intensive and computationally expensive. I'll try to keep the math to a minimum but if you are interested in the details, have a look at the references. If you have read much about three-dimensional graphics, you've probably already heard of Euler angles. An example of Euler angles is the yaw, pitch, and roll used to describe the orientation of an airplane. There are a few problems with using Euler angles that make them difficult to use for animation.

The order in which Euler angle rotations are applied can result in different orientations. While applying the rotations, a degree of freedom can be lost to something called a "gimbal lock". Over the course of multiple rotations, numeric corrections are often needed to keep the rotational animation looking good. Too make matters worse, it's computationally expensive to interpolate between orientations. Chris Hecker summed it up pretty well: "It's possible to prove that no three-scalar parameterization of 3D orientation exists that doesn't suck, for some suitably mathematically rigorous definition of suck." I did say that I would try to keep the math to a minimum. While Euler angles are easy to understand, we need something that overcomes the weaknesses of using Euler angles for rotational animation. This is where something called a quaternion can save the day (see the sidebar: Pop Quiz Hot Shot).

A quaternion is an extension to complex numbers consisting of a vector and a scalar. There's no use trying to picture a quaternion because it exists in four-dimensional space. In the spirit of keeping the math to a minimum, let's review the key features of quaternions. A unit length quaternion is perfect for representing a rotational orientation of an object. Java3D supports a unit quaternion with the Quat4f class. As the name implies, it consists of four floating-point numbers to make up the vector and scalar components of the quaternion. It's straightforward to convert Euler angles to a quaternion as shown in Figure 7.

Performing successive rotations with quaternions is as easy as multiplying them together. When compared to the traditional rotational matrix approach, quaternion multiplication (the details of which we won't cover here) and orientation interpolation is much more efficient, making it ideal for animating our rotating particle shapes. To animate the rotation, we need to specify the angular velocity in the vector portion of a quaternion. The angular velocity quaternion used to calculate the time differential of a quaternion is shown in Figure 7. The time differential can be used to interpolate quaternions, which helps us spin objects. That was probably the world's shortest description of quaternions, so be sure to review the references if you need more detail. Let's put this new knowledge to work in our shape particle system.

When shape particles are about to be emitted, the orientation is assigned through the use of Euler angles. The angular velocity is also assigned using the now familiar central value and variance approach discussed above. The orientation and angular velocity is converted into quaternions by the particle. When the particle is updated, the quaternion differential is calculated using the time interval of the particle system manager as described in Figure 7. Finally, the new orientation quaternion is set on the Transform3D of the shape along with the new position and scale and Java3D rotates the shape.


Vector3f translation = new Vector3f();
translation.set(aParticle.getLocalPosition());
aTransform3D.set(aParticle.getOrientation(),
translation, aParticle.getScale());

More Stories By Mike Jacobs

Mike Jacobs is technology architect and Technology Fellow focused on using technology to improve health care.

Comments (10) View Comments

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.


Most Recent Comments
mikejacobs 09/05/11 03:04:00 PM EDT

The commercial version of this particle system is now available free including the source. Visit here.

indie technologies 08/31/05 06:26:58 PM EDT

http://www.indietechnologies.com now has this Java 3D technology available as a commercial product.

indie technologies 08/06/05 07:05:33 PM EDT

This technology is being commercialized for Java 3D game developers. Visit www.indietechnologies.com for more information.

Java Developer's Journal 07/31/05 06:45:05 PM EDT

Star Trek Technology for Java3D. The Star Trek universe has inspired many technology ideas but I'm disappointed I don't have a transporter yet. One Star Trek technology that has been available for sometime is the particle system. No, this is not an exotic propulsion system for your flying car. The particle system was invented to animate the Genesis effect in Star Trek II: The Wrath of Khan. While the Genesis device was used to transform a barren planet into one full of life, we can adopt this technology for more modest effects in Java3D.

Mike Jacobs 07/11/05 09:04:30 AM EDT

If you are looking for the source it is at the following link (my previous comment had a period at the end of the link).

http://res.sys-con.com/story/jun05/99792/source.html

David Morris 07/01/05 09:25:29 AM EDT

Mike, the stated link to the source code is invalid. Could you please update this link.

Thanks, David

Mike Jacobs 06/30/05 10:13:20 AM EDT

The web editor decided to do things a bit different than the past. The first mention of the listings (Listing 1) is a link to all of the code. The link is http://res.sys-con.com/story/jun05/99792/source.html.
Mike

Michael Yankowski 06/29/05 09:57:32 PM EDT

I can't find any links to the source code.

Thanks,
Mike

Mike Jacobs 06/16/05 11:32:10 AM EDT

The source code is now current.

Mike Jacobs 06/16/05 09:20:07 AM EDT

It looks like the source for this article is slightly down level. JDJ is working on it.

Mike