use glam::{DQuat, DVec3};
use super::math;
use super::{Common, Impulses, Prepared as Joint, Velocities, limit_bias, motion, soft_zone};
use crate::components::RigidBody;
use crate::contact::solver::SolverBody;
use crate::joint::SphericalJoint;
#[derive(Clone, Copy, Debug)]
enum Swing {
Cone(f64),
Ellipse(f64, f64),
}
impl Swing {
fn new(x: f64, y: f64) -> Self {
if x == y {
Self::Cone(x)
} else {
Self::Ellipse(
crcbl_core::trig::sin(0.5 * x),
crcbl_core::trig::sin(0.5 * y),
)
}
}
fn gap(self, rel: DQuat, frame_a: DQuat) -> (f64, Option<DVec3>) {
match self {
Self::Cone(angle) => (angle - math::swing_angle(rel), None),
Self::Ellipse(a, b) => {
let (gap, axis) = math::ellipse_gap(a, b, rel);
(gap, Some(frame_a * axis))
}
}
}
}
fn axis_mass(sum: glam::DMat3, axis: DVec3) -> f64 {
let k = axis.dot(sum * axis);
if k > 0.0 { 1.0 / k } else { 0.0 }
}
pub(super) fn swing_gap(s: &SphericalJoint, rel: DQuat) -> f64 {
Swing::new(s.swing_x_angle, s.swing_y_angle)
.gap(rel, DQuat::IDENTITY)
.0
}
#[derive(Clone, Copy, Debug)]
pub(in crate::contact) struct Prepared {
settings: SphericalJoint,
swing: Swing,
swing_axis: DVec3,
swing_mass: f64,
soft_swing: Swing,
soft_swing_axis: DVec3,
soft_swing_mass: f64,
twist_jacobian: DVec3,
twist_mass: f64,
rotation_mass: glam::DMat3,
}
impl Prepared {
pub(super) fn new(settings: SphericalJoint, c: &Common) -> Self {
let sum = c.ia + c.ib;
let cone_axis = c.frame_a.q * DVec3::Z;
let twist_axis = c.frame_b.q * DVec3::Z;
let radial_axis = cone_axis.cross(twist_axis).normalize_or_zero();
let mass = |axis: DVec3| axis_mass(sum, axis);
let rel = math::inv_mul(c.frame_a.q, c.frame_b.q);
let swing = Swing::new(settings.swing_x_angle, settings.swing_y_angle);
let swing_axis = if settings.enable_swing_limit {
swing.gap(rel, c.frame_a.q).1.unwrap_or(radial_axis)
} else {
radial_axis
};
let soft_swing = Swing::new(settings.soft_swing_x_angle, settings.soft_swing_y_angle);
let soft_swing_axis = if settings.enable_soft_swing {
soft_swing.gap(rel, c.frame_a.q).1.unwrap_or(radial_axis)
} else {
DVec3::ZERO
};
let across = rel.z * rel.z + rel.w * rel.w;
let tan_half = if across > 0.0 {
((rel.x * rel.x + rel.y * rel.y) / across).sqrt()
} else {
0.0
};
let perp_axis = radial_axis.cross(cone_axis);
let twist_jacobian = cone_axis + tan_half * perp_axis;
Self {
settings,
swing,
swing_axis,
swing_mass: mass(swing_axis),
soft_swing,
soft_swing_axis,
soft_swing_mass: mass(soft_swing_axis),
twist_jacobian,
twist_mass: mass(twist_jacobian),
rotation_mass: if math::is_fixed_rotation(sum) {
glam::DMat3::ZERO
} else {
math::invert(sum)
},
}
}
pub(super) fn angular_impulse(&self, i: &Impulses) -> DVec3 {
let mut angular =
i.motor_vector - i.swing * self.swing_axis + (i.lower - i.upper) * self.twist_jacobian;
if self.settings.enable_soft_twist {
angular += (i.soft_lower - i.soft_upper) * self.twist_jacobian;
}
if self.settings.enable_soft_swing {
angular -= i.soft_swing * self.soft_swing_axis;
}
angular
}
pub(super) fn warm_start(
&self,
joint: &Joint,
solver: &[SolverBody],
bodies: &mut [RigidBody],
) {
let (_, dqa) = motion(solver, joint.a);
let (_, dqb) = motion(solver, joint.b);
let ra = dqa * joint.frame_a.p;
let rb = dqb * joint.frame_b.p;
let i = &joint.impulses;
let angular = self.angular_impulse(i);
let mut v = Velocities::read(joint, bodies);
v.push_turning(
joint,
i.linear,
ra.cross(i.linear) + angular,
rb.cross(i.linear) + angular,
);
v.write(joint, bodies);
}
pub(super) fn solve(
&mut self,
joint: &mut Joint,
solver: &[SolverBody],
bodies: &mut [RigidBody],
h: f64,
use_bias: bool,
) {
let s = self.settings;
let inverse_h = 1.0 / h;
let soft = joint.softness;
let mut v = Velocities::read(joint, bodies);
let (dpa, dqa) = motion(solver, joint.a);
let (dpb, dqb) = motion(solver, joint.b);
let rel = math::inv_mul(dqa * joint.frame_a.q, dqb * joint.frame_b.q);
if s.enable_motor && !joint.fixed_rotation {
let cdot = v.wb - v.wa;
let lambda = -(self.rotation_mass * (cdot - s.motor_velocity));
let mut new = joint.impulses.motor_vector + lambda;
let length = new.length();
let max = s.max_motor_torque * h;
if length > max {
new *= max / length;
}
let lambda = new - joint.impulses.motor_vector;
joint.impulses.motor_vector = new;
v.turn(joint, lambda);
}
if s.enable_soft_twist && !joint.fixed_rotation {
let angle = math::twist_angle(rel);
let jacobian = self.twist_jacobian;
let zone = s.soft_twist;
let old = joint.impulses.soft_lower;
joint.impulses.soft_lower = soft_zone(
angle - s.soft_lower_twist_angle,
angle - s.lower_twist_angle,
(v.wb - v.wa).dot(jacobian),
self.twist_mass,
old,
zone,
h,
);
v.turn(joint, (joint.impulses.soft_lower - old) * jacobian);
let old = joint.impulses.soft_upper;
joint.impulses.soft_upper = soft_zone(
s.soft_upper_twist_angle - angle,
s.upper_twist_angle - angle,
(v.wa - v.wb).dot(jacobian),
self.twist_mass,
old,
zone,
h,
);
v.turn(joint, -(joint.impulses.soft_upper - old) * jacobian);
}
if s.enable_twist_limit && !joint.fixed_rotation {
let angle = math::twist_angle(rel);
let jacobian = self.twist_jacobian;
let (bias, mass_scale, impulse_scale) =
limit_bias(angle - s.lower_twist_angle, inverse_h, soft, use_bias);
let cdot = (v.wb - v.wa).dot(jacobian);
let old = joint.impulses.lower;
let impulse = -mass_scale * self.twist_mass * (cdot + bias) - impulse_scale * old;
joint.impulses.lower = (old + impulse).max(0.0);
v.turn(joint, (joint.impulses.lower - old) * jacobian);
let (bias, mass_scale, impulse_scale) =
limit_bias(s.upper_twist_angle - angle, inverse_h, soft, use_bias);
let cdot = (v.wa - v.wb).dot(jacobian);
let old = joint.impulses.upper;
let impulse = -mass_scale * self.twist_mass * (cdot + bias) - impulse_scale * old;
joint.impulses.upper = (old + impulse).max(0.0);
v.turn(joint, -(joint.impulses.upper - old) * jacobian);
}
let quat_a = dqa * joint.frame_a.q;
let sum = joint.ia + joint.ib;
let swing_gap = if s.enable_swing_limit && !joint.fixed_rotation {
let (gap, axis) = self.swing.gap(rel, quat_a);
if let Some(axis) = axis {
self.swing_axis = axis;
self.swing_mass = axis_mass(sum, axis);
}
gap
} else {
0.0
};
if s.enable_soft_swing && !joint.fixed_rotation {
let (soft_gap, axis) = self.soft_swing.gap(rel, quat_a);
if let Some(axis) = axis {
self.soft_swing_axis = axis;
self.soft_swing_mass = axis_mass(sum, axis);
}
let axis = self.soft_swing_axis;
let old = joint.impulses.soft_swing;
joint.impulses.soft_swing = soft_zone(
soft_gap,
swing_gap,
(v.wa - v.wb).dot(axis),
self.soft_swing_mass,
old,
s.soft_swing,
h,
);
v.turn(joint, -(joint.impulses.soft_swing - old) * axis);
}
if s.enable_swing_limit && !joint.fixed_rotation {
let axis = self.swing_axis;
let (bias, mass_scale, impulse_scale) =
limit_bias(swing_gap, inverse_h, soft, use_bias);
let cdot = (v.wa - v.wb).dot(axis);
let old = joint.impulses.swing;
let impulse = -mass_scale * self.swing_mass * (cdot + bias) - impulse_scale * old;
joint.impulses.swing = (old + impulse).max(0.0);
v.turn(joint, -(joint.impulses.swing - old) * axis);
}
{
let ra = dqa * joint.frame_a.p;
let rb = dqb * joint.frame_b.p;
let cdot = v.relative(ra, rb);
let (bias, mass_scale, impulse_scale) = if use_bias {
let separation = (dpb - dpa) + (rb - ra) + joint.delta_center;
(
soft.bias_rate * separation,
soft.mass_scale,
soft.impulse_scale,
)
} else {
(DVec3::ZERO, 1.0, 0.0)
};
let k = math::point_mass(joint.ma, joint.mb, joint.ia, joint.ib, ra, rb);
let b = math::solve3(k, cdot + bias);
let impulse = -mass_scale * b - impulse_scale * joint.impulses.linear;
joint.impulses.linear += impulse;
v.push(joint, impulse, ra, rb);
}
v.write(joint, bodies);
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::contact::joint::Frame;
use crate::integrator::rotation_from_scaled_axis;
use glam::DMat3;
#[test]
fn the_twist_jacobian_is_the_twists_rate() {
let swing = rotation_from_scaled_axis(DVec3::new(0.15, 1.0, 0.0).normalize() * 0.8);
let twist = rotation_from_scaled_axis(DVec3::Z * 0.1);
let frame_b = swing * twist;
let common = Common {
ma: 1.0,
mb: 1.0,
ia: DMat3::IDENTITY,
ib: DMat3::IDENTITY,
frame_a: Frame {
p: DVec3::ZERO,
q: DQuat::IDENTITY,
},
frame_b: Frame {
p: DVec3::ZERO,
q: frame_b,
},
delta_center: DVec3::ZERO,
};
for settings in [
SphericalJoint::ball()
.with_swing(0.3, 1.2)
.with_twist(-0.2, 0.2),
SphericalJoint::ball().with_cone(1.2).with_twist(-0.2, 0.2),
] {
let prepared = Prepared::new(settings, &common);
let dt = 1.0e-6;
for omega in [
DVec3::X,
DVec3::Y,
DVec3::Z,
frame_b * DVec3::Z,
DVec3::new(0.3, -0.7, 0.5),
] {
let turned = rotation_from_scaled_axis(omega * dt) * frame_b;
let rate = (math::twist_angle(turned) - math::twist_angle(frame_b)) / dt;
let read = prepared.twist_jacobian.dot(omega);
assert!(
(rate - read).abs() < 1.0e-4,
"{settings:?} at {omega:?}: twist turns at {rate}, read {read}"
);
}
}
}
}