use super::*;
pub(crate) fn kin_quat_from_to(from: Vec3, to: Vec3) -> Quat {
let f: Vec3 = from.normalize();
let t: Vec3 = to.normalize();
let cross_vec: Vec3 = f.cross(t);
let sin_len: f32 = cross_vec.length();
let dot_val: f32 = f.dot(t).clamp(-1.0, 1.0);
if sin_len <= 1e-7 {
if dot_val >= 0.0 {
return Quat::identity();
}
let ref_axis: Vec3 = if f.get_x().abs() < 0.8 {
Vec3::new(1.0, 0.0, 0.0)
} else {
Vec3::new(0.0, 1.0, 0.0)
};
let axis: Vec3 = f.cross(ref_axis).normalize();
return Quat::from_axis_angle(axis, PI);
}
let angle: f32 = sin_len.atan2(dot_val);
Quat::from_axis_angle(cross_vec.scale(1.0 / sin_len), angle)
}
pub(crate) fn gjk_triple_cross(a: Vec3, b: Vec3, c: Vec3) -> Vec3 {
a.cross(b).cross(c)
}
pub(crate) fn gjk_support_diff(
shape_a: &ConvexShape3D,
shape_b: &ConvexShape3D,
dir: Vec3,
) -> Vec3 {
let safe_dir: Vec3 = if dir.length_sq() > 1e-12 {
dir
} else {
Vec3::new(1.0, 0.0, 0.0)
};
let pt_a: Vec3 = shape_a.support(safe_dir);
let pt_b: Vec3 = shape_b.support(safe_dir.scale(-1.0));
pt_a.sub(pt_b)
}
pub(crate) fn gjk_handle_line(simplex: &mut Vec<Vec3>, dir: &mut Vec3) -> bool {
let a: Vec3 = simplex[0];
let b: Vec3 = simplex[1];
let ab: Vec3 = b.sub(a);
let ao: Vec3 = a.scale(-1.0);
if ab.dot(ao) > 0.0 {
let next_dir: Vec3 = gjk_triple_cross(ab, ao, ab);
if next_dir.length_sq() <= 1e-12 {
return true;
}
*dir = next_dir;
} else {
simplex.truncate(1);
*dir = ao;
}
false
}
pub(crate) fn gjk_handle_triangle(simplex: &mut Vec<Vec3>, dir: &mut Vec3) -> bool {
let a: Vec3 = simplex[0];
let b: Vec3 = simplex[1];
let c: Vec3 = simplex[2];
let ab: Vec3 = b.sub(a);
let ac: Vec3 = c.sub(a);
let ao: Vec3 = a.scale(-1.0);
let abc: Vec3 = ab.cross(ac);
if abc.cross(ac).dot(ao) > 0.0 {
if ac.dot(ao) > 0.0 {
simplex[1] = c;
simplex.truncate(2);
let next_dir: Vec3 = gjk_triple_cross(ac, ao, ac);
if next_dir.length_sq() <= 1e-12 {
return true;
}
*dir = next_dir;
} else {
simplex.truncate(2);
return gjk_handle_line(simplex, dir);
}
} else if ab.cross(abc).dot(ao) > 0.0 {
simplex.truncate(2);
return gjk_handle_line(simplex, dir);
} else {
let side: f32 = abc.dot(ao);
if side.abs() <= 1e-9 {
return true;
}
if side > 0.0 {
*dir = abc;
} else {
simplex.swap(1, 2);
*dir = abc.scale(-1.0);
}
}
false
}
pub(crate) fn gjk_handle_tetrahedron(simplex: &mut Vec<Vec3>, dir: &mut Vec3) -> bool {
let a: Vec3 = simplex[0];
let mut b: Vec3 = simplex[1];
let mut c: Vec3 = simplex[2];
let d: Vec3 = simplex[3];
let ab_check: Vec3 = b.sub(a);
let ac_check: Vec3 = c.sub(a);
let ad_check: Vec3 = d.sub(a);
if ab_check.cross(ac_check).dot(ad_check) > 0.0 {
simplex.swap(1, 2);
b = simplex[1];
c = simplex[2];
}
let ab: Vec3 = b.sub(a);
let ac: Vec3 = c.sub(a);
let ad: Vec3 = d.sub(a);
let ao: Vec3 = a.scale(-1.0);
let abc: Vec3 = ab.cross(ac);
let acd: Vec3 = ac.cross(ad);
let adb: Vec3 = ad.cross(ab);
if abc.dot(ao) > 0.0 {
simplex.truncate(3);
return gjk_handle_triangle(simplex, dir);
}
if acd.dot(ao) > 0.0 {
simplex[1] = c;
simplex[2] = d;
simplex.truncate(3);
return gjk_handle_triangle(simplex, dir);
}
if adb.dot(ao) > 0.0 {
simplex[1] = d;
simplex[2] = b;
simplex.truncate(3);
return gjk_handle_triangle(simplex, dir);
}
true
}
pub(crate) fn epa_make_face(
verts: &[Vec3],
i0: usize,
mut i1: usize,
mut i2: usize,
interior: Vec3,
) -> Option<EpaFace3D> {
let v0: Vec3 = verts[i0];
let v1: Vec3 = verts[i1];
let v2: Vec3 = verts[i2];
let raw_n: Vec3 = v1.sub(v0).cross(v2.sub(v0));
let len_sq: f32 = raw_n.length_sq();
if len_sq <= 1e-14 {
return None;
}
let mut normal: Vec3 = raw_n.scale(1.0 / len_sq.sqrt());
if normal.dot(v0.sub(interior)) < 0.0 {
swap(&mut i1, &mut i2);
normal = normal.scale(-1.0);
}
let distance: f32 = normal.dot(v0);
Some(EpaFace3D {
indices: [i0, i1, i2],
normal,
distance,
})
}
pub(crate) fn catmull_rom_f32(p0: f32, p1: f32, p2: f32, p3: f32, t: f32) -> f32 {
let t2: f32 = t * t;
let t3: f32 = t2 * t;
0.5 * ((2.0 * p1)
+ (-p0 + p2) * t
+ (2.0 * p0 - 5.0 * p1 + 4.0 * p2 - p3) * t2
+ (-p0 + 3.0 * p1 - 3.0 * p2 + p3) * t3)
}
pub(crate) fn catmull_rom_vec3(p0: Vec3, p1: Vec3, p2: Vec3, p3: Vec3, t: f32) -> Vec3 {
Vec3::new(
catmull_rom_f32(p0.get_x(), p1.get_x(), p2.get_x(), p3.get_x(), t),
catmull_rom_f32(p0.get_y(), p1.get_y(), p2.get_y(), p3.get_y(), t),
catmull_rom_f32(p0.get_z(), p1.get_z(), p2.get_z(), p3.get_z(), t),
)
}
pub(crate) fn verlet_project_constraints(
particles: &mut [VerletParticle3D],
constraints: &[VerletDistanceConstraint],
spheres: &[VerletSphereCollider],
planes: &[VerletPlaneCollider],
iterations: usize,
) {
let iters: usize = iterations.max(1);
for _ in 0..iters {
for constraint in constraints {
let ia: usize = constraint.get_index_a();
let ib: usize = constraint.get_index_b();
if ia >= particles.len() || ib >= particles.len() || ia == ib {
continue;
}
let wa: f32 = particles[ia].get_inv_mass();
let wb: f32 = particles[ib].get_inv_mass();
let w_sum: f32 = wa + wb;
if w_sum <= 1e-12 {
continue;
}
let pa: Vec3 = particles[ia].get_position();
let pb: Vec3 = particles[ib].get_position();
let delta: Vec3 = pb.sub(pa);
let dist: f32 = delta.length();
if dist <= 1e-7 {
continue;
}
let diff: f32 = (dist - constraint.get_rest_length()) / dist;
let stiff: f32 = constraint.get_stiffness().clamp(0.0, 1.0);
let corr: Vec3 = delta.scale(diff * stiff / w_sum);
if wa > 0.0 {
particles[ia].set_position(pa.add(corr.scale(wa)));
}
if wb > 0.0 {
particles[ib].set_position(pb.sub(corr.scale(wb)));
}
}
for particle in particles.iter_mut() {
if particle.get_inv_mass() <= 0.0 {
continue;
}
let mut pos: Vec3 = particle.get_position();
for sphere in spheres {
let rad: f32 = sphere.get_radius().max(0.0);
let away: Vec3 = pos.sub(sphere.get_center());
let dist_sq: f32 = away.length_sq();
if dist_sq < rad * rad {
let push_dir: Vec3 = if dist_sq > 1e-10 {
away.scale(1.0 / dist_sq.sqrt())
} else {
Vec3::new(0.0, 1.0, 0.0)
};
pos = sphere.get_center().add(push_dir.scale(rad));
}
}
for plane in planes {
let n: Vec3 = plane.get_normal().normalize();
let signed: f32 = pos.dot(n) - plane.get_offset();
if signed < 0.0 {
pos = pos.sub(n.scale(signed));
}
}
particle.set_position(pos);
}
}
}