From b3ce3467c35f65b8c080536424367c80cfb2d9f9 Mon Sep 17 00:00:00 2001 From: Joona Aalto Date: Mon, 17 Jul 2023 13:58:51 +0300 Subject: [PATCH] Add `trimesh_with_flags` and `trimesh_from_bevy_mesh_with_flags` (#79) --- src/components/collider.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/components/collider.rs b/src/components/collider.rs index 77fdaf0f..0b396138 100644 --- a/src/components/collider.rs +++ b/src/components/collider.rs @@ -10,6 +10,9 @@ use parry::{ shape::{SharedShape, TypedShape}, }; +/// Flags used for the preprocessing of a triangle mesh collider. +pub type TriMeshFlags = parry::shape::TriMeshFlags; + /// A collider used for collision detection. /// /// By default, colliders generate [collision events](#collision-events) and cause a collision response for @@ -244,6 +247,17 @@ impl Collider { SharedShape::trimesh(vertices, indices).into() } + /// Creates a collider with a triangle mesh shape defined by its vertex and index buffers + /// and flags controlling the preprocessing. + pub fn trimesh_with_flags( + vertices: Vec, + indices: Vec<[u32; 3]>, + flags: TriMeshFlags, + ) -> Self { + let vertices = vertices.into_iter().map(|v| v.into()).collect(); + SharedShape::trimesh_with_flags(vertices, indices, flags).into() + } + /// Creates a collider with a triangle mesh shape built from a given Bevy `Mesh`. #[cfg(all(feature = "3d", feature = "collider-from-mesh"))] pub fn trimesh_from_bevy_mesh(mesh: &Mesh) -> Option { @@ -255,6 +269,14 @@ impl Collider { }) } + /// Creates a collider with a triangle mesh shape built from a given Bevy `Mesh` and flags + /// controlling its preprocessing. + #[cfg(all(feature = "3d", feature = "collider-from-mesh"))] + pub fn trimesh_from_bevy_mesh_with_flags(mesh: &Mesh, flags: TriMeshFlags) -> Option { + let vertices_indices = extract_mesh_vertices_indices(mesh); + vertices_indices.map(|(v, i)| SharedShape::trimesh_with_flags(v, i, flags).into()) + } + /// Creates a collider with a compound shape obtained from the decomposition of a triangle mesh /// built from a given Bevy `Mesh`. #[cfg(all(feature = "3d", feature = "collider-from-mesh"))]