Misleading example demo_MBS_collision_trimesh
Based on the example demo_MBS_collision_trimesh, one might think that Chrono/bullet is able to do collision checking of concave meshes. Also, visually inspecting the simulation suggests that this is perfectly feasible (adding 30 objects instead of 15 and removed the spheres):
The objects are lying nicely stacked on each other and the simulation seems reasonable.
But using a different mesh, namely just the simplest possible cube instead but through a ChTriangleMeshConnected-object like this:
// const auto mesh = ChTriangleMeshConnected::CreateFromWavefrontFile(GetChronoDataFile("models/bulldozer/shoe_view.obj"), false, true);
const auto mesh = ChTriangleMeshConnected::CreateFromSTLFile("PathToBasicCube.stl", false);
, the simulation looks like this, where lots of the objects are lying inside each other:
Instead of assuming that the code is able to do this kind of collision checking, I would suggest that that example is modified to copy the triangles from mesh into a std::shared_ptr<ChTriangleMeshSoup> and then set that on the ChCollisionShapeTriangleMesh. This would result in ChCollisionModelBullet::injectTriangleMesh(...) creating convex decompositions of the meshes and therefore a much more stable simulation. Also, that approach would be usable as a starting approach for others wanting to implement something that requires simulations using concave meshes.
Hmm, using the convex decomposition might be slightly less trivial than what I initially thought because doing the above (creating a ChTriangleMeshSoup for the collision chape) in debug mode leads to an assert failure in "chrono_thirdparty\HACDv2\dgGoogol.cpp" line 280:
HACD_ASSERT (hacd::HaI64 (A.m_mantissa[0]) >= 0);
A final update is that it seems to work even better when being even more explicit about using convex hulls for the collision shape.
Add the following after having loaded and scaled the mesh:
// using HACDv2 convex decomposition for collision shape. Using ChTriangleMeshConnected directly will NOT give the desired/correct collisions.
auto pDecomposition = chrono_types::make_shared<ChConvexDecompositionHACDv2>();
pDecomposition->AddTriangleMesh(*mesh);
pDecomposition->SetParameters(512, // max hull count
256, // max hull merge
64, // max hull vettices
0.2f, // concavity
0.0f, // small cluster threshold
1e-9f // fuse tolerance
);
pDecomposition->ComputeConvexDecomposition();
std::vector<std::shared_ptr<ChCollisionShapeConvexHull>> CollisionShapes;
for (unsigned int j = 0; j < pDecomposition->GetHullCount(); j++) {
std::vector<ChVector3d> ptlist;
pDecomposition->GetConvexHullResult(j, ptlist);
if (ptlist.size() > 0) {
const auto shape_hull = chrono_types::make_shared<ChCollisionShapeConvexHull>(mesh_mat, ptlist);
CollisionShapes.push_back(shape_hull);
}
}
And then add those to the falling shapes like this:
for (const std::shared_ptr<ChCollisionShapeConvexHull>& pCollisionShape : CollisionShapes) {
falling->AddCollisionShape(pCollisionShape);
}
That gives the following nice pile of objects:
And when changing to the simple boxes, the objects no longer intersect: