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
@@ -4,156 +4,130 @@
|
|
4
4
|
|
5
5
|
# Each burst is one instance of a particle system with Particles and
|
6
6
|
# CrazyParticles (a subclass of Particle).
|
7
|
-
|
7
|
+
require 'forwardable'
|
8
8
|
load_library :vecmath
|
9
9
|
|
10
10
|
module Runnable
|
11
11
|
def run
|
12
|
-
|
13
|
-
|
12
|
+
reject! { |item| item.dead? }
|
13
|
+
each { |item| item.run }
|
14
14
|
end
|
15
15
|
end
|
16
16
|
|
17
|
+
class ParticleSystem
|
18
|
+
include Enumerable, Runnable
|
19
|
+
extend Forwardable
|
20
|
+
def_delegators(:@particles, :reject!, :<<, :each, :empty?)
|
21
|
+
def_delegator(:@particles, :empty?, :dead?)
|
17
22
|
|
18
|
-
class ParticleSystem
|
19
|
-
extend Enumerable
|
20
|
-
include Runnable
|
21
|
-
|
22
|
-
attr_reader :particle_systems
|
23
|
-
|
24
23
|
def initialize(number, origin)
|
25
|
-
@
|
24
|
+
@particles = []
|
26
25
|
@origin = origin
|
27
26
|
kind = rand < 0.5 ? Sketch::Particle : Sketch::CrazyParticle
|
28
27
|
number.times { self << kind.new(origin) }
|
29
28
|
end
|
30
|
-
|
31
|
-
def each &block
|
32
|
-
particle_systems.each &block
|
33
|
-
end
|
34
|
-
|
35
|
-
def << obj
|
36
|
-
particle_systems << obj
|
37
|
-
end
|
38
|
-
|
39
|
-
def reject! &block
|
40
|
-
particle_systems.reject! &block
|
41
|
-
end
|
42
|
-
|
43
|
-
def dead?
|
44
|
-
particle_systems.empty?
|
45
|
-
end
|
46
29
|
end
|
47
30
|
|
31
|
+
attr_reader :particle_systems
|
48
32
|
|
49
33
|
def setup
|
50
|
-
size 640, 580
|
34
|
+
size 640, 580
|
51
35
|
color_mode(RGB, 255, 255, 255, 100)
|
52
|
-
ellipse_mode(CENTER)
|
53
|
-
@particle_systems = ParticleSystem.new(rand(
|
36
|
+
ellipse_mode(CENTER)
|
37
|
+
@particle_systems = ParticleSystem.new(rand(5..26), Vec2D.new(width / 2, height / 2))
|
54
38
|
end
|
55
39
|
|
56
40
|
def draw
|
57
41
|
background 0
|
58
|
-
|
42
|
+
particle_systems.run
|
59
43
|
end
|
60
44
|
|
61
45
|
def mouse_pressed
|
62
|
-
origin = rand(
|
46
|
+
origin = rand(5..26)
|
63
47
|
vector = Vec2D.new(mouse_x, mouse_y)
|
64
|
-
|
48
|
+
particle_systems << ParticleSystem.new(origin, vector)
|
65
49
|
end
|
66
50
|
|
67
51
|
|
68
52
|
class Particle
|
53
|
+
attr_reader :acceleration, :velocity
|
54
|
+
|
69
55
|
def initialize(origin)
|
70
56
|
@origin = origin
|
71
|
-
@velocity = Vec2D.new(rand(-1.0
|
57
|
+
@velocity = Vec2D.new(rand(-1.0..1), rand(-2.0..0))
|
72
58
|
@acceleration = Vec2D.new(0, 0.05)
|
73
59
|
@radius = 10
|
74
60
|
@lifespan = 100
|
75
61
|
end
|
76
|
-
|
62
|
+
|
77
63
|
def run
|
78
64
|
update
|
79
65
|
grow
|
80
66
|
render
|
81
67
|
render_velocity_vector
|
82
68
|
end
|
83
|
-
|
69
|
+
|
84
70
|
def update
|
85
|
-
@velocity +=
|
86
|
-
@origin +=
|
71
|
+
@velocity += acceleration
|
72
|
+
@origin += velocity
|
87
73
|
end
|
88
|
-
|
74
|
+
|
89
75
|
def grow
|
90
76
|
@lifespan -= 1
|
91
77
|
end
|
92
|
-
|
78
|
+
|
93
79
|
def dead?
|
94
80
|
@lifespan <= 0
|
95
81
|
end
|
96
|
-
|
82
|
+
|
97
83
|
def render
|
98
84
|
stroke(255, @lifespan)
|
99
85
|
fill(100, @lifespan)
|
100
86
|
ellipse(@origin.x, @origin.y, @radius, @radius)
|
101
87
|
end
|
102
|
-
|
88
|
+
|
103
89
|
def render_velocity_vector
|
104
90
|
scale = 10
|
105
91
|
arrow_size = 4
|
106
|
-
|
107
92
|
push_matrix
|
108
|
-
|
109
93
|
translate(@origin.x, @origin.y)
|
110
|
-
rotate(
|
111
|
-
|
112
|
-
length = @velocity.mag * scale
|
113
|
-
|
94
|
+
rotate(velocity.heading)
|
95
|
+
length = velocity.mag * scale
|
114
96
|
line 0, 0, length, 0
|
115
97
|
line length, 0, length - arrow_size, arrow_size / 2
|
116
98
|
line length, 0, length - arrow_size, -arrow_size / 2
|
117
|
-
|
118
99
|
pop_matrix
|
119
100
|
end
|
120
101
|
end
|
121
102
|
|
122
|
-
|
123
103
|
class CrazyParticle < Particle
|
124
104
|
def initialize(origin)
|
125
105
|
super
|
126
106
|
@theta = 0
|
127
107
|
end
|
128
|
-
|
108
|
+
|
129
109
|
def run
|
130
110
|
update
|
131
111
|
grow
|
132
112
|
render
|
133
113
|
render_rotation_line
|
134
114
|
end
|
135
|
-
|
115
|
+
|
136
116
|
def update
|
137
117
|
super
|
138
|
-
@theta +=
|
118
|
+
@theta += velocity.x * velocity.mag / 10
|
139
119
|
end
|
140
|
-
|
120
|
+
|
141
121
|
def grow
|
142
122
|
@lifespan -= 0.8
|
143
123
|
end
|
144
|
-
|
124
|
+
|
145
125
|
def render_rotation_line
|
146
126
|
push_matrix
|
147
|
-
|
148
127
|
translate(@origin.x, @origin.y)
|
149
128
|
rotate(@theta)
|
150
|
-
|
151
129
|
stroke(255, @lifespan)
|
152
|
-
|
153
130
|
line(0, 0, 25, 0)
|
154
|
-
|
155
131
|
pop_matrix
|
156
132
|
end
|
157
133
|
end
|
158
|
-
|
159
|
-
|
@@ -3,7 +3,7 @@
|
|
3
3
|
# Particles are generated each cycle, fall with gravity and fade out over
|
4
4
|
# time. A ParticleSystem (Array) object manages a variable size list of
|
5
5
|
# particles.
|
6
|
-
|
6
|
+
require 'forwardable'
|
7
7
|
load_library :vecmath
|
8
8
|
|
9
9
|
attr_reader :ps
|
@@ -21,50 +21,39 @@ end
|
|
21
21
|
|
22
22
|
module Runnable
|
23
23
|
def run
|
24
|
-
|
25
|
-
|
24
|
+
reject! { |item| item.lifespan <= 0 }
|
25
|
+
each { |item| item.run }
|
26
26
|
end
|
27
27
|
end
|
28
28
|
|
29
|
-
class ParticleSystem
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
29
|
+
class ParticleSystem
|
30
|
+
include Enumerable, Runnable
|
31
|
+
extend Forwardable
|
32
|
+
def_delegators(:@particles, :reject!, :<<, :each, :empty?)
|
33
|
+
def_delegator(:@particles, :empty?, :dead?)
|
34
|
+
|
35
|
+
attr_reader :origin
|
34
36
|
|
35
37
|
def initialize(loc)
|
36
|
-
@
|
37
|
-
@origin = loc
|
38
|
+
@particles = []
|
39
|
+
@origin = loc
|
38
40
|
end
|
39
|
-
|
40
|
-
def each &block
|
41
|
-
particle_system.each &block
|
42
|
-
end
|
43
|
-
|
41
|
+
|
44
42
|
def add_particle
|
45
|
-
|
43
|
+
self << Particle.new(origin)
|
46
44
|
end
|
47
|
-
|
48
|
-
def reject! &block
|
49
|
-
particle_system.reject! &block
|
50
|
-
end
|
51
|
-
|
52
|
-
def dead?
|
53
|
-
particle_systems.empty?
|
54
|
-
end
|
55
|
-
|
56
45
|
end
|
57
46
|
|
58
47
|
# A simple Particle class
|
59
48
|
|
60
|
-
class Particle
|
61
|
-
include Processing::Proxy
|
62
|
-
|
49
|
+
class Particle
|
50
|
+
include Processing::Proxy
|
51
|
+
|
63
52
|
attr_reader :loc, :vel, :acc, :lifespan
|
64
|
-
def initialize(l)
|
53
|
+
def initialize(l)
|
65
54
|
@acc = Vec2D.new(0, 0.05)
|
66
|
-
@vel = Vec2D.new(rand(-1.0
|
67
|
-
@loc = l.
|
55
|
+
@vel = Vec2D.new(rand(-1.0..1), rand(-2.0..0))
|
56
|
+
@loc = l.copy
|
68
57
|
@lifespan = 255.0
|
69
58
|
end
|
70
59
|
|
@@ -86,5 +75,4 @@ class Particle
|
|
86
75
|
fill(255,lifespan)
|
87
76
|
ellipse(loc.x, loc.y, 8, 8)
|
88
77
|
end
|
89
|
-
|
90
78
|
end
|
@@ -10,128 +10,116 @@ attr_reader :ps, :img, :wind
|
|
10
10
|
def setup
|
11
11
|
size(640,360)
|
12
12
|
@img = load_image('texture.png')
|
13
|
-
@ps = ParticleSystem.new(0, Vec2D.new(width/2, height -
|
13
|
+
@ps = ParticleSystem.new(0, Vec2D.new(width / 2, height - 75), img)
|
14
14
|
end
|
15
15
|
|
16
16
|
def draw
|
17
|
-
background(0)
|
18
|
-
# Calculate a
|
19
|
-
dx =
|
20
|
-
|
17
|
+
background(0)
|
18
|
+
# Calculate a "wind" force based on mouse horizontal position
|
19
|
+
dx = map1d(mouse_x, (0..width), (-0.2..0.2))
|
20
|
+
wind = Vec2D.new(dx, 0)
|
21
21
|
ps.apply_force(wind)
|
22
22
|
ps.run
|
23
|
-
2.times
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
# Draw a horizontal arrow representing the wind force
|
28
|
-
draw_vector(wind, Vec2D.new(width / 2, 50, 0), 500)
|
29
|
-
|
23
|
+
2.times { ps.add_particle }
|
24
|
+
# Draw an arrow representing the wind force
|
25
|
+
draw_vector(wind, Vec2D.new(width / 2, 50), 500)
|
30
26
|
end
|
31
27
|
|
32
28
|
# Renders a vector object 'v' as an arrow and a location 'loc'
|
33
29
|
def draw_vector(v, loc, scayl)
|
34
30
|
push_matrix
|
31
|
+
arrowsize = 4
|
35
32
|
# Translate to location to render vector
|
36
|
-
translate(loc.x, loc.y)
|
33
|
+
translate(loc.x, loc.y)
|
34
|
+
stroke(255)
|
35
|
+
# Call vector heading function to get direction (note that pointing up is a heading of 0) and rotate
|
37
36
|
rotate(v.heading)
|
38
|
-
# Calculate length of vector & scale
|
37
|
+
# Calculate length of vector & scale it to be bigger or smaller if necessary
|
39
38
|
len = v.mag * scayl
|
40
|
-
# Draw three lines to make an arrow
|
41
|
-
arrowsize = 4
|
42
|
-
stroke(255)
|
43
|
-
stroke_weight 2
|
39
|
+
# Draw three lines to make an arrow (draw pointing up since we've rotate to the proper direction)
|
44
40
|
line(0, 0, len, 0)
|
45
|
-
|
46
|
-
line(len, 0, len - arrowsize,
|
47
|
-
line(len, 0, len - arrowsize, -arrowsize/2)
|
41
|
+
line(len, 0, len - arrowsize, arrowsize / 2)
|
42
|
+
line(len, 0, len - arrowsize, -arrowsize / 2)
|
48
43
|
pop_matrix
|
49
44
|
end
|
50
45
|
|
51
46
|
module Runnable
|
52
47
|
def run
|
53
|
-
|
54
|
-
|
48
|
+
reject! { |item| item.lifespan <= 0 }
|
49
|
+
each { |item| item.run }
|
55
50
|
end
|
56
51
|
end
|
57
52
|
|
53
|
+
require 'forwardable'
|
54
|
+
|
55
|
+
class ParticleSystem
|
56
|
+
include Enumerable, Runnable
|
57
|
+
extend Forwardable
|
58
|
+
def_delegators(:@particles, :reject!, :<<, :each)
|
58
59
|
|
59
|
-
class ParticleSystem
|
60
|
-
extend Enumerable
|
61
|
-
include Runnable
|
62
|
-
|
63
60
|
attr_reader :particles, :origin, :image, :generator
|
64
|
-
|
61
|
+
|
65
62
|
def initialize(num, location, image)
|
66
63
|
@particles = []
|
67
|
-
@origin = location.
|
68
|
-
@image = image
|
64
|
+
@origin = location.copy
|
65
|
+
@image = image
|
69
66
|
(0 ... num).each do
|
70
67
|
particles << Particle.new(origin, image)
|
71
68
|
end
|
72
69
|
end
|
73
|
-
|
74
|
-
def each &block
|
75
|
-
particles.each &block
|
76
|
-
end
|
77
70
|
|
78
|
-
def reject! &block
|
79
|
-
particles.reject! &block
|
80
|
-
end
|
81
|
-
|
82
71
|
def add_particle(p = Particle.new(origin, image))
|
83
|
-
|
72
|
+
self << p
|
84
73
|
end
|
85
|
-
|
74
|
+
|
86
75
|
# Method to add a force vector to all particles currently in the system
|
87
|
-
def apply_force(dir)
|
88
|
-
|
76
|
+
def apply_force(dir)
|
77
|
+
each do |p|
|
89
78
|
p.apply_force(dir)
|
90
|
-
end
|
91
|
-
end
|
79
|
+
end
|
80
|
+
end
|
92
81
|
end
|
93
82
|
|
94
83
|
# A simple Particle class
|
95
84
|
|
96
|
-
class Particle
|
97
|
-
include Processing::Proxy
|
98
|
-
|
85
|
+
class Particle
|
86
|
+
include Processing::Proxy
|
87
|
+
|
99
88
|
attr_reader :loc, :acc, :vel, :lifespan, :img, :generator
|
100
|
-
|
101
|
-
def initialize(l, img_)
|
89
|
+
|
90
|
+
def initialize(l, img_)
|
102
91
|
@acc = Vec2D.new(0, 0)
|
103
92
|
vx = random_gaussian * 0.3
|
104
93
|
vy = random_gaussian * 0.3 - 1.0
|
105
94
|
@vel = Vec2D.new(vx, vy)
|
106
|
-
@loc = l.
|
95
|
+
@loc = l.copy
|
107
96
|
@lifespan = 100.0
|
108
97
|
@img = img_
|
109
98
|
end
|
110
|
-
|
99
|
+
|
111
100
|
def run
|
112
101
|
update
|
113
102
|
render
|
114
103
|
end
|
115
|
-
|
104
|
+
|
116
105
|
# Method to update location
|
117
106
|
def update
|
118
107
|
@vel += acc
|
119
108
|
@loc += vel
|
120
109
|
@lifespan -= 1.0
|
121
110
|
end
|
122
|
-
|
111
|
+
|
123
112
|
# Method to display
|
124
|
-
def render
|
113
|
+
def render
|
125
114
|
image_mode(CENTER)
|
126
115
|
tint(255,lifespan)
|
127
116
|
image(img, loc.x, loc.y)
|
128
117
|
end
|
129
|
-
|
118
|
+
|
130
119
|
# Method to add a force vector to all particles currently in the system
|
131
|
-
def apply_force(f)
|
120
|
+
def apply_force(f)
|
132
121
|
@acc += f
|
133
122
|
end
|
134
|
-
|
135
123
|
end
|
136
124
|
|
137
125
|
|
@@ -1,21 +1,19 @@
|
|
1
|
-
|
2
|
-
|
3
1
|
#
|
4
2
|
# Ported from http://www.processing.org/learning/topics/springs.html
|
5
3
|
#
|
6
|
-
# Move the mouse over one of the circles and click to re-position. When you release the mouse, it will
|
4
|
+
# Move the mouse over one of the circles and click to re-position. When you release the mouse, it will
|
7
5
|
# snap back into position. Each circle has a slightly different behavior.
|
8
6
|
#
|
9
7
|
|
10
8
|
|
11
9
|
def setup
|
12
|
-
size
|
10
|
+
size 400, 400
|
13
11
|
no_stroke
|
14
|
-
smooth
|
12
|
+
smooth 4
|
15
13
|
@springs = []
|
16
|
-
@springs << Spring.new(
|
17
|
-
@springs << Spring.new(
|
18
|
-
@springs << Spring.new(
|
14
|
+
@springs << Spring.new(70, 160, 20, 0.98, 8.0, 0.1, @springs, 0)
|
15
|
+
@springs << Spring.new(160, 130, 60, 0.95, 9.0, 0.1, @springs, 1)
|
16
|
+
@springs << Spring.new(80, 70, 120, 0.90, 9.9, 0.1, @springs, 2)
|
19
17
|
end
|
20
18
|
|
21
19
|
def draw
|
@@ -27,20 +25,18 @@ def draw
|
|
27
25
|
end
|
28
26
|
|
29
27
|
def mouse_pressed
|
30
|
-
@springs.each {|x| x.pressed}
|
28
|
+
@springs.each { |x| x.pressed }
|
31
29
|
end
|
32
30
|
|
33
31
|
def mouse_released
|
34
|
-
@springs.each {|x| x.released}
|
32
|
+
@springs.each { |x| x.released }
|
35
33
|
end
|
36
34
|
|
35
|
+
class Spring
|
36
|
+
include Processing::Proxy
|
37
37
|
|
38
|
+
def initialize(x, y, s, d, m, k, others, id)
|
38
39
|
|
39
|
-
class Spring
|
40
|
-
|
41
|
-
def initialize(app, x, y, s, d, m, k, others, id)
|
42
|
-
@app = app
|
43
|
-
|
44
40
|
# Screen values
|
45
41
|
@xpos = @tempxpos = x
|
46
42
|
@ypos = @tempypos = y
|
@@ -48,7 +44,7 @@ class Spring
|
|
48
44
|
@rest_posy = y # Rest position Y
|
49
45
|
@size = s
|
50
46
|
@damp = d # Damping
|
51
|
-
|
47
|
+
|
52
48
|
# Spring simulation constants
|
53
49
|
@mass = m # Mass
|
54
50
|
@kin = k # Spring constant
|
@@ -56,40 +52,40 @@ class Spring
|
|
56
52
|
@me = id # Index of me in @friends
|
57
53
|
@over = false
|
58
54
|
@move = false
|
59
|
-
|
55
|
+
|
60
56
|
# Spring simulation variables
|
61
57
|
@velx = 0.0 # X Velocity
|
62
58
|
@vely = 0.0 # Y Velocity
|
63
59
|
@accel = 0 # Acceleration
|
64
60
|
@force = 0 # Force
|
65
61
|
end
|
66
|
-
|
62
|
+
|
67
63
|
def update
|
68
64
|
if (@move)
|
69
|
-
@rest_posx =
|
70
|
-
@rest_posy =
|
65
|
+
@rest_posx = mouse_x
|
66
|
+
@rest_posy = mouse_y
|
71
67
|
end
|
72
|
-
|
73
|
-
@force = -1 * @kin * (@tempypos - @rest_posy) # f=-ky
|
74
|
-
@accel = @force / @mass # Set the acceleration, f=ma == a=f/m
|
75
|
-
@vely = @damp * (@vely + @accel); # Set the velocity
|
76
|
-
@tempypos = @tempypos + @vely; # Updated position
|
77
|
-
|
78
|
-
@force = -1 * @kin * (@tempxpos - @rest_posx) # f=-ky
|
79
|
-
@accel = @force / @mass; # Set the acceleration, f=ma == a=f/m
|
80
|
-
@velx = @damp * (@velx + @accel); # Set the velocity
|
81
|
-
@tempxpos = @tempxpos + @velx; # Updated position
|
82
|
-
|
68
|
+
|
69
|
+
@force = -1 * @kin * (@tempypos - @rest_posy) # f=-ky
|
70
|
+
@accel = @force / @mass # Set the acceleration, f=ma == a=f/m
|
71
|
+
@vely = @damp * (@vely + @accel); # Set the velocity
|
72
|
+
@tempypos = @tempypos + @vely; # Updated position
|
73
|
+
|
74
|
+
@force = -1 * @kin * (@tempxpos - @rest_posx) # f=-ky
|
75
|
+
@accel = @force / @mass; # Set the acceleration, f=ma == a=f/m
|
76
|
+
@velx = @damp * (@velx + @accel); # Set the velocity
|
77
|
+
@tempxpos = @tempxpos + @velx; # Updated position
|
78
|
+
|
83
79
|
@over = ((over? || @move) && !other_over?)
|
84
80
|
end
|
85
|
-
|
81
|
+
|
86
82
|
# Test to see if mouse is over this spring
|
87
83
|
def over?
|
88
|
-
dis_x = @tempxpos -
|
89
|
-
dis_y = @tempypos -
|
90
|
-
Math::sqrt(
|
84
|
+
dis_x = @tempxpos - mouse_x
|
85
|
+
dis_y = @tempypos - mouse_y
|
86
|
+
Math::sqrt((dis_x * dis_x) + (dis_y * dis_y)) < @size/2
|
91
87
|
end
|
92
|
-
|
88
|
+
|
93
89
|
# Make sure no other springs are active
|
94
90
|
def other_over?
|
95
91
|
@friends.each_with_index do |f, i|
|
@@ -101,24 +97,24 @@ class Spring
|
|
101
97
|
end
|
102
98
|
return false
|
103
99
|
end
|
104
|
-
|
100
|
+
|
105
101
|
def display
|
106
102
|
if (over?)
|
107
|
-
|
103
|
+
fill(153)
|
108
104
|
else
|
109
|
-
|
105
|
+
fill(255)
|
110
106
|
end
|
111
|
-
|
107
|
+
ellipse(@tempxpos, @tempypos, @size, @size)
|
112
108
|
end
|
113
|
-
|
109
|
+
|
114
110
|
def pressed
|
115
111
|
@move = over?
|
116
112
|
end
|
117
|
-
|
113
|
+
|
118
114
|
def released
|
119
|
-
@move = false
|
115
|
+
@move = false
|
120
116
|
@rest_posx = @xpos
|
121
117
|
@rest_posy = @ypos
|
122
118
|
end
|
123
|
-
|
119
|
+
|
124
120
|
end
|