@@ -10,17 +10,17 @@ def initialize(r = 0.0, velocity = nil, position = Vec2D.new)
1010 @position , @velocity , @r = position , velocity , r
1111 @m = r * 0.1
1212 end
13-
13+
1414 def move
1515 @position += velocity
1616 end
17-
17+
1818 def draw
1919 d = r * 2
2020 ellipse position . x , position . y , d , d
2121 @current = position . copy
2222 end
23-
23+
2424 def erase
2525 d = r * 2
2626 rect current . x , current . y , d , d
@@ -33,10 +33,10 @@ def setup
3333 sketch_title 'Circle Collision'
3434 no_stroke
3535 frame_rate 30
36- rect_mode RADIUS
36+ rect_mode RADIUS
3737 @balls = [ ]
3838 5 . times { balls << Ball . new ( 10 , Vec2D . new ( 2.15 , -1.35 ) , empty_space ( 15 ) ) }
39- 2 . times { balls << Ball . new ( 40 , Vec2D . new ( -1.65 , 0.42 ) , empty_space ( 45 ) ) }
39+ 2 . times { balls << Ball . new ( 40 , Vec2D . new ( -1.65 , 0.42 ) , empty_space ( 45 ) ) }
4040 @frame_time = nil
4141 @frame_count = 0
4242end
@@ -45,15 +45,15 @@ def draw
4545 t = Time . now
4646 fps = 1.0 / ( t - @frame_time ) if @frame_time
4747 @frame_time = t
48- @frame_count += 1
48+ @frame_count += 1
4949 # erase previous screen
5050 if @frame_count == 1
5151 background 51
5252 else
5353 fill 51
5454 balls . each { |ball | ball . erase }
55- end
56- # move the balls
55+ end
56+ # move the balls
5757 fill 240
5858 balls . each do |ball |
5959 ball . move
@@ -80,79 +80,73 @@ def empty_space?(position, r)
8080end
8181
8282def check_object_collisions
83-
83+
8484 ( 0 ...( balls . length ) ) . each do |ia |
8585 ( ( ia +1 ) ...( balls . length ) ) . each do |ib |
86-
8786 ba = balls [ ia ]
8887 bb = balls [ ib ]
89-
9088 # get distances between the balls components
91- bVect = bb . position - ba . position
89+ bVect = bb . position - ba . position
9290 # calculate magnitude of the vector separating the balls
9391 bVectMag = bVect . mag
9492 next if bVectMag >= ba . r + bb . r
9593 # get angle of bVect
96- theta = atan2 ( bVect . y , bVect . x )
94+ theta = bVect . heading
9795 # precalculate trig values
9896 sine = sin ( theta )
9997 cosine = cos ( theta )
100-
10198 # bTemp will hold rotated ball positions. You just
10299 # need to worry about bTemp[1] position
103100 bTemp = [ Ball . new , Ball . new ]
104101 # bb's position is relative to ba's
105- # so you can use the vector between them (bVect) as the
102+ # so you can use the vector between them (bVect) as the
106103 # reference point in the rotation expressions.
107104 # bTemp[0].x and bTemp[0].y will initialize
108105 # automatically to 0.0, which is what you want
109106 # since bb will rotate around ba
110107 bTemp [ 1 ] . position . x = cosine * bVect . x + sine * bVect . y
111108 bTemp [ 1 ] . position . y = cosine * bVect . y - sine * bVect . x
112-
113109 # rotate Temporary velocities
114110 vTemp = [ Vec2D . new , Vec2D . new ]
115111 vTemp [ 0 ] . x = cosine * ba . velocity . x + sine * ba . velocity . y
116112 vTemp [ 0 ] . y = cosine * ba . velocity . y - sine * ba . velocity . x
117113 vTemp [ 1 ] . x = cosine * bb . velocity . x + sine * bb . velocity . y
118114 vTemp [ 1 ] . y = cosine * bb . velocity . y - sine * bb . velocity . x
119-
120115 # Now that velocities are rotated, you can use 1D
121- # conservation of momentum equations to calculate
116+ # conservation of momentum equations to calculate
122117 # the final velocity along the x-axis.
123118 vFinal = [ Vec2D . new , Vec2D . new ]
124119 # final rotated velocity for ba
125120 vFinal [ 0 ] . x = ( ( ba . m - bb . m ) * vTemp [ 0 ] . x + 2 * bb . m * vTemp [ 1 ] . x ) / ( ba . m + bb . m )
126121 vFinal [ 0 ] . y = vTemp [ 0 ] . y
127122 # final rotated velocity for ba
128123 vFinal [ 1 ] . x = ( ( bb . m - ba . m ) * vTemp [ 1 ] . x + 2 * ba . m * vTemp [ 0 ] . x ) / ( ba . m + bb . m )
129- vFinal [ 1 ] . y = vTemp [ 1 ] . y
130-
124+ vFinal [ 1 ] . y = vTemp [ 1 ] . y
131125 # Rotate ball positions and velocities back
132- # Reverse signs in trig expressions to rotate
126+ # Reverse signs in trig expressions to rotate
133127 # in the opposite direction
134128 # rotate balls
135129 bFinal = [ Ball . new , Ball . new ]
136130 bFinal [ 0 ] . position . x = cosine * bTemp [ 0 ] . position . x - sine * bTemp [ 0 ] . position . y
137131 bFinal [ 0 ] . position . y = cosine * bTemp [ 0 ] . position . y + sine * bTemp [ 0 ] . position . x
138132 bFinal [ 1 ] . position . x = cosine * bTemp [ 1 ] . position . x - sine * bTemp [ 1 ] . position . y
139- bFinal [ 1 ] . position . y = cosine * bTemp [ 1 ] . position . y + sine * bTemp [ 1 ] . position . x
133+ bFinal [ 1 ] . position . y = cosine * bTemp [ 1 ] . position . y + sine * bTemp [ 1 ] . position . x
140134 # update balls to screen position
141- bb . position = ba . position + bFinal [ 1 ] . position
142- ba . position = ba . position + bFinal [ 0 ] . position
135+ bb . position = ba . position + bFinal [ 1 ] . position
136+ ba . position = ba . position + bFinal [ 0 ] . position
143137 # update velocities
144138 ba . velocity . x = cosine * vFinal [ 0 ] . x - sine * vFinal [ 0 ] . y
145139 ba . velocity . y = cosine * vFinal [ 0 ] . y + sine * vFinal [ 0 ] . x
146140 bb . velocity . x = cosine * vFinal [ 1 ] . x - sine * vFinal [ 1 ] . y
147141 bb . velocity . y = cosine * vFinal [ 1 ] . y + sine * vFinal [ 1 ] . x
148142 end
149- end
143+ end
150144end
151145
152146# reverse ball velocity if at sketch boundary
153- def check_boundary_collision ( ball )
154- ball . velocity . x *= -1 unless ( ball . r ..width - ball . r ) . include? ball . position . x
155- ball . velocity . y *= -1 unless ( ball . r ..height - ball . r ) . include? ball . position . y
147+ def check_boundary_collision ( ball )
148+ ball . velocity . x *= -1 unless ( ball . r ..width - ball . r ) . include? ball . position . x
149+ ball . velocity . y *= -1 unless ( ball . r ..height - ball . r ) . include? ball . position . y
156150end
157151
158152def settings
0 commit comments