btCollision.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platform.h"
  23. #include "T3D/physics/bullet/btCollision.h"
  24. #include "math/mPoint3.h"
  25. #include "math/mMatrix.h"
  26. #include "T3D/physics/bullet/bt.h"
  27. #include "T3D/physics/bullet/btCasts.h"
  28. class btHeightfieldTerrainShapeCustom : public btHeightfieldTerrainShape
  29. {
  30. bool* mHoles;
  31. public:
  32. btHeightfieldTerrainShapeCustom(const bool *holes,
  33. int heightStickWidth,
  34. int heightStickLength,
  35. const void* heightfieldData,
  36. btScalar heightScale,
  37. btScalar minHeight,
  38. btScalar maxHeight,
  39. int upAxis,
  40. PHY_ScalarType heightDataType,
  41. bool flipQuadEdges) : btHeightfieldTerrainShape(heightStickWidth,
  42. heightStickLength,
  43. heightfieldData,
  44. heightScale,
  45. minHeight,
  46. maxHeight,
  47. upAxis,
  48. heightDataType,
  49. flipQuadEdges)
  50. {
  51. mHoles = new bool[heightStickWidth * heightStickLength];
  52. dMemcpy(mHoles, holes, heightStickWidth * heightStickLength * sizeof(bool));
  53. }
  54. virtual ~btHeightfieldTerrainShapeCustom()
  55. {
  56. delete[] mHoles;
  57. }
  58. virtual void processAllTriangles(btTriangleCallback* callback, const btVector3& aabbMin, const btVector3& aabbMax) const;
  59. };
  60. BtCollision::BtCollision()
  61. : mCompound( NULL ),
  62. mLocalXfm( true )
  63. {
  64. }
  65. BtCollision::~BtCollision()
  66. {
  67. SAFE_DELETE( mCompound );
  68. for ( U32 i=0; i < mShapes.size(); i++ )
  69. delete mShapes[i];
  70. for ( U32 i=0; i < mMeshInterfaces.size(); i++ )
  71. delete mMeshInterfaces[i];
  72. }
  73. btCollisionShape* BtCollision::getShape()
  74. {
  75. if ( mCompound )
  76. return mCompound;
  77. if ( mShapes.empty() )
  78. return NULL;
  79. return mShapes.first();
  80. }
  81. void BtCollision::_addShape( btCollisionShape *shape, const MatrixF &localXfm )
  82. {
  83. AssertFatal( !shape->isCompound(), "BtCollision::_addShape - Shape should not be a compound!" );
  84. // Stick the shape into the array to delete later. Remember
  85. // that the compound shape doesn't delete its children.
  86. mShapes.push_back( shape );
  87. // If this is the first shape then just store the
  88. // local transform and we're done.
  89. if ( mShapes.size() == 1 )
  90. {
  91. mLocalXfm = localXfm;
  92. return;
  93. }
  94. // We use a compound to store the shapes with their
  95. // local transforms... so create it if we haven't already.
  96. if ( !mCompound )
  97. {
  98. mCompound = new btCompoundShape();
  99. // There should only be one shape now... add it and
  100. // clear the local transform.
  101. mCompound->addChildShape( btCast<btTransform>( mLocalXfm ), mShapes.first() );
  102. mLocalXfm = MatrixF::Identity;
  103. }
  104. // Add the new shape to the compound.
  105. mCompound->addChildShape( btCast<btTransform>( localXfm ), shape );
  106. }
  107. void BtCollision::addPlane( const PlaneF &plane )
  108. {
  109. // NOTE: Torque uses a negative D... thats why we flip it here.
  110. btStaticPlaneShape *shape = new btStaticPlaneShape( btVector3( plane.x, plane.y, plane.z ), -plane.d );
  111. _addShape( shape, MatrixF::Identity );
  112. }
  113. void BtCollision::addBox( const Point3F &halfWidth,
  114. const MatrixF &localXfm )
  115. {
  116. btBoxShape *shape = new btBoxShape( btVector3( halfWidth.x, halfWidth.y, halfWidth.z ) );
  117. shape->setMargin( 0.01f );
  118. _addShape( shape, localXfm );
  119. }
  120. void BtCollision::addSphere( const F32 radius,
  121. const MatrixF &localXfm )
  122. {
  123. btSphereShape *shape = new btSphereShape( radius );
  124. shape->setMargin( 0.01f );
  125. _addShape( shape, localXfm );
  126. }
  127. void BtCollision::addCapsule( F32 radius,
  128. F32 height,
  129. const MatrixF &localXfm )
  130. {
  131. btCapsuleShape *shape = new btCapsuleShape( radius, height );
  132. shape->setMargin( 0.01f );
  133. _addShape( shape, localXfm );
  134. }
  135. bool BtCollision::addConvex( const Point3F *points,
  136. U32 count,
  137. const MatrixF &localXfm )
  138. {
  139. btConvexHullShape *shape = new btConvexHullShape( (btScalar*)points, count, sizeof( Point3F ) );
  140. shape->setMargin( 0.01f );
  141. _addShape( shape, localXfm );
  142. return true;
  143. }
  144. bool BtCollision::addTriangleMesh( const Point3F *vert,
  145. U32 vertCount,
  146. const U32 *index,
  147. U32 triCount,
  148. const MatrixF &localXfm )
  149. {
  150. // Setup the interface for loading the triangles.
  151. btTriangleMesh *meshInterface = new btTriangleMesh( true, false );
  152. for ( ; triCount-- ; )
  153. {
  154. meshInterface->addTriangle( btCast<btVector3>( vert[ *( index + 0 ) ] ),
  155. btCast<btVector3>( vert[ *( index + 1 ) ] ),
  156. btCast<btVector3>( vert[ *( index + 2 ) ] ),
  157. false );
  158. index += 3;
  159. }
  160. mMeshInterfaces.push_back( meshInterface );
  161. btBvhTriangleMeshShape *shape = new btBvhTriangleMeshShape( meshInterface, true, true );
  162. shape->setMargin( 0.01f );
  163. _addShape( shape, localXfm );
  164. return true;
  165. }
  166. bool BtCollision::addHeightfield( const U16 *heights,
  167. const bool *holes, // TODO: Bullet height fields do not support holes
  168. U32 blockSize,
  169. F32 metersPerSample,
  170. const MatrixF &localXfm )
  171. {
  172. // We pass the absolute maximum and minimum of a U16 height
  173. // field and not the actual min and max. This helps with
  174. // placement.
  175. const F32 heightScale = 0.03125f;
  176. const F32 minHeight = 0;
  177. const F32 maxHeight = 65535 * heightScale;
  178. btHeightfieldTerrainShapeCustom* shape = new btHeightfieldTerrainShapeCustom(holes,
  179. blockSize, blockSize,
  180. reinterpret_cast<const void*>(heights),
  181. heightScale,
  182. 0, 0xFFFF * heightScale,
  183. 2, // Z up!
  184. PHY_SHORT,
  185. false);
  186. shape->setMargin( 0.01f );
  187. shape->setLocalScaling( btVector3( metersPerSample, metersPerSample, 1.0f ) );
  188. shape->setUseDiamondSubdivision( true );
  189. // The local axis of the heightfield is the exact center of
  190. // its bounds defined as...
  191. //
  192. // ( blockSize * samplesPerMeter, blockSize * samplesPerMeter, maxHeight ) / 2.0f
  193. //
  194. // So we create a local transform to move it to the min point
  195. // of the bounds so it matched Torque terrain.
  196. Point3F offset( (F32)blockSize * metersPerSample / 2.0f,
  197. (F32)blockSize * metersPerSample / 2.0f,
  198. maxHeight / 2.0f );
  199. // And also bump it by half a sample square size.
  200. offset.x -= metersPerSample / 2.0f;
  201. offset.y -= metersPerSample / 2.0f;
  202. MatrixF offsetXfm( true );
  203. offsetXfm.setPosition( offset );
  204. _addShape( shape, offsetXfm );
  205. return true;
  206. }
  207. void btHeightfieldTerrainShapeCustom::processAllTriangles(btTriangleCallback* callback, const btVector3& aabbMin, const btVector3& aabbMax) const
  208. {
  209. // scale down the input aabb's so they are in local (non-scaled) coordinates
  210. btVector3 localAabbMin = aabbMin * btVector3(1.f / m_localScaling[0], 1.f / m_localScaling[1], 1.f / m_localScaling[2]);
  211. btVector3 localAabbMax = aabbMax * btVector3(1.f / m_localScaling[0], 1.f / m_localScaling[1], 1.f / m_localScaling[2]);
  212. // account for local origin
  213. localAabbMin += m_localOrigin;
  214. localAabbMax += m_localOrigin;
  215. //quantize the aabbMin and aabbMax, and adjust the start/end ranges
  216. int quantizedAabbMin[3];
  217. int quantizedAabbMax[3];
  218. quantizeWithClamp(quantizedAabbMin, localAabbMin, 0);
  219. quantizeWithClamp(quantizedAabbMax, localAabbMax, 1);
  220. // expand the min/max quantized values
  221. // this is to catch the case where the input aabb falls between grid points!
  222. for (int i = 0; i < 3; ++i) {
  223. quantizedAabbMin[i]--;
  224. quantizedAabbMax[i]++;
  225. }
  226. int startX = 0;
  227. int endX = m_heightStickWidth - 1;
  228. int startJ = 0;
  229. int endJ = m_heightStickLength - 1;
  230. switch (m_upAxis)
  231. {
  232. case 0:
  233. {
  234. if (quantizedAabbMin[1] > startX)
  235. startX = quantizedAabbMin[1];
  236. if (quantizedAabbMax[1] < endX)
  237. endX = quantizedAabbMax[1];
  238. if (quantizedAabbMin[2] > startJ)
  239. startJ = quantizedAabbMin[2];
  240. if (quantizedAabbMax[2] < endJ)
  241. endJ = quantizedAabbMax[2];
  242. break;
  243. }
  244. case 1:
  245. {
  246. if (quantizedAabbMin[0] > startX)
  247. startX = quantizedAabbMin[0];
  248. if (quantizedAabbMax[0] < endX)
  249. endX = quantizedAabbMax[0];
  250. if (quantizedAabbMin[2] > startJ)
  251. startJ = quantizedAabbMin[2];
  252. if (quantizedAabbMax[2] < endJ)
  253. endJ = quantizedAabbMax[2];
  254. break;
  255. };
  256. case 2:
  257. {
  258. if (quantizedAabbMin[0] > startX)
  259. startX = quantizedAabbMin[0];
  260. if (quantizedAabbMax[0] < endX)
  261. endX = quantizedAabbMax[0];
  262. if (quantizedAabbMin[1] > startJ)
  263. startJ = quantizedAabbMin[1];
  264. if (quantizedAabbMax[1] < endJ)
  265. endJ = quantizedAabbMax[1];
  266. break;
  267. }
  268. default:
  269. {
  270. //need to get valid m_upAxis
  271. btAssert(0);
  272. }
  273. }
  274. for (int j = startJ; j < endJ; j++)
  275. {
  276. for (int x = startX; x < endX; x++)
  277. {
  278. U32 index = (m_heightStickLength - (m_heightStickLength - x - 1)) + (j * m_heightStickWidth);
  279. if (mHoles && mHoles[getMax((S32)index - 1, 0)])
  280. continue;
  281. btVector3 vertices[3];
  282. if (m_flipQuadEdges || (m_useDiamondSubdivision && !((j + x) & 1)) || (m_useZigzagSubdivision && !(j & 1)))
  283. {
  284. //first triangle
  285. getVertex(x, j, vertices[0]);
  286. getVertex(x, j + 1, vertices[1]);
  287. getVertex(x + 1, j + 1, vertices[2]);
  288. callback->processTriangle(vertices, x, j);
  289. //second triangle
  290. // getVertex(x,j,vertices[0]);//already got this vertex before, thanks to Danny Chapman
  291. getVertex(x + 1, j + 1, vertices[1]);
  292. getVertex(x + 1, j, vertices[2]);
  293. callback->processTriangle(vertices, x, j);
  294. }
  295. else
  296. {
  297. //first triangle
  298. getVertex(x, j, vertices[0]);
  299. getVertex(x, j + 1, vertices[1]);
  300. getVertex(x + 1, j, vertices[2]);
  301. callback->processTriangle(vertices, x, j);
  302. //second triangle
  303. getVertex(x + 1, j, vertices[0]);
  304. //getVertex(x,j+1,vertices[1]);
  305. getVertex(x + 1, j + 1, vertices[2]);
  306. callback->processTriangle(vertices, x, j);
  307. }
  308. }
  309. }
  310. }