public btPolyhedralConvexShape GetPolihedralConvexShape()
        {
            IntPtr cPtr = BulletCollisionPINVOKE.btConvexHullShape_GetPolihedralConvexShape(swigCPtr);
            btPolyhedralConvexShape ret = (cPtr == IntPtr.Zero) ? null : new btPolyhedralConvexShape(cPtr, false);

            return(ret);
        }
 internal static HandleRef getCPtr(btPolyhedralConvexShape obj)
 {
     return((obj == null) ? new HandleRef(null, IntPtr.Zero) : obj.swigCPtr);
 }
 internal static HandleRef getCPtr(btPolyhedralConvexShape obj)
 {
     return (obj == null) ? new HandleRef(null, IntPtr.Zero) : obj.swigCPtr;
 }
예제 #4
0
    public bool OnBulletCreate()
    {
        if( collisionShapePtr != null ) // can't be created multi-times
            return true;

        if( ShapeType == CollisionShapeType.BoxShape)
        {
            btVector3 vec = new btVector3(BoxShapeVec.x*transform.localScale.x,BoxShapeVec.y*transform.localScale.y,BoxShapeVec.z*transform.localScale.z);
            boxShape = new btBoxShape(vec.GetSwigPtr());
            collisionShapePtr = boxShape.GetSwigPtr();
        }
        else if( ShapeType == CollisionShapeType.SphereShape)
        {
            float maxFactor = Mathf.Max(transform.localScale.x,transform.localScale.y);
            maxFactor = Mathf.Max(transform.localScale.z,maxFactor);
            sphereShape = new btSphereShape(SphereShapeRadius*maxFactor);
            collisionShapePtr = sphereShape.GetSwigPtr();
        }
        else if( ShapeType == CollisionShapeType.CapsuleShape)
        {
            float maxFactor = Mathf.Max(transform.localScale.x,transform.localScale.z);

            capsuleShape = new btCapsuleShape(CapsuleRadius*maxFactor,CapsuleHeight*transform.localScale.y);
            collisionShapePtr = capsuleShape.GetSwigPtr();
        }
        else if( ShapeType == CollisionShapeType.CylinderShape)
        {
            float maxFactor = Mathf.Max(transform.localScale.x,transform.localScale.z);
            btVector3 vec = new btVector3(CylinderRadius*maxFactor,CylinderHeight*transform.localScale.y,CylinderRadius*maxFactor);
            cylinderShape = new btCylinderShape(vec.GetSwigPtr());
            collisionShapePtr = cylinderShape.GetSwigPtr();
        }
        else if( ShapeType == CollisionShapeType.ConeShape)
        {
            float maxFactor = Mathf.Max(transform.localScale.x,transform.localScale.z);
            coneShape = new btConeShape(ConeRadius*maxFactor,ConeHeight*transform.localScale.y);
            collisionShapePtr = coneShape.GetSwigPtr();
        }
        else if( ShapeType == CollisionShapeType.ConvexHull )
        {
            if(CheckUnityMesh() == false)
                return false;

            List<float> vertexposList = new List<float>();

            for(int index=0;index<meshFilter.mesh.vertexCount;index++)
            {
                Vector3 vec = meshFilter.mesh.vertices[index];
                vertexposList.Add(vec.x);
                vertexposList.Add(vec.y);
                vertexposList.Add(vec.z);
            }

            convexHull = new btConvexHullShape(vertexposList.ToArray(),meshFilter.mesh.vertexCount,3*sizeof(float));
            convexPolyhedral = convexHull.GetPolihedralConvexShape();
            //convexPolyhedral.initializePolyhedralFeatures();
            collisionShapePtr = convexHull.GetSwigPtr();

        }
        else if( ShapeType == CollisionShapeType.CompoundShape )
        {
            // use all its children's collision shapes to create itself...
            if( CollisionShapeArray == null || CollisionShapeArray.Length == 0 )
            {
                Debug.Log("There is no child collision shapes to use CompoundShape!");
                return false;
            }

            compoundShape = new btCompoundShape();

            for( int i=0;i<CollisionShapeArray.Length;i++)
            {
                if( CollisionShapeArray[i] == null )
                    continue;

                if( CollisionShapeArray[i].gameObject == gameObject )
                    continue;

                bool result = CollisionShapeArray[i].OnBulletCreate();
                if( result == false )
                {
                    Debug.Log(" Bullet Collision Create Error!");
                    return false;
                }

                Transform t = CollisionShapeArray[i].transform;
                Matrix4x4 objMatrix = Matrix4x4.TRS(t.localPosition,t.localRotation,t.localScale);

                btVector3 pos = new btVector3(t.localPosition.x,t.localPosition.y,t.localPosition.z);
                btQuaternion rot = new btQuaternion(t.localRotation.x,t.localRotation.y,t.localRotation.z,t.localRotation.w);

                btTransform trans = new btTransform(rot,pos);

                compoundShape.addChildShape(trans.GetSwigPtr(),CollisionShapeArray[i].GetCollisionShapePtr());
            }

            collisionShapePtr = compoundShape.GetSwigPtr();

        }
        else if( ShapeType == CollisionShapeType.BvhTriangleMeshShape )
        {
            if(CheckUnityMesh() == false)
                return false;
            List<float> verList = new List<float>();
            for(int index=0;index<meshFilter.mesh.vertexCount;index++)
            {
                Vector3 vec = meshFilter.mesh.vertices[index];
                //vec = transform.TransformPoint(vec);
                verList.Add(vec.x);
                verList.Add(vec.y);
                verList.Add(vec.z);
            }

            meshVertexArray = verList.ToArray();

            List<int> indexList = new List<int>();
            // Unity3D counter clock-wise to Bullet's clock wise.
            for( int i=0;i< meshFilter.mesh.triangles.Length;i+=3)
            {
                indexList.Add(meshFilter.mesh.triangles[i]);
                indexList.Add(meshFilter.mesh.triangles[i+2]);
                indexList.Add(meshFilter.mesh.triangles[i+1]);
            }

            meshIndexArray = indexList.ToArray();

            triangleArray = new btTriangleIndexVertexArray(indexList.Count/3,meshIndexArray,3*sizeof(int),
                                                                                      meshFilter.mesh.vertexCount,meshVertexArray,3*sizeof(float));
            bvhTriangleMeshShape = new btBvhTriangleMeshShape(triangleArray.GetSwigPtr(),true);
            collisionShapePtr = bvhTriangleMeshShape.GetSwigPtr();
        }
        else if( ShapeType == CollisionShapeType.StaticPlaneShape)
        {
            btVector3 vec = new btVector3(StaticPlaneNormal.x,StaticPlaneNormal.y,StaticPlaneNormal.z);
            staticPlaneShape = new btStaticPlaneShape(vec.GetSwigPtr(),StaticPlaneConstant);
            collisionShapePtr = staticPlaneShape.GetSwigPtr();
        }

        return true;
    }
예제 #5
0
    //it is very slow, so don't use it if you don't need it indeed..
    public static void DebugDrawPolyhedron(Vector3 position,Quaternion rotation,Vector3 scale,btPolyhedralConvexShape shape,Color color)
    {
        if( shape == null )
            return;

        Matrix4x4 matrix = Matrix4x4.TRS(position,rotation,scale);
        Gizmos.color = color;
        btConvexPolyhedron poly = shape.getConvexPolyhedron();
        if( poly == null )
            return;

        int faceSize = poly.m_faces.size();
        for (int i=0;i < faceSize;i++)
        {
            Vector3 centroid = new Vector3(0,0,0);
            btFace face = poly.m_faces.at(i);
            int numVerts = face.m_indices.size();
            if (numVerts > 0)
            {
                int lastV = face.m_indices.at(numVerts-1);
                for (int v=0;v < numVerts;v++)
                {
                    int curVert = face.m_indices.at(v);
                    btVector3 curVertObject = btVector3.GetObjectFromSwigPtr(poly.m_vertices.at(curVert));
                    centroid.x += curVertObject.x();
                    centroid.y += curVertObject.y();
                    centroid.z += curVertObject.z();
                    btVector3 btv1 = btVector3.GetObjectFromSwigPtr(poly.m_vertices.at(lastV));
                    btVector3 btv2 = btVector3.GetObjectFromSwigPtr(poly.m_vertices.at(curVert));
                    Vector3 v1 = new Vector3(btv1.x(),btv1.y(),btv1.z());
                    Vector3 v2 = new Vector3(btv2.x(),btv2.y(),btv2.z());
                    v1 = matrix.MultiplyPoint(v1);
                    v2 = matrix.MultiplyPoint(v2);
                    Gizmos.DrawLine(v1,v2);
                    lastV = curVert;
                }
            }
            float s = 1.0f/numVerts;
            centroid.x *= s;
            centroid.y *= s;
            centroid.z *= s;

            //normal draw
        //            {
        //                Vector3 normalColor = new Vector3(1,1,0);
        //
        //                btVector3 faceNormal(face.m_plane[0],poly->m_faces[i].m_plane[1],poly->m_faces[i].m_plane[2]);
        //                getDebugDrawer()->drawLine(worldTransform*centroid,worldTransform*(centroid+faceNormal),normalColor);
        //            }

        }
    }