btMultiSphereShape.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. #if defined (_WIN32) || defined (__i386__)
  14. #define BT_USE_SSE_IN_API
  15. #endif
  16. #include "btMultiSphereShape.h"
  17. #include "bullet/BulletCollision//CollisionShapes/btCollisionMargin.h"
  18. #include "bullet/LinearMath/btQuaternion.h"
  19. #include "bullet/LinearMath/btSerializer.h"
  20. btMultiSphereShape::btMultiSphereShape (const btVector3* positions,const btScalar* radi,int numSpheres)
  21. :btConvexInternalAabbCachingShape ()
  22. {
  23. m_shapeType = MULTI_SPHERE_SHAPE_PROXYTYPE;
  24. //btScalar startMargin = btScalar(BT_LARGE_FLOAT);
  25. m_localPositionArray.resize(numSpheres);
  26. m_radiArray.resize(numSpheres);
  27. for (int i=0;i<numSpheres;i++)
  28. {
  29. m_localPositionArray[i] = positions[i];
  30. m_radiArray[i] = radi[i];
  31. }
  32. recalcLocalAabb();
  33. }
  34. #ifndef MIN
  35. #define MIN( _a, _b) ((_a) < (_b) ? (_a) : (_b))
  36. #endif
  37. btVector3 btMultiSphereShape::localGetSupportingVertexWithoutMargin(const btVector3& vec0)const
  38. {
  39. btVector3 supVec(0,0,0);
  40. btScalar maxDot(btScalar(-BT_LARGE_FLOAT));
  41. btVector3 vec = vec0;
  42. btScalar lenSqr = vec.length2();
  43. if (lenSqr < (SIMD_EPSILON*SIMD_EPSILON))
  44. {
  45. vec.setValue(1,0,0);
  46. } else
  47. {
  48. btScalar rlen = btScalar(1.) / btSqrt(lenSqr );
  49. vec *= rlen;
  50. }
  51. btVector3 vtx;
  52. btScalar newDot;
  53. const btVector3* pos = &m_localPositionArray[0];
  54. const btScalar* rad = &m_radiArray[0];
  55. int numSpheres = m_localPositionArray.size();
  56. for( int k = 0; k < numSpheres; k+= 128 )
  57. {
  58. btVector3 temp[128];
  59. int inner_count = MIN( numSpheres - k, 128 );
  60. for( long i = 0; i < inner_count; i++ )
  61. {
  62. temp[i] = (*pos) +vec*m_localScaling*(*rad) - vec * getMargin();
  63. pos++;
  64. rad++;
  65. }
  66. long i = vec.maxDot( temp, inner_count, newDot);
  67. if( newDot > maxDot )
  68. {
  69. maxDot = newDot;
  70. supVec = temp[i];
  71. }
  72. }
  73. return supVec;
  74. }
  75. void btMultiSphereShape::batchedUnitVectorGetSupportingVertexWithoutMargin(const btVector3* vectors,btVector3* supportVerticesOut,int numVectors) const
  76. {
  77. for (int j=0;j<numVectors;j++)
  78. {
  79. btScalar maxDot(btScalar(-BT_LARGE_FLOAT));
  80. const btVector3& vec = vectors[j];
  81. btVector3 vtx;
  82. btScalar newDot;
  83. const btVector3* pos = &m_localPositionArray[0];
  84. const btScalar* rad = &m_radiArray[0];
  85. int numSpheres = m_localPositionArray.size();
  86. for( int k = 0; k < numSpheres; k+= 128 )
  87. {
  88. btVector3 temp[128];
  89. int inner_count = MIN( numSpheres - k, 128 );
  90. for( long i = 0; i < inner_count; i++ )
  91. {
  92. temp[i] = (*pos) +vec*m_localScaling*(*rad) - vec * getMargin();
  93. pos++;
  94. rad++;
  95. }
  96. long i = vec.maxDot( temp, inner_count, newDot);
  97. if( newDot > maxDot )
  98. {
  99. maxDot = newDot;
  100. supportVerticesOut[j] = temp[i];
  101. }
  102. }
  103. }
  104. }
  105. void btMultiSphereShape::calculateLocalInertia(btScalar mass,btVector3& inertia) const
  106. {
  107. //as an approximation, take the inertia of the box that bounds the spheres
  108. btVector3 localAabbMin,localAabbMax;
  109. getCachedLocalAabb(localAabbMin,localAabbMax);
  110. btVector3 halfExtents = (localAabbMax-localAabbMin)*btScalar(0.5);
  111. btScalar lx=btScalar(2.)*(halfExtents.x());
  112. btScalar ly=btScalar(2.)*(halfExtents.y());
  113. btScalar lz=btScalar(2.)*(halfExtents.z());
  114. inertia.setValue(mass/(btScalar(12.0)) * (ly*ly + lz*lz),
  115. mass/(btScalar(12.0)) * (lx*lx + lz*lz),
  116. mass/(btScalar(12.0)) * (lx*lx + ly*ly));
  117. }
  118. ///fills the dataBuffer and returns the struct name (and 0 on failure)
  119. const char* btMultiSphereShape::serialize(void* dataBuffer, btSerializer* serializer) const
  120. {
  121. btMultiSphereShapeData* shapeData = (btMultiSphereShapeData*) dataBuffer;
  122. btConvexInternalShape::serialize(&shapeData->m_convexInternalShapeData, serializer);
  123. int numElem = m_localPositionArray.size();
  124. shapeData->m_localPositionArrayPtr = numElem ? (btPositionAndRadius*)serializer->getUniquePointer((void*)&m_localPositionArray[0]): 0;
  125. shapeData->m_localPositionArraySize = numElem;
  126. if (numElem)
  127. {
  128. btChunk* chunk = serializer->allocate(sizeof(btPositionAndRadius),numElem);
  129. btPositionAndRadius* memPtr = (btPositionAndRadius*)chunk->m_oldPtr;
  130. for (int i=0;i<numElem;i++,memPtr++)
  131. {
  132. m_localPositionArray[i].serializeFloat(memPtr->m_pos);
  133. memPtr->m_radius = float(m_radiArray[i]);
  134. }
  135. serializer->finalizeChunk(chunk,"btPositionAndRadius",BT_ARRAY_CODE,(void*)&m_localPositionArray[0]);
  136. }
  137. return "btMultiSphereShapeData";
  138. }