diff options
author | eug-vs <eugene@eug-vs.xyz> | 2024-12-12 15:23:01 +0100 |
---|---|---|
committer | eug-vs <eugene@eug-vs.xyz> | 2024-12-12 18:14:54 +0100 |
commit | 783070635b568b44b6902bfdc01bdadf12b86bc8 (patch) | |
tree | 89718706dc37d02761bb30f9e0b15e7110cc4e7e /src/constraint/anchor.rs | |
parent | ecdafb45dd9c416cb0810d6687a20ac97e480ac9 (diff) | |
download | particle-physics-783070635b568b44b6902bfdc01bdadf12b86bc8.tar.gz |
feat: implement constraint system and PPM rendering
Diffstat (limited to 'src/constraint/anchor.rs')
-rw-r--r-- | src/constraint/anchor.rs | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/src/constraint/anchor.rs b/src/constraint/anchor.rs new file mode 100644 index 0000000..3ef94ff --- /dev/null +++ b/src/constraint/anchor.rs @@ -0,0 +1,42 @@ +use nalgebra::{DVector, RowDVector}; + +use crate::particle_system::{ParticleSystem, Point, Scalar, N}; + +use super::Constraint; + +pub struct AnchorConstraint { + pub particle_id: usize, + pub anchor: Point, + + jacobian: RowDVector<Scalar>, +} + +impl ParticleSystem { + pub fn add_anchor_constraint(&mut self, particle_id: usize) { + let anchor = self.particles[particle_id].position; + self.constraints.push(Box::new(AnchorConstraint { + particle_id, + anchor, + jacobian: RowDVector::zeros(self.particles.len() * N), + })); + } +} + +impl Constraint for AnchorConstraint { + fn get_particles(&self) -> Vec<usize> { + vec![self.particle_id] + } + + fn c(&self, q: &DVector<Scalar>) -> Scalar { + let position = q.fixed_rows(self.particle_id * N); + (position - self.anchor.coords).norm() + } + + fn set_jacobian(&mut self, jacobian: RowDVector<Scalar>) { + self.jacobian = jacobian + } + + fn jacobian_prev(&self) -> RowDVector<Scalar> { + self.jacobian.clone() + } +} |