ruby-processing 2.4.3 → 2.4.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (76) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +3 -1
  3. data/CHANGELOG +5 -0
  4. data/README.md +4 -2
  5. data/lib/ruby-processing/exporters/application_exporter.rb +1 -0
  6. data/lib/ruby-processing/version.rb +1 -1
  7. data/library/boids/boids.rb +14 -13
  8. data/library/vecmath/lib/vec.rb +20 -4
  9. data/samples/contributed/circle_collision.rb +92 -149
  10. data/samples/contributed/drawolver.rb +13 -27
  11. data/samples/contributed/elegant_ball.rb +158 -0
  12. data/samples/contributed/fern.rb +16 -3
  13. data/samples/contributed/flight_patterns.rb +16 -4
  14. data/samples/external_library/java_processing/{pbox2d → box2d_processing}/bumpy_surface_noise.rb +4 -9
  15. data/samples/external_library/java_processing/{pbox2d → box2d_processing}/data/java_args.txt +0 -0
  16. data/samples/external_library/java_processing/{pbox2d → box2d_processing}/library/custom_shape/custom_shape.rb +1 -1
  17. data/samples/external_library/java_processing/{pbox2d → box2d_processing}/library/particle_system/particle_system.rb +7 -10
  18. data/samples/external_library/java_processing/{pbox2d → box2d_processing}/library/surface/surface.rb +2 -2
  19. data/samples/external_library/java_processing/{pbox2d → box2d_processing}/liquidy.rb +7 -7
  20. data/samples/external_library/java_processing/{pbox2d → box2d_processing}/polygons.rb +4 -9
  21. data/samples/external_library/java_processing/custom/README.md +15 -0
  22. data/samples/external_library/java_processing/custom/Rakefile +28 -0
  23. data/samples/external_library/java_processing/custom/landscape.rb +58 -0
  24. data/samples/external_library/java_processing/custom/src/nn/Connection.java +47 -0
  25. data/samples/external_library/java_processing/custom/src/nn/HiddenNeuron.java +20 -0
  26. data/samples/external_library/java_processing/custom/src/nn/InputNeuron.java +23 -0
  27. data/samples/external_library/java_processing/custom/src/nn/Network.java +136 -0
  28. data/samples/external_library/java_processing/custom/src/nn/Neuron.java +79 -0
  29. data/samples/external_library/java_processing/custom/src/nn/OutputNeuron.java +7 -0
  30. data/samples/external_library/java_processing/custom/xor.rb +88 -0
  31. data/samples/external_library/ruby_gem/README +1 -1
  32. data/samples/external_library/ruby_gem/data/data.json +8 -0
  33. data/samples/external_library/ruby_gem/draw_test.rb +171 -0
  34. data/samples/processing_app/basics/form/icosahedra.rb +39 -0
  35. data/samples/processing_app/basics/form/library/icosahedron/icosahedron.rb +60 -0
  36. data/samples/processing_app/basics/form/toroid.rb +78 -92
  37. data/samples/processing_app/basics/transform/birds.rb +6 -12
  38. data/samples/processing_app/basics/transform/cubes_in_cube.rb +25 -22
  39. data/samples/processing_app/basics/transform/library/cube/cube.rb +16 -16
  40. data/samples/processing_app/basics/transform/rotate_push_pop.rb +1 -1
  41. data/samples/processing_app/demos/graphics/bezier_patch.rb +18 -31
  42. data/samples/processing_app/demos/graphics/trefoil.rb +15 -15
  43. data/samples/processing_app/library/vecmath/acceleration_with_vectors.rb +3 -3
  44. data/samples/processing_app/library/vecmath/hilbert_fractal.rb +2 -2
  45. data/samples/processing_app/library/vecmath/library/flock/flock.rb +18 -21
  46. data/samples/processing_app/library/vecmath/library/hilbert/hilbert.rb +11 -8
  47. data/samples/processing_app/library/vecmath/library/wiggler/wiggler.rb +7 -15
  48. data/samples/processing_app/library/vecmath/seeking_neural.rb +172 -0
  49. data/samples/processing_app/topics/animation/animated_sprite.rb +5 -8
  50. data/samples/processing_app/topics/animation/sequential.rb +2 -3
  51. data/samples/processing_app/topics/create_shapes/library/particle/particle_system.rb +7 -7
  52. data/samples/processing_app/topics/create_shapes/particle_system_pshape.rb +2 -2
  53. data/samples/processing_app/topics/create_shapes/wiggle_pshape.rb +2 -1
  54. data/samples/processing_app/topics/lsystems/koch.rb +1 -1
  55. data/samples/processing_app/topics/lsystems/library/koch/koch_fractal.rb +24 -23
  56. data/samples/processing_app/topics/motion/circle_collision.rb +117 -160
  57. data/samples/processing_app/topics/motion/library/cube/cube.rb +1 -1
  58. data/samples/processing_app/topics/motion/morph.rb +1 -1
  59. data/samples/processing_app/topics/motion/reflection1.rb +17 -16
  60. data/samples/processing_app/topics/shaders/conway.rb +2 -2
  61. data/samples/processing_app/topics/shaders/data/conway.glsl +10 -10
  62. data/samples/processing_app/topics/shaders/glsl_heightmap_noise.rb +9 -8
  63. data/samples/processing_app/topics/shaders/landscape.rb +1 -1
  64. data/samples/processing_app/topics/simulate/flocking.rb +1 -1
  65. data/samples/processing_app/topics/simulate/library/flock/flock.rb +62 -57
  66. data/samples/processing_app/topics/simulate/multiple_particle_systems.rb +8 -28
  67. data/samples/processing_app/topics/simulate/simple_particle_system.rb +9 -7
  68. data/samples/processing_app/topics/simulate/smoke_particle_system.rb +12 -11
  69. data/vendors/Rakefile +2 -2
  70. metadata +26 -21
  71. data/samples/contributed/pong.rb +0 -177
  72. data/samples/contributed/simple_buffer.rb +0 -44
  73. data/samples/external_library/java_processing/pbox2d/contact_test.rb +0 -23
  74. data/samples/processing_app/basics/form/icosahedra/icosahedra.rb +0 -72
  75. data/samples/processing_app/basics/form/icosahedra/icosahedron.rb +0 -116
  76. data/samples/processing_app/basics/form/icosahedra/shape_3D.rb +0 -25
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: af2855010f2d2de32e8e7dc8c1828f51c84f56af
4
- data.tar.gz: dab785b90f956a8ef19af5bffe4e449577397fcb
3
+ metadata.gz: b4495d11c567dab0a4324cbb26cfb493f18e413a
4
+ data.tar.gz: 49efbea8dca4fcbbc160312bb6888b38217e935d
5
5
  SHA512:
6
- metadata.gz: 50cf01b0dd1f3a1e327abbd76448dfacb202ba8f366337b12c80a6a2d7807b7b4b8a1020c2a4ecbee25ad1e0459c837791e6491bc0741f1c591f60ac0c47c141
7
- data.tar.gz: 844bc75fbdaea88f0f2e83d282da9c5bbb467475c56021177f60a8e01caccdfd57c95deb3a022fc86e47535a978da780d1ec975444170bd1c3eb7d152b5517c9
6
+ metadata.gz: c3fd10e43df37279cd790a9705ee42fec592335461f09490127097f3bf0aaa649bd9968faa86193df6bdb7dc30be4b12c1a04aa46cb2cc8aa2ae82a25b9aebee
7
+ data.tar.gz: 3b01b98834983da3428190813ac6e2a1d9c834d078d578b55a9131fcaaa75978fa4eec3d7fb39137f93745149cf1c501ec5aad8983ec4806c2e064ce64f8d41c
data/.gitignore CHANGED
@@ -1,7 +1,7 @@
1
1
  syntax: glob
2
2
  .DS_Store
3
3
  *.sw*
4
- *.*~
4
+ *~
5
5
  *.pdf
6
6
  *.gem
7
7
  *.tgz
@@ -9,3 +9,5 @@ syntax: glob
9
9
  *.zip
10
10
  samples/processing_app/topics/file_io/output/*.png
11
11
  Gemfile.lock
12
+ .ruby-version
13
+ .jrubyrc
data/CHANGELOG CHANGED
@@ -1,3 +1,8 @@
1
+ v2.4.4 Update to JRuby-1.7.12
2
+ * Enhancement to Vec2D & Vec3D (preferred to PVector as
3
+ * providing a more ruby-like interface), now provide a
4
+ * conditional set_mag method, via optional &block.
5
+
1
6
  v2.4.3 Update to JRuby-1.7.11
2
7
  * Added an autorun demo Rakefile to some sample directories
3
8
  * Support utf-8 in sketches
data/README.md CHANGED
@@ -1,5 +1,5 @@
1
1
  # Ruby-Processing
2
- [![Gem Version](https://badge.fury.io/rb/ruby-processing.png)](http://badge.fury.io/rb/ruby-processing)
2
+ [![Gem Version](https://badge.fury.io/rb/ruby-processing.svg)](http://badge.fury.io/rb/ruby-processing)
3
3
 
4
4
  Is a ruby wrapper around the [Processing][] code art framework, built using [JRuby][].
5
5
 
@@ -11,6 +11,7 @@ Is a ruby wrapper around the [Processing][] code art framework, built using [JRu
11
11
  smooth, text_align, translate, triangle...
12
12
 
13
13
  ## Installation
14
+ MacOSX users please try out this new [method](https://github.com/jashkenas/ruby-processing/wiki/Installing-ruby-processing-on-the-mac)
14
15
 
15
16
  Ideally you should install [jruby](http://jruby.org/download), at the very least you will have at least ruby-1.9.3 installed. You should [download][] and install vanilla [processing-2.0.3](https://processing.org/download/) (MacOSX) or [processing-2.1.0](https://processing.org/download/) (Windows, Linux) prior to installing this version of ruby-processing. You must also set the `PROCESSING_ROOT` in the .rp5rc yaml configuration file, the easiest way to do this is by running the [SetProcessingRoot.pde](https://gist.github.com/monkstone/7438749) sketch in the processing ide.
16
17
 
@@ -95,6 +96,7 @@ This library has been tested with the following ruby implementations
95
96
 
96
97
  * Ruby 1.9.3
97
98
  * Ruby 2.0.0
99
+ * Ruby 2.1.0
98
100
  * [JRuby][] preferred (required to use rubygems in sketches)
99
101
 
100
102
  [License][]
@@ -111,7 +113,7 @@ This library has been tested with the following ruby implementations
111
113
 
112
114
  ## What's new
113
115
 
114
- New capabilities since processing-2.0 include ability to use OPENGL VBO (PShape) and GLSL shaders (PShader), to keep abreast of latest shader developments for processing check out the [codeanticode blog](http://codeanticode.wordpress.com/2013/06/04/processing-2-0-is-out-processing-2-0-is-in/). Some worked examples using new feature are included in the samples. The update to the latest jruby is also significant as the default is to run with ruby-1.9 support (or even use ruby-2.0 syntax with `compat.version=2.0` in your `.jrubyrc` config), and there have been many performance improvements. Some performance improvements will not be available on the Mac as they require at least java-7+, and there is even more to come with java-8+ (adventurous Mac users can read more about [Oracle java here](http://www.java.com/en/download/faq/java_mac.xml) ). More than 330 worked examples are included in the [Samples][], many of which are rubified version of the examples included with vanilla processing, and or the contributed libraries, additions are welcome.
116
+ New capabilities since processing-2.0 include ability to use OPENGL VBO (PShape) and GLSL shaders (PShader), to keep abreast of latest shader developments for processing check out the [codeanticode blog](http://codeanticode.wordpress.com/2013/06/04/processing-2-0-is-out-processing-2-0-is-in/). Some worked examples using new feature are included in the samples. The update to the latest jruby is also significant as the default is to run with ruby-1.9 support (or even use ruby-2.0 syntax with `compat.version=2.0` in your `.jrubyrc` config), and there have been many performance improvements. Some performance improvements will not be available on the Mac as they require at least java-7+, and there is even more to come with java-8+ (adventurous Mac users can read more about [Oracle java here](http://www.java.com/en/download/faq/java_mac.xml) ). More than 330 worked examples are included in the [Samples][], many of which are rubified version of the examples included with vanilla processing, and or the contributed libraries, additions are welcome. In my experience ruby-processing works even better with the recently released jdk-8 (try `-XX:+TieredCompilation` option in java_args.txt). But please note you will not be able to use jdk-8 with vanilla-processing, at least for the forseeable future.
115
117
 
116
118
 
117
119
 
@@ -11,6 +11,7 @@ module Processing
11
11
  The application exporter will generate a Mac application for you.
12
12
  Usage: script/application <path_to_sketch>
13
13
  Example: script/applet samples/jwishy.rb
14
+ Probably won't work with Oracle Java on Mac
14
15
 
15
16
  EOS
16
17
 
@@ -1,3 +1,3 @@
1
1
  module RubyProcessing
2
- VERSION="2.4.3"
2
+ VERSION="2.4.4"
3
3
  end
@@ -18,8 +18,8 @@ class Boid
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.each do |boid|
22
- cvx, cvy, cvz = cvx+boid.x, cvy+boid.y, cvz+boid.z unless boid.equal? self
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
25
  cvx, cvy, cvz = cvx/count, cvy/count, cvz/count
@@ -29,13 +29,11 @@ class Boid
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.each do |boid|
33
- unless boid.equal? self
34
- dvx, dvy, dvz = x - boid.x, y - boid.y, z - boid.z
35
- svx += dvx if dvx.abs < radius
36
- svy += dvy if dvy.abs < radius
37
- svz += dvz if dvz.abs < radius
38
- end
32
+ boids.reject{|bd| bd.equal? self}.each do |boid|
33
+ dvx, dvy, dvz = x - boid.x, y - boid.y, z - boid.z
34
+ svx += dvx if dvx.abs < radius
35
+ svy += dvy if dvy.abs < radius
36
+ svz += dvz if dvz.abs < radius
39
37
  end
40
38
  return svx, svy, svz
41
39
  end
@@ -43,10 +41,8 @@ class Boid
43
41
  def alignment(d = 5.0)
44
42
  # Boids like to fly at the speed of traffic.
45
43
  avx, avy, avz = 0.0, 0.0, 0.0
46
- boids.each do |boid|
47
- unless boid.equal? self
48
- avx, avy, avz = avx+boid.vx, avy+boid.vy, avz+boid.vz
49
- end
44
+ boids.reject{|bd| bd.equal? self}.each do |boid|
45
+ avx, avy, avz = avx+boid.vx, avy+boid.vy, avz+boid.vz
50
46
  end
51
47
  count = boids.length - 1.0
52
48
  avx, avy, avz = avx / count, avy / count, avz / count
@@ -94,6 +90,11 @@ class Boids
94
90
  def each &block
95
91
  boids.each &block
96
92
  end
93
+
94
+ def reject &block
95
+ boids.reject &block
96
+ end
97
+
97
98
 
98
99
  def shuffle!
99
100
  boids.shuffle!
@@ -112,8 +112,14 @@ class Vec2D
112
112
  @x, @y = x / magnitude, y / magnitude
113
113
  return self
114
114
  end
115
-
116
- def set_mag(scalar)
115
+
116
+ # The &block option means we can conditionally set_mag to
117
+ # a limit value for example, when given &block returns true.
118
+
119
+ def set_mag(scalar, &block)
120
+ if block_given? && yield == false
121
+ return self
122
+ end
117
123
  magnitude = Math.hypot(x, y)
118
124
  @x, @y = (x * scalar) / magnitude, (y * scalar) / magnitude
119
125
  return self
@@ -146,6 +152,10 @@ class Vec2DR < Vec2D
146
152
  end
147
153
 
148
154
  class Vec3D < Vec2D
155
+
156
+ def mag_squared
157
+ x**2 + y**2 + z**2
158
+ end
149
159
 
150
160
  def modulus
151
161
  Math.sqrt(x**2 + y**2 + z**2)
@@ -197,8 +207,14 @@ class Vec3D < Vec2D
197
207
  @x, @y, @z = x / magnitude, y / magnitude, z / magnitude
198
208
  return self
199
209
  end
200
-
201
- def set_mag(scalar)
210
+
211
+ # The optional &block should evaluate to a boolean
212
+ # if given, the set_mag becomes conditional (eg set max/min)
213
+
214
+ def set_mag(scalar, &block)
215
+ if block_given? && yield == false
216
+ return self
217
+ end
202
218
  magnitude = Math.sqrt(x**2 + y**2 + z**2)
203
219
  @x, @y, @z = (x * scalar) / magnitude, (y * scalar) / magnitude, (z * scalar) / magnitude
204
220
  return self
@@ -1,193 +1,136 @@
1
1
  # Based on http://processing.org/learning/topics/circlecollision.html
2
- # by Joe Holt
3
2
 
3
+ load_library :vecmath
4
4
 
5
5
 
6
- # This inner class demonstrates the use of Ruby-Processing's emulation of
7
- # Java inner classes. The Balls are able to call Processing::App methods.
8
6
  class Ball
9
- attr_accessor :x, :y, :r, :m, :vec, :px, :py
10
- def initialize(r = 0.0, vec = nil, x = 0.0, y = 0.0)
11
- @x, @y, @r = x, y, r
12
- @m = r * 0.1
13
- @vec = vec
14
- end
15
-
16
- def move
17
- @x += vec.x
18
- @y += vec.y
19
- end
20
-
21
- def draw
22
- d = r * 2
23
- ellipse x, y, d, d
24
- @px, @py = x, y
25
- end
7
+ attr_accessor :position, :r, :m, :velocity
26
8
 
27
- def erase
28
- d = r * 2
29
- rect px, py, d, d
30
- end
31
- end
32
-
33
-
34
- def setup
35
- size 400, 400
36
- no_stroke
37
- frame_rate 30
38
- rect_mode RADIUS
39
- @balls = []
40
- 5.times { @balls << Ball.new(10, PVector.new(2.15, -1.35), *empty_space(15)) }
41
- 2.times { @balls << Ball.new(40, PVector.new(-1.65, 0.42), *empty_space(45)) }
42
- @frame_time = nil
43
- @frame_count = 0
44
- end
45
-
46
-
47
- def draw
48
- t = Time.now
49
- fps = 1.0 / (t - @frame_time) if @frame_time
50
- @frame_time = t
51
- @frame_count += 1
9
+ def initialize(x = 0.0, y = 0.0, r = 0.0)
10
+ @position = Vec2D.new(x, y)
11
+ @r = r
12
+ @m = r * 0.1
13
+ @velocity = Vec2D.new(rand(-3.0 .. 3), rand(-3.0 .. 3))
14
+ end
52
15
 
53
- # erase previous screen
54
- if @frame_count == 1
55
- background 51
56
- else
57
- fill 51
58
- @balls.each { |ball| ball.erase }
16
+ def update
17
+ @position += velocity
59
18
  end
60
19
 
61
- # move the balls
62
- fill 240
63
- @balls.each do |ball|
64
- ball.move
65
- ball.draw
66
- check_boundary_collision ball
67
- end
68
- check_object_collisions
69
- end
70
-
71
-
72
- def empty_space(r)
73
- x = y = nil
74
- while !x || !empty_space?(x, y, r) do
75
- x = rand(width)
76
- y = rand(height)
20
+ def check_boundary width, height
21
+ if (position.x > width - r)
22
+ position.x = width - r
23
+ velocity.x *= -1
24
+ elsif (position.x < r)
25
+ position.x = r
26
+ velocity.x *= -1
27
+ elsif (position.y > height - r)
28
+ position.y = height - r
29
+ velocity.y *= -1
30
+ elsif (position.y < r)
31
+ position.y = r
32
+ velocity.y *= -1
33
+ end
77
34
  end
78
- return x, y
79
- end
80
-
81
-
82
- def empty_space?(x, y, r)
83
- @balls.each do |ball|
84
- vx = x - ball.x
85
- vy = y - ball.y
86
- mag = sqrt(vx * vx + vy * vy)
87
- return false if mag < r + ball.r
88
- end
89
- return true
90
- end
91
-
92
-
93
- def check_object_collisions
94
35
 
95
- (0...(@balls.length)).each do |ia|
96
- ((ia+1)...(@balls.length)).each do |ib|
97
-
98
- ba = @balls[ia]
99
- bb = @balls[ib]
100
-
101
- # get distances between the balls components
102
- bVect = PVector.new
103
- bVect.x = bb.x - ba.x
104
- bVect.y = bb.y - ba.y
105
-
106
- # calculate magnitude of the vector separating the balls
107
- bVectMag = sqrt(bVect.x * bVect.x + bVect.y * bVect.y)
108
- next if bVectMag >= ba.r + bb.r
109
- # get angle of bVect
110
- theta = atan2(bVect.y, bVect.x)
36
+ def check_collision other_ball
37
+
38
+ # get distances between the balls components
39
+ difference = other_ball.position - position
40
+
41
+ # calculate magnitude of the vector separating the balls
42
+
43
+ dist_mag_squared = difference.mag_squared
44
+
45
+ if dist_mag_squared < (r + other_ball.r) * (r + other_ball.r)
46
+ # get angle of difference
47
+ theta = difference.heading
111
48
  # precalculate trig values
112
49
  sine = sin(theta)
113
50
  cosine = cos(theta)
114
51
 
115
- # bTemp will hold rotated ball positions. You just
116
- # need to worry about bTemp[1] position
117
- bTemp = [Ball.new, Ball.new]
118
- # bb's position is relative to ba's
119
- # so you can use the vector between them (bVect) as the
52
+ # ball_array will hold rotated ball positions. You just
53
+ # need to worry about ball_array[1] position
54
+ ball_array = [Ball.new, Ball.new]
55
+ # other_ball's position is relative to ball's
56
+ # so you can use the vector between them (difference) as the
120
57
  # reference point in the rotation expressions.
121
- # bTemp[0].x and bTemp[0].y will initialize
58
+ # ball_array[0].x and ball_array[0].y will initialize
122
59
  # automatically to 0.0, which is what you want
123
- # since bb will rotate around ba
124
- bTemp[1].x = cosine * bVect.x + sine * bVect.y
125
- bTemp[1].y = cosine * bVect.y - sine * bVect.x
60
+ # since other_ball will rotate around ball
61
+ ball_array[1].position.x = cosine * difference.x + sine * difference.y
62
+ ball_array[1].position.y = cosine * difference.y - sine * difference.x
126
63
 
127
64
  # rotate Temporary velocities
128
- vTemp = [PVector.new, PVector.new]
129
- vTemp[0].x = cosine * ba.vec.x + sine * ba.vec.y
130
- vTemp[0].y = cosine * ba.vec.y - sine * ba.vec.x
131
- vTemp[1].x = cosine * bb.vec.x + sine * bb.vec.y
132
- vTemp[1].y = cosine * bb.vec.y - sine * bb.vec.x
65
+ velocity_array = [Vec2D.new, Vec2D.new]
66
+ velocity_array[0].x = cosine * velocity.x + sine * velocity.y
67
+ velocity_array[0].y = cosine * velocity.y - sine * velocity.x
68
+ velocity_array[1].x = cosine * other_ball.velocity.x + sine * other_ball.velocity.y
69
+ velocity_array[1].y = cosine * other_ball.velocity.y - sine * other_ball.velocity.x
133
70
 
134
71
  # Now that velocities are rotated, you can use 1D
135
72
  # conservation of momentum equations to calculate
136
73
  # the final velocity along the x-axis.
137
- vFinal = [PVector.new, PVector.new]
138
- # final rotated velocity for ba
139
- vFinal[0].x = ((ba.m - bb.m) * vTemp[0].x + 2 * bb.m * vTemp[1].x) / (ba.m + bb.m)
140
- vFinal[0].y = vTemp[0].y
141
- # final rotated velocity for ba
142
- vFinal[1].x = ((bb.m - ba.m) * vTemp[1].x + 2 * ba.m * vTemp[0].x) / (ba.m + bb.m)
143
- vFinal[1].y = vTemp[1].y
74
+ final_velocities = [Vec2D.new, Vec2D.new]
75
+ # final rotated velocity for ball
76
+ final_velocities[0].x = ((m - other_ball.m) * velocity_array[0].x + 2 * other_ball.m * velocity_array[1].x) / (m + other_ball.m)
77
+ final_velocities[0].y = velocity_array[0].y
78
+ # final rotated velocity for ball
79
+ final_velocities[1].x = ((other_ball.m - m) * velocity_array[1].x + 2 * m * velocity_array[0].x) / (m + other_ball.m)
80
+ final_velocities[1].y = velocity_array[1].y
144
81
 
145
82
  # hack to avoid clumping
146
- bTemp[0].x += vFinal[0].x
147
- bTemp[1].x += vFinal[1].x
83
+ #ball_array[0].position.x += final_velocities[0].x
84
+ #ball_array[1].position.x += final_velocities[1].x
148
85
 
149
86
  # Rotate ball positions and velocities back
150
87
  # Reverse signs in trig expressions to rotate
151
88
  # in the opposite direction
152
89
  # rotate balls
153
- bFinal = [Ball.new, Ball.new]
154
- bFinal[0].x = cosine * bTemp[0].x - sine * bTemp[0].y
155
- bFinal[0].y = cosine * bTemp[0].y + sine * bTemp[0].x
156
- bFinal[1].x = cosine * bTemp[1].x - sine * bTemp[1].y
157
- bFinal[1].y = cosine * bTemp[1].y + sine * bTemp[1].x
90
+ final_positions = [Vec2D.new, Vec2D.new]
91
+ final_positions[0].x = cosine * ball_array[0].position.x - sine * ball_array[0].position.y
92
+ final_positions[0].y = cosine * ball_array[0].position.y + sine * ball_array[0].position.x
93
+ final_positions[1].x = cosine * ball_array[1].position.x - sine * ball_array[1].position.y
94
+ final_positions[1].y = cosine * ball_array[1].position.y + sine * ball_array[1].position.x
158
95
 
159
96
  # update balls to screen position
160
- bb.x = ba.x + bFinal[1].x
161
- bb.y = ba.y + bFinal[1].y
162
- ba.x = ba.x + bFinal[0].x
163
- ba.y = ba.y + bFinal[0].y
97
+ other_ball.position = position + final_positions[1]
98
+ @position += final_positions[0]
164
99
 
165
100
  # update velocities
166
- ba.vec.x = cosine * vFinal[0].x - sine * vFinal[0].y
167
- ba.vec.y = cosine * vFinal[0].y + sine * vFinal[0].x
168
- bb.vec.x = cosine * vFinal[1].x - sine * vFinal[1].y
169
- bb.vec.y = cosine * vFinal[1].y + sine * vFinal[1].x
101
+ velocity.x = cosine * final_velocities[0].x - sine * final_velocities[0].y
102
+ velocity.y = cosine * final_velocities[0].y + sine * final_velocities[0].x
103
+ other_ball.velocity.x = cosine * final_velocities[1].x - sine * final_velocities[1].y
104
+ other_ball.velocity.y = cosine * final_velocities[1].y + sine * final_velocities[1].x
105
+
170
106
  end
171
107
  end
172
108
 
109
+ def display
110
+ no_stroke
111
+ fill(204)
112
+ ellipse(position.x, position.y, r * 2, r * 2)
113
+ end
114
+
173
115
  end
174
116
 
117
+ attr_reader :balls
175
118
 
176
- def check_boundary_collision(ball)
177
- if ball.x > width-ball.r
178
- ball.x = width-ball.r
179
- ball.vec.x *= -1
180
- elsif ball.x < ball.r
181
- ball.x = ball.r
182
- ball.vec.x *= -1
183
- end
184
- if ball.y > height-ball.r
185
- ball.y = height-ball.r
186
- ball.vec.y *= -1
187
- elsif ball.y < ball.r
188
- ball.y = ball.r
189
- ball.vec.y *= -1
119
+ def setup
120
+ size 640, 360
121
+ @balls = [Ball.new(100, 40, 20), Ball.new(200, 100, 80)]
122
+ end
123
+
124
+
125
+ def draw
126
+ background(51)
127
+ balls.each do |b|
128
+ b.update
129
+ b.display
130
+ b.check_boundary width, height
190
131
  end
132
+
133
+ balls[0].check_collision(balls[1])
191
134
  end
192
135
 
193
136