This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
centerOfMass() // A general rule for finding the overall center | |
Vector result | |
FOR b in Boids | |
result += b.position | |
result /= Boids.length | |
RETURN result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |