Performance Results


A Fountain of Lines

The LineParticles class implements a particle system made up of yellow and red lines, which shoot out from the origin with parabolic trajectories. The effect, as seen in Figure 21-2, is something like a firework. The thickness of the lines is increased slightly, and anti-aliasing is switched on. When a line has completely dropped below the y-axis, it's reinitialized, which means the firework never runs out.

The main difference from PointParticles is the extra code required to initialize the lines and update them inside the float arrays. Six values in a float array are necessary to represent a single line, three values for each of the two end points.

The LineParticles constructor creates a LineArray object using BY_REFERENCE geometry for its coordinates and color:

     lineParts = new LineArray(numPoints,  LineArray.COORDINATES |                           LineArray.COLOR_3 | LineArray.BY_REFERENCE );         lineParts.setCapability(GeometryArray.ALLOW_REF_DATA_READ);     lineParts.setCapability(GeometryArray.ALLOW_REF_DATA_WRITE); 

Initializing the Particles

The changes start with the initialization of the float arrays inside createGeometry( ):

     // step in 6's == two (x,y,z) coords == one line     for(int i=0; i < numPoints*3; i=i+6)       initTwoParticles(i); 

initTwoParticles( ) initializes the two points. It assigns the same position and velocity to both points and then calls updateParticle( ) to update one of the point's position and velocity. This specifies a line with a point which is one update ahead of the other point. This means that as the particle system is updated, each line will follow a smooth path since one point is following the other.

The color of the line is set to red or yellow by fixing the colors of both points:

     private void initTwoParticles(int i)     { cs[i] = 0.0f; cs[i+1] = 0.0f; cs[i+2] = 0.0f;   // origin           // random velocity in XZ plane with combined vector XZ_VELOCITY       double xvel = Math.random(  )*XZ_VELOCITY;       double zvel = Math.sqrt((XZ_VELOCITY*XZ_VELOCITY) - (xvel*xvel));       vels[i] = (float)((Math.random(  )<0.5) ? -xvel : xvel);   // x vel       vels[i+2] = (float)((Math.random(  )<0.5) ? -zvel : zvel); // z vel       vels[i+1] = (float)(Math.random(  ) * Y_VELOCITY);y vel             // unchanging accelerations, downwards in y direction       accs[i] = 0.0f; accs[i+1] = -GRAVITY; accs[i+2] = 0.0f;           // next particle starts the same, but is one update advanced       cs[i+3] = cs[i]; cs[i+4] = cs[i+1]; cs[i+5] = cs[i+2];       vels[i+3] =vels[i]; vels[i+4] = vels[i+1]; vels[i+5] = vels[i+2];       accs[i+3] =accs[i]; accs[i+4] = accs[i+1]; accs[i+5] = accs[i+2];       updateParticle(i+3);           // set initial colors for the first particle       Color3f col = (Math.random(  ) < 0.5) ? yellow : red;       cols[i] = col.x;   cols[i+1] = col.y; cols[i+2] = col.z;       // the next particle has the same colors       cols[i+3] = col.x; cols[i+4] = col.y; cols[i+5] = col.z;     }  // end of initTwoParticles(  ) 

initTwoParticles( ) is similar to the initParticles( ) method in PointParticles because they set up a parabolic trajectory for their particles.

The updateParticle( ) method does the same task as the one in PointParticles but is located in the particle systems class (LineParticles) rather than in GeometryUpdater.

Particle Appearance

createAppearance( ) adjusts the line width, and switches on anti-aliasing:

     private void createAppearance(  )     { Appearance app = new Appearance(  );       LineAttributes la = new LineAttributes(  );       la.setLineWidth( LINEWIDTH );    // may cause bugs       la.setLineAntialiasingEnable(true);       app.setLineAttributes(la);       setAppearance(app);     } 

As mentioned previously, these features may cause DirectX-based system, or machines with old graphics cards, to respond strangely or crash.


Updating the Particle System

LinesUpdater implements the GeometryUpdater interface, and specifies updateData( ). The method ignores the Geometry argument and, instead, uses the global float arrays directly. It makes use of the initTwoParticles( ) and updateParticle( ) methods in LineParticles:

     public void updateData(Geometry geo)     {       // step in 6's == two (x,y,z) coords == one line       for(int i=0; i < numPoints*3; i=i+6) {         if ((cs[i+1] < 0.0f) && (cs[i+4] < 0.0f))           // both particles in the line have dropped below the y-axis           initTwoParticles(i);   // reinitialise them         else {       // update the two particles           updateParticle(i);           updateParticle(i+3);         }       }     } 



Killer Game Programming in Java
Killer Game Programming in Java
ISBN: 0596007302
EAN: 2147483647
Year: 2006
Pages: 340

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net