jrouwe / JoltPhysics

A multi core friendly rigid body physics and collision detection library. Written in C++. Suitable for games and VR applications. Used by Horizon Forbidden West.
MIT License
6.55k stars 425 forks source link

MeshShape mMaxTrianglesPerLeaf assert #1127

Closed DevLewa closed 3 months ago

DevLewa commented 4 months ago

I recently ran into an issue with a trianglemesh consisting of around 250k triangles. (A large terrain.)

In debug mode the debugger throws an error in the AABBTreeBuilder::BuildInternal function while building the mesh:

screen

Issue being that "mMaxTrianglesPerLeaf" is set to 8 while inTriangles.count seems to be returning 10. (in this case "right" has this range.) "left" is in the range of (40644-40644)

left

However, i wasn't able to notice any issues in release mode (so far). Is the mesh too large / is there a limit for MeshShapes with regards to trianglecount? (From the source code documentation the mMaxTrianglesPerLeaf should be in the range of 4-8?) What could be the root cause for this issue / how should this be handled? (Haven't investigated the TreeBuilder code deep enough yet.)

jrouwe commented 3 months ago

Hello,

First of all, this is not an error but a warning. The resulting mesh will work, but it will not be as efficient as it could be.

This code builds a tree structure to accelerate collision detection. At top level it will start with all triangles and then recursively divides the triangles into two batches. To do so it it uses an algorithm described here.

When this warning triggers, the algorithm was not able to create a reasonable split for the triangles. This usually happens when the triangles in a batch are intersecting. Alternatively, they could be overlapping when projected on the 3 coordinate axis.

Your error triggers when there are only 10 triangles left, so the performance loss is not a big deal. You could try to pass your mesh through some mesh cleaning/optimization algorithm if you want to get rid of this warning.