BsFPhysXCollider.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 BansheeEngine
  9. {
  10. FPhysXCollider::FPhysXCollider(PxShape* shape)
  11. :mShape(shape)
  12. {
  13. mStaticBody = gPhysX().getPhysX()->createRigidStatic(PxTransform());
  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->release();
  24. }
  25. Vector3 FPhysXCollider::getPosition() const
  26. {
  27. return fromPxVector(mShape->getLocalPose().p);
  28. }
  29. Quaternion FPhysXCollider::getRotation() const
  30. {
  31. return fromPxQuaternion(mShape->getLocalPose().q);
  32. }
  33. void FPhysXCollider::setTransform(const Vector3& pos, const Quaternion& rotation)
  34. {
  35. mShape->setLocalPose(toPxTransform(pos, rotation));
  36. }
  37. void FPhysXCollider::setIsTrigger(bool value)
  38. {
  39. if(value)
  40. {
  41. mShape->setFlag(PxShapeFlag::eSIMULATION_SHAPE, false);
  42. mShape->setFlag(PxShapeFlag::eTRIGGER_SHAPE, true);
  43. mIsTrigger = true;
  44. }
  45. else
  46. {
  47. mShape->setFlag(PxShapeFlag::eTRIGGER_SHAPE, false);
  48. mShape->setFlag(PxShapeFlag::eSIMULATION_SHAPE, true);
  49. mIsTrigger = false;
  50. }
  51. }
  52. bool FPhysXCollider::getIsTrigger() const
  53. {
  54. return (UINT32)(mShape->getFlags() & PxShapeFlag::eTRIGGER_SHAPE) != 0;
  55. }
  56. void FPhysXCollider::setIsStatic(bool value)
  57. {
  58. if (mIsStatic == value)
  59. return;
  60. if (mStaticBody != nullptr)
  61. {
  62. mStaticBody->detachShape(*mShape);
  63. mStaticBody->release();
  64. mStaticBody = nullptr;
  65. }
  66. mIsStatic = value;
  67. if (mIsStatic)
  68. {
  69. mStaticBody = gPhysX().getPhysX()->createRigidStatic(PxTransform());
  70. mStaticBody->attachShape(*mShape);
  71. PxScene* scene = gPhysX().getScene();
  72. scene->addActor(*mStaticBody);
  73. }
  74. }
  75. bool FPhysXCollider::getIsStatic() const
  76. {
  77. return mIsStatic;
  78. }
  79. void FPhysXCollider::setContactOffset(float value)
  80. {
  81. mShape->setContactOffset(value);
  82. }
  83. float FPhysXCollider::getContactOffset() const
  84. {
  85. return mShape->getContactOffset();
  86. }
  87. void FPhysXCollider::setRestOffset(float value)
  88. {
  89. mShape->setRestOffset(value);
  90. }
  91. float FPhysXCollider::getRestOffset() const
  92. {
  93. return mShape->getRestOffset();
  94. }
  95. void FPhysXCollider::setMaterial(const HPhysicsMaterial& material)
  96. {
  97. FCollider::setMaterial(material);
  98. PhysXMaterial* physXmaterial = nullptr;
  99. if(material.isLoaded())
  100. physXmaterial = static_cast<PhysXMaterial*>(material.get());
  101. PxMaterial* materials[1];
  102. if (physXmaterial != nullptr)
  103. materials[0] = physXmaterial->_getInternal();
  104. else
  105. materials[0] = nullptr;
  106. mShape->setMaterials(materials, sizeof(materials));
  107. }
  108. UINT64 FPhysXCollider::getLayer() const
  109. {
  110. return mLayer;
  111. }
  112. void FPhysXCollider::setLayer(UINT64 layer)
  113. {
  114. mLayer = layer;
  115. updateFilter();
  116. }
  117. CollisionReportMode FPhysXCollider::getCollisionReportMode() const
  118. {
  119. return mCollisionReportMode;
  120. }
  121. void FPhysXCollider::setCollisionReportMode(CollisionReportMode mode)
  122. {
  123. mCollisionReportMode = mode;
  124. updateFilter();
  125. }
  126. void FPhysXCollider::_setCCD(bool enabled)
  127. {
  128. mCCD = enabled;
  129. updateFilter();
  130. }
  131. void FPhysXCollider::updateFilter()
  132. {
  133. PxFilterData data;
  134. memcpy(&data.word0, &mLayer, sizeof(mLayer));
  135. mShape->setSimulationFilterData(data);
  136. mShape->setQueryFilterData(data);
  137. PhysXObjectFilterFlags flags;
  138. switch(mCollisionReportMode)
  139. {
  140. case CollisionReportMode::None:
  141. flags |= PhysXObjectFilterFlag::NoReport;
  142. case CollisionReportMode::Report:
  143. flags |= PhysXObjectFilterFlag::ReportBasic;
  144. case CollisionReportMode::ReportPersistent:
  145. flags |= PhysXObjectFilterFlag::ReportAll;
  146. }
  147. if (mCCD)
  148. flags |= PhysXObjectFilterFlag::CCD;
  149. data.word2 = flags;
  150. }
  151. }