1
0

btManifoldResult.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. Bullet Continuous Collision Detection and Physics Library
  3. Copyright (c) 2003-2006 Erwin Coumans http://continuousphysics.com/Bullet/
  4. This software is provided 'as-is', without any express or implied warranty.
  5. In no event will the authors be held liable for any damages arising from the use of this software.
  6. Permission is granted to anyone to use this software for any purpose,
  7. including commercial applications, and to alter it and redistribute it freely,
  8. subject to the following restrictions:
  9. 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
  10. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  11. 3. This notice may not be removed or altered from any source distribution.
  12. */
  13. #include "btManifoldResult.h"
  14. #include "bullet/BulletCollision//NarrowPhaseCollision/btPersistentManifold.h"
  15. #include "bullet/BulletCollision//CollisionDispatch/btCollisionObject.h"
  16. #include "bullet/BulletCollision//CollisionDispatch/btCollisionObjectWrapper.h"
  17. ///This is to allow MaterialCombiner/Custom Friction/Restitution values
  18. ContactAddedCallback gContactAddedCallback=0;
  19. ///User can override this material combiner by implementing gContactAddedCallback and setting body0->m_collisionFlags |= btCollisionObject::customMaterialCallback;
  20. inline btScalar calculateCombinedRollingFriction(const btCollisionObject* body0,const btCollisionObject* body1)
  21. {
  22. btScalar friction = body0->getRollingFriction() * body1->getRollingFriction();
  23. const btScalar MAX_FRICTION = btScalar(10.);
  24. if (friction < -MAX_FRICTION)
  25. friction = -MAX_FRICTION;
  26. if (friction > MAX_FRICTION)
  27. friction = MAX_FRICTION;
  28. return friction;
  29. }
  30. ///User can override this material combiner by implementing gContactAddedCallback and setting body0->m_collisionFlags |= btCollisionObject::customMaterialCallback;
  31. btScalar btManifoldResult::calculateCombinedFriction(const btCollisionObject* body0,const btCollisionObject* body1)
  32. {
  33. btScalar friction = body0->getFriction() * body1->getFriction();
  34. const btScalar MAX_FRICTION = btScalar(10.);
  35. if (friction < -MAX_FRICTION)
  36. friction = -MAX_FRICTION;
  37. if (friction > MAX_FRICTION)
  38. friction = MAX_FRICTION;
  39. return friction;
  40. }
  41. btScalar btManifoldResult::calculateCombinedRestitution(const btCollisionObject* body0,const btCollisionObject* body1)
  42. {
  43. return body0->getRestitution() * body1->getRestitution();
  44. }
  45. btManifoldResult::btManifoldResult(const btCollisionObjectWrapper* body0Wrap,const btCollisionObjectWrapper* body1Wrap)
  46. :m_manifoldPtr(0),
  47. m_body0Wrap(body0Wrap),
  48. m_body1Wrap(body1Wrap)
  49. #ifdef DEBUG_PART_INDEX
  50. ,m_partId0(-1),
  51. m_partId1(-1),
  52. m_index0(-1),
  53. m_index1(-1)
  54. #endif //DEBUG_PART_INDEX
  55. {
  56. }
  57. void btManifoldResult::addContactPoint(const btVector3& normalOnBInWorld,const btVector3& pointInWorld,btScalar depth)
  58. {
  59. btAssert(m_manifoldPtr);
  60. //order in manifold needs to match
  61. if (depth > m_manifoldPtr->getContactBreakingThreshold())
  62. // if (depth > m_manifoldPtr->getContactProcessingThreshold())
  63. return;
  64. bool isSwapped = m_manifoldPtr->getBody0() != m_body0Wrap->getCollisionObject();
  65. btVector3 pointA = pointInWorld + normalOnBInWorld * depth;
  66. btVector3 localA;
  67. btVector3 localB;
  68. if (isSwapped)
  69. {
  70. localA = m_body1Wrap->getCollisionObject()->getWorldTransform().invXform(pointA );
  71. localB = m_body0Wrap->getCollisionObject()->getWorldTransform().invXform(pointInWorld);
  72. } else
  73. {
  74. localA = m_body0Wrap->getCollisionObject()->getWorldTransform().invXform(pointA );
  75. localB = m_body1Wrap->getCollisionObject()->getWorldTransform().invXform(pointInWorld);
  76. }
  77. btManifoldPoint newPt(localA,localB,normalOnBInWorld,depth);
  78. newPt.m_positionWorldOnA = pointA;
  79. newPt.m_positionWorldOnB = pointInWorld;
  80. int insertIndex = m_manifoldPtr->getCacheEntry(newPt);
  81. newPt.m_combinedFriction = calculateCombinedFriction(m_body0Wrap->getCollisionObject(),m_body1Wrap->getCollisionObject());
  82. newPt.m_combinedRestitution = calculateCombinedRestitution(m_body0Wrap->getCollisionObject(),m_body1Wrap->getCollisionObject());
  83. newPt.m_combinedRollingFriction = calculateCombinedRollingFriction(m_body0Wrap->getCollisionObject(),m_body1Wrap->getCollisionObject());
  84. btPlaneSpace1(newPt.m_normalWorldOnB,newPt.m_lateralFrictionDir1,newPt.m_lateralFrictionDir2);
  85. //BP mod, store contact triangles.
  86. if (isSwapped)
  87. {
  88. newPt.m_partId0 = m_partId1;
  89. newPt.m_partId1 = m_partId0;
  90. newPt.m_index0 = m_index1;
  91. newPt.m_index1 = m_index0;
  92. } else
  93. {
  94. newPt.m_partId0 = m_partId0;
  95. newPt.m_partId1 = m_partId1;
  96. newPt.m_index0 = m_index0;
  97. newPt.m_index1 = m_index1;
  98. }
  99. //printf("depth=%f\n",depth);
  100. ///@todo, check this for any side effects
  101. if (insertIndex >= 0)
  102. {
  103. //const btManifoldPoint& oldPoint = m_manifoldPtr->getContactPoint(insertIndex);
  104. m_manifoldPtr->replaceContactPoint(newPt,insertIndex);
  105. } else
  106. {
  107. insertIndex = m_manifoldPtr->addManifoldPoint(newPt);
  108. }
  109. //User can override friction and/or restitution
  110. if (gContactAddedCallback &&
  111. //and if either of the two bodies requires custom material
  112. ((m_body0Wrap->getCollisionObject()->getCollisionFlags() & btCollisionObject::CF_CUSTOM_MATERIAL_CALLBACK) ||
  113. (m_body1Wrap->getCollisionObject()->getCollisionFlags() & btCollisionObject::CF_CUSTOM_MATERIAL_CALLBACK)))
  114. {
  115. //experimental feature info, for per-triangle material etc.
  116. const btCollisionObjectWrapper* obj0Wrap = isSwapped? m_body1Wrap : m_body0Wrap;
  117. const btCollisionObjectWrapper* obj1Wrap = isSwapped? m_body0Wrap : m_body1Wrap;
  118. (*gContactAddedCallback)(m_manifoldPtr->getContactPoint(insertIndex),obj0Wrap,newPt.m_partId0,newPt.m_index0,obj1Wrap,newPt.m_partId1,newPt.m_index1);
  119. }
  120. }