I want to implement a particle system in my game, but I've never programmed a particle system and don't know where to start.

I only want to display pixels (GL_POINTs) with different sizes in different places, something like Terraria or Minecraft when you hit a block, without the texture. Just a dead simple example.

Google search results for this tend to be very complex, or don't explain the concepts, or are written in hard-to-read C without OOP…

I'm using Java and OpenGL.


I think the key phrase is particle system so most of the samples out in the wild are going to be more complex than you would think a simple demonstration should be. OOP doesn't really enter into the low level part of particle systems and C is very straight forward to convert to other languages so I wouldn't worry about that.

The trick to particles is that you want to display a LOT and as fast as possible, that colors the kinds of code you'll see since everything is optimized in one way or another.

In its simplest form a particle system for a quick splash effect would have the following elements:

  1. A manager both to keep track of all your systems and to give global control to all effects.
  2. Array of "points" where each has position, velocity, life time, display size.
  3. A function to create that array with a starting position and (probably) random velocities and life times.
  4. A function to run a simulation on that array, this probably takes a delta time since the last simulation call.
  5. A function to take the positions and display sizes and display then in OpenGL.
  6. And lastly, an overseer to stop that system from playing when all the points are dead.

Organizing the functions can be OOP at the higher levels as long as the inner loops keep to themselves in the quest for speed. Super lax pseudo code sequence below, leaving out a lot of detail and mixing and matching straight C concepts with a cheesy OOP thrown in but I hope it makes sense. The class design for this would be an entire other discussion =)

For your example, you hit a brick at world location (10,20,30). You then call the manager and ask it to start up a new points particle system at that location. The manager then asks the PointsClass to create itself at position (10,20,30). The class allocates and randomizes the life times, etc... The manager then calls all the active particle systems it controls each frame with a simulation call. Later that frame it asks the PointsClass to draw itself into a GL context. Loop until done.

  • \$\begingroup\$ Thank you for your post! Now I have clear ideas about this. I think I'll give a break to NeHe tutorial about particle systems. \$\endgroup\$ – Puyover Jul 17 '11 at 0:05
  • \$\begingroup\$ NeHe is a great place, you won't go wrong there! Best of luck. \$\endgroup\$ – Patrick Hughes Jul 17 '11 at 0:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.