Initial Commit

This commit is contained in:
SoraKatadzuma committed 2026-11-14 15:41:09 -06:00
commit 11d94dba85
84 files changed
+18825

No files matched your search

+219
View File
@@ -0,0 +1,219 @@
#! Contains `character`, a body a game walks, as Box2D's character mover is:
#! not a rigid body of the space, but a shape moved through it by casts, sliding
#! along what it meets, stepping up what's low enough, sticking to the ground as
#! it walks down slopes and off steps, riding what it stands on as that moves,
#! pushed out of what moves into it, and pushing what's dynamic before it.
import std::math;
import malunal::linear;
module malunal::physics2d;
#! A character's body, and what it stands on, as its last move found it.
pub struct character {
#! Its shape, about its origin, a capsule standing up as `shape::pill`
#! makes one.
pub var shape := shape::pill(0.6, 1.8);
#! Where its origin is.
pub var at: vec2;
#! Which way is up, against gravity, of length 1: its shape stands along
#! it, as on a planet, where up is away from the middle.
pub var up := vec2{ 0, 1 };
#! How fast it moved in its last move, but for what it rode: what it was
#! asked to, less what it met. A game adds gravity to it while it's in the
#! air.
pub var velocity: vec2;
#! The steepest ground it stands on and walks up, in radians from flat.
pub var max_slope: f32 = 0.8;
#! The highest it steps up, as onto a stair, and the furthest down it keeps
#! to the ground, as walking down one.
pub var step_height: f32 = 0.35;
pub var snap: f32 = 0.35;
#! How far it keeps from what it meets.
pub var skin: f32 = 0.02;
#! Its mass, and the most force it pushes what's dynamic with.
pub var mass: f32 = 70;
pub var push: f32 = 500;
#! The layers of colliders it meets.
pub var mask: u32 = 0xFFFFFFFF;
#! A body of its own in the space, as a kinematic one moved with it so what
#! moves meets it, which it passes over.
pub var body: body_id;
#! Whether it stands on ground it may, and that ground: which way it faces,
#! its body, and how fast it moves where the character stands.
pub var grounded: bool;
pub var ground_normal := vec2{ 0, 1 };
pub var ground: body_id;
pub var ground_velocity: vec2;
}
#! Whether a character may stand on ground facing a direction.
pub def character::walkable(&this, normal: vec2): bool {
return normal.dot(this.up) >= math::cos(this.max_slope);
}
#! Its pose: at its place, turned from standing along y to stand along its up.
pub def character::pose(&this): pose {
return this.pose_at(this.at);
}
# Its pose, were it somewhere else.
def character::pose_at(&this, at: vec2): pose {
return pose::of(at, math::atan2(-this.up.x, this.up.y));
}
#! Moves a character through a step of `dt` seconds, at a velocity, as its
#! game asks, adding how fast what it stands on moves: pushed out of what it
#! overlaps, then cast along its motion, sliding along what it meets, stepping
#! up what's low enough while it's on the ground, and kept to the ground as it
#! walks down. Its velocity after is what it was asked, less what it met; what
#! it stands on is found again.
pub def space::move_character(&mut this, c: &mut character, velocity: vec2, dt: f32): void {
return if dt <= 0;
val was_grounded := c.grounded;
c.velocity = velocity;
this.push_character_out(c);
var motion := velocity * dt;
if was_grounded {
motion = motion + c.ground_velocity * dt;
}
for iteration in 0..4u {
if motion.length_squared < tiny * tiny {
break;
}
val hit := this.shape_cast(&c.shape, c.pose(), motion, c.mask, c.body) else {
c.at = c.at + motion;
break;
};
c.at = c.at + motion * hit.fraction + hit.normal * c.skin;
var left := motion * (1 - hit.fraction);
val walkable := c.walkable(hit.normal);
if !walkable && was_grounded && this.step_up(c, velocity * dt) {
break;
}
# What's left slides along what it met: along a wall it can't walk up,
# not up it.
var n := hit.normal;
if !walkable && was_grounded {
val flat := n - c.up * n.dot(c.up);
if flat.length_squared > tiny {
n = flat.normalized();
}
}
this.push_struck(c, &hit, dt);
left = left - n * math::min(left.dot(n), 0f);
c.velocity = c.velocity - n * math::min(c.velocity.dot(n), 0f);
motion = left;
}
# Kept to the ground walking down a slope or off a step, where it isn't
# moving up.
if was_grounded && c.velocity.dot(c.up) <= 0 && c.snap > 0 {
if this.shape_cast(&c.shape, c.pose(), c.up * -c.snap, c.mask, c.body) is some(down) {
if c.walkable(down.normal) {
c.at = c.at - c.up * (c.snap * down.fraction) + c.up * c.skin;
}
}
}
this.find_ground(c);
}
# Finds what a character stands on, just below it.
def space::find_ground(&this, c: &mut character): void {
c.grounded = false;
c.ground = body_id{};
c.ground_velocity = vec2::zero;
val below := this.shape_cast(&c.shape, c.pose(), c.up * (-3 * c.skin), c.mask, c.body) else { return; };
return if !c.walkable(below.normal);
c.grounded = true;
c.ground_normal = below.normal;
c.ground = below.body;
val b := &this.bodies[below.body.index as u64];
if b.kind != body_kind::static {
c.ground_velocity = b.velocity + spun(b.spin, c.at - b.center);
}
# Standing, it doesn't fall into the ground.
val falling := c.velocity.dot(c.up);
if falling < 0 {
c.velocity = c.velocity - c.up * falling;
}
}
# Steps a character up onto what it walked into, where it's no higher than its
# step: raised, moved on as far as it walks this step, and lowered, where
# feeling further ahead finds ground it may stand on there.
def space::step_up(&this, c: &mut character, left: vec2): bool {
val ahead := left - c.up * left.dot(c.up);
return false if ahead.length_squared < tiny * tiny || c.step_height <= 0;
var rise := c.step_height;
if this.shape_cast(&c.shape, c.pose(), c.up * c.step_height, c.mask, c.body) is some(above) {
rise = math::max(c.step_height * above.fraction - c.skin, 0f);
}
return false if rise <= c.skin;
val raised := c.pose_at(c.at + c.up * rise);
# Felt for further ahead than its round foot reaches, however little it's
# left to move, so it steps only onto what's there to stand on, higher than
# it stands, and not onto the step's edge.
val reach := math::max(ahead.length, c.shape.radius + 2 * c.skin);
val probe := ahead.normalized() * reach;
var moved := probe;
if this.shape_cast(&c.shape, raised, probe, c.mask, c.body) is some(wall) {
moved = probe * wall.fraction;
}
return false if moved.length < 0.5 * reach;
val drop := c.up * -(rise + c.skin);
val landing := this.shape_cast(&c.shape, c.pose_at(raised.at + moved), drop, c.mask, c.body) else { return false; };
return false if !c.walkable(landing.normal) || landing.fraction == 0;
return false if (rise + c.skin) * (1 - landing.fraction) <= c.skin;
# Then moved as far as it walks, and set down, on the step's edge, maybe, to
# walk on from.
val over := c.pose_at(raised.at + ahead * math::min(1f, moved.length / ahead.length));
val down := this.shape_cast(&c.shape, over, drop, c.mask, c.body) else { return false; };
c.at = over.at + drop * down.fraction + c.up * c.skin;
return true;
}
# Pushes a character out of what it overlaps, as what moves into it does, a
# few times over, by the deepest each time.
def space::push_character_out(&this, c: &mut character): void {
for iteration in 0..4u {
var deepest: f32 = 0;
var away := vec2::zero;
val placed := c.pose();
this.tree.query(c.shape.bounds(placed).grown(c.skin), def(leaf: u32, data: u64): bool {
return true if this.colliders[data].body == c.body.index || !this.passes(data as u32, &query_filter{ mask = c.mask }) || this.behind_one_way(data as u32, &c.shape, placed);
val held := &this.colliders[data];
val at := this.bodies[held.body as u64].at;
if held.is_grid() {
this.grids[held.grid as u64].collide(at, &c.shape, placed, def(x: u32, y: u32, m: manifold): bool {
if m.depth > deepest {
deepest = m.depth;
away = m.normal;
}
return true;
});
return true;
}
var m := collide(&held.made.shape, at, &c.shape, placed) else { return true; };
if held.made.shape.is_one_sided {
m = chained(&held.made.shape, at, &c.shape, placed, m);
}
if m.depth > deepest {
deepest = m.depth;
away = m.normal;
}
return true;
});
if deepest <= 0 {
return;
}
c.at = c.at + away * (deepest + c.skin);
}
}
# Pushes a dynamic body a character walked into, by no more than its push.
def space::push_struck(&mut this, c: &character, hit: &space_cast, dt: f32): void {
return if !this.has(hit.body) || this.bodies[hit.body.index as u64].kind != body_kind::dynamic;
val into := -c.velocity.dot(hit.normal);
return if into <= 0;
val impulse := math::min(into * c.mass, c.push * dt);
this.apply_impulse(hit.body, -hit.normal * impulse, hit.point);
}