summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorrealtradam <[email protected]>2021-08-09 10:32:45 -0400
committerrealtradam <[email protected]>2021-08-09 10:32:45 -0400
commitc1eb3f4c18404d4921adc3d38dfa7438a47f4aa0 (patch)
treee0150085494d014dfbcdc1e6e2314fde62c9af1c
parent501ec02e894865836f2960b0bbc16b3448e3707f (diff)
downloadruboids-c1eb3f4c18404d4921adc3d38dfa7438a47f4aa0.tar.gz
ruboids-c1eb3f4c18404d4921adc3d38dfa7438a47f4aa0.zip
initial piranha
-rw-r--r--lib/00-define.rb4
-rw-r--r--lib/02-apply_boid_calculations.rb13
-rw-r--r--lib/mobs/fish.rb20
-rw-r--r--lib/mobs/piranha.rb20
-rw-r--r--lib/rules/alignment.rb4
-rw-r--r--lib/rules/cohesion.rb12
-rw-r--r--lib/rules/limit.rb8
-rw-r--r--lib/rules/seperation.rb25
-rw-r--r--lib/rules/target.rb28
-rw-r--r--run.rb93
10 files changed, 165 insertions, 62 deletions
diff --git a/lib/00-define.rb b/lib/00-define.rb
index 0075b7f..930f56f 100644
--- a/lib/00-define.rb
+++ b/lib/00-define.rb
@@ -12,5 +12,9 @@ FelFlame::Components.new('Boids',
vx: 0, vy: 0,
# calculated velocity change for next frame
cx: 0, cy: 0)
+FelFlame::Components.new('Fish')
+FF::Cmp::Fish.new
+FelFlame::Components.new('Piranha')
+FF::Cmp::Piranha.new
FelFlame::Components.new('BoidVisuals',
:obj, :vect)
diff --git a/lib/02-apply_boid_calculations.rb b/lib/02-apply_boid_calculations.rb
index 8b84ab5..ec3f2de 100644
--- a/lib/02-apply_boid_calculations.rb
+++ b/lib/02-apply_boid_calculations.rb
@@ -32,6 +32,19 @@ FF::Scn::BoidCalculations.add(FF::Sys.new('ApplyBoidCalculations', priority: 75)
group_velocity[1] += boid.vy
end
+ group_velocity = [0.0,0.0]
+ center_mass = [0.0,0.0]
+ boids_count = FF::Cmp::Fish[0].entities.count
+
+ FF::Cmp::Fish[0].entities.each do |ent|
+ boid = ent.components[FF::Cmp::Boids].first
+
+ 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)
diff --git a/lib/mobs/fish.rb b/lib/mobs/fish.rb
new file mode 100644
index 0000000..a9999ae
--- /dev/null
+++ b/lib/mobs/fish.rb
@@ -0,0 +1,20 @@
+class Fish
+ def self.create(x, y)
+ puts 'creating...'
+ FF::Ent.new(
+ FF::Cmp::Fish[0],
+ FF::Cmp::Boids.new(x: x, y: y),
+ FF::Cmp::BoidVisuals.new(
+ obj: Camera::Image.new(
+ 'assets/Guppy Large Normal.png',
+ width: 25, height: 26
+ ),
+ vect: Camera::Line.new(
+ width: 7,
+ color: [1.0,0,0,0.5],
+ z: -1
+ )
+ )
+ )
+ end
+end
diff --git a/lib/mobs/piranha.rb b/lib/mobs/piranha.rb
new file mode 100644
index 0000000..c7ce8b1
--- /dev/null
+++ b/lib/mobs/piranha.rb
@@ -0,0 +1,20 @@
+class Piranha
+ def self.create(x, y)
+ puts 'creating...'
+ FF::Ent.new(
+ FF::Cmp::Piranha[0],
+ FF::Cmp::Boids.new(x: x, y: y),
+ FF::Cmp::BoidVisuals.new(
+ obj: Camera::Image.new(
+ 'assets/Predator 1.png',
+ width: 45, height: 46
+ ),
+ vect: Camera::Line.new(
+ width: 7,
+ color: [1.0,0,0,0.5],
+ z: -1
+ )
+ )
+ )
+ end
+end
diff --git a/lib/rules/alignment.rb b/lib/rules/alignment.rb
index b30683c..4072227 100644
--- a/lib/rules/alignment.rb
+++ b/lib/rules/alignment.rb
@@ -14,7 +14,7 @@ FF::Sys.new('Alignment', priority: 50) do
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
+ boid_update.cx += (move_boid[0] - boid_update.vx) / $config.alignment
+ boid_update.cy += (move_boid[1] - boid_update.vy) / $config.alignment
end
end
diff --git a/lib/rules/cohesion.rb b/lib/rules/cohesion.rb
index efbd71d..5abefd7 100644
--- a/lib/rules/cohesion.rb
+++ b/lib/rules/cohesion.rb
@@ -1,21 +1,23 @@
FF::Sys.new('Cohesion', priority: 50) do
center_mass = [0.0,0.0]
- boids_count = FF::Cmp::Boids.each.to_a.count
+ boids_count = FF::Cmp::Fish[0].entities.count
- FF::Cmp::Boids.each do |boid|
+ FF::Cmp::Fish[0].entities.each do |ent|
+ boid = ent.components[FF::Cmp::Boids].first
center_mass[0] += boid.x
center_mass[1] += boid.y
end
- FF::Cmp::Boids.each do |boid_update|
+ FF::Cmp::Fish[0].entities.each do |ent|
+ boid_update = ent.components[FF::Cmp::Boids].first
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
+ boid_update.cx += (move_boid[0] - boid_update.x) / $config.cohesion
+ boid_update.cy += (move_boid[1] - boid_update.y) / $config.cohesion
end
end
diff --git a/lib/rules/limit.rb b/lib/rules/limit.rb
index c454de4..dba9f96 100644
--- a/lib/rules/limit.rb
+++ b/lib/rules/limit.rb
@@ -1,12 +1,12 @@
# 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
+ unless $config.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
+ if absolute_velocity > $config.limit
+ boid.vx = (boid.vx / absolute_velocity) * $config.limit
+ boid.vy = (boid.vy / absolute_velocity) * $config.limit
end
end
end
diff --git a/lib/rules/seperation.rb b/lib/rules/seperation.rb
index 31039f3..20fd2e6 100644
--- a/lib/rules/seperation.rb
+++ b/lib/rules/seperation.rb
@@ -3,12 +3,31 @@ FF::Sys.new('Seperation', priority: 50) do
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
+ if Math.sqrt(((-boid_check.x + boid_update.x)**2) + ((-boid_check.y + boid_update.y)**2)).abs < $config.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
+ boid_update.cx += newvec[0] / $config.seperation
+ boid_update.cy += newvec[1] / $config.seperation
end
end
+
+=begin
+FF::Sys.new('Seperation', priority: 50) do
+ FF::Cmp::Fish[0].entities.each do |ent_update|
+ boid_update = ent_update.components[FF::Cmp::Boids].first
+ newvec = [0.0,0.0]
+ FF::Cmp::Fish[0].entities.each do |ent_check|
+ boid_check = ent_update.components[FF::Cmp::Boids].first
+ next if boid_check == boid_update
+ if Math.sqrt(((-boid_check.x + boid_update.x)**2) + ((-boid_check.y + boid_update.y)**2)).abs < $config.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] / $config.seperation
+ boid_update.cy += newvec[1] / $config.seperation
+ end
+end
+=end
diff --git a/lib/rules/target.rb b/lib/rules/target.rb
index ffee93a..f74bf7d 100644
--- a/lib/rules/target.rb
+++ b/lib/rules/target.rb
@@ -1,6 +1,28 @@
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
+
+ FF::Cmp::Fish[0].entities.each do |ent|
+ farthest = []
+ boid = ent.component[FF::Cmp::Boids].first
+ FF::Cmp::Piranha[0].entities.each do |_piranha|
+ piranha = _piranha.component[FF::Cmp::Boids].first
+ if farthest[0].nil? || Math.sqrt(((boid.x - piranha.x)**2) + ((boid.y + piranha.y)**2)).abs < farthest[0]
+ farthest[0] = Math.sqrt(((boid.x - piranha.x)**2) + ((boid.y + piranha.y)**2)).abs
+ farthest[1] = piranha
+ end
+ end
+ boid.cx -= (2 * (piranha.x - boid.x) / $config.target_strength)
+ boid.cy -= (2 * (piranha.y - boid.y) / $config.target_strength)
end
+
+ # FF::Cmp::Piranha[0].entities.each do |ent|
+ # boid = ent.components[FF::Cmp::Boids].first
+ # boid.cx += ($config.target_x - boid.x) / $config.target_strength
+ # boid.cy += ($config.target_y - boid.y) / $config.target_strength
+ # #find closest fish
+ # end
+
+ #FF::Cmp::Boids.each do |boid|
+ # boid.cx += ($config.target_x - boid.x) / $config.target_strength
+ # boid.cy += ($config.target_y - boid.y) / $config.target_strength
+ #end
end
diff --git a/run.rb b/run.rb
index e21f723..4e3a080 100644
--- a/run.rb
+++ b/run.rb
@@ -1,40 +1,60 @@
require 'ruby2d'
require 'ruby2d/camera'
require 'felflame'
-# If Camera should follow center mass
-$follow = false
-# Values that can tune strength of certain rules
-# Lower is stronger
-$cohesion = 3000.0
-$seperation = 75.0
-$seperation_distance = 125.0
-$alignment = 1000.0
-$target_strength = 2500.0
+# configuration for the simulation
+# change values here to see a change in the simulation
+FF::Cmp.new('SingletonConfig',
+ # Show vectors
+ debug: true,
-# The velocity limit of a boid
-# Set to negative to remove limit
-$limit = 12.0
+ # If the camera should follow the "center mass"
+ follow: false,
-# These are changed later by mouse position
-$x_target = 0.0
-$y_target = 0.0
+ # The velocity limit of a boid
+ # Set to negative to disable
+ limit: 6.0,
-FF::Cmp.new('SingletonConfig',
- debug: false,
- bounds_strength: 2.0,
+ # How strong the bounds should be
+ # Higher is stronger
+ bounds_strength: 1.0,
+ # What the bounds are
xmax: 480.0, xmin: -580.0,
- ymax: 250.0, ymin: -340.0)
+ ymax: 250.0, ymin: -340.0,
+
+ # How much the boids try to pull together
+ # Smaller is stronger
+ cohesion: 6000.0,
+
+ # How much the boids push away from eachother
+ # Smaller is stronger
+ seperation: 375.0,
+ # What the range of seperating should be
+ seperation_distance: 150.0,
+
+ # How strong their vector alignment should be
+ # Smaller is stronger
+ alignment: 1000.0,
+
+ # How much they try to follow their target(your mouse cursor)
+ # Smaller is stronger
+ target_strength: 2500.0,
+
+ # These are later set by the mouse position
+ target_x: 0, target_y: 0)
$config = FF::Cmp::SingletonConfig.new
# Used by the Camera Library
set(title: "Ruboids", width: 1164, height: 764, resizable: true)
+
+
Dir[File.join(__dir__, 'lib/**', '*.rb')].sort.each { |file| require file }
+
# Comment out to remove a rule
FF::Scn::BoidCalculations.add FF::Sys::Cohesion
-FF::Scn::BoidCalculations.add FF::Sys::Alignment
+#FF::Scn::BoidCalculations.add FF::Sys::Alignment
FF::Scn::BoidCalculations.add FF::Sys::Seperation
-FF::Scn::BoidCalculations.add FF::Sys::Target
+#FF::Scn::BoidCalculations.add FF::Sys::Target
FF::Scn::BoidCalculations.add FF::Sys::Bounds
FF::Stg.add FF::Scn::BoidCalculations
@@ -44,25 +64,11 @@ class GameWindow < Ruby2D::Window
super
Camera::Image.new('assets/Background.png', x: -get(:width)+57, y: -get(:height)+97, z: -99)
randspot = ((-get(:height) / 2)..(get(:height)/2)).to_a
- 6.times do
- FF::Ent.new(
- FF::Cmp::Boids.new(x: randspot.sample.to_f, y: randspot.sample.to_f),
- FF::Cmp::BoidVisuals.new(
- obj: Camera::Image.new(
- 'assets/Guppy Large Normal.png',
- width: 45, height: 46
- #obj: Camera::Circle.new(
- # color: [0.86,0.57,0.96,1],
- # radius: 7,
- # sectors: 10
- ),
- vect: Camera::Line.new(
- width: 7,
- color: [1.0,0,0,0.5],
- z: -1
- )
- )
- )
+ 7.times do
+ Fish.create(randspot.sample.to_f, randspot.sample.to_f)
+ end
+ 2.times do
+ Piranha.create(randspot.sample.to_f, randspot.sample.to_f)
end
unless $config.debug
FF::Cmp::BoidVisuals.each do |boid|
@@ -72,8 +78,8 @@ class GameWindow < Ruby2D::Window
end
def update
- $x_target = Camera.coordinate_to_worldspace(get(:mouse_x), get(:mouse_y))[0]
- $y_target = Camera.coordinate_to_worldspace(get(:mouse_y), get(:mouse_y))[1]
+ $config.target_x = Camera.coordinate_to_worldspace(get(:mouse_x), get(:mouse_y))[0]
+ $config.target_y = Camera.coordinate_to_worldspace(get(:mouse_y), get(:mouse_y))[1]
FF::Stage.call
Camera.y += 1 if key_held('s')
Camera.y -= 1 if key_held('w')
@@ -81,9 +87,6 @@ class GameWindow < Ruby2D::Window
Camera.x -= 1 if key_held('a')
Camera.zoom *= 1.1 if key_held('z')
Camera.zoom /= 1.1 if key_held('x')
- if key_held('space')
- FF::Scn::BoidCalculations.call
- end
end
def render