Skip to content
modkitv0.2

math/bounds.h

#include <modkit/math/bounds.h>25 functions · 5 structs · 1 enums · 6 typedefs

Bounding volumes + intersection / picking.

AABB, sphere, ray, plane and frustum types with the intersection tests games actually need: ray casts (mouse picking), overlap tests (broad-phase), and frustum culling. Pairs with the existing mk_screen_to_ray / mk_project_* helpers in math/project.h. Most functions are header-only. Frustum extraction and mk_ray_from_screen live in math.c because they depend on the backend clip-space convention ([-1,1] OpenGL vs [0,1] D3D/Vulkan/Metal). Naming note: the bare names mk_sphere / mk_plane are already the draw3d quick shapes (mk_draw3d's mk_sphere(), mk_plane()), so the bounding-volume types are mk_bsphere and mk_bplane. AABB, ray and frustum keep their standard names. Plane convention: dot(normal, p) + d >= 0 means "in front" (inside, for frustum planes whose normals point inward).

Functions

mk__mat4_pointfunction

mk_vec3 mk__mat4_point(const mk_mat4 *m, mk_vec3 p)

Transform a point by a column-major mk_mat4 (w=1, no perspective divide).

ParameterTypeDescription
mconst mk_mat4 *Value for m.
pmk_vec3Value for p.

Returns The resulting value.

mk__plane_distfunction

float mk__plane_dist(const mk_bplane *pl, mk_vec3 p)

Perform the plane dist operation.

ParameterTypeDescription
plconst mk_bplane *Value for pl.
pmk_vec3Value for p.

Returns The resulting value.

mk__vmaxfunction

float mk__vmax(float a, float b)

Perform the vmax operation.

ParameterTypeDescription
afloatValue for a.
bfloatValue for b.

Returns The resulting value.

mk__vminfunction

float mk__vmin(float a, float b)

Perform the vmin operation.

ParameterTypeDescription
afloatValue for a.
bfloatValue for b.

Returns The resulting value.

mk_aabb_centerfunction

mk_vec3 mk_aabb_center(mk_aabb b)

Perform the aabb center operation.

ParameterTypeDescription
bmk_aabbValue for b.

Returns The resulting value.

mk_aabb_contains_pointfunction

bool mk_aabb_contains_point(mk_aabb b, mk_vec3 p)

Perform the aabb contains point operation.

ParameterTypeDescription
bmk_aabbValue for b.
pmk_vec3Value for p.

Returns True when the operation succeeds.

mk_aabb_extentsfunction

mk_vec3 mk_aabb_extents(mk_aabb b)

Perform the aabb extents operation.

ParameterTypeDescription
bmk_aabbValue for b.

Returns The resulting value.

mk_aabb_from_center_extentsfunction

mk_aabb mk_aabb_from_center_extents(mk_vec3 center, mk_vec3 half)

Perform the aabb from center extents operation.

ParameterTypeDescription
centermk_vec3Value for center.
halfmk_vec3Value for half.

Returns The resulting value.

mk_aabb_from_pointsfunction

mk_aabb mk_aabb_from_points(const mk_vec3 *pts, int count)

Perform the aabb from points operation.

ParameterTypeDescription
ptsconst mk_vec3 *Value for pts.
countintNumber of entries.

Returns The resulting value.

mk_aabb_makefunction

mk_aabb mk_aabb_make(mk_vec3 min, mk_vec3 max)

Perform the aabb make operation.

ParameterTypeDescription
minmk_vec3Value for min.
maxmk_vec3Value for max.

Returns The resulting value.

mk_aabb_mergefunction

mk_aabb mk_aabb_merge(mk_aabb a, mk_aabb b)

Perform the aabb merge operation.

ParameterTypeDescription
amk_aabbValue for a.
bmk_aabbValue for b.

Returns The resulting value.

mk_aabb_overlaps_aabbfunction

bool mk_aabb_overlaps_aabb(mk_aabb a, mk_aabb b)

Perform the aabb overlaps aabb operation.

ParameterTypeDescription
amk_aabbValue for a.
bmk_aabbValue for b.

Returns True when the operation succeeds.

mk_aabb_overlaps_spherefunction

bool mk_aabb_overlaps_sphere(mk_aabb b, mk_bsphere s)

Perform the aabb overlaps sphere operation.

ParameterTypeDescription
bmk_aabbValue for b.
smk_bsphereValue for s.

Returns True when the operation succeeds.

mk_aabb_transformfunction

mk_aabb mk_aabb_transform(mk_aabb b, const mk_mat4 *m)

Transform an AABB by a matrix and re-fit an axis-aligned box around it.

ParameterTypeDescription
bmk_aabbValue for b.
mconst mk_mat4 *Value for m.

Returns The resulting value.

mk_frustum_contains_pointfunction

bool mk_frustum_contains_point(const mk_frustum *f, mk_vec3 p)

Perform the frustum contains point operation.

ParameterTypeDescription
fconst mk_frustum *Value for f.
pmk_vec3Value for p.

Returns True when the operation succeeds.

mk_frustum_from_vpfunction

mk_frustum mk_frustum_from_vp(const mk_mat4 *view_proj)

Extract the six frustum planes from a view-projection matrix (Gribb-Hartmann).

Normals point inward and are normalized; backend clip range handled internally.

ParameterTypeDescription
view_projconst mk_mat4 *Value for view proj.

Returns The resulting value.

mk_frustum_test_aabbfunction

mk_containment mk_frustum_test_aabb(const mk_frustum *f, mk_aabb b)

Perform the frustum test aabb operation.

ParameterTypeDescription
fconst mk_frustum *Value for f.
bmk_aabbValue for b.

Returns The resulting value.

mk_frustum_test_spherefunction

mk_containment mk_frustum_test_sphere(const mk_frustum *f, mk_bsphere s)

Perform the frustum test sphere operation.

ParameterTypeDescription
fconst mk_frustum *Value for f.
smk_bsphereValue for s.

Returns The resulting value.

mk_ray_from_screenfunction

mk_ray mk_ray_from_screen(float screen_x, float screen_y, const mk_camera *camera, const mk_vec4 *viewport)

Build a world-space picking ray from screen coordinates.

ParameterTypeDescription
screen_xfloatValue for screen x.
screen_yfloatValue for screen y.
cameraconst mk_camera *Value for camera.
viewportconst mk_vec4 *Value for viewport.

Returns The resulting value.

mk_ray_hit_aabbfunction

bool mk_ray_hit_aabb(mk_ray r, mk_aabb b, float *out_t)

Ray vs AABB (slab method).

ParameterTypeDescription
rmk_rayValue for r.
bmk_aabbValue for b.
out_tfloat *Receives the t.

Returns True when the operation succeeds.

mk_ray_hit_planefunction

bool mk_ray_hit_plane(mk_ray r, mk_bplane p, float *out_t)

Ray vs plane.

ParameterTypeDescription
rmk_rayValue for r.
pmk_bplaneValue for p.
out_tfloat *Receives the t.

Returns True when the operation succeeds.

mk_ray_hit_spherefunction

bool mk_ray_hit_sphere(mk_ray r, mk_bsphere s, float *out_t)

Ray vs sphere.

ParameterTypeDescription
rmk_rayValue for r.
smk_bsphereValue for s.
out_tfloat *Receives the t.

Returns True when the operation succeeds.

mk_ray_hit_trianglefunction

bool mk_ray_hit_triangle(mk_ray r, mk_vec3 a, mk_vec3 b, mk_vec3 c, float *out_t)

Ray vs triangle (Moller-Trumbore), two-sided.

ParameterTypeDescription
rmk_rayValue for r.
amk_vec3Value for a.
bmk_vec3Value for b.
cmk_vec3Value for c.
out_tfloat *Receives the t.

Returns True when the operation succeeds.

mk_sphere_from_aabbfunction

mk_bsphere mk_sphere_from_aabb(mk_aabb b)

Perform the sphere from aabb operation.

ParameterTypeDescription
bmk_aabbValue for b.

Returns The resulting value.

mk_sphere_overlaps_spherefunction

bool mk_sphere_overlaps_sphere(mk_bsphere a, mk_bsphere b)

Perform the sphere overlaps sphere operation.

ParameterTypeDescription
amk_bsphereValue for a.
bmk_bsphereValue for b.

Returns True when the operation succeeds.

Structs

mk_aabbstruct

Data for aabb.

FieldTypeDescription
minmk_vec3Minimum corner.
maxmk_vec3Maximum corner.

mk_bplanestruct

Data for bplane.

FieldTypeDescription
normalmk_vec3Plane normal.
dfloatSigned offset in dot(normal, point) + d.

mk_bspherestruct

Data for bsphere.

FieldTypeDescription
centermk_vec3The center.
radiusfloatThe radius.

mk_frustumstruct

Data for frustum.

FieldTypeDescription
planesmk_bplaneLeft, right, bottom, top, near, and far inward planes.

mk_raystruct

Data for ray.

FieldTypeDescription
originmk_vec3Ray origin.
dirmk_vec3Normalized ray direction.

Enums

mk_containmentenum

Values for containment.

ValueDescription
MK_OUTSIDESelects outside.
MK_INTERSECTSelects intersect.
MK_INSIDESelects inside.

Typedefs

mk_aabbtypedef

typedef struct mk_aabb mk_aabb

Data for aabb.

mk_bplanetypedef

typedef struct mk_bplane mk_bplane

Data for bplane.

mk_bspheretypedef

typedef struct mk_bsphere mk_bsphere

Data for bsphere.

mk_containmenttypedef

typedef enum mk_containment mk_containment

Values for containment.

mk_frustumtypedef

typedef struct mk_frustum mk_frustum

Data for frustum.

mk_raytypedef

typedef struct mk_ray mk_ray

Data for ray.