ruby-processing 2.6.2 → 2.6.3
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/lib/rpextras.jar +0 -0
- data/lib/ruby-processing.rb +5 -8
- data/lib/ruby-processing/app.rb +26 -27
- data/lib/ruby-processing/config.rb +0 -2
- data/lib/ruby-processing/exporters/application_exporter.rb +1 -1
- data/lib/ruby-processing/exporters/base_exporter.rb +1 -1
- data/lib/ruby-processing/exporters/creator.rb +11 -11
- data/lib/ruby-processing/helper_methods.rb +33 -31
- data/lib/ruby-processing/helpers/numeric.rb +1 -1
- data/lib/ruby-processing/helpers/range.rb +11 -0
- data/lib/ruby-processing/library_loader.rb +11 -16
- data/lib/ruby-processing/runner.rb +22 -24
- data/lib/ruby-processing/runners/watch.rb +1 -1
- data/lib/ruby-processing/version.rb +1 -1
- data/library/boids/boids.rb +39 -70
- data/library/control_panel/control_panel.rb +3 -3
- data/samples/contributed/circle_collision.rb +3 -3
- data/samples/contributed/fern.rb +7 -10
- data/samples/contributed/fibonacci_sphere.rb +2 -2
- data/samples/contributed/full_screen.rb +23 -22
- data/samples/contributed/grapher.rb +3 -3
- data/samples/contributed/gravity.rb +1 -1
- data/samples/contributed/mandelbrot.rb +10 -13
- data/samples/external_library/java_processing/box2d_processing/bumpy_surface_noise.rb +1 -1
- data/samples/external_library/java_processing/box2d_processing/library/custom_shape/custom_shape.rb +2 -2
- data/samples/external_library/java_processing/box2d_processing/library/particle_system/particle_system.rb +1 -1
- data/samples/external_library/java_processing/box2d_processing/library/surface/surface.rb +2 -2
- data/samples/external_library/java_processing/box2d_processing/liquidy.rb +1 -1
- data/samples/external_library/java_processing/fisica/bubbles.rb +2 -2
- data/samples/external_library/java_processing/fisica/joints.rb +4 -4
- data/samples/external_library/java_processing/generative_design/node_spring_attractor.rb +2 -2
- data/samples/external_library/java_processing/geomerative/hello_world_rotate_firstletter.rb +7 -7
- data/samples/external_library/java_processing/hemesh/twin_iso.rb +3 -3
- data/samples/external_library/java_processing/pbox2d/bumpy_surface_noise.rb +1 -1
- data/samples/external_library/java_processing/pbox2d/library/custom_shape/custom_shape.rb +2 -2
- data/samples/external_library/java_processing/pbox2d/library/particle_system/particle_system.rb +1 -1
- data/samples/external_library/java_processing/pbox2d/library/surface/surface.rb +2 -2
- data/samples/external_library/java_processing/pbox2d/liquidy.rb +1 -1
- data/samples/external_library/java_processing/peasy_cam/library/hilbert/hilbert.rb +4 -4
- data/samples/external_library/ruby_gem/draw_test.rb +2 -2
- data/samples/external_library/ruby_gem/game_of_life.rb +3 -3
- data/samples/processing_app/basics/arrays/array_objects.rb +2 -2
- data/samples/processing_app/basics/color/hue.rb +1 -1
- data/samples/processing_app/basics/control/conditionals1.rb +5 -24
- data/samples/processing_app/basics/control/conditionals2.rb +1 -1
- data/samples/processing_app/basics/form/bezier_ellipse.rb +1 -1
- data/samples/processing_app/basics/form/brick_tower.rb +4 -8
- data/samples/processing_app/basics/form/regular_polygon.rb +1 -1
- data/samples/processing_app/basics/form/star.rb +1 -1
- data/samples/processing_app/basics/form/triangle_strip.rb +1 -1
- data/samples/processing_app/basics/input/storing_input.rb +1 -1
- data/samples/processing_app/basics/lights/spot.rb +1 -1
- data/samples/processing_app/basics/math/additive_wave.rb +2 -2
- data/samples/processing_app/basics/math/double_random.rb +2 -2
- data/samples/processing_app/basics/math/noise_wave.rb +1 -1
- data/samples/processing_app/basics/math/random.rb +1 -1
- data/samples/processing_app/basics/objects/module.rb +1 -1
- data/samples/processing_app/basics/objects/struct.rb +1 -1
- data/samples/processing_app/basics/structure/loop.rb +19 -14
- data/samples/processing_app/basics/structure/recursion2.rb +2 -2
- data/samples/processing_app/basics/textures/texture3.rb +13 -33
- data/samples/processing_app/basics/transform/birds.rb +5 -5
- data/samples/processing_app/basics/transform/cubes_in_cube.rb +3 -3
- data/samples/processing_app/basics/transform/rotate.rb +1 -1
- data/samples/processing_app/basics/typography/kinetic_type.rb +1 -1
- data/samples/processing_app/demos/graphics/bezier_patch.rb +34 -34
- data/samples/processing_app/demos/graphics/trefoil.rb +1 -1
- data/samples/processing_app/demos/graphics/wiggling.rb +5 -5
- data/samples/processing_app/demos/performance/esfera.rb +4 -4
- data/samples/processing_app/demos/performance/text_rendering.rb +1 -1
- data/samples/processing_app/library/dxf/simple_export.rb +3 -3
- data/samples/processing_app/library/pdf/complex_3D.rb +11 -11
- data/samples/processing_app/library/vecmath/vec2d/bouncing_ball.rb +1 -1
- data/samples/processing_app/library/vecmath/vec2d/circle_collision.rb +3 -3
- data/samples/processing_app/library/vecmath/vec2d/library/flock/flock.rb +5 -5
- data/samples/processing_app/library/vecmath/vec2d/library/particle/{particle_system.rb → particle.rb} +28 -21
- data/samples/processing_app/library/vecmath/vec2d/morph.rb +3 -3
- data/samples/processing_app/library/vecmath/vec2d/particle_system_pshape.rb +3 -3
- data/samples/processing_app/library/vecmath/vec2d/reflection1.rb +2 -2
- data/samples/processing_app/library/vecmath/vec2d/seeking_neural.rb +3 -2
- data/samples/processing_app/library/vecmath/vec2d/simple_particle_system.rb +1 -1
- data/samples/processing_app/library/vecmath/vec3d/frame_of_reference.rb +4 -4
- data/samples/processing_app/topics/advanced_data/library/word/word.rb +1 -1
- data/samples/processing_app/topics/advanced_data/load_save_XML.rb +1 -1
- data/samples/processing_app/topics/advanced_data/load_save_json.rb +1 -1
- data/samples/processing_app/topics/advanced_data/load_save_struct_yaml.rb +1 -1
- data/samples/processing_app/topics/advanced_data/load_save_yaml.rb +1 -1
- data/samples/processing_app/topics/advanced_data/threads_two.rb +1 -2
- data/samples/processing_app/topics/cellular_automata/game_of_life.rb +3 -3
- data/samples/processing_app/topics/cellular_automata/library/ca/ca.rb +2 -2
- data/samples/processing_app/topics/cellular_automata/library/cell/cell.rb +4 -4
- data/samples/processing_app/topics/cellular_automata/library/simple_cell/simple_cell.rb +1 -1
- data/samples/processing_app/topics/cellular_automata/spore1.rb +1 -1
- data/samples/processing_app/topics/cellular_automata/spore2.rb +1 -1
- data/samples/processing_app/topics/create_shapes/group_pshape.rb +2 -2
- data/samples/processing_app/topics/create_shapes/library/particle/particle_system.rb +3 -3
- data/samples/processing_app/topics/create_shapes/library/polygon/polygon.rb +2 -2
- data/samples/processing_app/topics/create_shapes/library/star/star.rb +3 -3
- data/samples/processing_app/topics/create_shapes/particle_system_pshape.rb +1 -1
- data/samples/processing_app/topics/create_shapes/path_pshape.rb +1 -1
- data/samples/processing_app/topics/drawing/scribble_plotter.rb +2 -2
- data/samples/processing_app/topics/gui/button.rb +16 -16
- data/samples/processing_app/topics/gui/rollover.rb +13 -13
- data/samples/processing_app/topics/image_processing/blur.rb +2 -2
- data/samples/processing_app/topics/image_processing/edge_detection.rb +2 -2
- data/samples/processing_app/topics/lsystems/cstest.rb +1 -1
- data/samples/processing_app/topics/lsystems/library/stochastic_grammar/stochastic_grammar.rb +1 -1
- data/samples/processing_app/topics/motion/bouncy_bubbles.rb +1 -1
- data/samples/processing_app/topics/motion/brownian.rb +2 -2
- data/samples/processing_app/topics/motion/circle_collision.rb +1 -1
- data/samples/processing_app/topics/motion/cubes_in_cube.rb +1 -1
- data/samples/processing_app/topics/motion/library/cube/cube.rb +2 -2
- data/samples/processing_app/topics/motion/morph.rb +11 -11
- data/samples/processing_app/topics/motion/moving_on_curves.rb +4 -4
- data/samples/processing_app/topics/motion/puff.rb +4 -4
- data/samples/processing_app/topics/motion/reflection1.rb +3 -3
- data/samples/processing_app/topics/motion/reflection2.rb +1 -1
- data/samples/processing_app/topics/shaders/bw_shader.rb +1 -1
- data/samples/processing_app/topics/shaders/glsl_heightmap_noise.rb +2 -2
- data/samples/processing_app/topics/simulate/flocking.rb +6 -6
- data/samples/processing_app/topics/simulate/library/flock/flock.rb +33 -41
- data/samples/processing_app/topics/simulate/multiple_particle_systems.rb +34 -60
- data/samples/processing_app/topics/simulate/simple_particle_system.rb +20 -32
- data/samples/processing_app/topics/simulate/smoke_particle_system.rb +45 -57
- data/samples/processing_app/topics/simulate/springs.rb +40 -44
- metadata +4 -5
- data/samples/processing_app/library/vecmath/vec2d/multiple_particle_systems.rb +0 -141
- data/samples/processing_app/library/vecmath/vec2d/smoke_particle_system.rb +0 -133
@@ -20,10 +20,10 @@ module Processing
|
|
20
20
|
def load_libraries(*args)
|
21
21
|
args.each do |lib|
|
22
22
|
loaded = load_ruby_library(lib) || load_java_library(lib)
|
23
|
-
|
23
|
+
fail(LoadError.new, "no such file to load -- #{lib}") unless loaded
|
24
24
|
end
|
25
25
|
end
|
26
|
-
|
26
|
+
alias_method :load_library, :load_libraries
|
27
27
|
|
28
28
|
# For pure ruby libraries.
|
29
29
|
# The library should have an initialization ruby file
|
@@ -38,17 +38,16 @@ module Processing
|
|
38
38
|
return false
|
39
39
|
end
|
40
40
|
end
|
41
|
-
|
42
41
|
path = get_library_paths(library_name, 'rb').first
|
43
42
|
return false unless path
|
44
|
-
|
43
|
+
@loaded_libraries[library_name] = (require path)
|
45
44
|
end
|
46
45
|
|
47
46
|
# For pure java libraries, such as the ones that are available
|
48
47
|
# on this page: http://processing.org/reference/libraries/index.html
|
49
48
|
#
|
50
49
|
# P.S. -- Loading libraries which include native code needs to
|
51
|
-
# hack the Java ClassLoader, so that you don't have to
|
50
|
+
# hack the 'Java ClassLoader', so that you don't have to
|
52
51
|
# futz with your PATH. But it's probably bad juju.
|
53
52
|
def load_java_library(library_name)
|
54
53
|
library_name = library_name.to_sym
|
@@ -56,26 +55,23 @@ module Processing
|
|
56
55
|
jpath = get_library_directory_path(library_name, 'jar')
|
57
56
|
jars = get_library_paths(library_name, 'jar')
|
58
57
|
return false if jars.empty?
|
59
|
-
jars.each {|jar| require jar }
|
60
|
-
|
58
|
+
jars.each { |jar| require jar }
|
61
59
|
platform_specific_library_paths = get_platform_specific_library_paths(jpath)
|
62
60
|
platform_specific_library_paths = platform_specific_library_paths.select do |ppath|
|
63
61
|
test(?d, ppath) && !Dir.glob(File.join(ppath, '*.{so,dll,jnilib}')).empty?
|
64
62
|
end
|
65
63
|
|
66
|
-
|
64
|
+
unless platform_specific_library_paths.empty?
|
67
65
|
platform_specific_library_paths << java.lang.System.getProperty('java.library.path')
|
68
66
|
new_library_path = platform_specific_library_paths.join(java.io.File.pathSeparator)
|
69
|
-
|
70
67
|
java.lang.System.setProperty('java.library.path', new_library_path)
|
71
|
-
|
72
68
|
field = java.lang.Class.for_name('java.lang.ClassLoader').get_declared_field('sys_paths')
|
73
69
|
if field
|
74
70
|
field.accessible = true
|
75
71
|
field.set(java.lang.Class.for_name('java.lang.System').get_class_loader, nil)
|
76
72
|
end
|
77
73
|
end
|
78
|
-
|
74
|
+
@loaded_libraries[library_name] = true
|
79
75
|
end
|
80
76
|
|
81
77
|
def get_platform_specific_library_paths(basename)
|
@@ -87,12 +83,11 @@ module Processing
|
|
87
83
|
java.lang.System.getProperty('java.vm.name').index('64')
|
88
84
|
bits = '64'
|
89
85
|
end
|
90
|
-
|
91
|
-
_match_string_, platform = {'Mac' => 'macosx', 'Linux' => 'linux', 'Windows' => 'windows' }.detect do |string, _platform_|
|
86
|
+
_match_string_, platform = {'Mac' => 'macosx', 'Linux' => 'linux', 'Windows' => 'windows' }.find do |string, _platform_|
|
92
87
|
java.lang.System.getProperty('os.name').index(string)
|
93
88
|
end
|
94
89
|
platform ||= 'other'
|
95
|
-
[platform, platform + bits].
|
90
|
+
[platform, platform + bits].map { |p| File.join(basename, p) }
|
96
91
|
end
|
97
92
|
|
98
93
|
def get_library_paths(library_name, extension = nil)
|
@@ -103,7 +98,7 @@ module Processing
|
|
103
98
|
protected
|
104
99
|
|
105
100
|
def get_library_directory_path(library_name, extension = nil)
|
106
|
-
extensions = extension ? [extension] : %w
|
101
|
+
extensions = extension ? [extension] : %w(jar rb)
|
107
102
|
extensions.each do |ext|
|
108
103
|
[ "#{SKETCH_ROOT}/library/#{library_name}",
|
109
104
|
"#{Processing::RP_CONFIG['PROCESSING_ROOT']}/modes/java/libraries/#{library_name}/library",
|
@@ -138,7 +133,7 @@ module Processing
|
|
138
133
|
sketchbook_paths << spath
|
139
134
|
end
|
140
135
|
end
|
141
|
-
|
136
|
+
unless preferences_paths.empty?
|
142
137
|
matched_lines = File.readlines(preferences_paths.first).grep(/^sketchbook\.path=(.+)/) { $1 }
|
143
138
|
sketchbook_path = matched_lines.first
|
144
139
|
else
|
@@ -45,7 +45,7 @@ module Processing
|
|
45
45
|
|
46
46
|
# Start running a ruby-processing sketch from the passed-in arguments
|
47
47
|
def self.execute
|
48
|
-
runner =
|
48
|
+
runner = new
|
49
49
|
runner.parse_options(ARGV)
|
50
50
|
runner.execute!
|
51
51
|
end
|
@@ -92,20 +92,20 @@ module Processing
|
|
92
92
|
|
93
93
|
# Just simply run a ruby-processing sketch.
|
94
94
|
def run(sketch, args)
|
95
|
-
|
95
|
+
ensure_exists(sketch)
|
96
96
|
spin_up('run.rb', sketch, args)
|
97
97
|
end
|
98
98
|
|
99
99
|
# Run a sketch, keeping an eye on it's file, and reloading
|
100
100
|
# whenever it changes.
|
101
101
|
def watch(sketch, args)
|
102
|
-
|
102
|
+
ensure_exists(sketch)
|
103
103
|
spin_up('watch.rb', sketch, args)
|
104
104
|
end
|
105
105
|
|
106
106
|
# Run a sketch, opening its guts to IRB, letting you play with it.
|
107
107
|
def live(sketch, args)
|
108
|
-
|
108
|
+
ensure_exists(sketch)
|
109
109
|
spin_up('live.rb', sketch, args)
|
110
110
|
end
|
111
111
|
|
@@ -124,7 +124,7 @@ module Processing
|
|
124
124
|
check(proc_root, installed)
|
125
125
|
when /install/
|
126
126
|
system "cd #{RP5_ROOT}/vendors && rake"
|
127
|
-
|
127
|
+
unless proc_root
|
128
128
|
set_processing_root
|
129
129
|
warn 'PROCESSING_ROOT set optimistically, run check to confirm'
|
130
130
|
end
|
@@ -199,7 +199,7 @@ module Processing
|
|
199
199
|
end
|
200
200
|
|
201
201
|
def ensure_exists(sketch)
|
202
|
-
puts "Couldn't find: #{sketch}"
|
202
|
+
puts "Couldn't find: #{sketch}" && exit unless File.exist?(sketch)
|
203
203
|
end
|
204
204
|
|
205
205
|
def jruby_complete
|
@@ -213,23 +213,21 @@ module Processing
|
|
213
213
|
end
|
214
214
|
|
215
215
|
def os
|
216
|
-
@os ||=
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
end
|
216
|
+
@os ||= host_os = RbConfig::CONFIG['host_os']
|
217
|
+
case host_os
|
218
|
+
when /mswin|msys|mingw|cygwin|bccwin|wince|emc/
|
219
|
+
:windows
|
220
|
+
when /darwin|mac os/
|
221
|
+
:macosx
|
222
|
+
when /linux/
|
223
|
+
:linux
|
224
|
+
when /solaris|bsd/
|
225
|
+
:unix
|
226
|
+
else
|
227
|
+
fail "unknown os: #{host_os.inspect}"
|
228
|
+
end
|
230
229
|
end
|
231
230
|
|
232
|
-
|
233
231
|
# Optimistically set processing root
|
234
232
|
def set_processing_root
|
235
233
|
require 'psych'
|
@@ -237,13 +235,13 @@ module Processing
|
|
237
235
|
data = {}
|
238
236
|
path = File.expand_path("#{ENV['HOME']}/.rp5rc")
|
239
237
|
if @os == :macosx
|
240
|
-
data['PROCESSING_ROOT'] =
|
238
|
+
data['PROCESSING_ROOT'] = '/Applications/Processing.app/Contents/Java'
|
241
239
|
else
|
242
240
|
root = "#{ENV['HOME']}/processing-2.2.1"
|
243
241
|
data['PROCESSING_ROOT'] = root
|
244
242
|
end
|
245
|
-
data['JRUBY'] =
|
246
|
-
open(path, 'w:UTF-8') {|f| f.write(data.to_yaml) }
|
243
|
+
data['JRUBY'] = true
|
244
|
+
open(path, 'w:UTF-8') { |f| f.write(data.to_yaml) }
|
247
245
|
end
|
248
246
|
|
249
247
|
# On the Mac, we can display a fat, shiny ruby in the Dock.
|
@@ -15,7 +15,7 @@ module Processing
|
|
15
15
|
# and restarting the sketch whenever it changes.
|
16
16
|
def start_watching
|
17
17
|
start_runner
|
18
|
-
loop do
|
18
|
+
Kernel.loop do
|
19
19
|
if @files.find { |file| File.exist?(file) && File.stat(file).mtime > @time }
|
20
20
|
puts 'reloading sketch...'
|
21
21
|
$app && $app.close
|
data/library/boids/boids.rb
CHANGED
@@ -14,22 +14,22 @@ class Boid
|
|
14
14
|
@perch_time = 0.0
|
15
15
|
end
|
16
16
|
|
17
|
-
|
17
|
+
def cohesion(d = 100.0)
|
18
18
|
# Boids gravitate towards the center of the flock,
|
19
19
|
# Which is the averaged position of the rest of the boids.
|
20
20
|
cvx, cvy, cvz = 0.0, 0.0, 0.0
|
21
|
-
boids.reject{|bd| bd.equal? self}.each do |boid|
|
22
|
-
cvx, cvy, cvz = cvx+boid.x, cvy+boid.y, cvz+boid.z
|
21
|
+
boids.reject { |bd| bd.equal? self }.each do |boid|
|
22
|
+
cvx, cvy, cvz = cvx + boid.x, cvy + boid.y, cvz + boid.z
|
23
23
|
end
|
24
24
|
count = boids.length - 1.0
|
25
|
-
cvx, cvy, cvz = cvx/count, cvy/count, cvz/count
|
25
|
+
cvx, cvy, cvz = cvx / count, cvy / count, cvz / count
|
26
26
|
return (cvx - x) / d, (cvy - y) / d, (cvz - z) / d
|
27
27
|
end
|
28
28
|
|
29
29
|
def separation(radius = 10.0)
|
30
30
|
# Boids don't like to cuddle.
|
31
31
|
svx, svy, svz = 0.0, 0.0, 0.0
|
32
|
-
boids.reject{|bd| bd.equal? self}.each do |boid|
|
32
|
+
boids.reject { |bd| bd.equal? self }.each do |boid|
|
33
33
|
dvx, dvy, dvz = x - boid.x, y - boid.y, z - boid.z
|
34
34
|
svx += dvx if dvx.abs < radius
|
35
35
|
svy += dvy if dvy.abs < radius
|
@@ -41,15 +41,15 @@ class Boid
|
|
41
41
|
def alignment(d = 5.0)
|
42
42
|
# Boids like to fly at the speed of traffic.
|
43
43
|
avx, avy, avz = 0.0, 0.0, 0.0
|
44
|
-
boids.reject{|bd| bd.equal? self}.each do |boid|
|
45
|
-
avx, avy, avz = avx+boid.vx, avy+boid.vy, avz+boid.vz
|
44
|
+
boids.reject { |bd| bd.equal? self }.each do |boid|
|
45
|
+
avx, avy, avz = avx + boid.vx, avy + boid.vy, avz + boid.vz
|
46
46
|
end
|
47
47
|
count = boids.length - 1.0
|
48
48
|
avx, avy, avz = avx / count, avy / count, avz / count
|
49
49
|
return (avx - vx) / d, (avy - vy) / d, (avz - vz) / d
|
50
50
|
end
|
51
51
|
|
52
|
-
def limit(max=30.0)
|
52
|
+
def limit(max = 30.0)
|
53
53
|
# Tweet, Tweet! The boid police will bust you for breaking the speed limit.
|
54
54
|
most = [@vx.abs, @vy.abs, @vz.abs].max
|
55
55
|
return if most < max
|
@@ -71,10 +71,12 @@ class Boid
|
|
71
71
|
end
|
72
72
|
end
|
73
73
|
|
74
|
+
require 'forwardable'
|
74
75
|
|
75
|
-
|
76
|
-
|
77
|
-
extend
|
76
|
+
class Boids
|
77
|
+
include Enumerable
|
78
|
+
extend Forwardable
|
79
|
+
def_delegators(:@boids, :reject, :<<, :each, :shuffle!, :length, :next)
|
78
80
|
|
79
81
|
attr_accessor :boids, :x, :y, :w, :h,
|
80
82
|
:scattered, :has_goal, :flee
|
@@ -86,34 +88,13 @@ class Boids
|
|
86
88
|
def initialize
|
87
89
|
@boids = []
|
88
90
|
end
|
89
|
-
|
90
|
-
def each &block
|
91
|
-
boids.each &block
|
92
|
-
end
|
93
|
-
|
94
|
-
def reject &block
|
95
|
-
boids.reject &block
|
96
|
-
end
|
97
|
-
|
98
|
-
|
99
|
-
def shuffle!
|
100
|
-
boids.shuffle!
|
101
|
-
end
|
102
|
-
|
103
|
-
def << obj
|
104
|
-
boids << obj
|
105
|
-
end
|
106
|
-
|
107
|
-
def length
|
108
|
-
boids.length
|
109
|
-
end
|
110
|
-
|
91
|
+
|
111
92
|
def self.flock(n, x, y, w, h)
|
112
|
-
|
93
|
+
Boids.new.setup(n, x, y, w, h)
|
113
94
|
end
|
114
95
|
|
115
96
|
def setup(n, x, y, w, h)
|
116
|
-
n.times do
|
97
|
+
n.times do
|
117
98
|
dx, dy = rand(w), rand(h)
|
118
99
|
z = rand(200.0)
|
119
100
|
self << Boid.new(self, x + dx, y + dy, z)
|
@@ -125,11 +106,11 @@ class Boids
|
|
125
106
|
@scatter_i = 0.0
|
126
107
|
@perch = 1.0 # Lower this number to divebomb.
|
127
108
|
@perch_y = h
|
128
|
-
@perch_t = -> {rand(25..75.0)}
|
109
|
+
@perch_t = -> { rand(25..75.0) }
|
129
110
|
@has_goal = false
|
130
111
|
@flee = false
|
131
112
|
@goal_x = @goal_y = @goal_z = 0.0
|
132
|
-
|
113
|
+
self
|
133
114
|
end
|
134
115
|
|
135
116
|
def scatter(chance = 0.005, frames = 50.0)
|
@@ -142,7 +123,7 @@ class Boids
|
|
142
123
|
end
|
143
124
|
|
144
125
|
def perch(ground = nil, chance = 1.0, frames = nil)
|
145
|
-
frames ||= -> {rand(25..75.0)}
|
126
|
+
frames ||= -> { rand(25..75.0) }
|
146
127
|
ground ||= h
|
147
128
|
@perch, @perch_y, @perch_t = chance, ground, frames
|
148
129
|
end
|
@@ -164,40 +145,36 @@ class Boids
|
|
164
145
|
def constrain
|
165
146
|
# Put them boids in a cage.
|
166
147
|
dx, dy = w * 0.1, h * 0.1
|
167
|
-
|
148
|
+
each do |b|
|
168
149
|
b.vx += rand(dx) if b.x < x - dx
|
169
150
|
b.vx += rand(dy) if b.y < y - dy
|
170
151
|
b.vx -= rand(dx) if b.x > x + w + dx
|
171
152
|
b.vy -= rand(dy) if b.y > y + h + dy
|
172
153
|
b.vz += 10.0 if b.z < 0.0
|
173
154
|
b.vz -= 10.0 if b.z > 100.0
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
@perch_t.respond_to?(:call) ? b.perch_time = @perch_t.call : b.perch_time = @perch_t
|
180
|
-
end
|
155
|
+
next unless b.y > @perch_y && rand < @perch
|
156
|
+
b.y = @perch_y
|
157
|
+
b.vy = -(b.vy.abs) * 0.2
|
158
|
+
b.is_perching = true
|
159
|
+
@perch_t.respond_to?(:call) ? b.perch_time = @perch_t.call : b.perch_time = @perch_t
|
181
160
|
end
|
182
161
|
end
|
183
162
|
|
184
|
-
def update(opts={}) # Just flutter, little boids ... just flutter away.
|
163
|
+
def update(opts = {}) # Just flutter, little boids ... just flutter away.
|
185
164
|
options = {
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
}
|
165
|
+
shuffled: true, # Shuffling keeps things flowing smooth.
|
166
|
+
cohesion: 100.0,
|
167
|
+
separation: 10.0,
|
168
|
+
alignment: 5.0,
|
169
|
+
goal: 20.0,
|
170
|
+
limit: 30.0
|
171
|
+
}
|
193
172
|
options.merge! opts
|
194
|
-
|
195
|
-
self.shuffle! if options[:shuffled]
|
173
|
+
shuffle! if options[:shuffled]
|
196
174
|
m1 = 1.0 # cohesion
|
197
175
|
m2 = 1.0 # separation
|
198
176
|
m3 = 1.0 # alignment
|
199
177
|
m4 = 1.0 # goal
|
200
|
-
|
201
178
|
@scattered = true if !(@scattered) && rand < @scatter
|
202
179
|
if @scattered
|
203
180
|
m1 = -m1
|
@@ -208,11 +185,9 @@ class Boids
|
|
208
185
|
@scattered = false
|
209
186
|
@scatter_i = 0.0
|
210
187
|
end
|
211
|
-
|
212
188
|
m4 = 0.0 unless @has_goal
|
213
189
|
m4 = -m4 if @flee
|
214
|
-
|
215
|
-
self.each do |b|
|
190
|
+
each do |b|
|
216
191
|
if b.is_perching
|
217
192
|
if b.perch_time > 0.0
|
218
193
|
b.perch_time -= 1.0
|
@@ -221,24 +196,18 @@ class Boids
|
|
221
196
|
b.is_perching = false
|
222
197
|
end
|
223
198
|
end
|
224
|
-
|
225
199
|
vx1, vy1, vz1 = b.cohesion(options[:cohesion])
|
226
200
|
vx2, vy2, vz2 = b.separation(options[:separation])
|
227
201
|
vx3, vy3, vz3 = b.alignment(options[:alignment])
|
228
202
|
vx4, vy4, vz4 = b.goal(@goal_x, @goal_y, @goal_z, options[:goal])
|
229
|
-
|
230
|
-
b.
|
231
|
-
b.
|
232
|
-
b.vz += m1*vz1 + m2*vz2 + m3*vz3 + m4*vz4
|
233
|
-
|
203
|
+
b.vx += m1 * vx1 + m2 * vx2 + m3 * vx3 + m4 * vx4
|
204
|
+
b.vy += m1 * vy1 + m2 * vy2 + m3 * vy3 + m4 * vy4
|
205
|
+
b.vz += m1 * vz1 + m2 * vz2 + m3 * vz3 + m4 * vz4
|
234
206
|
b.limit(options[:limit])
|
235
|
-
|
236
207
|
b.x += b.vx
|
237
208
|
b.y += b.vy
|
238
209
|
b.z += b.vz
|
239
210
|
end
|
240
|
-
|
211
|
+
constrain
|
241
212
|
end
|
242
213
|
end
|
243
|
-
|
244
|
-
|
@@ -59,7 +59,7 @@ module ControlPanel
|
|
59
59
|
|
60
60
|
# Creates check-box elements for control_panel
|
61
61
|
class Checkbox < javax.swing.JCheckBox
|
62
|
-
def initialize(control_panel, name, proc=nil)
|
62
|
+
def initialize(control_panel, name, proc = nil)
|
63
63
|
@control_panel = control_panel
|
64
64
|
super(name.to_s)
|
65
65
|
set_preferred_size(java.awt.Dimension.new(190, 64))
|
@@ -107,7 +107,7 @@ module ControlPanel
|
|
107
107
|
set_size 200, 30 + (64 * elements.size)
|
108
108
|
set_default_close_operation javax.swing.JFrame::HIDE_ON_CLOSE
|
109
109
|
set_resizable false
|
110
|
-
set_location($app.width + 10, 0) unless
|
110
|
+
set_location($app.width + 10, 0) unless $app.width + 10 > $app.displayWidth
|
111
111
|
panel.visible = true
|
112
112
|
end
|
113
113
|
|
@@ -154,7 +154,7 @@ module ControlPanel
|
|
154
154
|
laf = lafinfo.select do |info|
|
155
155
|
info.getName.eql? lf.capitalize
|
156
156
|
end
|
157
|
-
javax.swing.UIManager.setLookAndFeel(laf[0].getClassName)
|
157
|
+
javax.swing.UIManager.setLookAndFeel(laf[0].getClassName)
|
158
158
|
end
|
159
159
|
end
|
160
160
|
|