btGImpactShape.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /*
  2. This source file is part of GIMPACT Library.
  3. For the latest info, see http://gimpact.sourceforge.net/
  4. Copyright (c) 2007 Francisco Leon Najera. C.C. 80087371.
  5. email: projectileman@yahoo.com
  6. This software is provided 'as-is', without any express or implied warranty.
  7. In no event will the authors be held liable for any damages arising from the use of this software.
  8. Permission is granted to anyone to use this software for any purpose,
  9. including commercial applications, and to alter it and redistribute it freely,
  10. subject to the following restrictions:
  11. 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.
  12. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  13. 3. This notice may not be removed or altered from any source distribution.
  14. */
  15. #include "btGImpactShape.h"
  16. #include "btGImpactMassUtil.h"
  17. btGImpactMeshShapePart::btGImpactMeshShapePart( btStridingMeshInterface * meshInterface, int part )
  18. {
  19. // moved from .h to .cpp because of conditional compilation
  20. // (The setting of BT_THREADSAFE may differ between various cpp files, so it is best to
  21. // avoid using it in h files)
  22. m_primitive_manager.m_meshInterface = meshInterface;
  23. m_primitive_manager.m_part = part;
  24. m_box_set.setPrimitiveManager( &m_primitive_manager );
  25. #if BT_THREADSAFE
  26. // If threadsafe is requested, this object uses a different lock/unlock
  27. // model with the btStridingMeshInterface -- lock once when the object is constructed
  28. // and unlock once in the destructor.
  29. // The other way of locking and unlocking for each collision check in the narrowphase
  30. // is not threadsafe. Note these are not thread-locks, they are calls to the meshInterface's
  31. // getLockedReadOnlyVertexIndexBase virtual function, which by default just returns a couple of
  32. // pointers. In theory a client could override the lock function to do all sorts of
  33. // things like reading data from GPU memory, or decompressing data on the fly, but such things
  34. // do not seem all that likely or useful, given the performance cost.
  35. m_primitive_manager.lock();
  36. #endif
  37. }
  38. btGImpactMeshShapePart::~btGImpactMeshShapePart()
  39. {
  40. // moved from .h to .cpp because of conditional compilation
  41. #if BT_THREADSAFE
  42. m_primitive_manager.unlock();
  43. #endif
  44. }
  45. void btGImpactMeshShapePart::lockChildShapes() const
  46. {
  47. // moved from .h to .cpp because of conditional compilation
  48. #if ! BT_THREADSAFE
  49. // called in the narrowphase -- not threadsafe!
  50. void * dummy = (void*) ( m_box_set.getPrimitiveManager() );
  51. TrimeshPrimitiveManager * dummymanager = static_cast<TrimeshPrimitiveManager *>( dummy );
  52. dummymanager->lock();
  53. #endif
  54. }
  55. void btGImpactMeshShapePart::unlockChildShapes() const
  56. {
  57. // moved from .h to .cpp because of conditional compilation
  58. #if ! BT_THREADSAFE
  59. // called in the narrowphase -- not threadsafe!
  60. void * dummy = (void*) ( m_box_set.getPrimitiveManager() );
  61. TrimeshPrimitiveManager * dummymanager = static_cast<TrimeshPrimitiveManager *>( dummy );
  62. dummymanager->unlock();
  63. #endif
  64. }
  65. #define CALC_EXACT_INERTIA 1
  66. void btGImpactCompoundShape::calculateLocalInertia(btScalar mass,btVector3& inertia) const
  67. {
  68. lockChildShapes();
  69. #ifdef CALC_EXACT_INERTIA
  70. inertia.setValue(0.f,0.f,0.f);
  71. int i = this->getNumChildShapes();
  72. btScalar shapemass = mass/btScalar(i);
  73. while(i--)
  74. {
  75. btVector3 temp_inertia;
  76. m_childShapes[i]->calculateLocalInertia(shapemass,temp_inertia);
  77. if(childrenHasTransform())
  78. {
  79. inertia = gim_inertia_add_transformed( inertia,temp_inertia,m_childTransforms[i]);
  80. }
  81. else
  82. {
  83. inertia = gim_inertia_add_transformed( inertia,temp_inertia,btTransform::getIdentity());
  84. }
  85. }
  86. #else
  87. // Calc box inertia
  88. btScalar lx= m_localAABB.m_max[0] - m_localAABB.m_min[0];
  89. btScalar ly= m_localAABB.m_max[1] - m_localAABB.m_min[1];
  90. btScalar lz= m_localAABB.m_max[2] - m_localAABB.m_min[2];
  91. const btScalar x2 = lx*lx;
  92. const btScalar y2 = ly*ly;
  93. const btScalar z2 = lz*lz;
  94. const btScalar scaledmass = mass * btScalar(0.08333333);
  95. inertia = scaledmass * (btVector3(y2+z2,x2+z2,x2+y2));
  96. #endif
  97. unlockChildShapes();
  98. }
  99. void btGImpactMeshShapePart::calculateLocalInertia(btScalar mass,btVector3& inertia) const
  100. {
  101. lockChildShapes();
  102. #ifdef CALC_EXACT_INERTIA
  103. inertia.setValue(0.f,0.f,0.f);
  104. int i = this->getVertexCount();
  105. btScalar pointmass = mass/btScalar(i);
  106. while(i--)
  107. {
  108. btVector3 pointintertia;
  109. this->getVertex(i,pointintertia);
  110. pointintertia = gim_get_point_inertia(pointintertia,pointmass);
  111. inertia+=pointintertia;
  112. }
  113. #else
  114. // Calc box inertia
  115. btScalar lx= m_localAABB.m_max[0] - m_localAABB.m_min[0];
  116. btScalar ly= m_localAABB.m_max[1] - m_localAABB.m_min[1];
  117. btScalar lz= m_localAABB.m_max[2] - m_localAABB.m_min[2];
  118. const btScalar x2 = lx*lx;
  119. const btScalar y2 = ly*ly;
  120. const btScalar z2 = lz*lz;
  121. const btScalar scaledmass = mass * btScalar(0.08333333);
  122. inertia = scaledmass * (btVector3(y2+z2,x2+z2,x2+y2));
  123. #endif
  124. unlockChildShapes();
  125. }
  126. void btGImpactMeshShape::calculateLocalInertia(btScalar mass,btVector3& inertia) const
  127. {
  128. #ifdef CALC_EXACT_INERTIA
  129. inertia.setValue(0.f,0.f,0.f);
  130. int i = this->getMeshPartCount();
  131. btScalar partmass = mass/btScalar(i);
  132. while(i--)
  133. {
  134. btVector3 partinertia;
  135. getMeshPart(i)->calculateLocalInertia(partmass,partinertia);
  136. inertia+=partinertia;
  137. }
  138. #else
  139. // Calc box inertia
  140. btScalar lx= m_localAABB.m_max[0] - m_localAABB.m_min[0];
  141. btScalar ly= m_localAABB.m_max[1] - m_localAABB.m_min[1];
  142. btScalar lz= m_localAABB.m_max[2] - m_localAABB.m_min[2];
  143. const btScalar x2 = lx*lx;
  144. const btScalar y2 = ly*ly;
  145. const btScalar z2 = lz*lz;
  146. const btScalar scaledmass = mass * btScalar(0.08333333);
  147. inertia = scaledmass * (btVector3(y2+z2,x2+z2,x2+y2));
  148. #endif
  149. }
  150. void btGImpactMeshShape::rayTest(const btVector3& rayFrom, const btVector3& rayTo, btCollisionWorld::RayResultCallback& resultCallback) const
  151. {
  152. }
  153. void btGImpactMeshShapePart::processAllTrianglesRay(btTriangleCallback* callback,const btVector3& rayFrom, const btVector3& rayTo) const
  154. {
  155. lockChildShapes();
  156. btAlignedObjectArray<int> collided;
  157. btVector3 rayDir(rayTo - rayFrom);
  158. rayDir.normalize();
  159. m_box_set.rayQuery(rayDir, rayFrom, collided);
  160. if(collided.size()==0)
  161. {
  162. unlockChildShapes();
  163. return;
  164. }
  165. int part = (int)getPart();
  166. btPrimitiveTriangle triangle;
  167. int i = collided.size();
  168. while(i--)
  169. {
  170. getPrimitiveTriangle(collided[i],triangle);
  171. callback->processTriangle(triangle.m_vertices,part,collided[i]);
  172. }
  173. unlockChildShapes();
  174. }
  175. void btGImpactMeshShapePart::processAllTriangles(btTriangleCallback* callback,const btVector3& aabbMin,const btVector3& aabbMax) const
  176. {
  177. lockChildShapes();
  178. btAABB box;
  179. box.m_min = aabbMin;
  180. box.m_max = aabbMax;
  181. btAlignedObjectArray<int> collided;
  182. m_box_set.boxQuery(box,collided);
  183. if(collided.size()==0)
  184. {
  185. unlockChildShapes();
  186. return;
  187. }
  188. int part = (int)getPart();
  189. btPrimitiveTriangle triangle;
  190. int i = collided.size();
  191. while(i--)
  192. {
  193. this->getPrimitiveTriangle(collided[i],triangle);
  194. callback->processTriangle(triangle.m_vertices,part,collided[i]);
  195. }
  196. unlockChildShapes();
  197. }
  198. void btGImpactMeshShape::processAllTriangles(btTriangleCallback* callback,const btVector3& aabbMin,const btVector3& aabbMax) const
  199. {
  200. int i = m_mesh_parts.size();
  201. while(i--)
  202. {
  203. m_mesh_parts[i]->processAllTriangles(callback,aabbMin,aabbMax);
  204. }
  205. }
  206. void btGImpactMeshShape::processAllTrianglesRay(btTriangleCallback* callback,const btVector3& rayFrom, const btVector3& rayTo) const
  207. {
  208. int i = m_mesh_parts.size();
  209. while(i--)
  210. {
  211. m_mesh_parts[i]->processAllTrianglesRay(callback, rayFrom, rayTo);
  212. }
  213. }
  214. ///fills the dataBuffer and returns the struct name (and 0 on failure)
  215. const char* btGImpactMeshShape::serialize(void* dataBuffer, btSerializer* serializer) const
  216. {
  217. btGImpactMeshShapeData* trimeshData = (btGImpactMeshShapeData*) dataBuffer;
  218. btCollisionShape::serialize(&trimeshData->m_collisionShapeData,serializer);
  219. m_meshInterface->serialize(&trimeshData->m_meshInterface, serializer);
  220. trimeshData->m_collisionMargin = float(m_collisionMargin);
  221. localScaling.serializeFloat(trimeshData->m_localScaling);
  222. trimeshData->m_gimpactSubType = int(getGImpactShapeType());
  223. return "btGImpactMeshShapeData";
  224. }