1. Home
  2. Explore
  3. Computer Science
  4. Boids Simulation

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Boids Simulation

🐠 Boids: Simulating Flocking Behavior

Boids is a term coined by Craig Reynolds to describe bird-like objects in his paper Flocks, Herds, and Schools: A Distributed Behavioral Model. It's a simulation of emergent behavior, where complex, coordinated movement arises from a few simple rules applied to individual agents.

πŸ‘οΈ Boid Perception and Environment

Each boid needs an awareness of its surroundings to interact meaningfully.

  • Radius of Vision: A circular area around the boid within which it can sense other boids.

  • View Angle: Defines a limited field of vision, preventing the boid from "seeing" directly behind itself.

βš–οΈ The Three Core Boid Rules

The behavior of each boid is determined by three fundamental rules:

🚫 Rule 1: Separation

  • Principle: Steer away from any nearby boids.

  • Purpose: To avoid collisions and maintain a minimum distance from other individuals.

  • Effect: Leads to boids successfully dodging and swerving to prevent bumping into each other.

↔️ Rule 2: Alignment

  • Principle: Steer to move in the same direction as nearby boids.

  • Purpose: To align velocities with neighbors, promoting coherent movement.

  • Effect: Helps avoid collisions and makes the group behave more like a cohesive flock.

🀝 Rule 3: Cohesion

  • Principle: Steer towards the center of any nearby boids.

  • Purpose: To keep the boids together as a group, preventing them from scattering.

  • Effect: By itself, this rule can cause swirling patterns. When combined with separation and alignment, it helps maintain the flock's integrity while still allowing for dynamic movement.

These three simple rulesβ€”separation, alignment, and cohesionβ€”combine to give rise to complex and coordinated flocking behavior.


🚧 Obstacle Avoidance

Boids can be extended to interact with their environment, not just other boids.

  • 3D World Application: The boid rules can be applied in three dimensions.

  • Wall Avoidance: Instead of simply teleporting to the other side of a boundary, boids can be programmed to avoid walls and other obstacles.

  • Ray Casting: A common approach involves casting rays at increasing angles from the boid's forward direction until a clear path is found. This allows boids to "look" for unobstructed directions.

🌐 Generating Points on a Sphere for 3D Ray Casting

To apply obstacle avoidance in 3D, a method for generating points on a sphere is needed, particularly for casting rays in various directions. A good approach involves first generating points within a disk and then projecting them onto a sphere.

πŸŒ€ Generating Points in a Disk (Spiral Method)

This method creates a spiral pattern of points within a disk, which can then be adapted for a sphere.

  1. Loop for Number of Points: Iterate for the desired number of points.

  2. Distance Calculation: Define a distance variable that goes from 0 to 1 over the course of the loop.

  3. Angle Calculation: In each iteration, the angle increases by a fraction of a circle.

  4. Cartesian Coordinates:

    • x=distanceΓ—cos⁑(angle)x = \text{distance} \times \cos(\text{angle})x=distanceΓ—cos(angle)

    • y=distanceΓ—sin⁑(angle)y = \text{distance} \times \sin(\text{angle})y=distanceΓ—sin(angle)

  5. Plotting: Plot the calculated (x,y)(x, y)(x,y) point.

Initially, points might form a straight line. As the "turn fraction" increases, a spiral begins to form.

πŸ“ The Golden Ratio in Point Distribution

To achieve a good distribution of points and avoid alignment, an irrational number is recommended for the turn fraction. The golden ratio (Ο•\phiΟ•) is particularly effective.

  • Definition of Golden Ratio (Ο•\phiΟ•): If you have a long segment (A) and a shorter segment (B), the golden ratio is when the ratio of (A+B)(A+B)(A+B) to AAA is equal to the ratio of AAA to BBB.

    A+BA=AB=Ο•\frac{A+B}{A} = \frac{A}{B} = \phiAA+B​=BA​=Ο•
  • Mathematical Value: The positive solution to this equation is:

    Ο•=1+52β‰ˆ1.618\phi = \frac{1 + \sqrt{5}}{2} \approx 1.618Ο•=21+5β€‹β€‹β‰ˆ1.618
  • Application: Using Ο•\phiΟ• as the turn fraction results in a very nice and even distribution of points in the spiral.

🌻 Connection to Fibonacci Sequence and Sunflowers

The golden ratio is intimately linked with the Fibonacci sequence, which appears in natural patterns like sunflower seed arrangements.

  • Fibonacci Sequence: A sequence where each number is the sum of the two preceding ones (e.g., 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55...).

  • Highlighting Points: If points are generated using the golden ratio and every nth point (where n is a Fibonacci number) is highlighted, spirals emerge.

    • Highlighting every 34th point shows curves from the edge to the center.

    • Adding an offset of 13 (another Fibonacci number) reveals the next curve.

    • Counting these curves shows there are 34 of them.

    • Highlighting every 55th point (the next Fibonacci number) shows curves going in the opposite direction.

    • With an offset of 21, there are 55 such curves.

  • Approximation of Golden Ratio: The ratio of consecutive Fibonacci numbers approximates the golden ratio more accurately as the numbers get larger (e.g., 55/34β‰ˆ1.617655/34 \approx 1.617655/34β‰ˆ1.6176, close to Ο•\phiΟ•). This demonstrates how Fibonacci numbers are embedded in the golden ratio-based spiral pattern.

βš–οΈ Correcting Central Bunching

When generating points, they often cluster too much in the center.

  • Power Function for Distance: To correct this, the distance can be raised to a power.

    • If the power is 1, points are bunched.

    • If the power is 0.5 (square root), the distribution becomes more even.

    • Negative powers can create interesting, though perhaps less practical, distributions.

🌍 Projecting to a Sphere

Projecting the disk points onto a sphere requires specific mathematical modifications, often involving swapping sines and cosines in the coordinate transformations. This can be an iterative process of trial and error to get the correct spherical distribution.


🎯 Advanced Boid Behaviors

πŸ’₯ Collision Ray Casting

  • Collision Rays: A specified number of rays are generated at increasing angles.

  • Collision Checks: Boids use these rays to perform collision checks. They repeatedly check along these rays until an unobstructed direction is found.

  • Integration: This obstacle avoidance force can be combined with the existing separation, alignment, and cohesion forces to allow boids to navigate complex environments while maintaining flocking behavior.

🧲 Targeting Force

  • Goal-Oriented Movement: A targeting force can be added to make boids fly towards a specific target object (e.g., a moving sphere).

  • Combined Forces: This force interacts with the other boid rules, allowing the flock to pursue a target while still avoiding collisions and maintaining cohesion.

πŸš€ Performance Optimization: Spatial Partitioning & Parallel Processing

Simulating many boids can be computationally intensive, especially when each boid has to check every other boid for interactions (O(N2)O(N^2)O(N2) complexity).

πŸ“¦ Spatial Partitioning

  • Concept: Divide the simulation space into a grid of cells.

  • Mechanism: Each boid is placed into a cell based on its position.

  • Benefit: Boids only need to check for interactions with other boids in their neighboring cells, significantly reducing the number of checks.

πŸ’» Parallel Processing with Shaders

  • GPU Acceleration: While spatial partitioning helps, processing the boid logic on a computer shader can be even faster.

  • Parallel Execution: Shaders allow computations to be performed in parallel across many threads, making it very efficient for simulations with many agents.

  • Combined Approach: The most optimized solution would combine spatial partitioning with shader-based parallel processing for maximum performance.

🐟 Real-World Application: Fish in an Underwater Scene

Boids are a perfect fit for simulating animal behavior, such as schools of fish.

  • Fish Model: A 3D fish model can be used.

  • Vertex Shader for Movement: A shader can be applied to "wiggle" the vertices of the fish model, creating the illusion of swimming.

  • Flocking Simulation: The boid rules (separation, alignment, cohesion) can then be applied to these fish models to create a realistic and dynamic school of fish.

Ask anything

We've read this whole study set, so questions about it get the best answers.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent