summaryrefslogtreecommitdiffhomepage
path: root/lib
diff options
context:
space:
mode:
authorrealtradam <[email protected]>2021-08-09 09:15:38 -0400
committerrealtradam <[email protected]>2021-08-09 09:15:38 -0400
commit501ec02e894865836f2960b0bbc16b3448e3707f (patch)
treead2c1124e5ab5807dea1b11002cc5bf2df1226d9 /lib
parent4e909c6b44794b76ef3a98c032ea90204b673f85 (diff)
downloadruboids-501ec02e894865836f2960b0bbc16b3448e3707f.tar.gz
ruboids-501ec02e894865836f2960b0bbc16b3448e3707f.zip
ruboids!
Diffstat (limited to 'lib')
-rw-r--r--lib/00-define.rb2
-rw-r--r--lib/01-render.rb20
-rw-r--r--lib/02-apply_boid_calculations.rb48
-rw-r--r--lib/03-cohesion.rb34
-rw-r--r--lib/rules/alignment.rb20
-rw-r--r--lib/rules/bounds.rb15
-rw-r--r--lib/rules/cohesion.rb21
-rw-r--r--lib/rules/limit.rb13
-rw-r--r--lib/rules/seperation.rb (renamed from lib/04-seperation.rb)14
-rw-r--r--lib/rules/target.rb6
10 files changed, 128 insertions, 65 deletions
diff --git a/lib/00-define.rb b/lib/00-define.rb
index b0606a2..0075b7f 100644
--- a/lib/00-define.rb
+++ b/lib/00-define.rb
@@ -1,5 +1,5 @@
FF::Stg.add FF::Scn.new('Default')
-FF::Stg.add FF::Scn.new('BoidCalculations')
+FF::Scn.new('BoidCalculations')
FelFlame::Components.new('Boids',
# holds the object to render
diff --git a/lib/01-render.rb b/lib/01-render.rb
index e3a1271..a4542e9 100644
--- a/lib/01-render.rb
+++ b/lib/01-render.rb
@@ -1,21 +1,13 @@
-#FF::Scn::Default.add(FelFlame::Systems.new('Render', priority: 99) do
-# Camera._redraw
-#end)
FF::Scn::Default.add(FelFlame::Systems.new('Render', priority: 99) do
FelFlame::Components::Boids.each do |boid|
renderable = boid.entities[0].components[FF::Cmp::BoidVisuals][0]
- renderable.vect.x2 = renderable.vect.x1 = renderable.obj.x = boid.x + renderable.obj.radius
- renderable.vect.y2 = renderable.vect.y1 = renderable.obj.y = boid.y + renderable.obj.radius
- renderable.vect.x1 += renderable.obj.radius
- renderable.vect.y1 += renderable.obj.radius
+ renderable.vect.x1 = renderable.obj.x = boid.x + renderable.obj.width
+ renderable.vect.y1 = renderable.obj.y = boid.y + renderable.obj.height
+ renderable.vect.x2 = renderable.vect.x1 += renderable.obj.width / 2.0
+ renderable.vect.y2 = renderable.vect.y1 += renderable.obj.height / 2.0
- renderable.vect.x2 += (boid.vx * 3) + (renderable.obj.radius * 2)
- renderable.vect.y2 += (boid.vy * 3) + (renderable.obj.radius * 2)
- #Circle.draw(x: boid.x,
- # y: boid.y,
- # color: [0.86,0.57,0.96,1],
- # radius: 7,
- # sectors: 10)
+ renderable.vect.x2 += (boid.vx * 10.0)
+ renderable.vect.y2 += (boid.vy * 10.0)
end
Camera._redraw
end)
diff --git a/lib/02-apply_boid_calculations.rb b/lib/02-apply_boid_calculations.rb
index 250a394..8b84ab5 100644
--- a/lib/02-apply_boid_calculations.rb
+++ b/lib/02-apply_boid_calculations.rb
@@ -1,15 +1,47 @@
-FF::Scn::BoidCalculations.add(FF::Sys.new('ApplyBoidCalculations', priority: 51) do
+FF::Scn::BoidCalculations.add(FF::Sys.new('ApplyBoidCalculations', priority: 75) do
+ @center ||= Camera::Circle.new(color: [0.25,0.0,0.5,0.5],
+ radius: 10,
+ sectors: 10,
+ z: -49)
+ @center_vel ||= Camera::Line.new(color: [0.25,0.0,0.5,0.5],
+ width: 12,
+ z: -50)
+ unless $config.debug
+ @center.remove
+ @center_vel.remove
+ end
+
+ group_velocity = [0.0,0.0]
+ center_mass = [0.0,0.0]
+ boids_count = FF::Cmp::Boids.each.to_a.count
+
FF::Cmp::Boids.each do |boid|
boid.vx += boid.cx
boid.vy += boid.cy
+ FF::Sys::Limit.call
boid.x += boid.vx
boid.y += boid.vy
- #boid.obj.x = boid.obj.x
- #boid.vect.x1 = boid.obj.x + 7
- #boid.vect.y1 = boid.obj.y + 7
- #boid.vect.x2 = boid.obj.x + (boid.vx * 3) + 7
- #boid.vect.y2 = boid.obj.y + (boid.vy * 3) + 7
- boid.cx = 0
- boid.cy = 0
+
+ boid.cx = 0.0
+ boid.cy = 0.0
+
+
+ center_mass[0] += boid.x
+ center_mass[1] += boid.y
+ group_velocity[0] += boid.vx
+ group_velocity[1] += boid.vy
+ end
+
+ @center_vel.x1 = @center.x = (center_mass[0] / boids_count)
+ @center_vel.y1 = @center.y = (center_mass[1] / boids_count)
+
+ @center_vel.x1 += @center.radius
+ @center_vel.y1 += @center.radius
+
+ if $follow
+ Camera.x = @center_vel.x1 = @center.x + @center.radius
+ Camera.y = @center_vel.y1 = @center.y + @center.radius
end
+ @center_vel.x2 = @center_vel.x1 + ((group_velocity[0] / boids_count) * 10.0)
+ @center_vel.y2 = @center_vel.y1 + ((group_velocity[1] / boids_count) * 10.0)
end)
diff --git a/lib/03-cohesion.rb b/lib/03-cohesion.rb
deleted file mode 100644
index 49ede9c..0000000
--- a/lib/03-cohesion.rb
+++ /dev/null
@@ -1,34 +0,0 @@
-FF::Scn::BoidCalculations.add(FF::Sys.new('Cohesion', priority: 50) do
- @center ||= Camera::Circle.new(color: [1,0.3,0.4,1],
- radius: 15,
- sectors: 10)
-
- center_mass = [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
-
-
- center_mass[0] #/= boids_count
- center_mass[1] #/= boids_count
-
- @center.x = (center_mass[0] / boids_count)
- @center.y = (center_mass[1] / boids_count)
-
- #Camera.x = center_mass[0] / boids_count
- #Camera.y = center_mass[1] / boids_count
-
- 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
- move_boid[1] /= boids_count - 1
-
- boid_update.cx += (move_boid[0] - boid_update.x) / 1000.0
- boid_update.cy += (move_boid[1] - boid_update.y) / 1000.0
- end
-end)
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/04-seperation.rb b/lib/rules/seperation.rb
index dad82f6..31039f3 100644
--- a/lib/04-seperation.rb
+++ b/lib/rules/seperation.rb
@@ -1,16 +1,14 @@
-FF::Scn::BoidCalculations.add(FF::Sys.new('Seperation', priority: 50) do
+FF::Sys.new('Seperation', priority: 50) do
FF::Cmp::Boids.each do |boid_update|
- newvec = [0,0]
+ 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 < 200
- puts 'repelling'
+ 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] / 100.0
- boid_update.cy += newvec[1] / 100.0
+ boid_update.cx += newvec[0] / $seperation
+ boid_update.cy += newvec[1] / $seperation
end
- puts 'end of frame'
-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