diff options
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() + } +} |