#! Contains how a step solves sliders, ball joints' limits, and six-way joints, #! as Box2D's soft step solves the rest: each way a joint holds is a row of its #! own, pushed toward holding it, softly where `use_bias` asks, a limit only as #! far as closes it, as a speculative contact. #! #! A joint's turn is taken within its frame as a twist about its x axis and a #! swing after it, as Jolt's swing-twist joints take it: a ball joint's swing is #! held within its cone, and a six-way joint's swing about y and z each within #! its own limits, as a pyramid. import std::math; import malunal::linear; module malunal::physics3d; # A joint's frame and levers as its bodies have moved and turned since the step # began: its axes in space, from `a`'s anchor to `b`'s, and the turn of `b`'s # frame within `a`'s, as a twist and a swing. struct joint_frame { var x: vec3; # Of `a`'s frame. var y: vec3; var z: vec3; var twist_x: vec3; # `b`'s frame's x, which it twists about, var swing_y: vec3; # and `a`'s y and z across it, which it swings about. var swing_z: vec3; var ra: vec3; var rb: vec3; var d: vec3; var twist: f32; var swung_y: f32; var swung_z: f32; } def frame_of(r: &joint_record, sa: &body_state, sb: &body_state): joint_frame { val fa := ((sa.dq * r.qa).normalized() * r.made.frame_a).normalized(); val fb := ((sb.dq * r.qb).normalized() * r.made.frame_b).normalized(); val ra := sa.dq * r.ra; val rb := sb.dq * r.rb; val x := fa * vec3::right; val y := fa * vec3::up; val z := fa * vec3::back; val bx := fb * vec3::right; var made := joint_frame{ x = x, y = y, z = z, twist_x = bx, swing_y = (y - bx * y.dot(bx)).normalized(), swing_z = (z - bx * z.dot(bx)).normalized(), ra = ra, rb = rb, d = (sb.dp - sa.dp) + r.delta_center + (rb - ra), }; # `b`'s frame within `a`'s, as a swing after a twist about x. var q := (fa.inverse() * fb).normalized(); if q.w < 0 { q = quat{ -q.x, -q.y, -q.z, -q.w }; } val about := math::sqrt(q.x * q.x + q.w * q.w); var twist := quat::identity; if about > tiny { twist = quat{ q.x / about, 0, 0, q.w / about }; made.twist = 2 * math::atan2(q.x, q.w); } val swing := q * twist.inverse(); val across := math::sqrt(swing.y * swing.y + swing.z * swing.z); if across > tiny { val angle := 2 * math::atan2(across, math::abs(swing.w)); val sign := swing.w < 0 ? -1f : 1f; made.swung_y = sign * angle * swing.y / across; made.swung_z = sign * angle * swing.z / across; } return made; } # How hard it is to turn the two apart about an axis of space. def turning_mass(axis: vec3, ia: mat3, ib: mat3): f32 { val k := axis.dot((ia + ib) * axis); return k > 0 ? 1 / k : 0f; } # How hard it is to push the two apart along a direction of space, `a` turning # about the place of `b`'s anchor, as a slider's axis is `a`'s. def sliding_mass(u: vec3, f: &joint_frame, r: &joint_record, ia: mat3, ib: mat3): f32 { val la := (f.ra + f.d).cross(u); val lb := f.rb.cross(u); val k := r.ma + r.mb + la.dot(ia * la) + lb.dot(ib * lb); return k > 0 ? 1 / k : 0f; } # How fast the two part along a direction of space, as a slider's. def sliding_speed(u: vec3, f: &joint_frame, sa: &body_state, sb: &body_state): f32 { return u.dot(sb.v - sa.v) + f.rb.cross(u).dot(sb.w) - (f.ra + f.d).cross(u).dot(sa.w); } # Pushes the two apart by an impulse along a slider's direction. def slide_apart(r: &joint_record, f: &joint_frame, sa: &mut body_state, sb: &mut body_state, ia: mat3, ib: mat3, p: vec3): void { sa.v = sa.v - p * r.ma; sa.w = sa.w - ia * (f.ra + f.d).cross(p); sb.v = sb.v + p * r.mb; sb.w = sb.w + ib * f.rb.cross(p); } # A row held still: its impulse toward undoing `c`, where it's drifted, at # `cdot`, how fast it drifts. def held_impulse(c: f32, cdot: f32, mass: f32, held: f32, soft: &softness, use_bias: bool): f32 { if !use_bias { return -mass * cdot; } return -mass * soft.mass_scale * (cdot + soft.bias_rate * c) - soft.impulse_scale * held; } # Each way of a six-way joint, as its frame lies now: its direction, and how # far it's moved or turned. def way_of(f: &joint_frame, k: u32): vec3 { return k == 0 ? f.x : (k == 1 ? f.y : f.z) if k < 3; return k == 3 ? f.twist_x : (k == 4 ? f.swing_y : f.swing_z); } def amount_of(f: &joint_frame, k: u32): f32 { return f.d.dot(way_of(f, k)) if k < 3; return k == 3 ? f.twist : (k == 4 ? f.swung_y : f.swung_z); } # The impulses a slider, a limited ball joint, or a six-way joint ended the last # substep with, given again, beyond those `warm_joints` gives every joint. def warm_free(r: &joint_record, sa: &mut body_state, sb: &mut body_state, ia: mat3, ib: mat3): void { val f := frame_of(r, sa, sb); if r.made.kind == joint_kind::slider { val axial := r.spring_impulse + r.motor_impulse + r.lower_impulse - r.upper_impulse; slide_apart(r, &f, sa, sb, ia, ib, f.x * axial + f.y * r.held[1] + f.z * r.held[2]); } else if r.made.kind == joint_kind::ball { turn_apart(sa, sb, ia, ib, cone_axis(r, &f) * -r.held[0] + f.twist_x * (r.held[1] - r.held[2])); } else { for k in 0..6u { val total := r.held[k] + r.held[6 + k] - r.held[12 + k] + r.held[18 + k]; continue if total == 0; if k < 3 { slide_apart(r, &f, sa, sb, ia, ib, way_of(&f, k as u32) * total); } else { turn_apart(sa, sb, ia, ib, way_of(&f, k as u32) * total); } } } } # The axis a ball joint's swing turns about, in space, from `a`'s frame's x to # `b`'s. def cone_axis(r: &joint_record, f: &joint_frame): vec3 { val across := f.x.cross(f.twist_x); return across.length_squared > tiny * tiny ? across.normalized() : vec3::zero; } # A slider: its turn held as a fixed joint's, its anchors held to its axis, # and along the axis its motor, spring, and limits. def solve_slider(r: &mut joint_record, sa: &mut body_state, sb: &mut body_state, ia: mat3, ib: mat3, soft: &softness, h: f32, use_bias: bool): void { solve_fixed_turn(r, sa, sb, ia, ib, use_bias); val made := &r.made; val f := frame_of(r, sa, sb); val axial_mass := sliding_mass(f.x, &f, r, ia, ib); val translation := f.d.dot(f.x); if made.motor { val most := h * made.max_motor_force; val held := r.motor_impulse; r.motor_impulse = math::clamp(held + axial_mass * (made.motor_speed - sliding_speed(f.x, &f, sa, sb)), -most, most); slide_apart(r, &f, sa, sb, ia, ib, f.x * (r.motor_impulse - held)); } if made.spring && made.hertz > 0 { val impulse := -r.spring_soft.mass_scale * axial_mass * (sliding_speed(f.x, &f, sa, sb) + r.spring_soft.bias_rate * translation) - r.spring_soft.impulse_scale * r.spring_impulse; r.spring_impulse += impulse; slide_apart(r, &f, sa, sb, ia, ib, f.x * impulse); } if made.limit { val lower := limit_impulse(translation - made.lower, sliding_speed(f.x, &f, sa, sb), axial_mass, r.lower_impulse, soft, 1 / h, use_bias); r.lower_impulse += lower; slide_apart(r, &f, sa, sb, ia, ib, f.x * lower); val upper := limit_impulse(made.upper - translation, -sliding_speed(f.x, &f, sa, sb), axial_mass, r.upper_impulse, soft, 1 / h, use_bias); r.upper_impulse += upper; slide_apart(r, &f, sa, sb, ia, ib, f.x * -upper); } # Held to its axis: neither of the directions across it. for k in 1..3u { val u := k == 1 ? f.y : f.z; val impulse := held_impulse(f.d.dot(u), sliding_speed(u, &f, sa, sb), sliding_mass(u, &f, r, ia, ib), r.held[k], soft, use_bias); r.held[k] += impulse; slide_apart(r, &f, sa, sb, ia, ib, u * impulse); } } # A ball joint's limits: its swing within its cone, its twist between its # lower and upper. def solve_cone(r: &mut joint_record, sa: &mut body_state, sb: &mut body_state, ia: mat3, ib: mat3, soft: &softness, h: f32, use_bias: bool): void { val f := frame_of(r, sa, sb); val made := &r.made; val swung := math::atan2(f.x.cross(f.twist_x).length, f.x.dot(f.twist_x)); val axis := cone_axis(r, &f); if axis.length_squared > 0 { val swing := limit_impulse(made.cone - swung, (sa.w - sb.w).dot(axis), turning_mass(axis, ia, ib), r.held[0], soft, 1 / h, use_bias); r.held[0] += swing; turn_apart(sa, sb, ia, ib, axis * -swing); } val mass := turning_mass(f.twist_x, ia, ib); val lower := limit_impulse(f.twist - made.lower, (sb.w - sa.w).dot(f.twist_x), mass, r.held[1], soft, 1 / h, use_bias); r.held[1] += lower; turn_apart(sa, sb, ia, ib, f.twist_x * lower); val upper := limit_impulse(made.upper - f.twist, (sa.w - sb.w).dot(f.twist_x), mass, r.held[2], soft, 1 / h, use_bias); r.held[2] += upper; turn_apart(sa, sb, ia, ib, f.twist_x * -upper); } # A six-way joint: each way driven by its motor, held within its limits, or # held still, its turns first, then its place; three places locked together # are held as a ball joint holds its anchors. def solve_six(r: &mut joint_record, sa: &mut body_state, sb: &mut body_state, ia: mat3, ib: mat3, soft: &softness, h: f32, use_bias: bool): void { val made := &r.made; val point := made.holds[0] == axis_hold::locked && made.holds[1] == axis_hold::locked && made.holds[2] == axis_hold::locked; var k: u32 = 3; while true { val f := frame_of(r, sa, sb); val u := way_of(&f, k); val amount := amount_of(&f, k); val sliding := k < 3; val mass := sliding ? sliding_mass(u, &f, r, ia, ib) : turning_mass(u, ia, ib); val speed := sliding ? sliding_speed(u, &f, sa, sb) : (sb.w - sa.w).dot(u); var push: f32 = 0; if made.holds[k] == axis_hold::locked { if !sliding || !point { push = held_impulse(amount, speed, mass, r.held[k], soft, use_bias); r.held[k] += push; } } else { if made.forces[k] > 0 { val most := h * made.forces[k]; val before := r.held[18 + k]; r.held[18 + k] = math::clamp(before + mass * (made.drives[k] - speed), -most, most); push += r.held[18 + k] - before; } if made.holds[k] == axis_hold::limited { val lower := limit_impulse(amount - made.lows[k], speed, mass, r.held[6 + k], soft, 1 / h, use_bias); r.held[6 + k] += lower; val upper := limit_impulse(made.highs[k] - amount, -speed - lower / math::max(mass, tiny), mass, r.held[12 + k], soft, 1 / h, use_bias); r.held[12 + k] += upper; push += lower - upper; } } if push != 0 { if sliding { slide_apart(r, &f, sa, sb, ia, ib, u * push); } else { turn_apart(sa, sb, ia, ib, u * push); } } # The turns, 3 to 5, then the places, 0 to 2. k = k == 5 ? 0 as u32 : k + 1; if k == 3 { break; } } if point { solve_point(r, sa, sb, ia, ib, soft, use_bias); } }