SpuContactResult.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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 "SpuContactResult.h"
  14. //#define DEBUG_SPU_COLLISION_DETECTION 1
  15. #ifdef DEBUG_SPU_COLLISION_DETECTION
  16. #ifndef __SPU__
  17. #include <stdio.h>
  18. #define spu_printf printf
  19. #endif
  20. #endif //DEBUG_SPU_COLLISION_DETECTION
  21. SpuContactResult::SpuContactResult()
  22. {
  23. m_manifoldAddress = 0;
  24. m_spuManifold = NULL;
  25. m_RequiresWriteBack = false;
  26. }
  27. SpuContactResult::~SpuContactResult()
  28. {
  29. g_manifoldDmaExport.swapBuffers();
  30. }
  31. ///User can override this material combiner by implementing gContactAddedCallback and setting body0->m_collisionFlags |= btCollisionObject::customMaterialCallback;
  32. inline btScalar calculateCombinedFriction(btScalar friction0,btScalar friction1)
  33. {
  34. btScalar friction = friction0*friction1;
  35. const btScalar MAX_FRICTION = btScalar(10.);
  36. if (friction < -MAX_FRICTION)
  37. friction = -MAX_FRICTION;
  38. if (friction > MAX_FRICTION)
  39. friction = MAX_FRICTION;
  40. return friction;
  41. }
  42. inline btScalar calculateCombinedRestitution(btScalar restitution0,btScalar restitution1)
  43. {
  44. return restitution0*restitution1;
  45. }
  46. void SpuContactResult::setContactInfo(btPersistentManifold* spuManifold, ppu_address_t manifoldAddress,const btTransform& worldTrans0,const btTransform& worldTrans1, btScalar restitution0,btScalar restitution1, btScalar friction0,btScalar friction1, bool isSwapped)
  47. {
  48. //spu_printf("SpuContactResult::setContactInfo ManifoldAddress: %lu\n", manifoldAddress);
  49. m_rootWorldTransform0 = worldTrans0;
  50. m_rootWorldTransform1 = worldTrans1;
  51. m_manifoldAddress = manifoldAddress;
  52. m_spuManifold = spuManifold;
  53. m_combinedFriction = calculateCombinedFriction(friction0,friction1);
  54. m_combinedRestitution = calculateCombinedRestitution(restitution0,restitution1);
  55. m_isSwapped = isSwapped;
  56. }
  57. void SpuContactResult::setShapeIdentifiersA(int partId0,int index0)
  58. {
  59. }
  60. void SpuContactResult::setShapeIdentifiersB(int partId1,int index1)
  61. {
  62. }
  63. ///return true if it requires a dma transfer back
  64. bool ManifoldResultAddContactPoint(const btVector3& normalOnBInWorld,
  65. const btVector3& pointInWorld,
  66. float depth,
  67. btPersistentManifold* manifoldPtr,
  68. btTransform& transA,
  69. btTransform& transB,
  70. btScalar combinedFriction,
  71. btScalar combinedRestitution,
  72. bool isSwapped)
  73. {
  74. // float contactTreshold = manifoldPtr->getContactBreakingThreshold();
  75. //spu_printf("SPU: add contactpoint, depth:%f, contactTreshold %f, manifoldPtr %llx\n",depth,contactTreshold,manifoldPtr);
  76. #ifdef DEBUG_SPU_COLLISION_DETECTION
  77. spu_printf("SPU: contactTreshold %f\n",contactTreshold);
  78. #endif //DEBUG_SPU_COLLISION_DETECTION
  79. if (depth > manifoldPtr->getContactBreakingThreshold())
  80. return false;
  81. //if (depth > manifoldPtr->getContactProcessingThreshold())
  82. // return false;
  83. btVector3 pointA;
  84. btVector3 localA;
  85. btVector3 localB;
  86. btVector3 normal;
  87. if (isSwapped)
  88. {
  89. normal = normalOnBInWorld * -1;
  90. pointA = pointInWorld + normal * depth;
  91. localA = transA.invXform(pointA );
  92. localB = transB.invXform(pointInWorld);
  93. }
  94. else
  95. {
  96. normal = normalOnBInWorld;
  97. pointA = pointInWorld + normal * depth;
  98. localA = transA.invXform(pointA );
  99. localB = transB.invXform(pointInWorld);
  100. }
  101. btManifoldPoint newPt(localA,localB,normal,depth);
  102. newPt.m_positionWorldOnA = pointA;
  103. newPt.m_positionWorldOnB = pointInWorld;
  104. newPt.m_combinedFriction = combinedFriction;
  105. newPt.m_combinedRestitution = combinedRestitution;
  106. int insertIndex = manifoldPtr->getCacheEntry(newPt);
  107. if (insertIndex >= 0)
  108. {
  109. // we need to replace the current contact point, otherwise small errors will accumulate (spheres start rolling etc)
  110. manifoldPtr->replaceContactPoint(newPt,insertIndex);
  111. return true;
  112. } else
  113. {
  114. /*
  115. ///@todo: SPU callbacks, either immediate (local on the SPU), or deferred
  116. //User can override friction and/or restitution
  117. if (gContactAddedCallback &&
  118. //and if either of the two bodies requires custom material
  119. ((m_body0->m_collisionFlags & btCollisionObject::customMaterialCallback) ||
  120. (m_body1->m_collisionFlags & btCollisionObject::customMaterialCallback)))
  121. {
  122. //experimental feature info, for per-triangle material etc.
  123. (*gContactAddedCallback)(newPt,m_body0,m_partId0,m_index0,m_body1,m_partId1,m_index1);
  124. }
  125. */
  126. manifoldPtr->addManifoldPoint(newPt);
  127. return true;
  128. }
  129. return false;
  130. }
  131. void SpuContactResult::writeDoubleBufferedManifold(btPersistentManifold* lsManifold, btPersistentManifold* mmManifold)
  132. {
  133. ///only write back the contact information on SPU. Other platforms avoid copying, and use the data in-place
  134. ///see SpuFakeDma.cpp 'cellDmaLargeGetReadOnly'
  135. #if defined (__SPU__) || defined (USE_LIBSPE2)
  136. memcpy(g_manifoldDmaExport.getFront(),lsManifold,sizeof(btPersistentManifold));
  137. g_manifoldDmaExport.swapBuffers();
  138. ppu_address_t mmAddr = (ppu_address_t)mmManifold;
  139. g_manifoldDmaExport.backBufferDmaPut(mmAddr, sizeof(btPersistentManifold), DMA_TAG(9));
  140. // Should there be any kind of wait here? What if somebody tries to use this tag again? What if we call this function again really soon?
  141. //no, the swapBuffers does the wait
  142. #endif
  143. }
  144. void SpuContactResult::addContactPoint(const btVector3& normalOnBInWorld,const btVector3& pointInWorld,btScalar depth)
  145. {
  146. #ifdef DEBUG_SPU_COLLISION_DETECTION
  147. spu_printf("*** SpuContactResult::addContactPoint: depth = %f\n",depth);
  148. spu_printf("*** normal = %f,%f,%f\n",normalOnBInWorld.getX(),normalOnBInWorld.getY(),normalOnBInWorld.getZ());
  149. spu_printf("*** position = %f,%f,%f\n",pointInWorld.getX(),pointInWorld.getY(),pointInWorld.getZ());
  150. #endif //DEBUG_SPU_COLLISION_DETECTION
  151. #ifdef DEBUG_SPU_COLLISION_DETECTION
  152. // int sman = sizeof(rage::phManifold);
  153. // spu_printf("sizeof_manifold = %i\n",sman);
  154. #endif //DEBUG_SPU_COLLISION_DETECTION
  155. btPersistentManifold* localManifold = m_spuManifold;
  156. btVector3 normalB(normalOnBInWorld.getX(),normalOnBInWorld.getY(),normalOnBInWorld.getZ());
  157. btVector3 pointWrld(pointInWorld.getX(),pointInWorld.getY(),pointInWorld.getZ());
  158. //process the contact point
  159. const bool retVal = ManifoldResultAddContactPoint(normalB,
  160. pointWrld,
  161. depth,
  162. localManifold,
  163. m_rootWorldTransform0,
  164. m_rootWorldTransform1,
  165. m_combinedFriction,
  166. m_combinedRestitution,
  167. m_isSwapped);
  168. m_RequiresWriteBack = m_RequiresWriteBack || retVal;
  169. }
  170. void SpuContactResult::flush()
  171. {
  172. if (m_spuManifold && m_spuManifold->getNumContacts())
  173. {
  174. m_spuManifold->refreshContactPoints(m_rootWorldTransform0,m_rootWorldTransform1);
  175. m_RequiresWriteBack = true;
  176. }
  177. if (m_RequiresWriteBack)
  178. {
  179. #ifdef DEBUG_SPU_COLLISION_DETECTION
  180. spu_printf("SPU: Start SpuContactResult::flush (Put) DMA\n");
  181. spu_printf("Num contacts:%d\n", m_spuManifold->getNumContacts());
  182. spu_printf("Manifold address: %llu\n", m_manifoldAddress);
  183. #endif //DEBUG_SPU_COLLISION_DETECTION
  184. // spu_printf("writeDoubleBufferedManifold\n");
  185. writeDoubleBufferedManifold(m_spuManifold, (btPersistentManifold*)m_manifoldAddress);
  186. #ifdef DEBUG_SPU_COLLISION_DETECTION
  187. spu_printf("SPU: Finished (Put) DMA\n");
  188. #endif //DEBUG_SPU_COLLISION_DETECTION
  189. }
  190. m_spuManifold = NULL;
  191. m_RequiresWriteBack = false;
  192. }