From 501ec02e894865836f2960b0bbc16b3448e3707f Mon Sep 17 00:00:00 2001 From: realtradam Date: Mon, 9 Aug 2021 09:15:38 -0400 Subject: ruboids! --- lib/rules/alignment.rb | 20 ++++++++++++++++++++ lib/rules/bounds.rb | 15 +++++++++++++++ lib/rules/cohesion.rb | 21 +++++++++++++++++++++ lib/rules/limit.rb | 13 +++++++++++++ lib/rules/seperation.rb | 14 ++++++++++++++ lib/rules/target.rb | 6 ++++++ 6 files changed, 89 insertions(+) create mode 100644 lib/rules/alignment.rb create mode 100644 lib/rules/bounds.rb create mode 100644 lib/rules/cohesion.rb create mode 100644 lib/rules/limit.rb create mode 100644 lib/rules/seperation.rb create mode 100644 lib/rules/target.rb (limited to 'lib/rules') 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 -- cgit v1.2.3