diff options
| author | realtradam <[email protected]> | 2021-08-09 05:17:42 -0400 |
|---|---|---|
| committer | realtradam <[email protected]> | 2021-08-09 05:17:42 -0400 |
| commit | 4e909c6b44794b76ef3a98c032ea90204b673f85 (patch) | |
| tree | 79cc49469447edf61ad835f61279bf48de51a61f /lib | |
| parent | 4401e68464d1d5932daec84e41aeb7b4a88c831e (diff) | |
| download | ruboids-4e909c6b44794b76ef3a98c032ea90204b673f85.tar.gz ruboids-4e909c6b44794b76ef3a98c032ea90204b673f85.zip | |
cohesion and seperation
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/00-define.rb | 16 | ||||
| -rw-r--r-- | lib/01-render.rb | 21 | ||||
| -rw-r--r-- | lib/02-apply_boid_calculations.rb | 15 | ||||
| -rw-r--r-- | lib/03-cohesion.rb | 34 | ||||
| -rw-r--r-- | lib/04-seperation.rb | 16 |
5 files changed, 102 insertions, 0 deletions
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) |
