summaryrefslogtreecommitdiff
path: root/physics/src/force/spring.rs
diff options
context:
space:
mode:
authoreug-vs <eugene@eug-vs.xyz>2024-12-15 13:17:43 +0100
committereug-vs <eugene@eug-vs.xyz>2024-12-15 13:17:43 +0100
commit70afc5a7d871919776a64782e8b93404e6b0defd (patch)
treec3d8a273bddf4cbc3c55d06c751766b93b961a1f /physics/src/force/spring.rs
parent297efa5127e83bea57132c503680dd348a725db5 (diff)
downloadparticle-physics-70afc5a7d871919776a64782e8b93404e6b0defd.tar.gz
feat!: add raylib rendering
Diffstat (limited to 'physics/src/force/spring.rs')
-rw-r--r--physics/src/force/spring.rs28
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);
+ }
+}