#! Contains joints, which hold two bodies together: a `ball` joint holds them #! at a point they turn about every way, as a shoulder; a `hinge` holds them at #! a point and an axis they turn about alone, as a door, with limits to how far #! it turns and a motor to turn it; a `fixed` joint holds them as one body, #! rigidly or springily; and a `distance` joint keeps two points apart, rigidly #! as a rod, within lengths as a rope, or springily. #! #! Joints are solved before contacts in each substep, 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::physics3d; #! What a joint holds. pub enum joint_kind: u8 { #! The two at a point, about which they turn every way. ball, #! The two at a point, about an axis of each, which they turn about alone. hinge, #! The two as one. fixed, #! Two points apart, as a rod, a rope, or a spring. distance, } #! 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::ball; pub var a: body_id; pub var b: body_id; pub var anchor_a: vec3; pub var anchor_b: vec3; #! Of a hinge, the axis it turns about, of each body's own directions. pub var axis_a := vec3::up; pub var axis_b := vec3::up; #! Of a hinge or a fixed joint, how `b` is turned within `a`'s own turn, #! where it rests. pub var reference := quat::identity; #! Whether the two bodies still meet where their colliders do. pub var collide: bool; #! Whether it's limited: a hinge to turning from `lower` to `upper` radians #! from where it rests, and a distance joint to lengths between them. pub var limit: bool; pub var lower: f32; pub var upper: f32; #! A motor turning a hinge 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 hinge's rest or a distance joint's length, of so many #! cycles a second, damped so: 1 doesn't spring back. Of a fixed joint, 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 fixed joint, 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; var next_b: u32 = 0xFFFFFFFF; var slot_a: u32; var slot_b: u32; var ma: f32; var mb: f32; var ra: vec3; # Each anchor from its body's center, as the step began. var rb: vec3; var qa: quat; # Each body's turn as the step began. var qb: quat; var delta_center: vec3; var axial_mass: f32; var linear: vec3; # Impulses, kept from step to step. var angular: vec3; 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 }; } #! Holds two bodies at a place of space, about which they turn every way. pub def space::ball(&mut this, a: body_id, b: body_id, at: vec3): joint_id { return joint_id{} if !this.has(a) || !this.has(b); return this.join(joint{ kind = joint_kind::ball, a = a, b = b, anchor_a = this.pose_of(a).unapply(at), anchor_b = this.pose_of(b).unapply(at) }); } #! Holds two bodies at a place of space and about an axis through it, of #! space's directions, as a door's hinge holds it. pub def space::hinge(&mut this, a: body_id, b: body_id, at: vec3, axis: vec3): joint_id { return joint_id{} if !this.has(a) || !this.has(b); val pa := this.pose_of(a); val pb := this.pose_of(b); val along := axis.normalized(); return this.join(joint{ kind = joint_kind::hinge, a = a, b = b, anchor_a = pa.unapply(at), anchor_b = pb.unapply(at), axis_a = pa.unrotate(along), axis_b = pb.unrotate(along), reference = (pa.turn.inverse() * pb.turn).normalized(), }); } #! Holds two bodies together as one, about a place of space. pub def space::fix(&mut this, a: body_id, b: body_id, at: vec3): 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::fixed, a = a, b = b, anchor_a = pa.unapply(at), anchor_b = pb.unapply(at), reference = (pa.turn.inverse() * pb.turn).normalized(), }); } #! Keeps two places of space, one of each body, as far apart as they are. pub def space::tie(&mut this, a: body_id, b: body_id, at_a: vec3, at_b: vec3): 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, }); } #! 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 #! 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 hinge is turned from where it rests, in radians, about its axis. 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 hinge_angle(&j.made, this.bodies[j.body_a as u64].at.turn, this.bodies[j.body_b as u64].at.turn); } # How far `b` is turned about a hinge's axis from where it rests, within `a`'s # own turn: the twist of the turn between them. def hinge_angle(made: &joint, qa: quat, qb: quat): f32 { val apart := (made.reference.inverse() * (qa.inverse() * qb)).normalized(); val axis := made.axis_a; val along := apart.x * axis.x + apart.y * axis.y + apart.z * axis.z; return unwound(2 * math::atan2(along, apart.w)); } # 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; } #! 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); this.touch_colliders(a); } 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; } } 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); } } 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); } } } 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); } } }