Serializing Colliders
I'm creating a game with a level editor that generates colliders from a png images. I have my own algorithm to generate a polygon from a png. I was glad to find the function Collider::convex_decomposition, as it is exactly what I need. To save time and also to save custom colliders (created in my own editor) to a file I looked at how to serialize rapier colliders. The suggested method to do, is to use the Reflect trait, but it seems that for the Collider struct, Reflect isn't yet implemented (there is a "TODO" in the source code). This has been the case for a while now (last time I checked was in autumn). Are there any plans for implementing this? It seems like quite an essential feature.
EDIT: Is there a workaround for this?
No workarounds as I know and it seems like implementing this is quite non-trivial - Collider makes use of the SharedShape type from the external parry2d crate so you cannot implement the Bevy Reflect (an external trait to bevy_rapier) on it. In this crude example I think the issue comes from the NamedFields where the new method requires the type to implement Reflect
impl Reflect for Collider {
fn type_name(&self) -> &str {
std::any::type_name::<Self>()
}
fn get_type_info(&self) -> &'static bevy::reflect::TypeInfo {
&TypeInfo::Struct(StructInfo::new::<Collider>(
self.type_name(),
&[
NamedField::new::<SharedShape>("raw"),
NamedField::new::<SharedShape>("unscaled"),
NamedField::new::<Vect>("scale")
]
))
}
....
This is just my guesswork and Rust at this level is way more complicated than what I'm accustomed to. From poking around I'd guess that SharedShape would need to be converted into a new type within bevy_rapier to allow Reflect to be implemented, or maybe defining/replacing SharedShape so its a native/local type and then it might be possible to impl
This is possible with https://github.com/dimforge/parry/blob/3ac8e405e26041db57acb6b2218c31066ee7cb6b/src/shape/shared_shape.rs#L366-L373, just not through Reflect at the moment.
Collider implements Serialize, have you tried feeding it to a serde serializer ?
I'm not sure but that's what I would try as a first attempt: serde_json::to_string(&collider)
If that works, you might be able to implement a custom serializer ? More info at https://serde.rs/impl-serializer.html.
Related issue for Reflect, but maybe not strictly needed for this issue: https://github.com/dimforge/bevy_rapier/issues/404