diff options
| author | realtradam <[email protected]> | 2021-08-09 09:15:38 -0400 |
|---|---|---|
| committer | realtradam <[email protected]> | 2021-08-09 09:15:38 -0400 |
| commit | 501ec02e894865836f2960b0bbc16b3448e3707f (patch) | |
| tree | ad2c1124e5ab5807dea1b11002cc5bf2df1226d9 /lib/rules | |
| parent | 4e909c6b44794b76ef3a98c032ea90204b673f85 (diff) | |
| download | ruboids-501ec02e894865836f2960b0bbc16b3448e3707f.tar.gz ruboids-501ec02e894865836f2960b0bbc16b3448e3707f.zip | |
ruboids!
Diffstat (limited to 'lib/rules')
| -rw-r--r-- | lib/rules/alignment.rb | 20 | ||||
| -rw-r--r-- | lib/rules/bounds.rb | 15 | ||||
| -rw-r--r-- | lib/rules/cohesion.rb | 21 | ||||
| -rw-r--r-- | lib/rules/limit.rb | 13 | ||||
| -rw-r--r-- | lib/rules/seperation.rb | 14 | ||||
| -rw-r--r-- | lib/rules/target.rb | 6 |
6 files changed, 89 insertions, 0 deletions
diff --git a/lib/rules/alignment.rb b/lib/rules/alignment.rb new file mode 100644 index 0000000..b30683c --- /dev/null +++ b/lib/rules/alignment.rb @@ -0,0 +1,20 @@ +FF::Sys.new('Alignment', priority: 50) do + group_velocity = [0.0,0.0] + boids_count = FF::Cmp::Boids.each.to_a.count + + FF::Cmp::Boids.each do |boid| + group_velocity[0] += boid.vx + group_velocity[1] += boid.vy + end + + FF::Cmp::Boids.each do |boid_update| + move_boid = group_velocity.dup + move_boid[0] -= boid_update.vx + move_boid[1] -= boid_update.vy + move_boid[0] /= boids_count - 1.0 + move_boid[1] /= boids_count - 1.0 + + boid_update.cx += (move_boid[0] - boid_update.vx) / $alignment + boid_update.cy += (move_boid[1] - boid_update.vy) / $alignment + end +end diff --git a/lib/rules/bounds.rb b/lib/rules/bounds.rb new file mode 100644 index 0000000..5045d60 --- /dev/null +++ b/lib/rules/bounds.rb @@ -0,0 +1,15 @@ +FF::Sys.new('Bounds', priority: 50) do + FF::Cmp::Boids.each do |boid| + if boid.x > $config.xmax + boid.cx -= $config.bounds_strength + elsif boid.x < $config.xmin + boid.cx += $config.bounds_strength + end + + if boid.y > $config.ymax + boid.cy -= $config.bounds_strength + elsif boid.y < $config.ymin + boid.cy += $config.bounds_strength + end + end +end diff --git a/lib/rules/cohesion.rb b/lib/rules/cohesion.rb new file mode 100644 index 0000000..efbd71d --- /dev/null +++ b/lib/rules/cohesion.rb @@ -0,0 +1,21 @@ +FF::Sys.new('Cohesion', priority: 50) do + + center_mass = [0.0,0.0] + boids_count = FF::Cmp::Boids.each.to_a.count + + FF::Cmp::Boids.each do |boid| + center_mass[0] += boid.x + center_mass[1] += boid.y + end + + FF::Cmp::Boids.each do |boid_update| + move_boid = center_mass.dup + move_boid[0] -= boid_update.x + move_boid[1] -= boid_update.y + move_boid[0] /= boids_count - 1.0 + move_boid[1] /= boids_count - 1.0 + + boid_update.cx += (move_boid[0] - boid_update.x) / $cohesion + boid_update.cy += (move_boid[1] - boid_update.y) / $cohesion + end +end diff --git a/lib/rules/limit.rb b/lib/rules/limit.rb new file mode 100644 index 0000000..c454de4 --- /dev/null +++ b/lib/rules/limit.rb @@ -0,0 +1,13 @@ +# This special function is already called by apply_boid_calculations.rb +# do not add or call this function elsewhere +FF::Sys.new('Limit') do + unless $limit < 0 + FF::Cmp::Boids.each do |boid| + absolute_velocity = Math.sqrt((boid.vx**2) + (boid.vy**2)) + if absolute_velocity > $limit + boid.vx = (boid.vx / absolute_velocity) * $limit + boid.vy = (boid.vy / absolute_velocity) * $limit + end + end + end +end diff --git a/lib/rules/seperation.rb b/lib/rules/seperation.rb new file mode 100644 index 0000000..31039f3 --- /dev/null +++ b/lib/rules/seperation.rb @@ -0,0 +1,14 @@ +FF::Sys.new('Seperation', priority: 50) do + FF::Cmp::Boids.each do |boid_update| + newvec = [0.0,0.0] + FF::Cmp::Boids.each do |boid_check| + next if boid_check == boid_update + if Math.sqrt(((-boid_check.x + boid_update.x)**2) + ((-boid_check.y + boid_update.y)**2)).abs < $seperation_distance + newvec[0] -= boid_check.x - boid_update.x + newvec[1] -= boid_check.y - boid_update.y + end + end + boid_update.cx += newvec[0] / $seperation + boid_update.cy += newvec[1] / $seperation + end +end diff --git a/lib/rules/target.rb b/lib/rules/target.rb new file mode 100644 index 0000000..ffee93a --- /dev/null +++ b/lib/rules/target.rb @@ -0,0 +1,6 @@ +FF::Sys.new('Target', priority: 50) do + FF::Cmp::Boids.each do |boid| + boid.cx += ($x_target - boid.x) / $target_strength + boid.cy += ($y_target - boid.y) / $target_strength + end +end |
