Skip to main content

oxiphysics_softbody/constraint/
distanceconstraint_traits.rs

1//! # DistanceConstraint - Trait Implementations
2//!
3//! This module contains trait implementations for `DistanceConstraint`.
4//!
5//! ## Implemented Traits
6//!
7//! - `SoftConstraint`
8//!
9//! 🤖 Generated with [SplitRS](https://github.com/cool-japan/splitrs)
10
11use crate::particle::SoftParticle;
12use oxiphysics_core::math::Real;
13
14use super::functions::SoftConstraint;
15use super::types::DistanceConstraint;
16
17impl SoftConstraint for DistanceConstraint {
18    fn project(&mut self, particles: &mut [SoftParticle], dt_sub: Real) {
19        let w1 = particles[self.i].inverse_mass;
20        let w2 = particles[self.j].inverse_mass;
21        let w_sum = w1 + w2;
22        if w_sum == 0.0 {
23            return;
24        }
25        let diff = particles[self.i].position - particles[self.j].position;
26        let dist = diff.norm();
27        if dist < 1e-12 {
28            return;
29        }
30        let alpha_tilde = self.compliance / (dt_sub * dt_sub);
31        let c = dist - self.rest_length;
32        let delta_lambda = (-c - alpha_tilde * self.lambda) / (w_sum + alpha_tilde);
33        self.lambda += delta_lambda;
34        let correction = diff / dist * delta_lambda;
35        particles[self.i].position += correction * w1;
36        particles[self.j].position -= correction * w2;
37    }
38}