/
play.rb
executable file
·225 lines (156 loc) · 3.57 KB
/
play.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#!/usr/bin/env ruby
# -*- coding: utf-8 -*-
require 'singleton'
require 'rubygems'
require 'bundler'
Bundler.require(:default, :debug)
require 'ostruct'
require './mouse'
# require_all 'lib/**/*.rb'
$h = 40
STEP = $h
W = 600
H = 600
class Vector2D
attr_accessor :x,:y
def initialize(x,y)
@x,@y = x,y
end
def +(b)
Vector2D.new(b.x + @x, b.y + @y)
end
def -(b)
Vector2D.new(@x - b.x, @y - b.y)
end
def *(alpha)
Vector2D.new(alpha * @x, alpha * @y)
end
def to_s
"(#{@x}, #{@y})"
end
def distance_squared(b)
dx = (b.x - @x)
dy = (b.y - @y)
(dx * dx + dy * dy)
end
end
class Particle
attr_accessor :pos, :density, :force, :v
def initialize(x,y, color = 'red')
@color = color
@m = 1.0
@density = 1.0
@pos = Vector2D.new(x, y)
@v = Vector2D.new(0.0,0.0)
@force = Vector2D.new(0.0,0.0)
@img = Gosu::Image.new($window, make_circle(2))
end
def neighbour?(p)
self.pos.distance_squared(p.pos) <= ($h * $h)
end
def calc_density
@density = neighbours.inject(0) do |rho, n|
x = 1.0 - (Math::sqrt(pos.distance_squared(n.pos)) / $h)
rho + x*x
end
end
def calc_pressure_force
rest_density = 1.0
k = 100.0
neighbours.each do |neighbour|
distance = Math::sqrt(pos.distance_squared(neighbour.pos))
q = 1.0 - distance / $h
press = k *(self.density + neighbour.density - (2 * rest_density))
c = (press / distance * 0.5)
r = self.pos - neighbour.pos
f = Vector2D.new( r.x * c, r.y * c)
@force = @force - f
end
end
def neighbours
$particles.select { |p| p != self && self.neighbour?(p) }
end
def make_circle(r)
circle = Magick::Image.new(2 * r, 2 * r) { self.background_color = 'none' }
gc = Magick::Draw.new
gc.fill(@color).circle(r, r, r, 0)
gc.draw(circle)
circle
end
def draw
@img.draw_rot(@pos.x, @pos.y, 0, 0)
end
def to_s
puts "---------------------------"
puts "pos = #{@pos}"
puts "v = #{@v}"
puts "density = #{@density}"
puts "force = #{@force}"
end
end
class WallParticle < Particle
def initialize(x,y)
super(x,y,'green')
@density = 10000
end
def calc_density
end
def calc_pressure_force
end
end
module Timer
# TODO: replace by threading
def every(millis, &block)
now = Gosu::milliseconds
@last_yield ||= now - millis
if (@last_yield + millis) <= now
yield
@last_yield = now
end
end
end
class Win < Gosu::Window
include Timer
def initialize()
super(W, H, false)
$window = self
@mouse = Mouse.new
$particles = []
(0..H).step(STEP) do |y|
(0..W).step(STEP) do |x|
# misplaced particle to change the initial density
oy = y
ox = x
# ox += 30 if x == 200 && y == 200
if x == 0 || y == 0 || x >= W || y >= H
$particles << WallParticle.new(ox,oy)
else
$particles << Particle.new(ox,oy)
end
end
end
end
def draw
@mouse.draw
if button_down?(Gosu::MsLeft)
nearest = $particles.min do |a,b|
a.pos.distance_squared(@mouse.pos) <=> b.pos.distance_squared(@mouse.pos)
end
puts nearest.inspect
end
$particles.each { |p| p.draw }
end
def update
every(100) do
$particles.each(&:calc_density)
$particles.each(&:calc_pressure_force)
dt = 0.01
$particles.each do |p|
p.v = p.v + p.force * dt
p.pos = p.pos + p.v * dt
end
end
close if button_down?(Gosu::KbEscape)
end
end
Win.new.show