Files
physics/physics2d/grid.sakura
T
2026-11-14 15:41:09 -06:00

197 lines
7.4 KiB
Plaintext

#! Contains `grid`: solid cells of a grid, as a tilemap's walls are, each a box
#! of the grid's cell size, from the grid's origin at the bottom left of its
#! first cell, x to the right and y up.
#!
#! A shape meets each solid cell as a box, but for the faces two solid cells
#! share: a manifold pushing the shape into a solid neighbor, along the axis
#! it pushes most, is passed over, so a body sliding along a floor of many
#! cells slides as along one, never caught on the seams between them.
import std::containers;
import std::math;
import malunal::linear;
module malunal::physics2d;
#! Solid cells of a grid, so many across and up.
pub struct grid {
#! How wide and high each cell is.
pub var cell := vec2{ 1, 1 };
var _width: u32;
var _height: u32;
var _solid: vector<u8>; # Each cell, a row at a time, from the bottom left.
}
#! A grid so many cells across and up, each so wide and high, none solid.
pub def grid::new(width: u32, height: u32, cell: vec2 = vec2{ 1, 1 }): grid {
var made := grid{ cell = cell, _width = width, _height = height, _solid = vector::<u8>::new() };
made._solid.resize(width as u64 * height as u64, 0);
return made;
}
pub val grid::width: u32 {
get => this._width;
}
pub val grid::height: u32 {
get => this._height;
}
#! Whether a cell is solid: none outside the grid is.
pub def grid::is_solid(&this, x: i64, y: i64): bool {
return false if x < 0 || y < 0 || x >= this._width as i64 || y >= this._height as i64;
return this._solid[(y as u64) * this._width as u64 + x as u64] != 0;
}
#! Makes a cell solid, or not.
pub def grid::set(&mut this, x: u32, y: u32, solid: bool): void {
return if x >= this._width || y >= this._height;
this._solid[y as u64 * this._width as u64 + x as u64] = solid ? 1 : 0;
}
#! A cell's box, of the grid's own places.
pub def grid::box_of(&this, x: u32, y: u32): aabb {
val low := vec2{ x as f32 * this.cell.x, y as f32 * this.cell.y };
return aabb{ low, low + this.cell };
}
#! The box of all its cells, of its own places.
pub val grid::bounds: aabb {
get => aabb{ vec2::zero, vec2{ this._width as f32 * this.cell.x, this._height as f32 * this.cell.y } };
}
#! Visits each solid cell a box of the grid's own places meets, as
#! `visit(x, y)`, until it gives false.
pub def grid::cells_in<F>(&this, box: aabb, visit: F): void where F: invokable<def(u32, u32): bool> {
return if this._width == 0 || this._height == 0;
val x0 := math::max(math::floor(box.min.x / this.cell.x) as i64, 0);
val y0 := math::max(math::floor(box.min.y / this.cell.y) as i64, 0);
val x1 := math::min(math::floor(box.max.x / this.cell.x) as i64, this._width as i64 - 1);
val y1 := math::min(math::floor(box.max.y / this.cell.y) as i64, this._height as i64 - 1);
var y := y0;
while y <= y1 {
var x := x0;
while x <= x1 {
if this.is_solid(x, y) {
return if !visit(x as u32, y as u32);
}
x++;
}
y++;
}
}
#! Visits the manifold of a shape against each solid cell it meets, as
#! `visit(x, y, m)`, until it gives false: `at` places the grid, and `at_s`
#! the shape, and each manifold's normal is from the cell toward the shape,
#! of the plane's places. Those pushing the shape into a solid neighbor are
#! passed over. Cells apart from the shape by no more than a margin are met
#! too, as `collide` meets them.
pub def grid::collide<F>(&this, at: pose, s: &shape, at_s: pose, visit: F, margin: f32 = 0): void
where F: invokable<def(u32, u32, manifold): bool> {
val local := at.unapply_pose(at_s);
val half := this.cell * 0.5;
this.cells_in(s.bounds(local).grown(margin), def(x: u32, y: u32): bool {
val box := shape::box(half, vec2{ (x as f32 + 0.5) * this.cell.x, (y as f32 + 0.5) * this.cell.y });
val found := collide(&box, pose::identity, s, local, margin) else { return true; };
# Along the axis it pushes most, toward a solid neighbor, it's a face the
# two share.
val n := found.normal;
var nx: i64 = 0;
var ny: i64 = 0;
if math::abs(n.x) > math::abs(n.y) {
nx = n.x > 0 ? 1 : -1;
} else {
ny = n.y > 0 ? 1 : -1;
}
return true if this.is_solid(x as i64 + nx, y as i64 + ny);
var placed := manifold{ normal = at.turn.apply(n), count = found.count };
for i in 0..found.count {
placed.points[i] = contact{ point = at.apply(found.points[i].point), depth = found.points[i].depth, id = found.points[i].id };
}
return visit(x, y, placed);
});
}
#! Whether a shape meets any solid cell, `at` placing the grid and `at_s` the
#! shape, but where it only meets the faces solid cells share.
pub def grid::overlaps(&this, at: pose, s: &shape, at_s: pose): bool {
var met := false;
this.collide(at, s, at_s, def(x: u32, y: u32, m: manifold): bool {
met = true;
return false;
});
return met;
}
#! Where a ray met a grid: the hit, and the solid cell it met.
pub struct grid_hit {
pub var hit: ray_hit;
pub var x: u32;
pub var y: u32;
}
#! Where a ray first meets a solid cell, `at` placing the grid: from a place,
#! along a direction of length 1, as far as `reach`, passing from cell to cell
#! as Amanatides and Woo pass, the face it enters by its normal. A ray
#! beginning in a solid cell meets it at 0, facing back along the ray.
pub def grid::ray_cast(&this, at: pose, origin: vec2, direction: vec2, reach: f32): maybe<grid_hit> {
val o := at.unapply(origin);
val d := at.turn.unapply(direction);
val whole := this.bounds;
val start := whole.ray_hit(o, d, reach) else { return nothing{}; };
val p := o + d * start;
var x := math::clamp(math::floor(p.x / this.cell.x) as i64, 0, this._width as i64 - 1);
var y := math::clamp(math::floor(p.y / this.cell.y) as i64, 0, this._height as i64 - 1);
# The face of the grid it entered by, or none where it began within it.
var normal := d * -1f;
if start > 0 {
val gaps: f32[4] = [math::abs(p.x - whole.min.x), math::abs(p.x - whole.max.x), math::abs(p.y - whole.min.y), math::abs(p.y - whole.max.y)];
val faces: vec2[4] = [vec2{ -1, 0 }, vec2{ 1, 0 }, vec2{ 0, -1 }, vec2{ 0, 1 }];
var nearest: u64 = 0;
for i in 1..4u {
if gaps[i] < gaps[nearest] {
nearest = i;
}
}
normal = faces[nearest];
}
val step_x: i64 = d.x > 0 ? 1 : d.x < 0 ? -1 : 0;
val step_y: i64 = d.y > 0 ? 1 : d.y < 0 ? -1 : 0;
val across_x := step_x != 0 ? this.cell.x / math::abs(d.x) : 1e30f;
val across_y := step_y != 0 ? this.cell.y / math::abs(d.y) : 1e30f;
# How far along the ray, from its beginning, each next line of cells is.
var next_x := 1e30f;
var next_y := 1e30f;
if step_x > 0 {
next_x = ((x + 1) as f32 * this.cell.x - o.x) / d.x;
} else if step_x < 0 {
next_x = (x as f32 * this.cell.x - o.x) / d.x;
}
if step_y > 0 {
next_y = ((y + 1) as f32 * this.cell.y - o.y) / d.y;
} else if step_y < 0 {
next_y = (y as f32 * this.cell.y - o.y) / d.y;
}
var t := start;
while true {
if this.is_solid(x, y) {
return grid_hit{
hit = ray_hit{ distance = t, point = at.apply(o + d * t), normal = at.turn.apply(normal) }, x = x as u32, y = y as u32,
};
}
if next_x < next_y {
t = next_x;
next_x += across_x;
x += step_x;
normal = vec2{ -step_x as f32, 0 };
} else {
t = next_y;
next_y += across_y;
y += step_y;
normal = vec2{ 0, -step_y as f32 };
}
return nothing{} if t > reach || x < 0 || y < 0 || x >= this._width as i64 || y >= this._height as i64;
}
return nothing{};
}