Effificient particles in 2D

Blitz3D Forums/Blitz3D Programming/Effificient particles in 2D

The Forum Archive is locked. These are the original blitzbasic.com forums, kept so their posts can still be read and searched. No new topics, replies or edits can be made here. To join the conversation, head over to the Blitz3D+ forums.

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.