Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rework deferred collider initialization (#378)
# Objective As we all know, `Collider` is not `Reflect`. This is a bit of a problem for people wanting to load collider shapes from code-external sources. Notably, people using @kaosat-dev 's excellent Blender-GLTF workflow as an editor need to setup their own `Reflect` version of `Collider` all the time, usually something like this: ```rust #[derive(Debug, Component, Reflect) #[reflect(Component)] enum Collider { Cuboid {...}, Sphere {...}, Trimesh, ... } ``` and then attach XPBD colliders to entities holding the above fake collider. I've been thinking about opening a PR on the Blender workflow to do this automatically based on a cargo feature, but then I thought that maybe this would be better in XPBD / Avian proper so that other reflect-heavy workflows may profit from it as well. I'm thinking about scene structures, embedding colliders in BSN and future Bevy editor prototypes. ## Solution My approach is to extend ComputedCollider to cover all variants of creating a collider. Some of these require a Mesh, some don't. When going through AsyncColliders, I can see if the specified creation method needs a Mesh. If so, the entity that the AsyncCollider is on must also hold a Handle<Mesh> --- ## Changelog - Reworked deferred / "async" collider initialization. Renamed most involved types. - `ComputedCollider` and `AsyncCollider` have been combined into `ColliderConstructor` and support all possible collider shapes except compounds. - `ColliderConstructor` is `Reflect`, `Serialize` and `Deserialize`. You can use it to statically configure your colliders when loading them from code external sources such as save files or serialized level definitions. - `ColliderConstructor` supports creating primitive shapes, so it does not need to be placed on an entity holding a mesh. - `ColliderConstructor` can be used in 2D when not using computed shapes - `AsyncSceneCollider` is now called `ColliderConstructorHierarchy` and no longer requires being placed on a `Scene`. It will generally generate a collider for all available descendants. - Changes to `ColliderConstructor` also apply here - If the new default feature `bevy_scene` is enabled, placing `ColliderConstructorHierarchy` on a `Scene` will still wait until the scene is loaded - When the desired collider shape should be created from a mesh, only descendants holding a mesh will receive a collider - This type is now especially useful for generating colliders on objects defined with the [Blender to Bevy workflow](https://github.com/kaosat-dev/Blender_bevy_components_workflow) - `ColliderConstructorHierarchy::new` now takes an `impl Into<Option>` instead of an `Option`, so you can drop the `Some` from your calls if you want: `AsyncSceneCollider::new(Some(Foo))` becomes `ColliderConstructorHierarchy::new(Foo)` - `ColliderConstructorHierarchy` can be used in 2D when not using computed shapes - `bevy_scene` can now be feature gated off while still using deferred colliders - `bevy_gltf` is no longer pulled in ## Migration Guide - Remove feature `async-collider`. If you need to use computed shapes, use the feature `collider-from-mesh`. If you depend on `ColliderConstructorHierarchy` waiting for a scene to load, use the feature `bevy_scene` - Remove `AsyncCollider` and use `ColliderConstructor` directly - Rename `AsyncSceneCollider ` to `ColliderConstructorHierarchy` - Rename `AsyncSceneCollider::default_shape` to `ColliderConstructorHierarchy::default_constructor` - Rename `AsyncSceneCollider::meshes_by_name` to `ColliderConstructorHierarchy::config` - Rename `AsyncSceneCollider::with_shape_for_name` to `ColliderConstructorHierarchy::with_constructor_for_name` - Rename `AsyncSceneCollider::without_shape_for_name` to `ColliderConstructorHierarchy::without_constructor_for_name` - Rename `AsyncSceneColliderData` to `ColliderConstructorHierarchyConfig` - Rename `AsyncSceneColliderData::shape` to `ColliderConstructorHierarchyConfig::constructor` - Rename `ComputedCollider` to `ColliderConstructor`. - Rename `ComputedCollider::TriMesh` to `ColliderConstructor::TrimeshFromMesh` - Rename `ComputedCollider::TriMeshWithFlags` to `ColliderConstructor::TrimeshFromMeshWithConfig` - Rename `ComputedCollider::ConvexHull` to `ColliderConstructor::ConvexHullFromMesh` - Rename `ComputedCollider::ConvexDecomposition` to `ColliderConstructor::ConvexDecompositionFromMeshWithConfig` - Rename `VHACDParameters` to `VhacdParameters` - Rename `Collider::halfspace` to `Collider::half_space` ## Linked Issues - Indirectly resolves #309 by verifying that something like that is not really possible in the current version - Resolves #306 --------- Co-authored-by: Joona Aalto <jondolf.dev@gmail.com>
- Loading branch information