Initial Commit

This commit is contained in:
SoraKatadzuma committed 2026-10-31 07:24:40 -05:00
commit c36061ea3e
64 files changed
+13419

No files matched your search

+94
View File
@@ -0,0 +1,94 @@
#! Contains `aabb`, a box along the plane's axes, as what holds a shape, and
#! what an `aabb_tree` sorts.
import std::math;
import std::serial;
import malunal::linear;
module malunal::physics2d;
#! A box along the plane's axes, from its least corner to its greatest.
@serial
pub struct aabb {
@serial pub var min: vec2;
@serial pub var max: vec2;
}
#! The box of a middle, reaching so far each way.
pub def aabb::around(center: vec2, half: vec2): aabb {
return aabb{ center - half, center + half };
}
pub def aabb::equals(&this, other: &aabb): bool {
return this.min == other.min && this.max == other.max;
}
pub val aabb::center: vec2 {
get => (this.min + this.max) * 0.5;
}
#! How far it reaches each way from its middle.
pub val aabb::half_size: vec2 {
get => (this.max - this.min) * 0.5;
}
pub val aabb::size: vec2 {
get => this.max - this.min;
}
#! How far around it is, which an `aabb_tree` weighs where to put a box by.
pub val aabb::perimeter: f32 {
get => 2 * ((this.max.x - this.min.x) + (this.max.y - this.min.y));
}
#! Whether a place is within it, its edges among it.
pub def aabb::contains(&this, p: vec2): bool {
return p.x >= this.min.x && p.x <= this.max.x && p.y >= this.min.y && p.y <= this.max.y;
}
#! Whether it holds another box whole.
pub def aabb::encloses(&this, other: aabb): bool {
return other.min.x >= this.min.x && other.min.y >= this.min.y && other.max.x <= this.max.x && other.max.y <= this.max.y;
}
#! Whether it and another box meet, touching among it.
pub def aabb::overlaps(&this, other: aabb): bool {
return other.min.x <= this.max.x && other.max.x >= this.min.x && other.min.y <= this.max.y && other.max.y >= this.min.y;
}
#! The least box holding it and another.
pub def aabb::merged(&this, other: aabb): aabb {
return aabb{ this.min.min(other.min), this.max.max(other.max) };
}
#! It grown so far each way.
pub def aabb::grown(&this, by: f32): aabb {
return aabb{ this.min - vec2{ by, by }, this.max + vec2{ by, by } };
}
#! How far along a ray it's first met, from a place, along a direction, as far
#! as `reach` lengths of the direction: 0 where the place is within it.
pub def aabb::ray_hit(&this, origin: vec2, direction: vec2, reach: f32): maybe<f32> {
var low: f32 = 0;
var high := reach;
val origins: f32[2] = [origin.x, origin.y];
val ways: f32[2] = [direction.x, direction.y];
val lows: f32[2] = [this.min.x, this.min.y];
val highs: f32[2] = [this.max.x, this.max.y];
for i in 0..2u {
if math::abs(ways[i]) < tiny {
return nothing{} if origins[i] < lows[i] || origins[i] > highs[i];
continue;
}
var near := (lows[i] - origins[i]) / ways[i];
var far := (highs[i] - origins[i]) / ways[i];
if near > far {
val swapped := near;
near = far;
far = swapped;
}
low = math::max(low, near);
high = math::min(high, far);
return nothing{} if low > high;
}
return low;
}