btGhostObject.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. Bullet Continuous Collision Detection and Physics Library
  3. Copyright (c) 2003-2008 Erwin Coumans http://bulletphysics.com
  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 "btGhostObject.h"
  14. #include "btCollisionWorld.h"
  15. #include "BulletCollision/CollisionShapes/btConvexShape.h"
  16. #include "LinearMath/btAabbUtil2.h"
  17. btGhostObject::btGhostObject()
  18. {
  19. m_internalType = CO_GHOST_OBJECT;
  20. }
  21. btGhostObject::~btGhostObject()
  22. {
  23. ///btGhostObject should have been removed from the world, so no overlapping objects
  24. btAssert(!m_overlappingObjects.size());
  25. }
  26. void btGhostObject::addOverlappingObjectInternal(btBroadphaseProxy* otherProxy, btBroadphaseProxy* thisProxy)
  27. {
  28. btCollisionObject* otherObject = (btCollisionObject*)otherProxy->m_clientObject;
  29. btAssert(otherObject);
  30. ///if this linearSearch becomes too slow (too many overlapping objects) we should add a more appropriate data structure
  31. int index = m_overlappingObjects.findLinearSearch(otherObject);
  32. if (index == m_overlappingObjects.size())
  33. {
  34. //not found
  35. m_overlappingObjects.push_back(otherObject);
  36. }
  37. }
  38. void btGhostObject::removeOverlappingObjectInternal(btBroadphaseProxy* otherProxy, btDispatcher* dispatcher, btBroadphaseProxy* thisProxy)
  39. {
  40. btCollisionObject* otherObject = (btCollisionObject*)otherProxy->m_clientObject;
  41. btAssert(otherObject);
  42. int index = m_overlappingObjects.findLinearSearch(otherObject);
  43. if (index < m_overlappingObjects.size())
  44. {
  45. m_overlappingObjects[index] = m_overlappingObjects[m_overlappingObjects.size() - 1];
  46. m_overlappingObjects.pop_back();
  47. }
  48. }
  49. btPairCachingGhostObject::btPairCachingGhostObject()
  50. {
  51. m_hashPairCache = new (btAlignedAlloc(sizeof(btHashedOverlappingPairCache), 16)) btHashedOverlappingPairCache();
  52. }
  53. btPairCachingGhostObject::~btPairCachingGhostObject()
  54. {
  55. m_hashPairCache->~btHashedOverlappingPairCache();
  56. btAlignedFree(m_hashPairCache);
  57. }
  58. void btPairCachingGhostObject::addOverlappingObjectInternal(btBroadphaseProxy* otherProxy, btBroadphaseProxy* thisProxy)
  59. {
  60. btBroadphaseProxy* actualThisProxy = thisProxy ? thisProxy : getBroadphaseHandle();
  61. btAssert(actualThisProxy);
  62. btCollisionObject* otherObject = (btCollisionObject*)otherProxy->m_clientObject;
  63. btAssert(otherObject);
  64. int index = m_overlappingObjects.findLinearSearch(otherObject);
  65. if (index == m_overlappingObjects.size())
  66. {
  67. m_overlappingObjects.push_back(otherObject);
  68. m_hashPairCache->addOverlappingPair(actualThisProxy, otherProxy);
  69. }
  70. }
  71. void btPairCachingGhostObject::removeOverlappingObjectInternal(btBroadphaseProxy* otherProxy, btDispatcher* dispatcher, btBroadphaseProxy* thisProxy1)
  72. {
  73. btCollisionObject* otherObject = (btCollisionObject*)otherProxy->m_clientObject;
  74. btBroadphaseProxy* actualThisProxy = thisProxy1 ? thisProxy1 : getBroadphaseHandle();
  75. btAssert(actualThisProxy);
  76. btAssert(otherObject);
  77. int index = m_overlappingObjects.findLinearSearch(otherObject);
  78. if (index < m_overlappingObjects.size())
  79. {
  80. m_overlappingObjects[index] = m_overlappingObjects[m_overlappingObjects.size() - 1];
  81. m_overlappingObjects.pop_back();
  82. m_hashPairCache->removeOverlappingPair(actualThisProxy, otherProxy, dispatcher);
  83. }
  84. }
  85. void btGhostObject::convexSweepTest(const btConvexShape* castShape, const btTransform& convexFromWorld, const btTransform& convexToWorld, btCollisionWorld::ConvexResultCallback& resultCallback, btScalar allowedCcdPenetration) const
  86. {
  87. btTransform convexFromTrans, convexToTrans;
  88. convexFromTrans = convexFromWorld;
  89. convexToTrans = convexToWorld;
  90. btVector3 castShapeAabbMin, castShapeAabbMax;
  91. /* Compute AABB that encompasses angular movement */
  92. {
  93. btVector3 linVel, angVel;
  94. btTransformUtil::calculateVelocity(convexFromTrans, convexToTrans, 1.0, linVel, angVel);
  95. btTransform R;
  96. R.setIdentity();
  97. R.setRotation(convexFromTrans.getRotation());
  98. castShape->calculateTemporalAabb(R, linVel, angVel, 1.0, castShapeAabbMin, castShapeAabbMax);
  99. }
  100. /// go over all objects, and if the ray intersects their aabb + cast shape aabb,
  101. // do a ray-shape query using convexCaster (CCD)
  102. int i;
  103. for (i = 0; i < m_overlappingObjects.size(); i++)
  104. {
  105. btCollisionObject* collisionObject = m_overlappingObjects[i];
  106. //only perform raycast if filterMask matches
  107. if (resultCallback.needsCollision(collisionObject->getBroadphaseHandle()))
  108. {
  109. //RigidcollisionObject* collisionObject = ctrl->GetRigidcollisionObject();
  110. btVector3 collisionObjectAabbMin, collisionObjectAabbMax;
  111. collisionObject->getCollisionShape()->getAabb(collisionObject->getWorldTransform(), collisionObjectAabbMin, collisionObjectAabbMax);
  112. AabbExpand(collisionObjectAabbMin, collisionObjectAabbMax, castShapeAabbMin, castShapeAabbMax);
  113. btScalar hitLambda = btScalar(1.); //could use resultCallback.m_closestHitFraction, but needs testing
  114. btVector3 hitNormal;
  115. if (btRayAabb(convexFromWorld.getOrigin(), convexToWorld.getOrigin(), collisionObjectAabbMin, collisionObjectAabbMax, hitLambda, hitNormal))
  116. {
  117. btCollisionWorld::objectQuerySingle(castShape, convexFromTrans, convexToTrans,
  118. collisionObject,
  119. collisionObject->getCollisionShape(),
  120. collisionObject->getWorldTransform(),
  121. resultCallback,
  122. allowedCcdPenetration);
  123. }
  124. }
  125. }
  126. }
  127. void btGhostObject::rayTest(const btVector3& rayFromWorld, const btVector3& rayToWorld, btCollisionWorld::RayResultCallback& resultCallback) const
  128. {
  129. btTransform rayFromTrans;
  130. rayFromTrans.setIdentity();
  131. rayFromTrans.setOrigin(rayFromWorld);
  132. btTransform rayToTrans;
  133. rayToTrans.setIdentity();
  134. rayToTrans.setOrigin(rayToWorld);
  135. int i;
  136. for (i = 0; i < m_overlappingObjects.size(); i++)
  137. {
  138. btCollisionObject* collisionObject = m_overlappingObjects[i];
  139. //only perform raycast if filterMask matches
  140. if (resultCallback.needsCollision(collisionObject->getBroadphaseHandle()))
  141. {
  142. btCollisionWorld::rayTestSingle(rayFromTrans, rayToTrans,
  143. collisionObject,
  144. collisionObject->getCollisionShape(),
  145. collisionObject->getWorldTransform(),
  146. resultCallback);
  147. }
  148. }
  149. }