btConvexInternalShape.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 "btConvexInternalShape.h"
  14. btConvexInternalShape::btConvexInternalShape()
  15. : m_localScaling(btScalar(1.),btScalar(1.),btScalar(1.)),
  16. m_collisionMargin(CONVEX_DISTANCE_MARGIN)
  17. {
  18. }
  19. void btConvexInternalShape::setLocalScaling(const btVector3& scaling)
  20. {
  21. m_localScaling = scaling.absolute();
  22. }
  23. void btConvexInternalShape::getAabbSlow(const btTransform& trans,btVector3&minAabb,btVector3&maxAabb) const
  24. {
  25. #ifndef __SPU__
  26. //use localGetSupportingVertexWithoutMargin?
  27. btScalar margin = getMargin();
  28. for (int i=0;i<3;i++)
  29. {
  30. btVector3 vec(btScalar(0.),btScalar(0.),btScalar(0.));
  31. vec[i] = btScalar(1.);
  32. btVector3 sv = localGetSupportingVertex(vec*trans.getBasis());
  33. btVector3 tmp = trans(sv);
  34. maxAabb[i] = tmp[i]+margin;
  35. vec[i] = btScalar(-1.);
  36. tmp = trans(localGetSupportingVertex(vec*trans.getBasis()));
  37. minAabb[i] = tmp[i]-margin;
  38. }
  39. #endif
  40. }
  41. btVector3 btConvexInternalShape::localGetSupportingVertex(const btVector3& vec)const
  42. {
  43. #ifndef __SPU__
  44. btVector3 supVertex = localGetSupportingVertexWithoutMargin(vec);
  45. if ( getMargin()!=btScalar(0.) )
  46. {
  47. btVector3 vecnorm = vec;
  48. if (vecnorm .length2() < (SIMD_EPSILON*SIMD_EPSILON))
  49. {
  50. vecnorm.setValue(btScalar(-1.),btScalar(-1.),btScalar(-1.));
  51. }
  52. vecnorm.normalize();
  53. supVertex+= getMargin() * vecnorm;
  54. }
  55. return supVertex;
  56. #else
  57. btAssert(0);
  58. return btVector3(0,0,0);
  59. #endif //__SPU__
  60. }
  61. btConvexInternalAabbCachingShape::btConvexInternalAabbCachingShape()
  62. : btConvexInternalShape(),
  63. m_localAabbMin(1,1,1),
  64. m_localAabbMax(-1,-1,-1),
  65. m_isLocalAabbValid(false)
  66. {
  67. }
  68. void btConvexInternalAabbCachingShape::getAabb(const btTransform& trans,btVector3& aabbMin,btVector3& aabbMax) const
  69. {
  70. getNonvirtualAabb(trans,aabbMin,aabbMax,getMargin());
  71. }
  72. void btConvexInternalAabbCachingShape::setLocalScaling(const btVector3& scaling)
  73. {
  74. btConvexInternalShape::setLocalScaling(scaling);
  75. recalcLocalAabb();
  76. }
  77. void btConvexInternalAabbCachingShape::recalcLocalAabb()
  78. {
  79. m_isLocalAabbValid = true;
  80. #if 1
  81. static const btVector3 _directions[] =
  82. {
  83. btVector3( 1., 0., 0.),
  84. btVector3( 0., 1., 0.),
  85. btVector3( 0., 0., 1.),
  86. btVector3( -1., 0., 0.),
  87. btVector3( 0., -1., 0.),
  88. btVector3( 0., 0., -1.)
  89. };
  90. btVector3 _supporting[] =
  91. {
  92. btVector3( 0., 0., 0.),
  93. btVector3( 0., 0., 0.),
  94. btVector3( 0., 0., 0.),
  95. btVector3( 0., 0., 0.),
  96. btVector3( 0., 0., 0.),
  97. btVector3( 0., 0., 0.)
  98. };
  99. batchedUnitVectorGetSupportingVertexWithoutMargin(_directions, _supporting, 6);
  100. for ( int i = 0; i < 3; ++i )
  101. {
  102. m_localAabbMax[i] = _supporting[i][i] + m_collisionMargin;
  103. m_localAabbMin[i] = _supporting[i + 3][i] - m_collisionMargin;
  104. }
  105. #else
  106. for (int i=0;i<3;i++)
  107. {
  108. btVector3 vec(btScalar(0.),btScalar(0.),btScalar(0.));
  109. vec[i] = btScalar(1.);
  110. btVector3 tmp = localGetSupportingVertex(vec);
  111. m_localAabbMax[i] = tmp[i]+m_collisionMargin;
  112. vec[i] = btScalar(-1.);
  113. tmp = localGetSupportingVertex(vec);
  114. m_localAabbMin[i] = tmp[i]-m_collisionMargin;
  115. }
  116. #endif
  117. }