btUniformScalingShape.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 "btUniformScalingShape.h"
  14. btUniformScalingShape::btUniformScalingShape( btConvexShape* convexChildShape,btScalar uniformScalingFactor):
  15. btConvexShape (), m_childConvexShape(convexChildShape),
  16. m_uniformScalingFactor(uniformScalingFactor)
  17. {
  18. m_shapeType = UNIFORM_SCALING_SHAPE_PROXYTYPE;
  19. }
  20. btUniformScalingShape::~btUniformScalingShape()
  21. {
  22. }
  23. btVector3 btUniformScalingShape::localGetSupportingVertexWithoutMargin(const btVector3& vec)const
  24. {
  25. btVector3 tmpVertex;
  26. tmpVertex = m_childConvexShape->localGetSupportingVertexWithoutMargin(vec);
  27. return tmpVertex*m_uniformScalingFactor;
  28. }
  29. void btUniformScalingShape::batchedUnitVectorGetSupportingVertexWithoutMargin(const btVector3* vectors,btVector3* supportVerticesOut,int numVectors) const
  30. {
  31. m_childConvexShape->batchedUnitVectorGetSupportingVertexWithoutMargin(vectors,supportVerticesOut,numVectors);
  32. int i;
  33. for (i=0;i<numVectors;i++)
  34. {
  35. supportVerticesOut[i] = supportVerticesOut[i] * m_uniformScalingFactor;
  36. }
  37. }
  38. btVector3 btUniformScalingShape::localGetSupportingVertex(const btVector3& vec)const
  39. {
  40. btVector3 tmpVertex;
  41. tmpVertex = m_childConvexShape->localGetSupportingVertex(vec);
  42. return tmpVertex*m_uniformScalingFactor;
  43. }
  44. void btUniformScalingShape::calculateLocalInertia(btScalar mass,btVector3& inertia) const
  45. {
  46. ///this linear upscaling is not realistic, but we don't deal with large mass ratios...
  47. btVector3 tmpInertia;
  48. m_childConvexShape->calculateLocalInertia(mass,tmpInertia);
  49. inertia = tmpInertia * m_uniformScalingFactor;
  50. }
  51. ///getAabb's default implementation is brute force, expected derived classes to implement a fast dedicated version
  52. void btUniformScalingShape::getAabb(const btTransform& trans,btVector3& aabbMin,btVector3& aabbMax) const
  53. {
  54. getAabbSlow(trans,aabbMin,aabbMax);
  55. }
  56. void btUniformScalingShape::getAabbSlow(const btTransform& t,btVector3& aabbMin,btVector3& aabbMax) const
  57. {
  58. #if 1
  59. btVector3 _directions[] =
  60. {
  61. btVector3( 1., 0., 0.),
  62. btVector3( 0., 1., 0.),
  63. btVector3( 0., 0., 1.),
  64. btVector3( -1., 0., 0.),
  65. btVector3( 0., -1., 0.),
  66. btVector3( 0., 0., -1.)
  67. };
  68. btVector3 _supporting[] =
  69. {
  70. btVector3( 0., 0., 0.),
  71. btVector3( 0., 0., 0.),
  72. btVector3( 0., 0., 0.),
  73. btVector3( 0., 0., 0.),
  74. btVector3( 0., 0., 0.),
  75. btVector3( 0., 0., 0.)
  76. };
  77. for (int i=0;i<6;i++)
  78. {
  79. _directions[i] = _directions[i]*t.getBasis();
  80. }
  81. batchedUnitVectorGetSupportingVertexWithoutMargin(_directions, _supporting, 6);
  82. btVector3 aabbMin1(0,0,0),aabbMax1(0,0,0);
  83. for ( int i = 0; i < 3; ++i )
  84. {
  85. aabbMax1[i] = t(_supporting[i])[i];
  86. aabbMin1[i] = t(_supporting[i + 3])[i];
  87. }
  88. btVector3 marginVec(getMargin(),getMargin(),getMargin());
  89. aabbMin = aabbMin1-marginVec;
  90. aabbMax = aabbMax1+marginVec;
  91. #else
  92. btScalar margin = getMargin();
  93. for (int i=0;i<3;i++)
  94. {
  95. btVector3 vec(btScalar(0.),btScalar(0.),btScalar(0.));
  96. vec[i] = btScalar(1.);
  97. btVector3 sv = localGetSupportingVertex(vec*t.getBasis());
  98. btVector3 tmp = t(sv);
  99. aabbMax[i] = tmp[i]+margin;
  100. vec[i] = btScalar(-1.);
  101. sv = localGetSupportingVertex(vec*t.getBasis());
  102. tmp = t(sv);
  103. aabbMin[i] = tmp[i]-margin;
  104. }
  105. #endif
  106. }
  107. void btUniformScalingShape::setLocalScaling(const btVector3& scaling)
  108. {
  109. m_childConvexShape->setLocalScaling(scaling);
  110. }
  111. const btVector3& btUniformScalingShape::getLocalScaling() const
  112. {
  113. return m_childConvexShape->getLocalScaling();
  114. }
  115. void btUniformScalingShape::setMargin(btScalar margin)
  116. {
  117. m_childConvexShape->setMargin(margin);
  118. }
  119. btScalar btUniformScalingShape::getMargin() const
  120. {
  121. return m_childConvexShape->getMargin() * m_uniformScalingFactor;
  122. }
  123. int btUniformScalingShape::getNumPreferredPenetrationDirections() const
  124. {
  125. return m_childConvexShape->getNumPreferredPenetrationDirections();
  126. }
  127. void btUniformScalingShape::getPreferredPenetrationDirection(int index, btVector3& penetrationVector) const
  128. {
  129. m_childConvexShape->getPreferredPenetrationDirection(index,penetrationVector);
  130. }