From bbcb74235011bdabac16a69b872a7b55947fe8a8 Mon Sep 17 00:00:00 2001
From: Fabian Paus <fabian.paus@gmail.com>
Date: Mon, 28 Jan 2019 17:12:05 +0100
Subject: [PATCH] Support concave object collisions if the object is not
 dynamic

---
 .../BulletEngine/BulletObject.cpp             | 40 ++++++++++++-------
 .../BulletEngine/BulletObject.h               |  3 +-
 2 files changed, 27 insertions(+), 16 deletions(-)

diff --git a/SimDynamics/DynamicsEngine/BulletEngine/BulletObject.cpp b/SimDynamics/DynamicsEngine/BulletEngine/BulletObject.cpp
index 95a6633ac..c8bc7f3d5 100644
--- a/SimDynamics/DynamicsEngine/BulletEngine/BulletObject.cpp
+++ b/SimDynamics/DynamicsEngine/BulletEngine/BulletObject.cpp
@@ -208,11 +208,11 @@ namespace SimDynamics
         return result;
     }
 
-    btConvexHullShape* BulletObject::createConvexHullShape(VirtualRobot::TriMeshModelPtr trimesh)
+    btCollisionShape* BulletObject::createConvexHullShape(VirtualRobot::TriMeshModelPtr trimesh)
     {
         VR_ASSERT(trimesh);
         // create triangle shape
-        btTriangleMesh* btTrimesh = new btTriangleMesh();
+        btTrimesh.reset(new btTriangleMesh());
 
         //com = trimesh->getCOM();
 
@@ -247,25 +247,35 @@ namespace SimDynamics
         Eigen::Matrix4f comConv = sceneObject->getGlobalPoseVisualization() * comLoc;
         com = comConv.block(0,3,3,1);*/
 
-        // build convex hull
-        boost::shared_ptr<btConvexShape> btConvexShape(new btConvexTriangleMeshShape(btTrimesh));
-        btConvexShape->setMargin(btMargin);
 
-        boost::shared_ptr<btShapeHull> btHull(new btShapeHull(btConvexShape.get()));
-        btHull->buildHull(btMargin);
-        btConvexHullShape* btConvex = new btConvexHullShape();
-        btConvex->setLocalScaling(btVector3(1, 1, 1));
 
-        for (int i = 0; i < btHull->numVertices(); i++)
+        if (sceneObject->getSimulationType() == VirtualRobot::SceneObject::Physics::eKinematic)
         {
-            btConvex->addPoint(btHull->getVertexPointer()[i]);
+            // Support concave objects if they are not dynamic
+            return new btBvhTriangleMeshShape(btTrimesh.get(), false);
         }
+        else
+        {
+            // build convex hull
+            boost::shared_ptr<btConvexShape> btConvexShape(new btConvexTriangleMeshShape(btTrimesh.get()));
+            btConvexShape->setMargin(btMargin);
 
-        btConvex->setMargin(btMargin);
+            boost::shared_ptr<btShapeHull> btHull(new btShapeHull(btConvexShape.get()));
+            btHull->buildHull(btMargin);
+            btConvexHullShape* btConvex = new btConvexHullShape();
+            btConvex->setLocalScaling(btVector3(1, 1, 1));
 
-        // trimesh not needed any more
-        delete btTrimesh;
-        return btConvex;
+            for (int i = 0; i < btHull->numVertices(); i++)
+            {
+                btConvex->addPoint(btHull->getVertexPointer()[i]);
+            }
+
+            btConvex->setMargin(btMargin);
+
+            // Trimesh no longer needed
+            btTrimesh.reset();
+            return btConvex;
+        }
     }
 
     boost::shared_ptr<btRigidBody> BulletObject::getRigidBody()
diff --git a/SimDynamics/DynamicsEngine/BulletEngine/BulletObject.h b/SimDynamics/DynamicsEngine/BulletEngine/BulletObject.h
index 4d751dafa..adebc0e6b 100644
--- a/SimDynamics/DynamicsEngine/BulletEngine/BulletObject.h
+++ b/SimDynamics/DynamicsEngine/BulletEngine/BulletObject.h
@@ -100,10 +100,11 @@ namespace SimDynamics
         void setPoseIntern(const Eigen::Matrix4f& pose);
         btCollisionShape* getShapeFromPrimitive(VirtualRobot::Primitive::PrimitivePtr primitive);
 
-        btConvexHullShape* createConvexHullShape(VirtualRobot::TriMeshModelPtr trimesh);
+        btCollisionShape* createConvexHullShape(VirtualRobot::TriMeshModelPtr trimesh);
 
         boost::shared_ptr<btRigidBody> rigidBody;
         boost::shared_ptr<btCollisionShape> collisionShape; // bullet collision shape
+        std::unique_ptr<btTriangleMesh> btTrimesh;
 
         Eigen::Vector3f com; // com offset of trimesh
 
-- 
GitLab