Skip to content

Commit 7cfd7a3

Browse files
author
monkstone
committed
some tidy up
1 parent 8bd0d98 commit 7cfd7a3

File tree

7 files changed

+113
-391
lines changed

7 files changed

+113
-391
lines changed

samples/contributed/circle_collision.rb

Lines changed: 70 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -2,109 +2,95 @@
22

33
load_library :vecmath
44

5-
5+
# The Ball class knows how to behave, their is a case for a separate boundary class
66
class Ball
77
attr_accessor :position, :r, :m, :velocity
8-
8+
99
def initialize(x = 0.0, y = 0.0, r = 0.0)
1010
@position = Vec2D.new(x, y)
1111
@r = r
1212
@m = r * 0.1
1313
@velocity = Vec2D.new(rand(-3.0..3), rand(-3.0..3))
14-
end
15-
14+
end
15+
1616
def update
1717
@position += velocity
1818
end
19-
20-
def check_boundary width, height
21-
if !(r..width - r).include?(position.x)
22-
(position.x > width - r)? position.x = width - r : position.x = r
19+
20+
def check_boundary(width, height)
21+
unless (r..width - r).include?(position.x)
22+
(position.x > width - r) ? position.x = width - r : position.x = r
2323
velocity.x *= -1
24-
elsif !(r..height - r).include?(position.y)
25-
(position.y > height - r)? position.y = height - r : position.y = r
26-
velocity.y *= -1
27-
end
24+
end
25+
return if (r..height - r).include?(position.y)
26+
(position.y > height - r) ? position.y = height - r : position.y = r
27+
velocity.y *= -1
2828
end
29-
30-
def check_collision other_ball
31-
29+
30+
def check_collision(other_ball)
3231
# get distances between the balls components
3332
difference = other_ball.position - position
34-
3533
# calculate magnitude of the vector separating the balls
36-
37-
38-
if difference.mag < (r + other_ball.r)
39-
# get angle of difference
40-
theta = difference.heading
41-
# precalculate trig values
42-
sine = sin(theta)
43-
cosine = cos(theta)
44-
45-
# ball_array will hold rotated ball positions. You just
46-
# need to worry about ball_array[1] position
47-
ball_array = [Ball.new, Ball.new]
48-
# other_ball's position is relative to ball's
49-
# so you can use the vector between them (difference) as the
50-
# reference point in the rotation expressions.
51-
# ball_array[0].x and ball_array[0].y will initialize
52-
# automatically to 0.0, which is what you want
53-
# since other_ball will rotate around ball
54-
ball_array[1].position.x = cosine * difference.x + sine * difference.y
55-
ball_array[1].position.y = cosine * difference.y - sine * difference.x
56-
57-
# rotate Temporary velocities
58-
velocity_array = [Vec2D.new, Vec2D.new]
59-
velocity_array[0].x = cosine * velocity.x + sine * velocity.y
60-
velocity_array[0].y = cosine * velocity.y - sine * velocity.x
61-
velocity_array[1].x = cosine * other_ball.velocity.x + sine * other_ball.velocity.y
62-
velocity_array[1].y = cosine * other_ball.velocity.y - sine * other_ball.velocity.x
63-
64-
# Now that velocities are rotated, you can use 1D
65-
# conservation of momentum equations to calculate
66-
# the final velocity along the x-axis.
67-
final_velocities = [Vec2D.new, Vec2D.new]
68-
# final rotated velocity for ball
69-
final_velocities[0].x = ((m - other_ball.m) * velocity_array[0].x + 2 * other_ball.m * velocity_array[1].x) / (m + other_ball.m)
70-
final_velocities[0].y = velocity_array[0].y
71-
# final rotated velocity for ball
72-
final_velocities[1].x = ((other_ball.m - m) * velocity_array[1].x + 2 * m * velocity_array[0].x) / (m + other_ball.m)
73-
final_velocities[1].y = velocity_array[1].y
74-
75-
# hack to avoid clumping
76-
#ball_array[0].position.x += final_velocities[0].x
77-
#ball_array[1].position.x += final_velocities[1].x
78-
79-
# Rotate ball positions and velocities back
80-
# Reverse signs in trig expressions to rotate
81-
# in the opposite direction
82-
# rotate balls
83-
final_positions = [Vec2D.new, Vec2D.new]
84-
final_positions[0].x = cosine * ball_array[0].position.x - sine * ball_array[0].position.y
85-
final_positions[0].y = cosine * ball_array[0].position.y + sine * ball_array[0].position.x
86-
final_positions[1].x = cosine * ball_array[1].position.x - sine * ball_array[1].position.y
87-
final_positions[1].y = cosine * ball_array[1].position.y + sine * ball_array[1].position.x
88-
89-
# update balls to screen position
90-
other_ball.position = position + final_positions[1]
91-
@position += final_positions[0]
92-
93-
# update velocities
94-
velocity.x = cosine * final_velocities[0].x - sine * final_velocities[0].y
95-
velocity.y = cosine * final_velocities[0].y + sine * final_velocities[0].x
96-
other_ball.velocity.x = cosine * final_velocities[1].x - sine * final_velocities[1].y
97-
other_ball.velocity.y = cosine * final_velocities[1].y + sine * final_velocities[1].x
98-
99-
end
34+
return unless difference.mag < (r + other_ball.r)
35+
# get angle of difference
36+
theta = difference.heading
37+
# precalculate trig values
38+
sine = sin(theta)
39+
cosine = cos(theta)
40+
# ball_array will hold rotated ball positions. You just
41+
# need to worry about ball_array[1] position
42+
ball_array = [Ball.new, Ball.new]
43+
# other_ball's position is relative to ball's
44+
# so you can use the vector between them (difference) as the
45+
# reference point in the rotation expressions.
46+
# ball_array[0].x and ball_array[0].y will initialize
47+
# automatically to 0.0, which is what you want
48+
# since other_ball will rotate around ball
49+
ball_array[1].position.x = cosine * difference.x + sine * difference.y
50+
ball_array[1].position.y = cosine * difference.y - sine * difference.x
51+
# rotate Temporary velocities
52+
velocity_array = [Vec2D.new, Vec2D.new]
53+
velocity_array[0].x = cosine * velocity.x + sine * velocity.y
54+
velocity_array[0].y = cosine * velocity.y - sine * velocity.x
55+
velocity_array[1].x = cosine * other_ball.velocity.x + sine * other_ball.velocity.y
56+
velocity_array[1].y = cosine * other_ball.velocity.y - sine * other_ball.velocity.x
57+
# Now that velocities are rotated, you can use 1D
58+
# conservation of momentum equations to calculate
59+
# the final velocity along the x-axis.
60+
final_velocities = [Vec2D.new, Vec2D.new]
61+
# final rotated velocity for ball
62+
final_velocities[0].x = ((m - other_ball.m) * velocity_array[0].x + 2 * other_ball.m * velocity_array[1].x) / (m + other_ball.m)
63+
final_velocities[0].y = velocity_array[0].y
64+
# final rotated velocity for ball
65+
final_velocities[1].x = ((other_ball.m - m) * velocity_array[1].x + 2 * m * velocity_array[0].x) / (m + other_ball.m)
66+
final_velocities[1].y = velocity_array[1].y
67+
# HACK: to avoid clumping
68+
# ball_array[0].position.x += final_velocities[0].x
69+
# ball_array[1].position.x += final_velocities[1].x
70+
# Rotate ball positions and velocities back
71+
# Reverse signs in trig expressions to rotate
72+
# in the opposite direction
73+
# rotate balls
74+
final_positions = [Vec2D.new, Vec2D.new]
75+
final_positions[0].x = cosine * ball_array[0].position.x - sine * ball_array[0].position.y
76+
final_positions[0].y = cosine * ball_array[0].position.y + sine * ball_array[0].position.x
77+
final_positions[1].x = cosine * ball_array[1].position.x - sine * ball_array[1].position.y
78+
final_positions[1].y = cosine * ball_array[1].position.y + sine * ball_array[1].position.x
79+
# update balls to screen position
80+
other_ball.position = position + final_positions[1]
81+
@position += final_positions[0]
82+
# update velocities
83+
velocity.x = cosine * final_velocities[0].x - sine * final_velocities[0].y
84+
velocity.y = cosine * final_velocities[0].y + sine * final_velocities[0].x
85+
other_ball.velocity.x = cosine * final_velocities[1].x - sine * final_velocities[1].y
86+
other_ball.velocity.y = cosine * final_velocities[1].y + sine * final_velocities[1].x
10087
end
101-
88+
10289
def display
10390
no_stroke
10491
fill(204)
10592
ellipse(position.x, position.y, r * 2, r * 2)
10693
end
107-
10894
end
10995

11096
attr_reader :balls
@@ -114,16 +100,12 @@ def setup
114100
@balls = [Ball.new(100, 40, 20), Ball.new(200, 100, 80)]
115101
end
116102

117-
118103
def draw
119-
background(51)
120-
balls.each do |b|
104+
background(51)
105+
balls.each do |b|
121106
b.update
122107
b.display
123108
b.check_boundary width, height
124109
end
125-
126110
balls[0].check_collision(balls[1])
127111
end
128-
129-

samples/contributed/elegant_ball.rb

Lines changed: 25 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,26 @@
22
# After a vanilla processing sketch by
33
# Ben Notorianni aka lazydog
44
#
5-
65
# elegant.rb
7-
8-
96
load_library :vecmath
10-
117
attr_reader :start_t, :renderer
128

13-
def setup()
9+
def setup
1410
size(800, 800, P3D)
15-
@renderer = AppRender.new(self)
11+
@renderer = AppRender.new(self)
1612
color_mode(RGB, 1)
1713
end
1814

19-
20-
21-
def draw()
22-
#background(0.25)
15+
def draw
2316
background(0)
2417
# Move the origin so that the scene is centered on the screen.
25-
translate(width/2, height/2, 0.0)
18+
translate(width / 2, height / 2, 0.0)
2619
# Set up the lighting.
2720
setup_lights
2821
# Rotate the local coordinate system.
2922
smooth_rotation(5.0, 6.7, 7.3)
3023
# Draw the inner object.
31-
no_stroke()
24+
no_stroke
3225
fill(smooth_colour(10.0, 12.0, 7.0))
3326
draw_icosahedron(5, 60.0, false)
3427
# Rotate the local coordinate system again.
@@ -42,12 +35,12 @@ def draw()
4235
def setup_lights
4336
ambient_light(0.025, 0.025, 0.025)
4437
directional_light(0.2, 0.2, 0.2, -1, -1, -1)
45-
spot_light(1.0, 1.0, 1.0, -200, 0, 300, 1, 0, -1, Math::PI/4, 20)
38+
spot_light(1.0, 1.0, 1.0, -200, 0, 300, 1, 0, -1, Math::PI / 4, 20)
4639
end
4740

4841
##
4942
# Generate a vector whose components change smoothly over time in the range [ 0, 1 ].
50-
# Each component uses a Math.sin() function to map the current time in milliseconds somewhere
43+
# Each component uses a Math.sin function to map the current time in milliseconds somewhere
5144
# in the range [ 0, 1 ].A 'speed' factor is specified for each component.
5245
#
5346
def smooth_vector(s1, s2, s3)
@@ -70,7 +63,7 @@ def smooth_colour(s1, s2, s3)
7063

7164
##
7265
# Rotate the current coordinate system.
73-
# Uses smooth_vector() to smoothly animate the rotation.
66+
# Uses smooth_vector to smoothly animate the rotation.
7467
#
7568
def smooth_rotation(s1, s2, s3)
7669
r1 = smooth_vector(s1, s2, s3)
@@ -91,24 +84,18 @@ def draw_icosahedron(depth, r, spherical)
9184
h = r / Math.sqrt(1.0 + gr * gr)
9285
v =
9386
[
94-
Vec3D.new(0, -h, h*gr), Vec3D.new(0, -h, -h*gr), Vec3D.new(0, h, -h*gr), Vec3D.new(0, h, h*gr),
95-
Vec3D.new(h, -h*gr, 0), Vec3D.new(h, h*gr, 0), Vec3D.new(-h, h*gr, 0), Vec3D.new(-h, -h*gr, 0),
96-
Vec3D.new(-h*gr, 0, h), Vec3D.new(-h*gr, 0, -h), Vec3D.new(h*gr, 0, -h), Vec3D.new(h*gr, 0, h)
97-
]
98-
87+
Vec3D.new(0, -h, h * gr), Vec3D.new(0, -h, -h * gr), Vec3D.new(0, h, -h * gr), Vec3D.new(0, h, h * gr),
88+
Vec3D.new(h, -h * gr, 0), Vec3D.new(h, h * gr, 0), Vec3D.new(-h, h * gr, 0), Vec3D.new(-h, -h * gr, 0),
89+
Vec3D.new(-h * gr, 0, h), Vec3D.new(-h * gr, 0, -h), Vec3D.new(h * gr, 0, -h), Vec3D.new(h * gr, 0, h)
90+
]
9991
# Draw the 20 triangular faces of the icosahedron.
100-
unless spherical then
101-
r = 0.0
102-
end
103-
92+
r = 0.0 unless spherical
10493
begin_shape(TRIANGLES)
105-
106-
draw_triangle(depth, r, v[0], v[7],v[4])
94+
draw_triangle(depth, r, v[0], v[7], v[4])
10795
draw_triangle(depth, r, v[0], v[4], v[11])
10896
draw_triangle(depth, r, v[0], v[11], v[3])
10997
draw_triangle(depth, r, v[0], v[3], v[8])
11098
draw_triangle(depth, r, v[0], v[8], v[7])
111-
11299
draw_triangle(depth, r, v[1], v[4], v[7])
113100
draw_triangle(depth, r, v[1], v[10], v[4])
114101
draw_triangle(depth, r, v[10], v[11], v[4])
@@ -119,23 +106,20 @@ def draw_icosahedron(depth, r, spherical)
119106
draw_triangle(depth, r, v[8], v[9], v[6])
120107
draw_triangle(depth, r, v[9], v[7], v[8])
121108
draw_triangle(depth, r, v[7], v[1], v[9])
122-
123109
draw_triangle(depth, r, v[2], v[1], v[9])
124110
draw_triangle(depth, r, v[2], v[10], v[1])
125111
draw_triangle(depth, r, v[2], v[5], v[10])
126112
draw_triangle(depth, r, v[2], v[6], v[5])
127113
draw_triangle(depth, r, v[2], v[9], v[6])
128-
129-
end_shape()
114+
end_shape
130115
end
131116

132117
##
133118
# Draw a triangle either immediately or subdivide it first.
134119
# If depth is 1 then draw the triangle otherwise subdivide first.
135120
#
136121
def draw_triangle(depth, r, p1, p2, p3)
137-
138-
if (depth == 1) then
122+
if (depth == 1)
139123
p1.to_vertex(renderer)
140124
p2.to_vertex(renderer)
141125
p3.to_vertex(renderer)
@@ -144,15 +128,15 @@ def draw_triangle(depth, r, p1, p2, p3)
144128
v1 = (p1 + p2) * 0.5
145129
v2 = (p2 + p3) * 0.5
146130
v3 = (p3 + p1) * 0.5
147-
unless (r == 0.0) then
148-
# Project the verticies out onto the sphere with radius r.
149-
v1.normalize!
150-
v1 *= r
151-
v2.normalize!
152-
v2 *= r
153-
v3.normalize!
154-
v3 *= r
155-
end
131+
unless (r == 0.0)
132+
# Project the verticies out onto the sphere with radius r.
133+
v1.normalize!
134+
v1 *= r
135+
v2.normalize!
136+
v2 *= r
137+
v3.normalize!
138+
v3 *= r
139+
end
156140
## Generate the next level of detail
157141
depth -= 1
158142
draw_triangle(depth, r, p1, v1, v3)
@@ -162,4 +146,3 @@ def draw_triangle(depth, r, p1, p2, p3)
162146
# draw_triangle(depth, r, v1, v2, v3)
163147
end
164148
end
165-

samples/contributed/empathy.rb

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
attr_reader :cells
1515

1616
def setup
17-
size(500, 500, P3D)
17+
size(500, 500, P2D)
1818
stroke(0, 0, 0, 25)
1919
initialize_cells
2020
start_cell_updates if MULTI_THREADED
@@ -31,32 +31,30 @@ def initialize_cells
3131
end
3232

3333
def start_cell_updates
34-
Thread.new { Kernel.loop { cells.each { |cell| cell.update } } }
34+
Thread.new { Kernel.loop { cells.each(&:update) } }
3535
end
3636

3737
def draw
3838
background 255
39-
cells.each { |cell| cell.sense } if started?
39+
cells.each(&:sense) if started?
4040
end
4141

4242
def started?
4343
pmouse_x != 0 || pmouse_y != 0
4444
end
4545

4646
def mouse_pressed
47-
cells.each { |cell| cell.reset }
47+
cells.each(&:reset)
4848
end
4949

5050
##
5151
# The cell responds to mouse movement
5252
##
53-
5453
class Cell
5554
attr_reader :x, :y, :spin, :angle
5655
def initialize(x, y)
5756
@x, @y = x, y
58-
@spin = 0
59-
@angle = 0
57+
reset
6058
end
6159

6260
def reset

0 commit comments

Comments
 (0)