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; }