BsFPhysXCollider.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. #include "BsFPhysXCollider.h"
  2. #include "BsPhysX.h"
  3. #include "BsPhysXRigidbody.h"
  4. #include "BsPhysXMaterial.h"
  5. #include "PxScene.h"
  6. #include "PxShape.h"
  7. using namespace physx;
  8. namespace bs
  9. {
  10. FPhysXCollider::FPhysXCollider(PxShape* shape)
  11. :mShape(shape)
  12. {
  13. mStaticBody = gPhysX().getPhysX()->createRigidStatic(PxTransform(PxIdentity));
  14. mStaticBody->attachShape(*mShape);
  15. PxScene* scene = gPhysX().getScene();
  16. scene->addActor(*mStaticBody);
  17. updateFilter();
  18. }
  19. FPhysXCollider::~FPhysXCollider()
  20. {
  21. if (mStaticBody != nullptr)
  22. mStaticBody->release();
  23. mShape->userData = nullptr;
  24. mShape->release();
  25. }
  26. void FPhysXCollider::_setShape(PxShape* shape)
  27. {
  28. if (mShape != nullptr)
  29. {
  30. shape->setLocalPose(mShape->getLocalPose());
  31. shape->setFlags(mShape->getFlags());
  32. shape->setContactOffset(mShape->getContactOffset());
  33. shape->setRestOffset(mShape->getRestOffset());
  34. UINT32 numMaterials = mShape->getNbMaterials();
  35. UINT32 bufferSize = sizeof(PxMaterial*) * numMaterials;
  36. PxMaterial** materials = (PxMaterial**)bs_stack_alloc(bufferSize);
  37. mShape->getMaterials(materials, bufferSize);
  38. shape->setMaterials(materials, numMaterials);
  39. shape->userData = mShape->userData;
  40. bs_stack_free(materials);
  41. PxActor* actor = mShape->getActor();
  42. if (actor != nullptr)
  43. {
  44. PxRigidActor* rigidActor = actor->is<PxRigidActor>();
  45. if (rigidActor != nullptr)
  46. {
  47. rigidActor->detachShape(*mShape);
  48. rigidActor->attachShape(*shape);
  49. }
  50. }
  51. }
  52. mShape = shape;
  53. updateFilter();
  54. }
  55. Vector3 FPhysXCollider::getPosition() const
  56. {
  57. return fromPxVector(mShape->getLocalPose().p);
  58. }
  59. Quaternion FPhysXCollider::getRotation() const
  60. {
  61. return fromPxQuaternion(mShape->getLocalPose().q);
  62. }
  63. void FPhysXCollider::setTransform(const Vector3& pos, const Quaternion& rotation)
  64. {
  65. mShape->setLocalPose(toPxTransform(pos, rotation));
  66. }
  67. void FPhysXCollider::setIsTrigger(bool value)
  68. {
  69. if(value)
  70. {
  71. mShape->setFlag(PxShapeFlag::eSIMULATION_SHAPE, false);
  72. mShape->setFlag(PxShapeFlag::eTRIGGER_SHAPE, true);
  73. mIsTrigger = true;
  74. }
  75. else
  76. {
  77. mShape->setFlag(PxShapeFlag::eTRIGGER_SHAPE, false);
  78. mShape->setFlag(PxShapeFlag::eSIMULATION_SHAPE, true);
  79. mIsTrigger = false;
  80. }
  81. }
  82. bool FPhysXCollider::getIsTrigger() const
  83. {
  84. return (UINT32)(mShape->getFlags() & PxShapeFlag::eTRIGGER_SHAPE) != 0;
  85. }
  86. void FPhysXCollider::setIsStatic(bool value)
  87. {
  88. if (mIsStatic == value)
  89. return;
  90. if (mStaticBody != nullptr)
  91. {
  92. mStaticBody->detachShape(*mShape);
  93. mStaticBody->release();
  94. mStaticBody = nullptr;
  95. }
  96. mIsStatic = value;
  97. if (mIsStatic)
  98. {
  99. mStaticBody = gPhysX().getPhysX()->createRigidStatic(PxTransform(PxIdentity));
  100. mStaticBody->attachShape(*mShape);
  101. PxScene* scene = gPhysX().getScene();
  102. scene->addActor(*mStaticBody);
  103. }
  104. }
  105. bool FPhysXCollider::getIsStatic() const
  106. {
  107. return mIsStatic;
  108. }
  109. void FPhysXCollider::setContactOffset(float value)
  110. {
  111. mShape->setContactOffset(value);
  112. }
  113. float FPhysXCollider::getContactOffset() const
  114. {
  115. return mShape->getContactOffset();
  116. }
  117. void FPhysXCollider::setRestOffset(float value)
  118. {
  119. mShape->setRestOffset(value);
  120. }
  121. float FPhysXCollider::getRestOffset() const
  122. {
  123. return mShape->getRestOffset();
  124. }
  125. void FPhysXCollider::setMaterial(const HPhysicsMaterial& material)
  126. {
  127. FCollider::setMaterial(material);
  128. PhysXMaterial* physXmaterial = nullptr;
  129. if(material.isLoaded())
  130. physXmaterial = static_cast<PhysXMaterial*>(material.get());
  131. PxMaterial* materials[1];
  132. if (physXmaterial != nullptr)
  133. materials[0] = physXmaterial->_getInternal();
  134. else
  135. materials[0] = gPhysX().getDefaultMaterial();
  136. mShape->setMaterials(materials, sizeof(materials) / sizeof(materials[0]));
  137. }
  138. UINT64 FPhysXCollider::getLayer() const
  139. {
  140. return mLayer;
  141. }
  142. void FPhysXCollider::setLayer(UINT64 layer)
  143. {
  144. mLayer = layer;
  145. updateFilter();
  146. }
  147. CollisionReportMode FPhysXCollider::getCollisionReportMode() const
  148. {
  149. return mCollisionReportMode;
  150. }
  151. void FPhysXCollider::setCollisionReportMode(CollisionReportMode mode)
  152. {
  153. mCollisionReportMode = mode;
  154. updateFilter();
  155. }
  156. void FPhysXCollider::_setCCD(bool enabled)
  157. {
  158. mCCD = enabled;
  159. updateFilter();
  160. }
  161. void FPhysXCollider::updateFilter()
  162. {
  163. PxFilterData data;
  164. memcpy(&data.word0, &mLayer, sizeof(mLayer));
  165. PhysXObjectFilterFlags flags;
  166. switch(mCollisionReportMode)
  167. {
  168. case CollisionReportMode::None:
  169. flags |= PhysXObjectFilterFlag::NoReport;
  170. break;
  171. case CollisionReportMode::Report:
  172. flags |= PhysXObjectFilterFlag::ReportBasic;
  173. break;
  174. case CollisionReportMode::ReportPersistent:
  175. flags |= PhysXObjectFilterFlag::ReportAll;
  176. break;
  177. }
  178. if (mCCD)
  179. flags |= PhysXObjectFilterFlag::CCD;
  180. data.word2 = flags;
  181. mShape->setSimulationFilterData(data);
  182. mShape->setQueryFilterData(data);
  183. }
  184. }