Jump to content

Add attractors/reppels to your partciles system


Recommended Posts

Hi guyz !


    I have to clean up a bit and optimize the code, but here is what i get playing with the particles system updateFunction.

    Maybe someone was looking for something similar, so i decided to post it.


   Any feedbacks/comments are welcome :)





Link to comment
Share on other sites

  • 2 weeks later...

Oh yeah, I've seen this before, and I forgot it existed.  Sorry Jerome, and thanks for the reminder. 


Yeah, that works nice.  If you have a moment more, Jerome, can you tell us a bit?  I can't seem to tell what they are flocking-toward.   But maybe this is one of those "Do not open the case.  No user-serviceable parts inside." -situations.  :D   

Link to comment
Share on other sites

What's happening actually :


For each particle, we consider its current neighbours defined by a given distance from this particle.

Then we add to the particle velocity all the neighbour velocities and we normalize this result. This makes the particle go the same global direction than the group it  belongs to. This is called "alignement"

We also compute the barycenter of all the particle positions in the neighbourood. We add the vector particle_position-barycenter to the current particle position. This makes the particle near the center of the group. This is called "cohesion".

At last, we add to the particle position the average vector of all particle-to-neighbour vectors. This makes the particle keep a certain average distance from the others within the group. This is called "separation"


Each correction (alignement, cohesion, separation) can be weighted to give more effect to one or the other aspect of the flocking: line 18 to 20


So each particle adjust its behavior (velocity and position) according to its neighbours... it's a kind of IA.


Moreover, in this example, the flocking computation isn't done in the render loop but in the separate process called each delay ms (line 22) for performance reasons : we have here to iterate, just for the physics, n² times for n particles.

So you can choose to call it only each 5 frames for instance.

You can even choose not to compute it for the whole pool of particles but only for a bunch of them each call with the pool value (line 21) if your computer isn't fast enough. If the pool is too small regarding the total number of particles, each particle will take a lot of time to be aware of its neighbours and the flocking will happen only a long time after... or never if the particles are too fast.

So don't set the pool lower than 1/2 or  1/3 of the global particle number.

If it's equal or greater than the particle number, it will just compute all the particles at once.


So you've to play with both the delay and the pool regarding the total number of particles to manage and your computer speed.


As a side note, all temporary results are stored in only 3 object variables (SPS.vars) in order to lower the GC activity.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...