Swarm! (using Reynolds steering behaviors)

Been working on this for a while. It’s a swarm!
If anyone can figure out how to prevent the swarm from going underground, that would be amazing.


Here’s another with a mouse-down scatter behavior added in. Just get a swarm going and then click your mouse a few times for the fun:


From the docs: Physics and Solid Particles | Babylon.js Documentation

The sps creation function takes a third arg , where you enable particleIntersection: true .

Then, you should be able to test the intersection against the ground mesh. Docs suggest changing velocity to zero but i think what you want to happen is more tricky than that


Yea, seems to be more tricky. Tried a couple different ways, but none is perfect:

Not changing Y if intersects (keeps the swarm at a higher altitude in general):

Resetting Y to Y of ground pos at X/Z (results in some jitter and some still go under)

i have an idea that may work? you could add some repulsion behavior , similar to, but opposite of the attraction behavior of the target box. the repulsive object to target could be the ground (or maybe an area of the ground instead of single point) that is closest to the target box.

1 Like

That is a fantastic idea. Will try.

This might give you some ideas too.

1 Like

Here’s a physics based implementation. The other option that might be cheaper computationally is to use an ammo js raycast test to clamp they y height to the surface of the terrain for each box’s xz coordinate for tick (scene.registerBeforeRender).

You can change the mass parameter to allow faster speeds.

1 Like

@bigrig I used ammo.js raycasting to achieve this, it works really well!


That is amazing. It works great. So cool to see a swarm go from below to over the mountain - looks like it’s out of a movie.

1 Like

Dude, I was thinking the exact same thing! Its like it’s out of World-war Z :laughing:

WOW this is SO FUN to see! :star_struck: I love how everyone jumped in to build this collaboratively the result is really good.

By the way I think this could look super cool in the Babylon 6.0 release video, maybe just adding some visual flair to the scenario and bam it would look great :slight_smile: @PirateJC

What sort of visual flair did you have in mind?

@Evgeni_Popov wanna team up to make each one of those cubes into a thin instance bug you used in that animation baker demo? :laughing: That would be terrifying to see lol.

Best movie monster ever:


Oh man, this is giving me so many ideas, I wish there was 5x me sometimes… I wonder if Ammo.js could be extended via one of it’s soft body constraint solvers specifically to solve for each tendril’s transformations

I bet we could add a behavior that constrains movement of any tendril to that of its neighbor - and simulates the effect. Kind of like a flowfield: 6.4: Steering Behaviors: Flow FIeld Following - The Nature of Code - YouTube


Not anything super complicated, maybe just some texture on the ground, a skybox, and any kind of mesh for the swarm… it could be little cute style ants following a cube of sugar, (like this)

or the scary tendril monsters too :rofl:

I wanted to play with it a bit and so mixed in @Evgeni_Popov 's amazing fluid renderer, so now you have: water that chases you! :rofl: chase object SWARM! | Babylon.js Playground (babylonjs.com)


Now that’s a cool movie monster. A sentient fluid that wants to incorporate you into its Borg. Awesome.