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

+121
View File
@@ -0,0 +1,121 @@
#! Contains chains of segments, as a level's ground and a cave's walls are
#! drawn, and one-way platforms, as a game jumps through from below.
#!
#! A chain's segments are each a collider of their own, met only on the side
#! their normals face, to the right going along the chain, as a polygon's
#! counterclockwise edges face out: what's behind a segment passes through it.
#! Where two segments join flat or hollow, neither pushes by the corner they
#! share, only by their faces, so a body slides across the joints as along one
#! surface, never caught on them, as Box2D's chains keep it; where they join at
#! a corner sticking out, the segment after it pushes by the corner, as far
#! around it as its face and the one before turn.
#!
#! A one-way collider is solid only to what comes onto it from the side its
#! `up` faces: a contact beginning from below it, or beside it, passes through
#! until the two part, as a platform is jumped through and landed on.
import std::containers;
import std::math;
import malunal::linear;
module malunal::physics2d;
#! Gives a body a chain of segments through places, as a level's ground or a
#! cave's walls: each a collider of the chain's own, met on its side to the
#! right going from one place to the next, so ground runs right to left, and a
#! cave's walls, met from within, clockwise; `looped` joins the last place to
#! the first. Their density is passed over, so a chain weighs nothing.
pub def space::attach_chain(&mut this, to: body_id, places: span<vec2>, looped: bool, made: collider): vector<collider_id> {
var ids := vector::<collider_id>::new();
val n := places.length;
return ids if n < 2 || !this.has(to);
val segments := looped ? n : n - 1;
for i in 0..segments {
var before: maybe<vec2> = nothing{};
var after: maybe<vec2> = nothing{};
if looped || i > 0 {
before = places[(i + n - 1) % n];
}
if looped || i + 2 < n {
after = places[(i + 2) % n];
}
var piece := made;
piece.shape = shape::chain_segment(places[i], places[(i + 1) % n], before, after);
piece.density = 0;
ids.append(this.attach(to, piece));
}
return ids;
}
# The middle of a shape's points, of its own places.
def shape::middle(&this): vec2 {
var sum := vec2::zero;
for i in 0..this._count {
sum = sum + this._points[i];
}
return sum * (1 / this._count as f32);
}
# A manifold of a chain's segment against another shape, met only on the
# segment's front, and by its ends only where they're the chain's own, or a
# corner sticking out the segment begins at: nothing where it's met otherwise,
# as the segment beside it meets it by its face.
def chained(segment: &shape, at: pose, other: &shape, at_other: pose, m: manifold): manifold {
return m if m.count == 0;
val p1 := at.apply(segment._points[0]);
val p2 := at.apply(segment._points[1]);
val n := at.turn.apply(segment._normals[0]);
return manifold{} if n.dot(at_other.apply(other.middle()) - p1) < 0;
return m if m.normal.dot(n) >= 0.9999;
val along := p2 - p1;
var spot := vec2::zero;
for i in 0..m.count {
spot = spot + m.points[i].point;
}
spot = spot * (1 / m.count as f32);
if (spot - p1).dot(along) >= 0.5 * along.length_squared {
# Its end: the segment after it meets what's there.
return manifold{} if (segment._chained & 4) != 0;
return m;
}
return m if (segment._chained & 2) == 0;
val incoming := p1 - at.apply(segment._points[2]);
# A corner flat or hollow: the segment before meets it by its face.
return manifold{} if incoming.cross(along) <= 0.017 * incoming.length * along.length;
val n_before := vec2{ incoming.y, -incoming.x }.normalized();
return m if n_before.cross(m.normal) >= 0 && m.normal.cross(n) >= 0;
return manifold{};
}
# Whether a one-sided collider, a chain's segment or a one-way collider, lets
# a shape where a pose places it through, as it's behind or below it.
def space::behind_one_way(&this, c: u32, s: &shape, at: pose): bool {
val held := &this.colliders[c as u64];
return false if held.is_grid();
val placed := this.bodies[held.body as u64].at;
if held.made.shape.is_one_sided {
val n := placed.turn.apply(held.made.shape._normals[0]);
return n.dot(at.apply(s.middle()) - placed.apply(held.made.shape._points[0])) < 0;
}
return false if !held.made.one_way;
val g := gap_between(&held.made.shape, placed, s, at);
return g.distance <= 0 || g.normal.dot(placed.turn.apply(held.made.up)) < 0.7;
}
# Whether a contact of a one-way collider holds: one beginning from its `up`
# side does, until the two part; one beginning otherwise passes through until
# then.
def space::one_way_holds(&mut this, k: u32, platform: u32, began: bool): bool {
val c := &mut this.contacts[k as u64];
if !c.touching {
c.passing = false;
return false;
}
if began {
val held := &this.colliders[platform as u64];
val up := this.bodies[held.body as u64].at.turn.apply(held.made.up);
# The normal from the platform toward what met it.
val toward := this.pairs[c.pair as u64].a == platform ? c.manifold.normal : c.manifold.normal * -1f;
c.passing = toward.dot(up) < 0.7;
}
return !c.passing;
}