#! 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, #! The two sliding along an axis, turning together, as a piston or a drawer. prismatic, #! The two along an axis, springily, turning as they like, as a car's wheel #! on its suspension. wheel, #! `b` driven toward a place and an angle of `a`'s own, by no more than so #! much force and torque, as a character steered or a door shut. motor, #! `b` pulled by a spring toward a target, as a mouse drags it. mouse, } #! 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; #! Where it holds each, of the body's own places: of a motor joint, where it #! drives `b`'s origin to, of `a`'s, and of a mouse joint, its target. pub var anchor_a: vec2; pub var anchor_b: vec2; #! Of a prismatic or wheel joint, the axis it slides along, of `a`'s own #! directions. pub var axis := vec2{ 1, 0 }; #! Whether the two bodies still meet where their colliders do. pub var collide: bool; #! Of a revolute, prismatic, or motor joint or a weld, how far `b` is #! turned from `a`, in radians, where it rests, or is driven to. pub var reference_angle: f32; #! Whether it's limited: a revolute joint to turning from `lower` to #! `upper` radians from its reference, a distance joint to lengths between #! them, and a prismatic or wheel joint to sliding between them along its #! axis, from where its anchors meet. pub var limit: bool; pub var lower: f32; pub var upper: f32; #! A motor turning a revolute or wheel joint at a speed, in radians a #! second, or lengthening a distance joint or sliding a prismatic one, in #! lengths a second, with no more than so much torque or force. Of a motor #! or mouse joint, the most force it drives `b` with. pub var motor: bool; pub var motor_speed: f32; pub var max_motor_force: f32; #! Of a motor joint, the most torque it turns `b` with, and how much of #! what's left to drive it undoes each step. pub var max_motor_torque: f32; pub var correction: f32 = 0.3; #! A spring toward a revolute joint's reference angle, a distance joint's #! length, or where a prismatic or wheel joint's anchors meet, of so many #! cycles a second, damped so: 1 doesn't spring back. Of a weld, how stiff it #! holds its place, 0 rigid, and of a mouse joint, how stiff it pulls. 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 axis: vec2; # Of a prismatic or wheel joint, as `a` was turned as the step began. var axial_mass: f32; var motor_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), }); } #! Lets two bodies slide along an axis of the plane through a place, turning #! together, as a piston: a prismatic joint, its limits and motor its own to #! set. pub def space::slide(&mut this, a: body_id, b: body_id, at: vec2, axis: 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::prismatic, a = a, b = b, anchor_a = pa.unapply(at), anchor_b = pb.unapply(at), axis = pa.turn.unapply(axis.normalized()), reference_angle = angle_between(pb.turn, pa.turn), }); } #! Holds a wheel, `b`, to a body, `a`, at a place of the plane, along an axis #! it rides on springily, as a car's suspension: a spring of so many cycles a #! second, damped so, and a motor to turn it its own to set. pub def space::wheel(&mut this, a: body_id, b: body_id, at: vec2, axis: vec2, hertz: f32 = 4, damping: f32 = 0.7): joint_id { return joint_id{} if !this.has(a) || !this.has(b); val pa := this.pose_of(a); return this.join(joint{ kind = joint_kind::wheel, a = a, b = b, anchor_a = pa.unapply(at), anchor_b = this.pose_of(b).unapply(at), axis = pa.turn.unapply(axis.normalized()), spring = true, hertz = hertz, damping = damping, }); } #! Drives `b` to stay where it is of `a`, and turned as it is, by no more than #! so much force and torque, as `space::drive_to` moves where: a motor joint. pub def space::drive(&mut this, a: body_id, b: body_id, max_force: f32, max_torque: f32): 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::motor, a = a, b = b, anchor_a = pa.unapply(pb.at), reference_angle = angle_between(pb.turn, pa.turn), max_motor_force = max_force, max_motor_torque = max_torque, collide = true, }); } #! Grabs a body at a place of the plane, pulling that place of it toward a #! target by a spring of so many cycles a second, damped so, with no more than #! so much force, as a mouse drags it: a mouse joint, held by `ground`, a body #! standing still, its target moved by `space::drive_to`. pub def space::grab(&mut this, ground: body_id, b: body_id, at: vec2, max_force: f32, hertz: f32 = 5, damping: f32 = 0.7): joint_id { return joint_id{} if !this.has(ground) || !this.has(b); return this.join(joint{ kind = joint_kind::mouse, a = ground, b = b, anchor_a = this.pose_of(ground).unapply(at), anchor_b = this.pose_of(b).unapply(at), max_motor_force = max_force, spring = true, hertz = hertz, damping = damping, collide = true, }); } #! Moves where a motor joint drives its body, a place of the plane, turned by an #! angle from `a`, or a mouse joint's target, waking its bodies. pub def space::drive_to(&mut this, id: joint_id, at: vec2, angle: f32 = 0): void { return if !this.has(id); val j := id.index as u64; val a := this.joints[j].body_a; this.joints[j].made.anchor_a = this.bodies[a as u64].at.unapply(at); if this.joints[j].made.kind == joint_kind::motor { this.joints[j].made.reference_angle = angle; } this.wake_body(a); this.wake_body(this.joints[j].body_b); } #! How far a prismatic or wheel joint has slid along its axis, from where its #! anchors meet. pub def space::translation_of(&this, id: joint_id): f32 { return 0 if !this.has(id); val j := &this.joints[id.index as u64]; val pa := this.bodies[j.body_a as u64].at; val pb := this.bodies[j.body_b as u64].at; return (pb.apply(j.made.anchor_b) - pa.apply(j.made.anchor_a)).dot(pa.turn.apply(j.made.axis.normalized())); } #! 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. What it no longer asks pushes no more: a motor, #! limits, or a spring turned off. 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; if !made.motor { this.joints[j].motor_impulse = 0; } if !made.limit { this.joints[j].lower_impulse = 0; this.joints[j].upper_impulse = 0; } if !made.spring { this.joints[j].spring_impulse = 0; } 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) }; } #! Visits each joint, as `visit(joint, at_a, at_b)`, where it holds each of its #! bodies, of space's places, until it gives false, as gizmos draw them. pub def space::each_joint(&this, visit: F): void where F: invokable { for j in 0..this.joints.length { val r := &this.joints[j]; continue if !r.alive; val at_a := this.bodies[r.body_a as u64].at.apply(r.made.anchor_a); val at_b := this.bodies[r.body_b as u64].at.apply(r.made.anchor_b); return if !visit(joint_id{ index = j as u32, generation = r.generation }, at_a, at_b); } }