btCollisionShape.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 "bullet/BulletCollision//CollisionShapes/btCollisionShape.h"
  14. #include "bullet/LinearMath/btSerializer.h"
  15. /*
  16. Make sure this dummy function never changes so that it
  17. can be used by probes that are checking whether the
  18. library is actually installed.
  19. */
  20. extern "C"
  21. {
  22. void btBulletCollisionProbe ();
  23. void btBulletCollisionProbe () {}
  24. }
  25. void btCollisionShape::getBoundingSphere(btVector3& center,btScalar& radius) const
  26. {
  27. btTransform tr;
  28. tr.setIdentity();
  29. btVector3 aabbMin,aabbMax;
  30. getAabb(tr,aabbMin,aabbMax);
  31. radius = (aabbMax-aabbMin).length()*btScalar(0.5);
  32. center = (aabbMin+aabbMax)*btScalar(0.5);
  33. }
  34. btScalar btCollisionShape::getContactBreakingThreshold(btScalar defaultContactThreshold) const
  35. {
  36. return getAngularMotionDisc() * defaultContactThreshold;
  37. }
  38. btScalar btCollisionShape::getAngularMotionDisc() const
  39. {
  40. ///@todo cache this value, to improve performance
  41. btVector3 center;
  42. btScalar disc;
  43. getBoundingSphere(center,disc);
  44. disc += (center).length();
  45. return disc;
  46. }
  47. void btCollisionShape::calculateTemporalAabb(const btTransform& curTrans,const btVector3& linvel,const btVector3& angvel,btScalar timeStep, btVector3& temporalAabbMin,btVector3& temporalAabbMax) const
  48. {
  49. //start with static aabb
  50. getAabb(curTrans,temporalAabbMin,temporalAabbMax);
  51. btScalar temporalAabbMaxx = temporalAabbMax.getX();
  52. btScalar temporalAabbMaxy = temporalAabbMax.getY();
  53. btScalar temporalAabbMaxz = temporalAabbMax.getZ();
  54. btScalar temporalAabbMinx = temporalAabbMin.getX();
  55. btScalar temporalAabbMiny = temporalAabbMin.getY();
  56. btScalar temporalAabbMinz = temporalAabbMin.getZ();
  57. // add linear motion
  58. btVector3 linMotion = linvel*timeStep;
  59. ///@todo: simd would have a vector max/min operation, instead of per-element access
  60. if (linMotion.x() > btScalar(0.))
  61. temporalAabbMaxx += linMotion.x();
  62. else
  63. temporalAabbMinx += linMotion.x();
  64. if (linMotion.y() > btScalar(0.))
  65. temporalAabbMaxy += linMotion.y();
  66. else
  67. temporalAabbMiny += linMotion.y();
  68. if (linMotion.z() > btScalar(0.))
  69. temporalAabbMaxz += linMotion.z();
  70. else
  71. temporalAabbMinz += linMotion.z();
  72. //add conservative angular motion
  73. btScalar angularMotion = angvel.length() * getAngularMotionDisc() * timeStep;
  74. btVector3 angularMotion3d(angularMotion,angularMotion,angularMotion);
  75. temporalAabbMin = btVector3(temporalAabbMinx,temporalAabbMiny,temporalAabbMinz);
  76. temporalAabbMax = btVector3(temporalAabbMaxx,temporalAabbMaxy,temporalAabbMaxz);
  77. temporalAabbMin -= angularMotion3d;
  78. temporalAabbMax += angularMotion3d;
  79. }
  80. ///fills the dataBuffer and returns the struct name (and 0 on failure)
  81. const char* btCollisionShape::serialize(void* dataBuffer, btSerializer* serializer) const
  82. {
  83. btCollisionShapeData* shapeData = (btCollisionShapeData*) dataBuffer;
  84. char* name = (char*) serializer->findNameForPointer(this);
  85. shapeData->m_name = (char*)serializer->getUniquePointer(name);
  86. if (shapeData->m_name)
  87. {
  88. serializer->serializeName(name);
  89. }
  90. shapeData->m_shapeType = m_shapeType;
  91. //shapeData->m_padding//??
  92. return "btCollisionShapeData";
  93. }
  94. void btCollisionShape::serializeSingleShape(btSerializer* serializer) const
  95. {
  96. int len = calculateSerializeBufferSize();
  97. btChunk* chunk = serializer->allocate(len,1);
  98. const char* structType = serialize(chunk->m_oldPtr, serializer);
  99. serializer->finalizeChunk(chunk,structType,BT_SHAPE_CODE,(void*)this);
  100. }