Jondolf / avian

ECS-driven 2D and 3D physics engine for the Bevy game engine.
https://crates.io/crates/avian3d
Apache License 2.0
1.55k stars 120 forks source link

Panic using a ColliderConstructor to create a collider from a Gltf mesh. #459

Closed Logan-010 closed 4 months ago

Logan-010 commented 4 months ago

Attempting to create a mesh using my gltf blender export creates a panic:

Tried to add a collider to entity <unnamed entity 18> via TrimeshFromMesh that requires a mesh, but no mesh handle was found

Here is the following code that causes a panic:

commands.spawn(SceneBundle {
        scene: asset_server.load("maps/demo.gltf#Scene0"),
        transform: Transform::from_xyz(0.0, 0.0, 0.0),
        ..Default::default()
    }).insert((RigidBody::Static, ColliderConstructor::TrimeshFromMesh));

And here is a snippet from my Gltf file:

"nodes":[
        {
            "mesh":0,
            "name":"Plane",
            "scale":[
                11.705395698547363,
                11.705395698547363,
                11.705395698547363
            ]
        },
        {
            "mesh":1,
            "name":"Cube",
            "scale":[
                1,
                2.6922757625579834,
                4.149056434631348
            ],
            "translation":[
                -6.870007038116455,
                2.6396241188049316,
                0
            ]
        }
    ],
    "meshes":[
        {
            "name":"Plane",
            "primitives":[
                {
                    "attributes":{
                        "POSITION":0,
                        "NORMAL":1,
                        "TEXCOORD_0":2
                    },
                    "indices":3
                }
            ]
        },
        {
            "name":"Cube.001",
            "primitives":[
                {
                    "attributes":{
                        "POSITION":4,
                        "NORMAL":5,
                        "TEXCOORD_0":6
                    },
                    "indices":7
                }
            ]
        }
    ],

It seems like there is a mesh defined in the Gltf file (I am unfamiliar with the format, so correct me if I'm wrong) and am clueless to what the issue is.

Jondolf commented 4 months ago

ColliderConstructor tries to generate a collider for the entity that the component is on, but your scene doesn't actually add a mesh to that entity. The glTF scene instead spawns a hierarchy of children for all the meshes in the scene.

To generate colliders for those descendants, you should use the ColliderConstructorHierarchy component instead of ColliderConstructor :)

Logan-010 commented 4 months ago

Ok, thank you. I probably should have read the example.