Roblox Boids Implementation

Links

GitHub | Docs | Test Place | Download .rbxmx

Introduction

Boids is an algorithm developed by Craig Reynolds with the aim of replicating the flocking behavior of birds, fish, and herding animals.

Use Cases
  • Schools of fish, a flock of birds, or insect swarms
  • Horde of zombies
  • Drones
  • Particles
  • Unit formations

Showcase

Example Usage

-- Require the FlockManager once from the server
local flockModule = game.ReplicatedStorage.FlockManager
local flockManager = require(flockModule)
local config = require(flockModule.Config)

-- Create a new group ( the group will share the config file used passed here)
local manager = flockManager.new(config.testFile)

local birds = workspace.Birds:GetChildren()

-- Add your Boids
manager:add(birds)

-- And start applying the rules
manager:start()

Features

  • The core 3 rules (separation, alignment, cohesion)
  • Obstacle avoidance
  • Target following

Optimization Techniques Used

  • Spatial Partitioning (Grids)
  • Network Replicator (To reduce the bandwidth)
  • Multithreading (Parallel Luau)

3rd Party Tools Used:

  • Packet (To handle batching and compression)
Planned
  • Moving from LinearVelocity to CFrame based movement.
  • Better steering/ smoother turning.
  • More optimizations.
    • Spreading the math across multiple frames (instead executing everything/Boid at 60 FPS).
      Sacrificing a bit of accuracy to get a big performance gain.
Known Issues
  • Objects with CanCollide disabled may pass through walls at low framerates.
    • Cause: Object movement is dependent on LinearVelocity (Roblox physics), which can update an object’s position at low framerates before the Obstacle Avoidance code can check for collisions.

Note: Some use cases may require you to modify the system,or remove some of the features, or only use a small part of it.

7 Likes