Skip to content

Instantly share code, notes, and snippets.

View rvishwajith's full-sized avatar

Rohith Vishwajith rvishwajith

View GitHub Profile
limitSpeed(Boid b)
Double maxMag = 0.033 // note this is a double, not a vector
IF magnitude(b.velocity) > maxMag
b.velocity = (b.velocity / magnitude(b.velocity)) * maxMag
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
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
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 dist(b.position, b2.position) < distance
result -= (b2.position - b.position)
RETURN result
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
centerOfMass() // A general rule for finding the overall center
Vector result
FOR b in Boids
result += b.position
result /= Boids.length
RETURN result
Boids[] // this is a global variable 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)
finalVector = vector1 + vector2 + vector3 ... + vectorX