ruby-processing 2.4.3 → 2.4.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitignore +3 -1
- data/CHANGELOG +5 -0
- data/README.md +4 -2
- data/lib/ruby-processing/exporters/application_exporter.rb +1 -0
- data/lib/ruby-processing/version.rb +1 -1
- data/library/boids/boids.rb +14 -13
- data/library/vecmath/lib/vec.rb +20 -4
- data/samples/contributed/circle_collision.rb +92 -149
- data/samples/contributed/drawolver.rb +13 -27
- data/samples/contributed/elegant_ball.rb +158 -0
- data/samples/contributed/fern.rb +16 -3
- data/samples/contributed/flight_patterns.rb +16 -4
- data/samples/external_library/java_processing/{pbox2d → box2d_processing}/bumpy_surface_noise.rb +4 -9
- data/samples/external_library/java_processing/{pbox2d → box2d_processing}/data/java_args.txt +0 -0
- data/samples/external_library/java_processing/{pbox2d → box2d_processing}/library/custom_shape/custom_shape.rb +1 -1
- data/samples/external_library/java_processing/{pbox2d → box2d_processing}/library/particle_system/particle_system.rb +7 -10
- data/samples/external_library/java_processing/{pbox2d → box2d_processing}/library/surface/surface.rb +2 -2
- data/samples/external_library/java_processing/{pbox2d → box2d_processing}/liquidy.rb +7 -7
- data/samples/external_library/java_processing/{pbox2d → box2d_processing}/polygons.rb +4 -9
- data/samples/external_library/java_processing/custom/README.md +15 -0
- data/samples/external_library/java_processing/custom/Rakefile +28 -0
- data/samples/external_library/java_processing/custom/landscape.rb +58 -0
- data/samples/external_library/java_processing/custom/src/nn/Connection.java +47 -0
- data/samples/external_library/java_processing/custom/src/nn/HiddenNeuron.java +20 -0
- data/samples/external_library/java_processing/custom/src/nn/InputNeuron.java +23 -0
- data/samples/external_library/java_processing/custom/src/nn/Network.java +136 -0
- data/samples/external_library/java_processing/custom/src/nn/Neuron.java +79 -0
- data/samples/external_library/java_processing/custom/src/nn/OutputNeuron.java +7 -0
- data/samples/external_library/java_processing/custom/xor.rb +88 -0
- data/samples/external_library/ruby_gem/README +1 -1
- data/samples/external_library/ruby_gem/data/data.json +8 -0
- data/samples/external_library/ruby_gem/draw_test.rb +171 -0
- data/samples/processing_app/basics/form/icosahedra.rb +39 -0
- data/samples/processing_app/basics/form/library/icosahedron/icosahedron.rb +60 -0
- data/samples/processing_app/basics/form/toroid.rb +78 -92
- data/samples/processing_app/basics/transform/birds.rb +6 -12
- data/samples/processing_app/basics/transform/cubes_in_cube.rb +25 -22
- data/samples/processing_app/basics/transform/library/cube/cube.rb +16 -16
- data/samples/processing_app/basics/transform/rotate_push_pop.rb +1 -1
- data/samples/processing_app/demos/graphics/bezier_patch.rb +18 -31
- data/samples/processing_app/demos/graphics/trefoil.rb +15 -15
- data/samples/processing_app/library/vecmath/acceleration_with_vectors.rb +3 -3
- data/samples/processing_app/library/vecmath/hilbert_fractal.rb +2 -2
- data/samples/processing_app/library/vecmath/library/flock/flock.rb +18 -21
- data/samples/processing_app/library/vecmath/library/hilbert/hilbert.rb +11 -8
- data/samples/processing_app/library/vecmath/library/wiggler/wiggler.rb +7 -15
- data/samples/processing_app/library/vecmath/seeking_neural.rb +172 -0
- data/samples/processing_app/topics/animation/animated_sprite.rb +5 -8
- data/samples/processing_app/topics/animation/sequential.rb +2 -3
- data/samples/processing_app/topics/create_shapes/library/particle/particle_system.rb +7 -7
- data/samples/processing_app/topics/create_shapes/particle_system_pshape.rb +2 -2
- data/samples/processing_app/topics/create_shapes/wiggle_pshape.rb +2 -1
- data/samples/processing_app/topics/lsystems/koch.rb +1 -1
- data/samples/processing_app/topics/lsystems/library/koch/koch_fractal.rb +24 -23
- data/samples/processing_app/topics/motion/circle_collision.rb +117 -160
- data/samples/processing_app/topics/motion/library/cube/cube.rb +1 -1
- data/samples/processing_app/topics/motion/morph.rb +1 -1
- data/samples/processing_app/topics/motion/reflection1.rb +17 -16
- data/samples/processing_app/topics/shaders/conway.rb +2 -2
- data/samples/processing_app/topics/shaders/data/conway.glsl +10 -10
- data/samples/processing_app/topics/shaders/glsl_heightmap_noise.rb +9 -8
- data/samples/processing_app/topics/shaders/landscape.rb +1 -1
- data/samples/processing_app/topics/simulate/flocking.rb +1 -1
- data/samples/processing_app/topics/simulate/library/flock/flock.rb +62 -57
- data/samples/processing_app/topics/simulate/multiple_particle_systems.rb +8 -28
- data/samples/processing_app/topics/simulate/simple_particle_system.rb +9 -7
- data/samples/processing_app/topics/simulate/smoke_particle_system.rb +12 -11
- data/vendors/Rakefile +2 -2
- metadata +26 -21
- data/samples/contributed/pong.rb +0 -177
- data/samples/contributed/simple_buffer.rb +0 -44
- data/samples/external_library/java_processing/pbox2d/contact_test.rb +0 -23
- data/samples/processing_app/basics/form/icosahedra/icosahedra.rb +0 -72
- data/samples/processing_app/basics/form/icosahedra/icosahedron.rb +0 -116
- data/samples/processing_app/basics/form/icosahedra/shape_3D.rb +0 -25
@@ -5,34 +5,28 @@
|
|
5
5
|
# pushMatrix() / popMatrix() functions.
|
6
6
|
# Trigonometry functions handle the flapping and sinuous movement.
|
7
7
|
|
8
|
-
load_library
|
8
|
+
load_library :bird
|
9
9
|
|
10
10
|
BIRD_COUNT = 200
|
11
11
|
|
12
|
-
def setup
|
13
|
-
|
12
|
+
def setup
|
14
13
|
size 640, 360, P3D
|
15
14
|
no_stroke
|
16
15
|
lights
|
17
|
-
@birds = []
|
18
|
-
|
19
|
-
0.upto(BIRD_COUNT) { |i|
|
20
|
-
|
16
|
+
@birds = []
|
17
|
+
0.upto(BIRD_COUNT) do |i|
|
21
18
|
bird = Bird.new rand(-300 .. 300), rand(-300 .. 300), rand(-2500 .. -500), rand(5 .. 30), rand(5 .. 30)
|
22
19
|
bird.set_flight rand(20 .. 340), rand(30 .. 350), rand( 1000 .. 4800 ),
|
23
20
|
rand(-160 .. 160), rand(-55 .. 55), rand(-20 .. 20)
|
24
21
|
bird.set_wing_speed rand(0.1 .. 3.75)
|
25
22
|
bird.set_rot_speed rand(0.025 .. 0.15)
|
26
23
|
@birds << bird
|
27
|
-
|
28
|
-
|
24
|
+
end
|
29
25
|
end
|
30
26
|
|
31
27
|
def draw
|
32
28
|
background 0
|
33
29
|
translate width/2, height/2, -700
|
34
|
-
@birds.each
|
35
|
-
b.fly
|
36
|
-
end
|
30
|
+
@birds.each {|b| b.fly}
|
37
31
|
end
|
38
32
|
|
@@ -2,11 +2,12 @@
|
|
2
2
|
# by Ira Greenberg.
|
3
3
|
#
|
4
4
|
# Collision detection against all outer cube's surfaces.
|
5
|
-
# Uses the
|
5
|
+
# Uses the Vec3D and Cube classes.
|
6
6
|
|
7
7
|
# fjenett, 2010-03-12: did some cleanups and rubyfication here
|
8
|
+
# Martin Prout went a bit further on 2014-03-26
|
8
9
|
|
9
|
-
|
10
|
+
load_libraries :vecmath, :cube
|
10
11
|
|
11
12
|
|
12
13
|
def setup
|
@@ -18,18 +19,14 @@ def setup
|
|
18
19
|
@cube_count = 20
|
19
20
|
@cubes = []
|
20
21
|
|
21
|
-
0.upto( @cube_count )
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
c
|
26
|
-
|
27
|
-
c.position = PVector.new(0.0, 0.0, 0.0)
|
28
|
-
c.speed = PVector.new(rand(-1 .. 1), rand(-1 .. 1), rand(-1 .. 1))
|
29
|
-
c.rotation = PVector.new(rand(40 .. 100), rand(40 .. 100), rand(40 .. 100))
|
30
|
-
|
22
|
+
0.upto( @cube_count ) do |i|
|
23
|
+
cube_size = rand(5 .. 15)
|
24
|
+
c = Cube.new(cube_size)
|
25
|
+
c.position = Vec3D.new(0.0, 0.0, 0.0)
|
26
|
+
c.speed = Vec3D.new(rand(-1.0 .. 1), rand(-1.0 .. 1), rand(-1.0 .. 1))
|
27
|
+
c.rotation = Vec3D.new(rand(40 .. 100), rand(40 .. 100), rand(40 .. 100))
|
31
28
|
@cubes << c
|
32
|
-
|
29
|
+
end
|
33
30
|
|
34
31
|
@cube_colors = [
|
35
32
|
color(0), color(51), color(102), color(153), color(204), color(255)
|
@@ -37,7 +34,7 @@ def setup
|
|
37
34
|
@cube_colors.reverse
|
38
35
|
|
39
36
|
@stage_size = 300
|
40
|
-
@stage = Cube.new @stage_size
|
37
|
+
@stage = Cube.new @stage_size
|
41
38
|
|
42
39
|
end
|
43
40
|
|
@@ -56,7 +53,7 @@ def draw
|
|
56
53
|
|
57
54
|
@stage.draw
|
58
55
|
|
59
|
-
@cubes.each_with_index
|
56
|
+
@cubes.each_with_index do |c, i|
|
60
57
|
|
61
58
|
# draw cube
|
62
59
|
push_matrix
|
@@ -75,25 +72,31 @@ def draw
|
|
75
72
|
pop_matrix
|
76
73
|
|
77
74
|
# move it
|
78
|
-
c.position
|
75
|
+
c.position += c.speed
|
79
76
|
|
80
77
|
# draw lines
|
81
78
|
if i > 0
|
82
79
|
|
83
80
|
stroke 0
|
84
|
-
c2 = @cubes[i-1]
|
81
|
+
c2 = @cubes[i - 1]
|
85
82
|
line c.position.x, c.position.y, c.position.z,
|
86
83
|
c2.position.x, c2.position.y, c2.position.z
|
87
84
|
|
88
85
|
end
|
89
86
|
|
90
87
|
# collision
|
91
|
-
|
92
|
-
c.speed.x *= -1
|
93
|
-
c.speed.y *= -1
|
94
|
-
c.speed.z *= -1
|
95
|
-
|
88
|
+
boundary = Boundary.new(-@stage_size / 2, @stage_size / 2)
|
89
|
+
c.speed.x *= -1 unless boundary.include? c.position.x
|
90
|
+
c.speed.y *= -1 unless boundary.include? c.position.y
|
91
|
+
c.speed.z *= -1 unless boundary.include? c.position.z
|
92
|
+
end
|
96
93
|
|
97
94
|
end
|
95
|
+
|
96
|
+
Boundary = Struct.new(:lower, :upper) do
|
97
|
+
def include? x
|
98
|
+
(lower ... upper).cover? x
|
99
|
+
end
|
100
|
+
end
|
98
101
|
|
99
102
|
|
@@ -4,25 +4,25 @@ class Cube
|
|
4
4
|
|
5
5
|
attr_accessor :vertices
|
6
6
|
attr_accessor :w, :h, :d
|
7
|
-
attr_accessor :position, :speed, :rotation #
|
7
|
+
attr_accessor :position, :speed, :rotation # Vec3D
|
8
8
|
|
9
|
-
def initialize (
|
9
|
+
def initialize (dim)
|
10
10
|
|
11
|
-
@w, @h, @d =
|
11
|
+
@w, @h, @d = dim, dim, dim
|
12
12
|
|
13
|
-
w2 = @w/2
|
14
|
-
h2 = @h/2
|
15
|
-
d2 = @d/2
|
13
|
+
w2 = @w / 2
|
14
|
+
h2 = @h / 2
|
15
|
+
d2 = @d / 2
|
16
16
|
|
17
|
-
tfl =
|
18
|
-
tfr =
|
19
|
-
tbr =
|
20
|
-
tbl =
|
17
|
+
tfl = Vec3D.new(-w2, h2, d2) # four points making the top quad:
|
18
|
+
tfr = Vec3D.new(w2, h2, d2) # "tfl" is "top front left", etc
|
19
|
+
tbr = Vec3D.new(w2, h2,-d2)
|
20
|
+
tbl = Vec3D.new(-w2, h2,-d2)
|
21
21
|
|
22
|
-
bfl =
|
23
|
-
bfr =
|
24
|
-
bbr =
|
25
|
-
bbl =
|
22
|
+
bfl = Vec3D.new(-w2,-h2, d2) # bottom quad points
|
23
|
+
bfr = Vec3D.new(w2,-h2, d2)
|
24
|
+
bbr = Vec3D.new(w2,-h2,-d2)
|
25
|
+
bbl = Vec3D.new(-w2,-h2,-d2)
|
26
26
|
|
27
27
|
@vertices = [
|
28
28
|
[tfl, tfr, tbr, tbl], # top
|
@@ -42,9 +42,9 @@ class Cube
|
|
42
42
|
|
43
43
|
fill side_colors[i] if side_colors && i < side_colors.length
|
44
44
|
|
45
|
-
quad.each { |
|
45
|
+
quad.each { |vec|
|
46
46
|
|
47
|
-
vertex
|
47
|
+
vertex vec.x, vec.y, vec.z
|
48
48
|
}
|
49
49
|
|
50
50
|
end_shape
|
@@ -70,17 +70,9 @@ end
|
|
70
70
|
def build_geometry
|
71
71
|
@outp = []
|
72
72
|
@normp = []
|
73
|
-
@inp =
|
74
|
-
uitang =
|
75
|
-
ujtang =
|
76
|
-
|
77
|
-
(0 ... NI).each do |i|
|
78
|
-
row = Array.new
|
79
|
-
(0 ... NJ).each do |j|
|
80
|
-
row << PVector.new(i, j, rand(-3.0 .. 3.0))
|
81
|
-
end
|
82
|
-
inp << row
|
83
|
-
end
|
73
|
+
@inp = Array.new(NI){|i| Array.new(NJ){|j| Vec3D.new(i, j, rand(-3.0 .. 3))}}
|
74
|
+
uitang = Vec3D.new
|
75
|
+
ujtang = Vec3D.new
|
84
76
|
|
85
77
|
(0 ... RESI).each do |i|
|
86
78
|
mui = i.fdiv(RESI - 1)
|
@@ -88,34 +80,29 @@ def build_geometry
|
|
88
80
|
row_n = []
|
89
81
|
(0 ... RESJ).each do |j|
|
90
82
|
muj = j.fdiv(RESJ - 1)
|
91
|
-
vect =
|
92
|
-
uitang.
|
93
|
-
ujtang.
|
83
|
+
vect = Vec3D.new
|
84
|
+
uitang.x, uitang.y, uitang.z = 0, 0, 0
|
85
|
+
ujtang.x, ujtang.y, ujtang.z = 0, 0, 0
|
94
86
|
(0 ... NI).each do |ki|
|
95
87
|
bi = bezier_blend(ki, mui, NI)
|
96
88
|
dbi = d_bezier_blend(ki, mui, NI)
|
97
89
|
(0 ... NJ).each do |kj|
|
98
90
|
bj = bezier_blend(kj, muj, NJ)
|
99
91
|
dbj = d_bezier_blend(kj, muj, NJ)
|
100
|
-
|
101
|
-
vect
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
uitang.z += (inp[ki][kj].z * dbi * bj)
|
107
|
-
|
108
|
-
ujtang.x += (inp[ki][kj].x * bi * dbj)
|
109
|
-
ujtang.y += (inp[ki][kj].y * bi * dbj)
|
110
|
-
ujtang.z += (inp[ki][kj].z * bi * dbj)
|
92
|
+
|
93
|
+
vect += inp[ki][kj] * bi * bj
|
94
|
+
|
95
|
+
uitang += inp[ki][kj] * dbi * bj
|
96
|
+
|
97
|
+
ujtang += inp[ki][kj] * bi * dbj
|
111
98
|
end
|
112
99
|
end
|
113
|
-
vect.
|
114
|
-
vect
|
115
|
-
row << vect.
|
116
|
-
uitang.normalize
|
117
|
-
ujtang.normalize
|
118
|
-
row_n << uitang.cross(ujtang).
|
100
|
+
vect += Vec3D.new(-NI / 2, -NJ / 2, 0)
|
101
|
+
vect *= 100
|
102
|
+
row << vect.to_a
|
103
|
+
uitang.normalize!
|
104
|
+
ujtang.normalize!
|
105
|
+
row_n << uitang.cross(ujtang).to_a
|
119
106
|
end
|
120
107
|
@outp << row
|
121
108
|
@normp << row_n
|
@@ -2,6 +2,8 @@
|
|
2
2
|
# A parametric surface is textured procedurally
|
3
3
|
# by drawing on an offscreen PGraphics surface.
|
4
4
|
|
5
|
+
load_library :vecmath
|
6
|
+
|
5
7
|
attr_reader :pg, :trefoil
|
6
8
|
|
7
9
|
def setup
|
@@ -43,7 +45,7 @@ end
|
|
43
45
|
# Code to draw a trefoil knot surface, with normals and texture
|
44
46
|
# coordinates.
|
45
47
|
# Adapted from the parametric equations example by Philip Rideout:
|
46
|
-
# http
|
48
|
+
# http://iphone-3d-programming.labs.oreilly.com/ch03.html
|
47
49
|
|
48
50
|
# This function draws a trefoil knot surface as a triangle mesh derived
|
49
51
|
# from its parametric equation.
|
@@ -100,12 +102,11 @@ def eval_normal(u, v)
|
|
100
102
|
p = eval_point(u, v)
|
101
103
|
tangU = eval_point(u + 0.01, v)
|
102
104
|
tangV = eval_point(u, v + 0.01)
|
103
|
-
tangU
|
104
|
-
tangV
|
105
|
+
tangU -= p
|
106
|
+
tangV -= p
|
105
107
|
|
106
|
-
normUV = tangV.cross(tangU)
|
107
|
-
normUV.normalize
|
108
|
-
return normUV
|
108
|
+
normUV = tangV.cross(tangU)
|
109
|
+
return normUV.normalize!
|
109
110
|
end
|
110
111
|
|
111
112
|
# Evaluates the surface point corresponding to normalized
|
@@ -115,26 +116,25 @@ def eval_point(u, v)
|
|
115
116
|
b = 0.3
|
116
117
|
c = 0.5
|
117
118
|
d = 0.1
|
118
|
-
s =
|
119
|
-
t = (
|
119
|
+
s = TAU * u
|
120
|
+
t = (TAU * (1 - v)) * 2
|
120
121
|
|
121
122
|
r = a + b * cos(1.5 * t)
|
122
123
|
x = r * cos(t)
|
123
124
|
y = r * sin(t)
|
124
125
|
z = c * sin(1.5 * t)
|
125
126
|
|
126
|
-
dv =
|
127
|
+
dv = Vec3D.new
|
127
128
|
dv.x = -1.5 * b * sin(1.5 * t) * cos(t) - (a + b * cos(1.5 * t)) * sin(t)
|
128
129
|
dv.y = -1.5 * b * sin(1.5 * t) * sin(t) + (a + b * cos(1.5 * t)) * cos(t)
|
129
130
|
dv.z = 1.5 * c * cos(1.5 * t)
|
130
131
|
|
131
|
-
|
132
|
-
|
133
|
-
qvn
|
134
|
-
qvn
|
135
|
-
ww = q.cross(qvn)
|
132
|
+
dv.normalize!
|
133
|
+
qvn = Vec3D.new(dv.y, -dv.x, 0)
|
134
|
+
qvn.normalize!
|
135
|
+
ww = dv.cross(qvn)
|
136
136
|
|
137
|
-
pt =
|
137
|
+
pt = Vec3D.new
|
138
138
|
pt.x = x + d * (qvn.x * cos(s) + ww.x * sin(s))
|
139
139
|
pt.y = y + d * (qvn.y * cos(s) + ww.y * sin(s))
|
140
140
|
pt.z = z + d * ww.z * sin(s)
|
@@ -57,9 +57,9 @@ class Mover
|
|
57
57
|
# Velocity changes according to acceleration vector
|
58
58
|
@velocity += acceleration
|
59
59
|
# Limit the velocity to topspeed, PVector has a limit function
|
60
|
-
|
61
|
-
|
62
|
-
|
60
|
+
# here we supply a block that evaluates to a boolean to set
|
61
|
+
# the velocit conditionally
|
62
|
+
velocity.set_mag topspeed {velocity.mag_squared > topspeed**2}
|
63
63
|
# Location changes by velocity vector
|
64
64
|
@location += velocity
|
65
65
|
end
|
@@ -7,13 +7,13 @@
|
|
7
7
|
########################################################
|
8
8
|
|
9
9
|
load_libraries :hilbert, :vecmath
|
10
|
-
|
10
|
+
|
11
11
|
attr_reader :hilbert, :arcball
|
12
12
|
|
13
13
|
def setup
|
14
14
|
size 1024, 768, P3D
|
15
15
|
@arcball = ArcBall.new(width/2.0, height/2.0, min(width - 20, height - 20) * 0.5)
|
16
|
-
@hilbert = Hilbert.new(height/2, 3)
|
16
|
+
@hilbert = Hilbert.new(size: height/2, gen: 3)
|
17
17
|
no_stroke
|
18
18
|
end
|
19
19
|
|
@@ -13,6 +13,10 @@ class Flock
|
|
13
13
|
boids.each &block
|
14
14
|
end
|
15
15
|
|
16
|
+
def reject &block
|
17
|
+
boids &block
|
18
|
+
end
|
19
|
+
|
16
20
|
def << obj
|
17
21
|
boids << obj
|
18
22
|
end
|
@@ -72,10 +76,8 @@ class Boid
|
|
72
76
|
def update
|
73
77
|
# Update velocity
|
74
78
|
@velocity += acceleration
|
75
|
-
# Limit speed
|
76
|
-
|
77
|
-
velocity.set_mag(maxspeed)
|
78
|
-
end
|
79
|
+
# Limit speed
|
80
|
+
velocity.set_mag(maxspeed) {velocity.mag_squared > maxspeed**2}
|
79
81
|
@location += velocity
|
80
82
|
# Reset accelertion to 0 each cycle
|
81
83
|
@acceleration *= 0
|
@@ -90,9 +92,8 @@ class Boid
|
|
90
92
|
desired *= maxspeed
|
91
93
|
# Steering = Desired minus Velocity
|
92
94
|
steer = desired - velocity
|
93
|
-
|
94
|
-
|
95
|
-
end
|
95
|
+
# Limit to maximum steering force
|
96
|
+
steer.set_mag(maxforce) {steer.mag_squared > maxforce**2}
|
96
97
|
return steer
|
97
98
|
end
|
98
99
|
|
@@ -134,10 +135,10 @@ class Boid
|
|
134
135
|
desiredseparation = 25.0
|
135
136
|
steer = Vec2D.new
|
136
137
|
count = 0
|
137
|
-
# For every
|
138
|
-
boids.each do |other|
|
138
|
+
# For every other bird in the system, check if it's too close
|
139
|
+
boids.reject{|bd| bd.equal? self}.each do |other|
|
139
140
|
d = Vec2D.dist(location, other.location)
|
140
|
-
# If the distance is greater than 0 and less than an arbitrary amount
|
141
|
+
# If the distance is greater than 0 and less than an arbitrary amount
|
141
142
|
if ((d > 0) && (d < desiredseparation))
|
142
143
|
# Calculate vector pointing away from neighbor
|
143
144
|
diff = location - other.location
|
@@ -157,21 +158,19 @@ class Boid
|
|
157
158
|
# Implement Reynolds: Steering = Desired - Velocity
|
158
159
|
steer.normalize!
|
159
160
|
steer *= maxspeed
|
160
|
-
steer -= velocity
|
161
|
-
|
162
|
-
steer.set_mag(maxforce)
|
163
|
-
end
|
161
|
+
steer -= velocity
|
162
|
+
steer.set_mag(maxforce){steer.mag_squared > maxforce**2}
|
164
163
|
end
|
165
164
|
return steer
|
166
165
|
end
|
167
166
|
|
168
167
|
# Alignment
|
169
|
-
# For every nearby boid in the system, calculate the average velocity
|
168
|
+
# For every other nearby boid in the system, calculate the average velocity
|
170
169
|
def align boids
|
171
170
|
neighbordist = 50
|
172
171
|
sum = Vec2D.new
|
173
172
|
count = 0
|
174
|
-
boids.each do |other|
|
173
|
+
boids.reject{|bd| bd.equal? self}.each do |other|
|
175
174
|
d = Vec2D.dist_squared(location, other.location)
|
176
175
|
if ((d > 0) && (d < neighbordist * neighbordist))
|
177
176
|
sum += other.velocity
|
@@ -183,9 +182,7 @@ class Boid
|
|
183
182
|
sum.normalize!
|
184
183
|
sum *= maxspeed
|
185
184
|
steer = sum - velocity
|
186
|
-
|
187
|
-
steer.set_mag(maxforce)
|
188
|
-
end
|
185
|
+
steer.set_mag(maxforce){steer.mag_squared > maxforce**2}
|
189
186
|
return steer
|
190
187
|
else
|
191
188
|
return Vec2D.new
|
@@ -193,12 +190,12 @@ class Boid
|
|
193
190
|
end
|
194
191
|
|
195
192
|
# Cohesion
|
196
|
-
# For the average location (i.e. center) of all nearby boids, calculate steering vector towards that location
|
193
|
+
# For the average location (i.e. center) of all other nearby boids, calculate steering vector towards that location
|
197
194
|
def cohesion boids
|
198
195
|
neighbordist = 50
|
199
196
|
sum = Vec2D.new # Start with empty vector to accumulate all locations
|
200
197
|
count = 0
|
201
|
-
boids.each do |other|
|
198
|
+
boids.reject{|bd| bd.equal? self}.each do |other|
|
202
199
|
d = Vec2D.dist_squared(location, other.location)
|
203
200
|
if ((d > 0) && (d < neighbordist * neighbordist))
|
204
201
|
sum += other.location # Add location
|