Class: HotCocoa::Graphics::Particle
- Defined in:
- lib/hotcocoa/graphics/elements/particle.rb
Overview
wandering particle with brownian motion
Instance Attribute Summary collapse
-
#acceleration ⇒ Object
Returns the value of attribute acceleration.
-
#points ⇒ Object
Returns the value of attribute points.
-
#stroke ⇒ Object
Returns the value of attribute stroke.
-
#velocity_x ⇒ Object
Returns the value of attribute velocity_x.
-
#velocity_y ⇒ Object
Returns the value of attribute velocity_y.
-
#x ⇒ Object
Returns the value of attribute x.
-
#y ⇒ Object
Returns the value of attribute y.
Instance Method Summary collapse
- #draw(canvas) ⇒ Object
-
#initialize(x, y, velocity_x = 0.0, velocity_y = 2.0) ⇒ Particle
constructor
initialize particle origin x,y coordinates (relative to the center).
-
#move ⇒ Object
move to a new position using brownian motion.
Constructor Details
#initialize(x, y, velocity_x = 0.0, velocity_y = 2.0) ⇒ Particle
initialize particle origin x,y coordinates (relative to the center)
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/hotcocoa/graphics/elements/particle.rb', line 23 def initialize (x, y, velocity_x=0.0, velocity_y=2.0) @age = 0 @acceleration = 0.5 @x = x @y = y @previous_x = 0 @previous_y = 0 # initialize velocity @velocity_x=velocity_x @velocity_y=velocity_y # append the point to the array @points = [NSPoint.new(@x, @y)] @stroke = Color.white end |
Instance Attribute Details
#acceleration ⇒ Object
Returns the value of attribute acceleration.
20 21 22 |
# File 'lib/hotcocoa/graphics/elements/particle.rb', line 20 def acceleration @acceleration end |
#points ⇒ Object
Returns the value of attribute points.
20 21 22 |
# File 'lib/hotcocoa/graphics/elements/particle.rb', line 20 def points @points end |
#stroke ⇒ Object
Returns the value of attribute stroke.
20 21 22 |
# File 'lib/hotcocoa/graphics/elements/particle.rb', line 20 def stroke @stroke end |
#velocity_x ⇒ Object
Returns the value of attribute velocity_x.
20 21 22 |
# File 'lib/hotcocoa/graphics/elements/particle.rb', line 20 def velocity_x @velocity_x end |
#velocity_y ⇒ Object
Returns the value of attribute velocity_y.
20 21 22 |
# File 'lib/hotcocoa/graphics/elements/particle.rb', line 20 def velocity_y @velocity_y end |
#x ⇒ Object
Returns the value of attribute x.
20 21 22 |
# File 'lib/hotcocoa/graphics/elements/particle.rb', line 20 def x @x end |
#y ⇒ Object
Returns the value of attribute y.
20 21 22 |
# File 'lib/hotcocoa/graphics/elements/particle.rb', line 20 def y @y end |
Instance Method Details
#draw(canvas) ⇒ Object
67 68 69 70 |
# File 'lib/hotcocoa/graphics/elements/particle.rb', line 67 def draw(canvas) canvas.nofill canvas.lines(@points) end |
#move ⇒ Object
move to a new position using brownian motion
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/hotcocoa/graphics/elements/particle.rb', line 43 def move # save old x,y position @previous_x=@x @previous_y=@y # move particle by velocity_x,velocity_y @x += @velocity_x @y += @velocity_y # randomly increase/decrease direction @velocity_x += random(-1.0, 1.0) * @acceleration @velocity_y += random(-1.0, 1.0) * @acceleration # draw a line from the old position to the new #CANVAS.line(@previous_x,@previous_y,@x,@y); @points.push(NSPoint.new(@x, @y)) # grow old @age += 1 if @age>200 # die and be reborn end end |