Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (539)
Games in Android Showcase (133)
games submitted by our members
Games in WIP (603)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Can anyaone test this please?  (Read 979 times)
0 Members and 1 Guest are viewing this topic.
Offline misterX

Junior Devvie




java forever!


« Posted 2004-08-07 19:01:26 »

hi,

i've a small problem, i've made a simple particle system where the particles should fade, but they don't. To be sure it's not a software or hardware problem, could one of you be kind enough to test this class?
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
180  
181  
182  
183  
184  
185  
186  
187  
188  
189  
190  
191  
192  
193  
194  
195  
196  
197  
198  
199  
200  
201  
202  
203  
204  
205  
206  
207  
208  
209  
210  
211  
212  
213  
214  
215  
216  
217  
218  
219  
220  
221  
222  
223  
224  
225  
226  
227  
228  
229  
230  
231  
232  
233  
234  
235  
236  
237  
238  
239  
240  
241  
242  
243  
244  
245  
246  
247  
248  
249  
250  
251  
252  
253  
254  
255  
256  
257  
package spacerace.core;

import java.util.Enumeration;
import com.sun.j3d.utils.universe.*;
import javax.media.j3d.*;
import javax.vecmath.*;
import com.sun.j3d.utils.geometry.*;


// TODO: optimizations possible, biggest one is checking if there are still particles "alive",
// second one to update just the right ones

/**
 * A burst are particles thrown in a direction and fading with time. It is usually
 * used to represent the flames of a propulsion system (in particular for the ships).
 * To have more advanced effects, many different bursts can be added together.
 */

class Burst extends BranchGroup
{
      // behavior responsible for creating and recycling the particles
      protected ParticlesCreator particlesCreator;
      // behavior responsible for particles state
      protected ParticlesUpdater particlesUpdater;

      // The position where the new particles are thrown from
      Tuple3f pos;
      // The direction in which the new particles are thrown
      Vector3f direction;
      // number of particles thrown per second
      protected int intensity;
      //the time in milliseconds taken for any particle to become totally transparent.
      protected long fadingTime;

      // the array of point particles
      protected PointArray particles;
      // the start position of each particle
      protected Tuple3f[] startPos;
      // the velocity of each particle
      protected Tuple3f[] velocity;
      // when the particle was thrown
      protected long[] startTime;

      /**
       * Creates a red burst with default values (intensity = 100, fading time = 1000 ms).
       */

      Burst ()
      {
            this(new Color3f(1,0,0), 100, 1000);
      }

      /**
       * Creates a burst of given color, intensity and fading time.
       *
       * @param      color - the burst color
       * @param      intensity - number of particles thrown per second
       * @param      fadingTime - the time in milliseconds taken for any particle to become totally transparent.
       */

      Burst(Color3f color, int intensity, long fadingTime)
      {
            this.intensity = intensity;
            this.fadingTime = fadingTime;

            // The +1 is to round up
            int nbParticles = (int) (intensity * fadingTime) / 1000 + 1;

            // Create the data
            startPos = new Tuple3f[nbParticles];
            for (int i=0; i<startPos.length; i++)
                  startPos[i] = new Vector3f();

            velocity = new Tuple3f[nbParticles];
            for (int i=0; i<velocity.length; i++)
                  velocity[i] = new Vector3f();
            startTime = new long[nbParticles];

            float[] coords = new float[nbParticles*4];

            float[] colors = new float[nbParticles*4];
            for (int i=0; i<nbParticles ; i++)
            {
                  colors[4*i+0] = color.x;
                  colors[4*i+1] = color.y;
                  colors[4*i+2] = color.z;
                  colors[4*i+3] = 1;
            }

            // Create the particles
            particles = new PointArray(nbParticles, GeometryArray.COORDINATES | GeometryArray.COLOR_4 | GeometryArray.BY_REFERENCE);

            particles.setCapability(GeometryArray.ALLOW_REF_DATA_WRITE);
            particles.setCapability(GeometryArray.ALLOW_REF_DATA_READ);
            particles.setCapability(GeometryArray.ALLOW_COUNT_READ);

            particles.setCoordRefFloat(coords);
            particles.setColorRefFloat(colors);

            // Create the particles creator
            particlesCreator = new ParticlesCreator();
            particlesCreator.setSchedulingBounds(new BoundingSphere(new Point3d(), Double.MAX_VALUE));

            // Create the particles updater
            particlesUpdater = new ParticlesUpdater();
            particlesUpdater.setSchedulingBounds(new BoundingSphere(new Point3d(), Double.MAX_VALUE));

            // Add them all
            this.addChild(new Shape3D(particles));
            this.addChild(particlesCreator);
            this.addChild(particlesUpdater);
      }



      /**
      * Turns the burst on, while continuing to update older particles.
      */

      public void activate()
      {
            particlesCreator.setEnable(true);
      }


      /**
       * Turns the burst off, while continuing to update existing particles.
       */

      public void deactivate()
      {
            particlesCreator.setEnable(false);
      }


       /**
        * This is the behavior responsible for creating and recycling the particles.
        */

       class ParticlesCreator extends Behavior
      {
            private WakeupCriterion wakeupCondition;
            private int newParticleIndex = 0;

            ParticlesCreator()
            {
                  wakeupCondition = new WakeupOnElapsedTime((int) (1000f / intensity));
            }

            public void initialize()
            {
                  this.wakeupOn(wakeupCondition);
            }

            /**
             * Creates/recycles a particle.
             */

            public void processStimulus(Enumeration criteria)
            {
                  // select the next particle index
                  newParticleIndex++;
                  if (newParticleIndex == particles.getValidVertexCount())
                        newParticleIndex = 0;

                  // set its start position as the current position
                  startPos[newParticleIndex].set(pos);

                  // set its velocity in the current randomly altered direction
                  float L = direction.length();
                  velocity[newParticleIndex].set(
                        direction.x + L*(float)(Math.random()-0.5),
                        direction.y + L*(float)(Math.random()-0.5),
                        direction.z + L*(float)(Math.random()-0.5)
                  );

                  // set its throwing time
                  startTime[newParticleIndex] = System.currentTimeMillis();

                  this.wakeupOn(wakeupCondition);
            }
      }

      /**
       * This updates the position and the velocity of particles based on their values
       * and the elapsed time.
       */

      class ParticlesUpdater extends Behavior implements GeometryUpdater
      {
            private WakeupCriterion wakeupCondition = new WakeupOnElapsedFrames(0);

            public void initialize()
            {
                  this.wakeupOn(wakeupCondition);
            }

            /**
             * Updates the particles state.
             */

            public void processStimulus(Enumeration criteria)
            {
                  particles.updateData(this);
                  this.wakeupOn(wakeupCondition);
            }

            /**
             * Called by the geometry updater to perform updating.
             */

            public void updateData(Geometry geo)
            {
                  // get the data
                  PointArray pa = (PointArray) geo;
                  int n = pa.getValidVertexCount();
                  float[] coords = pa.getCoordRefFloat();
                  float[] colors = pa.getColorRefFloat();

                  long currentTime = System.currentTimeMillis();

                  for (int i=0; i<n; i++)
                        if (startTime[i] > 0)      // if particle is alive
                        {
                              // compute the delay between now and when the particle was created
                              long dt_ms = currentTime - startTime[i];
                              float dt_s = (float) dt_ms / 1000;

                              // update the coordinates
                              coords[3*i+0] = startPos[i].x + velocity[i].x * dt_s;
                              coords[3*i+1] = startPos[i].y + velocity[i].y * dt_s;
                              coords[3*i+2] = startPos[i].z + velocity[i].z * dt_s;

                              // update the color
                              colors[4*i+3] = (float) dt_ms/fadingTime;

                              // if particle has finished its life cycle
                              if (dt_ms >= fadingTime)
                              {
                                    startTime[i] = -1; // no more alive
                                    colors[4*i+3] = 1; // make it totally transparent
                              }
                        }

            }
      }

      /**
       * Shows a burst.
       */

      public static void main(String[] args)
      {
            SimpleUniverse u = new SimpleUniverse();

            u.getViewingPlatform().setNominalViewingTransform();

            Burst burst = new Burst(new Color3f(0,1,1), 100, 2000);
            burst.pos = new Vector3f();
            burst.direction = new Vector3f(0.25f,0,0);

            BranchGroup bg = new BranchGroup();
            bg.addChild(burst);
            bg.compile();

            u.addBranchGraph(bg);
      }
}

thanks in advance.

edit: "simple particle system" are maybe words that don't go together! Tongue
Offline Breakfast

Senior Devvie




for great justice!


« Reply #1 - Posted 2004-08-09 20:21:15 »

A small scatter of blue points flies from left to right. After a short duration each point vanishes. There is no perceptible change in their appearance before they vanish.
Offline NVaidya

Junior Devvie




Java games rock!


« Reply #2 - Posted 2004-08-10 00:39:39 »


The A in RGBA stands for Alpha and not Transparency Wink, if you want your particles to fade. So modify your settings for Alpha as follows:

colors[4*i+3] = 1.f - (float) dt_ms/fadingTime;

and

colors[4*i+3] = 0; // make it totally transparent

Then create a new Appearance and set it on your Shape3D with a PointAttributes with setPointAntialiasingEnable(true). This will (also) provide the transparent effect.

With the above mods I see the nice effect that you're after.



Gravity Sucks !
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline misterX

Junior Devvie




java forever!


« Reply #3 - Posted 2004-08-10 07:18:56 »

thanks man!  Grin
Offline misterX

Junior Devvie




java forever!


« Reply #4 - Posted 2004-08-10 08:33:58 »

hmm... i was talking too quick... i made the changes, also changed the point size to "see" the PointAttributes were correctly set. But still no transparency...
After looking aroud a bit, i figured out that the DX version (what i have Tongue) may simply not have point antialiasing feature.
Offline Breakfast

Senior Devvie




for great justice!


« Reply #5 - Posted 2004-08-10 13:20:12 »

I tested it on the OpenGL version, though.
Offline NVaidya

Junior Devvie




Java games rock!


« Reply #6 - Posted 2004-08-10 14:52:00 »

Quote
hmm... i was talking too quick... i made the changes, also changed the point size to "see" the PointAttributes were correctly set. But still no transparency...
After looking aroud a bit, i figured out that the DX version (what i have Tongue) may simply not have point antialiasing feature.


If PointAA doesn't work, try setting a TransparencyAttributes() on the appearance with a transparency of, say, 0.6f and see how it goes...

But without PointAA your particles will lose much of their visual appeal !

Edit: And did I say that before ? works nicely for me with the mods on WinOGL Java 3D.

Gravity Sucks !
Offline misterX

Junior Devvie




java forever!


« Reply #7 - Posted 2004-08-10 18:46:25 »

thanks for feedback. i wonder how they fade on OGL  Roll Eyes Tongue
but since it won't work here, i simply not make they die (removing the 'startTime = -1;'). Doing this, it looks ok in DX version and i guess even better on GL! ;-)
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

rwatson462 (37 views)
2014-12-15 09:26:44

Mr.CodeIt (31 views)
2014-12-14 19:50:38

BurntPizza (62 views)
2014-12-09 22:41:13

BurntPizza (99 views)
2014-12-08 04:46:31

JscottyBieshaar (60 views)
2014-12-05 12:39:02

SHC (74 views)
2014-12-03 16:27:13

CopyableCougar4 (77 views)
2014-11-29 21:32:03

toopeicgaming1999 (138 views)
2014-11-26 15:22:04

toopeicgaming1999 (127 views)
2014-11-26 15:20:36

toopeicgaming1999 (38 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!