physics/src/joint.rs#
Source: lib/physics/src/joint.rs
joint -- constraints between two bodies: ball (with a swing cone and a twist range, for ragdolls),
hinge (limits, motor, spring), fixed, slider (limits, motor, spring), distance (rope, rod, spring)
Every joint has a frame on each body: its origin is the anchor, its X axis the axis (of a hinge, a
slider, the twist of a ball joint). The parts are solved one after the other in every pass of the solver
(solver.rs): motor and spring first, then the limits, the locked rotations, the anchors last (what must
hold best). Soft as the contacts: with the bias the error goes away like a damped spring of the
simulation's joint frequency, the relax pass removes the velocity that leaves. Limits are one-sided and
speculative like contacts: a hinge that is still short of its limit may turn exactly up to it in a
substep. The impulses are kept from step to step (warm start).
struct JointIda joint of a [crate::Simulation]: an index and a generation, like [BodyId]
impl JointId
fn index(self) -> u32the slot (used again after a remove)fn generation(self) -> u32how often the slot was used beforestruct Motordrives a hinge (rad/s) or a slider (m/s) towards a speed with at mostmax_force(N m or N)speed: f64-- the speed it drives towardsmax_force: f64-- the largest torque or force it uses
struct Springpulls a hinge's angle (rad), a slider's position (m) or a distance joint's length (m) towardstargetlike a damped springtarget: f64-- the angle, position or length it pulls towardshertz: f64-- how fast it oscillates (Hz); 0 is no springdamping_ratio: f64-- 1 is critically damped, below oscillates, above creeps
enum JointKindwhat a joint allowsBall { cone: Option<f64>, twist: Option<(f64, f64)> }-- the anchors stay together, the bodies turn freely; optionally the X axes of the two frames stay within a cone of this half angle (radians, the swing) and body b turns about its X axis only within (low, high) radians relative to a (the twist): a shoulder, a hip, a neckHinge { limits: Option<(f64, f64)>, motor: Option<Motor>, spring: Option<Spring> }-- the anchors stay together, the bodies turn about the X axis only, by an angle that starts at 0 when the frames are aligned; within (low, high) radians, driven by a motor, pulled by a springFixed-- no relative motion at allSlider { limits: Option<(f64, f64)>, motor: Option<Motor>, spring: Option<Spring> }-- no rotation; b's anchor moves along a's X axis only, at a position that starts at 0 on a's anchor; within (low, high) metres, driven by a motor, pulled by a springDistance { min: f64, max: f64, spring: Option<Spring> }-- the anchors stay betweenminandmaxapart (a rope: min 0; a rod: min = max;maxmay be infinite), free otherwise, with a spring towards its target length between them
impl JointKind
const BALL: JointKind = JointKind::Ball { cone: None, twist: None }a ball joint without limitsconst HINGE: JointKind = JointKind::Hinge { limits: None, motor: None, spring: None }a hinge without limits, motor or springconst SLIDER: JointKind = JointKind::Slider { limits: None, motor: None, spring: No ...a slider without limits, motor or springconst fn rod(length: f64) -> JointKinda rod of lengthlengthconst fn rope(length: f64) -> JointKinda rope of at mostlengthstruct JointA joint between bodiesaandb: its frame on each (in the body's own frame, the frame its shape is in), what it allows, whether the two bodies still collide with each other.a: BodyId-- the first bodyb: BodyId-- the second body (limits, motors and positions are b relative to a)frame_a: Iso-- the joint's frame in a's frame: the origin is the anchor, X the axisframe_b: Iso-- the joint's frame in b's framekind: JointKind-- what it allowscollide_connected: bool-- do a and b still collide with each other (default false)user: u64-- free for the caller
enum JointErrorwhy a joint was refusedDeadBody-- a body id is deadSameBody-- both ends are the same bodyNothingMoves-- neither end is a dynamic body (nothing could move)NotFinite-- a frame, a limit, a motor or a spring is not finiteRange-- a limit's low end above its high end, a negative cone, length, force, frequency or damping