Particle Engine Scrutiny

Started by Funkapotamus, January 29, 2005, 21:14:04

Previous topic - Next topic

Funkapotamus

Well, here's my particle engine:
http://students.uww.edu/gilardijd05/ParticleSystem.java

It's great- I get 20,000 16X16px particles @ 60fps with my machine (1900XP/512 333 RAM/Radeon 9800 128).  Do you guys have any suggestions on how I could have made this better?  I've seen MANY examples of particle engines, and many ways of going about things.  However, they are all very basic.  Nothing has really covered creating a particle engine from conception to final result- the're all just theories and snippits.  Because of this, I've had to glue together my understanding of things myself.  Which, by the way was 90% of the fun involved. :)

Anyway, what I'm looking for here is someone else's viewpoint on the finished product.  I'm looking more for changes in the overall theory of how I'm going about things rather than specific small optimizations of code.

A few notes:
-I'm using the 'verlet' system of updaing/colliding particles
-class "ParticleCollection" manages a collection of particles without creating/deleting any objects.  This way the garbage collector doesn't freak out under heavy load.
-I use sin/cos/tan tables of precalculated values rather than calculating things on the fly- vectorToAngle and angleToVector are as fast as can be.
-The engine is intentionally 2D (Might as well get 2 dimentions right before I try and tackle 3!).

A few ways I could speed things up:
-I could use vertex arrays.  (I have chosen not to right now, so I'm ignoring this mostly).
-I could "recycle" particles's data rather than have it randomly generated every time a particle is 'spawned

Anyway, I'll leave you with some screenshots of the engine in action!

http://students.uww.edu/gilardijd05/particles.gif
http://students.uww.edu/gilardijd05/laser.gif
http://students.uww.edu/gilardijd05/rings.gif

spasi

Wow! Great screenshots! It must have been pure OpenGL fun! :wink:

Some optimizations you might want to consider:

a) Yes, you should use vertex arrays. It may be a little uncomfortable with Java (Cas' "structs" would've been great for such stuff), but you should get a nice speedup.

b) Maybe try using triangles instead of quads. Depending on how you'll end up sending the particle data, it may (but not necessarily) be faster.

c) Absolutely go for a vertex shader/program. I don't remember the specific details, but you can highly optimize the data sent for each particle. The idea is to vary the position (3 floats), normal/direction (3 floats) and size (1 or 2 floats) for each particle and automatically generate the final positions in the shader (or sth like that at least). You could even use shorts instead of floats for normal and/or size. You'll find many examples with this technique around the net. It's worth the trouble!

Anonymous

Quote from: "spasi"c) Absolutely go for a vertex shader/program. I don't remember the specific details, but you can highly optimize the data sent for each particle. The idea is to vary the position (3 floats), normal/direction (3 floats) and size (1 or 2 floats) for each particle and automatically generate the final positions in the shader (or sth like that at least). You could even use shorts instead of floats for normal and/or size. You'll find many examples with this technique around the net. It's worth the trouble!

Amazing technique!  I'll have to look into that!  This is precicely what I was looking for.  What a unique way of doing things... using the video card gpu... wow!  

Man, If I keep redoing this particle engine, I'll never get my game done... Oh well.  I'll continue development with what I have here and work on that later.    I'll keep everyone posted.

spasi

One more detail; If your particles always "look" the camera (fortunately 99% of all particle systems work this way), you don't even have to supply a normal, you just use the inverse camera direction in the shader. Have fun! :wink:

Anonymous

The code looks good, well commented, which is a pleasant surprise.

One thought on how you've structured the code: the particle system class includes rendering code, and is tied to several other classes such as Sprite, Screen, Color, EngineFunc, etc.  To use the ParticleSystem I have to include code for things that I've already created on my own.  Have you thought about breaking out the particle system to handle just the particle generation, motion, collision, etc. and leave the rendering/sprite/screen stuff for the parent program (or a particleRenderer class)?  That way proggers could drop your particle system into their code without hacking your code, or throwing out their own.

It's more work, and maybe not what you're interested in, but this code looks like it could be a nice self-contained class for general use.

napier

BTW the post above was by napier.  Somehow I got logged out before submitting....
penGL/Java/LWJGL demos and code: http://potatoland.org/code/gl

rgrzywinski

Not to go off the specific topic of Funkapotamus' particle engine, but for another take on an MVC-based particle engine take a look at some of the tutorials in my Simulation Container:

http://www.realityinteractive.com/software/index.html

If you don't want to set the whole thing up, then just download the tutorials (from the bottom of the link above) and look at tutorial #5.