: Boids try to fly towards the center of mass of neighboring boids. The center o
ID: 3546050 • Letter: #
Question
: Boids try to fly towards the center of mass of neighboring boids.
The center of mass is of boid j is simply the average position of all the boids excluding. 2
Assume we have N boids, called b1, b2, ..., bN. The center of mass of boid bj is
CMj
.x = (b1.x + b2.x + ... + bJ-1.x +
bJ+1.x + ... + bN.x) / (N-1)
and
CMj
.y = (b1.y + b2.y + ... + bJ-1.y +
bJ+1.y + ... + bN.y) / (N-1)
The boid j then moves 1% of the way towards the centre .
bj.x = bj.x + (bj.x-CMj.x)*0.01
bj.y = bj.y + (bj.y-CMj.y)*0.01
Repeat this for all boids.
Rule 2: Boids try to keep a small distance away from boids.
The purpose of this rule is to for boids to make sure they don't collide into each other. For boid j
If |bj.x
Explanation / Answer
The 'centre of mass' is simply the average position of all the boids. I use the term centre of mass by analogy with the corresponding physical formula (however we ignore individual masses here and treat all boids having the same mass).
Assume we have N boids, called b1, b2, ..., bN. Also, the position of a boid b is denoted b.position. Then the 'centre of mass' c of all N boids is given by:
Remember that the positions here are vectors, and N is a scalar.
However, the 'centre of mass' is a property of the entire flock; it is not something that would be considered by an individual boid. I prefer to move the boid toward its 'perceived centre', which is the centre of all the other boids, not including itself. Thus, for boidJ (1 <= J <= N), the perceived centre pcJ is given by:
Having calculated the perceived centre, we need to work out how to move the boid towards it. To move it 1% of the way towards the centre (this is about the factor I use) this is given by (pcJ - bJ.position) / 100.
Summarising this in pseudocode:
Thus we have calculated the first vector offset, v1, for the boid.
The purpose of this rule is to for boids to make sure they don't collide into each other. I simply look at each boid, and if it's within a defined small distance (say 100 units) of another boid move it as far away again as it already is. This is done by subtracting from a vector c the displacement of each boid which is near by. We initialise c to zero as we want this rule to give us a vector which when added to the current position moves a boid away from those near it.
In pseudocode:
It may seem odd that we choose to simply double the distance from nearby boids, as it means that boids which are very close are not immediately "repelled". Remember that if two boids are near each other, this rule will be applied to both of them. They will be slightly steered away from each other, and at the next time step if they are still near each other they will be pushed further apart. Hence, the resultant repulsion takes the form of a smooth acceleration. It is a good idea to maintain a principle of ensuring smooth motion. If two boids are very close to each other it's probably because they have been flying very quickly towards each other, considering that their previous motion has also been restrained by this rule. Suddenly jerking them away from each other, such that they each have their motion reversed, would appear unnatural, as if they bounced off each other's invisible force fields. Instead, we have them slow down and accelerate away from each other until they are far enough apart for our liking.
This is similar to Rule 1, however instead of averaging the positions of the other boids we average the velocities. We calculate a 'perceived velocity', pvJ, then add a small portion (about an eighth) to the boid's current velocity.
That's all there is to it :) The three rules are fairly simple to implement.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.