use crate::{dynamics::solver::xpbd::*, prelude::*};
use bevy::{
ecs::{
entity::{EntityMapper, MapEntities},
reflect::ReflectMapEntities,
},
prelude::*,
};
#[derive(Component, Clone, Copy, Debug, PartialEq, Reflect)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serialize", reflect(Serialize, Deserialize))]
#[reflect(Debug, Component, MapEntities, PartialEq)]
pub struct DistanceJoint {
pub entity1: Entity,
pub entity2: Entity,
pub local_anchor1: Vector,
pub local_anchor2: Vector,
pub rest_length: Scalar,
pub length_limits: Option<DistanceLimit>,
pub damping_linear: Scalar,
pub damping_angular: Scalar,
pub lagrange: Scalar,
pub compliance: Scalar,
pub force: Vector,
}
impl XpbdConstraint<2> for DistanceJoint {
fn entities(&self) -> [Entity; 2] {
[self.entity1, self.entity2]
}
fn clear_lagrange_multipliers(&mut self) {
self.lagrange = 0.0;
}
fn solve(&mut self, bodies: [&mut RigidBodyQueryItem; 2], dt: Scalar) {
self.force = self.constrain_length(bodies, dt);
}
}
impl Joint for DistanceJoint {
fn new(entity1: Entity, entity2: Entity) -> Self {
Self {
entity1,
entity2,
local_anchor1: Vector::ZERO,
local_anchor2: Vector::ZERO,
rest_length: 0.0,
length_limits: None,
damping_linear: 0.0,
damping_angular: 0.0,
lagrange: 0.0,
compliance: 0.0,
force: Vector::ZERO,
}
}
fn with_compliance(self, compliance: Scalar) -> Self {
Self { compliance, ..self }
}
fn with_local_anchor_1(self, anchor: Vector) -> Self {
Self {
local_anchor1: anchor,
..self
}
}
fn with_local_anchor_2(self, anchor: Vector) -> Self {
Self {
local_anchor2: anchor,
..self
}
}
fn with_linear_velocity_damping(self, damping: Scalar) -> Self {
Self {
damping_linear: damping,
..self
}
}
fn with_angular_velocity_damping(self, damping: Scalar) -> Self {
Self {
damping_angular: damping,
..self
}
}
fn local_anchor_1(&self) -> Vector {
self.local_anchor1
}
fn local_anchor_2(&self) -> Vector {
self.local_anchor2
}
fn damping_linear(&self) -> Scalar {
self.damping_linear
}
fn damping_angular(&self) -> Scalar {
self.damping_angular
}
}
impl DistanceJoint {
fn constrain_length(&mut self, bodies: [&mut RigidBodyQueryItem; 2], dt: Scalar) -> Vector {
let [body1, body2] = bodies;
let world_r1 = *body1.rotation * self.local_anchor1;
let world_r2 = *body2.rotation * self.local_anchor2;
let limits = self
.length_limits
.unwrap_or(DistanceLimit::new(self.rest_length, self.rest_length));
let (dir, distance) = limits.compute_correction(
body1.current_position() + world_r1,
body2.current_position() + world_r2,
);
if distance.abs() < Scalar::EPSILON {
return Vector::ZERO;
}
let w1 = PositionConstraint::compute_generalized_inverse_mass(self, body1, world_r1, dir);
let w2 = PositionConstraint::compute_generalized_inverse_mass(self, body2, world_r2, dir);
let w = [w1, w2];
let delta_lagrange =
self.compute_lagrange_update(self.lagrange, distance, &w, self.compliance, dt);
self.lagrange += delta_lagrange;
self.apply_positional_lagrange_update(
body1,
body2,
delta_lagrange,
dir,
world_r1,
world_r2,
);
self.compute_force(self.lagrange, dir, dt)
}
pub fn with_limits(self, min: Scalar, max: Scalar) -> Self {
Self {
length_limits: Some(DistanceLimit::new(min, max)),
..self
}
}
pub fn with_rest_length(self, rest_length: Scalar) -> Self {
Self {
rest_length,
..self
}
}
}
impl PositionConstraint for DistanceJoint {}
impl AngularConstraint for DistanceJoint {}
impl MapEntities for DistanceJoint {
fn map_entities<M: EntityMapper>(&mut self, entity_mapper: &mut M) {
self.entity1 = entity_mapper.map_entity(self.entity1);
self.entity2 = entity_mapper.map_entity(self.entity2);
}
}