Finding length of hypoteneuse involves a slow sqrt()
.
But it would suffice to compare sum of squared deltas against radius ** 2
radius2.
And then a good optimizing compiler would hoist it out of the loop,
or you could help it out by assigning a radius_squared = radius *** 2radius
temp var.
Finding length of hypoteneuse involves a slow sqrt()
.
But it would suffice to compare sum of squared deltas against radius ** 2
.
And then a good optimizing compiler would hoist it out of the loop,
or you could help it out by assigning a radius_squared = radius ** 2
temp var.
Finding length of hypoteneuse involves a slow sqrt()
.
But it would suffice to compare sum of squared deltas against radius2.
And then a good optimizing compiler would hoist it out of the loop,
or you could help it out by assigning a radius_squared = radius * radius
temp var.
I gotta tell ya, QuadTrees is definitely the appropriate datastructure.
And it's just not that hard.
Let coord
be the bit interleaving of x
and y
coords.
That is, the top two bits are MSB of x and MSB of y.
For the next two bits, keep shifting.
Last two bits are LSB of x and y.
I'm assuming htonl()
network order.
Now sort in the usual way, and locate a boid at index i
.
All the boids that are spatially near will also have nearby indexes.
I gotta tell ya, QuadTrees is definitely the appropriate datastructure.
And it's just not that hard.
Let coord
be the bit interleaving of x
and y
coords.
That is, the top two bits are MSB of x and MSB of y.
For the next two bits keep shifting.
Last two bits are LSB of x and y.
Now sort in the usual way, and locate a boid at index i
.
All the boids that are spatially near will also have nearby indexes.
I gotta tell ya, QuadTrees is definitely the appropriate datastructure.
And it's just not that hard.
Let coord
be the bit interleaving of x
and y
coords.
That is, the top two bits are MSB of x and MSB of y.
For the next two bits, keep shifting.
Last two bits are LSB of x and y.
I'm assuming htonl()
network order.
Now sort in the usual way, and locate a boid at index i
.
All the boids that are spatially near will also have nearby indexes.
aggregate weights
I was kind of expecting to see "the N-body problem" in the update loop, where we sum up gravitational attraction of our neighbors, or compute some similar potential function.
Distant boids make a negligible contribution, so you can approximate by ignoring them beyond some cutoff threshold. Aggregating their statistics to a bin, and mostly having those numbers influence velocity is an approach worth pursuing. (Maybe avoid aggregation for just the current bin a boid is in.) It might motivate using more than a 3 ×ばつ 3 bin window.
square bins
I note in passing that bins have the same aspect ratio as your display window. So you might want e.g. 20 ×ばつ 15 bins for certain display sizes, if you're applying an isotropic potential function.
aggregate weights
I was kind of expecting to see "the N-body problem" in the update loop, where we sum up gravitational attraction of our neighbors, or compute some similar potential function.
Distant boids make a negligible contribution, so you can approximate by ignoring them beyond some cutoff threshold. Aggregating their statistics to a bin, and mostly having those numbers influence velocity is an approach worth pursuing. (Maybe avoid aggregation for just the current bin a boid is in.) It might motivate using more than a 3 ×ばつ 3 bin window.
square bins
I note in passing that bins have the same aspect ratio as your display window. So you might want e.g. 20 ×ばつ 15 bins for certain display sizes, if you're applying an isotropic potential function.