Search Instagram Twitter Facebook Spotify Grid Tag Date Folder Chat Pencil

Flocking Study 2

Continued to study flocking behaviour. Beginning to use alignment forces and predators:

Flocking Study 2 from William Chyr on Vimeo.

Again, most of this is based on Robert Hodgin’s tutorial on flocking, specifically chapter 5.

Origin

I had originally wanted to create a particle system typography, in which various particles would fly in from one side of the screen, come together to form a word, and then fly off in random directions. My initial approach was to simply change the prey particles to be attracted to the predator particles, instead of being repelled by them. Then, all I would need to do is set up the predator particles in the arrangement of the word I want to show.

However, it turns out to be a lot more complicated than that. When there’s only one predator, it’s pretty straightforward. But when there are multiple attractor predators, it is the sum of their attraction forces acting on the prey particles. This makes it much more difficult to precisely determine the final shape that the prey particles take on. For example, when the predators are lined up in a straight line, the prey particles end up circling it perpendicularly, as shown in the video above ( the predators aren’t drawn).

Changes to Code

The first big change was to change the prey particles’ response to the predators, to make them attract instead of repel. I made it so that if the prey particles are a certain distance away from the predators, then they begin to feel an attraction force.

1
2
3
4
5
6
7
8
9
10
for( list::iterator predator = mPredators.begin(); predator != mPredators.end(); ++predator )
{
Vec3f dir = p1->mPos - predator->mPos[0];
float distSqrd = dir.lengthSquared();
if( distSqrd > predatorZoneRadiusSqrd ){
dir.normalize();
float pullStrength = 0.001f;
p1->mVel -= dir * ( ( distSqrd - predatorZoneRadiusSqrd ) * pullStrength );
}
}

Then I made the predators so that they did not move, and remained stationary. That was pretty straight forward: simply comment out the lines in Predator::update ( bool flatten ) which update the velocity component.

I then created a function in the ParticleController class called addStationaryPredators( ), which in this case just adds 10 predators in a line, spaced equally from each other:

1
2
3
4
5
6
7
8
void ParticleController::addStationaryPredators()
{
for (int i = 0; i < 10; i++){
Vec3f pos = Vec3f(-40 + i*20.0f, 0.0f, 0.0f);
Vec3f vel = Vec3f(0.0f, 0.0f, 0.0f);
mPredators.push_back( Predator( pos, vel ) );
}
}

Source Code

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.