Files
physics/physics3d/heightfield.sakura
T

255 lines
9.3 KiB
Plaintext
Raw Permalink Normal View History

2026-10-27 22:08:25 -05:00
#! Contains `heightfield`, a level's ground as heights on a grid, as terrain is
#! painted: two triangles of each square of four heights, made as they're met
#! rather than kept, so a field of a million heights costs their floats alone.
#! A space keeps one as a `triangle_mesh` keeps its triangles, met a triangle
#! at a time, its edges two triangles share flat or hollow passed over as a
#! mesh's are, so a body rolls across it as along one surface.
import std::containers;
import std::math;
import malunal::geometry;
import malunal::linear;
module malunal::physics3d;
#! Heights on a grid along x and z, as a terrain's ground: `columns` along x by
#! `rows` along z, `cell` apart each way, the first at its origin.
pub struct heightfield {
var _columns: u32;
var _rows: u32;
var _cell: vec2;
var _heights: vector<f32>;
var _low: f32;
var _high: f32;
}
#! The heights of a grid of `columns` along x by `rows` along z, a row of
#! columns after another, from the origin toward +z, each `cell` apart along x
#! and z: those missing are 0. Of fewer than two each way, it holds no
#! triangles.
pub def heightfield::new(columns: u32, rows: u32, heights: span<f32>, cell: vec2 = vec2{ 1, 1 }): heightfield {
var made := heightfield{ _columns = columns, _rows = rows, _cell = cell, _heights = vector::<f32>::new() };
val count := columns as u64 * rows as u64;
for i in 0..count {
made._heights.append(i < heights.length ? heights[i] : 0f);
}
if count > 0 {
made._low = made._heights[0];
made._high = made._heights[0];
for i in 1..count {
made._low = math::min(made._low, made._heights[i]);
made._high = math::max(made._high, made._heights[i]);
}
}
return made;
}
pub val heightfield::columns: u32 {
get => this._columns;
}
pub val heightfield::rows: u32 {
get => this._rows;
}
#! How far apart its heights are along x, and along z.
pub val heightfield::cell: vec2 {
get => this._cell;
}
#! How many triangles it has: two of each square of four heights.
pub val heightfield::length: u64 {
get => this._columns < 2 || this._rows < 2 ? 0 as u64 : (this._columns - 1) as u64 * (this._rows - 1) as u64 * 2;
}
#! The height at a column and row.
pub def heightfield::height(&this, column: u32, row: u32): f32 {
return this._heights[row as u64 * this._columns as u64 + column as u64];
}
#! The ground's height above a place of its own, between its heights, as its
#! triangles lie: nothing beyond it.
pub def heightfield::height_at(&this, x: f32, z: f32): maybe<f32> {
return nothing{} if this.length == 0;
val fx := x / this._cell.x;
val fz := z / this._cell.y;
return nothing{} if fx < 0 || fz < 0 || fx > (this._columns - 1) as f32 || fz > (this._rows - 1) as f32;
val i := math::min(fx as u32, this._columns - 2);
val j := math::min(fz as u32, this._rows - 2);
val u := fx - i as f32;
val v := fz - j as f32;
val h00 := this.height(i, j);
val h10 := this.height(i + 1, j);
val h01 := this.height(i, j + 1);
val h11 := this.height(i + 1, j + 1);
return h00 + (h10 - h00) * u + (h01 - h00) * v if u + v <= 1;
return h11 + (h01 - h11) * (1 - u) + (h10 - h11) * (1 - v);
}
#! The box holding it, of its own places.
pub val heightfield::bounds: bounds {
get => bounds{
vec3{ 0, this._low, 0 },
vec3{ math::max(this._columns, 1 as u32) as f32 * this._cell.x - this._cell.x, this._high, math::max(this._rows, 1 as u32) as f32 * this._cell.y - this._cell.y },
};
}
# The place of a height, of its own places.
def heightfield::corner(&this, i: u32, j: u32): vec3 {
return vec3{ i as f32 * this._cell.x, this.height(i, j), j as f32 * this._cell.y };
}
# Triangle `t` is of the square at column `i` and row `j`, its first half or
# its second.
def heightfield::triangle_of(&this, i: u32, j: u32, half: u32): u32 {
return (j * (this._columns - 1) + i) * 2 + half;
}
#! A triangle's corners, counterclockwise as they're seen from above: of each
#! square, the half by its first corner, then the half by its last.
pub def heightfield::triangle(&this, t: u32): vec3[3] {
val square := t / 2;
val i := square % (this._columns - 1);
val j := square / (this._columns - 1);
return [this.corner(i, j), this.corner(i, j + 1), this.corner(i + 1, j)] if t % 2 == 0;
return [this.corner(i + 1, j), this.corner(i, j + 1), this.corner(i + 1, j + 1)];
}
#! The way a triangle faces.
pub def heightfield::normal(&this, t: u32): vec3 {
val c := this.triangle(t);
return (c[1] - c[0]).cross(c[2] - c[0]).normalized();
}
# Which of a triangle's edges push as they meet, a bit each, as a mesh's: those
# its neighbor shares flat or hollow don't.
def heightfield::active(&this, t: u32): u8 {
val square := t / 2;
val i := square % (this._columns - 1);
val j := square / (this._columns - 1);
val c := this.triangle(t);
val n := this.normal(t);
var bits: u8 = 7;
for k in 0..3u {
var other: i64 = -1;
var off := vec3::zero;
if t % 2 == 0 {
if k == 0 && i > 0 {
other = this.triangle_of(i - 1, j, 1) as i64;
off = this.corner(i - 1, j + 1);
} else if k == 1 {
other = this.triangle_of(i, j, 1) as i64;
off = this.corner(i + 1, j + 1);
} else if k == 2 && j > 0 {
other = this.triangle_of(i, j - 1, 1) as i64;
off = this.corner(i + 1, j - 1);
}
} else {
if k == 0 {
other = this.triangle_of(i, j, 0) as i64;
off = this.corner(i, j);
} else if k == 1 && j + 2 < this._rows {
other = this.triangle_of(i, j + 1, 0) as i64;
off = this.corner(i, j + 2);
} else if k == 2 && i + 2 < this._columns {
other = this.triangle_of(i + 1, j, 0) as i64;
off = this.corner(i + 2, j);
}
}
continue if other < 0;
val ridged := n.dot(off - c[k]) < 0 && n.dot(this.normal(other as u32)) < ridge;
if !ridged {
bits = without_bit(bits, k as u64);
}
}
return bits;
}
# The columns or rows of squares a span of its own places reaches, within it.
def square_span(low: f32, high: f32, size: f32, squares: u32): i64[2] {
val first := math::max(math::floor(low / size) as i64, (0 as i64));
val last := math::min(math::floor(high / size) as i64, squares as i64 - 1);
return [first, last];
}
#! Visits each triangle whose square a box of its own places reaches, as
#! `visit(triangle)`, until it gives false.
pub def heightfield::triangles_in<F>(&this, box: bounds, visit: F): void where F: invokable<def(u32): bool> {
return if this.length == 0 || box.max.y < this._low || box.min.y > this._high;
val xs := square_span(box.min.x, box.max.x, this._cell.x, this._columns - 1);
val zs := square_span(box.min.z, box.max.z, this._cell.y, this._rows - 1);
var j := zs[0];
while j <= zs[1] {
var i := xs[0];
while i <= xs[1] {
for half in 0..2u {
return if !visit(this.triangle_of(i as u32, j as u32, half as u32));
}
i++;
}
j++;
}
}
# Where a ray, of its own places, first meets a triangle, passed from square to
# square along x and z as Amanatides and Woo pass one: the first square it
# meets a triangle of holds the nearest.
def heightfield::ray_cast(&this, o: vec3, d: vec3, reach: f32): maybe<mesh_hit> {
return nothing{} if this.length == 0;
val box := this.bounds;
var enter: f32 = 0;
var leave := reach;
val lows: f32[3] = [box.min.x, box.min.y, box.min.z];
val highs: f32[3] = [box.max.x, box.max.y, box.max.z];
val os: f32[3] = [o.x, o.y, o.z];
val ds: f32[3] = [d.x, d.y, d.z];
for k in 0..3u {
if math::abs(ds[k]) < tiny {
return nothing{} if os[k] < lows[k] || os[k] > highs[k];
} else {
val t1 := (lows[k] - os[k]) / ds[k];
val t2 := (highs[k] - os[k]) / ds[k];
enter = math::max(enter, math::min(t1, t2));
leave = math::min(leave, math::max(t1, t2));
return nothing{} if enter > leave;
}
}
val start := o + d * enter;
val squares_x := (this._columns - 1) as i64;
val squares_z := (this._rows - 1) as i64;
var i := math::clamp(math::floor(start.x / this._cell.x) as i64, (0 as i64), squares_x - 1);
var j := math::clamp(math::floor(start.z / this._cell.y) as i64, (0 as i64), squares_z - 1);
val step_x: i64 = d.x > 0 ? 1 : -1;
val step_z: i64 = d.z > 0 ? 1 : -1;
val far := 1e30f;
var next_x := math::abs(d.x) < tiny ? far : (((i + (d.x > 0 ? (1 as i64) : (0 as i64))) as f32) * this._cell.x - o.x) / d.x;
var next_z := math::abs(d.z) < tiny ? far : (((j + (d.z > 0 ? (1 as i64) : (0 as i64))) as f32) * this._cell.y - o.z) / d.z;
val across_x := math::abs(d.x) < tiny ? far : this._cell.x / math::abs(d.x);
val across_z := math::abs(d.z) < tiny ? far : this._cell.y / math::abs(d.z);
while i >= 0 && j >= 0 && i < squares_x && j < squares_z {
var best: maybe<mesh_hit> = nothing{};
var nearest := reach;
for half in 0..2u {
val t := this.triangle_of(i as u32, j as u32, half as u32);
val c := this.triangle(t);
if ray_triangle(o, d, c[0], c[1], c[2]) is some(along) {
if along <= nearest {
nearest = along;
val n := this.normal(t);
best = mesh_hit{ distance = along, point = o + d * along, normal = n.dot(d) <= 0 ? n : -n, triangle = t };
}
}
}
return best if best is some(_);
if math::min(next_x, next_z) > leave {
break;
}
if next_x < next_z {
i += step_x;
next_x += across_x;
} else {
j += step_z;
next_z += across_z;
}
}
return nothing{};
}