-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparticles.rb
More file actions
96 lines (75 loc) · 1.47 KB
/
particles.rb
File metadata and controls
96 lines (75 loc) · 1.47 KB
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
require 'ruby-processing'
class Particle
include Processing::Proxy
def initialize l
@location = l.get
@velocity = PVector.new((rand * 2 - 1), (rand * 2-1))
@lifespan = 250
@color = color(rand(255), rand(255), rand(255))
@acceleration = PVector.new(0, 0)
end
def update
@velocity.add(@acceleration)
@location.add(@velocity)
@lifespan -= 2
@acceleration.mult(0)
end
def apply_force(f)
@acceleration.add(f)
end
def display
push_matrix
fill(@color, @lifespan)
ellipse(@location.x, @location.y, 10, 10)
pop_matrix
end
def is_dead
@lifespan <= 0
end
end
class ParticleSystem
include Processing::Proxy
def initialize
@particles = []
end
def add_particle(p)
@particles << p
end
def apply_force(f)
@particles.each do |p|
p.apply_force(f)
end
end
def display
push_matrix
@particles.each do |p|
p.display
end
pop_matrix
end
def update
@particles.each do |p|
p.update
end
@particles.delete_if { |p| p.is_dead }
end
end
class ParticleSystemDrawer < Processing::App
def setup
size(600, 300)
@ps = ParticleSystem.new
@gravity = PVector.new(0, 0.05)
@wind = PVector.new(1, 0)
end
def draw
background(250)
@ps.add_particle(Particle.new(PVector.new(width/2, 50)))
@ps.apply_force(@gravity)
@ps.update
@ps.display
end
def mouse_pressed
@ps.apply_force(@wind)
end
end
ParticleSystemDrawer.new