BsFPhysXCollider.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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(physx::PxShape* shape)
  11. :mShape(shape), mStaticBody(nullptr), mIsTrigger(false)
  12. {
  13. mStaticBody = gPhysX().getPhysX()->createRigidStatic(PxTransform());
  14. mStaticBody->attachShape(*mShape);
  15. PxScene* scene = gPhysX().getScene();
  16. scene->addActor(*mStaticBody);
  17. }
  18. FPhysXCollider::~FPhysXCollider()
  19. {
  20. if (mStaticBody != nullptr)
  21. mStaticBody->release();
  22. mShape->release();
  23. }
  24. Vector3 FPhysXCollider::getPosition() const
  25. {
  26. return fromPxVector(mShape->getLocalPose().p);
  27. }
  28. Quaternion FPhysXCollider::getRotation() const
  29. {
  30. return fromPxQuaternion(mShape->getLocalPose().q);
  31. }
  32. void FPhysXCollider::setTransform(const Vector3& pos, const Quaternion& rotation)
  33. {
  34. mShape->setLocalPose(toPxTransform(pos, rotation));
  35. }
  36. void FPhysXCollider::setIsTrigger(bool value)
  37. {
  38. if(value)
  39. {
  40. mShape->setFlag(PxShapeFlag::eSIMULATION_SHAPE, false);
  41. mShape->setFlag(PxShapeFlag::eTRIGGER_SHAPE, true);
  42. mIsTrigger = true;
  43. }
  44. else
  45. {
  46. mShape->setFlag(PxShapeFlag::eTRIGGER_SHAPE, false);
  47. mShape->setFlag(PxShapeFlag::eSIMULATION_SHAPE, true);
  48. mIsTrigger = false;
  49. }
  50. }
  51. bool FPhysXCollider::getIsTrigger() const
  52. {
  53. return (UINT32)(mShape->getFlags() & PxShapeFlag::eTRIGGER_SHAPE) != 0;
  54. }
  55. void FPhysXCollider::setContactOffset(float value)
  56. {
  57. mShape->setContactOffset(value);
  58. }
  59. float FPhysXCollider::getContactOffset()
  60. {
  61. return mShape->getContactOffset();
  62. }
  63. void FPhysXCollider::setRestOffset(float value)
  64. {
  65. mShape->setRestOffset(value);
  66. }
  67. float FPhysXCollider::getRestOffset()
  68. {
  69. return mShape->getRestOffset();
  70. }
  71. void FPhysXCollider::setRigidbody(const SPtr<Rigidbody>& rigidbody)
  72. {
  73. if (mRigidbody == rigidbody)
  74. return;
  75. if(mStaticBody != nullptr)
  76. {
  77. mStaticBody->detachShape(*mShape);
  78. mStaticBody->release();
  79. mStaticBody = nullptr;
  80. }
  81. FCollider::setRigidbody(rigidbody);
  82. if(rigidbody == nullptr)
  83. {
  84. mStaticBody = gPhysX().getPhysX()->createRigidStatic(PxTransform());
  85. mStaticBody->attachShape(*mShape);
  86. PxScene* scene = gPhysX().getScene();
  87. scene->addActor(*mStaticBody);
  88. }
  89. }
  90. void FPhysXCollider::setMaterial(const HPhysicsMaterial& material)
  91. {
  92. FCollider::setMaterial(material);
  93. PhysXMaterial* physXmaterial = nullptr;
  94. if(material.isLoaded())
  95. physXmaterial = static_cast<PhysXMaterial*>(material.get());
  96. PxMaterial* materials[1];
  97. if (physXmaterial != nullptr)
  98. materials[0] = physXmaterial->_getInternal();
  99. else
  100. materials[0] = nullptr;
  101. mShape->setMaterials(materials, sizeof(materials));
  102. }
  103. void FPhysXCollider::setIsActive(bool value)
  104. {
  105. // Note: A different option might be just to fully destroy the shape & actor when disabled (might result in better
  106. // performance if a lot of disabled actors in scene).
  107. if (!value)
  108. mShape->setFlags((PxShapeFlags)0);
  109. else
  110. {
  111. mShape->setFlag(PxShapeFlag::eSCENE_QUERY_SHAPE, true);
  112. setIsTrigger(mIsTrigger);
  113. }
  114. FCollider::setIsActive(value);
  115. }
  116. void FPhysXCollider::setLayer(UINT64 layer)
  117. {
  118. PxFilterData data;
  119. memcpy(&data.word0, &layer, sizeof(layer));
  120. mShape->setSimulationFilterData(data);
  121. }
  122. }