From 70afc5a7d871919776a64782e8b93404e6b0defd Mon Sep 17 00:00:00 2001 From: eug-vs Date: Sun, 15 Dec 2024 13:17:43 +0100 Subject: feat!: add raylib rendering --- physics/src/constraint/anchor.rs | 43 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 physics/src/constraint/anchor.rs (limited to 'physics/src/constraint/anchor.rs') diff --git a/physics/src/constraint/anchor.rs b/physics/src/constraint/anchor.rs new file mode 100644 index 0000000..c90cfb2 --- /dev/null +++ b/physics/src/constraint/anchor.rs @@ -0,0 +1,43 @@ +use nalgebra::{DVector, RowDVector}; + +use crate::particle_system::ParticleSystem; +use crate::algebra::{Point, Scalar, N}; + +use super::Constraint; + +pub struct AnchorConstraint { + pub particle_id: usize, + pub anchor: Point, + + jacobian: RowDVector, +} + +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 { + vec![self.particle_id] + } + + fn c(&self, q: &DVector) -> Scalar { + let position = q.fixed_rows(self.particle_id * N); + (position - self.anchor.coords).norm() + } + + fn set_jacobian(&mut self, jacobian: RowDVector) { + self.jacobian = jacobian + } + + fn jacobian_prev(&self) -> RowDVector { + self.jacobian.clone() + } +} -- cgit v1.2.3