public bool OnBulletCreate() { if( ConstraintType == ConstraintTypes.Point2Point ) { if( RigidBodyA != null && RigidBodyB == null ) { btVector3 vecA = new btVector3(PivotInA.x,PivotInA.y,PivotInA.z); btRigidBody rA = RigidBodyA.GetRigidBody(); if( rA == null ) { Debug.LogError("Can't Create Constraint for null RigidBody!"); return false; } point2pointConstraint = new btPoint2PointConstraint(rA,vecA.GetSwigPtr()); constraintPtr = point2pointConstraint.GetSwigPtr(); return true; } else if( RigidBodyA != null && RigidBodyB != null ) { btVector3 vecA = new btVector3(PivotInA.x,PivotInA.y,PivotInA.z); btRigidBody rA = RigidBodyA.GetRigidBody(); if( rA == null ) { Debug.LogError("Can't Create Constraint for null RigidBody!"); return false; } btVector3 vecB = new btVector3(PivotInB.x,PivotInB.y,PivotInB.z); btRigidBody rB = RigidBodyB.GetRigidBody(); if( rB == null ) { Debug.LogError("Can't Create Constraint for null RigidBody!"); return false; } point2pointConstraint = new btPoint2PointConstraint(rA,rB,vecA.GetSwigPtr(),vecB.GetSwigPtr()); constraintPtr = point2pointConstraint.GetSwigPtr(); return true; } return false; } else if( ConstraintType == ConstraintTypes.Hinge ) { if( RigidBodyA != null && RigidBodyB == null ) { btVector3 vecA = new btVector3(PivotInA.x,PivotInA.y,PivotInA.z); btVector3 axisA = new btVector3(AxisInA.x,AxisInA.y,AxisInA.z); btRigidBody rA = RigidBodyA.GetRigidBody(); if( rA == null ) { Debug.LogError("Can't Create Constraint for null RigidBody!"); return false; } hingeConstraint = new btHingeConstraint(rA,vecA.GetSwigPtr(),axisA.GetSwigPtr(),useReferenceFrameAHinge); constraintPtr = hingeConstraint.GetSwigPtr(); return true; } else if( RigidBodyA != null && RigidBodyB != null ) { btVector3 vecA = new btVector3(PivotInA.x,PivotInA.y,PivotInA.z); btVector3 axisA = new btVector3(AxisInA.x,AxisInA.y,AxisInA.z); btRigidBody rA = RigidBodyA.GetRigidBody(); if( rA == null ) { Debug.LogError("Can't Create Constraint for null RigidBody!"); return false; } btVector3 vecB = new btVector3(PivotInB.x,PivotInB.y,PivotInB.z); btVector3 axisB = new btVector3(AxisInB.x,AxisInB.y,AxisInB.z); btRigidBody rB = RigidBodyB.GetRigidBody(); if( rB == null ) { Debug.LogError("Can't Create Constraint for null RigidBody!"); return false; } hingeConstraint = new btHingeConstraint(rA,rB,vecA.GetSwigPtr(),vecB.GetSwigPtr(),axisA.GetSwigPtr(),axisB.GetSwigPtr(),useReferenceFrameAHinge); constraintPtr = hingeConstraint.GetSwigPtr(); return true; } return false; } else if( ConstraintType == ConstraintTypes.Slider ) { if( RigidBodyA != null && RigidBodyB == null ) { btVector3 vecA = new btVector3(PivotInA.x,PivotInA.y,PivotInA.z); btQuaternion rot = new btQuaternion(RotationA.x,RotationA.y,RotationA.z); btTransform transA = new btTransform(); transA.setIdentity(); transA.setOrigin(vecA); transA.setRotation(rot); btRigidBody rA = RigidBodyA.GetRigidBody(); if( rA == null ) { Debug.LogError("Can't Create Constraint for null RigidBody!"); return false; } sliderConstraint = new btSliderConstraint(rA,transA.GetSwigPtr(),useLinearReferenceFrameASlider); constraintPtr = sliderConstraint.GetSwigPtr(); return true; } else if( RigidBodyA != null && RigidBodyB != null ) { btVector3 vecA = new btVector3(PivotInA.x,PivotInA.y,PivotInA.z); btQuaternion rotA = new btQuaternion(RotationA.x,RotationA.y,RotationA.z); btTransform transA = new btTransform(); transA.setIdentity(); transA.setOrigin(vecA); transA.setRotation(rotA); btRigidBody rA = RigidBodyA.GetRigidBody(); if( rA == null ) { Debug.LogError("Can't Create Constraint for null RigidBody!"); return false; } btVector3 vecB = new btVector3(PivotInB.x,PivotInB.y,PivotInB.z); btQuaternion rotB = new btQuaternion(RotationB.x,RotationB.y,RotationB.z); btTransform transB = new btTransform(); transB.setIdentity(); transB.setOrigin(vecB); transB.setRotation(rotB); btRigidBody rB = RigidBodyB.GetRigidBody(); if( rB == null ) { Debug.LogError("Can't Create Constraint for null RigidBody!"); return false; } sliderConstraint = new btSliderConstraint(rA,rB,transA.GetSwigPtr(),transB.GetSwigPtr(), useLinearReferenceFrameASlider); constraintPtr = sliderConstraint.GetSwigPtr(); return true; } return false; } else if( ConstraintType == ConstraintTypes.ConeTwist ) { if( RigidBodyA != null && RigidBodyB == null ) { btVector3 vecA = new btVector3(PivotInA.x,PivotInA.y,PivotInA.z); btQuaternion rot = new btQuaternion(RotationA.x,RotationA.y,RotationA.z); btTransform transA = new btTransform(); transA.setIdentity(); transA.setOrigin(vecA); transA.setRotation(rot); btRigidBody rA = RigidBodyA.GetRigidBody(); if( rA == null ) { Debug.LogError("Can't Create Constraint for null RigidBody!"); return false; } coneTwistConstraint = new btConeTwistConstraint(rA,transA.GetSwigPtr()); constraintPtr = coneTwistConstraint.GetSwigPtr(); return true; } else if( RigidBodyA != null && RigidBodyB != null ) { btVector3 vecA = new btVector3(PivotInA.x,PivotInA.y,PivotInA.z); btQuaternion rotA = new btQuaternion(RotationA.x,RotationA.y,RotationA.z); btTransform transA = new btTransform(); transA.setIdentity(); transA.setOrigin(vecA); transA.setRotation(rotA); btRigidBody rA = RigidBodyA.GetRigidBody(); if( rA == null ) { Debug.LogError("Can't Create Constraint for null RigidBody!"); return false; } btVector3 vecB = new btVector3(PivotInB.x,PivotInB.y,PivotInB.z); btQuaternion rotB = new btQuaternion(RotationB.x,RotationB.y,RotationB.z); btTransform transB = new btTransform(); transB.setIdentity(); transB.setOrigin(vecB); transB.setRotation(rotB); btRigidBody rB = RigidBodyB.GetRigidBody(); if( rB == null ) { Debug.LogError("Can't Create Constraint for null RigidBody!"); return false; } coneTwistConstraint = new btConeTwistConstraint(rA,rB,transA.GetSwigPtr(),transB.GetSwigPtr()); constraintPtr = coneTwistConstraint.GetSwigPtr(); return true; } return false; } else if( ConstraintType == ConstraintTypes.Gear ) { if( RigidBodyA != null && RigidBodyB != null ) { btVector3 axisA = new btVector3(AxisInA.x,AxisInA.y,AxisInA.z); btVector3 axisB = new btVector3(AxisInB.x,AxisInB.y,AxisInB.z); btRigidBody rA = RigidBodyA.GetRigidBody(); if( rA == null ) { Debug.LogError("Can't Create Constraint for null RigidBody!"); return false; } btRigidBody rB = RigidBodyB.GetRigidBody(); if( rB == null ) { Debug.LogError("Can't Create Constraint for null RigidBody!"); return false; } gearConstraint = new btGearConstraint(rA,rB,axisA.GetSwigPtr(),axisB.GetSwigPtr(),GearConstraintRatio); constraintPtr = gearConstraint.GetSwigPtr(); return true; } return false; } else if( ConstraintType == ConstraintTypes.Generic6Dof ) { if( RigidBodyA != null && RigidBodyB != null ) { btVector3 vecA = new btVector3(PivotInA.x,PivotInA.y,PivotInA.z); btQuaternion rotA = new btQuaternion(RotationA.x,RotationA.y,RotationA.z); btTransform transA = new btTransform(); transA.setIdentity(); transA.setOrigin(vecA); transA.setRotation(rotA); btRigidBody rA = RigidBodyA.GetRigidBody(); if( rA == null ) { Debug.LogError("Can't Create Constraint for null RigidBody!"); return false; } btVector3 vecB = new btVector3(PivotInB.x,PivotInB.y,PivotInB.z); btQuaternion rotB = new btQuaternion(RotationB.x,RotationB.y,RotationB.z); btTransform transB = new btTransform(); transB.setIdentity(); transB.setOrigin(vecB); transB.setRotation(rotB); btRigidBody rB = RigidBodyB.GetRigidBody(); if( rB == null ) { Debug.LogError("Can't Create Constraint for null RigidBody!"); return false; } generic6Dof = new btGeneric6DofConstraint(rA,rB,transA.GetSwigPtr(),transB.GetSwigPtr(),useLinearReferenceFrameASlider); if( UseLinearLimit ) { btVector3 lowerVec = new btVector3(LinearLowerLimit.x,LinearLowerLimit.y,LinearLowerLimit.z); btVector3 uppderVec = new btVector3(LinearUpperLimit.x,LinearUpperLimit.y,LinearUpperLimit.z); generic6Dof.setLinearLowerLimit(lowerVec.GetSwigPtr()); generic6Dof.setLinearUpperLimit(uppderVec.GetSwigPtr()); } if( UseAngularLimit ) { btVector3 lowerVec = new btVector3(AngularLowerLimit.x,AngularLowerLimit.y,AngularLowerLimit.z); btVector3 uppderVec = new btVector3(AngularUpperLimit.x,AngularUpperLimit.y,AngularUpperLimit.z); generic6Dof.setAngularLowerLimit(lowerVec.GetSwigPtr()); generic6Dof.setAngularUpperLimit(uppderVec.GetSwigPtr()); } constraintPtr = generic6Dof.GetSwigPtr(); return true; } return false; } return false; }
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; }