Skip to content

Instantly share code, notes, and snippets.

@rvishwajith
Created January 13, 2021 05:59
Show Gist options
  • Save rvishwajith/db6aa1af6795693033557c2adee3720c to your computer and use it in GitHub Desktop.
Save rvishwajith/db6aa1af6795693033557c2adee3720c to your computer and use it in GitHub Desktop.
Boids[] // this is a global vairable accesible in other methods
Update()
FOR b in Boids
vector1 = rule1(b) // Boids flock to the center of mass
vector2 = rule2(b) // Boids avoid other boids
vector3 = rule3(b) // Boids try to match the speed of other boids
// additional rules can be added directly after
vector4 = rule4(b)
vectorX = ruleX(b)
// limiting speed after all changes to velocity
finalVector = vector1 + vector2 + vector3 ... + vectorX
b.velocity += finalVector // Adjust direction and speed
b.position += b.velocity // Update the position to the new positio
rule1(Boid b)
Vector pC = <0, 0, 0> // Number of dimensions can change
FOR b2 in Boids
IF b != b2 // Ignore duplicate boids
pC += b2.position
pC = pC / (Boids.length - 1)
result = (pC - b.position) / 200 // 0.5% towards the percieved center
RETURN result
rule2(Boid b)
distance = 100 // Threshold of distance between boids
result = <0, 0, 0>
FOR b2 in Boids
IF b != b2 // Ignore duplicate boids
IF magnitude(b.position - b2.position) < distance
result -= (b2.position - b.position)
RETURN result
rule3(Boid b)
Vector pV = <0, 0, 0> // Number of dimensions can change
FOR b2 in Boids
IF b != b2 // Ignore duplicate boids
pV += b2.velocity
pV = pV / (Boids.length - 1)
result = (pV - b.velocity) / 10 // 0.5% towards the percieved center
RETURN result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment