1515def setup
1616 size ( 640 , 360 )
1717 @balls = [ ]
18- ( 0 ... NUM_BALLS ) . each do |i |
18+ ( 0 ...NUM_BALLS ) . each do |i |
1919 balls << Ball . new ( rand ( width ) , rand ( height ) , rand ( 30 ..70 ) , i , balls )
2020 end
2121 noStroke
@@ -24,17 +24,17 @@ def setup
2424
2525def draw
2626 background ( 0 )
27- ( 0 ... NUM_BALLS ) . each do |i |
27+ ( 0 ...NUM_BALLS ) . each do |i |
2828 balls [ i ] . collide
29- balls [ i ] . move
29+ balls [ i ] . move ( width , height )
3030 balls [ i ] . display
3131 end
3232end
3333
3434class Ball
3535 attr_accessor :vx , :vy
36- attr_reader :x , :y , :diameter , :others , :id , :width , :height
37-
36+ attr_reader :x , :y , :diameter , :others , :id
37+
3838 def initialize ( xin , yin , din , idin , oin )
3939 @x = xin
4040 @y = yin
@@ -43,54 +43,50 @@ def initialize(xin, yin, din, idin, oin)
4343 @vx = 0
4444 @vy = 0
4545 @others = oin
46- @width = $app. width
47- @height = $app. height
4846 end
49-
47+
5048 def collide
51- ( ( id + 1 ) ... NUM_BALLS ) . each do |i |
49+ ( ( id + 1 ) ...NUM_BALLS ) . each do |i |
5250 dx = others [ i ] . x - x
5351 dy = others [ i ] . y - y
54- sq_dist = dx *dx + dy *dy
55- min_dist = ( others [ i ] . diameter /2 + diameter /2 )
56- sq_min_dist = min_dist * min_dist
57- if ( sq_dist < sq_min_dist )
58- angle = atan2 ( dy , dx )
59- target_x = x + cos ( angle ) * min_dist
60- target_y = y + sin ( angle ) * min_dist
61- ax = ( target_x - others [ i ] . x ) * SPRING
62- ay = ( target_y - others [ i ] . y ) * SPRING
63- @vx -= ax
64- @vy -= ay
65- others [ i ] . vx += ax
66- others [ i ] . vy += ay
67- end
68-
52+ distance = dist ( others [ i ] . x , others [ i ] . y , x , y )
53+ min_dist = ( others [ i ] . diameter / 2 + diameter / 2 )
54+ next unless distance < min_dist
55+ angle = atan2 ( dy , dx )
56+ target_x = x + cos ( angle ) * min_dist
57+ target_y = y + sin ( angle ) * min_dist
58+ ax = ( target_x - others [ i ] . x ) * SPRING
59+ ay = ( target_y - others [ i ] . y ) * SPRING
60+ @vx -= ax
61+ @vy -= ay
62+ others [ i ] . vx += ax
63+ others [ i ] . vy += ay
6964 end
70-
71- def move
72- @vy += GRAVITY
73- @x += vx
74- @y += vy
75- if ( x + diameter /2 > width )
76- @x = width - diameter /2
77- @vx *= FRICTION
78- elsif ( x - diameter /2 < 0 )
79- @x = diameter /2
80- @vx *= FRICTION
81- end
82-
83- if ( y + diameter /2 > height )
84- @y = height - diameter /2
85- @vy *= FRICTION
86- elsif ( y - diameter /2 < 0 )
87- @y = diameter /2
88- @vy *= FRICTION
89- end
65+ end
66+
67+ def move ( width , height )
68+ @vy += GRAVITY
69+ @x += vx
70+ @y += vy
71+ if x + diameter / 2 > width
72+ @x = width - diameter / 2
73+ @vx *= FRICTION
74+ elsif x - diameter / 2 < 0
75+ @x = diameter / 2
76+ @vx *= FRICTION
9077 end
9178
92- def display
93- ellipse ( x , y , diameter , diameter )
79+ if y + diameter / 2 > height
80+ @y = height - diameter / 2
81+ @vy *= FRICTION
82+ elsif y - diameter / 2 < 0
83+ @y = diameter / 2
84+ @vy *= FRICTION
9485 end
9586 end
87+
88+ def display
89+ ellipse ( x , y , diameter , diameter )
90+ end
9691end
92+
0 commit comments