diff options
| author | Sébastien Crozet <developer@crozet.re> | 2021-03-08 10:09:43 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-03-08 10:09:43 +0100 |
| commit | beaddea6f2596dce936355940fd98ca90945cb65 (patch) | |
| tree | 22a8e6863cce5daab7782c5de108749e5d046fbb /src | |
| parent | e7f805aea45612abb655b3c36a133357fecfcdc4 (diff) | |
| parent | 152ada67ecd122fe38a9cae8b262542b4abf25fc (diff) | |
| download | rapier-beaddea6f2596dce936355940fd98ca90945cb65.tar.gz rapier-beaddea6f2596dce936355940fd98ca90945cb65.tar.bz2 rapier-beaddea6f2596dce936355940fd98ca90945cb65.zip | |
Merge pull request #146 from dimforge/implicit_friction_cone
Projection friction impulses on an implicit cone instead of a pyramid approximation.
Diffstat (limited to 'src')
| -rw-r--r-- | src/dynamics/coefficient_combine_rule.rs | 2 | ||||
| -rw-r--r-- | src/dynamics/solver/delta_vel.rs | 8 | ||||
| -rw-r--r-- | src/dynamics/solver/mod.rs | 4 | ||||
| -rw-r--r-- | src/dynamics/solver/velocity_constraint.rs | 232 | ||||
| -rw-r--r-- | src/dynamics/solver/velocity_constraint_element.rs | 261 | ||||
| -rw-r--r-- | src/dynamics/solver/velocity_constraint_wide.rs | 191 | ||||
| -rw-r--r-- | src/dynamics/solver/velocity_ground_constraint.rs | 160 | ||||
| -rw-r--r-- | src/dynamics/solver/velocity_ground_constraint_element.rs | 202 | ||||
| -rw-r--r-- | src/dynamics/solver/velocity_ground_constraint_wide.rs | 170 | ||||
| -rw-r--r-- | src/geometry/contact_pair.rs | 16 | ||||
| -rw-r--r-- | src/pipeline/physics_hooks.rs | 10 | ||||
| -rw-r--r-- | src/utils.rs | 12 |
12 files changed, 785 insertions, 483 deletions
diff --git a/src/dynamics/coefficient_combine_rule.rs b/src/dynamics/coefficient_combine_rule.rs index 5e8b4a0..2c66888 100644 --- a/src/dynamics/coefficient_combine_rule.rs +++ b/src/dynamics/coefficient_combine_rule.rs @@ -25,7 +25,7 @@ impl CoefficientCombineRule { let effective_rule = rule_value1.max(rule_value2); match effective_rule { - 0 => (coeff1 + coeff1) / 2.0, + 0 => (coeff1 + coeff2) / 2.0, 1 => coeff1.min(coeff2), 2 => coeff1 * coeff2, _ => coeff1.max(coeff2), diff --git a/src/dynamics/solver/delta_vel.rs b/src/dynamics/solver/delta_vel.rs index 378d302..b50cb76 100644 --- a/src/dynamics/solver/delta_vel.rs +++ b/src/dynamics/solver/delta_vel.rs @@ -1,5 +1,6 @@ use crate::math::{AngVector, Vector}; use na::{Scalar, SimdRealField}; +use std::ops::AddAssign; #[derive(Copy, Clone, Debug)] //#[repr(align(64))] @@ -16,3 +17,10 @@ impl<N: SimdRealField> DeltaVel<N> { } } } + +impl<N: SimdRealField> AddAssign for DeltaVel<N> { + fn add_assign(&mut self, rhs: Self) { + self.linear += rhs.linear; + self.angular += rhs.angular; + } +} diff --git a/src/dynamics/solver/mod.rs b/src/dynamics/solver/mod.rs index 090d0f3..33b6f9a 100644 --- a/src/dynamics/solver/mod.rs +++ b/src/dynamics/solver/mod.rs @@ -24,9 +24,11 @@ pub(self) use position_ground_constraint::*; #[cfg(feature = "simd-is-enabled")] pub(self) use position_ground_constraint_wide::*; pub(self) use velocity_constraint::*; +pub(self) use velocity_constraint_element::*; #[cfg(feature = "simd-is-enabled")] pub(self) use velocity_constraint_wide::*; pub(self) use velocity_ground_constraint::*; +pub(self) use velocity_ground_constraint_element::*; #[cfg(feature = "simd-is-enabled")] pub(self) use velocity_ground_constraint_wide::*; @@ -55,9 +57,11 @@ mod position_solver; #[cfg(not(feature = "parallel"))] mod solver_constraints; mod velocity_constraint; +mod velocity_constraint_element; #[cfg(feature = "simd-is-enabled")] mod velocity_constraint_wide; mod velocity_ground_constraint; +mod velocity_ground_constraint_element; #[cfg(feature = "simd-is-enabled")] mod velocity_ground_constraint_wide; #[cfg(not(feature = "parallel"))] diff --git a/src/dynamics/solver/velocity_constraint.rs b/src/dynamics/solver/velocity_constraint.rs index 243d7d7..643facb 100644 --- a/src/dynamics/solver/velocity_constraint.rs +++ b/src/dynamics/solver/velocity_constraint.rs @@ -1,12 +1,12 @@ -use super::DeltaVel; use crate::dynamics::solver::VelocityGroundConstraint; #[cfg(feature = "simd-is-enabled")] use crate::dynamics::solver::{WVelocityConstraint, WVelocityGroundConstraint}; use crate::dynamics::{IntegrationParameters, RigidBodySet}; use crate::geometry::{ContactManifold, ContactManifoldIndex}; -use crate::math::{AngVector, Real, Vector, DIM, MAX_MANIFOLD_POINTS}; +use crate::math::{Real, Vector, DIM, MAX_MANIFOLD_POINTS}; use crate::utils::{WAngularInertia, WBasis, WCross, WDot}; -use simba::simd::SimdPartialOrd; + +use super::{DeltaVel, VelocityConstraintElement, VelocityConstraintNormalPart}; //#[repr(align(64))] #[derive(Copy, Clone, Debug)] @@ -78,46 +78,12 @@ impl AnyVelocityConstraint { } #[derive(Copy, Clone, Debug)] -pub(crate) struct VelocityConstraintElementPart { - pub gcross1: AngVector<Real>, - pub gcross2: AngVector<Real>, - pub rhs: Real, - pub impulse: Real, - pub r: Real, -} - -#[cfg(not(target_arch = "wasm32"))] -impl VelocityConstraintElementPart { - fn zero() -> Self { - Self { - gcross1: na::zero(), - gcross2: na::zero(), - rhs: 0.0, - impulse: 0.0, - r: 0.0, - } - } -} - -#[derive(Copy, Clone, Debug)] -pub(crate) struct VelocityConstraintElement { - pub normal_part: VelocityConstraintElementPart, - pub tangent_part: [VelocityConstraintElementPart; DIM - 1], -} - -#[cfg(not(target_arch = "wasm32"))] -impl VelocityConstraintElement { - pub fn zero() -> Self { - Self { - normal_part: VelocityConstraintElementPart::zero(), - tangent_part: [VelocityConstraintElementPart::zero(); DIM - 1], - } - } -} - -#[derive(Copy, Clone, Debug)] pub(crate) struct VelocityConstraint { pub dir1: Vector<Real>, // Non-penetration force direction for the first body. + #[cfg(feature = "dim3")] + pub tangent1: Vector<Real>, // One of the friction force directions. + #[cfg(feature = "dim3")] + pub tangent_rot1: na::UnitComplex<Real>, // Orientation of the tangent basis wrt. the reference basis. pub im1: Real, pub im2: Real, pub limit: Real, @@ -126,7 +92,7 @@ pub(crate) struct VelocityConstraint { pub manifold_id: ContactManifoldIndex, pub manifold_contact_id: [u8; MAX_MANIFOLD_POINTS], pub num_contacts: u8, - pub elements: [VelocityConstraintElement; MAX_MANIFOLD_POINTS], + pub elements: [VelocityConstraintElement<Real>; MAX_MANIFOLD_POINTS], } impl VelocityConstraint { @@ -156,6 +122,12 @@ impl VelocityConstraint { let force_dir1 = -manifold.data.normal; let warmstart_coeff = manifold.data.warmstart_multiplier * params.warmstart_coeff; + #[cfg(feature = "dim2")] + let tangents1 = force_dir1.orthonormal_basis(); + #[cfg(feature = "dim3")] + let (tangents1, tangent_rot1) = + super::compute_tangent_contact_directions(&force_dir1, &rb1.linvel, &rb2.linvel); + for (_l, manifold_points) in manifold .data .solver_contacts @@ -165,6 +137,10 @@ impl VelocityConstraint { #[cfg(not(target_arch = "wasm32"))] let mut constraint = VelocityConstraint { dir1: force_dir1, + #[cfg(feature = "dim3")] + tangent1: tangents1[0], + #[cfg(feature = "dim3")] + tangent_rot1, elements: [VelocityConstraintElement::zero(); MAX_MANIFOLD_POINTS], im1: rb1.effective_inv_mass, im2: rb2.effective_inv_mass, @@ -203,7 +179,7 @@ impl VelocityConstraint { .as_nongrouped_mut() .unwrap() } else { - unreachable!(); // We don't have parallelization on WASMÂ yet, so this is unreachable. + unreachable!(); // We don't have parallelization on WASM yet, so this is unreachable. }; #[cfg(target_arch = "wasm32")] @@ -254,7 +230,7 @@ impl VelocityConstraint { rhs *= is_bouncy + is_resting * params.velocity_solve_fraction; rhs += is_resting * velocity_based_erp_inv_dt * manifold_point.dist.min(0.0); - constraint.elements[k].normal_part = VelocityConstraintElementPart { + constraint.elements[k].normal_part = VelocityConstraintNormalPart { gcross1, gcross2, rhs, @@ -265,7 +241,12 @@ impl VelocityConstraint { // Tangent parts. { - let tangents1 = force_dir1.orthonormal_basis(); + #[cfg(feature = "dim3")] + let impulse = + tangent_rot1 * manifold_points[k].data.tangent_impulse * warmstart_coeff; + #[cfg(feature = "dim2")] + let impulse = [manifold_points[k].data.tangent_impulse * warmstart_coeff]; + constraint.elements[k].tangent_part.impulse = impulse; for j in 0..DIM - 1 { let gcross1 = rb1 @@ -281,18 +262,11 @@ impl VelocityConstraint { + gcross2.gdot(gcross2)); let rhs = (vel1 - vel2 + manifold_point.tangent_velocity).dot(&tangents1[j]); - #[cfg(feature = "dim2")] - let impulse = manifold_point.data.tangent_impulse * warmstart_coeff; - #[cfg(feature = "dim3")] - let impulse = manifold_point.data.tangent_impulse[j] * warmstart_coeff; - - constraint.elements[k].tangent_part[j] = VelocityConstraintElementPart { - gcross1, - gcross2, - rhs, - impulse, - r, - }; + + constraint.elements[k].tangent_part.gcross1[j] = gcross1; + constraint.elements[k].tangent_part.gcross2[j] = gcross2; + constraint.elements[k].tangent_part.rhs[j] = rhs; + constraint.elements[k].tangent_part.r[j] = r; } } } @@ -311,79 +285,36 @@ impl VelocityConstraint { let mut mj_lambda1 = DeltaVel::zero(); let mut mj_lambda2 = DeltaVel::zero(); - for i in 0..self.num_contacts as usize { - let elt = &self.elements[i].normal_part; - mj_lambda1.linear += self.dir1 * (self.im1 * elt.impulse); - mj_lambda1.angular += elt.gcross1 * elt.impulse; - - mj_lambda2.linear += self.dir1 * (-self.im2 * elt.impulse); - mj_lambda2.angular += elt.gcross2 * elt.impulse; - - // FIXME: move this out of the for loop? - let tangents1 = self.dir1.orthonormal_basis(); - - for j in 0..DIM - 1 { - let elt = &self.elements[i].tangent_part[j]; - mj_lambda1.linear += tangents1[j] * (self.im1 * elt.impulse); - mj_lambda1.angular += elt.gcross1 * elt.impulse; - - mj_lambda2.linear += tangents1[j] * (-self.im2 * elt.impulse); - mj_lambda2.angular += elt.gcross2 * elt.impulse; - } - } - - mj_lambdas[self.mj_lambda1 as usize].linear += mj_lambda1.linear; - mj_lambdas[self.mj_lambda1 as usize].angular += mj_lambda1.angular; - mj_lambdas[self.mj_lambda2 as usize].linear += mj_lambda2.linear; - mj_lambdas[self.mj_lambda2 as usize].angular += mj_lambda2.angular; + VelocityConstraintElement::warmstart_group( + &self.elements[..self.num_contacts as usize], + &self.dir1, + #[cfg(feature = "dim3")] + &self.tangent1, + self.im1, + self.im2, + &mut mj_lambda1, + &mut mj_lambda2, + ); + + mj_lambdas[self.mj_lambda1 as usize] += mj_lambda1; + mj_lambdas[self.mj_lambda2 as usize] += mj_lambda2; } pub fn solve(&mut self, mj_lambdas: &mut [DeltaVel<Real>]) { let mut mj_lambda1 = mj_lambdas[self.mj_lambda1 as usize]; let mut mj_lambda2 = mj_lambdas[self.mj_lambda2 as usize]; - // Solve friction. - for i in 0..self.num_contacts as usize { - let tangents1 = self.dir1.orthonormal_basis(); - - for j in 0..DIM - 1 { - let normal_elt = &self.elements[i].normal_part; - let elt = &mut self.elements[i].tangent_part[j]; - let dimpulse = tangents1[j].dot(&mj_lambda1.linear) - + elt.gcross1.gdot(mj_lambda1.angular) - - tangents1[j].dot(&mj_lambda2.linear) - + elt.gcross2.gdot(mj_lambda2.angular) - + elt.rhs; - let limit = self.limit * normal_elt.impulse; - let new_impulse = (elt.impulse - elt.r * dimpulse).simd_clamp(-limit, limit); - let dlambda = new_impulse - elt.impulse; - elt.impulse = new_impulse; - - mj_lambda1.linear += tangents1[j] * (self.im1 * dlambda); - mj_lambda1.angular += elt.gcross1 * dlambda; - - mj_lambda2.linear += tangents1[j] * (-self.im2 * dlambda); - mj_lambda2.angular += elt.gcross2 * dlambda; - } - } - - // Solve non-penetration. - for i in 0..self.num_contacts as usize { - let elt = &mut self.elements[i].normal_part; - let dimpulse = self.dir1.dot(&mj_lambda1.linear) + elt.gcross1.gdot(mj_lambda1.angular) - - self.dir1.dot(&mj_lambda2.linear) - + elt.gcross2.gdot(mj_lambda2.angular) - + elt.rhs; - let new_impulse = (elt.impulse - elt.r * dimpulse).max(0.0); - let dlambda = new_impulse - elt.impulse; - elt.impulse = new_impulse; - - mj_lambda1.linear += self.dir1 * (self.im1 * dlambda); - mj_lambda1.angular += elt.gcross1 * dlambda; - - mj_lambda2.linear += self.dir1 * (-self.im2 * dlambda); - mj_lambda2.angular += elt.gcross2 * dlambda; - } + VelocityConstraintElement::solve_group( + &mut self.elements[..self.num_contacts as usize], + &self.dir1, + #[cfg(feature = "dim3")] + &self.tangent1, + self.im1, + self.im2, + self.limit, + &mut mj_lambda1, + &mut mj_lambda2, + ); mj_lambdas[self.mj_lambda1 as usize] = mj_lambda1; mj_lambdas[self.mj_lambda2 as usize] = mj_lambda2; @@ -398,15 +329,58 @@ impl VelocityConstraint { active_contact.data.impulse = self.elements[k].normal_part.impulse; #[cfg(feature = "dim2")] { - active_contact.data.tangent_impulse = self.elements[k].tangent_part[0].impulse; + active_contact.data.tangent_impulse = self.elements[k].tangent_part.impulse[0]; } #[cfg(feature = "dim3")] { - active_contact.data.tangent_impulse = [ - self.elements[k].tangent_part[0].impulse, - self.elements[k].tangent_part[1].impulse, - ]; + active_contact.data.tangent_impulse = self + .tangent_rot1 + .inverse_transform_vector(&self.elements[k].tangent_part.impulse); } } } } + +#[inline(always)] +#[cfg(feature = "dim3")] +pub(crate) fn compute_tangent_contact_directions<N>( + force_dir1: &Vector<N>, + linvel1: &Vector<N>, + linvel2: &Vector<N>, +) -> ([Vector<N>; DIM - 1], na::UnitComplex<N>) +where + N: na::SimdRealField, + N::Element: na::RealField, + Vector<N>: WBasis, +{ + use na::SimdValue; + + // Compute the tangent direction. Pick the direction of + // the linear relative velocity, if it is not too small. + // Otherwise use a fallback direction. + let relative_linvel = linvel1 - linvel2; + let mut tangent_relative_linvel = + relative_linvel - force_dir1 * (force_dir1.dot(&relative_linvel)); + let tangent_linvel_norm = tangent_relative_linvel.normalize_mut(); + let threshold: N::Element = na::convert(1.0e-4); + let use_fallback = tangent_linvel_norm.simd_lt(N::splat(threshold)); + let tangent_fallback = force_dir1.orthonormal_vector(); + + let tangent1 = tangent_fallback.select(use_fallback, tangent_relative_linvel); + let bitangent1 = force_dir1.cross(&tangent1); + + // Rotation such that: rot * tangent_fallback = tangent1 + // (when projected in the tangent plane.) This is needed to ensure the + // warmstart impulse has the correct orientation. Indeed, at frame n + 1, + // we need to reapply the same impulse as we did in frame n. However the + // basis on which the tangent impulse is expresses may change at each frame + // (because the the relative linvel may change direction at each frame). + // So we need this rotation to: + // - Project the impulse back to the "reference" basis at after friction is resolved. + // - Project the old impulse on the new basis before the friction is resolved. + let rot = na::UnitComplex::new_unchecked(na::Complex::new( + tangent1.dot(&tangent_fallback), + bitangent1.dot(&tangent_fallback), + )); + ([tangent1, bitangent1], rot) +} diff --git a/src/dynamics/solver/velocity_constraint_element.rs b/src/dynamics/solver/velocity_constraint_element.rs new file mode 100644 index 0000000..1324c35 --- /dev/null +++ b/src/dynamics/solver/velocity_constraint_element.rs @@ -0,0 +1,261 @@ +use super::DeltaVel; +use crate::math::{AngVector, Vector, DIM}; +use crate::utils::{WBasis, WDot}; +use na::SimdRealField; + +#[derive(Copy, Clone, Debug)] +pub(crate) struct VelocityConstraintTangentPart<N: SimdRealField> { + pub gcross1: [AngVector<N>; DIM - 1], + pub gcross2: [AngVector<N>; DIM - 1], + pub rhs: [N; DIM - 1], + #[cfg(feature = "dim2")] + pub impulse: [N; DIM - 1], + #[cfg(feature = "dim3")] + pub impulse: na::Vector2<N>, + pub r: [N; DIM - 1], +} + +impl<N: SimdRealField> VelocityConstraintTangentPart<N> { + #[cfg(not(target_arch = "wasm32"))] + fn zero() -> Self { + Self { + gcross1: [na::zero(); DIM - 1], + gcross2: [na::zero(); DIM - 1], + rhs: [na::zero(); DIM - 1], + #[cfg(feature = "dim2")] + impulse: [na::zero(); DIM - 1], + #[cfg(feature = "dim3")] + impulse: na::zero(), + r: [na::zero(); DIM - 1], + } + } + + #[inline] + pub fn warmstart( + &self, + tangents1: [&Vector<N>; DIM - 1], + im1: N, + im2: N, + mj_lambda1: &mut DeltaVel<N>, + mj_lambda2: &mut DeltaVel<N>, + ) where + AngVector<N>: WDot<AngVector<N>, Result = N>, + N::Element: SimdRealField, + { + for j in 0..DIM - 1 { + mj_lambda1.linear += tangents1[j] * (im1 * self.impulse[j]); + mj_lambda1.angular += self.gcross1[j] * self.impulse[j]; + + mj_lambda2.linear += tangents1[j] * (-im2 * self.impulse[j]); + mj_lambda2.angular += self.gcross2[j] * self.impulse[j]; + } + } + + #[inline] + pub fn solve( + &mut self, + tangents1: [&Vector<N>; DIM - 1], + im1: N, + im2: N, + limit: N, + mj_lambda1: &mut DeltaVel<N>, + mj_lambda2: &mut DeltaVel<N>, + ) where + AngVector<N>: WDot<AngVector<N>, Result = N>, + N::Element: SimdRealField, + { + #[cfg(feature = "dim2")] + { + let dimpulse = tangents1[0].dot(&mj_lambda1.linear) + + self.gcross1[0].gdot(mj_lambda1.angular) + - tangents1[0].dot(&mj_lambda2.linear) + + self.gcross2[0].gdot(mj_lambda2.angular) + + self.rhs[0]; + let new_impulse = (self.impulse[0] - self.r[0] * dimpulse).simd_clamp(-limit, limit); + let dlambda = new_impulse - self.impulse[0]; + self.impulse[0] = new_impulse; + + mj_lambda1.linear += tangents1[0] * (im1 * dlambda); + mj_lambda1.angular += self.gcross1[0] * dlambda; + + mj_lambda2.linear += tangents1[0] * (-im2 * dlambda); + mj_lambda2.angular += self.gcross2[0] * dlambda; + } + + #[cfg(feature = "dim3")] + { + let dimpulse_0 = tangents1[0].dot(&mj_lambda1.linear) + + self.gcross1[0].gdot(mj_lambda1.angular) + - tangents1[0].dot(&mj_lambda2.linear) + + self.gcross2[0].gdot(mj_lambda2.angular) + + self.rhs[0]; + let dimpulse_1 = tangents1[1].dot(&mj_lambda1.linear) + + self.gcross1[1].gdot(mj_lambda1.angular) + - tangents1[1].dot(&mj_lambda2.linear) + + self.gcross2[1].gdot(mj_lambda2.angular) + + self.rhs[1]; + + let new_impulse = na::Vector2::new( + self.impulse[0] - self.r[0] * dimpulse_0, + self.impulse[1] - self.r[1] * dimpulse_1, + ); + let new_impulse = new_impulse.simd_cap_magnitude(limit); + let dlambda = new_impulse - self.impulse; + self.impulse = new_impulse; + + mj_lambda1.linear += + tangents1[0] * (im1 * dlambda[0]) + tangents1[1] * (im1 * dlambda[1]); + mj_lambda1.angular += self.gcross1[0] * dlambda[0] + self.gcross1[1] * dlambda[1]; + + mj_lambda2.linear += + tangents1[0] * (-im2 * dlambda[0]) + tangents1[1] * (-im2 * dlambda[1]); + mj_lambda2.angular += self.gcross2[0] * dlambda[0] + self.gcross2[1] * dlambda[1]; + } + } +} + +#[derive(Copy, Clone, Debug)] +pub(crate) struct VelocityConstraintNormalPart<N: SimdRealField> { + pub gcross1: AngVector<N>, + pub gcross2: AngVector<N>, + pub rhs: N, + pub impulse: N, + pub r: N, +} + +impl<N: SimdRealField> VelocityConstraintNormalPart<N> { + #[cfg(not(target_arch = "wasm32"))] + fn zero() -> Self { + Self { + gcross1: na::zero(), + gcross2: na::zero(), + rhs: na::zero(), + impulse: na::zero(), + r: na::zero(), + } + } + + #[inline] + pub fn warmstart( + &self, + dir1: &Vector<N>, + im1: N, + im2: N, + mj_lambda1: &mut DeltaVel<N>, + mj_lambda2: &mut DeltaVel<N>, + ) where + AngVector<N>: WDot<AngVector<N>, Result = N>, + { + mj_lambda1.linear += dir1 * (im1 * self.impulse); + mj_lambda1.angular += self.gcross1 * self.impulse; + + mj_lambda2.linear += dir1 * (-im2 * self.impulse); + mj_lambda2.angular += self.gcross2 * self.impulse; + } + + #[inline] + pub fn solve( + &mut self, + dir1: &Vector<N>, + im1: N, + im2: N, + mj_lambda1: &mut DeltaVel<N>, + mj_lambda2: &mut DeltaVel<N>, + ) where + AngVector<N>: WDot<AngVector<N>, Result = N>, + { + let dimpulse = dir1.dot(&mj_lambda1.linear) + self.gcross1.gdot(mj_lambda1.angular) + - dir1.dot(&mj_lambda2.linear) + + self.gcross2.gdot(mj_lambda2.angular) + + self.rhs; + let new_impulse = (self.impulse - self.r * dimpulse).simd_max(N::zero()); + let dlambda = new_impulse - self.impulse; + self.impulse = new_impulse; + + mj_lambda1.linear += dir1 * (im1 * dlambda); + mj_lambda1.angular += self.gcross1 * dlambda; + + mj_lambda2.linear += dir1 * (-im2 * dlambda); + mj_lambda2.angular += self.gcross2 * dlambda; + } +} + +#[derive(Copy, Clone, Debug)] +pub(crate) struct VelocityConstraintElement<N: SimdRealField> { + pub normal_part: VelocityConstraintNormalPart<N>, + pub tangent_part: VelocityConstraintTangentPart<N>, +} + +impl<N: SimdRealField> VelocityConstraintElement<N> { + #[cfg(not(target_arch = "wasm32"))] + pub fn zero() -> Self { + Self { + normal_part: VelocityConstraintNormalPart::zero(), + tangent_part: VelocityConstraintTangentPart::zero(), + } + } + + #[inline] + pub fn warmstart_group( + elements: &[Self], + dir1: &Vector<N>, + #[cfg(feature = "dim3")] tangent1: &Vector<N>, + im1: N, + im2: N, + mj_lambda1: &mut DeltaVel<N>, + mj_lambda2: &mut DeltaVel<N>, + ) where + Vector<N>: WBasis, + AngVector<N>: WDot<AngVector<N>, Result = N>, + N::Element: SimdRealField, + { + #[cfg(feature = "dim3")] + let tangents1 = [tangent1, &dir1.cross(&tangent1)]; + #[cfg(feature = "dim2")] + let tangents1 = [&dir1.orthonormal_vector()]; + + for element in elements { + element + .tangent_part + .warmstart(tangents1, im1, im2, mj_lambda1, mj_lambda2); + element + .normal_part + .warmstart(dir1, im1, im2, mj_lambda1, mj_lambda2); + } + } + + #[inline] + pub fn solve_group( + elements: &mut [Self], + dir1: &Vector<N>, + #[cfg(feature = "dim3")] tangent1: &Vector<N>, + im1: N, + im2: N, + limit: N, + mj_lambda1: &mut DeltaVel<N>, + mj_lambda2: &mut DeltaVel<N>, + ) where + Vector<N>: WBasis, + AngVector<N>: WDot<AngVector<N>, Result = N>, + N::Element: SimdRealField, + { + // Solve friction. + #[cfg(feature = "dim3")] + let tangents1 = [tangent1, &dir1.cross(&tangent1)]; + #[cfg(feature = "dim2")] + let tangents1 = [&dir1.orthonormal_vector()]; + + for element in elements.iter_mut() { + let limit = limit * element.normal_part.impulse; + let part = &mut element.tangent_part; + part.solve(tangents1, im1, im2, limit, mj_lambda1, mj_lambda2); + } + + // Solve penetration. + for element in elements.iter_mut() { + element + .normal_part + .solve(&dir1, im1, im2, mj_lambda1, mj_lambda2); + } + } +} diff --git a/src/dynamics/solver/velocity_constraint_wide.rs b/src/dynamics/solver/velocity_constraint_wide.rs index d97602c..673af54 100644 --- a/src/dynamics/solver/velocity_constraint_wide.rs +++ b/src/dynamics/solver/velocity_constraint_wide.rs @@ -1,53 +1,25 @@ -use super::{AnyVelocityConstraint, DeltaVel}; +use super::{ + AnyVelocityConstraint, DeltaVel, VelocityConstraintElement, VelocityConstraintNormalPart, +}; use crate::dynamics::{IntegrationParameters, RigidBodySet}; use crate::geometry::{ContactManifold, ContactManifoldIndex}; use crate::math::{ AngVector, AngularInertia, Point, Real, SimdReal, Vector, DIM, MAX_MANIFOLD_POINTS, SIMD_WIDTH, }; -use crate::utils::{WAngularInertia, WBasis, WCross, WDot}; +#[cfg(feature = "dim2")] +use crate::utils::WBasis; +use crate::utils::{WAngularInertia, WCross, WDot}; use num::Zero; use simba::simd::{SimdPartialOrd, SimdValue}; #[derive(Copy, Clone, Debug)] -pub(crate) struct WVelocityConstraintElementPart { - pub gcross1: AngVector<SimdReal>, - pub gcross2: AngVector<SimdReal>, - pub rhs: SimdReal, - pub impulse: SimdReal, - pub r: SimdReal, -} - -impl WVelocityConstraintElementPart { - pub fn zero() -> Self { - Self { - gcross1: AngVector::zero(), - gcross2: AngVector::zero(), - rhs: SimdReal::zero(), - impulse: SimdReal::zero(), - r: SimdReal::zero(), - } - } -} - -#[derive(Copy, Clone, Debug)] -pub(crate) struct WVelocityConstraintElement { - pub normal_part: WVelocityConstraintElementPart, - pub tangent_parts: [WVelocityConstraintElementPart; DIM - 1], -} - -impl WVelocityConstraintElement { - pub fn zero() -> Self { - Self { - normal_part: WVelocityConstraintElementPart::zero(), - tangent_parts: [WVelocityConstraintElementPart::zero(); DIM - 1], - } - } -} - -#[derive(Copy, Clone, Debug)] pub(crate) struct WVelocityConstraint { pub dir1: Vector<SimdReal>, // Non-penetration force direction for the first body. - pub elements: [WVelocityConstraintElement; MAX_MANIFOLD_POINTS], + #[cfg(feature = "dim3")] + pub tangent1: Vector<SimdReal>, // One of the friction force directions. + #[cfg(feature = "dim3")] + pub tangent_rot1: na::UnitComplex<SimdReal>, // Orientation of the tangent basis wrt. the reference basis. + pub elements: [VelocityConstraintElement<SimdReal>; MAX_MANIFOLD_POINTS], pub num_contacts: u8, pub im1: SimdReal, pub im2: SimdReal, @@ -108,6 +80,12 @@ impl WVelocityConstraint { let warmstart_coeff = warmstart_multiplier * SimdReal::splat(params.warmstart_coeff); let num_active_contacts = manifolds[0].data.num_active_contacts(); + #[cfg(feature = "dim2")] + let tangents1 = force_dir1.orthonormal_basis(); + #[cfg(feature = "dim3")] + let (tangents1, tangent_rot1) = + super::compute_tangent_contact_directions(&force_dir1, &linvel1, &linvel2); + for l in (0..num_active_contacts).step_by(MAX_MANIFOLD_POINTS) { let manifold_points = array![|ii| &manifolds[ii].data.solver_contacts[l..num_active_contacts]; SIMD_WIDTH @@ -116,7 +94,11 @@ impl WVelocityConstraint { let mut constraint = WVelocityConstraint { dir1: force_dir1, - elements: [WVelocityConstraintElement::zero(); MAX_MANIFOLD_POINTS], + #[cfg(feature = "dim3")] + tangent1: tangents1[0], + #[cfg(feature = "dim3")] + tangent_rot1, + elements: [VelocityConstraintElement::zero(); MAX_MANIFOLD_POINTS], im1, im2, limit: SimdReal::splat(0.0), @@ -169,7 +151,7 @@ impl WVelocityConstraint { rhs += dist.simd_min(SimdReal::zero()) * (velocity_based_erp_inv_dt * is_resting); - constraint.elements[k].normal_part = WVelocityConstraintElementPart { + constraint.elements[k].normal_part = VelocityConstraintNormalPart { gcross1, gcross2, rhs, @@ -179,31 +161,30 @@ impl WVelocityConstraint { } // tangent parts. - let tangents1 = force_dir1.orthonormal_basis(); + #[cfg(feature = "dim2")] + let impulse = [SimdReal::from( + array![|ii| manifold_points[ii][k].data.tangent_impulse; SIMD_WIDTH], + )]; - for j in 0..DIM - 1 { - #[cfg(feature = "dim2")] - let impulse = SimdReal::from( + #[cfg(feature = "dim3")] + let impulse = tangent_rot1 + * na::Vector2::from( array![|ii| manifold_points[ii][k].data.tangent_impulse; SIMD_WIDTH], ); - #[cfg(feature = "dim3")] - let impulse = SimdReal::from( - array![|ii| manifold_points[ii][k].data.tangent_impulse[j]; SIMD_WIDTH], - ); + constraint.elements[k].tangent_part.impulse = impulse; + + for j in 0..DIM - 1 { let gcross1 = ii1.transform_vector(dp1.gcross(tangents1[j])); let gcross2 = ii2.transform_vector(dp2.gcross(-tangents1[j])); let r = SimdReal::splat(1.0) / (im1 + im2 + gcross1.gdot(gcross1) + gcross2.gdot(gcross2)); let rhs = (vel1 - vel2 + tangent_velocity).dot(&tangents1[j]); - constraint.elements[k].tangent_parts[j] = WVelocityConstraintElementPart { - gcross1, - gcross2, - rhs, - impulse: impulse * warmstart_coeff, - r, - }; + constraint.elements[k].tangent_part.gcross1[j] = gcross1; + constraint.elements[k].tangent_part.gcross2[j] = gcross2; + constraint.elements[k].tangent_part.rhs[j] = rhs; + constraint.elements[k].tangent_part.r[j] = r; } } @@ -235,26 +216,16 @@ impl WVelocityConstraint { ), }; - for i in 0..self.num_contacts as usize { - let elt = &self.elements[i].normal_part; - mj_lambda1.linear += self.dir1 * (self.im1 * elt.impulse); - mj_lambda1.angular += elt.gcross1 * elt.impulse; - - mj_lambda2.linear += self.dir1 * (-self.im2 * elt.impulse); - mj_lambda2.angular += elt.gcross2 * elt.impulse; - - // FIXME: move this out of the for loop? - let tangents1 = self.dir1.orthonormal_basis(); - - for j in 0..DIM - 1 { - let elt = &self.elements[i].tangent_parts[j]; - mj_lambda1.linear += tangents1[j] * (self.im1 * elt.impulse); - mj_lambda1.angular += elt.gcross1 * elt.impulse; - - mj_lambda2.linear += tangents1[j] * (-self.im2 * elt.impulse); - mj_lambda2.angular += elt.gcross2 * elt.impulse; - } - } + VelocityConstraintElement::warmstart_group( + &self.elements[..self.num_contacts as usize], + &self.dir1, + #[cfg(feature = "dim3")] + &self.tangent1, + self.im1, + self.im2, |
