Skip to content

Commit

Permalink
Fixed add_angle (#421)
Browse files Browse the repository at this point in the history
# Objective

Fixes [415](#415) and [416](#416)

## Solution

Old implementation was only for small angles. When impact on `RevoluteJoint` is big, XPBD [tries to apply](https://github.com/Jondolf/avian/blob/main/src/dynamics/solver/xpbd/positional_constraint.rs#L70) a big update on angles and fails

Implemented `add_angle` function using precise computation of sin and cos
  • Loading branch information
hocop authored Jul 13, 2024
1 parent 15e92b6 commit e71de1a
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/position.rs
Original file line number Diff line number Diff line change
Expand Up @@ -407,9 +407,16 @@ impl Rotation {
#[inline]
#[must_use]
/// Adds the given counterclockiwise angle in radians to the [`Rotation`].
/// Uses small-angle approximation
pub fn add_angle(&self, radians: Scalar) -> Self {
Rotation::from_sin_cos(self.sin + radians * self.cos, self.cos - radians * self.sin)
.normalize()
let (sin, cos) = (self.sin + radians * self.cos, self.cos - radians * self.sin);
let magnitude_squared = sin * sin + cos * cos;
let magnitude_recip = if magnitude_squared > 0.0 {
magnitude_squared.sqrt().recip()
} else {
0.0
};
Rotation::from_sin_cos(sin * magnitude_recip, cos * magnitude_recip)
}

/// Performs a linear interpolation between `self` and `rhs` based on
Expand Down

0 comments on commit e71de1a

Please sign in to comment.