Class: BulldogPhysics::Particles::Generators::ParticleSpring

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

Instance Attribute Summary collapse

Attributes inherited from ParticleForceGenerator

#registrations

Instance Method Summary collapse

Constructor Details

#initialize(other, spring_const, rest_len) ⇒ ParticleSpring

Returns a new instance of ParticleSpring.



12
13
14
15
16
# File 'lib/Particles/particle_spring.rb', line 12

def initialize(other, spring_const, rest_len)
  @other_particle = other
  @spring_constant = spring_const
  @rest_length = rest_len
end

Instance Attribute Details

#other_particleObject

particle at other end of the spring



7
8
9
# File 'lib/Particles/particle_spring.rb', line 7

def other_particle
  @other_particle
end

#rest_lengthObject

rest length of the string



9
10
11
# File 'lib/Particles/particle_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_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
34
35
# File 'lib/Particles/particle_spring.rb', line 18

def update_force(particle, duration)

  #calculate vector of the spring
  force = particle.position.dup

  force.subtractVector(@other_particle.position)
  # calculate magnitude of the force
  magnitude = force.magnitude
  magnitude = (magnitude - @rest_length).abs
  magnitude *= @spring_constant
  
  #calculate final force and apply it
  force.normalize

  force.multiplyByScalar(-magnitude)
  particle.addForce(force)
  
end