diff options
author | eug-vs <eugene@eug-vs.xyz> | 2024-12-14 18:35:30 +0100 |
---|---|---|
committer | eug-vs <eugene@eug-vs.xyz> | 2024-12-14 18:35:30 +0100 |
commit | bd9424635e997ce70504b564a3561506ee6d51df (patch) | |
tree | 57f22bf9a98798eb71ba365832023a63b32739fa /src/constraint | |
parent | 969507fed807e01b876b05fe0cd00a0e143e9a0d (diff) | |
download | particle-physics-bd9424635e997ce70504b564a3561506ee6d51df.tar.gz |
refactor: add Line struct
Diffstat (limited to 'src/constraint')
-rw-r--r-- | src/constraint/slider.rs | 33 |
1 files changed, 13 insertions, 20 deletions
diff --git a/src/constraint/slider.rs b/src/constraint/slider.rs index 526b6f8..ceea4ea 100644 --- a/src/constraint/slider.rs +++ b/src/constraint/slider.rs @@ -2,18 +2,18 @@ use std::usize; use nalgebra::{DVector, RowDVector}; -use crate::{particle_system::{Scalar, N}, ParticleSystem, Point, Vector}; +use crate::{ + algebra::line::Line, + particle_system::{Scalar, N}, + ParticleSystem, Point, Vector, +}; use super::Constraint; - pub struct SliderConstraint { pub particle_id: usize, - pub point: Point, - - /// Has to be normalized - pub dir: Vector, + pub line: Line, jacobian: RowDVector<Scalar>, } @@ -21,15 +21,11 @@ pub struct SliderConstraint { impl ParticleSystem { pub fn add_slider_constraint(&mut self, particle_id: usize, direction: Vector) { let point = self.particles[particle_id].position; - self.constraints.push(Box::new( - SliderConstraint { - particle_id, - point, - dir: direction.normalize(), - jacobian: RowDVector::zeros(self.particles.len() * N), - } - )); - + self.constraints.push(Box::new(SliderConstraint { + particle_id, + line: Line::new(point, direction), + jacobian: RowDVector::zeros(self.particles.len() * N), + })); } } @@ -40,11 +36,8 @@ impl Constraint for SliderConstraint { fn c(&self, q: &DVector<Scalar>) -> Scalar { let position = q.fixed_rows::<N>(self.particle_id * N); - let d = position - self.point.coords; - let lambda = d.dot(&self.dir); - let distance_to_line = d - lambda * self.dir; - - distance_to_line.norm() + let point = Point::from_coordinates(position.into()); + self.line.distance_to_point(point) } fn set_jacobian(&mut self, jacobian: RowDVector<Scalar>) { |