PageRenderTime 120ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/cocos2d/external/bullet/BulletCollision/CollisionShapes/btCompoundShape.cpp

https://gitlab.com/gasabr/flappy-test
C++ | 356 lines | 249 code | 72 blank | 35 comment | 27 complexity | 8ef1dd6f97610abf05ad33afc10f5dcb MD5 | raw file
  1. /*
  2. Bullet Continuous Collision Detection and Physics Library
  3. Copyright (c) 2003-2009 Erwin Coumans http://bulletphysics.org
  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 "btCompoundShape.h"
  14. #include "btCollisionShape.h"
  15. #include "bullet/BulletCollision//BroadphaseCollision/btDbvt.h"
  16. #include "bullet/LinearMath/btSerializer.h"
  17. btCompoundShape::btCompoundShape(bool enableDynamicAabbTree)
  18. : m_localAabbMin(btScalar(BT_LARGE_FLOAT),btScalar(BT_LARGE_FLOAT),btScalar(BT_LARGE_FLOAT)),
  19. m_localAabbMax(btScalar(-BT_LARGE_FLOAT),btScalar(-BT_LARGE_FLOAT),btScalar(-BT_LARGE_FLOAT)),
  20. m_dynamicAabbTree(0),
  21. m_updateRevision(1),
  22. m_collisionMargin(btScalar(0.)),
  23. m_localScaling(btScalar(1.),btScalar(1.),btScalar(1.))
  24. {
  25. m_shapeType = COMPOUND_SHAPE_PROXYTYPE;
  26. if (enableDynamicAabbTree)
  27. {
  28. void* mem = btAlignedAlloc(sizeof(btDbvt),16);
  29. m_dynamicAabbTree = new(mem) btDbvt();
  30. btAssert(mem==m_dynamicAabbTree);
  31. }
  32. }
  33. btCompoundShape::~btCompoundShape()
  34. {
  35. if (m_dynamicAabbTree)
  36. {
  37. m_dynamicAabbTree->~btDbvt();
  38. btAlignedFree(m_dynamicAabbTree);
  39. }
  40. }
  41. void btCompoundShape::addChildShape(const btTransform& localTransform,btCollisionShape* shape)
  42. {
  43. m_updateRevision++;
  44. //m_childTransforms.push_back(localTransform);
  45. //m_childShapes.push_back(shape);
  46. btCompoundShapeChild child;
  47. child.m_node = 0;
  48. child.m_transform = localTransform;
  49. child.m_childShape = shape;
  50. child.m_childShapeType = shape->getShapeType();
  51. child.m_childMargin = shape->getMargin();
  52. //extend the local aabbMin/aabbMax
  53. btVector3 localAabbMin,localAabbMax;
  54. shape->getAabb(localTransform,localAabbMin,localAabbMax);
  55. for (int i=0;i<3;i++)
  56. {
  57. if (m_localAabbMin[i] > localAabbMin[i])
  58. {
  59. m_localAabbMin[i] = localAabbMin[i];
  60. }
  61. if (m_localAabbMax[i] < localAabbMax[i])
  62. {
  63. m_localAabbMax[i] = localAabbMax[i];
  64. }
  65. }
  66. if (m_dynamicAabbTree)
  67. {
  68. const btDbvtVolume bounds=btDbvtVolume::FromMM(localAabbMin,localAabbMax);
  69. int index = m_children.size();
  70. child.m_node = m_dynamicAabbTree->insert(bounds,(void*)index);
  71. }
  72. m_children.push_back(child);
  73. }
  74. void btCompoundShape::updateChildTransform(int childIndex, const btTransform& newChildTransform,bool shouldRecalculateLocalAabb)
  75. {
  76. m_children[childIndex].m_transform = newChildTransform;
  77. if (m_dynamicAabbTree)
  78. {
  79. ///update the dynamic aabb tree
  80. btVector3 localAabbMin,localAabbMax;
  81. m_children[childIndex].m_childShape->getAabb(newChildTransform,localAabbMin,localAabbMax);
  82. ATTRIBUTE_ALIGNED16(btDbvtVolume) bounds=btDbvtVolume::FromMM(localAabbMin,localAabbMax);
  83. //int index = m_children.size()-1;
  84. m_dynamicAabbTree->update(m_children[childIndex].m_node,bounds);
  85. }
  86. if (shouldRecalculateLocalAabb)
  87. {
  88. recalculateLocalAabb();
  89. }
  90. }
  91. void btCompoundShape::removeChildShapeByIndex(int childShapeIndex)
  92. {
  93. m_updateRevision++;
  94. btAssert(childShapeIndex >=0 && childShapeIndex < m_children.size());
  95. if (m_dynamicAabbTree)
  96. {
  97. m_dynamicAabbTree->remove(m_children[childShapeIndex].m_node);
  98. }
  99. m_children.swap(childShapeIndex,m_children.size()-1);
  100. if (m_dynamicAabbTree)
  101. m_children[childShapeIndex].m_node->dataAsInt = childShapeIndex;
  102. m_children.pop_back();
  103. }
  104. void btCompoundShape::removeChildShape(btCollisionShape* shape)
  105. {
  106. m_updateRevision++;
  107. // Find the children containing the shape specified, and remove those children.
  108. //note: there might be multiple children using the same shape!
  109. for(int i = m_children.size()-1; i >= 0 ; i--)
  110. {
  111. if(m_children[i].m_childShape == shape)
  112. {
  113. removeChildShapeByIndex(i);
  114. }
  115. }
  116. recalculateLocalAabb();
  117. }
  118. void btCompoundShape::recalculateLocalAabb()
  119. {
  120. // Recalculate the local aabb
  121. // Brute force, it iterates over all the shapes left.
  122. m_localAabbMin = btVector3(btScalar(BT_LARGE_FLOAT),btScalar(BT_LARGE_FLOAT),btScalar(BT_LARGE_FLOAT));
  123. m_localAabbMax = btVector3(btScalar(-BT_LARGE_FLOAT),btScalar(-BT_LARGE_FLOAT),btScalar(-BT_LARGE_FLOAT));
  124. //extend the local aabbMin/aabbMax
  125. for (int j = 0; j < m_children.size(); j++)
  126. {
  127. btVector3 localAabbMin,localAabbMax;
  128. m_children[j].m_childShape->getAabb(m_children[j].m_transform, localAabbMin, localAabbMax);
  129. for (int i=0;i<3;i++)
  130. {
  131. if (m_localAabbMin[i] > localAabbMin[i])
  132. m_localAabbMin[i] = localAabbMin[i];
  133. if (m_localAabbMax[i] < localAabbMax[i])
  134. m_localAabbMax[i] = localAabbMax[i];
  135. }
  136. }
  137. }
  138. ///getAabb's default implementation is brute force, expected derived classes to implement a fast dedicated version
  139. void btCompoundShape::getAabb(const btTransform& trans,btVector3& aabbMin,btVector3& aabbMax) const
  140. {
  141. btVector3 localHalfExtents = btScalar(0.5)*(m_localAabbMax-m_localAabbMin);
  142. btVector3 localCenter = btScalar(0.5)*(m_localAabbMax+m_localAabbMin);
  143. //avoid an illegal AABB when there are no children
  144. if (!m_children.size())
  145. {
  146. localHalfExtents.setValue(0,0,0);
  147. localCenter.setValue(0,0,0);
  148. }
  149. localHalfExtents += btVector3(getMargin(),getMargin(),getMargin());
  150. btMatrix3x3 abs_b = trans.getBasis().absolute();
  151. btVector3 center = trans(localCenter);
  152. btVector3 extent = localHalfExtents.dot3(abs_b[0], abs_b[1], abs_b[2]);
  153. aabbMin = center-extent;
  154. aabbMax = center+extent;
  155. }
  156. void btCompoundShape::calculateLocalInertia(btScalar mass,btVector3& inertia) const
  157. {
  158. //approximation: take the inertia from the aabb for now
  159. btTransform ident;
  160. ident.setIdentity();
  161. btVector3 aabbMin,aabbMax;
  162. getAabb(ident,aabbMin,aabbMax);
  163. btVector3 halfExtents = (aabbMax-aabbMin)*btScalar(0.5);
  164. btScalar lx=btScalar(2.)*(halfExtents.x());
  165. btScalar ly=btScalar(2.)*(halfExtents.y());
  166. btScalar lz=btScalar(2.)*(halfExtents.z());
  167. inertia[0] = mass/(btScalar(12.0)) * (ly*ly + lz*lz);
  168. inertia[1] = mass/(btScalar(12.0)) * (lx*lx + lz*lz);
  169. inertia[2] = mass/(btScalar(12.0)) * (lx*lx + ly*ly);
  170. }
  171. void btCompoundShape::calculatePrincipalAxisTransform(btScalar* masses, btTransform& principal, btVector3& inertia) const
  172. {
  173. int n = m_children.size();
  174. btScalar totalMass = 0;
  175. btVector3 center(0, 0, 0);
  176. int k;
  177. for (k = 0; k < n; k++)
  178. {
  179. btAssert(masses[k]>0);
  180. center += m_children[k].m_transform.getOrigin() * masses[k];
  181. totalMass += masses[k];
  182. }
  183. btAssert(totalMass>0);
  184. center /= totalMass;
  185. principal.setOrigin(center);
  186. btMatrix3x3 tensor(0, 0, 0, 0, 0, 0, 0, 0, 0);
  187. for ( k = 0; k < n; k++)
  188. {
  189. btVector3 i;
  190. m_children[k].m_childShape->calculateLocalInertia(masses[k], i);
  191. const btTransform& t = m_children[k].m_transform;
  192. btVector3 o = t.getOrigin() - center;
  193. //compute inertia tensor in coordinate system of compound shape
  194. btMatrix3x3 j = t.getBasis().transpose();
  195. j[0] *= i[0];
  196. j[1] *= i[1];
  197. j[2] *= i[2];
  198. j = t.getBasis() * j;
  199. //add inertia tensor
  200. tensor[0] += j[0];
  201. tensor[1] += j[1];
  202. tensor[2] += j[2];
  203. //compute inertia tensor of pointmass at o
  204. btScalar o2 = o.length2();
  205. j[0].setValue(o2, 0, 0);
  206. j[1].setValue(0, o2, 0);
  207. j[2].setValue(0, 0, o2);
  208. j[0] += o * -o.x();
  209. j[1] += o * -o.y();
  210. j[2] += o * -o.z();
  211. //add inertia tensor of pointmass
  212. tensor[0] += masses[k] * j[0];
  213. tensor[1] += masses[k] * j[1];
  214. tensor[2] += masses[k] * j[2];
  215. }
  216. tensor.diagonalize(principal.getBasis(), btScalar(0.00001), 20);
  217. inertia.setValue(tensor[0][0], tensor[1][1], tensor[2][2]);
  218. }
  219. void btCompoundShape::setLocalScaling(const btVector3& scaling)
  220. {
  221. for(int i = 0; i < m_children.size(); i++)
  222. {
  223. btTransform childTrans = getChildTransform(i);
  224. btVector3 childScale = m_children[i].m_childShape->getLocalScaling();
  225. // childScale = childScale * (childTrans.getBasis() * scaling);
  226. childScale = childScale * scaling / m_localScaling;
  227. m_children[i].m_childShape->setLocalScaling(childScale);
  228. childTrans.setOrigin((childTrans.getOrigin()) * scaling / m_localScaling);
  229. updateChildTransform(i, childTrans,false);
  230. }
  231. m_localScaling = scaling;
  232. recalculateLocalAabb();
  233. }
  234. void btCompoundShape::createAabbTreeFromChildren()
  235. {
  236. if ( !m_dynamicAabbTree )
  237. {
  238. void* mem = btAlignedAlloc(sizeof(btDbvt),16);
  239. m_dynamicAabbTree = new(mem) btDbvt();
  240. btAssert(mem==m_dynamicAabbTree);
  241. for ( int index = 0; index < m_children.size(); index++ )
  242. {
  243. btCompoundShapeChild &child = m_children[index];
  244. //extend the local aabbMin/aabbMax
  245. btVector3 localAabbMin,localAabbMax;
  246. child.m_childShape->getAabb(child.m_transform,localAabbMin,localAabbMax);
  247. const btDbvtVolume bounds=btDbvtVolume::FromMM(localAabbMin,localAabbMax);
  248. child.m_node = m_dynamicAabbTree->insert(bounds,(void*)index);
  249. }
  250. }
  251. }
  252. ///fills the dataBuffer and returns the struct name (and 0 on failure)
  253. const char* btCompoundShape::serialize(void* dataBuffer, btSerializer* serializer) const
  254. {
  255. btCompoundShapeData* shapeData = (btCompoundShapeData*) dataBuffer;
  256. btCollisionShape::serialize(&shapeData->m_collisionShapeData, serializer);
  257. shapeData->m_collisionMargin = float(m_collisionMargin);
  258. shapeData->m_numChildShapes = m_children.size();
  259. shapeData->m_childShapePtr = 0;
  260. if (shapeData->m_numChildShapes)
  261. {
  262. btChunk* chunk = serializer->allocate(sizeof(btCompoundShapeChildData),shapeData->m_numChildShapes);
  263. btCompoundShapeChildData* memPtr = (btCompoundShapeChildData*)chunk->m_oldPtr;
  264. shapeData->m_childShapePtr = (btCompoundShapeChildData*)serializer->getUniquePointer(memPtr);
  265. for (int i=0;i<shapeData->m_numChildShapes;i++,memPtr++)
  266. {
  267. memPtr->m_childMargin = float(m_children[i].m_childMargin);
  268. memPtr->m_childShape = (btCollisionShapeData*)serializer->getUniquePointer(m_children[i].m_childShape);
  269. //don't serialize shapes that already have been serialized
  270. if (!serializer->findPointer(m_children[i].m_childShape))
  271. {
  272. btChunk* chunk = serializer->allocate(m_children[i].m_childShape->calculateSerializeBufferSize(),1);
  273. const char* structType = m_children[i].m_childShape->serialize(chunk->m_oldPtr,serializer);
  274. serializer->finalizeChunk(chunk,structType,BT_SHAPE_CODE,m_children[i].m_childShape);
  275. }
  276. memPtr->m_childShapeType = m_children[i].m_childShapeType;
  277. m_children[i].m_transform.serializeFloat(memPtr->m_transform);
  278. }
  279. serializer->finalizeChunk(chunk,"btCompoundShapeChildData",BT_ARRAY_CODE,chunk->m_oldPtr);
  280. }
  281. return "btCompoundShapeData";
  282. }