-
Notifications
You must be signed in to change notification settings - Fork 0
/
collisions.rb
182 lines (139 loc) · 3.79 KB
/
collisions.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
require 'ruby2d'
require 'matrix'
set title: "Ball chaos"
set background: 'white'
set width:1200
set height:800
class Box
attr_accessor :x, :y, :x_velocity, :y_velocity, :mass, :colliding, :radius, :size, :circle, :in_reach, :grabbed
attr_reader :radius, :size
def initialize(args)
# say something if one of the args is not recognized?
@x = args[:x] || rand(Window.width)
@y = args[:y] || rand(Window.height)
@x_velocity = args[:x_velocity] || (-3..3).to_a.sample
@y_velocity = args[:y_velocity] || (-3..3).to_a.sample
@mass = args[:m] || (4..10).to_a.sample
@color = Color.new('random')
@colliding = false
@shape = 'circle'
@grabbed = false
end
def radius
# 10 is an arbitrary constant relating radius to mass**0.5 (all ball habve the same density), to get a suitable ball radius for display
10*@mass**0.5
end
def size
10*@mass**0.5
end
def draw
# @square = Square.new(x: @x,y:@y,size:@size, color:@color)
case @shape
when 'square'
# puts 'mysize'
# puts self.size
@square = Square.new(x: @x,y:@y,size: self.size,color:@color)
when 'circle'
@circle = Circle.new(
x: @x, y: @y,
radius: self.radius,
color: @color)
end
end
def move
@x= (@x+@x_velocity) % Window.width
@y= (@y+ @y_velocity) % Window.height
end
def check_for_collision
if (@square || @circle) && colision_detected?
other_box = colision_detected?
x1 = Vector[@x,@y]
v1 = Vector[@x_velocity,@y_velocity]
if !@grabbed
m1 = @mass
else
m1 = 1000
end
x2 = Vector[other_box.x,other_box.y]
v2 = Vector[other_box.x_velocity,other_box.y_velocity]
if !other_box.grabbed
m2 = other_box.mass
else
m2 = 1000
end
out = collisions_2d(x1,v1,m1,x2,v2,m2)
# puts "collision"
# puts out
@x_velocity = out[0][0]
@y_velocity = out[0][1]
other_box.x_velocity = out[1][0]
other_box.y_velocity = out[1][1]
@colliding = true
other_box.colliding = true
#
# @x_velocity = - @x_velocity
# @y_velocity = -@y_velocity
end
end
def colision_detected?
case @shape
when 'square'
($boxes - Array(self)).any? do |other_box|
if other_box.include?(@square)
return other_box
end
end
when 'circle'
($boxes - Array(self)).any? do |other_box|
distance_c_c = ((@x-other_box.x)**2 + (@y-other_box.y)**2 )**0.5
if distance_c_c < self.radius + other_box.radius
return other_box
end
end
end
end
def include?(other_square)
@square.contains?(other_square.x1,other_square.y1) ||
@square.contains?(other_square.x2,other_square.y2) ||
@square.contains?(other_square.x3,other_square.y3) ||
@square.contains?(other_square.x4,other_square.y4)
end
def in_reach?(player)
if @circle && @circle.contains?(player.x,player.y)
# puts "in reach"
@in_reach = true
else
@in_reach = false
end
end
def grabbed?(player)
if player.left_pressed && @in_reach
@grabbed = true
puts "grabbed"
else
@grabbed = false
end
end
end
def collisions_2d(x1,v1,m1,x2,v2,m2)
m2 = m2.to_f
m1 = m1.to_f
@V1f = v1 - 2 * m2 / (m1+m2) * (((v1-v2).inner_product (x1-x2))/(x1-x2).magnitude**2)*(x1-x2)
@V2f = v2 - 2 * m1 / (m1+m2) * (((v2-v1).inner_product (x2-x1))/(x2-x1).magnitude**2)*(x2-x1)
return @V1f,@V2f
end
class Player
attr_accessor :x,:y,:delta_x, :delta_y, :left_pressed
def initialize
@x = -5
@y = -5
end
def release_ball
$boxes.any? do |box|
if box.grabbed
puts "box released"
box.in_reach = false
end
end
end
end