#! 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::physics2d; #! 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: vec2; #! How fast it turns, in radians a second, counterclockwise. pub var spin: f32; #! 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; #! 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 grid, which `space::attach_grid` #! gives a body, has no shape or density of its own. pub struct collider { pub var shape: shape; #! Its mass for each area 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, 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: vec2; # Its center of mass, in the plane. var local_center: vec2; # The same, of its own places. var velocity: vec2; var spin: f32; var force: vec2; var torque: f32; var mass: f32; var inv_mass: f32; var inertia: f32; # About its center of mass. var inv_inertia: f32; 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; # Its first joint edge. var group: u32 = 0xFFFFFFFF; # The first body of the group it sleeps with. var next_in_group: u32 = 0xFFFFFFFF; var slot: u32; # Its solver state this step, 0 where it doesn't move. var data: u64; } # A collider as the space keeps it: of a shape, or of a grid the space holds. struct collider_record { var made: collider; var grid: u32 = 0xFFFFFFFF; var body: u32; var leaf: u32 = 0xFFFFFFFF; # Its leaf of the space's tree. var next: u32 = 0xFFFFFFFF; # The body's next collider. var generation: u32; var alive: bool; var moved: bool; # Whether it's to find what it newly nears. } # Whether a body's kind is moved by what it meets. def body_record::is_dynamic(&this): bool { return this.kind == body_kind::dynamic; } # Whether a collider is of a grid. def collider_record::is_grid(&this): bool { return this.grid != none; }