JFLib

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 JointId a joint of a [crate::Simulation]: an index and a generation, like [BodyId]

impl JointId

  • fn index(self) -> u32 the slot (used again after a remove)

  • fn generation(self) -> u32 how often the slot was used before

  • struct Motor drives a hinge (rad/s) or a slider (m/s) towards a speed with at most max_force (N m or N)

    • speed: f64 -- the speed it drives towards
    • max_force: f64 -- the largest torque or force it uses
  • struct Spring pulls a hinge's angle (rad), a slider's position (m) or a distance joint's length (m) towards target like a damped spring

    • target: f64 -- the angle, position or length it pulls towards
    • hertz: f64 -- how fast it oscillates (Hz); 0 is no spring
    • damping_ratio: f64 -- 1 is critically damped, below oscillates, above creeps
  • enum JointKind what a joint allows

    • Ball { 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 neck
    • Hinge { 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 spring
    • Fixed -- no relative motion at all
    • Slider { 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 spring
    • Distance { min: f64, max: f64, spring: Option<Spring> } -- the anchors stay between min and max apart (a rope: min 0; a rod: min = max; max may 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 limits

  • const HINGE: JointKind = JointKind::Hinge { limits: None, motor: None, spring: None } a hinge without limits, motor or spring

  • const SLIDER: JointKind = JointKind::Slider { limits: None, motor: None, spring: No ... a slider without limits, motor or spring

  • const fn rod(length: f64) -> JointKind a rod of length length

  • const fn rope(length: f64) -> JointKind a rope of at most length

  • struct Joint A joint between bodies a and b: 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 body
    • b: 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 axis
    • frame_b: Iso -- the joint's frame in b's frame
    • kind: JointKind -- what it allows
    • collide_connected: bool -- do a and b still collide with each other (default false)
    • user: u64 -- free for the caller
  • enum JointError why a joint was refused

    • DeadBody -- a body id is dead
    • SameBody -- both ends are the same body
    • NothingMoves -- neither end is a dynamic body (nothing could move)
    • NotFinite -- a frame, a limit, a motor or a spring is not finite
    • Range -- a limit's low end above its high end, a negative cone, length, force, frequency or damping

Source: docs/API.md in the JFLib repository.