ruby-processing 2.4.4 → 2.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (227) hide show
  1. checksums.yaml +4 -4
  2. data/lib/rpextras.jar +0 -0
  3. data/lib/ruby-processing.rb +8 -6
  4. data/lib/ruby-processing/app.rb +54 -67
  5. data/lib/ruby-processing/config.rb +7 -7
  6. data/lib/ruby-processing/exporters/application_exporter.rb +37 -38
  7. data/lib/ruby-processing/exporters/base_exporter.rb +34 -37
  8. data/lib/ruby-processing/exporters/creator.rb +27 -25
  9. data/lib/ruby-processing/helper_methods.rb +18 -22
  10. data/lib/ruby-processing/helpers/numeric.rb +5 -6
  11. data/lib/ruby-processing/helpers/string.rb +11 -20
  12. data/lib/ruby-processing/library_loader.rb +33 -35
  13. data/lib/ruby-processing/runner.rb +118 -65
  14. data/lib/ruby-processing/runners/base.rb +21 -26
  15. data/lib/ruby-processing/runners/live.rb +3 -4
  16. data/lib/ruby-processing/runners/run.rb +1 -3
  17. data/lib/ruby-processing/runners/watch.rb +8 -8
  18. data/lib/ruby-processing/version.rb +1 -1
  19. data/lib/templates/application/lib/args.txt.erb +1 -1
  20. data/lib/templates/application/run.erb +3 -3
  21. data/library/control_panel/control_panel.rb +29 -29
  22. data/library/fastmath/fastmath.rb +3 -0
  23. data/library/file_chooser/file_chooser.rb +11 -13
  24. data/library/vecmath/vecmath.rb +6 -35
  25. data/samples/Rakefile +3 -1
  26. data/samples/configRP5/configRP5.pde +150 -0
  27. data/samples/contributed/Rakefile +6 -6
  28. data/samples/contributed/animator.rb +6 -6
  29. data/samples/contributed/bezier_playground.rb +29 -68
  30. data/samples/contributed/circle_collision.rb +6 -13
  31. data/samples/contributed/elegant_ball.rb +14 -17
  32. data/samples/contributed/empathy.rb +8 -5
  33. data/samples/contributed/fibonacci_sphere.rb +10 -49
  34. data/samples/contributed/full_screen.rb +1 -1
  35. data/samples/contributed/grapher.rb +10 -9
  36. data/samples/contributed/gravity.rb +11 -16
  37. data/samples/contributed/jwishy.rb +18 -20
  38. data/samples/contributed/quadraticvertex.rb +26 -26
  39. data/samples/external_library/java_processing/custom/landscape.rb +6 -4
  40. data/samples/external_library/java_processing/hemesh/twin_iso.rb +69 -71
  41. data/samples/external_library/java_processing/pbox2d/bumpy_surface_noise.rb +57 -0
  42. data/samples/external_library/java_processing/pbox2d/contact_test.rb +23 -0
  43. data/samples/external_library/java_processing/pbox2d/data/java_args.txt +2 -0
  44. data/samples/external_library/java_processing/pbox2d/library/custom_shape/custom_shape.rb +158 -0
  45. data/samples/external_library/java_processing/pbox2d/library/particle_system/particle_system.rb +212 -0
  46. data/samples/external_library/java_processing/pbox2d/library/surface/surface.rb +154 -0
  47. data/samples/external_library/java_processing/pbox2d/liquidy.rb +58 -0
  48. data/samples/external_library/java_processing/pbox2d/polygons.rb +58 -0
  49. data/samples/external_library/java_processing/toxiclibs/library/vbo/vbo.rb +52 -0
  50. data/samples/external_library/java_processing/toxiclibs/model_align.rb +9 -16
  51. data/samples/processing_app/basics/color/color_wheel.rb +1 -1
  52. data/samples/processing_app/basics/form/regular_polygon.rb +2 -2
  53. data/samples/processing_app/basics/form/rgb_cube.rb +2 -2
  54. data/samples/processing_app/basics/form/shape_primitives.rb +1 -1
  55. data/samples/processing_app/basics/form/star.rb +2 -2
  56. data/samples/processing_app/basics/input/clock.rb +21 -16
  57. data/samples/processing_app/basics/input/storing_input.rb +6 -6
  58. data/samples/processing_app/basics/math/additive_wave.rb +1 -1
  59. data/samples/processing_app/basics/math/sine.rb +2 -2
  60. data/samples/processing_app/basics/math/sine_cosine.rb +1 -1
  61. data/samples/processing_app/basics/math/sine_wave.rb +1 -1
  62. data/samples/processing_app/basics/objects/inheritance.rb +2 -0
  63. data/samples/processing_app/basics/objects/inheritance_two.rb +88 -0
  64. data/samples/processing_app/basics/objects/module.rb +89 -0
  65. data/samples/processing_app/{library/vecmath/drawolver.rb → basics/objects/module_two.rb} +1 -1
  66. data/samples/processing_app/basics/objects/multiple_constructors.rb +5 -0
  67. data/samples/processing_app/basics/objects/struct.rb +38 -0
  68. data/samples/processing_app/basics/structure/recursion2.rb +1 -1
  69. data/samples/processing_app/basics/transform/birds.rb +7 -10
  70. data/samples/processing_app/basics/transform/library/bird/bird.rb +3 -3
  71. data/samples/processing_app/basics/transform/rotate.rb +1 -1
  72. data/samples/processing_app/basics/transform/rotate1.rb +1 -1
  73. data/samples/processing_app/basics/transform/rotate_x_y.rb +1 -1
  74. data/samples/processing_app/demos/graphics/bezier_patch.rb +1 -38
  75. data/samples/processing_app/demos/graphics/trefoil.rb +50 -40
  76. data/samples/processing_app/demos/graphics/wiggling.rb +36 -38
  77. data/samples/processing_app/demos/performance/cubic_grid_retained.rb +14 -14
  78. data/samples/processing_app/demos/performance/esfera.rb +27 -39
  79. data/samples/processing_app/demos/performance/text_rendering.rb +1 -1
  80. data/samples/processing_app/library/fastmath/clock.rb +44 -0
  81. data/samples/processing_app/library/pdf/complex_3D.rb +26 -24
  82. data/samples/processing_app/library/vecmath/{Rakefile → arcball/Rakefile} +6 -6
  83. data/samples/processing_app/library/vecmath/arcball/arcball_box.rb +23 -0
  84. data/samples/processing_app/library/vecmath/arcball/arcball_radius.rb +23 -0
  85. data/samples/processing_app/library/vecmath/arcball/arcball_shape.rb +29 -0
  86. data/samples/processing_app/library/vecmath/{README.txt → vec2d/README.txt} +0 -0
  87. data/samples/processing_app/library/vecmath/vec2d/Rakefile +30 -0
  88. data/samples/processing_app/library/vecmath/{acceleration_with_vectors.rb → vec2d/acceleration_with_vectors.rb} +4 -5
  89. data/samples/processing_app/library/vecmath/{bouncing_ball.rb → vec2d/bouncing_ball.rb} +9 -15
  90. data/samples/processing_app/library/vecmath/{circle_collision.rb → vec2d/circle_collision.rb} +46 -51
  91. data/samples/processing_app/library/vecmath/{data → vec2d/data}/java_args.txt +0 -0
  92. data/samples/processing_app/library/vecmath/{data → vec2d/data}/sprite.png +0 -0
  93. data/samples/processing_app/library/vecmath/{data → vec2d/data}/texture.png +0 -0
  94. data/samples/processing_app/library/vecmath/{flocking.rb → vec2d/flocking.rb} +3 -6
  95. data/samples/processing_app/library/vecmath/{library → vec2d/library}/flock/flock.rb +47 -67
  96. data/samples/processing_app/library/vecmath/{library → vec2d/library}/particle/particle_system.rb +5 -5
  97. data/samples/processing_app/library/vecmath/vec2d/library/tile/tile.rb +85 -0
  98. data/samples/processing_app/library/vecmath/{morph.rb → vec2d/morph.rb} +17 -16
  99. data/samples/processing_app/library/vecmath/{multiple_particle_systems.rb → vec2d/multiple_particle_systems.rb} +11 -28
  100. data/samples/processing_app/library/vecmath/{particle_system_pshape.rb → vec2d/particle_system_pshape.rb} +4 -6
  101. data/samples/processing_app/library/vecmath/vec2d/penrose.rb +71 -0
  102. data/samples/processing_app/library/vecmath/{reflection1.rb → vec2d/reflection1.rb} +6 -6
  103. data/samples/processing_app/library/vecmath/{seeking_neural.rb → vec2d/seeking_neural.rb} +12 -16
  104. data/samples/processing_app/library/vecmath/{simple_particle_system.rb → vec2d/simple_particle_system.rb} +8 -12
  105. data/samples/processing_app/library/vecmath/{smoke_particle_system.rb → vec2d/smoke_particle_system.rb} +8 -13
  106. data/samples/processing_app/library/vecmath/{vector_math.rb → vec2d/vector_math.rb} +0 -0
  107. data/samples/processing_app/library/vecmath/{library/wiggler/wiggler.rb → vec2d/wiggle_pshape.rb} +28 -4
  108. data/samples/processing_app/library/vecmath/vec3d/Rakefile +30 -0
  109. data/samples/processing_app/library/vecmath/vec3d/drawolver.rb +86 -0
  110. data/samples/processing_app/library/vecmath/vec3d/frame_of_reference.rb +103 -0
  111. data/samples/processing_app/library/vecmath/{hilbert_fractal.rb → vec3d/hilbert_fractal.rb} +5 -33
  112. data/samples/processing_app/library/vecmath/vec3d/library/geometry/geometry.rb +3 -0
  113. data/samples/processing_app/library/vecmath/vec3d/library/geometry/lib/cylinder.rb +44 -0
  114. data/samples/processing_app/library/vecmath/vec3d/library/geometry/lib/mat4.rb +28 -0
  115. data/samples/processing_app/library/vecmath/vec3d/library/geometry/lib/plane.rb +60 -0
  116. data/samples/processing_app/library/vecmath/{library → vec3d/library}/hilbert/hilbert.rb +14 -17
  117. data/samples/processing_app/library/vecmath/{retained_menger.rb → vec3d/retained_menger.rb} +20 -55
  118. data/samples/processing_app/topics/advanced_data/Rakefile +6 -6
  119. data/samples/processing_app/topics/advanced_data/counting_words.rb +7 -7
  120. data/samples/processing_app/topics/advanced_data/data/data.xml +0 -15
  121. data/samples/processing_app/topics/advanced_data/data/struct_data.yml +5 -0
  122. data/samples/processing_app/topics/advanced_data/library/bubble/bubble.rb +8 -8
  123. data/samples/processing_app/topics/advanced_data/load_save_XML.rb +41 -42
  124. data/samples/processing_app/topics/advanced_data/load_save_json.rb +11 -11
  125. data/samples/processing_app/topics/advanced_data/load_save_struct_yaml.rb +22 -23
  126. data/samples/processing_app/topics/advanced_data/load_save_table.rb +12 -12
  127. data/samples/processing_app/topics/advanced_data/load_save_yaml.rb +10 -12
  128. data/samples/processing_app/topics/advanced_data/regex.rb +5 -6
  129. data/samples/processing_app/topics/advanced_data/threads.rb +27 -27
  130. data/samples/processing_app/topics/advanced_data/threads_two.rb +3 -2
  131. data/samples/processing_app/topics/advanced_data/word_frequency.rb +18 -16
  132. data/samples/processing_app/topics/advanced_data/yahoo_weather.rb +6 -5
  133. data/samples/processing_app/topics/animation/animated_sprite.rb +9 -7
  134. data/samples/processing_app/topics/animation/sequential.rb +19 -19
  135. data/samples/processing_app/topics/cellular_automata/library/ca/ca.rb +1 -1
  136. data/samples/processing_app/topics/cellular_automata/wolfram.rb +1 -1
  137. data/samples/processing_app/topics/create_shapes/begin_end_contour.rb +1 -1
  138. data/samples/processing_app/topics/create_shapes/library/wiggler/wiggler.rb +4 -4
  139. data/samples/processing_app/topics/create_shapes/particle_system_pshape.rb +2 -2
  140. data/samples/processing_app/topics/create_shapes/path_pshape.rb +1 -1
  141. data/samples/processing_app/topics/create_shapes/polygon_pshape_oo3.rb +1 -1
  142. data/samples/processing_app/topics/create_shapes/polygon_shape.rb +3 -3
  143. data/samples/processing_app/topics/file_io/load_file1.rb +1 -1
  144. data/samples/processing_app/topics/file_io/save_frames.rb +5 -5
  145. data/samples/processing_app/topics/file_io/save_one_image.rb +2 -2
  146. data/samples/processing_app/topics/file_io/tile_images.rb +1 -1
  147. data/samples/processing_app/topics/gui/scrollbar.rb +3 -3
  148. data/samples/processing_app/topics/image_processing/blending.rb +13 -13
  149. data/samples/processing_app/topics/image_processing/blur.rb +1 -1
  150. data/samples/processing_app/topics/image_processing/brightness.rb +1 -1
  151. data/samples/processing_app/topics/image_processing/convolution.rb +1 -1
  152. data/samples/processing_app/topics/image_processing/edge_detection.rb +4 -4
  153. data/samples/processing_app/topics/image_processing/explode.rb +1 -1
  154. data/samples/processing_app/topics/image_processing/extrusion.rb +1 -1
  155. data/samples/processing_app/topics/image_processing/histogram.rb +1 -1
  156. data/samples/processing_app/topics/image_processing/linear_image.rb +1 -1
  157. data/samples/processing_app/topics/image_processing/pixel_array.rb +1 -1
  158. data/samples/processing_app/topics/image_processing/zoom.rb +2 -2
  159. data/samples/processing_app/topics/lsystems/Rakefile +6 -6
  160. data/samples/processing_app/topics/lsystems/chequer.rb +4 -10
  161. data/samples/processing_app/topics/lsystems/csplant.rb +25 -25
  162. data/samples/processing_app/topics/lsystems/cstest.rb +9 -4
  163. data/samples/processing_app/topics/lsystems/david_tour.rb +14 -16
  164. data/samples/processing_app/topics/lsystems/koch.rb +8 -18
  165. data/samples/processing_app/topics/lsystems/library/cs_grammar/cs_grammar.rb +18 -24
  166. data/samples/processing_app/topics/lsystems/library/grammar/grammar.rb +7 -9
  167. data/samples/processing_app/topics/lsystems/library/koch/koch_fractal.rb +75 -94
  168. data/samples/processing_app/topics/lsystems/library/stochastic_grammar/stochastic_grammar.rb +11 -13
  169. data/samples/processing_app/topics/lsystems/mpeano.rb +29 -32
  170. data/samples/processing_app/topics/lsystems/peano.rb +19 -21
  171. data/samples/processing_app/topics/lsystems/penrose.rb +31 -37
  172. data/samples/processing_app/topics/lsystems/penrose_snowflake.rb +10 -15
  173. data/samples/processing_app/topics/lsystems/pentagonal.rb +20 -23
  174. data/samples/processing_app/topics/lsystems/snake_kolam.rb +13 -13
  175. data/samples/processing_app/topics/lsystems/stochastic_test.rb +25 -28
  176. data/samples/processing_app/topics/lsystems/three_d_tree.rb +25 -24
  177. data/samples/processing_app/topics/motion/bouncy_bubbles.rb +17 -17
  178. data/samples/processing_app/topics/motion/cubes_in_cube.rb +1 -3
  179. data/samples/processing_app/topics/motion/library/cube/cube.rb +2 -2
  180. data/samples/processing_app/topics/motion/reflection2.rb +1 -1
  181. data/samples/processing_app/topics/shaders/blur_filter.rb +3 -3
  182. data/samples/processing_app/topics/shaders/bw_shader.rb +3 -3
  183. data/samples/processing_app/topics/shaders/conway.rb +5 -5
  184. data/samples/processing_app/topics/shaders/deform.rb +5 -5
  185. data/samples/processing_app/topics/shaders/edge_detect.rb +3 -5
  186. data/samples/processing_app/topics/shaders/glossy_fish_eye.rb +15 -21
  187. data/samples/processing_app/topics/shaders/glsl_heightmap_noise.rb +21 -19
  188. data/samples/processing_app/topics/shaders/image_mask.rb +6 -6
  189. data/samples/processing_app/topics/shaders/landscape.rb +4 -4
  190. data/samples/processing_app/topics/shaders/monjori.rb +15 -15
  191. data/samples/processing_app/topics/shaders/nebula.rb +3 -3
  192. data/samples/processing_app/topics/shaders/sep_blur.rb +15 -15
  193. data/samples/processing_app/topics/shaders/toon_shading.rb +2 -2
  194. data/samples/processing_app/topics/simulate/chain.rb +16 -14
  195. data/samples/processing_app/topics/simulate/smoke_particle_system.rb +3 -3
  196. data/samples/processing_app/topics/vectors/acceleration_with_vectors.rb +3 -2
  197. data/samples/processing_app/topics/vectors/bouncing_ball.rb +1 -0
  198. data/samples/processing_app/topics/vectors/vector_math.rb +2 -1
  199. data/vendors/Rakefile +15 -3
  200. metadata +99 -86
  201. data/.gitignore +0 -13
  202. data/.travis.yml +0 -6
  203. data/.yardopts +0 -5
  204. data/CHANGELOG +0 -316
  205. data/CONTRIBUTING.md +0 -35
  206. data/Gemfile +0 -6
  207. data/LICENSE.md +0 -39
  208. data/README.md +0 -119
  209. data/Rakefile +0 -37
  210. data/bin/install_jruby_complete +0 -13
  211. data/lib/ruby-processing/exporters/exporters.rb +0 -7
  212. data/library/vecmath/lib/arcball.rb +0 -64
  213. data/library/vecmath/lib/quaternion.rb +0 -62
  214. data/library/vecmath/lib/vec.rb +0 -232
  215. data/ruby-processing.gemspec +0 -54
  216. data/samples/processing_app/library/pdf/.gitignore +0 -1
  217. data/samples/processing_app/library/vecmath/retained_test.rb +0 -65
  218. data/samples/processing_app/library/vecmath/retained_test2.rb +0 -65
  219. data/samples/processing_app/library/vecmath/wiggle_pshape.rb +0 -22
  220. data/test/README.md +0 -6
  221. data/test/basic.rb +0 -14
  222. data/test/draw_ex.rb +0 -12
  223. data/test/graphics.rb +0 -7
  224. data/test/p2d.rb +0 -14
  225. data/test/p3d.rb +0 -14
  226. data/test/rp5_test.rb +0 -86
  227. data/test/setup_ex.rb +0 -12
@@ -0,0 +1,212 @@
1
+ module PS
2
+ include_package 'org.jbox2d.collision.shapes'
3
+ include_package 'org.jbox2d.common'
4
+ include_package 'org.jbox2d.dynamics'
5
+ java_import 'pbox2d.PBox2D'
6
+
7
+
8
+ # Box2D Particle System
9
+ # <http://www.shiffman.net/teaching/nature>
10
+ # Spring 2010
11
+
12
+ # A class to describe a group of Particles
13
+ # An ArrayList is used to manage the list of Particles
14
+
15
+ class ParticleSystem
16
+
17
+ attr_reader :particles, :x, :y
18
+
19
+ def initialize(bd, num, x, y)
20
+ @particles = [] # Initialize the ArrayList
21
+ @x, @y = x, y # Store the origin point
22
+ num.times do
23
+ particles << PS::Particle.new(bd, x, y)
24
+ end
25
+ end
26
+
27
+ def run
28
+ # Display all the particles
29
+ particles.each do |p|
30
+ p.display
31
+ end
32
+ # Particles that leave the screen, we delete them
33
+ # (note they have to be deleted from both the box2d world and our list
34
+
35
+ particles.each_with_index do |p, i|
36
+ if (p.done)
37
+ particles.delete_at(i)
38
+ end
39
+ end
40
+ end
41
+
42
+ def add_particles(bd, n)
43
+ n.times do
44
+ particles << PS::Particle.new(bd, x, y)
45
+ end
46
+ end
47
+
48
+ # A method to test if the particle system still has particles
49
+ def dead
50
+ particles.empty?
51
+ end
52
+
53
+ end
54
+
55
+ # The Nature of Code
56
+ # <http://www.shiffman.net/teaching/nature>
57
+ # Spring 2012
58
+ # PBox2D example
59
+
60
+ # A Particle
61
+
62
+ class Particle
63
+ TRAIL_SIZE = 6
64
+ # We need to keep track of a Body
65
+
66
+ attr_reader :trail, :body, :box2d
67
+
68
+ # Constructor
69
+ def initialize(b2d, x, y)
70
+ @box2d = b2d
71
+ @trail = Array.new(TRAIL_SIZE, [x, y])
72
+
73
+ # Add the box to the box2d world
74
+ # Here's a little trick, let's make a tiny tiny radius
75
+ # This way we have collisions, but they don't overwhelm the system
76
+ make_body(PS::Vec2.new(x,y), 0.2)
77
+ end
78
+
79
+ # This function removes the particle from the box2d world
80
+ def kill_body
81
+ box2d.destroy_body(body)
82
+ end
83
+
84
+ # Is the particle ready for deletion?
85
+ def done
86
+ # Let's find the screen position of the particle
87
+ pos = box2d.get_body_pixel_coord(body)
88
+ # Is it off the bottom of the screen?
89
+ if (pos.y > $app.height + 20)
90
+ kill_body
91
+ return true
92
+ end
93
+ return false
94
+ end
95
+
96
+ # Drawing the box
97
+ def display
98
+ # We look at each body and get its screen position
99
+ pos = box2d.get_body_pixel_coord(body)
100
+
101
+ # Keep track of a history of screen positions in an array
102
+ (TRAIL_SIZE - 1).times do |i|
103
+ trail[i] = trail[i + 1]
104
+ end
105
+ trail[TRAIL_SIZE - 1] = [pos.x, pos.y]
106
+
107
+ # Draw particle as a trail
108
+ begin_shape
109
+ noFill
110
+ stroke_weight(2)
111
+ stroke(0,150)
112
+ trail.each do |v|
113
+ vertex(v[0], v[1])
114
+ end
115
+ end_shape
116
+ end
117
+
118
+ # This function adds the rectangle to the box2d world
119
+ def make_body(center, r)
120
+ # Define and create the body
121
+ bd = PS::BodyDef.new
122
+ bd.type = PS::BodyType::DYNAMIC
123
+
124
+ bd.position.set(box2d.coord_pixels_to_world(center))
125
+ @body = box2d.create_body(bd)
126
+
127
+ # Give it some initial random velocity
128
+ body.set_linear_velocity(PS::Vec2.new(rand(-1 .. 1), rand(-1 .. 1)))
129
+
130
+ # Make the body's shape a circle
131
+ cs = PS::CircleShape.new
132
+ cs.m_radius = box2d.scalar_pixels_to_world(r)
133
+
134
+ fd = PS::FixtureDef.new
135
+ fd.shape = cs
136
+
137
+ fd.density = 1
138
+ fd.friction = 0 # Slippery when wet!
139
+ fd.restitution = 0.5
140
+
141
+ # We could use this if we want to turn collisions off
142
+ #cd.filter.groupIndex = -10
143
+
144
+ # Attach fixture to body
145
+ body.create_fixture(fd)
146
+
147
+ end
148
+
149
+ end
150
+ # The Nature of Code
151
+ # <http://www.shiffman.net/teaching/nature>
152
+ # Spring 2012
153
+ # PBox2D example
154
+
155
+ # A fixed boundary class (now incorporates angle)
156
+
157
+
158
+
159
+ class Boundary
160
+
161
+ attr_reader :box2d, :b, :x, :y, :w, :h #, :a
162
+
163
+ def initialize(b2d, x, y, w, h, a)
164
+ @box2d = b2d
165
+ @x = x
166
+ @y = y
167
+ @w = w
168
+ @h = h
169
+
170
+ # Define the polygon
171
+ sd = PS::PolygonShape.new
172
+
173
+ # Figure out the box2d coordinates
174
+ box2dW = box2d.scalar_pixels_to_world(w/2)
175
+ box2dH = box2d.scalar_pixels_to_world(h/2)
176
+ # We're just a box
177
+ sd.set_as_box(box2dW, box2dH)
178
+
179
+
180
+ # Create the body
181
+ bd = PS::BodyDef.new
182
+ bd.type = PS::BodyType::STATIC
183
+ bd.angle = a
184
+ bd.position.set(box2d.coord_pixels_to_world(x,y))
185
+ @b = box2d.create_body(bd)
186
+
187
+ # Attached the shape to the body using a Fixture
188
+ b.create_fixture(sd,1)
189
+ end
190
+
191
+ # Draw the boundary, it doesn't move so we don't have to ask the Body for location
192
+ def display
193
+ fill(0)
194
+ stroke(0)
195
+ stroke_weight(1)
196
+ rect_mode(CENTER)
197
+ a = b.get_angle
198
+ push_matrix
199
+ translate(x,y)
200
+ rotate(-a)
201
+ rect(0,0,w,h)
202
+ pop_matrix
203
+ end
204
+
205
+ end
206
+
207
+ end
208
+
209
+
210
+
211
+
212
+
@@ -0,0 +1,154 @@
1
+ # The Nature of Code
2
+ # <http:#www.shiffman.net/teaching/nature>
3
+ # Spring 2010
4
+ # PBox2D example
5
+
6
+ # An uneven surface boundary
7
+ module SB
8
+
9
+ include_package 'org.jbox2d.collision.shapes'
10
+ include_package 'org.jbox2d.common'
11
+ include_package 'org.jbox2d.dynamics'
12
+ java_import 'pbox2d.PBox2D'
13
+
14
+
15
+
16
+ class Surface
17
+ # We'll keep track of all of the surface points
18
+ attr_reader :surface, :body, :box2d, :y, :width, :height
19
+
20
+
21
+ def initialize b2d
22
+ @box2d = b2d
23
+ @surface = []
24
+ @width, @height = $app.width, $app.height
25
+ # This is what box2d uses to put the surface in its world
26
+ chain = SB::ChainShape.new
27
+
28
+ # Perlin noise argument
29
+ xoff = 0.0
30
+
31
+ # This has to go backwards so that the objects bounce off the top of the surface
32
+ # This "edgechain" will only work in one direction!
33
+ (width + 10).step(-10, -5) do |x|
34
+ # Doing some stuff with perlin noise to calculate a surface that points down on one side
35
+ # and up on the other
36
+
37
+ if (x > width/2)
38
+ @y = 100 + (width - x)*1.1 + map(noise(xoff),0,1,-80,80)
39
+ else
40
+ @y = 100 + x*1.1 + map(noise(xoff),0,1,-80,80)
41
+ end
42
+
43
+ # Store the vertex in screen coordinates
44
+ surface << SB::Vec2.new(x, y)
45
+
46
+ # Move through perlin noise
47
+ xoff += 0.1
48
+
49
+ end
50
+
51
+ # Build an array of vertices in Box2D coordinates
52
+ # from the ArrayList we made
53
+ vertices = []
54
+ surface.each do |surf|
55
+ vertices << box2d.coord_pixels_to_world(surf)
56
+ end
57
+ # Create the chain!
58
+ chain.createChain(vertices, vertices.length)
59
+ # The edge chain is now attached to a body via a fixture
60
+ bd = SB::BodyDef.new
61
+ bd.position.set(0.0, 0.0)
62
+ @body = box2d.createBody(bd)
63
+ # Shortcut, we could define a fixture if we
64
+ # want to specify frictions, restitution, etc.
65
+ body.createFixture(chain, 1)
66
+ end
67
+
68
+ # A simple function to just draw the edge chain as a series of vertex points
69
+ def display
70
+ stroke_weight(2)
71
+ stroke(0)
72
+ fill(135, 206, 250)
73
+ beginShape
74
+ vertex(width, 0) # extra vertices so we can fill sky
75
+ surface.each do |v|
76
+ vertex(v[0], v[1]) # the mountain range
77
+ end
78
+ vertex(0, 0) # extra vertices so we can fill sky
79
+ endShape
80
+ end
81
+ end
82
+
83
+ class Particle
84
+ # We need to keep track of a Body
85
+
86
+ attr_reader :body, :box2d, :x, :y, :r
87
+
88
+ # Constructor
89
+ def initialize(b2d, x, y, r)
90
+ @box2d, @x, @y, @r = b2d, x, y, r
91
+ # This function puts the particle in the Box2d world
92
+ make_body(x, y, r)
93
+ end
94
+
95
+ # This function removes the particle from the box2d world
96
+ def kill_body
97
+ box2d.destroy_body(body)
98
+ end
99
+
100
+ # Is the particle ready for deletion?
101
+ def done
102
+ pos = box2d.get_body_pixel_coord(body)
103
+ # Is it off the bottom of the screen?
104
+ if (pos[1] > $app.height + r * 2)
105
+ kill_body
106
+ return true
107
+ end
108
+ return false
109
+ end
110
+
111
+ def display
112
+ # We look at each body and get its screen position
113
+ pos = box2d.get_body_pixel_coord(body)
114
+ # Get its angle of rotation
115
+ a = body.get_angle
116
+ push_matrix
117
+ translate(pos[0], pos[1])
118
+ rotate(-a)
119
+ fill(175)
120
+ stroke(0)
121
+ stroke_weight(1)
122
+ ellipse(0,0,r*2,r*2)
123
+ # Let's add a line so we can see the rotation
124
+ line(0,0,r,0)
125
+ pop_matrix
126
+ end
127
+
128
+ # This function adds the rectangle to the box2d world
129
+ def make_body(x, y, r)
130
+ # Define and create the body
131
+ bd = SB::BodyDef.new
132
+ bd.position = box2d.coord_pixels_to_world(x,y)
133
+ bd.type = SB::BodyType::DYNAMIC
134
+ @body = box2d.world.create_body(bd)
135
+ # Make the body's shape a circle
136
+ cs = SB::CircleShape.new
137
+ cs.m_radius = box2d.scalar_pixels_to_world(r)
138
+ fd = SB::FixtureDef.new
139
+ fd.shape = cs
140
+ # Parameters that affect physics
141
+ fd.density = 1
142
+ fd.friction = 0.01
143
+ fd.restitution = 0.3
144
+ # Attach fixture to body
145
+ body.create_fixture(fd)
146
+ # Give it a random initial velocity (and angular velocity)
147
+ body.set_linear_velocity(SB::Vec2.new(rand(-10 .. 10), rand(5 .. 10)))
148
+ body.set_angular_velocity(rand(-10 .. 10))
149
+ end
150
+ end
151
+ end
152
+
153
+
154
+
@@ -0,0 +1,58 @@
1
+ # The Nature of Code
2
+ # <http:#www.shiffman.net/teaching/nature>
3
+ # Spring 2011
4
+ # PBox2D example
5
+
6
+ # Box2D particle system example
7
+
8
+ load_library :pbox2d
9
+ load_library :particle_system
10
+
11
+ # module PS is a wrapper for java imports, and Boundary and Particle classes
12
+ include PS
13
+
14
+ attr_reader :box2d, :boundaries, :systems
15
+
16
+ def setup
17
+ size(400,300)
18
+ smooth
19
+ # Initialize box2d physics and create the world
20
+ @box2d = PBox2D.new(self)
21
+ box2d.create_world
22
+ # We are setting a custom gravity
23
+ box2d.set_gravity(0, -20)
24
+ # Create ArrayLists
25
+ @systems = []
26
+ @boundaries = []
27
+ # Add a bunch of fixed boundaries
28
+ boundaries << Boundary.new(box2d, 50, 100, 300, 5, -0.3)
29
+ boundaries << Boundary.new(box2d, 250, 175, 300, 5, 0.5)
30
+ end
31
+
32
+ def draw
33
+ background(255)
34
+ # We must always step through time!
35
+ box2d.step
36
+ # Run all the particle systems
37
+ if systems.size > 0
38
+ systems.each do |system|
39
+ system.run
40
+ system.add_particles(box2d, rand(0 .. 2))
41
+ end
42
+ end
43
+ # Display all the boundaries
44
+ boundaries.each do |wall|
45
+ wall.display
46
+ end
47
+ end
48
+
49
+
50
+ def mouse_pressed
51
+ # Add a new Particle System whenever the mouse is clicked
52
+ systems << ParticleSystem.new(box2d, 0, mouse_x, mouse_y)
53
+ end
54
+
55
+
56
+
57
+
58
+
@@ -0,0 +1,58 @@
1
+ # The Nature of Code
2
+ # <http://www.shiffman.net/teaching/nature>
3
+ # Spring 2011
4
+ # PBox2D example
5
+
6
+ # Basic example of falling rectangles
7
+ load_library :pbox2d
8
+ load_library :custom_shape
9
+
10
+ # module B2D is a wrapper for java imports, and Boundary and CustomShape classes
11
+ include B2D
12
+
13
+ attr_reader :box2d, :boundaries, :polygons
14
+
15
+ def setup
16
+ size(640,360)
17
+ smooth
18
+ # Initialize box2d physics and create the world
19
+ @box2d = PBox2D.new(self)
20
+ box2d.create_world
21
+ # We are setting a custom gravity
22
+ box2d.set_gravity(0, -20)
23
+ # Create Arrays
24
+ @polygons = []
25
+ @boundaries = []
26
+ # Add a bunch of fixed boundaries
27
+ boundaries << Boundary.new(box2d, width / 4, height - 5, width/2 - 50, 10, 0)
28
+ boundaries << Boundary.new(box2d, 3*width / 4, height - 50, width/2 - 50, 10, 0)
29
+ boundaries << Boundary.new(box2d, width - 5,height / 2, 10, height, 0)
30
+ boundaries << Boundary.new(box2d, 5, height / 2, 10, height, 0)
31
+ end
32
+
33
+ def draw
34
+ background(255)
35
+ # We must always step through time!
36
+ box2d.step
37
+ # Display all the boundaries
38
+ boundaries.each do |wall|
39
+ wall.display
40
+ end
41
+
42
+ # Display all the polygons
43
+ polygons.each do |cs|
44
+ cs.display
45
+ end
46
+
47
+ # polygons that leave the screen, we delete them
48
+ # (note they have to be deleted from both the box2d world and our list
49
+ polygons.each_with_index do |polygon, i|
50
+ if polygon.done
51
+ polygons.delete_at(i)
52
+ end
53
+ end
54
+ end
55
+
56
+ def mouse_pressed
57
+ polygons << CustomShape.new(box2d, mouse_x, mouse_y)
58
+ end