#! Contains joints, which hold two bodies together: a `revolute` joint pins #! them at a point they turn about, as a hinge or an axle, with limits to how #! far it turns and a motor to turn it; a `distance` joint keeps two points #! apart, rigidly as a rod, within lengths as a rope, or springily; and a #! `weld` holds them as one body, rigidly or springily. #! #! Joints are solved before contacts in each substep, as Box2D's are, softly #! as contacts are: each undoes what has drifted as a stiff, damped spring of #! the space's joint frequency, and a limit pushes only as far as closes it #! this substep where it's apart, as a speculative contact does. import std::containers; import std::math; import malunal::linear; module malunal::physics2d; #! What a joint holds. pub enum joint_kind: u8 { #! The two at a point, about which they turn, as a hinge. revolute, #! Two points apart, as a rod, a rope, or a spring. distance, #! The two as one. weld, } #! What a joint is made as: its kind, its bodies, where it holds each, of the #! body's own places, and what each kind asks. pub struct joint { pub var kind := joint_kind::revolute; pub var a: body_id; pub var b: body_id; pub var anchor_a: vec2; pub var anchor_b: vec2; #! Whether the two bodies still meet where their colliders do. pub var collide: bool; #! Of a revolute joint or a weld, how far `b` is turned from `a`, in #! radians, where it rests. pub var reference_angle: f32; #! Whether it's limited: a revolute joint to turning from `lower` to #! `upper` radians from its reference, and a distance joint to lengths #! between them. pub var limit: bool; pub var lower: f32; pub var upper: f32; #! A motor turning a revolute joint at a speed, in radians a second, or #! lengthening a distance joint, in lengths a second, with no more than so #! much torque or force. pub var motor: bool; pub var motor_speed: f32; pub var max_motor_force: f32; #! A spring toward a revolute joint's reference angle or a distance joint's #! length, of so many cycles a second, damped so: 1 doesn't spring back. #! Of a weld, how stiff it holds its place, 0 rigid. pub var spring: bool; pub var hertz: f32; pub var damping: f32 = 1; #! Of a distance joint, the length it holds. pub var length: f32 = 1; #! Of a weld, how stiff it holds its turn, 0 rigid. pub var angular_hertz: f32; pub var angular_damping: f32 = 1; #! A number of its user's. pub var data: u64; } #! A joint of a space, as `space::join` names it. pub struct joint_id { pub var index: u32 = 0xFFFFFFFF; pub var generation: u32; } pub def joint_id::equals(&this, other: &joint_id): bool { return this.index == other.index && this.generation == other.generation; } # A joint as the space keeps it, and what a step works out of it. struct joint_record { var made: joint; var generation: u32; var alive: bool; var body_a: u32; var body_b: u32; var next_a: u32 = 0xFFFFFFFF; # The next joint of each body. var next_b: u32 = 0xFFFFFFFF; var slot_a: u32; var slot_b: u32; var ma: f32; var ia: f32; var mb: f32; var ib: f32; var ra: vec2; # Each anchor from its body's center, as the step began. var rb: vec2; var delta_center: vec2; var delta_angle: f32; var axial_mass: f32; var linear: vec2; # Impulses, kept from step to step. var axial: f32; var spring_impulse: f32; var motor_impulse: f32; var lower_impulse: f32; var upper_impulse: f32; var spring_soft: softness; var linear_soft: softness; var angular_soft: softness; } #! Joins two bodies. pub def space::join(&mut this, made: joint): joint_id { return joint_id{} if !this.has(made.a) || !this.has(made.b) || made.a.index == made.b.index; var index: u32 = 0; if this.free_joints.length > 0 { index = this.free_joints[this.free_joints.length - 1]; this.free_joints.truncate(); } else { index = this.joints.length as u32; this.joints.append(joint_record{}); } val generation := this.joints[index as u64].generation + 1; val a := made.a.index; val b := made.b.index; this.joints[index as u64] = joint_record{ made = made, generation = generation, alive = true, body_a = a, body_b = b, next_a = this.bodies[a as u64].joints, next_b = this.bodies[b as u64].joints, }; this.bodies[a as u64].joints = index; this.bodies[b as u64].joints = index; if !made.collide { this.drop_pairs_between(a, b); } this.wake_body(a); this.wake_body(b); return joint_id{ index = index, generation = generation }; } #! Pins two bodies at a place of the plane, about which they turn. pub def space::pin(&mut this, a: body_id, b: body_id, at: vec2): joint_id { return joint_id{} if !this.has(a) || !this.has(b); val pa := this.pose_of(a); val pb := this.pose_of(b); return this.join(joint{ kind = joint_kind::revolute, a = a, b = b, anchor_a = pa.unapply(at), anchor_b = pb.unapply(at), reference_angle = angle_between(pb.turn, pa.turn), }); } #! Keeps two places of the plane, one of each body, as far apart as they are. pub def space::tie(&mut this, a: body_id, b: body_id, at_a: vec2, at_b: vec2): joint_id { return joint_id{} if !this.has(a) || !this.has(b); return this.join(joint{ kind = joint_kind::distance, a = a, b = b, anchor_a = this.pose_of(a).unapply(at_a), anchor_b = this.pose_of(b).unapply(at_b), length = (at_b - at_a).length, collide = true, }); } #! Holds two bodies together as one, about a place of the plane. pub def space::weld(&mut this, a: body_id, b: body_id, at: vec2): joint_id { return joint_id{} if !this.has(a) || !this.has(b); val pa := this.pose_of(a); val pb := this.pose_of(b); return this.join(joint{ kind = joint_kind::weld, a = a, b = b, anchor_a = pa.unapply(at), anchor_b = pb.unapply(at), reference_angle = angle_between(pb.turn, pa.turn), }); } #! Whether a joint is of it still. pub def space::has(&this, id: joint_id): bool { return id.index < this.joints.length as u32 && this.joints[id.index as u64].alive && this.joints[id.index as u64].generation == id.generation; } #! What a joint was made as, or last changed to. pub def space::joint_of(&this, id: joint_id): &joint { return &this.joints[id.index as u64].made; } #! Changes what a joint asks, as its motor or limits, keeping its bodies and #! anchors, and wakes them. pub def space::change(&mut this, id: joint_id, made: joint): void { return if !this.has(id); val j := id.index as u64; val a := this.joints[j].made.a; val b := this.joints[j].made.b; this.joints[j].made = made; this.joints[j].made.a = a; this.joints[j].made.b = b; this.wake_body(a.index); this.wake_body(b.index); } #! How far a revolute joint is turned from its reference, in radians. pub def space::angle_of(&this, id: joint_id): f32 { return 0 if !this.has(id); val j := &this.joints[id.index as u64]; return unwound(angle_between(this.bodies[j.body_b as u64].at.turn, this.bodies[j.body_a as u64].at.turn) - j.made.reference_angle); } #! Takes a joint away, waking its bodies. pub def space::unjoin(&mut this, id: joint_id): void { return if !this.has(id); this.drop_joint(id.index); } def space::drop_joint(&mut this, j: u32): void { val a := this.joints[j as u64].body_a; val b := this.joints[j as u64].body_b; this.unlink_joint(j, a); this.unlink_joint(j, b); this.joints[j as u64].alive = false; this.free_joints.append(j); this.wake_body(a); this.wake_body(b); # What it kept apart may meet again. this.touch_colliders(a); } # Takes a joint from a body's list of them. def space::unlink_joint(&mut this, j: u32, body: u32): void { val next := this.next_joint(j, body); if this.bodies[body as u64].joints == j { this.bodies[body as u64].joints = next; return; } var k := this.bodies[body as u64].joints; while k != none { val after := this.next_joint(k, body); if after == j { if this.joints[k as u64].body_a == body { this.joints[k as u64].next_a = next; } else { this.joints[k as u64].next_b = next; } return; } k = after; } } # The joint after another of a body's. def space::next_joint(&this, j: u32, body: u32): u32 { return this.joints[j as u64].body_a == body ? this.joints[j as u64].next_a : this.joints[j as u64].next_b; } # Whether a joint holds two bodies and keeps them from meeting. def space::joined_apart(&this, a: u32, b: u32): bool { var j := this.bodies[a as u64].joints; while j != none { val joint := &this.joints[j as u64]; val other := joint.body_a == a ? joint.body_b : joint.body_a; return true if other == b && !joint.made.collide; j = this.next_joint(j, a); } return false; } def space::drop_joints_of(&mut this, b: u32): void { while this.bodies[b as u64].joints != none { this.drop_joint(this.bodies[b as u64].joints); } } # Drops the pairs of two bodies' colliders. def space::drop_pairs_between(&mut this, a: u32, b: u32): void { for p in 0..this.pairs.length { continue if !this.pairs[p].alive; val ba := this.colliders[this.pairs[p].a as u64].body; val bb := this.colliders[this.pairs[p].b as u64].body; if (ba == a && bb == b) || (ba == b && bb == a) { this.drop_pair(p as u32, false); } } } # Has a body's colliders find what they near again, as after a joint that # kept them from meeting is gone. def space::touch_colliders(&mut this, b: u32): void { var c := this.bodies[b as u64].colliders; while c != none { if !this.colliders[c as u64].moved { this.colliders[c as u64].moved = true; this.moved.append(c); } c = this.colliders[c as u64].next; } } def space::join_jointed(&this, parent: &mut vector): void { for j in 0..this.joints.length { val joint := &this.joints[j]; continue if !joint.alive; if parent[joint.body_a as u64] != none && parent[joint.body_b as u64] != none { join(parent, joint.body_a, joint.body_b); } } } # How far `b` is turned past `a`, from -π to π. def angle_between(b: rot, a: rot): f32 { return math::atan2(b.s * a.c - b.c * a.s, b.c * a.c + b.s * a.s); } # An angle brought within -π to π. def unwound(angle: f32): f32 { val pi := math::pi as f32; return angle < -pi ? angle + 2 * pi : angle > pi ? angle - 2 * pi : angle; } # What solves `[k11 k12; k21 k22] x = b`. def solved(k11: f32, k12: f32, k21: f32, k22: f32, b: vec2): vec2 { var det := k11 * k22 - k12 * k21; if det != 0 { det = 1 / det; } return vec2{ det * (k22 * b.x - k12 * b.y), det * (k11 * b.y - k21 * b.x) }; }