Exemple #1
0
        private CollisionShape MakeCollisionShape()
        {
            CollisionShape shape;

            switch (Shape)
            {
            case Shape.Cube:
                shape = new BoxShape(1, 1, 1);
                break;

            case Shape.Sphere:
                shape = new SphereShape(1);
                break;

            case Shape.Cylinder:
                shape = new ConeShape(1, 1);
                break;

            case Shape.Wedge:
                var wedgeGeo = Primitives.WedgeGeometry;
                shape = new ConvexTriangleMeshShape(new TriangleIndexVertexArray(wedgeGeo.Indices, wedgeGeo.Vertices.Select(v => (BulletVector3)v.Position).ToList()));
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
            shape.Margin      *= PhysicsSimulation.Scale;
            shape.Margin       = 0;
            shape.LocalScaling = new BulletVector3(_size.x / 2, _size.y / 2, _size.z / 2);
            return(shape);
        }
Exemple #2
0
        public override void SetupFromMesh(Mesh mesh)
        {
            var triMesh = new TriangleMesh();

            int i = 0;

            var vertices = mesh.Vertices;

            while (i < mesh.Indices.Length)
            {
                triMesh.AddTriangle(
                    vertices[mesh.Indices[i++]],
                    vertices[mesh.Indices[i++]],
                    vertices[mesh.Indices[i++]]
                    );
            }

            var tempShape = new ConvexTriangleMeshShape(triMesh);

            using var tempHull = new ShapeHull(tempShape);

            tempHull.BuildHull(tempShape.Margin);

            collisionShape = new ConvexHullShape(tempHull.Vertices);
        }
Exemple #3
0
 private ConvexHullShape CreateHullApproximation(TriangleMesh triangleMesh)
 {
     using (var tmpConvexShape = new ConvexTriangleMeshShape(triangleMesh))
     {
         using (var hull = new ShapeHull(tmpConvexShape))
         {
             hull.BuildHull(tmpConvexShape.Margin);
             var convexShape = new ConvexHullShape(hull.Vertices);
             if (_enableSat)
             {
                 convexShape.InitializePolyhedralFeatures();
             }
             return(convexShape);
         }
     }
 }
        private static CollisionShape loadConvexHull(Mesh mesh, Vector3 scale)
        {
            TriangleMesh trimesh = new TriangleMesh();

            // Shift vertices in so margin is not visible
            Vector3 shift = Vector3.Normalize(scale) - new Vector3(0.04f);

            for (int i = 0; i < mesh.submeshes[0].vertex_data.Length; i += 3)
            {
                int index0 = (int)mesh.submeshes[0].index_data[i];
                int index1 = (int)mesh.submeshes[0].index_data[i + 1];
                int index2 = (int)mesh.submeshes[0].index_data[i + 2];

                Vector3 vertex0 = new Vector3(mesh.submeshes[0].vertex_data[index0].position.X, mesh.submeshes[0].vertex_data[index0].position.Y, mesh.submeshes[0].vertex_data[index0].position.Z) * (scale);
                //vertex0 *= shift;
                Vector3 vertex1 = new Vector3(mesh.submeshes[0].vertex_data[index1].position.X, mesh.submeshes[0].vertex_data[index1].position.Y, mesh.submeshes[0].vertex_data[index1].position.Z) * (scale);
                //vertex1 *= shift;
                Vector3 vertex2 = new Vector3(mesh.submeshes[0].vertex_data[index2].position.X, mesh.submeshes[0].vertex_data[index2].position.Y, mesh.submeshes[0].vertex_data[index2].position.Z) * (scale);
                //vertex2 *= shift;

                trimesh.AddTriangle(vertex0, vertex1, vertex2);
            }
            ConvexShape tmpShape = new ConvexTriangleMeshShape(trimesh);

            ShapeHull hull   = new ShapeHull(tmpShape);
            float     margin = tmpShape.Margin;

            hull.BuildHull(margin);
            tmpShape.UserObject = hull;

            ConvexHullShape convexShape = new ConvexHullShape();

            foreach (Vector3 v in hull.Vertices)
            {
                convexShape.AddPoint(v);
            }

            hull.Dispose();
            tmpShape.Dispose();


            return(convexShape);
        }
Exemple #5
0
 public override CollisionShape GetCollisionShape()
 {
     if (collisionShapePtr == null)
     {
         Vector3[] verts = hullMesh.vertices;
         int[]     tris  = hullMesh.triangles;
         //todo test for convex. Make convex if not.
         TriangleMesh tm = new TriangleMesh();
         for (int i = 0; i < tris.Length; i += 3)
         {
             tm.AddTriangle(verts[tris[i]].ToBullet(),
                            verts[tris[i + 1]].ToBullet(),
                            verts[tris[i + 2]].ToBullet(),
                            true);
         }
         collisionShapePtr = new ConvexTriangleMeshShape(tm);
     }
     return(collisionShapePtr);
 }
        ConvexTriangleMeshShape _CreateConvexTriangleMeshShape()
        {
            Vector3[] verts = hullMesh.vertices;
            int[]     tris  = hullMesh.triangles;
            //todo test for convex. Make convex if not.
            TriangleMesh tm = new TriangleMesh();

            for (int i = 0; i < tris.Length; i += 3)
            {
                tm.AddTriangle(verts[tris[i]].ToBullet(),
                               verts[tris[i + 1]].ToBullet(),
                               verts[tris[i + 2]].ToBullet(),
                               true);
            }
            ConvexTriangleMeshShape ms = new ConvexTriangleMeshShape(tm);

            ms.LocalScaling = m_localScaling.ToBullet();
            return(ms);
        }
        private static RigidBody CreateRigidBodyFromTgcMesh(TgcMesh mesh, TGCVector3 position, float mass)
        {
            var          vertexCoords = mesh.getVertexPositions();
            TriangleMesh triangleMesh = new TriangleMesh();

            for (int i = 0; i < vertexCoords.Length; i = i + 3)
            {
                triangleMesh.AddTriangle(vertexCoords[i].ToBsVector, vertexCoords[i + 1].ToBsVector, vertexCoords[i + 2].ToBsVector);
            }

            var transformationMatrix = TGCMatrix.RotationYawPitchRoll(0, 0, 0).ToBsMatrix;

            transformationMatrix.Origin = position.ToBsVector;
            DefaultMotionState motionState = new DefaultMotionState(transformationMatrix);
            var bulletShape  = new ConvexTriangleMeshShape(triangleMesh, true);
            var localInertia = bulletShape.CalculateLocalInertia(mass);

            var bodyInfo = new RigidBodyConstructionInfo(mass, motionState, bulletShape, localInertia);

            var rigidBody = new RigidBody(bodyInfo);

            return(rigidBody);
        }
Exemple #8
0
        public RigidBody AddDynamicGeometryAccurateConvel(ColladaGeometry geometry, Matrix4 transform)
        {
            TriangleMesh mesh = new TriangleMesh();

            foreach (Triangle tri in geometry.triangles)
            {
                mesh.AddTriangle(
                    tri.vertices[0],
                    tri.vertices[1],
                    tri.vertices[2]
                    );
            }
            CollisionShape shape = new ConvexTriangleMeshShape(mesh);



            shape.UserObject = geometry;

            collisionShapes.Add(shape);

            RigidBody body = CreateRigidBody(geometry.triangles.Count, transform, shape);

            return(body);
        }
Exemple #9
0
        protected override void OnInitializePhysics()
        {
            ManifoldPoint.ContactAdded += MyContactCallback;

            SetupEmptyDynamicsWorld();

            WavefrontObj wo     = new WavefrontObj();
            int          tcount = wo.LoadObj("data/file.obj");

            if (tcount > 0)
            {
                TriangleMesh trimesh = new TriangleMesh();
                trimeshes.Add(trimesh);

                Vector3        localScaling = new Vector3(6, 6, 6);
                List <int>     indices      = wo.Indices;
                List <Vector3> vertices     = wo.Vertices;

                int i;
                for (i = 0; i < tcount; i++)
                {
                    int index0 = indices[i * 3];
                    int index1 = indices[i * 3 + 1];
                    int index2 = indices[i * 3 + 2];

                    Vector3 vertex0 = vertices[index0] * localScaling;
                    Vector3 vertex1 = vertices[index1] * localScaling;
                    Vector3 vertex2 = vertices[index2] * localScaling;

                    trimesh.AddTriangle(vertex0, vertex1, vertex2);
                }

                ConvexShape tmpConvexShape = new ConvexTriangleMeshShape(trimesh);

                //create a hull approximation
                ShapeHull hull   = new ShapeHull(tmpConvexShape);
                float     margin = tmpConvexShape.Margin;
                hull.BuildHull(margin);
                tmpConvexShape.UserObject = hull;

                ConvexHullShape convexShape = new ConvexHullShape();
                foreach (Vector3 v in hull.Vertices)
                {
                    convexShape.AddPoint(v);
                }

                if (sEnableSAT)
                {
                    convexShape.InitializePolyhedralFeatures();
                }
                tmpConvexShape.Dispose();
                //hull.Dispose();


                CollisionShapes.Add(convexShape);

                float mass = 1.0f;

                LocalCreateRigidBody(mass, Matrix.Translation(0, 2, 14), convexShape);

                const bool     useQuantization = true;
                CollisionShape concaveShape    = new BvhTriangleMeshShape(trimesh, useQuantization);
                LocalCreateRigidBody(0, Matrix.Translation(convexDecompositionObjectOffset), concaveShape);

                CollisionShapes.Add(concaveShape);


                // Bullet Convex Decomposition

                FileStream   outputFile = new FileStream("file_convex.obj", FileMode.Create, FileAccess.Write);
                StreamWriter writer     = new StreamWriter(outputFile);

                DecompDesc desc = new DecompDesc
                {
                    mVertices    = wo.Vertices.ToArray(),
                    mTcount      = tcount,
                    mIndices     = wo.Indices.ToArray(),
                    mDepth       = 5,
                    mCpercent    = 5,
                    mPpercent    = 15,
                    mMaxVertices = 16,
                    mSkinWidth   = 0.0f
                };

                MyConvexDecomposition convexDecomposition = new MyConvexDecomposition(writer, this);
                desc.mCallback = convexDecomposition;


                // HACD

                Hacd myHACD = new Hacd();
                myHACD.SetPoints(wo.Vertices);
                myHACD.SetTriangles(wo.Indices);
                myHACD.CompacityWeight = 0.1;
                myHACD.VolumeWeight    = 0.0;

                // HACD parameters
                // Recommended parameters: 2 100 0 0 0 0
                int          nClusters = 2;
                const double concavity = 100;
                //bool invert = false;
                const bool addExtraDistPoints      = false;
                const bool addNeighboursDistPoints = false;
                const bool addFacesPoints          = false;

                myHACD.NClusters               = nClusters;       // minimum number of clusters
                myHACD.VerticesPerConvexHull   = 100;             // max of 100 vertices per convex-hull
                myHACD.Concavity               = concavity;       // maximum concavity
                myHACD.AddExtraDistPoints      = addExtraDistPoints;
                myHACD.AddNeighboursDistPoints = addNeighboursDistPoints;
                myHACD.AddFacesPoints          = addFacesPoints;

                myHACD.Compute();
                nClusters = myHACD.NClusters;

                myHACD.Save("output.wrl", false);


                if (true)
                {
                    CompoundShape compound = new CompoundShape();
                    CollisionShapes.Add(compound);

                    Matrix trans = Matrix.Identity;

                    for (int c = 0; c < nClusters; c++)
                    {
                        //generate convex result
                        Vector3[] points;
                        int[]     triangles;
                        myHACD.GetCH(c, out points, out triangles);

                        ConvexResult r = new ConvexResult(points, triangles);
                        convexDecomposition.ConvexDecompResult(r);
                    }

                    for (i = 0; i < convexDecomposition.convexShapes.Count; i++)
                    {
                        Vector3 centroid = convexDecomposition.convexCentroids[i];
                        trans = Matrix.Translation(centroid);
                        ConvexHullShape convexShape2 = convexDecomposition.convexShapes[i] as ConvexHullShape;
                        compound.AddChildShape(trans, convexShape2);

                        RigidBody body = LocalCreateRigidBody(1.0f, trans, convexShape2);
                    }

#if true
                    mass  = 10.0f;
                    trans = Matrix.Translation(-convexDecompositionObjectOffset);
                    RigidBody body2 = LocalCreateRigidBody(mass, trans, compound);
                    body2.CollisionFlags |= CollisionFlags.CustomMaterialCallback;

                    convexDecompositionObjectOffset.Z = 6;
                    trans = Matrix.Translation(-convexDecompositionObjectOffset);
                    body2 = LocalCreateRigidBody(mass, trans, compound);
                    body2.CollisionFlags |= CollisionFlags.CustomMaterialCallback;

                    convexDecompositionObjectOffset.Z = -6;
                    trans = Matrix.Translation(-convexDecompositionObjectOffset);
                    body2 = LocalCreateRigidBody(mass, trans, compound);
                    body2.CollisionFlags |= CollisionFlags.CustomMaterialCallback;
#endif
                }

                writer.Dispose();
                outputFile.Dispose();
            }
        }
        private void CreateMesh(string filePath)
        {
            Bodies       = new List <RigidBody>();
            VisualMeshes = new List <Mesh>();

            BXDAMesh mesh = new BXDAMesh();

            mesh.ReadFromFile(filePath);

            foreach (FieldNode node in NodeGroup.EnumerateAllLeafFieldNodes())
            {
                if (!GetPropertySets().ContainsKey(node.PropertySetID))
                {
                    return;
                }

                PropertySet    current  = GetPropertySets()[node.PropertySetID];
                CollisionShape subShape = null;
                switch (current.Collider.CollisionType)
                {
                case PropertySet.PropertySetCollider.PropertySetCollisionType.BOX:
                {
                    //Create a box shape
                    //This is a mess, though I was told that this is how it works
                    Vector3[]             vertices = MeshUtilities.DataToVector(mesh.meshes[node.SubMeshID].verts);
                    StridingMeshInterface temp = MeshUtilities.BulletShapeFromSubMesh(mesh.meshes[node.SubMeshID]);
                    Vector3 min, max;
                    temp.CalculateAabbBruteForce(out min, out max);

                    PropertySet.BoxCollider colliderInfo = (PropertySet.BoxCollider)current.Collider;
                    subShape = new BoxShape((max - min) * colliderInfo.Scale.Convert() * 0.5f);
                    if (debug)
                    {
                        Console.WriteLine("Created Box");
                    }
                    break;
                }

                case PropertySet.PropertySetCollider.PropertySetCollisionType.SPHERE:
                {
                    //Create a sphere shape
                    PropertySet.SphereCollider colliderInfo = (PropertySet.SphereCollider)current.Collider;
                    subShape = new SphereShape(colliderInfo.Scale);
                    if (debug)
                    {
                        Console.WriteLine("Created Sphere");
                    }
                    break;
                }

                case PropertySet.PropertySetCollider.PropertySetCollisionType.MESH:
                {
                    //Create a mesh shape
                    if (node.CollisionMeshID == -1)
                    {
                        break;
                    }

                    PropertySet.MeshCollider colliderInfo = (PropertySet.MeshCollider)current.Collider;

                    Vector3[] vertices = MeshUtilities.DataToVector(mesh.colliders[node.CollisionMeshID].verts);

                    if (colliderInfo.Convex)
                    {
                        subShape = new ConvexHullShape(vertices);
                        if (debug)
                        {
                            Console.WriteLine("Created Convex Mesh");
                        }
                    }
                    else
                    {
                        StridingMeshInterface sMesh = MeshUtilities.BulletShapeFromSubMesh(mesh.colliders[node.CollisionMeshID]);
                        subShape = new ConvexTriangleMeshShape(sMesh, true);         //still not really concave
                        if (debug)
                        {
                            Console.WriteLine("Created Concave Mesh");
                        }
                    }
                    break;
                }
                }

                if (null == subShape)
                {
                    return;
                }

                //set sub shape local position/rotation and add it to the compound shape
                Vector3    Translation = node.Position.Convert();
                Quaternion rotation    = node.Rotation.Convert();

                DefaultMotionState motion = new DefaultMotionState(Matrix4.CreateFromQuaternion(rotation) * Matrix4.CreateTranslation(Translation));
                motion.CenterOfMassOffset = Matrix4.CreateTranslation(mesh.physics.centerOfMass.Convert());

                RigidBodyConstructionInfo info = new RigidBodyConstructionInfo(current.Mass, motion, subShape, subShape.CalculateLocalInertia(current.Mass));
                info.Friction = current.Friction;
                Bodies.Add(new RigidBody(info));

                VisualMeshes.Add(new Mesh(mesh.meshes[node.SubMeshID], Translation));
                if (debug)
                {
                    Console.WriteLine("Created " + node.PropertySetID);
                }
            }
        }
Exemple #11
0
        protected override void OnInitializePhysics()
        {
            ManifoldPoint.ContactAdded += MyContactCallback;

            SetupEmptyDynamicsWorld();

            //CompoundCollisionAlgorithm.CompoundChildShapePairCallback = MyCompoundChildShapeCallback;
            convexDecompositionObjectOffset = new Vector3(10, 0, 0);


            // Load wavefront file
            var wo = new WavefrontObj();

            //string filename = UnityEngine.Application.dataPath + "/BulletUnity/Examples/Scripts/BulletSharpDemos/ConvexDecompositionDemo/data/file.obj";
            UnityEngine.TextAsset bytes      = (UnityEngine.TextAsset)UnityEngine.Resources.Load("file.obj");
            System.IO.Stream      byteStream = new System.IO.MemoryStream(bytes.bytes);

            int tcount = wo.LoadObj(byteStream);

            if (tcount == 0)
            {
                return;
            }

            // Convert file data to TriangleMesh
            var trimesh = new TriangleMesh();

            trimeshes.Add(trimesh);

            Vector3        localScaling = new Vector3(6, 6, 6);
            List <int>     indices      = wo.Indices;
            List <Vector3> vertices     = wo.Vertices;

            int i;

            for (i = 0; i < tcount; i++)
            {
                int index0 = indices[i * 3];
                int index1 = indices[i * 3 + 1];
                int index2 = indices[i * 3 + 2];

                Vector3 vertex0 = vertices[index0] * localScaling;
                Vector3 vertex1 = vertices[index1] * localScaling;
                Vector3 vertex2 = vertices[index2] * localScaling;

                trimesh.AddTriangleRef(ref vertex0, ref vertex1, ref vertex2);
            }

            // Create a hull approximation
            ConvexHullShape convexShape;

            using (var tmpConvexShape = new ConvexTriangleMeshShape(trimesh))
            {
                using (var hull = new ShapeHull(tmpConvexShape))
                {
                    hull.BuildHull(tmpConvexShape.Margin);
                    convexShape = new ConvexHullShape(hull.Vertices);
                }
            }
            if (sEnableSAT)
            {
                convexShape.InitializePolyhedralFeatures();
            }
            CollisionShapes.Add(convexShape);


            // Add non-moving body to world
            float mass = 1.0f;

            LocalCreateRigidBody(mass, Matrix.Translation(0, 2, 14), convexShape);

            const bool useQuantization = true;
            var        concaveShape    = new BvhTriangleMeshShape(trimesh, useQuantization);

            LocalCreateRigidBody(0, Matrix.Translation(convexDecompositionObjectOffset), concaveShape);

            CollisionShapes.Add(concaveShape);


            // HACD
            var hacd = new Hacd();

            hacd.SetPoints(wo.Vertices);
            hacd.SetTriangles(wo.Indices);
            hacd.CompacityWeight = 0.1;
            hacd.VolumeWeight    = 0.0;

            // Recommended HACD parameters: 2 100 false false false
            hacd.NClusters               = 2;        // minimum number of clusters
            hacd.Concavity               = 100;      // maximum concavity
            hacd.AddExtraDistPoints      = false;
            hacd.AddNeighboursDistPoints = false;
            hacd.AddFacesPoints          = false;
            hacd.NVerticesPerCH          = 100; // max of 100 vertices per convex-hull

            hacd.Compute();
            hacd.Save("output.wrl", false);


            // Generate convex result
            var outputFile = new FileStream("file_convex.obj", FileMode.Create, FileAccess.Write);
            var writer     = new StreamWriter(outputFile);

            var convexDecomposition = new ConvexDecomposition(writer, this);

            convexDecomposition.LocalScaling = localScaling;

            for (int c = 0; c < hacd.NClusters; c++)
            {
                int      nVertices    = hacd.GetNPointsCH(c);
                int      trianglesLen = hacd.GetNTrianglesCH(c) * 3;
                double[] points       = new double[nVertices * 3];
                long[]   triangles    = new long[trianglesLen];
                hacd.GetCH(c, points, triangles);

                if (trianglesLen == 0)
                {
                    continue;
                }

                Vector3[] verticesArray = new Vector3[nVertices];
                int       vi3           = 0;
                for (int vi = 0; vi < nVertices; vi++)
                {
                    verticesArray[vi] = new Vector3(
                        (float)points[vi3], (float)points[vi3 + 1], (float)points[vi3 + 2]);
                    vi3 += 3;
                }

                int[] trianglesInt = new int[trianglesLen];
                for (int ti = 0; ti < trianglesLen; ti++)
                {
                    trianglesInt[ti] = (int)triangles[ti];
                }

                convexDecomposition.ConvexDecompResult(verticesArray, trianglesInt);
            }


            // Combine convex shapes into a compound shape
            var compound = new CompoundShape();

            for (i = 0; i < convexDecomposition.convexShapes.Count; i++)
            {
                Vector3 centroid     = convexDecomposition.convexCentroids[i];
                var     convexShape2 = convexDecomposition.convexShapes[i];
                Matrix  trans        = Matrix.Translation(centroid);
                if (sEnableSAT)
                {
                    convexShape2.InitializePolyhedralFeatures();
                }
                CollisionShapes.Add(convexShape2);
                compound.AddChildShape(trans, convexShape2);

                LocalCreateRigidBody(1.0f, trans, convexShape2);
            }
            CollisionShapes.Add(compound);

            writer.Dispose();
            outputFile.Dispose();

#if true
            mass = 10.0f;
            var body2 = LocalCreateRigidBody(mass, Matrix.Translation(-convexDecompositionObjectOffset), compound);
            body2.CollisionFlags |= CollisionFlags.CustomMaterialCallback;

            convexDecompositionObjectOffset.Z = 6;
            body2 = LocalCreateRigidBody(mass, Matrix.Translation(-convexDecompositionObjectOffset), compound);
            body2.CollisionFlags |= CollisionFlags.CustomMaterialCallback;

            convexDecompositionObjectOffset.Z = -6;
            body2 = LocalCreateRigidBody(mass, Matrix.Translation(-convexDecompositionObjectOffset), compound);
            body2.CollisionFlags |= CollisionFlags.CustomMaterialCallback;
#endif
        }
        protected override void OnInitializePhysics()
        {
            ManifoldPoint.ContactAdded += MyContactCallback;

            SetupEmptyDynamicsWorld();

            CompoundCollisionAlgorithm.CompoundChildShapePairCallback = MyCompoundChildShapeCallback;
            convexDecompositionObjectOffset = new Vector3(10, 0, 0);


            // Load wavefront file
            var wo     = new WavefrontObj();
            int tcount = wo.LoadObj("data/file.obj");

            if (tcount == 0)
            {
                return;
            }

            // Convert file data to TriangleMesh
            var trimesh = new TriangleMesh();

            trimeshes.Add(trimesh);

            Vector3        localScaling = new Vector3(6, 6, 6);
            List <int>     indices      = wo.Indices;
            List <Vector3> vertices     = wo.Vertices;

            int i;

            for (i = 0; i < tcount; i++)
            {
                int index0 = indices[i * 3];
                int index1 = indices[i * 3 + 1];
                int index2 = indices[i * 3 + 2];

                Vector3 vertex0 = vertices[index0] * localScaling;
                Vector3 vertex1 = vertices[index1] * localScaling;
                Vector3 vertex2 = vertices[index2] * localScaling;

                trimesh.AddTriangleRef(ref vertex0, ref vertex1, ref vertex2);
            }

            // Create a hull approximation
            ConvexHullShape convexShape;

            using (var tmpConvexShape = new ConvexTriangleMeshShape(trimesh))
            {
                using (var hull = new ShapeHull(tmpConvexShape))
                {
                    hull.BuildHull(tmpConvexShape.Margin);
                    convexShape = new ConvexHullShape(hull.Vertices);
                }
            }
            if (sEnableSAT)
            {
                convexShape.InitializePolyhedralFeatures();
            }
            CollisionShapes.Add(convexShape);


            // Add non-moving body to world
            float mass = 1.0f;

            LocalCreateRigidBody(mass, Matrix.Translation(0, 2, 14), convexShape);

            const bool useQuantization = true;
            var        concaveShape    = new BvhTriangleMeshShape(trimesh, useQuantization);

            LocalCreateRigidBody(0, Matrix.Translation(convexDecompositionObjectOffset), concaveShape);

            CollisionShapes.Add(concaveShape);


            // HACD
            var hacd = new Hacd();

            hacd.SetPoints(wo.Vertices);
            hacd.SetTriangles(wo.Indices);
            hacd.CompacityWeight = 0.1;
            hacd.VolumeWeight    = 0.0;

            // Recommended HACD parameters: 2 100 false false false
            hacd.NClusters                = 2;       // minimum number of clusters
            hacd.Concavity                = 100;     // maximum concavity
            hacd.AddExtraDistPoints       = false;
            hacd.AddNeighboursDistPoints  = false;
            hacd.AddFacesPoints           = false;
            hacd.NumVerticesPerConvexHull = 100;     // max of 100 vertices per convex-hull

            hacd.Compute();
            hacd.Save("output.wrl", false);


            // Generate convex result
            var outputFile = new FileStream("file_convex.obj", FileMode.Create, FileAccess.Write);
            var writer     = new StreamWriter(outputFile);

            var convexDecomposition = new ConvexDecomposition(writer, this);

            convexDecomposition.LocalScaling = localScaling;

            for (int c = 0; c < hacd.NClusters; c++)
            {
                Vector3[] points;
                int[]     triangles;
                hacd.GetCH(c, out points, out triangles);

                convexDecomposition.ConvexDecompResult(points, triangles);
            }


            // Combine convex shapes into a compound shape
            var compound = new CompoundShape();

            for (i = 0; i < convexDecomposition.convexShapes.Count; i++)
            {
                Vector3 centroid     = convexDecomposition.convexCentroids[i];
                Matrix  trans        = Matrix.Translation(centroid);
                var     convexShape2 = convexDecomposition.convexShapes[i] as ConvexHullShape;
                if (sEnableSAT)
                {
                    convexShape2.InitializePolyhedralFeatures();
                }
                CollisionShapes.Add(convexShape2);
                compound.AddChildShape(trans, convexShape2);

                LocalCreateRigidBody(1.0f, trans, convexShape2);
            }
            CollisionShapes.Add(compound);

            writer.Dispose();
            outputFile.Dispose();

#if true
            mass = 10.0f;
            var body2 = LocalCreateRigidBody(mass, Matrix.Translation(-convexDecompositionObjectOffset), compound);
            body2.CollisionFlags |= CollisionFlags.CustomMaterialCallback;

            convexDecompositionObjectOffset.Z = 6;
            body2 = LocalCreateRigidBody(mass, Matrix.Translation(-convexDecompositionObjectOffset), compound);
            body2.CollisionFlags |= CollisionFlags.CustomMaterialCallback;

            convexDecompositionObjectOffset.Z = -6;
            body2 = LocalCreateRigidBody(mass, Matrix.Translation(-convexDecompositionObjectOffset), compound);
            body2.CollisionFlags |= CollisionFlags.CustomMaterialCallback;
#endif
        }
        public void DebugDrawObject(Matrix worldTransform, CollisionShape shape, Vector3 color)
        {
            if (shape.ShapeType == BroadphaseNativeTypes.Compound)
            {
                CompoundShape compoundShape = shape as CompoundShape;
                for (int i = compoundShape.ChildShapeCount - 1; i >= 0; i--)
                {
                    Matrix         childTrans = compoundShape.GetChildTransform(i);
                    CollisionShape colShape   = compoundShape.GetChildShape(i);
                    DebugDrawObject(worldTransform * childTrans, colShape, color);
                }
            }
            else
            {
                switch (shape.ShapeType)
                {
                case BroadphaseNativeTypes.Sphere:
                {
                    SphereShape sphereShape = shape as SphereShape;
                    float       radius      = sphereShape.Margin;                         //radius doesn't include the margin, so draw with margin
                    Vector3     start       = worldTransform.Translation;
                    DebugDrawer.DrawLine(start, start + Vector3.TransformNormal(new Vector3(radius, 0, 0), worldTransform), color);
                    DebugDrawer.DrawLine(start, start + Vector3.TransformNormal(new Vector3(0, radius, 0), worldTransform), color);
                    DebugDrawer.DrawLine(start, start + Vector3.TransformNormal(new Vector3(0, 0, radius), worldTransform), color);
                    //drawSphere
                    break;
                }

                case BroadphaseNativeTypes.MultiSphere:
                case BroadphaseNativeTypes.Cone:
                {
                    ConeShape coneShape = shape as ConeShape;
                    float     radius    = coneShape.Radius;                             //+coneShape->getMargin();
                    float     height    = coneShape.Height;                             //+coneShape->getMargin();
                    Vector3   start     = worldTransform.Translation;
                    DebugDrawer.DrawLine(start + Vector3.TransformNormal(new Vector3(0f, 0f, 0.5f * height), worldTransform), start + Vector3.TransformNormal(new Vector3(radius, 0f, -0.5f * height), worldTransform), color);
                    DebugDrawer.DrawLine(start + Vector3.TransformNormal(new Vector3(0f, 0f, 0.5f * height), worldTransform), start + Vector3.TransformNormal(new Vector3(-radius, 0f, -0.5f * height), worldTransform), color);
                    DebugDrawer.DrawLine(start + Vector3.TransformNormal(new Vector3(0f, 0f, 0.5f * height), worldTransform), start + Vector3.TransformNormal(new Vector3(0f, radius, -0.5f * height), worldTransform), color);
                    DebugDrawer.DrawLine(start + Vector3.TransformNormal(new Vector3(0f, 0f, 0.5f * height), worldTransform), start + Vector3.TransformNormal(new Vector3(0f, -radius, -0.5f * height), worldTransform), color);
                    break;
                }

                case BroadphaseNativeTypes.Cylinder:
                {
                    CylinderShape cylinder     = shape as CylinderShape;
                    int           upAxis       = cylinder.UpAxis;
                    float         radius       = cylinder.Radius;
                    float         halfHeight   = MathHelper.GetElement(cylinder.HalfExtents, upAxis);
                    Vector3       start        = worldTransform.Translation;
                    Vector3       offsetHeight = new Vector3();
                    MathHelper.SetElement(ref offsetHeight, upAxis, halfHeight);
                    Vector3 offsetRadius = new Vector3();
                    MathHelper.SetElement(ref offsetRadius, (upAxis + 1) % 3, radius);
                    DebugDrawer.DrawLine(start + Vector3.TransformNormal(offsetHeight + offsetRadius, worldTransform), start + Vector3.TransformNormal(-offsetHeight + offsetRadius, worldTransform), color);
                    DebugDrawer.DrawLine(start + Vector3.TransformNormal(offsetHeight - offsetRadius, worldTransform), start + Vector3.TransformNormal(-offsetHeight - offsetRadius, worldTransform), color);
                    break;
                }

                default:
                {
                    if (shape.ShapeType == BroadphaseNativeTypes.TriangleMesh)
                    {
                        TriangleMeshShape concaveMesh = shape as TriangleMeshShape;
                        //btVector3 aabbMax(1e30f,1e30f,1e30f);
                        //btVector3 aabbMax(100,100,100);//1e30f,1e30f,1e30f);

                        //todo pass camera, for some culling
                        Vector3 aabbMax = new Vector3(1e30f, 1e30f, 1e30f);
                        Vector3 aabbMin = new Vector3(-1e30f, -1e30f, -1e30f);

                        DebugDrawCallback drawCallback = new DebugDrawCallback(DebugDrawer, worldTransform, color);
                        concaveMesh.ProcessAllTriangles(drawCallback, aabbMin, aabbMax);
                    }

                    if (shape.ShapeType == BroadphaseNativeTypes.ConvexTriangleMesh)
                    {
                        ConvexTriangleMeshShape convexMesh = shape as ConvexTriangleMeshShape;
                        //todo: pass camera for some culling
                        Vector3 aabbMax = new Vector3(1e30f, 1e30f, 1e30f);
                        Vector3 aabbMin = new Vector3(-1e30f, -1e30f, -1e30f);
                        //DebugDrawcallback drawCallback;
                        DebugDrawCallback drawCallback = new DebugDrawCallback(DebugDrawer, worldTransform, color);
                        convexMesh.getStridingMesh().InternalProcessAllTriangles(drawCallback, aabbMin, aabbMax);
                    }

                    // for polyhedral shapes
                    if (shape.IsPolyhedral)
                    {
                        PolyhedralConvexShape polyshape = shape as PolyhedralConvexShape;

                        for (int i = 0; i < polyshape.EdgeCount; i++)
                        {
                            Vector3 a, b;
                            polyshape.GetEdge(i, out a, out b);
                            a = Vector3.TransformNormal(a, worldTransform);
                            b = Vector3.TransformNormal(b, worldTransform);
                            DebugDrawer.DrawLine(a, b, color);
                        }
                    }
                    break;
                }
                }
            }
        }