summaryrefslogtreecommitdiff
path: root/physics/src/constraint/beam.rs
blob: 14b1c1f327a8ac35016caf04ae1655b1e4735348 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
use nalgebra::{DVector, RowDVector};

use crate::particle_system::ParticleSystem;
use crate::algebra::{Scalar, N};

use super::Constraint;

pub struct BeamConstraint {
    pub particle_ids: [usize; 2],
    pub length: Scalar,

    jacobian: RowDVector<Scalar>,
}

impl ParticleSystem {
    pub fn add_beam_constraint(&mut self, particle_ids: [usize; 2]) {
        let a = &self.particles[particle_ids[0]];
        let b = &self.particles[particle_ids[1]];

        self.constraints.push(Box::new(BeamConstraint {
            particle_ids,
            length: (a.position - b.position).norm(),
            jacobian: RowDVector::zeros(self.particles.len() * N),
        }));
    }
}

impl Constraint for BeamConstraint {
    fn get_particles(&self) -> Vec<usize> {
        Vec::from(self.particle_ids)
    }

    fn c(&self, q: &DVector<Scalar>) -> Scalar {
        let a = q.fixed_rows::<N>(self.particle_ids[0] * N);
        let b = q.fixed_rows::<N>(self.particle_ids[1] * N);

        (a - b).norm() - self.length
    }

    fn set_jacobian(&mut self, jacobian: RowDVector<Scalar>) {
        self.jacobian = jacobian
    }

    fn jacobian_prev(&self) -> RowDVector<Scalar> {
        self.jacobian.clone()
    }
}