Effificient particles in 2D

Blitz3D Forums/Blitz3D Programming/Effificient particles in 2D

andy_mc(Posted 2008) [#1]
Hi all,

I'm working on a simple particle engine in 2D, for explosions etc...

Right now when I generate an explosion, I simply have a for loop that generates 100 or more particle objects which are of a particle type which has fields, x#,y#,mx#,my#,life
Then to update and display them I update each particles coords then decrement it's life, it's deleted when life hits zero. I lock the image buffer before drawing the particles as I've found this is quicker.

Is using types a good way to do particles, or would I be better off using an array or memory bank? Also, those of you who know this stuff, is it best to update every particle every frame, or half of them?


Ross C(Posted 2008) [#2]
Types are great for particles :o) However, to keep things sensible, recycle your particles. Upon the death of a particle, flag it as dead. So, when you go to update the particles, the dead ones, don't get processed.

Upon needing a new particle, you search for dead particles, and unflag them and set their properties. I have found this to be faster. Inserting, creating types, although fast, does have a small slowdown.

I experienced this with my pathfinding program. I used types. Using create and insert cause some slowdown. Obviously in the program ALOT of creating, deleting and inserting was happening.

I tested the speed of these and found recycling was faster. Ended up using arrays for more speed though.

So, i'd say types are good for particles, as it keeps things a little easier to understand :o)

As for updating, set a rate of updat and limit it to that. Such as 60 fps update. Obviously updating the particles at a rate that's slower than the screen will be pretty noticable :o) I have never really seen the need to update the particles at a greater rate than the screen update. BUT, if your game is running slow graphically, then you'd want the particles to still be updating at a decent rate.


Stevie G(Posted 2008) [#3]
Is using types a good way to do particles, or would I be better off using an array or memory bank? Also, those of you who know this stuff, is it best to update every particle every frame, or half of them?


The method you use for storing them is really six and half a dozen and down to personal preference. I always use types for the simple reason that it's easier to add stuff to and makes my code more readable.

One thing which you may consider for a slight speedup is recycling your particles, rather than deleting them. Basically, create as many particle instances as your likely to need, have an 'active' parameter ( or use your 'life' field to do same ) and only update the ones which are active. When you create a new particle .. move a global PARTICLEnext.particle on to the next available particle in the list ....

global PARTICLEnext.particle

type particle
  field x#, y#, mx#, my#, life
end type
  
Function PARTICLEcreate( x#, y#, mx#, my#, life )

   PARTICLEnext\x = x
   PARTICLEnext\y = y
   PARTICLEnext\mx = mx
   PARTICLEnext\my = my
   PARTICLEnext\Life = 100
   PARTICLEnext = after PARTICLEnext
   if PARTICLEnext = null PARTICLEnext = first particle

End Function

;==================================

function PARTICLEupdate()

  for p.particle = each particle
    if p\Life > 0
      p\x = p\x + p\mx
      p\y = p\y + p\my
      p\Life = p\Life - 1
      {print to screen}
   endif
  next

end function


I use this method all the time but have different linked lists of particles to ensure that some get priority over others. Another good reason to use this method is that you can tailor the maximum number of particles to suit so that you can prevent major slowdowns if they get out of hand.

Stevie

EDIT : Ross must have posted before I'd finished typing this .. ah well at least we agree.


Rob Farley(Posted 2008) [#4]
I don't know if this helps...

http://www.blitzbasic.com/Community/posts.php?topic=72566


andy_mc(Posted 2008) [#5]
Thanks, given that I'm only using particles for 2D games with small explosions I think types are fine. When I come to implement this in 3D I'm sticking to the same method and obviously using forward facing sprites for the particles.