summaryrefslogtreecommitdiffhomepage
path: root/lib/02-apply_boid_calculations.rb
blob: 8b84ab5d56c728366f79dfc132e286eb3561e42d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
FF::Scn::BoidCalculations.add(FF::Sys.new('ApplyBoidCalculations', priority: 75) do
  @center ||= Camera::Circle.new(color: [0.25,0.0,0.5,0.5],
                                 radius: 10,
                                 sectors: 10,
                                z: -49)
  @center_vel ||= Camera::Line.new(color: [0.25,0.0,0.5,0.5],
                                   width: 12,
                                  z: -50)
  unless $config.debug
    @center.remove
    @center_vel.remove
  end

  group_velocity = [0.0,0.0]
  center_mass = [0.0,0.0]
  boids_count = FF::Cmp::Boids.each.to_a.count

  FF::Cmp::Boids.each do |boid|
    boid.vx += boid.cx
    boid.vy += boid.cy
    FF::Sys::Limit.call
    boid.x += boid.vx
    boid.y += boid.vy

    boid.cx = 0.0
    boid.cy = 0.0


    center_mass[0] += boid.x
    center_mass[1] += boid.y
    group_velocity[0] += boid.vx
    group_velocity[1] += boid.vy
  end

  @center_vel.x1 = @center.x = (center_mass[0] / boids_count)
  @center_vel.y1 = @center.y = (center_mass[1] / boids_count)

  @center_vel.x1 += @center.radius
  @center_vel.y1 += @center.radius

  if $follow
    Camera.x = @center_vel.x1 = @center.x + @center.radius
    Camera.y = @center_vel.y1 = @center.y + @center.radius
  end
  @center_vel.x2 = @center_vel.x1 + ((group_velocity[0] / boids_count) * 10.0)
  @center_vel.y2 = @center_vel.y1 + ((group_velocity[1] / boids_count) * 10.0)
end)