Boids nearest 7 neighbours

I’m trying to adapt https://www.processing.org/examples/flocking.html limiting the number of neighbours a boid can see to 7 based on closest distance. But I am struggling can someone help

2 Likes

Hi,

Welcome to the community! :slight_smile:

You could use the following technique :

  • For a specific boid, you compute the distance to all other boids and store them in a Map (like HashMap<Boid,Float>, FLoat being the distance and notice that Float is an Object).
  • You sort the Map based on the values (the distances) and you take only the 7 closest boids.
  • You apply the steering behavior to the boid based on those 7 boids

In fact you can generalize this to n noids, n being the number of boids that it can see.

2 Likes

Hey, and welcome to the forum! Nice to have you here.

1 Like

Hello again, how does one sort the hashmap based on values?

In fact, it’s a little bit tricky :

1 Like