Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serializing Colliders #363

Open
lordbenedikt opened this issue Apr 21, 2023 · 3 comments
Open

Serializing Colliders #363

lordbenedikt opened this issue Apr 21, 2023 · 3 comments
Labels
A-Geometry A-Integration very bevy specific D-Medium P-Medium question Further information is requested S-not-started Work has not started

Comments

@lordbenedikt
Copy link

lordbenedikt commented Apr 21, 2023

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?

@BlondeBurrito
Copy link

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

@Aceeri
Copy link
Contributor

Aceeri commented Aug 24, 2023

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.

@Vrixyz
Copy link
Contributor

Vrixyz commented May 23, 2024

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: #404

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Geometry A-Integration very bevy specific D-Medium P-Medium question Further information is requested S-not-started Work has not started
Projects
None yet
Development

No branches or pull requests

4 participants