Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
# Objective Fixes #104. Depends on #154 and #189. Currently, there's no easy way to add colliders to meshes loaded from scenes (glTFs and so on). A very common request is to have a way to generate the colliders automatically. ## Solution Add `AsyncCollider` and `AsyncSceneCollider`, which are essentially equivalents to bevy_rapier's components. Also add a `ComputedCollider` enum that determines if the colliders should be trimeshes or convex hulls. Because these rely on some Bevy features, there is a new `async-collider` feature that is enabled by default, but only in 3D because colliders can't be created from meshes in 2D currently. ### `AsyncCollider` example: ```rust use bevy::prelude::*; use bevy_xpbd_3d::prelude::*; fn setup(mut commands: Commands, mut assets: ResMut<AssetServer>) { // Spawn a cube with a convex hull collider generated from the mesh commands.spawn(( AsyncCollider(ComputedCollider::ConvexHull), PbrBundle { mesh: meshes.add(Mesh::from(shape::Cube { size: 1.0 })), ..default(), }, )); } ``` ### `AsyncSceneCollider` example: ```rust use bevy::prelude::*; use bevy_xpbd_3d::prelude::*; fn setup(mut commands: Commands, mut assets: ResMut<AssetServer>) { let scene = SceneBundle { scene: assets.load("my_model.gltf#Scene0"), ..default() }; // Spawn the scene and automatically generate triangle mesh colliders commands.spawn(( scene.clone(), AsyncSceneCollider::new(Some(ComputedCollider::TriMesh)), )); // Specify configuration for specific meshes by name commands.spawn(( scene.clone(), AsyncSceneCollider::new(Some(ComputedCollider::TriMesh)) .with_shape_for_name("Tree", ComputedCollider::ConvexHull) .with_layers_for_name("Tree", CollisionLayers::from_bits(0b0010)) .with_density_for_name("Tree", 2.5), )); // Only generate colliders for specific meshes by name commands.spawn(( scene.clone(), AsyncSceneCollider::new(None) .with_shape_for_name("Tree".to_string(), Some(ComputedCollider::ConvexHull)), )); // Generate colliders for everything except specific meshes by name commands.spawn(( scene, AsyncSceneCollider::new(ComputedCollider::TriMesh) .without_shape_for_name("Tree"), )); } ``` --- ## Changes to methods I also renamed and added some more methods to make things more consistent and simple. I removed `_bevy` from the names because this crate is already for Bevy so it's unnecessary to have the prefix. ### Renamed - `trimesh_with_flags` → `trimesh_with_config` - `convex_decomposition_with_params` → `convex_decomposition_with_config` - `trimesh_from_bevy_mesh` → `trimesh_from_mesh` - `trimesh_from_bevy_mesh_with_flags` → `trimesh_from_mesh_with_config` - `convex_decomposition_from_bevy_mesh` → `convex_decomposition_from_mesh` ### Added - `convex_decomposition_from_mesh_with_config` - `convex_hull_from_mesh`
- Loading branch information