Class: BulldogPhysics::Particles::Generators::ParticleAnchoredSpring
- Inherits:
-
ParticleForceGenerator
- Object
- ParticleForceGenerator
- BulldogPhysics::Particles::Generators::ParticleAnchoredSpring
- Defined in:
- lib/Particles/particle_anchored_spring.rb
Instance Attribute Summary collapse
-
#anchor ⇒ Object
location of the anchored end of the spring.
-
#rest_length ⇒ Object
rest length of the string.
-
#spring_constant ⇒ Object
Returns the value of attribute spring_constant.
Attributes inherited from ParticleForceGenerator
Instance Method Summary collapse
-
#initialize(anchor, spring_const, rest_len) ⇒ ParticleAnchoredSpring
constructor
A new instance of ParticleAnchoredSpring.
- #update_force(particle, duration) ⇒ Object
Constructor Details
#initialize(anchor, spring_const, rest_len) ⇒ ParticleAnchoredSpring
Returns a new instance of ParticleAnchoredSpring.
12 13 14 15 16 |
# File 'lib/Particles/particle_anchored_spring.rb', line 12 def initialize(anchor, spring_const, rest_len) @anchor = anchor @spring_constant = spring_const @rest_length = rest_len end |
Instance Attribute Details
#anchor ⇒ Object
location of the anchored end of the spring
7 8 9 |
# File 'lib/Particles/particle_anchored_spring.rb', line 7 def anchor @anchor end |
#rest_length ⇒ Object
rest length of the string
9 10 11 |
# File 'lib/Particles/particle_anchored_spring.rb', line 9 def rest_length @rest_length end |
#spring_constant ⇒ Object
Returns the value of attribute spring_constant.
8 9 10 |
# File 'lib/Particles/particle_anchored_spring.rb', line 8 def spring_constant @spring_constant end |
Instance Method Details
#update_force(particle, duration) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/Particles/particle_anchored_spring.rb', line 18 def update_force(particle, duration) #calculate vector of the spring force = particle.position.dup force = force.subtractVector(@anchor) # calculate magnitude of the force magnitude = force.magnitude magnitude = (@rest_length - magnitude) * @spring_constant magnitude *= @spring_constant #calculate final force and apply it force = force.unit force.multiplyByScalar(magnitude) particle.addForce(force) end |