btCollision.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. BtCollision::BtCollision()
  29. : mCompound( NULL ),
  30. mLocalXfm( true )
  31. {
  32. }
  33. BtCollision::~BtCollision()
  34. {
  35. SAFE_DELETE( mCompound );
  36. for ( U32 i=0; i < mShapes.size(); i++ )
  37. delete mShapes[i];
  38. for ( U32 i=0; i < mMeshInterfaces.size(); i++ )
  39. delete mMeshInterfaces[i];
  40. }
  41. btCollisionShape* BtCollision::getShape()
  42. {
  43. if ( mCompound )
  44. return mCompound;
  45. if ( mShapes.empty() )
  46. return NULL;
  47. return mShapes.first();
  48. }
  49. void BtCollision::_addShape( btCollisionShape *shape, const MatrixF &localXfm )
  50. {
  51. AssertFatal( !shape->isCompound(), "BtCollision::_addShape - Shape should not be a compound!" );
  52. // Stick the shape into the array to delete later. Remember
  53. // that the compound shape doesn't delete its children.
  54. mShapes.push_back( shape );
  55. // If this is the first shape then just store the
  56. // local transform and we're done.
  57. if ( mShapes.size() == 1 )
  58. {
  59. mLocalXfm = localXfm;
  60. return;
  61. }
  62. // We use a compound to store the shapes with their
  63. // local transforms... so create it if we haven't already.
  64. if ( !mCompound )
  65. {
  66. mCompound = new btCompoundShape();
  67. // There should only be one shape now... add it and
  68. // clear the local transform.
  69. mCompound->addChildShape( btCast<btTransform>( mLocalXfm ), mShapes.first() );
  70. mLocalXfm = MatrixF::Identity;
  71. }
  72. // Add the new shape to the compound.
  73. mCompound->addChildShape( btCast<btTransform>( localXfm ), shape );
  74. }
  75. void BtCollision::addPlane( const PlaneF &plane )
  76. {
  77. // NOTE: Torque uses a negative D... thats why we flip it here.
  78. btStaticPlaneShape *shape = new btStaticPlaneShape( btVector3( plane.x, plane.y, plane.z ), -plane.d );
  79. _addShape( shape, MatrixF::Identity );
  80. }
  81. void BtCollision::addBox( const Point3F &halfWidth,
  82. const MatrixF &localXfm )
  83. {
  84. btBoxShape *shape = new btBoxShape( btVector3( halfWidth.x, halfWidth.y, halfWidth.z ) );
  85. shape->setMargin( 0.01f );
  86. _addShape( shape, localXfm );
  87. }
  88. void BtCollision::addSphere( const F32 radius,
  89. const MatrixF &localXfm )
  90. {
  91. btSphereShape *shape = new btSphereShape( radius );
  92. shape->setMargin( 0.01f );
  93. _addShape( shape, localXfm );
  94. }
  95. void BtCollision::addCapsule( F32 radius,
  96. F32 height,
  97. const MatrixF &localXfm )
  98. {
  99. btCapsuleShape *shape = new btCapsuleShape( radius, height );
  100. shape->setMargin( 0.01f );
  101. _addShape( shape, localXfm );
  102. }
  103. bool BtCollision::addConvex( const Point3F *points,
  104. U32 count,
  105. const MatrixF &localXfm )
  106. {
  107. btConvexHullShape *shape = new btConvexHullShape( (btScalar*)points, count, sizeof( Point3F ) );
  108. shape->setMargin( 0.01f );
  109. _addShape( shape, localXfm );
  110. return true;
  111. }
  112. bool BtCollision::addTriangleMesh( const Point3F *vert,
  113. U32 vertCount,
  114. const U32 *index,
  115. U32 triCount,
  116. const MatrixF &localXfm )
  117. {
  118. // Setup the interface for loading the triangles.
  119. btTriangleMesh *meshInterface = new btTriangleMesh( true, false );
  120. for ( ; triCount-- ; )
  121. {
  122. meshInterface->addTriangle( btCast<btVector3>( vert[ *( index + 0 ) ] ),
  123. btCast<btVector3>( vert[ *( index + 1 ) ] ),
  124. btCast<btVector3>( vert[ *( index + 2 ) ] ),
  125. false );
  126. index += 3;
  127. }
  128. mMeshInterfaces.push_back( meshInterface );
  129. btBvhTriangleMeshShape *shape = new btBvhTriangleMeshShape( meshInterface, true, true );
  130. shape->setMargin( 0.01f );
  131. _addShape( shape, localXfm );
  132. return true;
  133. }
  134. bool BtCollision::addHeightfield( const U16 *heights,
  135. const bool *holes, // TODO: Bullet height fields do not support holes
  136. U32 blockSize,
  137. F32 metersPerSample,
  138. const MatrixF &localXfm )
  139. {
  140. // We pass the absolute maximum and minimum of a U16 height
  141. // field and not the actual min and max. This helps with
  142. // placement.
  143. const F32 heightScale = 0.03125f;
  144. const F32 minHeight = 0;
  145. const F32 maxHeight = 65535 * heightScale;
  146. btHeightfieldTerrainShape *shape = new btHeightfieldTerrainShape( blockSize, blockSize,
  147. (void*)heights,
  148. heightScale,
  149. minHeight, maxHeight,
  150. 2, // Z up!
  151. PHY_SHORT,
  152. false );
  153. shape->setMargin( 0.01f );
  154. shape->setLocalScaling( btVector3( metersPerSample, metersPerSample, 1.0f ) );
  155. shape->setUseDiamondSubdivision( true );
  156. // The local axis of the heightfield is the exact center of
  157. // its bounds defined as...
  158. //
  159. // ( blockSize * samplesPerMeter, blockSize * samplesPerMeter, maxHeight ) / 2.0f
  160. //
  161. // So we create a local transform to move it to the min point
  162. // of the bounds so it matched Torque terrain.
  163. Point3F offset( (F32)blockSize * metersPerSample / 2.0f,
  164. (F32)blockSize * metersPerSample / 2.0f,
  165. maxHeight / 2.0f );
  166. // And also bump it by half a sample square size.
  167. offset.x -= metersPerSample / 2.0f;
  168. offset.y -= metersPerSample / 2.0f;
  169. MatrixF offsetXfm( true );
  170. offsetXfm.setPosition( offset );
  171. _addShape( shape, offsetXfm );
  172. return true;
  173. }