0

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

sro5h
  • 322
  • 2
  • 12
  • Is `sf::Vector2f` default-initialized to (0,0)? If not, then the code (as quoted here) exhibits undefined behaviour (because `c` and `v3` are not initialized). – mindriot Apr 27 '16 at 11:32
  • Also, what are the semantics of calling `boid != b`? If this is a comparison of the boids' states, rather than their IDs, you should replace that with an index comparison to be sure. – mindriot Apr 27 '16 at 11:35
  • yes it is Default-initialized to (0, 0). – sro5h Apr 27 '16 at 11:36
  • It compares two Boid* pointers both stored in the same std::vector – sro5h Apr 27 '16 at 11:37
  • Oh yes, just spotted the `Boid*` in the function signature. – mindriot Apr 27 '16 at 11:40
  • I recommend commenting out the call for rule 1, so you can be sure that it is really rule 2 causing the effect. Then, take only two boids, and initialize their positions/velocities in a fixed manner so you know what motions you would _expect_. – mindriot Apr 27 '16 at 11:41
  • Im gonna try it.Shouldnt the outcome of rule 2 be dependent on the distance. Do I dont check for the distance but multiply the velocitychange with the distance? – sro5h Apr 27 '16 at 11:42
  • I tested it with 2 boids and low velocities, they instantly turn around. I also disabled rule 1 – sro5h Apr 27 '16 at 11:56
  • I now changed the code so that rule 2 Returns `(boid->position - b->position) * (1 / | boid->position - b->position |)` if `(| boid->position - b->position |)` is less than 100. It is smooth now but with higher velocities boids go through each other, is that aceptable? – sro5h Apr 27 '16 at 12:06
  • I'm not sure if that pseudocode is really accurate, but I saw that you add `boid->velocity * dt * 0.1f` instead of just `boid->velocity`. Why did you use that? That definitely gives different behavior to the pseudocode. Have you tried removing the scale factors? – mindriot Apr 27 '16 at 12:36
  • dt is the deltatime since the last update() call, since I dont use a fixed time step thats the way i make sure the velocity I add doesnt fluctuate because the program somtimes runs slower. The factory was just an arbitrary number to control the Simulation Speed. I removed it now and no it didnt affect the behaviour of the boids, just the Speed at which it happens. Yes I think that the pseudocode is Kind of weird I once read it like a year ago somewhere else and they used the same Approach as I mentioned in my last comment I think. For now erverything seems to work. Ill post an answer here – sro5h Apr 27 '16 at 12:45

0 Answers0