"Boids" Flocking Swarming Simulation Luau Algorithm - Library Animated Bird+Bats+Insects V.1.3 [Open Source] FREE MODEL

IDK but I’m really happy with the code and I’m excited to share it. It’s the first example open sourced for ROBLOX. Their was some one other attempt but he never shared it.

I’ve been meaning to write this for a while so I’m glad I got it done. It goes nicely with my fish simulation. I’m going to publish iterations to this code. Proabably going to make examples of different flocks smaller flocks and the like.

This system is great to build off! You could probably add species and predators or make all sorts of different swarms.
Some main points are
More complex rules can be added, such as obstacle avoidance and goal seeking.
The boids framework is often used in computer graphics, providing realistic-looking representations of flocks of birds and other creatures, such as schools of fish or herds of animals. It was for instance used in the 1998 video game Half-Life for the flying bird-like creatures seen at the end of the game on Xen, named “boid” in the game files.

The Boids model can be used for direct control and stabilization of teams of simple unmanned ground vehicles (UGV)[6] or micro aerial vehicles (MAV)[7] in swarm robotics. For stabilization of heterogeneous UAV-UGV teams, the model was adapted for using onboard relative localization by Saska et al.[8]
–exerpt from the wikipedia article

9 Likes

I’m here to share an update for this model! I have updated to the most current version and added some new examples of this in action! I also converted it into a module.
I added Butterflies
image
Bumble Bees
image

I also added support for humanoid objects because I wanted to add pixies so I’m using my template humanoid which requires a library that is not open sourced

Also this flocking behavior can be converted into a pathing style function and it will effectively generate swarms say you want to create some zombies

6 Likes

Could you please provide some footage?
Ty

3 Likes

This looks really cool! The code also looks pretty good until around halfway through where it completely falls apart with inconsistent variables, weird spacing, and terrible formatting in general. I really suggest you fix it up a bit.

4 Likes

Theirs no issue with the code lol I fail to see the issue you see. Their are no “inconsistent” variables and the code works as intended. Thanks for the feedback always glad to hear real constructive criticisms on something I gave away for free.


Modular functions are in a table and other functions are called as local functions. This script runs very efficiently. I tested it in several iterations. It can run a swarm of 100 objects and uses minimal resouces.
The first version is listed in the parent post and the newest version is found in the published model.

3 Likes

I’m not talking about the functionality of the code, I’m talking about the organization and formatting.

For the first half of the code, the code is neatly indented and spaced apart, the variables names are all pascalCase, and lots of the code is explained with comments.


But then around at line 197 the code starts getting kind of ugly; notice how the code is only sometimes randomly indented, theres a mix between pascalCase variables and lowercase variables, the variables names are abbreviated, and the isnt spaced the best.



5 Likes

If you don’t like it you can use this online tool to format it automatically.
Lua Beautifier Online to beautify Lua code (codebeautify.org)
I’m not really big on indentation since this is Luau not a language where indentation actually matters.
In conclusion I do not care about your opinions about formatting especially in a coding language were it makes no functional difference and is rather a matter of preference and style choice.
I disable all of studios auto-indentation settings. You may not but it’s still preference and has nothing to do with the functionality or usefulness of the code I shared.

I shared this resource because I was looking for something similar in the past and found no resources on the subject.
It appears the only ones who want to share feedback are the ones who can think of something negative to say and the rest just take the free model.

Appreciation the feedback but it’s not sufficient to simply criticize formatting and style choices when I shared a Novel and useful resource that is an accurate implementation of the Boids Flocking Framework. The quality is great because I provided animated models and converted the code into a module. It works as intended and can be used on a variety of applications ranging from particle effects to swarms and even potentially a unit of navigating models moving together as a swarm like ants.

The Get Meta Table functions allow you to Dynamically change the variables of the module without editing any code. So those are the only variables you should be concerned with, considering their’s a read and write function for the variables I would further consider the concerns not urgent.

Also the functions at the end of the script are different execution methods. I kept them easy to build new functions it requires nothing in the model but a primary part. Although currently it is designed to write the position via a tween. You could instead return the positions and execute a pathing function. I also updated it to work with models in addition to parts.

3 Likes

I’d recomend putting the store link in bold, or putting the rest of the other links in Hide Details; I missed it due to it being at the top of the page

2 Likes

I understand I’ll be posting the final updates to this resource this evening. I’m currently implementing it into my Procedural world generator and will add features as needed and I’ll make sure to place the GetMetaTable and SetMetaTable functions to the top to improve readability and run the code through the Lua beautifier so the formatting is prettier.
Also I feel the top of the page is the most visible place to place the link.

4 Likes

I posted what is likely the Final update to this Resource! I have added an executable object called “SwarmFocus” you simply place the object and poof appears a random swarm! I have included the model I used to generate Pixies using my AI character generator.

I also tested this an it navigates around obstacles!

It adds nice ambience to the game. It would be really easy to make a game catching butterflies with a net.
Or even JellyFish

Here’s some inspiration

I have updated the model with non animated Jellyfish to inspire.

In the future might modularize the code more to it core components in a seperate module and different implementations in a seperate descendant module.

Also improved the readability and modularity of the code in the published model and added Dove animation

Another update I fixed the orientation of the birds so they face the direction they are moving and added a thing that randomizes the size of the bird.
I fixed this bug where it was going through objects and stuff I accidentally had this variable set to 0 and meant to be the opposite
image

Okay finally have debugged the code to make it not throw positions inside terrain with this function I had laying around I used for a previous project.

local function detecter(material)
for i,v in material do 
if material[i] then
if i~="Size" then
--if material[i][1] then
for t,o in material[i] do 
for c,p in material[i][t] do 
if material[i][t][c]~=Enum.Material.Water and material[i][t][c]~=Enum.Material.Air then
return true
end
end 
end
end
end
end
return false
end


local terrain=workspace.Terrain

local function CheckTerrain(GoalPosition)
local GoalPosition=CFrame.new(GoalPosition)
local region = Region3.new(GoalPosition.Position-Vector3.new(2,2,2),GoalPosition.Position+Vector3.new(2,2,2))
local material = terrain:ReadVoxels(region, 4)        
if  detecter(material) ==true then
repeat
GoalPosition=GoalPosition:ToWorldSpace(CFrame.new(0,2,0))
local region = Region3.new(GoalPosition.Position-Vector3.new(2,2,2),GoalPosition.Position+Vector3.new(2,2,2))
material = terrain:ReadVoxels(region, 4)            
until detecter(material) ==false
GoalPosition=GoalPosition:ToWorldSpace(CFrame.new(0,0,0))
end
return GoalPosition.Position
end```
3 Likes

This model has been updated to it most recent version!


I fixed a thing where the boids were navigating through terrain.

In addition I fixed a their orientation to have less weight on aligning with nearby boids and more facing towards where they are moving. Also added doves! Which are recolors of the crows

1 Like

I make one joke about the New York accent, and next thing I know, I am notified of every change…

2 Likes

my bad I was posting updates. The resource is completed now though. Tested and bug free

2 Likes

I have added A animated bat to this Open Source Boid Library in addition I have done some performance improvements that allow thousands of boids to be on a single game at once without using too many resources. Enjoy the boids share your use case would be cool to see =]

I also have added a Blowfly and Dragonfly rigs to this library.
image
image

Side notes! I have adjusted the behavior to have gradually changing flock formation so the boids spread out and regroup over time while also moving the entire flock

Any way of having an option where they sometimes attact ? Like the bats ? With attack time to every 40 seconds …

Perhaps option for like wayoonts , so you could set places for them to go ?

Thanks

How to use it? I’m new to roblox studio and really wan’t to have it in my so called game

1 Like

Their is a object called swarmfocus in the model that is an executable object that you can clone into the workspace.

image

The code to run a flock in a particular position is at its simplest this.

local pos=script.Parent.Parent.Position
local b=require(game.ServerStorage.BoidsLib.Boid)
b.Generate(pos)

I have updated and taken care of some bug fixes that caused some issues.

This is incredibly cool! Brilliant work!

I have made several attempts in the past to implement Boids in an RTS project, and seeing this has revitalized the idea once more! I may just get to see my army of chibi knights, thanks to you! :smile:

1 Like

Nice! Let me know if you have any issues or need any help implementing something. Happy to help :smile:
I think you could execute the function to follow a player or any moving object really by configuring the focus object or adding to the module to define a object to follow instead of the physics object.
Last thing I did on this project was update the model to its current version.

In terms of additions to this library by me I have no current ideas or ambitions beyond adding more example boids to the library.

You could add whatever you want to it, hope the updated code or the original source code help you with your project!

1 Like