diff options
Diffstat (limited to 'physics/src/force/spring.rs')
-rw-r--r-- | physics/src/force/spring.rs | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/physics/src/force/spring.rs b/physics/src/force/spring.rs new file mode 100644 index 0000000..6c2cf67 --- /dev/null +++ b/physics/src/force/spring.rs @@ -0,0 +1,28 @@ +use crate::algebra::Scalar; + +use super::Force; + +pub struct Spring { + pub particle_ids: [usize; 2], + pub rest_length: Scalar, + pub spring_constant: Scalar, + pub damping_constant: Scalar, +} + +impl Force for Spring { + fn apply(&self, particles: &mut Vec<crate::particle_system::Particle>) { + let a = &particles[self.particle_ids[0]]; + let b = &particles[self.particle_ids[1]]; + let i = a.position - b.position; + let i_dot = a.velocity - b.velocity; + let i_norm = i.norm(); + + let force = -(self.spring_constant * (i_norm - self.rest_length) + + (self.damping_constant * i.dot(&i_dot) / i_norm)) + * i + / i_norm; + + particles[self.particle_ids[0]].apply_force(force); + particles[self.particle_ids[1]].apply_force(-force); + } +} |