diff options
author | eug-vs <eugene@eug-vs.xyz> | 2024-12-15 13:17:43 +0100 |
---|---|---|
committer | eug-vs <eugene@eug-vs.xyz> | 2024-12-15 13:17:43 +0100 |
commit | 70afc5a7d871919776a64782e8b93404e6b0defd (patch) | |
tree | c3d8a273bddf4cbc3c55d06c751766b93b961a1f /physics/src/constraint/beam.rs | |
parent | 297efa5127e83bea57132c503680dd348a725db5 (diff) | |
download | particle-physics-70afc5a7d871919776a64782e8b93404e6b0defd.tar.gz |
feat!: add raylib rendering
Diffstat (limited to 'physics/src/constraint/beam.rs')
-rw-r--r-- | physics/src/constraint/beam.rs | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/physics/src/constraint/beam.rs b/physics/src/constraint/beam.rs new file mode 100644 index 0000000..14b1c1f --- /dev/null +++ b/physics/src/constraint/beam.rs @@ -0,0 +1,47 @@ +use nalgebra::{DVector, RowDVector}; + +use crate::particle_system::ParticleSystem; +use crate::algebra::{Scalar, N}; + +use super::Constraint; + +pub struct BeamConstraint { + pub particle_ids: [usize; 2], + pub length: Scalar, + + jacobian: RowDVector<Scalar>, +} + +impl ParticleSystem { + pub fn add_beam_constraint(&mut self, particle_ids: [usize; 2]) { + let a = &self.particles[particle_ids[0]]; + let b = &self.particles[particle_ids[1]]; + + self.constraints.push(Box::new(BeamConstraint { + particle_ids, + length: (a.position - b.position).norm(), + jacobian: RowDVector::zeros(self.particles.len() * N), + })); + } +} + +impl Constraint for BeamConstraint { + fn get_particles(&self) -> Vec<usize> { + Vec::from(self.particle_ids) + } + + fn c(&self, q: &DVector<Scalar>) -> Scalar { + let a = q.fixed_rows::<N>(self.particle_ids[0] * N); + let b = q.fixed_rows::<N>(self.particle_ids[1] * N); + + (a - b).norm() - self.length + } + + fn set_jacobian(&mut self, jacobian: RowDVector<Scalar>) { + self.jacobian = jacobian + } + + fn jacobian_prev(&self) -> RowDVector<Scalar> { + self.jacobian.clone() + } +} |