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

Speaker 1Speaker 2

Boids Simulation

7:37
0:00-7:37
  1. Michael

    Hey Hope, welcome to Coding Adventures! Today we're diving into something really interesting: 'boids.' Have you heard that term before? It's a bit unusual, right?

  2. Hope

    I'm excited to be here, Michael! 'Boids' does sound a bit quirky. From what I understand, it's a term coined by Craig Reynolds in his paper on flocks, herds, and schools, referring to bird-like objects in a simulation. It's such a clever way to describe them.

  3. Michael

    Exactly! After reading his paper, I actually created some small flying triangles to represent these boids. But for them to do anything truly interesting, they needed to be aware of their surroundings. We can highlight one to observe its behavior.

  4. Hope

    That makes sense. So, this 'subject boid' needs to sense others within a certain range, which you've visualized as a radius of vision. And there's also a view angle, so it can't see directly behind itself, which adds a layer of realism, I imagine.

  5. Michael

    Precisely. Reynolds' paper describes three core rules that govern each boid's behavior. Rule number one is 'separation.' This one makes boids steer away from nearby boids to prevent collisions, which is pretty fundamental for flocking.

  6. Hope

    Ah, so with separation enabled, you'd see them successfully dodging and swerving, right? It's like a built-in personal space bubble. That's crucial for avoiding a chaotic pile-up when you have many entities moving together.

  7. Michael

    Exactly. Then we have rule number two: 'alignment.' This tells boids to steer in the same direction as their nearby counterparts. It helps them avoid bumping into each other and, more importantly, makes them move cohesively like an actual flock.

  8. Hope

    Alignment is what gives the flock that coordinated, almost hypnotic motion. Without it, they'd just be a group of individual boids avoiding each other, rather than moving as a unified entity. It's fascinating how simple rules create complex patterns.

  9. Michael

    It truly is. The final rule, number three, is 'cohesion.' This rule instructs boids to steer towards the center of any nearby boids. By itself, cohesion can make them form swirling patterns as they try to cluster together.

  10. Hope

    So, when you combine all three—separation, alignment, and cohesion—it's remarkable how they interact. They're trying not to crash, moving in the same direction, and also trying to stay near the 'center' of their flock mates. It's a delicate balance.

  11. Michael

    It really is. It's incredibly cool how these three simple rules, commonly called separation, alignment, and cohesion, combine to produce what looks like complex, coordinated behavior. They give rise to emergent properties, which is always mind-blowing.

  12. Hope

    And the beauty is these rules aren't limited to a 2D plane; they can be applied in a three-dimensional world too. You mentioned your boids were initially just passing through walls, but you wanted them to avoid obstacles. How did you tackle that?

  13. Michael

    My idea was to cast rays at increasing angles until a clear path was found. To apply this in 3D, I needed a way to generate points on a sphere. After some research, I found an approach that starts by generating points within a 2D disk.

  14. Hope

    Generating points on a sphere sounds like a neat challenge. So, for the 2D disk, you're using a loop, defining a distance from 0 to 1, and an angle that increments by a fraction of a circle each iteration. Then you calculate X and Y using cosine and sine, right?

  15. Michael

    Exactly. Initially, if the turn fraction is small, the points just line up. But as the turn fraction increases, you start to see a spiral forming, which is visually pretty cool. You can play around with different values and see the patterns emerge.

  16. Hope

    I can imagine! So, to get a good distribution of points and avoid them lining up, you need a specific type of number for the turn fraction. I'm guessing it's an irrational number, and probably a famous one at that?

  17. Michael

    You're spot on! The recommendation is to use the golden ratio, which is represented by the symbol Phi. For those who might have forgotten, it's essentially when the ratio of the sum of two segments to the longer segment equals the ratio of the longer to the shorter.

  18. Hope

    Ah, the golden ratio! That's one plus the square root of five, all divided by two, roughly 1.618. I remember that from geometry class. Plugging that in as the turn fraction must give a really even and aesthetically pleasing distribution.

  19. Michael

    It absolutely does. It creates a beautiful, even spread of points. I actually got a bit sidetracked playing with this pattern because it looked so interesting, adding code to highlight every nth point.

  20. Hope

    That's a fun rabbit hole to go down! And this is where the connection to sunflowers and the Fibonacci sequence comes in, isn't it? I've heard about it but never fully understood the link.

  21. Michael

    Yes! If you list the Fibonacci numbers and then highlight, say, every 34th point, you see a curve from the edge to the center. If you then add an offset of 13, another Fibonacci number, you hop to the next curve. It's amazing how they're embedded.

  22. Hope

    So, if you highlight every 55th point, which is the next Fibonacci number, you get curves going in the opposite direction. And 55 divided by 34 approximates the golden ratio! It's incredible how these mathematical concepts manifest in natural patterns like that.

  23. Michael

    It's truly fascinating how nature seems to follow these mathematical principles. But back to the point distribution – the points were bunched up in the center, so I had to correct that by raising the distance to a power.

  24. Hope

    Right, if you set that power to 0.5, or the square root, it spreads them out more evenly, which makes sense. It's all about fine-tuning these parameters to achieve the desired effect. And then you had to project this onto a sphere.

  25. Michael

    Yes, projecting to a sphere needed a few modifications. I initially ended up with a highly un-spherical shape, but after swapping some sines and cosines around and a bit of trial and error, it finally worked. It was a fun challenge.

  26. Hope

    That's the iterative process of coding for you! So, now with the sphere and the correct point distribution, you could go back to the boids and implement the collision avoidance using those rays you mentioned earlier.

  27. Michael

    Exactly. I can now specify the number of collision rays and the view angle. This generates an array of vectors. Each boid then uses these rays to check for obstructions at increasing angles from its forward direction until it finds a clear path.

  28. Hope

    You tested it with a single boid and a moving block, and it seemed to work well. Then, letting a bunch of them fly around an empty room, the new obstacle avoidance force integrated nicely with the existing separation, alignment, and cohesion rules.

  29. Michael

    It did! I also experimented with adding a targeting force, so the boids would try to fly towards a sphere as I moved it around. That added another layer of dynamic interaction to their behavior.

  30. Hope

    That sounds like a great way to make them respond to their environment in a more directed way. And for the ultimate test, you created a more elaborate setup: a course with an arch and some hoops. How did they fare navigating that?

  31. Michael

    They did pretty well, but I hit a performance issue. Each boid had to loop over every other boid to find those nearby, which is computationally expensive. So, I implemented a spatial partitioning scheme.

  32. Hope

    Ah, spatial partitioning! So instead of checking every single boid, they only interact with boids in their own cell and neighboring cells, right? That should drastically reduce the number of calculations needed.

  33. Michael

    It did help a lot, but surprisingly, I found it was actually faster to just do the crude loop over all boids, but inside a compute shader, so it could be done in parallel. Combining partitioning with a shader would be even faster, but that's a future project.

  34. Hope

    That's an interesting discovery about the shader's power! It's always about optimizing. And finally, you brought this whole boids concept to life in your underwater scene, turning those comments about fish into a reality.

  35. Michael

    Yes! Boids are perfect for simulating fish. I downloaded a fish model and found a shader online that wiggles the vertices to make it look like it's swimming. Now I can just 'board my submarine and go swimming with the fishes!'

Ask anything

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

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent