aboutsummaryrefslogtreecommitdiff
path: root/src/dynamics/solver/velocity_ground_constraint.rs
diff options
context:
space:
mode:
authorCrozet Sébastien <developer@crozet.re>2021-03-07 11:43:47 +0100
committerCrozet Sébastien <developer@crozet.re>2021-03-07 11:44:19 +0100
commitbed47a82e706a13c22799fcf644753c69fdec6ad (patch)
tree51db7ca364983b29ce31ddb6256e6badaad60c06 /src/dynamics/solver/velocity_ground_constraint.rs
parente7f805aea45612abb655b3c36a133357fecfcdc4 (diff)
downloadrapier-bed47a82e706a13c22799fcf644753c69fdec6ad.tar.gz
rapier-bed47a82e706a13c22799fcf644753c69fdec6ad.tar.bz2
rapier-bed47a82e706a13c22799fcf644753c69fdec6ad.zip
Projection friction impulses on an implicit cone instead of a pyramidal approximation.
Diffstat (limited to 'src/dynamics/solver/velocity_ground_constraint.rs')
-rw-r--r--src/dynamics/solver/velocity_ground_constraint.rs158
1 files changed, 114 insertions, 44 deletions
diff --git a/src/dynamics/solver/velocity_ground_constraint.rs b/src/dynamics/solver/velocity_ground_constraint.rs
index 4e195cd..78afd39 100644
--- a/src/dynamics/solver/velocity_ground_constraint.rs
+++ b/src/dynamics/solver/velocity_ground_constraint.rs
@@ -1,13 +1,42 @@
use super::{AnyVelocityConstraint, DeltaVel};
use crate::math::{AngVector, Real, Vector, DIM, MAX_MANIFOLD_POINTS};
-use crate::utils::{WAngularInertia, WBasis, WCross, WDot};
+#[cfg(feature = "dim2")]
+use crate::utils::WBasis;
+use crate::utils::{WAngularInertia, WCross, WDot};
use crate::dynamics::{IntegrationParameters, RigidBodySet};
use crate::geometry::{ContactManifold, ContactManifoldIndex};
-use simba::simd::SimdPartialOrd;
+#[cfg(feature = "dim2")]
+use na::SimdPartialOrd;
#[derive(Copy, Clone, Debug)]
-pub(crate) struct VelocityGroundConstraintElementPart {
+pub(crate) struct VelocityGroundConstraintTangentPart {
+ pub gcross2: [AngVector<Real>; DIM - 1],
+ pub rhs: [Real; DIM - 1],
+ #[cfg(feature = "dim2")]
+ pub impulse: [Real; DIM - 1],
+ #[cfg(feature = "dim3")]
+ pub impulse: na::Vector2<Real>,
+ pub r: [Real; DIM - 1],
+}
+
+#[cfg(not(target_arch = "wasm32"))]
+impl VelocityGroundConstraintTangentPart {
+ fn zero() -> Self {
+ Self {
+ gcross2: [na::zero(); DIM - 1],
+ rhs: [0.0; DIM - 1],
+ #[cfg(feature = "dim2")]
+ impulse: [0.0; DIM - 1],
+ #[cfg(feature = "dim3")]
+ impulse: na::zero(),
+ r: [0.0; DIM - 1],
+ }
+ }
+}
+
+#[derive(Copy, Clone, Debug)]
+pub(crate) struct VelocityGroundConstraintNormalPart {
pub gcross2: AngVector<Real>,
pub rhs: Real,
pub impulse: Real,
@@ -15,7 +44,7 @@ pub(crate) struct VelocityGroundConstraintElementPart {
}
#[cfg(not(target_arch = "wasm32"))]
-impl VelocityGroundConstraintElementPart {
+impl VelocityGroundConstraintNormalPart {
fn zero() -> Self {
Self {
gcross2: na::zero(),
@@ -28,16 +57,16 @@ impl VelocityGroundConstraintElementPart {
#[derive(Copy, Clone, Debug)]
pub(crate) struct VelocityGroundConstraintElement {
- pub normal_part: VelocityGroundConstraintElementPart,
- pub tangent_part: [VelocityGroundConstraintElementPart; DIM - 1],
+ pub normal_part: VelocityGroundConstraintNormalPart,
+ pub tangent_part: VelocityGroundConstraintTangentPart,
}
#[cfg(not(target_arch = "wasm32"))]
impl VelocityGroundConstraintElement {
pub fn zero() -> Self {
Self {
- normal_part: VelocityGroundConstraintElementPart::zero(),
- tangent_part: [VelocityGroundConstraintElementPart::zero(); DIM - 1],
+ normal_part: VelocityGroundConstraintNormalPart::zero(),
+ tangent_part: VelocityGroundConstraintTangentPart::zero(),
}
}
}
@@ -45,6 +74,10 @@ impl VelocityGroundConstraintElement {
#[derive(Copy, Clone, Debug)]
pub(crate) struct VelocityGroundConstraint {
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 im2: Real,
pub limit: Real,
pub mj_lambda2: usize,
@@ -77,6 +110,12 @@ impl VelocityGroundConstraint {
(-manifold.data.normal, 1.0)
};
+ #[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);
+
let mj_lambda2 = rb2.active_set_offset;
let warmstart_coeff = manifold.data.warmstart_multiplier * params.warmstart_coeff;
@@ -89,6 +128,10 @@ impl VelocityGroundConstraint {
#[cfg(not(target_arch = "wasm32"))]
let mut constraint = VelocityGroundConstraint {
dir1: force_dir1,
+ #[cfg(feature = "dim3")]
+ tangent1: tangents1[0],
+ #[cfg(feature = "dim3")]
+ tangent_rot1,
elements: [VelocityGroundConstraintElement::zero(); MAX_MANIFOLD_POINTS],
im2: rb2.effective_inv_mass,
limit: 0.0,
@@ -166,7 +209,7 @@ impl VelocityGroundConstraint {
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 = VelocityGroundConstraintElementPart {
+ constraint.elements[k].normal_part = VelocityGroundConstraintNormalPart {
gcross2,
rhs,
impulse: manifold_point.data.impulse * warmstart_coeff,
@@ -176,7 +219,12 @@ impl VelocityGroundConstraint {
// 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 gcross2 = rb2
@@ -186,18 +234,10 @@ impl VelocityGroundConstraint {
let rhs = (vel1 - vel2
+ flipped_multiplier * manifold_point.tangent_velocity)
.dot(&tangents1[j]);
- #[cfg(feature = "dim2")]
- let impulse = manifold_points[k].data.tangent_impulse * warmstart_coeff;
- #[cfg(feature = "dim3")]
- let impulse = manifold_points[k].data.tangent_impulse[j] * warmstart_coeff;
-
- constraint.elements[k].tangent_part[j] =
- VelocityGroundConstraintElementPart {
- gcross2,
- rhs,
- impulse,
- r,
- };
+
+ constraint.elements[k].tangent_part.gcross2[j] = gcross2;
+ constraint.elements[k].tangent_part.rhs[j] = rhs;
+ constraint.elements[k].tangent_part.r[j] = r;
}
}
}
@@ -214,6 +254,9 @@ impl VelocityGroundConstraint {
pub fn warmstart(&self, mj_lambdas: &mut [DeltaVel<Real>]) {
let mut mj_lambda2 = DeltaVel::zero();
+ #[cfg(feature = "dim3")]
+ let tangents1 = [self.tangent1, self.dir1.cross(&self.tangent1)];
+ #[cfg(feature = "dim2")]
let tangents1 = self.dir1.orthonormal_basis();
for i in 0..self.num_contacts as usize {
@@ -222,9 +265,9 @@ impl VelocityGroundConstraint {
mj_lambda2.angular += elt.gcross2 * elt.impulse;
for j in 0..DIM - 1 {
- let elt = &self.elements[i].tangent_part[j];
- mj_lambda2.linear += tangents1[j] * (-self.im2 * elt.impulse);
- mj_lambda2.angular += elt.gcross2 * elt.impulse;
+ let elt = &self.elements[i].tangent_part;
+ mj_lambda2.linear += tangents1[j] * (-self.im2 * elt.impulse[j]);
+ mj_lambda2.angular += elt.gcross2[j] * elt.impulse[j];
}
}
@@ -236,23 +279,51 @@ impl VelocityGroundConstraint {
let mut mj_lambda2 = mj_lambdas[self.mj_lambda2 as usize];
// Solve friction.
+ #[cfg(feature = "dim3")]
+ let bitangent1 = self.dir1.cross(&self.tangent1);
+ #[cfg(feature = "dim2")]
let tangents1 = self.dir1.orthonormal_basis();
+ #[cfg(feature = "dim2")]
for i in 0..self.num_contacts as usize {
- 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_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_lambda2.linear += tangents1[j] * (-self.im2 * dlambda);
- mj_lambda2.angular += elt.gcross2 * dlambda;
- }
+ let normal_elt = &self.elements[i].normal_part;
+ let elt = &mut self.elements[i].tangent_part;
+ let dimpulse = -tangents1[0].dot(&mj_lambda2.linear)
+ + elt.gcross2[0].gdot(mj_lambda2.angular)
+ + elt.rhs[0];
+ let limit = self.limit * normal_elt.impulse;
+ let new_impulse = (elt.impulse[0] - elt.r[0] * dimpulse).simd_clamp(-limit, limit);
+ let dlambda = new_impulse - elt.impulse[0];
+ elt.impulse[0] = new_impulse;
+
+ mj_lambda2.linear += tangents1[0] * (-self.im2 * dlambda);
+ mj_lambda2.angular += elt.gcross2[0] * dlambda;
+ }
+
+ #[cfg(feature = "dim3")]
+ for i in 0..self.num_contacts as usize {
+ let limit = self.limit * self.elements[i].normal_part.impulse;
+ let elts = &mut self.elements[i].tangent_part;
+
+ let dimpulse_0 = -self.tangent1.dot(&mj_lambda2.linear)
+ + elts.gcross2[0].gdot(mj_lambda2.angular)
+ + elts.rhs[0];
+ let dimpulse_1 = -bitangent1.dot(&mj_lambda2.linear)
+ + elts.gcross2[1].gdot(mj_lambda2.angular)
+ + elts.rhs[1];
+
+ let new_impulse = na::Vector2::new(
+ elts.impulse[0] - elts.r[0] * dimpulse_0,
+ elts.impulse[1] - elts.r[1] * dimpulse_1,
+ );
+ let new_impulse = new_impulse.cap_magnitude(limit);
+ let dlambda = new_impulse - elts.impulse;
+
+ elts.impulse = new_impulse;
+
+ mj_lambda2.linear +=
+ self.tangent1 * (-self.im2 * dlambda[0]) + bitangent1 * (-self.im2 * dlambda[1]);
+ mj_lambda2.angular += elts.gcross2[0] * dlambda[0] + elts.gcross2[1] * dlambda[1];
}
// Solve penetration.
@@ -281,14 +352,13 @@ impl VelocityGroundConstraint {
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);
}
}
}