btCollisionObject.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. Bullet Continuous Collision Detection and Physics Library
  3. Copyright (c) 2003-2006 Erwin Coumans http://continuousphysics.com/Bullet/
  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 "btCollisionObject.h"
  14. #include "LinearMath/btSerializer.h"
  15. #include "BulletCollision/BroadphaseCollision/btBroadphaseProxy.h"
  16. btCollisionObject::btCollisionObject()
  17. : m_interpolationLinearVelocity(0.f, 0.f, 0.f),
  18. m_interpolationAngularVelocity(0.f, 0.f, 0.f),
  19. m_anisotropicFriction(1.f, 1.f, 1.f),
  20. m_hasAnisotropicFriction(false),
  21. m_contactProcessingThreshold(BT_LARGE_FLOAT),
  22. m_broadphaseHandle(0),
  23. m_collisionShape(0),
  24. m_extensionPointer(0),
  25. m_rootCollisionShape(0),
  26. m_collisionFlags(btCollisionObject::CF_STATIC_OBJECT),
  27. m_islandTag1(-1),
  28. m_companionId(-1),
  29. m_worldArrayIndex(-1),
  30. m_activationState1(1),
  31. m_deactivationTime(btScalar(0.)),
  32. m_friction(btScalar(0.5)),
  33. m_restitution(btScalar(0.)),
  34. m_rollingFriction(0.0f),
  35. m_spinningFriction(0.f),
  36. m_contactDamping(.1),
  37. m_contactStiffness(BT_LARGE_FLOAT),
  38. m_internalType(CO_COLLISION_OBJECT),
  39. m_userObjectPointer(0),
  40. m_userIndex2(-1),
  41. m_userIndex(-1),
  42. m_userIndex3(-1),
  43. m_hitFraction(btScalar(1.)),
  44. m_ccdSweptSphereRadius(btScalar(0.)),
  45. m_ccdMotionThreshold(btScalar(0.)),
  46. m_checkCollideWith(false),
  47. m_updateRevision(0)
  48. {
  49. m_worldTransform.setIdentity();
  50. m_interpolationWorldTransform.setIdentity();
  51. }
  52. btCollisionObject::~btCollisionObject()
  53. {
  54. }
  55. void btCollisionObject::setActivationState(int newState) const
  56. {
  57. if ((m_activationState1 != DISABLE_DEACTIVATION) && (m_activationState1 != DISABLE_SIMULATION))
  58. m_activationState1 = newState;
  59. }
  60. void btCollisionObject::forceActivationState(int newState) const
  61. {
  62. m_activationState1 = newState;
  63. }
  64. void btCollisionObject::activate(bool forceActivation) const
  65. {
  66. if (forceActivation || !(m_collisionFlags & (CF_STATIC_OBJECT | CF_KINEMATIC_OBJECT)))
  67. {
  68. setActivationState(ACTIVE_TAG);
  69. m_deactivationTime = btScalar(0.);
  70. }
  71. }
  72. const char* btCollisionObject::serialize(void* dataBuffer, btSerializer* serializer) const
  73. {
  74. btCollisionObjectData* dataOut = (btCollisionObjectData*)dataBuffer;
  75. m_worldTransform.serialize(dataOut->m_worldTransform);
  76. m_interpolationWorldTransform.serialize(dataOut->m_interpolationWorldTransform);
  77. m_interpolationLinearVelocity.serialize(dataOut->m_interpolationLinearVelocity);
  78. m_interpolationAngularVelocity.serialize(dataOut->m_interpolationAngularVelocity);
  79. m_anisotropicFriction.serialize(dataOut->m_anisotropicFriction);
  80. dataOut->m_hasAnisotropicFriction = m_hasAnisotropicFriction;
  81. dataOut->m_contactProcessingThreshold = m_contactProcessingThreshold;
  82. dataOut->m_broadphaseHandle = 0;
  83. dataOut->m_collisionShape = serializer->getUniquePointer(m_collisionShape);
  84. dataOut->m_rootCollisionShape = 0; //@todo
  85. dataOut->m_collisionFlags = m_collisionFlags;
  86. dataOut->m_islandTag1 = m_islandTag1;
  87. dataOut->m_companionId = m_companionId;
  88. dataOut->m_activationState1 = m_activationState1;
  89. dataOut->m_deactivationTime = m_deactivationTime;
  90. dataOut->m_friction = m_friction;
  91. dataOut->m_rollingFriction = m_rollingFriction;
  92. dataOut->m_contactDamping = m_contactDamping;
  93. dataOut->m_contactStiffness = m_contactStiffness;
  94. dataOut->m_restitution = m_restitution;
  95. dataOut->m_internalType = m_internalType;
  96. char* name = (char*)serializer->findNameForPointer(this);
  97. dataOut->m_name = (char*)serializer->getUniquePointer(name);
  98. if (dataOut->m_name)
  99. {
  100. serializer->serializeName(name);
  101. }
  102. dataOut->m_hitFraction = m_hitFraction;
  103. dataOut->m_ccdSweptSphereRadius = m_ccdSweptSphereRadius;
  104. dataOut->m_ccdMotionThreshold = m_ccdMotionThreshold;
  105. dataOut->m_checkCollideWith = m_checkCollideWith;
  106. if (m_broadphaseHandle)
  107. {
  108. dataOut->m_collisionFilterGroup = m_broadphaseHandle->m_collisionFilterGroup;
  109. dataOut->m_collisionFilterMask = m_broadphaseHandle->m_collisionFilterMask;
  110. dataOut->m_uniqueId = m_broadphaseHandle->m_uniqueId;
  111. }
  112. else
  113. {
  114. dataOut->m_collisionFilterGroup = 0;
  115. dataOut->m_collisionFilterMask = 0;
  116. dataOut->m_uniqueId = -1;
  117. }
  118. return btCollisionObjectDataName;
  119. }
  120. void btCollisionObject::serializeSingleObject(class btSerializer* serializer) const
  121. {
  122. int len = calculateSerializeBufferSize();
  123. btChunk* chunk = serializer->allocate(len, 1);
  124. const char* structType = serialize(chunk->m_oldPtr, serializer);
  125. serializer->finalizeChunk(chunk, structType, BT_COLLISIONOBJECT_CODE, (void*)this);
  126. }