Class: Pongo::AbstractParticle

Inherits:
AbstractItem show all
Defined in:
lib/pongo/abstract_particle.rb

Overview

The abstract base class for all particles.

You should not instantiate this class directly – instead use one of the subclasses.

Direct Known Subclasses

CircleParticle, RectangleParticle

Instance Attribute Summary collapse

Attributes inherited from AbstractItem

#always_repaint, #display_object, #display_object_offset, #display_object_rotation, #fill_alpha, #fill_color, #line_alpha, #line_color, #line_thickness, #renderer, #solid, #user_data, #visible

Instance Method Summary collapse

Methods inherited from AbstractItem

#add_event_listener, #always_redraw!, #always_redraw=, #always_redraw?, #cleanup, #dispatch_event, #draw, #has_event_listener, #init, #set_fill, #set_line, #set_style, #visible!, #visible?

Constructor Details

#initialize(x, y, is_fixed, mass, elasticity, friction) ⇒ AbstractParticle



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/pongo/abstract_particle.rb', line 9

def initialize(x, y, is_fixed, mass, elasticity, friction) 
  super()
  @interval = Interval.new
  @curr = Vector.new(x, y)
  @prev = Vector.new(x, y)
  @samp = Vector.new
  @temp = Vector.new
  @fixed = is_fixed
  @forces = Vector.new
  @force_list = []
  @collision = Collision.new
  @collidable = true
  @first_collision = false
  self.mass = mass
  self.elasticity = elasticity
  self.friction = friction
  set_style
  @center = Vector.new
  @multisample = 0
end

Instance Attribute Details

#centerObject

Returns the value of attribute center.



7
8
9
# File 'lib/pongo/abstract_particle.rb', line 7

def center
  @center
end

#collidableObject

Returns the value of attribute collidable.



7
8
9
# File 'lib/pongo/abstract_particle.rb', line 7

def collidable
  @collidable
end

#collisionObject

Returns the value of attribute collision.



6
7
8
# File 'lib/pongo/abstract_particle.rb', line 6

def collision
  @collision
end

#currObject

Returns the value of attribute curr.



6
7
8
# File 'lib/pongo/abstract_particle.rb', line 6

def curr
  @curr
end

#first_collisionObject

Returns the value of attribute first_collision.



6
7
8
# File 'lib/pongo/abstract_particle.rb', line 6

def first_collision
  @first_collision
end

#fixedObject

Returns the value of attribute fixed.



7
8
9
# File 'lib/pongo/abstract_particle.rb', line 7

def fixed
  @fixed
end

#force_listObject

Returns the value of attribute force_list.



6
7
8
# File 'lib/pongo/abstract_particle.rb', line 6

def force_list
  @force_list
end

#forcesObject

Returns the value of attribute forces.



6
7
8
# File 'lib/pongo/abstract_particle.rb', line 6

def forces
  @forces
end

#frictionObject

Returns the value of attribute friction.



7
8
9
# File 'lib/pongo/abstract_particle.rb', line 7

def friction
  @friction
end

#intervalObject

Returns the value of attribute interval.



6
7
8
# File 'lib/pongo/abstract_particle.rb', line 6

def interval
  @interval
end

#inv_massObject

Returns the value of attribute inv_mass.



7
8
9
# File 'lib/pongo/abstract_particle.rb', line 7

def inv_mass
  @inv_mass
end

#kfrObject

Returns the value of attribute kfr.



7
8
9
# File 'lib/pongo/abstract_particle.rb', line 7

def kfr
  @kfr
end

#massObject

Returns the value of attribute mass.



7
8
9
# File 'lib/pongo/abstract_particle.rb', line 7

def mass
  @mass
end

#multisampleObject

Returns the value of attribute multisample.



7
8
9
# File 'lib/pongo/abstract_particle.rb', line 7

def multisample
  @multisample
end

#prevObject

Returns the value of attribute prev.



6
7
8
# File 'lib/pongo/abstract_particle.rb', line 6

def prev
  @prev
end

#sampObject

Returns the value of attribute samp.



6
7
8
# File 'lib/pongo/abstract_particle.rb', line 6

def samp
  @samp
end

#tempObject

Returns the value of attribute temp.



6
7
8
# File 'lib/pongo/abstract_particle.rb', line 6

def temp
  @temp
end

Instance Method Details

#accumulate_forcesObject

Accumulates both the particle forces and the global forces



210
211
212
213
214
# File 'lib/pongo/abstract_particle.rb', line 210

def accumulate_forces
  @force_list.each {|f| @forces.plus!(f.get_value(@inv_mass))}
  APEngine.forces.each {|f| @forces.plus!(f.get_value(@inv_mass))}
  @forces
end

#add_force(force) ⇒ Object

Adds a force to the particle. Using this method to a force directly to the particle will only apply that force for a single APEngine.step() cycle.



150
151
152
# File 'lib/pongo/abstract_particle.rb', line 150

def add_force(force)
  @force_list << force
end

#clear_forcesObject

Clears out all forces on the particle



217
218
219
220
# File 'lib/pongo/abstract_particle.rb', line 217

def clear_forces
  @force_list.clear
  @forces.set_to(0, 0)
end

#collidable!Object



144
145
146
# File 'lib/pongo/abstract_particle.rb', line 144

def collidable!
  @collidable = true
end

#collidable?Boolean

Determines if the particle can collide with other particles or constraints. The default state is true.



140
141
142
# File 'lib/pongo/abstract_particle.rb', line 140

def collidable?
  @collidable
end

#components(collision_normal) ⇒ Object Also known as: get_components



176
177
178
179
180
181
182
# File 'lib/pongo/abstract_particle.rb', line 176

def components(collision_normal)
  vel = velocity
  vdotn = collision_normal.dot(vel)
  @collision.vn = collision_normal * vdotn
  @collision.vt = vel - collision.vn
  @collision
end

#elasticityObject

The elasticity of the particle. Standard values are between 0 and 1. The higher the value, the greater the elasticity.

During collisions the elasticity values are combined. If one particle’s elasticity is set to 0.4 and the other is set to 0.4 then the collision will be have a total elasticity of 0.8. The result will be the same if one particle has an elasticity of 0 and the other 0.8.

Setting the elasticity to greater than 1 (of a single particle, or in a combined collision) will cause particles to bounce with energy greater than naturally possible.



47
48
49
# File 'lib/pongo/abstract_particle.rb', line 47

def elasticity
  @kfr
end

#elasticity=(k) ⇒ Object



51
52
53
# File 'lib/pongo/abstract_particle.rb', line 51

def elasticity=(k)
  @kfr = k
end

#fixed?Boolean



76
77
78
# File 'lib/pongo/abstract_particle.rb', line 76

def fixed?
  @fixed
end

#positionObject

The position of the particle. Getting the position of the particle is useful for drawing it or testing it for some custom purpose.

<p> When you get the position of a particle you are given a copy of the current location. Because of this you cannot change the position of a particle by altering the x and y components of the Vector you have retrieved from the position property. You have to do something instead like: position = new Vector(100,100), or you can use the px and py properties instead. </p>

<p> You can alter the position of a particle three ways: change its position, set its velocity, or apply a force to it. Setting the position of a non-fixed particle is not the same as setting its fixed property to true. A particle held in place by its position will behave as if it’s attached there by a 0 length spring constraint.



98
99
100
# File 'lib/pongo/abstract_particle.rb', line 98

def position
  @curr.dup
end

#position=(p) ⇒ Object



102
103
104
105
# File 'lib/pongo/abstract_particle.rb', line 102

def position=(p)
  @curr.copy(p)
  @prev.copy(p)
end

#pxObject



107
108
109
# File 'lib/pongo/abstract_particle.rb', line 107

def px
  @curr.x
end

#px=(x) ⇒ Object



111
112
113
114
# File 'lib/pongo/abstract_particle.rb', line 111

def px=(x)
  @curr.x = x
  @prev.x = x
end

#pyObject



116
117
118
# File 'lib/pongo/abstract_particle.rb', line 116

def py
  @curr.y
end

#py=(y) ⇒ Object



120
121
122
123
# File 'lib/pongo/abstract_particle.rb', line 120

def py=(y)
  @curr.y = y
  @prev.y = y
end

#reset_first_collision!Object Also known as: reset_first_collision

Resets the collision state of the particle. This value is used in conjuction with the CollisionEvent.FIRST_COLLISION event.



171
172
173
# File 'lib/pongo/abstract_particle.rb', line 171

def reset_first_collision!
  @first_collision = false
end

#resolve_collision(mtd, vel, n, d, order, particle) ⇒ Object

Make sure to align the overriden versions of this method in WheelParticle



187
188
189
190
191
192
193
# File 'lib/pongo/abstract_particle.rb', line 187

def resolve_collision(mtd, vel, n, d, order, particle)
  test_particle_events(particle)
  return if fixed? or (not solid?) or (not particle.solid?)
  @curr.copy(@samp)
  @curr.plus!(mtd)
  self.velocity = vel
end

#solid?Boolean



222
223
224
# File 'lib/pongo/abstract_particle.rb', line 222

def solid?
  @solid
end

#test_particle_events(p) ⇒ Object



195
196
197
198
199
200
201
202
203
# File 'lib/pongo/abstract_particle.rb', line 195

def test_particle_events(p)
  if has_event_listener(CollisionEvent::COLLIDE)
    dispatch_event(CollisionEvent.new(CollisionEvent::COLLIDE, p))
  end
  if has_event_listener(CollisionEvent::FIRST_COLLIDE) and not @first_collision
    @first_collision = true
    dispatch_event(CollisionEvent.new(CollisionEvent::FIRST_COLLIDE, p))
  end
end

#update(dt2) ⇒ Object

The update() method is called automatically during the APEngine.step() cycle. This method integrates the particle.



156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/pongo/abstract_particle.rb', line 156

def update(dt2)
  return if fixed?

  accumulate_forces

  @temp.copy(@curr)
  nv = velocity + @forces.mult!(dt2)
  @curr.plus!(nv.mult!(APEngine.damping))
  @prev.copy(@temp)

  clear_forces
end

#velocityObject

The velocity of the particle. If you need to change the motion of a particle, you should either use this property, or one of the addForce methods. Generally, the addForce methods are best for slowly altering the motion. The velocity property is good for instantaneously setting the velocity, e.g., for projectiles.



130
131
132
# File 'lib/pongo/abstract_particle.rb', line 130

def velocity
  @curr - @prev
end

#velocity=(v) ⇒ Object



134
135
136
# File 'lib/pongo/abstract_particle.rb', line 134

def velocity=(v)
  @prev = @curr - v
end