godot icon indicating copy to clipboard operation
godot copied to clipboard

create_multiple_collision_shapes() does not do convex decomposition

Open CornbreadWithMarmalade opened this issue 3 years ago • 14 comments

Godot version

4.0 dev( 4.1 beta, 4.2 beta, 4.3 beta)

System information

OS Version : MacOS Monterrey, Hardware : Mac OS Silicon

Issue description

When trying to automatically create multiple convex collision shapes from a concave MeshInstance3D, only one convex collision shape is created instead of multiple. This happens in the editor and when calling create_multiple_convex_collisions() in code on a MeshInstance3D.

Steps to reproduce

In editor:

  1. Create a new 3D scene with Node3D as root.
  2. Add RigidBody3D as child of scene root
  3. Add MeshInstance3D as child of RigidBody3D
  4. Assign TorusMesh to the mesh of the MeshInstance3D
  5. Select the MeshInstance3D in the scene tree
  6. Click the Mesh icon next to the view icon in the editor
  7. Click Create Multiple Convex Collision Siblings
  8. Observe that only one convex collision shape is added as a child to the RigidBody3D

In code:

  1. Create a torus mesh as the mesh of a MeshInstance3D that is the child of a rigid body
  2. call create_multiple_convex_collisions() on the mesh instance
  3. reparent the collision shapes to be the siblings of the MeshInstance3D
  4. put a rigid body with a collision shape that fits in the hole of the torus, and notice that it does not fall through, because the only one convex collision shape is being created for the entire mesh even though it is concave.

Minimal reproduction project

ExampleOfBug.zip

CornbreadWithMarmalade avatar Oct 23 '22 14:10 CornbreadWithMarmalade

4.0 dev( 4.1 beta, 4.2 beta, 4.3 beta)

There are no 4.1/4.2/4.3 betas yet :slightly_smiling_face: You meant 4.0.beta1, 4.0.beta2 and 4.0.beta3 instead.

Calinou avatar Oct 23 '22 17:10 Calinou

That's what I meant

ghost avatar Oct 23 '22 17:10 ghost

You are asking to create a convex hull shape for a single torus triangle mesh while keeping the torus hole which is impossible.

~~The "Create Multiple Convex Collision Siblings" function will give you multiple collision shapes if you have a mesh resource with triangles arranged in a way that it requires multiple convex hulls. A single torus triangle mesh requires a single hull so you only get one collision shape.~~ Actually the Godot 4 defaults limit the convex hull count to 1 so it would not even work for meshes with disconnected triangles in the same mesh resource.

~~The real~~ One bug here is that the other "Create Single Convex Collision Sibling" Editor option tries to build a convex hull shape for a torus with a hole and fails half the shape creation while the other convex menu options do what is correct, close the hole to be convex.

smix8 avatar Oct 23 '22 18:10 smix8

No, "Create Multiple Convex Collisions" is supposed to create multiple convex collision shapes that approximately do what a trimesh collision shape would do, using convex decomposition using v-hacd. This bug isn't just happening with toruses but any concave mesh. The hole should not be closed to make the torus's collision shape be convex, it should have multiple collision shapes generated using v-hacd convex decomposition, whose union approximates what a trimesh collision shape does.

ghost avatar Oct 23 '22 18:10 ghost

and other rigid bodies should be then able to fall through the hole of the torus

ghost avatar Oct 23 '22 18:10 ghost

is supposed to

I agree for VHACD in general but VHACD has a wide range of bake parameters and those are set very low for both Editor Menu option as well as MeshInstance3D.create_multiple_convex_collisions(). So low in fact they have max_convex_hulls=1 set and this was done in 2021 (before it was set to infinite hulls) so it seemed like a deliberate choice but don't ask me why, not my area.

Still even with this limits lifted in a custom build the torus has problems, might be the TriMesh creation or geometry parsing or something else I don't know but maybe someone else can use some of this info to fix it.

smix8 avatar Oct 23 '22 23:10 smix8

The default option for the maximum number of convex hulls should probably be 10 or so, rather than 1.

Calinou avatar Oct 23 '22 23:10 Calinou

Why not have the max number of convex hulls be a parameter in MeshInstance3D.create_multiple_convex_collisions() function

ghost avatar Oct 24 '22 20:10 ghost

It is not only the hull count, there are a lot of parameters, to much for a function, needs to be wrapped if exposed. https://github.com/godotengine/godot/blob/32997a5b9de14bd08f75f1f5d4c0a7b913338d2c/scene/resources/mesh.h#L178-L207

smix8 avatar Oct 25 '22 09:10 smix8

It would be really helpful if this was exposed

ghost avatar Nov 19 '22 14:11 ghost

I think the ConvexDecompositionSettings could be exposed as a public property (serializable). Also, the user should be able to edit these settings in the editor: maybe displayed as a popup window when clicking on Mesh > Create Multiple Convex Collision Siblings.

This should be added to the Godot 4.0 Issues Roadmap (Physics) since it's a breaking bug.

bvigario avatar Jan 17 '23 10:01 bvigario

When will this be exposed?

ghost avatar Jan 23 '23 20:01 ghost

When will this be exposed?

Issues are resolved on a best-effort basis, so there's no ETA. A quick way to fix this would be to change the hardcoded hull count to something like 10 as I suggested, but this may introduce performance issues in certain situations.

Calinou avatar Jan 25 '23 18:01 Calinou

I can confirm this is behavior creates a regression in Godot 4.0. A simple concave shape mesh creates multiple convex collision shapes in Godot 3.5 when I use "Create Multiple Convex Collision Siblings", but in Godot 4.0 the same concave shape mesh creates one single convex block.

I'm annexing a simple project with the shape in question. I haven't tested the PR yet, but it's weird that the default values for creating convex shapes in Godot 4.0 is worse than Godot 3.5; the behavior for a simple mesh like this should be the same.

convex_shapes_regression.zip

henriquelalves avatar Mar 07 '23 17:03 henriquelalves