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
Welcome to the community!
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
Floatis 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 being the number of boids that it can see.
Hey, and welcome to the forum! Nice to have you here.
Hello again, how does one sort the hashmap based on values?
In fact, it’s a little bit tricky :
- https://dzone.com/articles/how-to-sort-a-map-by-value-in-java-8 (Java 8 streams)