## 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.

sayboooo

##### Share on other sites

• 2 weeks later...

Very nice!

##### Share on other sites

Here is an implementation of the flocking algo : the (solid) particles (should work with classical particles too) aggregate together when they are close enough.

Moreover they get the same direction and velocity and keep at a coherent distance from each other.

http://www.babylonjs-playground.com/#2FPT1A#35

##### Share on other sites

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.

##### 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.

##### Share on other sites

Hi guyz ! i'll be back tomorow on this conversation.

Btw, +1 @jerome for the example and the "sa mémé cowboy" great explanation !

Just for a test, i added reppel to jerome's example, congrats guys, we got now an IA fishes

http://www.babylonjs-playground.com/#2FPT1A#46

##### Share on other sites

It remains to make a neural network to do a real IA

Lib here https://github.com/harthur/brain

## 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.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.