From f70283e5ad5f5a5744ca0695a184f72f88803aca Mon Sep 17 00:00:00 2001 From: _Tradam Date: Thu, 23 Dec 2021 03:39:35 -0500 Subject: cleaned up boid rules, changed how strength works (#8) --- app/components/rules/alignment.rb | 2 +- app/components/rules/cohesion.rb | 2 +- app/components/rules/decay_speed.rb | 2 +- app/components/rules/follow.rb | 2 +- app/components/rules/minimum_speed.rb | 2 +- app/components/rules/separation.rb | 2 +- app/helpers/vectors/magnitude.rb | 7 +++++++ app/main.rb | 3 +++ app/systems/rules/alignment.rb | 4 ++-- app/systems/rules/cohesion.rb | 6 +++--- app/systems/rules/decay_speed.rb | 6 ++++-- app/systems/rules/follow.rb | 26 ++++++++++++++++++++++---- app/systems/start_game.rb | 12 ++++++------ 13 files changed, 53 insertions(+), 23 deletions(-) create mode 100644 app/helpers/vectors/magnitude.rb diff --git a/app/components/rules/alignment.rb b/app/components/rules/alignment.rb index a62cc78..0542aa7 100644 --- a/app/components/rules/alignment.rb +++ b/app/components/rules/alignment.rb @@ -1,2 +1,2 @@ FF::Cmp.new('BoidsAlignment', - strength: 10) + strength: 1) diff --git a/app/components/rules/cohesion.rb b/app/components/rules/cohesion.rb index 6acd5ea..ef66b33 100644 --- a/app/components/rules/cohesion.rb +++ b/app/components/rules/cohesion.rb @@ -1,2 +1,2 @@ FF::Cmp.new('BoidsCohesion', - strength: 100) + strength: 1) diff --git a/app/components/rules/decay_speed.rb b/app/components/rules/decay_speed.rb index 83a3938..6ff00b0 100644 --- a/app/components/rules/decay_speed.rb +++ b/app/components/rules/decay_speed.rb @@ -1,2 +1,2 @@ FF::Cmp.new('DecaySpeed', - strength: 0.8) + strength: 1) diff --git a/app/components/rules/follow.rb b/app/components/rules/follow.rb index 868d44f..5a4968d 100644 --- a/app/components/rules/follow.rb +++ b/app/components/rules/follow.rb @@ -1,3 +1,3 @@ FF::Cmp.new('Follow', :target, - strength: 100) + strength: 1) diff --git a/app/components/rules/minimum_speed.rb b/app/components/rules/minimum_speed.rb index 2e0e7fe..035ce9b 100644 --- a/app/components/rules/minimum_speed.rb +++ b/app/components/rules/minimum_speed.rb @@ -1,3 +1,3 @@ FF::Cmp.new('BoidMinimumSpeed', - speed: 5) + speed: 1) diff --git a/app/components/rules/separation.rb b/app/components/rules/separation.rb index 5298623..594eedb 100644 --- a/app/components/rules/separation.rb +++ b/app/components/rules/separation.rb @@ -1,3 +1,3 @@ FF::Cmp.new('BoidsSeparation', - :strength, + strength: 1, distance: 500) diff --git a/app/helpers/vectors/magnitude.rb b/app/helpers/vectors/magnitude.rb new file mode 100644 index 0000000..638f034 --- /dev/null +++ b/app/helpers/vectors/magnitude.rb @@ -0,0 +1,7 @@ +class Helpers + class Vectors + def self.magnitude(x2, y2, x1 = 0, y1 = 0) + Math.sqrt((((x2 - x1)**2) + ((y2 - y1)**2)).to_f) + end + end +end diff --git a/app/main.rb b/app/main.rb index 5a172c5..234143b 100644 --- a/app/main.rb +++ b/app/main.rb @@ -1,3 +1,5 @@ +require 'app/helpers/vectors/magnitude.rb' + require 'app/felflame.rb' require 'app/scenes/scenes.rb' @@ -50,6 +52,7 @@ require 'app/systems/cleanup_bullets.rb' require 'app/systems/ai/scatter.rb' require 'app/systems/ai/rejoin.rb' require 'app/systems/ai/target_player.rb' + require 'app/factories/bullet.rb' require 'app/factories/ships/osprey.rb' diff --git a/app/systems/rules/alignment.rb b/app/systems/rules/alignment.rb index 2c2af11..cbb3d99 100644 --- a/app/systems/rules/alignment.rb +++ b/app/systems/rules/alignment.rb @@ -17,8 +17,8 @@ FF::Scn::BoidRules.add( 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 + 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/systems/rules/cohesion.rb b/app/systems/rules/cohesion.rb index 7e33961..6347a39 100644 --- a/app/systems/rules/cohesion.rb +++ b/app/systems/rules/cohesion.rb @@ -10,7 +10,7 @@ FF::Scn::BoidRules.add( #puts boid.x #puts boid.y end - + #puts center_mass FF::Cmp::BoidsCohesion.each do |cohesion| boid_update = cohesion.entities[0].components[FF::Cmp::Boid][0] @@ -20,8 +20,8 @@ FF::Scn::BoidRules.add( move_boid[0] /= boids_count - 1.0 move_boid[1] /= boids_count - 1.0 - boid_update.cx += (move_boid[0] - boid_update.x) / cohesion.strength.to_i - boid_update.cy += (move_boid[1] - boid_update.y) / cohesion.strength.to_i + boid_update.cx += (move_boid[0] - boid_update.x) * cohesion.strength.to_i + boid_update.cy += (move_boid[1] - boid_update.y) * cohesion.strength.to_i end end ) diff --git a/app/systems/rules/decay_speed.rb b/app/systems/rules/decay_speed.rb index 490c7d0..dd7761f 100644 --- a/app/systems/rules/decay_speed.rb +++ b/app/systems/rules/decay_speed.rb @@ -2,8 +2,10 @@ FF::Scn::BoidRules.add( FF::Sys.new('DecaySpeed', priority: 50) do FF::Cmp::DecaySpeed.each do |decay| boid = decay.entities[0].components[FF::Cmp::Boid][0] - boid.vx *= decay.strength - boid.vy *= decay.strength + #boid.vx *= decay.strength + #boid.vy *= decay.strength + boid.cx += (boid.vx * decay.strength) - boid.vx + boid.cy += (boid.vy * decay.strength) - boid.vy end end ) diff --git a/app/systems/rules/follow.rb b/app/systems/rules/follow.rb index 0b5045e..abaabd7 100644 --- a/app/systems/rules/follow.rb +++ b/app/systems/rules/follow.rb @@ -14,14 +14,32 @@ FF::Scn::BoidRules.add( camera = FF::Cmp::SingletonCamera[0] angle = camera.angle * (Math::PI / 180) mouse = $gtk.args.inputs.mouse + mouse_x = mouse.x + mouse_y = mouse.y half_width = $gtk.args.grid.w * 0.5 half_height = $gtk.args.grid.h * 0.5 - target_coords[0] = (((mouse.x - half_width) / camera.zoom) * Math.cos(-angle)) - (((mouse.y - half_height) / camera.zoom) * Math.sin(-angle)) + camera.x - target_coords[1] = (((mouse.x - half_width) / camera.zoom) * Math.sin(-angle)) + (((mouse.y - half_height) / camera.zoom) * Math.cos(-angle)) + camera.y + + mag = Helpers::Vectors.magnitude(mouse_x, + mouse_y, + half_width, + half_height) / camera.zoom + if mag > half_height + mouse_x = (((mouse_x - half_width) / mag) * half_height) + half_width + mouse_y = (((mouse_y - half_height) / mag) * half_height) + half_height + end + #puts "x: #{mouse_x}" + #puts "y: #{mouse_y}" + #puts "x mag: #{mouse_x / mag}" + #puts "y mag: #{mouse_y / mag}" + $gtk.args.outputs.solids << [mouse_x, mouse_y, 250, 250, 255, 0, 0, 255] + + target_coords[0] = (((mouse_x - half_width) / camera.zoom) * Math.cos(-angle)) - (((mouse_y - half_height) / camera.zoom) * Math.sin(-angle)) + camera.x + target_coords[1] = (((mouse_x - half_width) / camera.zoom) * Math.sin(-angle)) + (((mouse_y - half_height) / camera.zoom) * Math.cos(-angle)) + camera.y + end - boid.cx += (target_coords[0] - boid.x) / follow.strength.to_f - boid.cy += (target_coords[1] - boid.y) / follow.strength.to_f + boid.cx += (target_coords[0] - boid.x) * follow.strength.to_f + boid.cy += (target_coords[1] - boid.y) * follow.strength.to_f end end ) diff --git a/app/systems/start_game.rb b/app/systems/start_game.rb index d6546df..4657e77 100644 --- a/app/systems/start_game.rb +++ b/app/systems/start_game.rb @@ -28,9 +28,9 @@ FF::Sys.new('StartGame', priority: 50 ) do FF::Cmp::SingletonCamera[0], FF::Cmp::Boid.new(h: 1920 * 2, w: 1920 * 2) ) - 15.times do |pos| - Factory::Osprey.new(x: position_range.sample, y: position_range.sample) - end + #15.times do |pos| + # Factory::Osprey.new(x: position_range.sample, y: position_range.sample) + #end sprite = FF::Cmp::Sprite.new sprite.props[:path] = [ @@ -45,12 +45,12 @@ FF::Sys.new('StartGame', priority: 50 ) do debug_arrow, FF::Cmp::SingletonCamera[0], FF::Cmp::BoidBounds.new, - FF::Cmp::Follow.new(target: :mouse, strength: 500), + FF::Cmp::Follow.new(target: :mouse, strength: 0.007), FF::Cmp::SingletonPlayer[0], FF::Cmp::Team.new(team: 'player'), FF::Cmp::Weapon.new, - FF::Cmp::BoidMinimumSpeed.new, - FF::Cmp::DecaySpeed.new(strength: 0.9), + FF::Cmp::BoidMinimumSpeed.new(speed: 5), + FF::Cmp::DecaySpeed.new(strength: 0.8), ) -- cgit v1.2.3