Skip to main content

Swarm


The Swarm object


Brakeza3D includes a dedicated object for simulating flocking behaviour, called Swarm.

A Swarm manages a group of scene objects (called boids) that move together using the classic Reynolds boids algorithm, which combines three steering forces:

  • Separation — boids steer away from neighbours that are too close.
  • Alignment — boids steer towards the average heading of nearby neighbours.
  • Cohesion — boids steer towards the average position of nearby neighbours.

A Swarm also supports predator objects that make boids flee, and an axis-aligned bounding box (AABB) that keeps the flock inside a defined volume.

Any Object3D already present in the scene can be assigned as a boid or predator — the Swarm controls their position every frame but does not own them.


Editor workflow


  1. Add a Swarm object via the Add Object menu in the editor.
  2. Add the boid/predator meshes to the scene normally (e.g. Mesh3D).
  3. Open the Swarm Members section in the properties panel.
  4. Use the combo selector to assign scene objects as boids or predators, then click Assign.
  5. Press Load members to resolve any pending names saved in the scene file.

Enable Debug AABB in the inspector to visualise the swarm volume as a yellow wireframe box.


Properties


Volume

PropertyTypeDescription
sizeVertex3DHalf-size of the AABB that bounds the flock
debugboolDraw the AABB wireframe each frame when true

Boid weights

PropertyTypeDefaultDescription
separationWeightfloat1.2Strength of the separation steering force
alignmentWeightfloat0.9Strength of the alignment steering force
cohesionWeightfloat0.7Strength of the cohesion steering force

Movement

PropertyTypeDefaultDescription
neighborDistfloat2.0Radius within which other boids are considered neighbours
maxSpeedfloat0.8Maximum velocity magnitude for each boid
velocityBoidsFactorfloat5.0Global multiplier applied to boid velocity each frame
turnFactorfloat0.15How sharply boids turn when approaching the AABB boundary

Center attraction

PropertyTypeDefaultDescription
centerThresholdfloat1.0Distance from center below which attraction is not applied
centerAttractionWeightfloat0.5Strength of the force pulling boids back towards the swarm center

Predator avoidance

PropertyTypeDefaultDescription
predatorThresholdfloat5.0Radius within which a predator triggers avoidance
predatorAvoidanceWeightfloat50.0Strength of the force pushing boids away from a predator

Global bias

These multipliers scale their respective steering forces globally, making it easy to tune the overall balance of the flock without touching individual weights.

PropertyTypeDefaultDescription
globalBiasSeparationfloat1.0Global multiplier for separation
globalBiasAlignmentfloat1.0Global multiplier for alignment
globalBiasCohesionfloat1.0Global multiplier for cohesion

Scene file format


A Swarm is serialised like any other Object3D but with additional fields:

{
"name": "SwarmDemo",
"type": 12,
"position": { "x": 0, "y": 5, "z": 0 },
"size": { "x": 10, "y": 10, "z": 10 },
"debug": false,
"separationWeight": 1.2,
"alignmentWeight": 0.9,
"cohesionWeight": 0.7,
"neighborDist": 2.0,
"maxSpeed": 0.8,
"velocityBoidsFactor": 5.0,
"turnFactor": 0.15,
"centerThreshold": 1.0,
"centerAttractionWeight": 0.5,
"predatorThreshold": 5.0,
"predatorAvoidanceWeight": 50.0,
"globalBiasSeparation": 1.0,
"globalBiasAlignment": 1.0,
"globalBiasCohesion": 1.0,
"boids": ["Boid_1", "Boid_2", "Boid_3"],
"predators": ["Predator_1"]
}

The "boids" and "predators" arrays store the names of other scene objects. On load, the engine resolves these names after all objects have been added to the scene.


Example — DemoScene05


assets/scenes/DemoScene05.json ships with the engine and demonstrates a fully configured swarm:

  • 30 icosphere boids scattered around the origin.
  • 1 red cube predator at (6, 5, 0) that the boids flee from.
  • Swarm volume: 10 × 10 × 10, centered at (0, 5, 0).
  • Strong predator avoidance (predatorAvoidanceWeight = 50) so the flight pattern is clearly visible.

Load the scene, press Load members in the Swarm inspector, and the flock will start moving immediately.