btGImpactShape.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. #define CALC_EXACT_INERTIA 1
  18. void btGImpactCompoundShape::calculateLocalInertia(btScalar mass,btVector3& inertia) const
  19. {
  20. lockChildShapes();
  21. #ifdef CALC_EXACT_INERTIA
  22. inertia.setValue(0.f,0.f,0.f);
  23. int i = this->getNumChildShapes();
  24. btScalar shapemass = mass/btScalar(i);
  25. while(i--)
  26. {
  27. btVector3 temp_inertia;
  28. m_childShapes[i]->calculateLocalInertia(shapemass,temp_inertia);
  29. if(childrenHasTransform())
  30. {
  31. inertia = gim_inertia_add_transformed( inertia,temp_inertia,m_childTransforms[i]);
  32. }
  33. else
  34. {
  35. inertia = gim_inertia_add_transformed( inertia,temp_inertia,btTransform::getIdentity());
  36. }
  37. }
  38. #else
  39. // Calc box inertia
  40. btScalar lx= m_localAABB.m_max[0] - m_localAABB.m_min[0];
  41. btScalar ly= m_localAABB.m_max[1] - m_localAABB.m_min[1];
  42. btScalar lz= m_localAABB.m_max[2] - m_localAABB.m_min[2];
  43. const btScalar x2 = lx*lx;
  44. const btScalar y2 = ly*ly;
  45. const btScalar z2 = lz*lz;
  46. const btScalar scaledmass = mass * btScalar(0.08333333);
  47. inertia = scaledmass * (btVector3(y2+z2,x2+z2,x2+y2));
  48. #endif
  49. unlockChildShapes();
  50. }
  51. void btGImpactMeshShapePart::calculateLocalInertia(btScalar mass,btVector3& inertia) const
  52. {
  53. lockChildShapes();
  54. #ifdef CALC_EXACT_INERTIA
  55. inertia.setValue(0.f,0.f,0.f);
  56. int i = this->getVertexCount();
  57. btScalar pointmass = mass/btScalar(i);
  58. while(i--)
  59. {
  60. btVector3 pointintertia;
  61. this->getVertex(i,pointintertia);
  62. pointintertia = gim_get_point_inertia(pointintertia,pointmass);
  63. inertia+=pointintertia;
  64. }
  65. #else
  66. // Calc box inertia
  67. btScalar lx= m_localAABB.m_max[0] - m_localAABB.m_min[0];
  68. btScalar ly= m_localAABB.m_max[1] - m_localAABB.m_min[1];
  69. btScalar lz= m_localAABB.m_max[2] - m_localAABB.m_min[2];
  70. const btScalar x2 = lx*lx;
  71. const btScalar y2 = ly*ly;
  72. const btScalar z2 = lz*lz;
  73. const btScalar scaledmass = mass * btScalar(0.08333333);
  74. inertia = scaledmass * (btVector3(y2+z2,x2+z2,x2+y2));
  75. #endif
  76. unlockChildShapes();
  77. }
  78. void btGImpactMeshShape::calculateLocalInertia(btScalar mass,btVector3& inertia) const
  79. {
  80. #ifdef CALC_EXACT_INERTIA
  81. inertia.setValue(0.f,0.f,0.f);
  82. int i = this->getMeshPartCount();
  83. btScalar partmass = mass/btScalar(i);
  84. while(i--)
  85. {
  86. btVector3 partinertia;
  87. getMeshPart(i)->calculateLocalInertia(partmass,partinertia);
  88. inertia+=partinertia;
  89. }
  90. #else
  91. // Calc box inertia
  92. btScalar lx= m_localAABB.m_max[0] - m_localAABB.m_min[0];
  93. btScalar ly= m_localAABB.m_max[1] - m_localAABB.m_min[1];
  94. btScalar lz= m_localAABB.m_max[2] - m_localAABB.m_min[2];
  95. const btScalar x2 = lx*lx;
  96. const btScalar y2 = ly*ly;
  97. const btScalar z2 = lz*lz;
  98. const btScalar scaledmass = mass * btScalar(0.08333333);
  99. inertia = scaledmass * (btVector3(y2+z2,x2+z2,x2+y2));
  100. #endif
  101. }
  102. void btGImpactMeshShape::rayTest(const btVector3& rayFrom, const btVector3& rayTo, btCollisionWorld::RayResultCallback& resultCallback) const
  103. {
  104. }
  105. void btGImpactMeshShapePart::processAllTrianglesRay(btTriangleCallback* callback,const btVector3& rayFrom, const btVector3& rayTo) const
  106. {
  107. lockChildShapes();
  108. btAlignedObjectArray<int> collided;
  109. btVector3 rayDir(rayTo - rayFrom);
  110. rayDir.normalize();
  111. m_box_set.rayQuery(rayDir, rayFrom, collided);
  112. if(collided.size()==0)
  113. {
  114. unlockChildShapes();
  115. return;
  116. }
  117. int part = (int)getPart();
  118. btPrimitiveTriangle triangle;
  119. int i = collided.size();
  120. while(i--)
  121. {
  122. getPrimitiveTriangle(collided[i],triangle);
  123. callback->processTriangle(triangle.m_vertices,part,collided[i]);
  124. }
  125. unlockChildShapes();
  126. }
  127. void btGImpactMeshShapePart::processAllTriangles(btTriangleCallback* callback,const btVector3& aabbMin,const btVector3& aabbMax) const
  128. {
  129. lockChildShapes();
  130. btAABB box;
  131. box.m_min = aabbMin;
  132. box.m_max = aabbMax;
  133. btAlignedObjectArray<int> collided;
  134. m_box_set.boxQuery(box,collided);
  135. if(collided.size()==0)
  136. {
  137. unlockChildShapes();
  138. return;
  139. }
  140. int part = (int)getPart();
  141. btPrimitiveTriangle triangle;
  142. int i = collided.size();
  143. while(i--)
  144. {
  145. this->getPrimitiveTriangle(collided[i],triangle);
  146. callback->processTriangle(triangle.m_vertices,part,collided[i]);
  147. }
  148. unlockChildShapes();
  149. }
  150. void btGImpactMeshShape::processAllTriangles(btTriangleCallback* callback,const btVector3& aabbMin,const btVector3& aabbMax) const
  151. {
  152. int i = m_mesh_parts.size();
  153. while(i--)
  154. {
  155. m_mesh_parts[i]->processAllTriangles(callback,aabbMin,aabbMax);
  156. }
  157. }
  158. void btGImpactMeshShape::processAllTrianglesRay(btTriangleCallback* callback,const btVector3& rayFrom, const btVector3& rayTo) const
  159. {
  160. int i = m_mesh_parts.size();
  161. while(i--)
  162. {
  163. m_mesh_parts[i]->processAllTrianglesRay(callback, rayFrom, rayTo);
  164. }
  165. }
  166. ///fills the dataBuffer and returns the struct name (and 0 on failure)
  167. const char* btGImpactMeshShape::serialize(void* dataBuffer, btSerializer* serializer) const
  168. {
  169. btGImpactMeshShapeData* trimeshData = (btGImpactMeshShapeData*) dataBuffer;
  170. btCollisionShape::serialize(&trimeshData->m_collisionShapeData,serializer);
  171. m_meshInterface->serialize(&trimeshData->m_meshInterface, serializer);
  172. trimeshData->m_collisionMargin = float(m_collisionMargin);
  173. localScaling.serializeFloat(trimeshData->m_localScaling);
  174. trimeshData->m_gimpactSubType = int(getGImpactShapeType());
  175. return "btGImpactMeshShapeData";
  176. }