physics/src/mesh.rs#
Source: lib/physics/src/mesh.rs
mesh -- a static triangle mesh: vertices in f32 (as render3d's meshes), a fixed BVH, the active edges,
and the queries against it in its own frame
Seams: every triangle is tested as the closed triangle it is, so a shape sweeping across a shared edge
meets the edge of one triangle or the other, there is no gap. The opposite trouble -- a shape sliding over
a flat or concave edge between two triangles hits the edge itself with a normal that leans backwards
(a "ghost" collision) -- is handled by the active edges: an edge is active when the surface bends
outwards there by more than the threshold angle (or the edge is open or shared by more than two
triangles). A contact on an inactive edge (or on a vertex whose two edges of this triangle are inactive)
gets the triangle's normal, and a cast ignores it unless the shape moves into that face. As in Jolt's
"active edges"; the side matters, an edge that is concave from the front is convex from the back.
/// at most this many triangles in one mesh
MAX_TRIANGLES: usize = 1 << 28
/// cos 5 degrees: two triangles whose normals differ by less than this meet at a flat edge
ACTIVE_EDGE_COS: f64 = 0.996_194_698_091_745_5
enum MeshErrorwhy [TriMesh::new] refused the dataIndexCount-- the index count is not a multiple of 3, or there are more than [MAX_TRIANGLES] trianglesIndexRange-- an index is not below the vertex countNotFinite-- a coordinate is NaN or infinite
struct MeshOptionshow a [TriMesh] is builtactive_edge_cos: f64-- an edge between two triangles whose normals have a cosine of at least this is flat (inactive); above 1 every edge is active (no handling of ghost collisions)max_leaf: usize-- at most so many triangles in a leaf of the tree
struct TriMeshA static triangle mesh with its tree. Triangles have two sides. Triangles without area (all three corners on a line) are left out: they cannot be hit and their edges belong to their neighbours.
impl TriMesh
fn new(positions: &[[f32; 3]], indices: &[u32]) -> Result<TriMesh, MeshError>a mesh from positions and a triangle list (three indices per triangle), with [MeshOptions]' defaultsfn with_options(positions: &[[f32; 3]], indices: &[u32], opt: &MeshOptions) -> Result<TriMesh, MeshError>a mesh as [TriMesh::new] makes it, with the threshold of the active edges and the leaf size ofoptfn triangle_count(&self) -> usizethe number of triangles in the caller's list (with the ones left out for having no area)fn stored_triangle_count(&self) -> usizethe number of triangles in the treefn vertices(&self) -> &[[f32; 3]]the positions as given, in the mesh's framefn aabb(&self) -> Aabbthe box around the mesh in its own framefn nodes(&self) -> &[BvhNode]the tree's nodes (the root first)fn depth(&self) -> u32the depth of the treefn triangles(&self) -> impl Iterator<Item = (u32, [Vec3; 3])> + '_the stored triangles in tree order: their caller's index and corners (for brute-force references)fn edge_flags(&self, id: u32) -> Option<u8>the active-edge bits of the triangle with the caller's indexid(bit e: edge e from the front, bit 3 + e: from the back), None when it was left outfn memory_bytes(&self) -> usizethe bytes the mesh holds (vertices, triangles, ids, flags, nodes)