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