summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorrealtradam <[email protected]>2021-08-09 05:17:42 -0400
committerrealtradam <[email protected]>2021-08-09 05:17:42 -0400
commit4e909c6b44794b76ef3a98c032ea90204b673f85 (patch)
tree79cc49469447edf61ad835f61279bf48de51a61f
parent4401e68464d1d5932daec84e41aeb7b4a88c831e (diff)
downloadruboids-4e909c6b44794b76ef3a98c032ea90204b673f85.tar.gz
ruboids-4e909c6b44794b76ef3a98c032ea90204b673f85.zip
cohesion and seperation
-rw-r--r--Gemfile2
-rw-r--r--Gemfile.lock3
-rw-r--r--lib/00-define.rb16
-rw-r--r--lib/01-render.rb21
-rw-r--r--lib/02-apply_boid_calculations.rb15
-rw-r--r--lib/03-cohesion.rb34
-rw-r--r--lib/04-seperation.rb16
-rw-r--r--run.rb74
8 files changed, 181 insertions, 0 deletions
diff --git a/Gemfile b/Gemfile
index 3bdf259..4a642e7 100644
--- a/Gemfile
+++ b/Gemfile
@@ -9,3 +9,5 @@ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
gem "ruby2d", "~> 0.10.0"
gem "felflame", "~> 3.0"
+
+gem "ruby2d-camera", "~> 1.0"
diff --git a/Gemfile.lock b/Gemfile.lock
index 218d8de..2c8d3d8 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -3,6 +3,8 @@ GEM
specs:
felflame (3.0.0)
ruby2d (0.10.0)
+ ruby2d-camera (1.0.0)
+ ruby2d (~> 0.10)
PLATFORMS
x86_64-linux
@@ -10,6 +12,7 @@ PLATFORMS
DEPENDENCIES
felflame (~> 3.0)
ruby2d (~> 0.10.0)
+ ruby2d-camera (~> 1.0)
BUNDLED WITH
2.2.22
diff --git a/lib/00-define.rb b/lib/00-define.rb
new file mode 100644
index 0000000..b0606a2
--- /dev/null
+++ b/lib/00-define.rb
@@ -0,0 +1,16 @@
+FF::Stg.add FF::Scn.new('Default')
+FF::Stg.add FF::Scn.new('BoidCalculations')
+
+FelFlame::Components.new('Boids',
+ # holds the object to render
+ #:obj,
+ # holds the vector to render
+ #:vect,
+ # the current position
+ x: 0, y: 0,
+ # current velocity
+ vx: 0, vy: 0,
+ # calculated velocity change for next frame
+ cx: 0, cy: 0)
+FelFlame::Components.new('BoidVisuals',
+ :obj, :vect)
diff --git a/lib/01-render.rb b/lib/01-render.rb
new file mode 100644
index 0000000..e3a1271
--- /dev/null
+++ b/lib/01-render.rb
@@ -0,0 +1,21 @@
+#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.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)
+ end
+ Camera._redraw
+end)
diff --git a/lib/02-apply_boid_calculations.rb b/lib/02-apply_boid_calculations.rb
new file mode 100644
index 0000000..250a394
--- /dev/null
+++ b/lib/02-apply_boid_calculations.rb
@@ -0,0 +1,15 @@
+FF::Scn::BoidCalculations.add(FF::Sys.new('ApplyBoidCalculations', priority: 51) do
+ FF::Cmp::Boids.each do |boid|
+ boid.vx += boid.cx
+ boid.vy += boid.cy
+ 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
+ end
+end)
diff --git a/lib/03-cohesion.rb b/lib/03-cohesion.rb
new file mode 100644
index 0000000..49ede9c
--- /dev/null
+++ b/lib/03-cohesion.rb
@@ -0,0 +1,34 @@
+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/04-seperation.rb b/lib/04-seperation.rb
new file mode 100644
index 0000000..dad82f6
--- /dev/null
+++ b/lib/04-seperation.rb
@@ -0,0 +1,16 @@
+FF::Scn::BoidCalculations.add(FF::Sys.new('Seperation', priority: 50) do
+ FF::Cmp::Boids.each do |boid_update|
+ newvec = [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'
+ 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
+ end
+ puts 'end of frame'
+end)
diff --git a/run.rb b/run.rb
index e69de29..bfce180 100644
--- a/run.rb
+++ b/run.rb
@@ -0,0 +1,74 @@
+require 'ruby2d'
+require 'ruby2d/camera'
+require 'felflame'
+
+Dir[File.join(__dir__, 'lib/**', '*.rb')].sort.each { |file| require file }
+#FF::Scn::BoidCalculations.remove FF::Sys::Cohesion
+FF::Stg.remove FF::Scn::BoidCalculations
+
+class GameWindow < Ruby2D::Window
+ def initialize
+ super
+ #Camera.x = 10
+ #Camera.y = 10
+ randspot = ((-720 / 2)..(720/2)).to_a
+ @ent = FF::Ent.new(
+ FF::Cmp::Boids.new(x: randspot.sample, y:randspot.sample),
+ FF::Cmp::BoidVisuals.new(
+ obj: Camera::Circle.new(
+ color: [0.86,1.0,0.96,1],
+ radius: 7,
+ sectors: 10
+ ),
+ vect: Camera::Line.new(
+ width: 7,
+ color: 'red',
+ z: -1
+ )
+ )
+ )
+ 5.times do
+ FF::Ent.new(
+ FF::Cmp::Boids.new(x: randspot.sample, y:randspot.sample),
+ FF::Cmp::BoidVisuals.new(
+ obj: Camera::Circle.new(
+ color: [0.86,0.57,0.96,1],
+ radius: 7,
+ sectors: 10
+ ),
+ vect: Camera::Line.new(
+ width: 7,
+ color: 'red',
+ z: -1
+ )
+ )
+ )
+ end
+ end
+
+ def update
+ FF::Stage.call
+ Camera.y += 5 if key_held('s')
+ Camera.y -= 5 if key_held('w')
+ Camera.x += 5 if key_held('d')
+ Camera.x -= 5 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 #if key_held('space')
+ puts "X: #{@ent.components[FF::Cmp::Boids].first.x}"
+ puts "Y: #{@ent.components[FF::Cmp::Boids].first.y}"
+ puts "VX:#{@ent.components[FF::Cmp::Boids].first.vx}"
+ puts "VY:#{@ent.components[FF::Cmp::Boids].first.vy}"
+ end
+ end
+
+ def render
+ end
+end
+
+
+gamewindow = GameWindow.new
+set(title: "Ruboids", width: 1280, height: 720, resizable: true)
+gamewindow.set(title: "Ruboids", width: 1280, height: 720, resizable: true)
+gamewindow.show