btCompoundFromGimpact.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #ifndef BT_COMPOUND_FROM_GIMPACT
  2. #define BT_COMPOUND_FROM_GIMPACT
  3. #include "BulletCollision/CollisionShapes/btCompoundShape.h"
  4. #include "btGImpactShape.h"
  5. #include "BulletCollision/NarrowPhaseCollision/btRaycastCallback.h"
  6. ATTRIBUTE_ALIGNED16(class) btCompoundFromGimpactShape : public btCompoundShape
  7. {
  8. public:
  9. BT_DECLARE_ALIGNED_ALLOCATOR();
  10. virtual ~btCompoundFromGimpactShape()
  11. {
  12. /*delete all the btBU_Simplex1to4 ChildShapes*/
  13. for (int i = 0; i < m_children.size(); i++)
  14. {
  15. delete m_children[i].m_childShape;
  16. }
  17. }
  18. };
  19. struct MyCallback : public btTriangleRaycastCallback
  20. {
  21. int m_ignorePart;
  22. int m_ignoreTriangleIndex;
  23. MyCallback(const btVector3& from, const btVector3& to, int ignorePart, int ignoreTriangleIndex)
  24. :btTriangleRaycastCallback(from,to),
  25. m_ignorePart(ignorePart),
  26. m_ignoreTriangleIndex(ignoreTriangleIndex)
  27. {
  28. }
  29. virtual btScalar reportHit(const btVector3& hitNormalLocal, btScalar hitFraction, int partId, int triangleIndex)
  30. {
  31. if (partId!=m_ignorePart || triangleIndex!=m_ignoreTriangleIndex)
  32. {
  33. if (hitFraction < m_hitFraction)
  34. return hitFraction;
  35. }
  36. return m_hitFraction;
  37. }
  38. };
  39. struct MyInternalTriangleIndexCallback :public btInternalTriangleIndexCallback
  40. {
  41. const btGImpactMeshShape* m_gimpactShape;
  42. btCompoundShape* m_colShape;
  43. btScalar m_depth;
  44. MyInternalTriangleIndexCallback (btCompoundShape* colShape, const btGImpactMeshShape* meshShape, btScalar depth)
  45. :m_colShape(colShape),
  46. m_gimpactShape(meshShape),
  47. m_depth(depth)
  48. {
  49. }
  50. virtual void internalProcessTriangleIndex(btVector3* triangle,int partId,int triangleIndex)
  51. {
  52. btVector3 scale = m_gimpactShape->getLocalScaling();
  53. btVector3 v0=triangle[0]*scale;
  54. btVector3 v1=triangle[1]*scale;
  55. btVector3 v2=triangle[2]*scale;
  56. btVector3 centroid = (v0+v1+v2)/3;
  57. btVector3 normal = (v1-v0).cross(v2-v0);
  58. normal.normalize();
  59. btVector3 rayFrom = centroid;
  60. btVector3 rayTo = centroid-normal*m_depth;
  61. MyCallback cb(rayFrom,rayTo,partId,triangleIndex);
  62. m_gimpactShape->processAllTrianglesRay(&cb,rayFrom, rayTo);
  63. if (cb.m_hitFraction<1)
  64. {
  65. rayTo.setInterpolate3(cb.m_from,cb.m_to,cb.m_hitFraction);
  66. //rayTo = cb.m_from;
  67. //rayTo = rayTo.lerp(cb.m_to,cb.m_hitFraction);
  68. //gDebugDraw.drawLine(tr(centroid),tr(centroid+normal),btVector3(1,0,0));
  69. }
  70. btBU_Simplex1to4* tet = new btBU_Simplex1to4(v0,v1,v2,rayTo);
  71. btTransform ident;
  72. ident.setIdentity();
  73. m_colShape->addChildShape(ident,tet);
  74. }
  75. };
  76. btCompoundShape* btCreateCompoundFromGimpactShape(const btGImpactMeshShape* gimpactMesh, btScalar depth)
  77. {
  78. btCompoundShape* colShape = new btCompoundFromGimpactShape();
  79. btTransform tr;
  80. tr.setIdentity();
  81. MyInternalTriangleIndexCallback cb(colShape,gimpactMesh, depth);
  82. btVector3 aabbMin,aabbMax;
  83. gimpactMesh->getAabb(tr,aabbMin,aabbMax);
  84. gimpactMesh->getMeshInterface()->InternalProcessAllTriangles(&cb,aabbMin,aabbMax);
  85. return colShape;
  86. }
  87. #endif //BT_COMPOUND_FROM_GIMPACT