143 lines
4.9 KiB
Plaintext
143 lines
4.9 KiB
Plaintext
#! Contains `body` and `collider`, what a `space` is given to make a body and
|
|
#! the shapes it's of, and `body_id` and `collider_id`, which name them there.
|
|
|
|
import malunal::linear;
|
|
module malunal::physics3d;
|
|
|
|
#! What moves a body.
|
|
pub enum body_kind: u8 {
|
|
#! Nothing: it stays where it's put, as the ground does.
|
|
static,
|
|
#! Its velocity alone, as it's set: what it meets doesn't push it, as a
|
|
#! moving platform's doesn't.
|
|
kinematic,
|
|
#! Forces, gravity, and what it meets, as a crate.
|
|
dynamic,
|
|
}
|
|
|
|
#! What a body is made as: its kind, its pose, how fast it moves at first,
|
|
#! and how it's slowed and pulled.
|
|
pub struct body {
|
|
pub var kind := body_kind::dynamic;
|
|
#! Where its origin is, and which way it's turned; its colliders' shapes are
|
|
#! of places about the origin.
|
|
pub var at := pose::identity;
|
|
#! How fast its center of mass moves, in lengths a second.
|
|
pub var velocity: vec3;
|
|
#! How fast it turns, about the axis it points along, by its length in
|
|
#! radians a second.
|
|
pub var spin: vec3;
|
|
#! How much of its velocity, and of its spin, it loses each second, as air
|
|
#! slows it.
|
|
pub var linear_damping: f32;
|
|
pub var angular_damping: f32 = 0.05;
|
|
#! How much of the space's gravity pulls it: 0 floats.
|
|
pub var gravity_scale: f32 = 1;
|
|
#! Whether it never turns, as a character standing up doesn't.
|
|
pub var fixed_rotation: bool;
|
|
#! Whether it may sleep when it and what it rests on are still.
|
|
pub var can_sleep: bool = true;
|
|
#! A number of its user's, as an entity's bits.
|
|
pub var data: u64;
|
|
}
|
|
|
|
#! What a body's collider is made as: its shape, about the body's origin; its
|
|
#! density, which sums to the body's mass; how it slides and bounces; and the
|
|
#! layers it's on and meets. A collider of a triangle mesh, which
|
|
#! `space::attach_mesh` gives a body, has no shape or density of its own.
|
|
pub struct collider {
|
|
pub var shape: shape;
|
|
#! Its mass for each volume of 1.
|
|
pub var density: f32 = 1;
|
|
#! How hard it is to slide along: 0 slides freely. Two colliders' are mixed
|
|
#! as the square root of their product.
|
|
pub var friction: f32 = 0.6;
|
|
#! How much of its speed toward what it meets it keeps, bounced back: 0
|
|
#! stops, 1 bounces as high as it fell. Two colliders' greater is taken.
|
|
pub var restitution: f32;
|
|
#! The layers it's on, and those it meets: two colliders meet where each is
|
|
#! on a layer of the other's mask.
|
|
pub var layers: u32 = 1;
|
|
pub var mask: u32 = 0xFFFFFFFF;
|
|
#! Whether it only notes what it overlaps, pushing nothing.
|
|
pub var sensor: bool;
|
|
#! A number of its user's, as an entity's bits.
|
|
pub var data: u64;
|
|
}
|
|
|
|
#! A body of a space, as `space::add` names it.
|
|
pub struct body_id {
|
|
pub var index: u32 = 0xFFFFFFFF;
|
|
pub var generation: u32;
|
|
}
|
|
|
|
pub def body_id::equals(&this, other: &body_id): bool {
|
|
return this.index == other.index && this.generation == other.generation;
|
|
}
|
|
|
|
#! A collider of a body of a space, as `space::attach` names it.
|
|
pub struct collider_id {
|
|
pub var index: u32 = 0xFFFFFFFF;
|
|
pub var generation: u32;
|
|
}
|
|
|
|
pub def collider_id::equals(&this, other: &collider_id): bool {
|
|
return this.index == other.index && this.generation == other.generation;
|
|
}
|
|
|
|
# No body, collider, contact, mesh, or joint.
|
|
const none: u32 = 0xFFFFFFFF;
|
|
|
|
# A body as the space keeps it.
|
|
struct body_record {
|
|
var kind: body_kind;
|
|
var generation: u32;
|
|
var alive: bool;
|
|
var at: pose; # Of its origin.
|
|
var center: vec3; # Its center of mass, in space.
|
|
var local_center: vec3; # The same, of its own places.
|
|
var velocity: vec3;
|
|
var spin: vec3;
|
|
var force: vec3;
|
|
var torque: vec3;
|
|
var mass: f32;
|
|
var inv_mass: f32;
|
|
var inertia: mat3; # About its center of mass, of its own axes.
|
|
var inv_inertia: mat3; # The same, inverted.
|
|
var linear_damping: f32;
|
|
var angular_damping: f32;
|
|
var gravity_scale: f32 = 1;
|
|
var fixed_rotation: bool;
|
|
var can_sleep: bool = true;
|
|
var awake: bool = true;
|
|
var sleep_time: f32;
|
|
var extent: f32; # How far its colliders reach from its center.
|
|
var colliders: u32 = 0xFFFFFFFF;
|
|
var joints: u32 = 0xFFFFFFFF;
|
|
var group: u32 = 0xFFFFFFFF;
|
|
var next_in_group: u32 = 0xFFFFFFFF;
|
|
var slot: u32;
|
|
var data: u64;
|
|
}
|
|
|
|
# A collider as the space keeps it: of a shape, or of a mesh the space holds.
|
|
struct collider_record {
|
|
var made: collider;
|
|
var mesh: u32 = 0xFFFFFFFF;
|
|
var body: u32;
|
|
var leaf: u32 = 0xFFFFFFFF;
|
|
var next: u32 = 0xFFFFFFFF;
|
|
var generation: u32;
|
|
var alive: bool;
|
|
var moved: bool;
|
|
}
|
|
|
|
def body_record::is_dynamic(&this): bool {
|
|
return this.kind == body_kind::dynamic;
|
|
}
|
|
|
|
# Whether a collider is of a mesh.
|
|
def collider_record::is_mesh(&this): bool {
|
|
return this.mesh != none;
|
|
}
|