BsFPhysXCollider.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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)
  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. void FPhysXCollider::setMass(float mass)
  60. {
  61. FCollider::setMass(mass);
  62. if (mRigidbody != nullptr)
  63. mRigidbody->_updateMassDistribution(); // Note: Perhaps I can just mark mass distribution as dirty and recalculate it delayed, in case a lot of colliders change
  64. }
  65. float FPhysXCollider::getContactOffset() const
  66. {
  67. return mShape->getContactOffset();
  68. }
  69. void FPhysXCollider::setRestOffset(float value)
  70. {
  71. mShape->setRestOffset(value);
  72. }
  73. float FPhysXCollider::getRestOffset() const
  74. {
  75. return mShape->getRestOffset();
  76. }
  77. void FPhysXCollider::setRigidbody(const SPtr<Rigidbody>& rigidbody)
  78. {
  79. if (mRigidbody == rigidbody)
  80. return;
  81. if(mStaticBody != nullptr)
  82. {
  83. mStaticBody->detachShape(*mShape);
  84. mStaticBody->release();
  85. mStaticBody = nullptr;
  86. }
  87. FCollider::setRigidbody(rigidbody);
  88. if(rigidbody == nullptr)
  89. {
  90. mStaticBody = gPhysX().getPhysX()->createRigidStatic(PxTransform());
  91. mStaticBody->attachShape(*mShape);
  92. PxScene* scene = gPhysX().getScene();
  93. scene->addActor(*mStaticBody);
  94. }
  95. }
  96. void FPhysXCollider::setMaterial(const HPhysicsMaterial& material)
  97. {
  98. FCollider::setMaterial(material);
  99. PhysXMaterial* physXmaterial = nullptr;
  100. if(material.isLoaded())
  101. physXmaterial = static_cast<PhysXMaterial*>(material.get());
  102. PxMaterial* materials[1];
  103. if (physXmaterial != nullptr)
  104. materials[0] = physXmaterial->_getInternal();
  105. else
  106. materials[0] = nullptr;
  107. mShape->setMaterials(materials, sizeof(materials));
  108. }
  109. void FPhysXCollider::setLayer(UINT64 layer)
  110. {
  111. PxFilterData data;
  112. memcpy(&data.word0, &layer, sizeof(layer));
  113. mShape->setSimulationFilterData(data);
  114. }
  115. }