summaryrefslogtreecommitdiff
path: root/physics/src/constraint/slider.rs
diff options
context:
space:
mode:
Diffstat (limited to 'physics/src/constraint/slider.rs')
-rw-r--r--physics/src/constraint/slider.rs47
1 files changed, 47 insertions, 0 deletions
diff --git a/physics/src/constraint/slider.rs b/physics/src/constraint/slider.rs
new file mode 100644
index 0000000..fa7e840
--- /dev/null
+++ b/physics/src/constraint/slider.rs
@@ -0,0 +1,47 @@
+use nalgebra::{DVector, RowDVector};
+
+use crate::{
+ algebra::{subspace::Line, Point, Scalar, Vector, N},
+ particle_system::ParticleSystem,
+};
+
+use super::Constraint;
+
+pub struct SliderConstraint {
+ pub particle_id: usize,
+
+ pub line: Line,
+
+ jacobian: RowDVector<Scalar>,
+}
+
+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,
+ line: Line::new(point, [direction]),
+ jacobian: RowDVector::zeros(self.particles.len() * N),
+ }));
+ }
+}
+
+impl Constraint for SliderConstraint {
+ fn get_particles(&self) -> Vec<usize> {
+ vec![self.particle_id]
+ }
+
+ fn c(&self, q: &DVector<Scalar>) -> Scalar {
+ let position = q.fixed_rows::<N>(self.particle_id * N);
+ let point = Point::from_coordinates(position.into());
+ self.line.distance(point)
+ }
+
+ fn set_jacobian(&mut self, jacobian: RowDVector<Scalar>) {
+ self.jacobian = jacobian
+ }
+
+ fn jacobian_prev(&self) -> RowDVector<Scalar> {
+ self.jacobian.clone()
+ }
+}