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.
- checksums.yaml +4 -4
- data/.gitignore +3 -1
- data/CHANGELOG +5 -0
- data/README.md +4 -2
- data/lib/ruby-processing/exporters/application_exporter.rb +1 -0
- data/lib/ruby-processing/version.rb +1 -1
- data/library/boids/boids.rb +14 -13
- data/library/vecmath/lib/vec.rb +20 -4
- data/samples/contributed/circle_collision.rb +92 -149
- data/samples/contributed/drawolver.rb +13 -27
- data/samples/contributed/elegant_ball.rb +158 -0
- data/samples/contributed/fern.rb +16 -3
- data/samples/contributed/flight_patterns.rb +16 -4
- data/samples/external_library/java_processing/{pbox2d → box2d_processing}/bumpy_surface_noise.rb +4 -9
- data/samples/external_library/java_processing/{pbox2d → box2d_processing}/data/java_args.txt +0 -0
- data/samples/external_library/java_processing/{pbox2d → box2d_processing}/library/custom_shape/custom_shape.rb +1 -1
- data/samples/external_library/java_processing/{pbox2d → box2d_processing}/library/particle_system/particle_system.rb +7 -10
- data/samples/external_library/java_processing/{pbox2d → box2d_processing}/library/surface/surface.rb +2 -2
- data/samples/external_library/java_processing/{pbox2d → box2d_processing}/liquidy.rb +7 -7
- data/samples/external_library/java_processing/{pbox2d → box2d_processing}/polygons.rb +4 -9
- data/samples/external_library/java_processing/custom/README.md +15 -0
- data/samples/external_library/java_processing/custom/Rakefile +28 -0
- data/samples/external_library/java_processing/custom/landscape.rb +58 -0
- data/samples/external_library/java_processing/custom/src/nn/Connection.java +47 -0
- data/samples/external_library/java_processing/custom/src/nn/HiddenNeuron.java +20 -0
- data/samples/external_library/java_processing/custom/src/nn/InputNeuron.java +23 -0
- data/samples/external_library/java_processing/custom/src/nn/Network.java +136 -0
- data/samples/external_library/java_processing/custom/src/nn/Neuron.java +79 -0
- data/samples/external_library/java_processing/custom/src/nn/OutputNeuron.java +7 -0
- data/samples/external_library/java_processing/custom/xor.rb +88 -0
- data/samples/external_library/ruby_gem/README +1 -1
- data/samples/external_library/ruby_gem/data/data.json +8 -0
- data/samples/external_library/ruby_gem/draw_test.rb +171 -0
- data/samples/processing_app/basics/form/icosahedra.rb +39 -0
- data/samples/processing_app/basics/form/library/icosahedron/icosahedron.rb +60 -0
- data/samples/processing_app/basics/form/toroid.rb +78 -92
- data/samples/processing_app/basics/transform/birds.rb +6 -12
- data/samples/processing_app/basics/transform/cubes_in_cube.rb +25 -22
- data/samples/processing_app/basics/transform/library/cube/cube.rb +16 -16
- data/samples/processing_app/basics/transform/rotate_push_pop.rb +1 -1
- data/samples/processing_app/demos/graphics/bezier_patch.rb +18 -31
- data/samples/processing_app/demos/graphics/trefoil.rb +15 -15
- data/samples/processing_app/library/vecmath/acceleration_with_vectors.rb +3 -3
- data/samples/processing_app/library/vecmath/hilbert_fractal.rb +2 -2
- data/samples/processing_app/library/vecmath/library/flock/flock.rb +18 -21
- data/samples/processing_app/library/vecmath/library/hilbert/hilbert.rb +11 -8
- data/samples/processing_app/library/vecmath/library/wiggler/wiggler.rb +7 -15
- data/samples/processing_app/library/vecmath/seeking_neural.rb +172 -0
- data/samples/processing_app/topics/animation/animated_sprite.rb +5 -8
- data/samples/processing_app/topics/animation/sequential.rb +2 -3
- data/samples/processing_app/topics/create_shapes/library/particle/particle_system.rb +7 -7
- data/samples/processing_app/topics/create_shapes/particle_system_pshape.rb +2 -2
- data/samples/processing_app/topics/create_shapes/wiggle_pshape.rb +2 -1
- data/samples/processing_app/topics/lsystems/koch.rb +1 -1
- data/samples/processing_app/topics/lsystems/library/koch/koch_fractal.rb +24 -23
- data/samples/processing_app/topics/motion/circle_collision.rb +117 -160
- data/samples/processing_app/topics/motion/library/cube/cube.rb +1 -1
- data/samples/processing_app/topics/motion/morph.rb +1 -1
- data/samples/processing_app/topics/motion/reflection1.rb +17 -16
- data/samples/processing_app/topics/shaders/conway.rb +2 -2
- data/samples/processing_app/topics/shaders/data/conway.glsl +10 -10
- data/samples/processing_app/topics/shaders/glsl_heightmap_noise.rb +9 -8
- data/samples/processing_app/topics/shaders/landscape.rb +1 -1
- data/samples/processing_app/topics/simulate/flocking.rb +1 -1
- data/samples/processing_app/topics/simulate/library/flock/flock.rb +62 -57
- data/samples/processing_app/topics/simulate/multiple_particle_systems.rb +8 -28
- data/samples/processing_app/topics/simulate/simple_particle_system.rb +9 -7
- data/samples/processing_app/topics/simulate/smoke_particle_system.rb +12 -11
- data/vendors/Rakefile +2 -2
- metadata +26 -21
- data/samples/contributed/pong.rb +0 -177
- data/samples/contributed/simple_buffer.rb +0 -44
- data/samples/external_library/java_processing/pbox2d/contact_test.rb +0 -23
- data/samples/processing_app/basics/form/icosahedra/icosahedra.rb +0 -72
- data/samples/processing_app/basics/form/icosahedra/icosahedron.rb +0 -116
- 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:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b4495d11c567dab0a4324cbb26cfb493f18e413a
|
4
|
+
data.tar.gz: 49efbea8dca4fcbbc160312bb6888b38217e935d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c3fd10e43df37279cd790a9705ee42fec592335461f09490127097f3bf0aaa649bd9968faa86193df6bdb7dc30be4b12c1a04aa46cb2cc8aa2ae82a25b9aebee
|
7
|
+
data.tar.gz: 3b01b98834983da3428190813ac6e2a1d9c834d078d578b55a9131fcaaa75978fa4eec3d7fb39137f93745149cf1c501ec5aad8983ec4806c2e064ce64f8d41c
|
data/.gitignore
CHANGED
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
|
-
[](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
|
|
data/library/boids/boids.rb
CHANGED
@@ -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
|
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
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
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
|
-
|
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!
|
data/library/vecmath/lib/vec.rb
CHANGED
@@ -112,8 +112,14 @@ class Vec2D
|
|
112
112
|
@x, @y = x / magnitude, y / magnitude
|
113
113
|
return self
|
114
114
|
end
|
115
|
-
|
116
|
-
|
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
|
-
|
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 :
|
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
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
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
|
-
|
54
|
-
|
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
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
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
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
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
|
-
#
|
116
|
-
# need to worry about
|
117
|
-
|
118
|
-
#
|
119
|
-
# so you can use the vector between them (
|
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
|
-
#
|
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
|
124
|
-
|
125
|
-
|
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
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
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
|
-
|
138
|
-
# final rotated velocity for
|
139
|
-
|
140
|
-
|
141
|
-
# final rotated velocity for
|
142
|
-
|
143
|
-
|
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
|
-
|
147
|
-
|
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
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
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
|
-
|
161
|
-
|
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
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
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
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
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
|
|