Skip to content

Error in calculation of avoidance contribution in Boids pseudocode #7

@martinbudden

Description

@martinbudden

Hi,

I've found an error in the Boids pseudocode.

In the algorithm you have:

if (squared_distance < protected_range_squared):
    # If so, calculate difference in x/y-coordinates to nearfield boid
    close_dx += boid.x - otherboid.x 
    close_dy += boid.y - otherboid.y
...then later
# Add the avoidance contribution to velocity
boid.vx = boid.vx + (close_dx*avoidfactor)
boid.vy = boid.vy + (close_dy*avoidfactor)

The trouble with this is that the "repulsion force" represented by close_dx*avoidfactor decreases as the boids get closer together. In particular the repulsion force has value protected_range*avoidfactor when the boids are protected_range apart which then decreases down to zero when the distance between the boids is zero.

We want it to be the other way around, that is we want the repulsion force to be zero when the distance between the boids is protected_range (or greater), and have the repulsion force increase up to value protected_range*avoidfactor when the distance between the boids is zero.

So the pseudocode becomes something like:

turnX = (close_dx<=0) ? - proteced_range - close_dx : proteced_range - close_dx
turnY = (close_dy<=0) ? - proteced_range - close_dy : proteced_range - close_dy
...then later
boid.vx = boid.vx + (turnX*avoidfactor)
boid.vx = boid.vx + (turnY*avoidfactor)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions