summaryrefslogtreecommitdiff
path: root/src/constraint
diff options
context:
space:
mode:
Diffstat (limited to 'src/constraint')
-rw-r--r--src/constraint/slider.rs33
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>) {