use glam::{DMat3, DQuat, DVec3};
use crate::contact::solver::Softness;
const TINY_DETERMINANT: f64 = 1000.0 * f64::MIN_POSITIVE;
pub(super) fn make_soft(hertz: f64, zeta: f64, h: f64) -> Softness {
if hertz == 0.0 {
return Softness {
bias_rate: 0.0,
mass_scale: 0.0,
impulse_scale: 0.0,
};
}
Softness::new(hertz, zeta, h)
}
pub(crate) fn atan2(y: f64, x: f64) -> f64 {
if x == 0.0 && y == 0.0 {
return 0.0;
}
let ax = x.abs();
let ay = y.abs();
let mx = ay.max(ax);
let mn = ay.min(ax);
let a = mn / mx;
let s = a * a;
let c = s * a;
let q = s * s;
let mut r = 0.024_840_285 * q + 0.186_814_18;
let t = -0.094_097_948 * q - 0.332_130_72;
r = r * s + t;
r = r * c + a;
if ay > ax {
r = core::f64::consts::FRAC_PI_2 - r;
}
if x < 0.0 {
r = core::f64::consts::PI - r;
}
if y < 0.0 {
r = -r;
}
r
}
pub(crate) fn twist_angle(q: DQuat) -> f64 {
let twist = if q.w < 0.0 {
atan2(-q.z, -q.w)
} else {
atan2(q.z, q.w)
};
2.0 * twist
}
pub(crate) fn swing_angle(q: DQuat) -> f64 {
let x = (q.z * q.z + q.w * q.w).sqrt();
let y = (q.x * q.x + q.y * q.y).sqrt();
2.0 * atan2(y, x)
}
pub(super) fn inv_mul(q1: DQuat, q2: DQuat) -> DQuat {
q1.conjugate() * q2
}
pub(super) fn delta_rotation(q: DQuat, target: DQuat) -> DVec3 {
let s = if q.dot(target) < 0.0 { -q } else { q };
let diff = DQuat::from_xyzw(
target.x - s.x,
target.y - s.y,
target.z - s.z,
target.w - s.w,
);
let product = diff * s.conjugate();
2.0 * DVec3::new(product.x, product.y, product.z)
}
pub(super) fn skew(v: DVec3) -> DMat3 {
DMat3::from_cols(
DVec3::new(0.0, v.z, -v.y),
DVec3::new(-v.z, 0.0, v.x),
DVec3::new(v.y, -v.x, 0.0),
)
}
pub(super) fn solve3(m: DMat3, a: DVec3) -> DVec3 {
let det = m.determinant();
if det.abs() > TINY_DETERMINANT {
let inv = 1.0 / det;
let sx = m.y_axis.cross(m.z_axis);
let sy = m.z_axis.cross(m.x_axis);
let sz = m.x_axis.cross(m.y_axis);
DVec3::new(inv * sx.dot(a), inv * sy.dot(a), inv * sz.dot(a))
} else {
DVec3::ZERO
}
}
pub(super) fn invert(m: DMat3) -> DMat3 {
let det = m.determinant();
if det.abs() > TINY_DETERMINANT {
let inv = 1.0 / det;
DMat3::from_cols(
m.y_axis.cross(m.z_axis) * inv,
m.z_axis.cross(m.x_axis) * inv,
m.x_axis.cross(m.y_axis) * inv,
)
.transpose()
} else {
DMat3::ZERO
}
}
pub(super) fn is_fixed_rotation(m: DMat3) -> bool {
m.determinant() < TINY_DETERMINANT
}
pub(super) fn solve2(xx: f64, xy: f64, yy: f64, b: [f64; 2]) -> [f64; 2] {
let det = xx * yy - xy * xy;
if det > TINY_DETERMINANT {
let inv = 1.0 / det;
[
inv * yy * b[0] - inv * xy * b[1],
-inv * xy * b[0] + inv * xx * b[1],
]
} else {
[0.0, 0.0]
}
}
pub(super) fn point_mass(ma: f64, mb: f64, ia: DMat3, ib: DMat3, ra: DVec3, rb: DVec3) -> DMat3 {
let sa = skew(ra);
let sb = skew(rb);
let k = -(sa * ia * sa + sb * ib * sb);
k + DMat3::from_diagonal(DVec3::splat(ma + mb))
}
#[cfg(test)]
mod tests {
use super::*;
use crate::integrator::rotation_from_scaled_axis;
#[test]
#[expect(
clippy::disallowed_methods,
reason = "the platform's atan2 is the reference the transcription is measured against"
)]
fn the_arc_tangent_is_within_its_bound() {
let mut worst: f64 = 0.0;
for k in 0..3600 {
let angle = -core::f64::consts::PI + f64::from(k) * core::f64::consts::TAU / 3600.0;
let (y, x) = (crcbl_core::trig::sin(angle), crcbl_core::trig::cos(angle));
for scale in [1.0e-3, 1.0, 7.0e2] {
worst =
worst.max((atan2(y * scale, x * scale) - (y * scale).atan2(x * scale)).abs());
}
}
assert!(worst < 3.0e-5, "worst error {worst:e} rad");
assert!(worst > 0.0, "an exact arc tangent would not need a bound");
assert_eq!(atan2(0.0, 0.0), 0.0);
assert_eq!(atan2(0.0, 1.0), 0.0);
}
#[test]
fn twist_and_swing_read_back_what_made_them() {
for angle in [-2.5, -0.7, 0.0, 0.3, 1.9, 3.0] {
let twist = rotation_from_scaled_axis(DVec3::Z * angle);
assert!((twist_angle(twist) - angle).abs() < 6.0e-5, "{angle}");
assert!(swing_angle(twist).abs() < 1.0e-12, "{angle}");
let swing = rotation_from_scaled_axis(DVec3::new(0.6, 0.8, 0.0) * angle.abs());
assert!((swing_angle(swing) - angle.abs()).abs() < 6.0e-5, "{angle}");
assert!(twist_angle(swing).abs() < 1.0e-12, "{angle}");
}
}
#[test]
fn the_delta_rotation_undoes_a_small_turn() {
let turn = DVec3::new(0.01, -0.02, 0.015);
let q = rotation_from_scaled_axis(turn);
let back = delta_rotation(q, DQuat::IDENTITY);
assert!((back + turn).length() < 1.0e-5, "{back:?}");
}
#[test]
fn the_solves_invert_and_refuse_singular_matrices() {
let m = DMat3::from_cols(
DVec3::new(4.0, 1.0, 0.5),
DVec3::new(1.0, 3.0, 0.2),
DVec3::new(0.5, 0.2, 2.0),
);
let a = DVec3::new(1.0, -2.0, 0.5);
assert!((m * solve3(m, a) - a).length() < 1.0e-12);
assert!((m * invert(m) - DMat3::IDENTITY).abs_diff_eq(DMat3::ZERO, 1.0e-12));
assert_eq!(solve3(DMat3::ZERO, a), DVec3::ZERO);
let x = solve2(4.0, 1.0, 3.0, [1.0, 2.0]);
assert!((4.0 * x[0] + x[1] - 1.0).abs() < 1.0e-12);
assert!((x[0] + 3.0 * x[1] - 2.0).abs() < 1.0e-12);
assert_eq!(solve2(0.0, 0.0, 0.0, [1.0, 1.0]), [0.0, 0.0]);
}
}