Skip to content

Instantly share code, notes, and snippets.

@pjpetersik
Last active April 6, 2022 10:26
using StatsBase
function flocking(agent::Agent, model::Model)
N = 0
repel = [0.0; 0.0]
cohere = [0.0; 0.0]
for other_agent in model.peloton
if agent != other_agent
N += 1
heading = other_agent.position - agent.position
distance = sum((heading).^2)^0.5
cohere += heading
if distance < 0.1 && heading[1] > 0
angle_factor = abs(acot(heading[2] / heading[1])) / π * 2
repel -= heading ./ distance^2 * angle_factor
end
end
end
velocity = agent.cohere_factor * cohere / N + agent.repel_factor * repel / N
return velocity
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment