ruby-processing 2.4.4 → 2.5.0
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 +8 -6
- data/lib/ruby-processing/app.rb +54 -67
- data/lib/ruby-processing/config.rb +7 -7
- data/lib/ruby-processing/exporters/application_exporter.rb +37 -38
- data/lib/ruby-processing/exporters/base_exporter.rb +34 -37
- data/lib/ruby-processing/exporters/creator.rb +27 -25
- data/lib/ruby-processing/helper_methods.rb +18 -22
- data/lib/ruby-processing/helpers/numeric.rb +5 -6
- data/lib/ruby-processing/helpers/string.rb +11 -20
- data/lib/ruby-processing/library_loader.rb +33 -35
- data/lib/ruby-processing/runner.rb +118 -65
- data/lib/ruby-processing/runners/base.rb +21 -26
- data/lib/ruby-processing/runners/live.rb +3 -4
- data/lib/ruby-processing/runners/run.rb +1 -3
- data/lib/ruby-processing/runners/watch.rb +8 -8
- data/lib/ruby-processing/version.rb +1 -1
- data/lib/templates/application/lib/args.txt.erb +1 -1
- data/lib/templates/application/run.erb +3 -3
- data/library/control_panel/control_panel.rb +29 -29
- data/library/fastmath/fastmath.rb +3 -0
- data/library/file_chooser/file_chooser.rb +11 -13
- data/library/vecmath/vecmath.rb +6 -35
- data/samples/Rakefile +3 -1
- data/samples/configRP5/configRP5.pde +150 -0
- data/samples/contributed/Rakefile +6 -6
- data/samples/contributed/animator.rb +6 -6
- data/samples/contributed/bezier_playground.rb +29 -68
- data/samples/contributed/circle_collision.rb +6 -13
- data/samples/contributed/elegant_ball.rb +14 -17
- data/samples/contributed/empathy.rb +8 -5
- data/samples/contributed/fibonacci_sphere.rb +10 -49
- data/samples/contributed/full_screen.rb +1 -1
- data/samples/contributed/grapher.rb +10 -9
- data/samples/contributed/gravity.rb +11 -16
- data/samples/contributed/jwishy.rb +18 -20
- data/samples/contributed/quadraticvertex.rb +26 -26
- data/samples/external_library/java_processing/custom/landscape.rb +6 -4
- data/samples/external_library/java_processing/hemesh/twin_iso.rb +69 -71
- data/samples/external_library/java_processing/pbox2d/bumpy_surface_noise.rb +57 -0
- data/samples/external_library/java_processing/pbox2d/contact_test.rb +23 -0
- data/samples/external_library/java_processing/pbox2d/data/java_args.txt +2 -0
- data/samples/external_library/java_processing/pbox2d/library/custom_shape/custom_shape.rb +158 -0
- data/samples/external_library/java_processing/pbox2d/library/particle_system/particle_system.rb +212 -0
- data/samples/external_library/java_processing/pbox2d/library/surface/surface.rb +154 -0
- data/samples/external_library/java_processing/pbox2d/liquidy.rb +58 -0
- data/samples/external_library/java_processing/pbox2d/polygons.rb +58 -0
- data/samples/external_library/java_processing/toxiclibs/library/vbo/vbo.rb +52 -0
- data/samples/external_library/java_processing/toxiclibs/model_align.rb +9 -16
- data/samples/processing_app/basics/color/color_wheel.rb +1 -1
- data/samples/processing_app/basics/form/regular_polygon.rb +2 -2
- data/samples/processing_app/basics/form/rgb_cube.rb +2 -2
- data/samples/processing_app/basics/form/shape_primitives.rb +1 -1
- data/samples/processing_app/basics/form/star.rb +2 -2
- data/samples/processing_app/basics/input/clock.rb +21 -16
- data/samples/processing_app/basics/input/storing_input.rb +6 -6
- data/samples/processing_app/basics/math/additive_wave.rb +1 -1
- data/samples/processing_app/basics/math/sine.rb +2 -2
- data/samples/processing_app/basics/math/sine_cosine.rb +1 -1
- data/samples/processing_app/basics/math/sine_wave.rb +1 -1
- data/samples/processing_app/basics/objects/inheritance.rb +2 -0
- data/samples/processing_app/basics/objects/inheritance_two.rb +88 -0
- data/samples/processing_app/basics/objects/module.rb +89 -0
- data/samples/processing_app/{library/vecmath/drawolver.rb → basics/objects/module_two.rb} +1 -1
- data/samples/processing_app/basics/objects/multiple_constructors.rb +5 -0
- data/samples/processing_app/basics/objects/struct.rb +38 -0
- data/samples/processing_app/basics/structure/recursion2.rb +1 -1
- data/samples/processing_app/basics/transform/birds.rb +7 -10
- data/samples/processing_app/basics/transform/library/bird/bird.rb +3 -3
- data/samples/processing_app/basics/transform/rotate.rb +1 -1
- data/samples/processing_app/basics/transform/rotate1.rb +1 -1
- data/samples/processing_app/basics/transform/rotate_x_y.rb +1 -1
- data/samples/processing_app/demos/graphics/bezier_patch.rb +1 -38
- data/samples/processing_app/demos/graphics/trefoil.rb +50 -40
- data/samples/processing_app/demos/graphics/wiggling.rb +36 -38
- data/samples/processing_app/demos/performance/cubic_grid_retained.rb +14 -14
- data/samples/processing_app/demos/performance/esfera.rb +27 -39
- data/samples/processing_app/demos/performance/text_rendering.rb +1 -1
- data/samples/processing_app/library/fastmath/clock.rb +44 -0
- data/samples/processing_app/library/pdf/complex_3D.rb +26 -24
- data/samples/processing_app/library/vecmath/{Rakefile → arcball/Rakefile} +6 -6
- data/samples/processing_app/library/vecmath/arcball/arcball_box.rb +23 -0
- data/samples/processing_app/library/vecmath/arcball/arcball_radius.rb +23 -0
- data/samples/processing_app/library/vecmath/arcball/arcball_shape.rb +29 -0
- data/samples/processing_app/library/vecmath/{README.txt → vec2d/README.txt} +0 -0
- data/samples/processing_app/library/vecmath/vec2d/Rakefile +30 -0
- data/samples/processing_app/library/vecmath/{acceleration_with_vectors.rb → vec2d/acceleration_with_vectors.rb} +4 -5
- data/samples/processing_app/library/vecmath/{bouncing_ball.rb → vec2d/bouncing_ball.rb} +9 -15
- data/samples/processing_app/library/vecmath/{circle_collision.rb → vec2d/circle_collision.rb} +46 -51
- data/samples/processing_app/library/vecmath/{data → vec2d/data}/java_args.txt +0 -0
- data/samples/processing_app/library/vecmath/{data → vec2d/data}/sprite.png +0 -0
- data/samples/processing_app/library/vecmath/{data → vec2d/data}/texture.png +0 -0
- data/samples/processing_app/library/vecmath/{flocking.rb → vec2d/flocking.rb} +3 -6
- data/samples/processing_app/library/vecmath/{library → vec2d/library}/flock/flock.rb +47 -67
- data/samples/processing_app/library/vecmath/{library → vec2d/library}/particle/particle_system.rb +5 -5
- data/samples/processing_app/library/vecmath/vec2d/library/tile/tile.rb +85 -0
- data/samples/processing_app/library/vecmath/{morph.rb → vec2d/morph.rb} +17 -16
- data/samples/processing_app/library/vecmath/{multiple_particle_systems.rb → vec2d/multiple_particle_systems.rb} +11 -28
- data/samples/processing_app/library/vecmath/{particle_system_pshape.rb → vec2d/particle_system_pshape.rb} +4 -6
- data/samples/processing_app/library/vecmath/vec2d/penrose.rb +71 -0
- data/samples/processing_app/library/vecmath/{reflection1.rb → vec2d/reflection1.rb} +6 -6
- data/samples/processing_app/library/vecmath/{seeking_neural.rb → vec2d/seeking_neural.rb} +12 -16
- data/samples/processing_app/library/vecmath/{simple_particle_system.rb → vec2d/simple_particle_system.rb} +8 -12
- data/samples/processing_app/library/vecmath/{smoke_particle_system.rb → vec2d/smoke_particle_system.rb} +8 -13
- data/samples/processing_app/library/vecmath/{vector_math.rb → vec2d/vector_math.rb} +0 -0
- data/samples/processing_app/library/vecmath/{library/wiggler/wiggler.rb → vec2d/wiggle_pshape.rb} +28 -4
- data/samples/processing_app/library/vecmath/vec3d/Rakefile +30 -0
- data/samples/processing_app/library/vecmath/vec3d/drawolver.rb +86 -0
- data/samples/processing_app/library/vecmath/vec3d/frame_of_reference.rb +103 -0
- data/samples/processing_app/library/vecmath/{hilbert_fractal.rb → vec3d/hilbert_fractal.rb} +5 -33
- data/samples/processing_app/library/vecmath/vec3d/library/geometry/geometry.rb +3 -0
- data/samples/processing_app/library/vecmath/vec3d/library/geometry/lib/cylinder.rb +44 -0
- data/samples/processing_app/library/vecmath/vec3d/library/geometry/lib/mat4.rb +28 -0
- data/samples/processing_app/library/vecmath/vec3d/library/geometry/lib/plane.rb +60 -0
- data/samples/processing_app/library/vecmath/{library → vec3d/library}/hilbert/hilbert.rb +14 -17
- data/samples/processing_app/library/vecmath/{retained_menger.rb → vec3d/retained_menger.rb} +20 -55
- data/samples/processing_app/topics/advanced_data/Rakefile +6 -6
- data/samples/processing_app/topics/advanced_data/counting_words.rb +7 -7
- data/samples/processing_app/topics/advanced_data/data/data.xml +0 -15
- data/samples/processing_app/topics/advanced_data/data/struct_data.yml +5 -0
- data/samples/processing_app/topics/advanced_data/library/bubble/bubble.rb +8 -8
- data/samples/processing_app/topics/advanced_data/load_save_XML.rb +41 -42
- data/samples/processing_app/topics/advanced_data/load_save_json.rb +11 -11
- data/samples/processing_app/topics/advanced_data/load_save_struct_yaml.rb +22 -23
- data/samples/processing_app/topics/advanced_data/load_save_table.rb +12 -12
- data/samples/processing_app/topics/advanced_data/load_save_yaml.rb +10 -12
- data/samples/processing_app/topics/advanced_data/regex.rb +5 -6
- data/samples/processing_app/topics/advanced_data/threads.rb +27 -27
- data/samples/processing_app/topics/advanced_data/threads_two.rb +3 -2
- data/samples/processing_app/topics/advanced_data/word_frequency.rb +18 -16
- data/samples/processing_app/topics/advanced_data/yahoo_weather.rb +6 -5
- data/samples/processing_app/topics/animation/animated_sprite.rb +9 -7
- data/samples/processing_app/topics/animation/sequential.rb +19 -19
- data/samples/processing_app/topics/cellular_automata/library/ca/ca.rb +1 -1
- data/samples/processing_app/topics/cellular_automata/wolfram.rb +1 -1
- data/samples/processing_app/topics/create_shapes/begin_end_contour.rb +1 -1
- data/samples/processing_app/topics/create_shapes/library/wiggler/wiggler.rb +4 -4
- data/samples/processing_app/topics/create_shapes/particle_system_pshape.rb +2 -2
- data/samples/processing_app/topics/create_shapes/path_pshape.rb +1 -1
- data/samples/processing_app/topics/create_shapes/polygon_pshape_oo3.rb +1 -1
- data/samples/processing_app/topics/create_shapes/polygon_shape.rb +3 -3
- data/samples/processing_app/topics/file_io/load_file1.rb +1 -1
- data/samples/processing_app/topics/file_io/save_frames.rb +5 -5
- data/samples/processing_app/topics/file_io/save_one_image.rb +2 -2
- data/samples/processing_app/topics/file_io/tile_images.rb +1 -1
- data/samples/processing_app/topics/gui/scrollbar.rb +3 -3
- data/samples/processing_app/topics/image_processing/blending.rb +13 -13
- data/samples/processing_app/topics/image_processing/blur.rb +1 -1
- data/samples/processing_app/topics/image_processing/brightness.rb +1 -1
- data/samples/processing_app/topics/image_processing/convolution.rb +1 -1
- data/samples/processing_app/topics/image_processing/edge_detection.rb +4 -4
- data/samples/processing_app/topics/image_processing/explode.rb +1 -1
- data/samples/processing_app/topics/image_processing/extrusion.rb +1 -1
- data/samples/processing_app/topics/image_processing/histogram.rb +1 -1
- data/samples/processing_app/topics/image_processing/linear_image.rb +1 -1
- data/samples/processing_app/topics/image_processing/pixel_array.rb +1 -1
- data/samples/processing_app/topics/image_processing/zoom.rb +2 -2
- data/samples/processing_app/topics/lsystems/Rakefile +6 -6
- data/samples/processing_app/topics/lsystems/chequer.rb +4 -10
- data/samples/processing_app/topics/lsystems/csplant.rb +25 -25
- data/samples/processing_app/topics/lsystems/cstest.rb +9 -4
- data/samples/processing_app/topics/lsystems/david_tour.rb +14 -16
- data/samples/processing_app/topics/lsystems/koch.rb +8 -18
- data/samples/processing_app/topics/lsystems/library/cs_grammar/cs_grammar.rb +18 -24
- data/samples/processing_app/topics/lsystems/library/grammar/grammar.rb +7 -9
- data/samples/processing_app/topics/lsystems/library/koch/koch_fractal.rb +75 -94
- data/samples/processing_app/topics/lsystems/library/stochastic_grammar/stochastic_grammar.rb +11 -13
- data/samples/processing_app/topics/lsystems/mpeano.rb +29 -32
- data/samples/processing_app/topics/lsystems/peano.rb +19 -21
- data/samples/processing_app/topics/lsystems/penrose.rb +31 -37
- data/samples/processing_app/topics/lsystems/penrose_snowflake.rb +10 -15
- data/samples/processing_app/topics/lsystems/pentagonal.rb +20 -23
- data/samples/processing_app/topics/lsystems/snake_kolam.rb +13 -13
- data/samples/processing_app/topics/lsystems/stochastic_test.rb +25 -28
- data/samples/processing_app/topics/lsystems/three_d_tree.rb +25 -24
- data/samples/processing_app/topics/motion/bouncy_bubbles.rb +17 -17
- data/samples/processing_app/topics/motion/cubes_in_cube.rb +1 -3
- data/samples/processing_app/topics/motion/library/cube/cube.rb +2 -2
- data/samples/processing_app/topics/motion/reflection2.rb +1 -1
- data/samples/processing_app/topics/shaders/blur_filter.rb +3 -3
- data/samples/processing_app/topics/shaders/bw_shader.rb +3 -3
- data/samples/processing_app/topics/shaders/conway.rb +5 -5
- data/samples/processing_app/topics/shaders/deform.rb +5 -5
- data/samples/processing_app/topics/shaders/edge_detect.rb +3 -5
- data/samples/processing_app/topics/shaders/glossy_fish_eye.rb +15 -21
- data/samples/processing_app/topics/shaders/glsl_heightmap_noise.rb +21 -19
- data/samples/processing_app/topics/shaders/image_mask.rb +6 -6
- data/samples/processing_app/topics/shaders/landscape.rb +4 -4
- data/samples/processing_app/topics/shaders/monjori.rb +15 -15
- data/samples/processing_app/topics/shaders/nebula.rb +3 -3
- data/samples/processing_app/topics/shaders/sep_blur.rb +15 -15
- data/samples/processing_app/topics/shaders/toon_shading.rb +2 -2
- data/samples/processing_app/topics/simulate/chain.rb +16 -14
- data/samples/processing_app/topics/simulate/smoke_particle_system.rb +3 -3
- data/samples/processing_app/topics/vectors/acceleration_with_vectors.rb +3 -2
- data/samples/processing_app/topics/vectors/bouncing_ball.rb +1 -0
- data/samples/processing_app/topics/vectors/vector_math.rb +2 -1
- data/vendors/Rakefile +15 -3
- metadata +99 -86
- data/.gitignore +0 -13
- data/.travis.yml +0 -6
- data/.yardopts +0 -5
- data/CHANGELOG +0 -316
- data/CONTRIBUTING.md +0 -35
- data/Gemfile +0 -6
- data/LICENSE.md +0 -39
- data/README.md +0 -119
- data/Rakefile +0 -37
- data/bin/install_jruby_complete +0 -13
- data/lib/ruby-processing/exporters/exporters.rb +0 -7
- data/library/vecmath/lib/arcball.rb +0 -64
- data/library/vecmath/lib/quaternion.rb +0 -62
- data/library/vecmath/lib/vec.rb +0 -232
- data/ruby-processing.gemspec +0 -54
- data/samples/processing_app/library/pdf/.gitignore +0 -1
- data/samples/processing_app/library/vecmath/retained_test.rb +0 -65
- data/samples/processing_app/library/vecmath/retained_test2.rb +0 -65
- data/samples/processing_app/library/vecmath/wiggle_pshape.rb +0 -22
- data/test/README.md +0 -6
- data/test/basic.rb +0 -14
- data/test/draw_ex.rb +0 -12
- data/test/graphics.rb +0 -7
- data/test/p2d.rb +0 -14
- data/test/p3d.rb +0 -14
- data/test/rp5_test.rb +0 -86
- data/test/setup_ex.rb +0 -12
|
File without changes
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
# Simple demo Rakefile to autorun samples in current directory
|
|
2
|
+
# adjust path to rp5 executable, and or opts as required
|
|
3
|
+
|
|
4
|
+
SAMPLES_DIR = './'
|
|
5
|
+
|
|
6
|
+
desc 'run demo'
|
|
7
|
+
task default: [:demo]
|
|
8
|
+
|
|
9
|
+
desc 'demo'
|
|
10
|
+
task :demo do
|
|
11
|
+
samples_list.shuffle.each{ |sample| run_sample sample }
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def samples_list
|
|
15
|
+
files = []
|
|
16
|
+
Dir.chdir(SAMPLES_DIR)
|
|
17
|
+
Dir.glob('*.rb').each do |file|
|
|
18
|
+
files << File.join(SAMPLES_DIR, file)
|
|
19
|
+
end
|
|
20
|
+
return files
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def run_sample(sample_name)
|
|
24
|
+
puts "Running #{sample_name}...quit to run next sample"
|
|
25
|
+
open("|rp5 --nojruby run #{sample_name}", 'r') do |io|
|
|
26
|
+
while l = io.gets
|
|
27
|
+
puts(l.chop)
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
@@ -5,7 +5,8 @@
|
|
|
5
5
|
#
|
|
6
6
|
# Demonstration of the basics of motion with vector.
|
|
7
7
|
# A "Mover" object stores location, velocity, and acceleration as vectors
|
|
8
|
-
# The motion is controlled by affecting the acceleration (in this case
|
|
8
|
+
# The motion is controlled by affecting the acceleration (in this case
|
|
9
|
+
# towards the mouse)
|
|
9
10
|
#
|
|
10
11
|
# For more examples of simulating motion and physics with vectors, see
|
|
11
12
|
# Simulate/ForcesWithVectors, Simulate/GravitationalAttraction3D
|
|
@@ -16,13 +17,12 @@ load_library 'vecmath'
|
|
|
16
17
|
attr_reader :mover
|
|
17
18
|
|
|
18
19
|
def setup
|
|
19
|
-
size(640,360)
|
|
20
|
+
size(640, 360)
|
|
20
21
|
@mover = Mover.new(width, height)
|
|
21
22
|
end
|
|
22
23
|
|
|
23
24
|
def draw
|
|
24
25
|
background(0)
|
|
25
|
-
|
|
26
26
|
# Update the location
|
|
27
27
|
mover.update
|
|
28
28
|
# Display the Mover
|
|
@@ -59,7 +59,7 @@ class Mover
|
|
|
59
59
|
# Limit the velocity to topspeed, PVector has a limit function
|
|
60
60
|
# here we supply a block that evaluates to a boolean to set
|
|
61
61
|
# the velocit conditionally
|
|
62
|
-
velocity.set_mag
|
|
62
|
+
velocity.set_mag(topspeed) { velocity.mag > topspeed }
|
|
63
63
|
# Location changes by velocity vector
|
|
64
64
|
@location += velocity
|
|
65
65
|
end
|
|
@@ -70,6 +70,5 @@ class Mover
|
|
|
70
70
|
fill(127)
|
|
71
71
|
ellipse(location.x, location.y, 48, 48)
|
|
72
72
|
end
|
|
73
|
-
|
|
74
73
|
end
|
|
75
74
|
|
|
@@ -8,44 +8,38 @@
|
|
|
8
8
|
# See AccelerationWithVectors for an example of how to simulate motion using vectors in an object
|
|
9
9
|
#
|
|
10
10
|
load_library :vecmath
|
|
11
|
+
RADIUS = 24
|
|
11
12
|
|
|
12
13
|
attr_reader :loc, # Location of shape
|
|
13
14
|
:velocity, # Velocity of shape
|
|
14
15
|
:gravity # Gravity acts at the shape's acceleration
|
|
15
16
|
|
|
16
17
|
def setup
|
|
17
|
-
size(640,360)
|
|
18
|
+
size(640, 360)
|
|
18
19
|
smooth 4
|
|
19
|
-
@loc = Vec2D.new(100,100)
|
|
20
|
-
@velocity = Vec2D.new(1.5,2.1)
|
|
21
|
-
@gravity = Vec2D.new(0,0.2)
|
|
20
|
+
@loc = Vec2D.new(100, 100)
|
|
21
|
+
@velocity = Vec2D.new(1.5, 2.1)
|
|
22
|
+
@gravity = Vec2D.new(0, 0.2)
|
|
22
23
|
|
|
23
24
|
end
|
|
24
25
|
|
|
25
26
|
def draw
|
|
26
27
|
background(0)
|
|
27
|
-
|
|
28
28
|
# Add velocity to the location.
|
|
29
29
|
@loc += velocity
|
|
30
30
|
# Add gravity to velocity
|
|
31
31
|
@velocity += gravity
|
|
32
|
-
|
|
33
32
|
# Bounce off edges
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
end
|
|
37
|
-
if (loc.y > height)
|
|
33
|
+
velocity.x *= -1 unless (RADIUS .. width - RADIUS).include?(loc.x)
|
|
34
|
+
if loc.y > (height - RADIUS)
|
|
38
35
|
# We're reducing velocity ever so slightly
|
|
39
36
|
# when it hits the bottom of the window
|
|
40
37
|
velocity.y *= -0.95
|
|
41
|
-
loc.y = height
|
|
38
|
+
loc.y = height - RADIUS
|
|
42
39
|
end
|
|
43
|
-
|
|
44
40
|
# Display circle at location vector
|
|
45
41
|
stroke(255)
|
|
46
42
|
stroke_weight(2)
|
|
47
43
|
fill(127)
|
|
48
|
-
ellipse(loc.x,loc.y,
|
|
44
|
+
ellipse(loc.x, loc.y, RADIUS * 2, RADIUS * 2)
|
|
49
45
|
end
|
|
50
|
-
|
|
51
|
-
|
data/samples/processing_app/library/vecmath/{circle_collision.rb → vec2d/circle_collision.rb}
RENAMED
|
@@ -6,30 +6,30 @@ load_library :vecmath
|
|
|
6
6
|
# This inner class demonstrates the use of Ruby-Processing's emulation of
|
|
7
7
|
# Java inner classes. The Balls are able to call Processing::App methods.
|
|
8
8
|
class Ball
|
|
9
|
-
attr_accessor :
|
|
10
|
-
def initialize(r = 0.0,
|
|
11
|
-
@
|
|
9
|
+
attr_accessor :position, :r, :m, :velocity, :current
|
|
10
|
+
def initialize(r = 0.0, velocity = nil, position = Vec2D.new)
|
|
11
|
+
@position, @velocity, @r = position, velocity, r
|
|
12
12
|
@m = r * 0.1
|
|
13
|
-
@vec = vec
|
|
14
13
|
end
|
|
15
14
|
|
|
16
15
|
def move
|
|
17
|
-
@
|
|
18
|
-
@y += @vec.y
|
|
16
|
+
@position += velocity
|
|
19
17
|
end
|
|
20
18
|
|
|
21
19
|
def draw
|
|
22
|
-
|
|
23
|
-
ellipse
|
|
24
|
-
@
|
|
20
|
+
d = r * 2
|
|
21
|
+
ellipse position.x, position.y, d, d
|
|
22
|
+
@current = position.copy
|
|
25
23
|
end
|
|
26
24
|
|
|
27
25
|
def erase
|
|
28
|
-
|
|
29
|
-
rect
|
|
26
|
+
d = r * 2
|
|
27
|
+
rect current.x, current.y, d, d
|
|
30
28
|
end
|
|
31
29
|
end
|
|
32
30
|
|
|
31
|
+
attr_reader :balls
|
|
32
|
+
|
|
33
33
|
|
|
34
34
|
def setup
|
|
35
35
|
size 400, 400
|
|
@@ -37,8 +37,8 @@ def setup
|
|
|
37
37
|
frame_rate 30
|
|
38
38
|
rect_mode RADIUS
|
|
39
39
|
@balls = []
|
|
40
|
-
5.times {
|
|
41
|
-
2.times {
|
|
40
|
+
5.times { balls << Ball.new(10, Vec2D.new(2.15, -1.35), empty_space(15)) }
|
|
41
|
+
2.times { balls << Ball.new(40, Vec2D.new(-1.65, 0.42), empty_space(45)) }
|
|
42
42
|
@frame_time = nil
|
|
43
43
|
@frame_count = 0
|
|
44
44
|
end
|
|
@@ -55,12 +55,12 @@ def draw
|
|
|
55
55
|
background 51
|
|
56
56
|
else
|
|
57
57
|
fill 51
|
|
58
|
-
|
|
58
|
+
balls.each { |ball| ball.erase }
|
|
59
59
|
end
|
|
60
60
|
|
|
61
61
|
# move the balls
|
|
62
62
|
fill 240
|
|
63
|
-
|
|
63
|
+
balls.each do |ball|
|
|
64
64
|
ball.move
|
|
65
65
|
ball.draw
|
|
66
66
|
check_boundary_collision ball
|
|
@@ -70,21 +70,17 @@ end
|
|
|
70
70
|
|
|
71
71
|
|
|
72
72
|
def empty_space(r)
|
|
73
|
-
|
|
74
|
-
while !
|
|
75
|
-
|
|
76
|
-
y = rand(height - 3 * r) + r
|
|
73
|
+
pos = nil
|
|
74
|
+
while !pos || !empty_space?(pos, r) do
|
|
75
|
+
pos = Vec2D.new(rand(r .. width - r), rand(r .. height - r))
|
|
77
76
|
end
|
|
78
|
-
return
|
|
77
|
+
return pos
|
|
79
78
|
end
|
|
80
79
|
|
|
81
80
|
|
|
82
|
-
def empty_space?(
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
vy = y - ball.y
|
|
86
|
-
mag = sqrt(vx * vx + vy * vy)
|
|
87
|
-
return false if mag < r + ball.r
|
|
81
|
+
def empty_space?(position, r)
|
|
82
|
+
balls.each do |ball|
|
|
83
|
+
return false if position.dist(ball.position) < r + ball.r
|
|
88
84
|
end
|
|
89
85
|
return true
|
|
90
86
|
end
|
|
@@ -92,14 +88,14 @@ end
|
|
|
92
88
|
|
|
93
89
|
def check_object_collisions
|
|
94
90
|
|
|
95
|
-
(0...(
|
|
96
|
-
((ia+1)...(
|
|
91
|
+
(0...(balls.length)).each do |ia|
|
|
92
|
+
((ia+1)...(balls.length)).each do |ib|
|
|
97
93
|
|
|
98
|
-
ba =
|
|
99
|
-
bb =
|
|
94
|
+
ba = balls[ia]
|
|
95
|
+
bb = balls[ib]
|
|
100
96
|
|
|
101
97
|
# get distances between the balls components
|
|
102
|
-
bVect =
|
|
98
|
+
bVect = bb.position - ba.position
|
|
103
99
|
# calculate magnitude of the vector separating the balls
|
|
104
100
|
bVectMag = bVect.mag
|
|
105
101
|
next if bVectMag >= ba.r + bb.r
|
|
@@ -118,15 +114,15 @@ def check_object_collisions
|
|
|
118
114
|
# bTemp[0].x and bTemp[0].y will initialize
|
|
119
115
|
# automatically to 0.0, which is what you want
|
|
120
116
|
# since bb will rotate around ba
|
|
121
|
-
bTemp[1].x = cosine * bVect.x + sine * bVect.y
|
|
122
|
-
bTemp[1].y = cosine * bVect.y - sine * bVect.x
|
|
117
|
+
bTemp[1].position.x = cosine * bVect.x + sine * bVect.y
|
|
118
|
+
bTemp[1].position.y = cosine * bVect.y - sine * bVect.x
|
|
123
119
|
|
|
124
120
|
# rotate Temporary velocities
|
|
125
121
|
vTemp = [Vec2D.new, Vec2D.new]
|
|
126
|
-
vTemp[0].x = cosine * ba.
|
|
127
|
-
vTemp[0].y = cosine * ba.
|
|
128
|
-
vTemp[1].x = cosine * bb.
|
|
129
|
-
vTemp[1].y = cosine * bb.
|
|
122
|
+
vTemp[0].x = cosine * ba.velocity.x + sine * ba.velocity.y
|
|
123
|
+
vTemp[0].y = cosine * ba.velocity.y - sine * ba.velocity.x
|
|
124
|
+
vTemp[1].x = cosine * bb.velocity.x + sine * bb.velocity.y
|
|
125
|
+
vTemp[1].y = cosine * bb.velocity.y - sine * bb.velocity.x
|
|
130
126
|
|
|
131
127
|
# Now that velocities are rotated, you can use 1D
|
|
132
128
|
# conservation of momentum equations to calculate
|
|
@@ -144,22 +140,21 @@ def check_object_collisions
|
|
|
144
140
|
# in the opposite direction
|
|
145
141
|
# rotate balls
|
|
146
142
|
bFinal = [Ball.new, Ball.new]
|
|
147
|
-
bFinal[0].x = cosine * bTemp[0].x - sine * bTemp[0].y
|
|
148
|
-
bFinal[0].y = cosine * bTemp[0].y + sine * bTemp[0].x
|
|
149
|
-
bFinal[1].x = cosine * bTemp[1].x - sine * bTemp[1].y
|
|
150
|
-
bFinal[1].y = cosine * bTemp[1].y + sine * bTemp[1].x
|
|
143
|
+
bFinal[0].position.x = cosine * bTemp[0].position.x - sine * bTemp[0].position.y
|
|
144
|
+
bFinal[0].position.y = cosine * bTemp[0].position.y + sine * bTemp[0].position.x
|
|
145
|
+
bFinal[1].position.x = cosine * bTemp[1].position.x - sine * bTemp[1].position.y
|
|
146
|
+
bFinal[1].position.y = cosine * bTemp[1].position.y + sine * bTemp[1].position.x
|
|
151
147
|
|
|
152
148
|
# update balls to screen position
|
|
153
|
-
bb.
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
ba.y = ba.y + bFinal[0].y
|
|
149
|
+
bb.position = ba.position + bFinal[1].position
|
|
150
|
+
ba.position = ba.position + bFinal[0].position
|
|
151
|
+
|
|
157
152
|
|
|
158
153
|
# update velocities
|
|
159
|
-
ba.
|
|
160
|
-
ba.
|
|
161
|
-
bb.
|
|
162
|
-
bb.
|
|
154
|
+
ba.velocity.x = cosine * vFinal[0].x - sine * vFinal[0].y
|
|
155
|
+
ba.velocity.y = cosine * vFinal[0].y + sine * vFinal[0].x
|
|
156
|
+
bb.velocity.x = cosine * vFinal[1].x - sine * vFinal[1].y
|
|
157
|
+
bb.velocity.y = cosine * vFinal[1].y + sine * vFinal[1].x
|
|
163
158
|
end
|
|
164
159
|
end
|
|
165
160
|
|
|
@@ -167,8 +162,8 @@ end
|
|
|
167
162
|
|
|
168
163
|
# reverse ball velocity if at sketch boundary
|
|
169
164
|
def check_boundary_collision(ball)
|
|
170
|
-
ball.
|
|
171
|
-
ball.
|
|
165
|
+
ball.velocity.x *= -1 unless (ball.r .. width - ball.r).include? ball.position.x
|
|
166
|
+
ball.velocity.y *= -1 unless (ball.r .. height - ball.r).include? ball.position.y
|
|
172
167
|
end
|
|
173
168
|
|
|
174
169
|
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
@@ -15,11 +15,8 @@ attr_reader :flock
|
|
|
15
15
|
|
|
16
16
|
def setup
|
|
17
17
|
size(640, 360)
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
150.times do
|
|
21
|
-
flock << Boid.new(width/2, height/2)
|
|
22
|
-
end
|
|
18
|
+
# Add an initial set of boids into the system at the center
|
|
19
|
+
@flock = Flock.new(150, Vec2D.new(width / 2.0, height / 2.0))
|
|
23
20
|
end
|
|
24
21
|
|
|
25
22
|
def draw
|
|
@@ -29,5 +26,5 @@ end
|
|
|
29
26
|
|
|
30
27
|
# Add a new boid into the System
|
|
31
28
|
def mouse_pressed
|
|
32
|
-
flock << Boid.new(mouse_x, mouse_y)
|
|
29
|
+
flock << Boid.new(Vec2D.new(mouse_x, mouse_y))
|
|
33
30
|
end
|
|
@@ -1,24 +1,13 @@
|
|
|
1
1
|
# The Flock (a list of Boid objects)
|
|
2
|
+
require 'forwardable'
|
|
2
3
|
|
|
3
4
|
class Flock
|
|
4
|
-
extend
|
|
5
|
+
extend Forwardable
|
|
6
|
+
def_delegators(:@boids, :each, :<<, :reject)
|
|
7
|
+
include Enumerable
|
|
5
8
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
def initialize
|
|
9
|
-
@boids = []
|
|
10
|
-
end
|
|
11
|
-
|
|
12
|
-
def each &block
|
|
13
|
-
boids.each &block
|
|
14
|
-
end
|
|
15
|
-
|
|
16
|
-
def reject &block
|
|
17
|
-
boids &block
|
|
18
|
-
end
|
|
19
|
-
|
|
20
|
-
def << obj
|
|
21
|
-
boids << obj
|
|
9
|
+
def initialize(size, position)
|
|
10
|
+
@boids = (0 .. size).map{ Boid.new(position) }
|
|
22
11
|
end
|
|
23
12
|
|
|
24
13
|
def run
|
|
@@ -35,14 +24,16 @@ class Boid
|
|
|
35
24
|
include Processing::Proxy
|
|
36
25
|
import 'vecmath'
|
|
37
26
|
attr_reader :location, :velocity, :acceleration, :r, :maxforce, :maxspeed
|
|
38
|
-
|
|
39
|
-
def initialize(
|
|
27
|
+
attr_reader :width, :height
|
|
28
|
+
def initialize(loc)
|
|
40
29
|
@acceleration = Vec2D.new
|
|
41
30
|
@velocity = Vec2D.new(rand(-1.0 .. 1), rand(-1.0 .. 1))
|
|
42
|
-
@location =
|
|
31
|
+
@location = loc
|
|
43
32
|
@r = 2.0
|
|
44
33
|
@maxspeed = 2
|
|
45
34
|
@maxforce = 0.03
|
|
35
|
+
@width = $app.width
|
|
36
|
+
@height = $app.height
|
|
46
37
|
end
|
|
47
38
|
|
|
48
39
|
def run(boids)
|
|
@@ -58,7 +49,7 @@ class Boid
|
|
|
58
49
|
end
|
|
59
50
|
|
|
60
51
|
# We accumulate a new acceleration each time based on three rules
|
|
61
|
-
def flock
|
|
52
|
+
def flock(boids)
|
|
62
53
|
sep = separate(boids) # Separation
|
|
63
54
|
ali = align(boids) # Alignment
|
|
64
55
|
coh = cohesion(boids) # Cohesion
|
|
@@ -76,8 +67,8 @@ class Boid
|
|
|
76
67
|
def update
|
|
77
68
|
# Update velocity
|
|
78
69
|
@velocity += acceleration
|
|
79
|
-
# Limit speed
|
|
80
|
-
velocity.set_mag(maxspeed) {velocity.
|
|
70
|
+
# Limit speed
|
|
71
|
+
velocity.set_mag(maxspeed) { velocity.mag > maxspeed }
|
|
81
72
|
@location += velocity
|
|
82
73
|
# Reset accelertion to 0 each cycle
|
|
83
74
|
@acceleration *= 0
|
|
@@ -93,86 +84,79 @@ class Boid
|
|
|
93
84
|
# Steering = Desired minus Velocity
|
|
94
85
|
steer = desired - velocity
|
|
95
86
|
# Limit to maximum steering force
|
|
96
|
-
steer.set_mag(maxforce) {steer.
|
|
87
|
+
steer.set_mag(maxforce) { steer.mag > maxforce }
|
|
97
88
|
return steer
|
|
98
89
|
end
|
|
99
90
|
|
|
100
91
|
def render
|
|
101
92
|
# Draw a triangle rotated in the direction of velocity
|
|
102
|
-
theta = velocity.heading + Math::PI/2
|
|
103
|
-
fill(200,100)
|
|
93
|
+
theta = velocity.heading + Math::PI/2
|
|
94
|
+
fill(200, 100)
|
|
104
95
|
stroke(255)
|
|
105
96
|
push_matrix
|
|
106
97
|
translate(location.x,location.y)
|
|
107
98
|
rotate(theta)
|
|
108
99
|
begin_shape(TRIANGLES)
|
|
109
|
-
vertex(0, -r*2)
|
|
110
|
-
vertex(-r, r*2)
|
|
111
|
-
vertex(r, r*2)
|
|
100
|
+
vertex(0, -r * 2)
|
|
101
|
+
vertex(-r, r * 2)
|
|
102
|
+
vertex(r, r * 2)
|
|
112
103
|
end_shape
|
|
113
104
|
pop_matrix
|
|
114
105
|
end
|
|
115
106
|
|
|
116
107
|
# Wraparound
|
|
117
108
|
def borders
|
|
118
|
-
if
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
if
|
|
122
|
-
location.y = $app.height+r
|
|
123
|
-
end
|
|
124
|
-
if (location.x > $app.width+r)
|
|
125
|
-
location.x = -r
|
|
126
|
-
end
|
|
127
|
-
if (location.y > $app.height+r)
|
|
128
|
-
location.y = -r
|
|
129
|
-
end
|
|
109
|
+
location.x = width + r if location.x < -r
|
|
110
|
+
location.y = height + r if location.y < -r
|
|
111
|
+
location.x = -r if location.x > width + r
|
|
112
|
+
location.y = -r if location.y > height + r
|
|
130
113
|
end
|
|
131
114
|
|
|
132
115
|
# Separation
|
|
133
116
|
# Method checks for nearby boids and steers away
|
|
134
|
-
def separate
|
|
117
|
+
def separate(boids)
|
|
135
118
|
desiredseparation = 25.0
|
|
136
119
|
steer = Vec2D.new
|
|
137
120
|
count = 0
|
|
138
121
|
# For every other bird in the system, check if it's too close
|
|
139
|
-
boids.reject{|bd| bd.equal? self}.each do |other|
|
|
140
|
-
d =
|
|
122
|
+
boids.reject{ |bd| bd.equal? self }.each do |other|
|
|
123
|
+
d = location.dist(other.location)
|
|
141
124
|
# If the distance is greater than 0 and less than an arbitrary amount
|
|
142
|
-
if (
|
|
125
|
+
if (0.0001 .. desiredseparation).include? d
|
|
143
126
|
# Calculate vector pointing away from neighbor
|
|
144
127
|
diff = location - other.location
|
|
145
128
|
diff.normalize!
|
|
146
|
-
diff /= d
|
|
129
|
+
diff /= d # Weight by distance
|
|
147
130
|
steer += diff
|
|
148
131
|
count += 1 # Keep track of how many
|
|
149
132
|
end
|
|
150
133
|
end
|
|
151
134
|
# Average -- divide by how many
|
|
152
|
-
if
|
|
135
|
+
if count > 0
|
|
153
136
|
steer /= count.to_f
|
|
154
137
|
end
|
|
155
138
|
|
|
156
139
|
# As long as the vector is greater than 0
|
|
157
|
-
if
|
|
140
|
+
if steer.mag > 0
|
|
158
141
|
# Implement Reynolds: Steering = Desired - Velocity
|
|
159
142
|
steer.normalize!
|
|
160
143
|
steer *= maxspeed
|
|
161
144
|
steer -= velocity
|
|
162
|
-
steer.set_mag(maxforce){steer.
|
|
145
|
+
steer.set_mag(maxforce) { steer.mag > maxforce }
|
|
163
146
|
end
|
|
164
147
|
return steer
|
|
165
148
|
end
|
|
166
149
|
|
|
167
150
|
# Alignment
|
|
168
151
|
# For every other nearby boid in the system, calculate the average velocity
|
|
169
|
-
def align
|
|
152
|
+
def align(boids)
|
|
170
153
|
neighbordist = 50
|
|
171
154
|
sum = Vec2D.new
|
|
155
|
+
steer = Vec2D.new
|
|
172
156
|
count = 0
|
|
173
|
-
boids.reject{|bd| bd.equal? self}.each do |other|
|
|
174
|
-
d =
|
|
175
|
-
if (
|
|
157
|
+
boids.reject{ |bd| bd.equal? self }.each do |other|
|
|
158
|
+
d = location.dist(other.location)
|
|
159
|
+
if (0 .. neighbordist).include? d
|
|
176
160
|
sum += other.velocity
|
|
177
161
|
count += 1
|
|
178
162
|
end
|
|
@@ -182,30 +166,26 @@ class Boid
|
|
|
182
166
|
sum.normalize!
|
|
183
167
|
sum *= maxspeed
|
|
184
168
|
steer = sum - velocity
|
|
185
|
-
steer.set_mag(maxforce){steer.
|
|
186
|
-
return steer
|
|
187
|
-
else
|
|
188
|
-
return Vec2D.new
|
|
169
|
+
steer.set_mag(maxforce) { steer.mag > maxforce }
|
|
189
170
|
end
|
|
171
|
+
return steer
|
|
190
172
|
end
|
|
191
173
|
|
|
192
174
|
# Cohesion
|
|
193
|
-
# For the average location (i.e. center) of all other nearby boids, calculate
|
|
194
|
-
|
|
175
|
+
# For the average location (i.e. center) of all other nearby boids, calculate
|
|
176
|
+
# steering vector towards that location
|
|
177
|
+
def cohesion(boids)
|
|
195
178
|
neighbordist = 50
|
|
196
179
|
sum = Vec2D.new # Start with empty vector to accumulate all locations
|
|
197
180
|
count = 0
|
|
198
|
-
boids.reject{|bd| bd.equal? self}.each do |other|
|
|
199
|
-
d =
|
|
200
|
-
if (
|
|
181
|
+
boids.reject { |bd| bd.equal? self }.each do |other|
|
|
182
|
+
d = location.dist(other.location)
|
|
183
|
+
if (0.0001 .. neighbordist).include? d
|
|
201
184
|
sum += other.location # Add location
|
|
202
185
|
count += 1
|
|
203
186
|
end
|
|
204
187
|
end
|
|
205
|
-
sum /= count unless
|
|
206
|
-
|
|
188
|
+
sum /= count unless count == 0 # avoid div by zero
|
|
189
|
+
(count > 0) ? seek(sum) : Vec2D.new
|
|
207
190
|
end
|
|
208
191
|
end
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|