bevyengine/bevy
bevy_math
Vector and matrix math, geometric primitives, sampled curves, easing functions, and the deterministic-math wrappers (ops) that the rest of the workspace uses instead of f32::sin and friends.
Purpose
bevy_math is the math substrate of Bevy. It exists for a few reasons:
- Re-export
glamtypes (Vec2,Vec3,Quat,Mat4, …) under stable paths so other crates don't pin aglamversion. - Provide a curve framework (
Curvetrait, easing, sampling). - Provide geometric primitives (
Circle,Sphere,Triangle3d,Capsule3d,Polygon) and operations on them (intersections, sampling, area/volume). - Provide deterministic math wrappers in
ops::that route throughlibmwhen thelibmfeature is on.
Directory layout
crates/bevy_math/src/
├── lib.rs # Re-exports glam, exposes ops/curve/primitives modules
├── ops.rs # Deterministic float ops (powf, sin, cos, …)
├── primitives/ # Geometric primitive types
├── curve/ # Curve trait, easing functions, samplers
├── bounding/ # AABB, sphere, plane, ray
├── compass.rs # CompassQuadrant, CompassOctant
├── direction.rs # Dir2, Dir3 (unit-vector wrappers)
├── ray.rs, rects.rs
├── isometry.rs # Isometry2d, Isometry3d
├── sampling/ # Random sampling on shapes
├── cubic_splines/ # Bezier, B-spline, cubic Hermite
└── …Key abstractions
| Type | File | Description |
|---|---|---|
Vec2 / Vec3 / Quat / Mat4 |
re-exported from glam in lib.rs |
Bread-and-butter math types. |
Dir2 / Dir3 |
crates/bevy_math/src/direction.rs |
Compile-time unit-vector wrapper. |
Curve trait |
crates/bevy_math/src/curve/mod.rs |
t -> sample with metadata. |
EaseFunction |
crates/bevy_math/src/curve/easing.rs |
Built-in easings (sine, quad, cubic, elastic, bounce, …). |
Aabb2d / Aabb3d / BoundingCircle / BoundingSphere |
bounding/ |
Bounding volumes with intersection tests. |
Ray2d / Ray3d |
crates/bevy_math/src/ray.rs |
Origin + direction. |
| Primitive shapes | crates/bevy_math/src/primitives/ |
Circle, Annulus, Triangle2d, Rectangle, Polygon, Sphere, Cuboid, Cylinder, Capsule3d, Cone, Torus, etc. |
ops:: |
crates/bevy_math/src/ops.rs |
sin, cos, powf, ln, etc — deterministic via libm. |
How it works
Determinism
clippy.toml bans direct calls to f32::sin, f32::powf, etc across the workspace. bevy_math::ops::* is the workspace-wide replacement: with the libm feature on, these route through libm for cross-platform reproducibility (important for replays and rollback netcode); with libm off, they fall back to the standard library. The ops module is no_std-friendly.
Curves
A Curve produces a value at a parameter t. The trait has methods to map, blend, sample at uniform intervals, and integrate. EaseFunction provides the standard ~30 easing curves; combinators in curve/ let you splice or repeat curves.
The animation system in bevy_animation uses curves to animate any reflectable field.
Primitives
Each primitive shape implements Primitive2d / Primitive3d, which exposes area, perimeter/volume, and (in many cases) point-in-shape tests. They are also Meshable, meaning bevy_mesh can produce a Mesh from them.
use bevy::prelude::*;
let cube_mesh = Cuboid::new(1.0, 1.0, 1.0).mesh();
let sphere_mesh = Sphere::new(0.5).mesh().ico(4).unwrap();Bounding volumes
The bounding/ module provides AABB / bounding circle / bounding sphere with IntersectsVolume / BoundingVolume traits. Used by frustum culling, ray casting, picking, and many gameplay queries.
Integration points
- Depends on:
glam,serde(optional),libm(optional),rand(optional, forsampling). - Depended on by: Practically every other crate. Math types are pervasive.
Entry points for modification
- New primitive: add a struct under
primitives/, implementPrimitive2d/Primitive3d, and (if it should generate a mesh) implementMeshableinbevy_mesh. - New easing function: add a variant to
EaseFunctionincurve/easing.rs. The graphs documented in rustdoc are generated bytools/build-easefunction-graphs. - New curve combinator:
curve/is full of templates. - Adding a deterministic op:
ops.rsand add adisallowed-methodsentry inclippy.toml.
See also
bevy_animation— primary consumer ofCurve.bevy_mesh— usesPrimitive*d::mesh().bevy_color— sister crate for color spaces.
Built by Factory AutoWiki from public repository content. It is a generated preview for codebase exploration, not source-maintained documentation.