Files
physics/geometry/geometry.sakura
T
2026-10-31 07:24:40 -05:00

52 lines
2.2 KiB
Plaintext

#! Contains `geometry`: the shapes of space, made of `linear`'s vectors, and
#! what games, their renderers, and their physics ask of them: where a ray first
#! meets a plane, a sphere, or a box, which side of a plane something lies on,
#! whether boxes and spheres meet or hold a place, and whether a camera's
#! frustum reaches something, so only what it might see is drawn.
#!
#! Space is `linear`'s: right-handed, y up, and a camera looking down its -z. A
#! frustum is of a matrix's clip space as `linear`'s projections make it, depth
#! from 0 at the near plane to 1 at the far one.
#!
#! Each shape is of `linear`'s vectors and plain `f32`s, worked as lanes where
#! `linear` works them, and a frustum holds its six planes' numbers a lane
#! each, so an executable optimized with `-O2` tests a place, a box, or a sphere
#! against four planes at once, then the other two. What gives a number of a
#! few, as a dot product does, is a number at a time, as `linear`'s `cross` is:
#! lanes taken apart to be added, then worked as numbers, ran two to three times
#! as slow. The virtual machine works everything a number at a time, in the order
#! the lanes do, so either way gives the same answers and the same bits.
#!
#! A place exactly on a shape's surface is within it: a box holds its corners,
#! a sphere the places at its radius, and two boxes touching meet.
import malunal::linear;
module malunal::geometry;
# The dot product of two vectors, a number at a time, in the order `vec3::dot`
# adds them.
def dot_of(a: &vec3, b: &vec3): f32 {
return a.x * b.x + a.y * b.y + a.z * b.z;
}
# The square of the distance between two places, a number at a time, as the
# dot product of `a - b` with itself gives it.
def distance_squared(a: &vec3, b: &vec3): f32 {
val x := a.x - b.x;
val y := a.y - b.y;
val z := a.z - b.z;
return x * x + y * y + z * z;
}
# The lesser of two numbers, and the greater: of a NaN, either may be given,
# where `math::min` and `math::max` work three times as long to give it.
def least(a: f32, b: f32): f32 { return b < a ? b : a; }
def most(a: f32, b: f32): f32 { return a < b ? b : a; }
# Four numbers as lanes, read from where the first lies, which needs no
# alignment.
def lanes_at(first: &f32): f32x4 {
return ((first as *f32) as *f32x4)[0];
}