My implementation of rule 2 of the boids pseudocode produces exactly the oposite of the desired behaviour(the boids get pushed away as if they each had a force field):
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. [Text beneath Rule 2]
As far as I cant tell my Code should do the exact same Thing as the pseudocode
sf::Vector2f App::rule2(Boid* b)
{
sf::Vector2f c;
for(auto& boid : boidslist)
{
if(boid != b)
{
sf::Vector2f delta = boid->position - b->position;
if(std::sqrt(delta.x * delta.x + delta.y * delta.y) < 100)
{
c = c - (boid->position - b->position);
}
}
}
return c;
}
And here's my update function to prove im doing the right thing here aswell:
void App::update(float dt)
{
sf::Vector2f v1, v2, v3;
for(auto& boid : boidslist)
{
v1 = rule1(boid);
v2 = rule2(boid);
boid->velocity = boid->velocity + v1 + v2 + v3;
boid->position = boid->position + (boid->velocity * dt * 0.1f);
}
}
PS: If you desire you can inspect the whole code(6 files) here