BsFPhysXCollider.cpp 3.2 KB

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