From 9cb11bdcac69471a3962397b75c3fdbb374d3962 Mon Sep 17 00:00:00 2001 From: arngo <27396817+arngo@users.noreply.github.com> Date: Fri, 17 Dec 2021 22:13:15 -0500 Subject: implement alignment rule --- app/components/rules/alignment.rb | 2 ++ app/main.rb | 2 ++ app/systems/rules/alignment.rb | 24 ++++++++++++++++++++++++ app/tick.rb | 4 ++++ 4 files changed, 32 insertions(+) create mode 100644 app/components/rules/alignment.rb create mode 100644 app/systems/rules/alignment.rb diff --git a/app/components/rules/alignment.rb b/app/components/rules/alignment.rb new file mode 100644 index 0000000..a62cc78 --- /dev/null +++ b/app/components/rules/alignment.rb @@ -0,0 +1,2 @@ +FF::Cmp.new('BoidsAlignment', + strength: 10) diff --git a/app/main.rb b/app/main.rb index 1ab9e32..20cc937 100644 --- a/app/main.rb +++ b/app/main.rb @@ -6,6 +6,7 @@ require 'app/components/sprite.rb' require 'app/components/boid.rb' require 'app/components/rules/bounds.rb' require 'app/components/rules/cohesion.rb' +require 'app/components/rules/alignment.rb' require 'app/components/debug/debug_vector_arrow.rb' require 'app/systems/render.rb' @@ -13,6 +14,7 @@ require 'app/systems/update_boid_sprite.rb' require 'app/systems/update_boid_position.rb' require 'app/systems/rules/bounds.rb' require 'app/systems/rules/cohesion.rb' +require 'app/systems/rules/alignment.rb' require 'app/systems/rules/reset_change_vector.rb' require 'app/systems/debug/debug_render_vector_arrow.rb' diff --git a/app/systems/rules/alignment.rb b/app/systems/rules/alignment.rb new file mode 100644 index 0000000..2c2af11 --- /dev/null +++ b/app/systems/rules/alignment.rb @@ -0,0 +1,24 @@ +FF::Scn::BoidRules.add( + FF::Sys.new('BoidsAlignment', priority: 50) do + group_velocity = [0.0, 0.0] + boids_count = FF::Cmp::BoidsCohesion.data.count + + FF::Cmp::BoidsAlignment.each do |alignment| + boid = alignment.entities[0].components[FF::Cmp::Boid][0] + group_velocity[0] += boid.vx + group_velocity[1] += boid.vy + end + + FF::Cmp::BoidsAlignment.each do |alignment| + boid_update = alignment.entities[0].components[FF::Cmp::Boid][0] + 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.strength + boid_update.cy += (move_boid[1] - boid_update.vy) / alignment.strength + end + end +) diff --git a/app/tick.rb b/app/tick.rb index 7c44f47..077c4bb 100644 --- a/app/tick.rb +++ b/app/tick.rb @@ -1,19 +1,23 @@ FF::Ent.new(FF::Cmp::Sprite.new, FF::Cmp::Boid.new(x: 10, y: 10, vx: 0, vy: 0), FF::Cmp::BoidBounds.new, + FF::Cmp::BoidsAlignment.new, FF::Cmp::BoidsCohesion.new) FF::Ent.new(FF::Cmp::Sprite.new, FF::Cmp::Boid.new(x: 50, y: 50), FF::Cmp::BoidBounds.new, + FF::Cmp::BoidsAlignment.new, FF::Cmp::BoidsCohesion.new) FF::Ent.new(FF::Cmp::Sprite.new, FF::Cmp::Boid.new(x: 70, y: 20), FF::Cmp::BoidBounds.new, + FF::Cmp::BoidsAlignment.new, FF::Cmp::BoidsCohesion.new) FF::Ent.new(FF::Cmp::DebugVectorArrow.new(length: 5), FF::Cmp::Sprite.new, FF::Cmp::Boid.new(x: 150, y: 250), FF::Cmp::BoidBounds.new, + FF::Cmp::BoidsAlignment.new, FF::Cmp::BoidsCohesion.new) FF::Scn::Debug.add(FF::Sys::DebugRenderVectorArrow) @pause = false -- cgit v1.2.3