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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
use super::XpbdConstraint;
use crate::prelude::*;
/// An angular constraint applies an angular correction around a given axis.
pub trait AngularConstraint: XpbdConstraint<2> {
/// Applies an angular correction to two bodies.
///
/// Returns the angular impulse that is applied proportional
/// to the inverse moments of inertia of the bodies.
#[cfg(feature = "2d")]
fn apply_angular_lagrange_update(
&self,
body1: &mut RigidBodyQueryItem,
body2: &mut RigidBodyQueryItem,
delta_lagrange: Scalar,
) -> Scalar {
if delta_lagrange.abs() <= Scalar::EPSILON {
return 0.0;
}
self.apply_angular_impulse(body1, body2, -delta_lagrange)
}
/// Applies an angular impulse to two bodies.
///
/// Returns the impulse that is applied proportional
/// to the inverse moments of inertia of the bodies.
#[cfg(feature = "2d")]
fn apply_angular_impulse(
&self,
body1: &mut RigidBodyQueryItem,
body2: &mut RigidBodyQueryItem,
impulse: Scalar,
) -> Scalar {
let inv_inertia1 = body1.effective_world_inv_inertia();
let inv_inertia2 = body2.effective_world_inv_inertia();
// Apply rotational updates
if body1.rb.is_dynamic() && body1.dominance() <= body2.dominance() {
let delta_angle = Self::get_delta_rot(*body1.rotation, inv_inertia1, impulse);
*body1.rotation = body1.rotation.add_angle(delta_angle);
}
if body2.rb.is_dynamic() && body2.dominance() <= body1.dominance() {
let delta_angle = Self::get_delta_rot(*body2.rotation, inv_inertia2, -impulse);
*body2.rotation = body2.rotation.add_angle(delta_angle);
}
impulse
}
/// Applies an angular correction to two bodies.
///
/// Returns the angular impulse that is applied proportional
/// to the inverse moments of inertia of the bodies.
#[cfg(feature = "3d")]
fn apply_angular_lagrange_update(
&self,
body1: &mut RigidBodyQueryItem,
body2: &mut RigidBodyQueryItem,
delta_lagrange: Scalar,
axis: Vector,
) -> Vector {
if delta_lagrange.abs() <= Scalar::EPSILON {
return Vector::ZERO;
}
let impulse = -delta_lagrange * axis;
self.apply_angular_impulse(body1, body2, impulse)
}
/// Applies an angular impulse to two bodies.
///
/// Returns the impulse that is applied proportional
/// to the inverse moments of inertia of the bodies.
#[cfg(feature = "3d")]
fn apply_angular_impulse(
&self,
body1: &mut RigidBodyQueryItem,
body2: &mut RigidBodyQueryItem,
impulse: Vector,
) -> Vector {
let inv_inertia1 = body1.effective_world_inv_inertia();
let inv_inertia2 = body2.effective_world_inv_inertia();
// Apply rotational updates
if body1.rb.is_dynamic() {
// In 3D, adding quaternions can result in unnormalized rotations,
// which causes stability issues (see #235) and panics when trying to rotate unit vectors.
// TODO: It would be nice to avoid normalization if possible.
// Maybe the math above can be done in a way that keeps rotations normalized?
let delta_quat = Self::get_delta_rot(*body1.rotation, inv_inertia1, impulse);
body1.rotation.0 = (body1.rotation.0 + delta_quat).normalize();
}
if body2.rb.is_dynamic() {
// See comments for `body1` above.
let delta_quat = Self::get_delta_rot(*body2.rotation, inv_inertia2, -impulse);
body2.rotation.0 = (body2.rotation.0 + delta_quat).normalize();
}
impulse
}
/// Applies an angular correction that aligns the orientation of the bodies.
///
/// Returns the torque exerted by the alignment.
#[cfg(feature = "2d")]
fn align_orientation(
&self,
body1: &mut RigidBodyQueryItem,
body2: &mut RigidBodyQueryItem,
angle: Scalar,
lagrange: &mut Scalar,
compliance: Scalar,
dt: Scalar,
) -> Torque {
if angle.abs() <= Scalar::EPSILON {
return Torque::ZERO;
}
let w1 = body1.effective_world_inv_inertia();
let w2 = body2.effective_world_inv_inertia();
let w = [w1, w2];
// Compute Lagrange multiplier update
let delta_lagrange = self.compute_lagrange_update(*lagrange, angle, &w, compliance, dt);
*lagrange += delta_lagrange;
// Apply angular correction to aling the bodies
self.apply_angular_lagrange_update(body1, body2, delta_lagrange);
// Return constraint torque
self.compute_torque(delta_lagrange, dt)
}
/// Applies an angular correction that aligns the orientation of the bodies.
///
/// Returns the torque exerted by the alignment.
#[cfg(feature = "3d")]
fn align_orientation(
&self,
body1: &mut RigidBodyQueryItem,
body2: &mut RigidBodyQueryItem,
rotation_difference: Vector,
lagrange: &mut Scalar,
compliance: Scalar,
dt: Scalar,
) -> Torque {
let angle = rotation_difference.length();
if angle <= Scalar::EPSILON {
return Torque::ZERO;
}
let axis = rotation_difference / angle;
// Compute generalized inverse masses
let w1 = AngularConstraint::compute_generalized_inverse_mass(self, body1, axis);
let w2 = AngularConstraint::compute_generalized_inverse_mass(self, body2, axis);
let w = [w1, w2];
// Compute Lagrange multiplier update
let delta_lagrange = self.compute_lagrange_update(*lagrange, angle, &w, compliance, dt);
*lagrange += delta_lagrange;
// Apply angular correction to aling the bodies
self.apply_angular_lagrange_update(body1, body2, delta_lagrange, axis);
// Return constraint torque
self.compute_torque(delta_lagrange, axis, dt)
}
/// Applies angular constraints for interactions between two bodies.
///
/// Here in 2D, `axis` is a unit vector with the Z coordinate set to 1 or -1. It controls if the body should rotate counterclockwise or clockwise.
///
/// Returns the angular impulse that is applied proportional to the inverse masses of the bodies.
#[cfg(feature = "2d")]
fn apply_angular_correction(
&self,
body1: &mut RigidBodyQueryItem,
body2: &mut RigidBodyQueryItem,
delta_lagrange: Scalar,
axis: Vector3,
) -> Scalar {
if delta_lagrange.abs() <= Scalar::EPSILON {
return 0.0;
}
// Compute angular impulse
// `axis.z` is 1 or -1 and it controls if the body should rotate counterclockwise or clockwise
let p = -delta_lagrange * axis.z;
let inv_inertia1 = body1.effective_world_inv_inertia();
let inv_inertia2 = body2.effective_world_inv_inertia();
// Apply rotational updates
if body1.rb.is_dynamic() && body1.dominance() <= body2.dominance() {
let delta_angle = Self::get_delta_rot(*body1.rotation, inv_inertia1, p);
*body1.rotation = body1.rotation.add_angle(delta_angle);
}
if body2.rb.is_dynamic() && body2.dominance() <= body1.dominance() {
let delta_angle = Self::get_delta_rot(*body2.rotation, inv_inertia2, -p);
*body2.rotation = body2.rotation.add_angle(delta_angle);
}
p
}
/// Applies angular constraints for interactions between two bodies.
///
/// Returns the angular impulse that is applied proportional to the inverse masses of the bodies.
#[cfg(feature = "3d")]
fn apply_angular_correction(
&self,
body1: &mut RigidBodyQueryItem,
body2: &mut RigidBodyQueryItem,
delta_lagrange: Scalar,
axis: Vector,
) -> Vector {
if delta_lagrange.abs() <= Scalar::EPSILON {
return Vector::ZERO;
}
// Compute angular impulse
let p = -delta_lagrange * axis;
let inv_inertia1 = body1.effective_world_inv_inertia();
let inv_inertia2 = body2.effective_world_inv_inertia();
// Apply rotational updates
if body1.rb.is_dynamic() {
// In 3D, adding quaternions can result in unnormalized rotations,
// which causes stability issues (see #235) and panics when trying to rotate unit vectors.
// TODO: It would be nice to avoid normalization if possible.
// Maybe the math above can be done in a way that keeps rotations normalized?
let delta_quat = Self::get_delta_rot(*body1.rotation, inv_inertia1, p);
body1.rotation.0 = (body1.rotation.0 + delta_quat).normalize();
}
if body2.rb.is_dynamic() {
// See comments for `body1` above.
let delta_quat = Self::get_delta_rot(*body2.rotation, inv_inertia2, -p);
body2.rotation.0 = (body2.rotation.0 + delta_quat).normalize();
}
p
}
/// Computes the generalized inverse mass of a body when applying an angular correction
/// around `axis`.
///
/// In 2D, `axis` should only have the z axis set to either -1 or 1 to indicate counterclockwise or
/// clockwise rotation.
#[cfg(feature = "2d")]
fn compute_generalized_inverse_mass(&self, body: &RigidBodyQueryItem, axis: Vector3) -> Scalar {
if body.rb.is_dynamic() {
axis.dot(body.inverse_inertia.0 * axis)
} else {
// Static and kinematic bodies are a special case, where 0.0 can be thought of as infinite mass.
0.0
}
}
/// Computes the generalized inverse mass of a body when applying an angular correction
/// around `axis`.
#[cfg(feature = "3d")]
fn compute_generalized_inverse_mass(&self, body: &RigidBodyQueryItem, axis: Vector) -> Scalar {
if body.rb.is_dynamic() {
axis.dot(body.effective_world_inv_inertia() * axis)
} else {
// Static and kinematic bodies are a special case, where 0.0 can be thought of as infinite mass.
0.0
}
}
/// Computes the update in rotation when applying an angular correction `p`.
#[cfg(feature = "2d")]
fn get_delta_rot(_rot: Rotation, inverse_inertia: Scalar, p: Scalar) -> Scalar {
// Equation 8/9 but in 2D
inverse_inertia * p
}
/// Computes the update in rotation when applying an angular correction `p`.
#[cfg(feature = "3d")]
fn get_delta_rot(rot: Rotation, inverse_inertia: Matrix3, p: Vector) -> Quaternion {
// Equation 8/9
Quaternion::from_vec4(0.5 * (inverse_inertia * p).extend(0.0)) * rot.0
}
/// Computes the torque acting along the constraint using the equation `tau = lambda * n / h^2`,
/// where `n` is the Z axis in 2D.
#[cfg(feature = "2d")]
fn compute_torque(&self, lagrange: Scalar, dt: Scalar) -> Torque {
// Eq (17)
lagrange / dt.powi(2)
}
/// Computes the torque acting along the constraint using the equation `tau = lambda * n / h^2`
#[cfg(feature = "3d")]
fn compute_torque(&self, lagrange: Scalar, axis: Vector, dt: Scalar) -> Torque {
// Eq (17)
lagrange * axis / dt.powi(2)
}
}