Initial Commit

This commit is contained in:
SoraKatadzuma committed 2026-11-11 11:53:37 -06:00
commit 5346df6dfb
84 files changed
+18825

No files matched your search

+120
View File
@@ -0,0 +1,120 @@
#! Contains continuous collision, as Box2D's keeps a fast body from passing
#! through what's thin: a dynamic body that moved further in a step than half
#! the least its shapes reach is swept, from where it began the step to where
#! it ended it, against what stands still, and put back where it first comes
#! within the slop of what it met, its velocity kept, so the next step's
#! contacts stop it there. A bullet is swept against moving bodies too, where
#! they ended the step, as a shot against a crate.
import std::containers;
import std::math;
import malunal::linear;
module malunal::physics2d;
# A body moving fast enough this step to pass through something thin, and how
# it moved.
struct fast_body {
var body: u32;
var moved: sweep;
}
# Notes a body that moved fast enough to be swept, as the step ends.
def space::note_fast(&mut this, b: u32, moved: sweep): void {
val body := &this.bodies[b as u64];
return if !this.continuous || body.kind != body_kind::dynamic || body.least <= 0;
val travel := (moved.c2 - moved.c1).length + math::abs(angle_between(moved.q2, moved.q1)) * body.extent;
if travel > 0.5 * body.least {
this.fast.append(fast_body{ body = b, moved = moved });
}
}
# Sweeps the fast bodies, bullets last, so they meet the rest where they end
# up, and places their colliders.
def space::sweep_fast(&mut this): void {
var fast := move(this.fast);
for pass in 0..2u {
for i in 0..fast.length {
val bullet := this.bodies[fast[i].body as u64].bullet;
continue if bullet != (pass == 1);
this.sweep_body(&fast[i]);
this.place_colliders(fast[i].body);
}
}
fast.clear();
this.fast = move(fast);
}
# Puts a fast body back where it first meets what it may pass through.
def space::sweep_body(&mut this, f: &fast_body): void {
val b := f.body;
val bullet := this.bodies[b as u64].bullet;
val target := this.slop;
val tolerance := 0.25 * this.slop;
var first: f32 = 1;
var c := this.bodies[b as u64].colliders;
while c != none {
val own := &this.colliders[c as u64];
if !own.made.sensor && !own.is_grid() {
val start := f.moved.at(0);
val box := own.made.shape.bounds(start).merged(own.made.shape.bounds(f.moved.at(1)));
this.tree.query(box, def(leaf: u32, data: u64): bool {
val other := &this.colliders[data];
return true if other.body == b || other.made.sensor;
return true if (other.made.layers & own.made.mask) == 0 || (own.made.layers & other.made.mask) == 0;
val held := &this.bodies[other.body as u64];
return true if held.kind != body_kind::static && (!bullet || held.bullet);
return true if this.joined_apart(b, other.body);
val still := sweep::still(held.at);
if other.is_grid() {
val cells := &this.grids[other.grid as u64];
cells.cells_in(local_box(box, held.at), def(x: u32, y: u32): bool {
val cell := cells.box_of(x, y);
val solid := shape::box(cell.half_size, cell.center);
val t := impact_of(&solid, &still, &own.made.shape, &f.moved, target, tolerance);
if t > 0 && t < first {
first = t;
}
return true;
});
return true;
}
return true if this.behind_one_way(data as u32, &own.made.shape, start);
val t := impact_of(&other.made.shape, &still, &own.made.shape, &f.moved, target, tolerance);
if t > 0 && t < first {
first = t;
}
return true;
});
}
c = this.colliders[c as u64].next;
}
return if first >= 1;
val at := f.moved.at(first);
this.bodies[b as u64].at = at;
this.bodies[b as u64].center = at.apply(this.bodies[b as u64].local_center);
}
# When a fast shape first meets what stands still: where they touch as the
# step begins, as a corner resting on the ground does, when a small circle
# about the fast shape's middle first meets it, so its middle at least never
# passes through, as Box2D finds it.
def impact_of(still_shape: &shape, still: &sweep, fast: &shape, moved: &sweep, target: f32, tolerance: f32): f32 {
val t := time_of_impact(still_shape, still, fast, moved, target, tolerance);
return t if t > 0;
val core := shape::circle(0.25 * fast.least_reach(), fast.middle());
return time_of_impact(still_shape, still, &core, moved, target, tolerance);
}
# A box of the plane as one of a pose's own places, holding it however it's
# turned.
def local_box(box: aabb, at: pose): aabb {
val corners: vec2[4] = [box.min, vec2{ box.max.x, box.min.y }, box.max, vec2{ box.min.x, box.max.y }];
var low := at.unapply(corners[0]);
var high := low;
for k in 1..4u {
val p := at.unapply(corners[k]);
low = low.min(p);
high = high.max(p);
}
return aabb{ low, high };
}