diff options
| author | arngo <[email protected]> | 2021-12-17 22:13:15 -0500 |
|---|---|---|
| committer | arngo <[email protected]> | 2021-12-17 22:13:15 -0500 |
| commit | 9cb11bdcac69471a3962397b75c3fdbb374d3962 (patch) | |
| tree | 6451461a80c2a14295e2b8fe7c56ed8bba937f4f | |
| parent | 8f5eb1177dd404cfe562ca562415a0246f2cbca1 (diff) | |
| download | SteelWings-9cb11bdcac69471a3962397b75c3fdbb374d3962.tar.gz SteelWings-9cb11bdcac69471a3962397b75c3fdbb374d3962.zip | |
implement alignment rule
| -rw-r--r-- | app/components/rules/alignment.rb | 2 | ||||
| -rw-r--r-- | app/main.rb | 2 | ||||
| -rw-r--r-- | app/systems/rules/alignment.rb | 24 | ||||
| -rw-r--r-- | app/tick.rb | 4 |
4 files changed, 32 insertions, 0 deletions
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 |
