Class: BulldogPhysics::Particles::Generators::ParticleAnchoredSpring

Inherits:
ParticleForceGenerator show all
Defined in:
lib/Particles/particle_anchored_spring.rb

Instance Attribute Summary collapse

Attributes inherited from ParticleForceGenerator

#registrations

Instance Method Summary collapse

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

#anchorObject

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_lengthObject

rest length of the string



9
10
11
# File 'lib/Particles/particle_anchored_spring.rb', line 9

def rest_length
  @rest_length
end

#spring_constantObject

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