BsPhysXMeshCollider.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #include "BsPhysXMeshCollider.h"
  2. #include "BsPhysX.h"
  3. #include "PxPhysics.h"
  4. #include "BsFPhysXCollider.h"
  5. #include "BsPhysXMesh.h"
  6. using namespace physx;
  7. namespace bs
  8. {
  9. PhysXMeshCollider::PhysXMeshCollider(PxPhysics* physx, const Vector3& position, const Quaternion& rotation)
  10. {
  11. PxSphereGeometry geometry(0.01f); // Dummy
  12. PxShape* shape = physx->createShape(geometry, *gPhysX().getDefaultMaterial(), true);
  13. shape->setLocalPose(toPxTransform(position, rotation));
  14. shape->userData = this;
  15. mInternal = bs_new<FPhysXCollider>(shape);
  16. }
  17. PhysXMeshCollider::~PhysXMeshCollider()
  18. {
  19. bs_delete(mInternal);
  20. }
  21. void PhysXMeshCollider::setScale(const Vector3& scale)
  22. {
  23. MeshCollider::setScale(scale);
  24. applyGeometry();
  25. }
  26. void PhysXMeshCollider::onMeshChanged()
  27. {
  28. applyGeometry();
  29. }
  30. void PhysXMeshCollider::applyGeometry()
  31. {
  32. if (!mMesh.isLoaded())
  33. {
  34. setGeometry(PxSphereGeometry(0.01f)); // Dummy
  35. return;
  36. }
  37. FPhysXMesh* physxMesh = static_cast<FPhysXMesh*>(mMesh->_getInternal());
  38. if (mMesh->getType() == PhysicsMeshType::Convex)
  39. {
  40. PxConvexMeshGeometry geometry;
  41. geometry.scale = PxMeshScale(toPxVector(getScale()), PxIdentity);
  42. geometry.convexMesh = physxMesh->_getConvex();
  43. setGeometry(geometry);
  44. }
  45. else // Triangle
  46. {
  47. PxTriangleMeshGeometry geometry;
  48. geometry.scale = PxMeshScale(toPxVector(getScale()), PxIdentity);
  49. geometry.triangleMesh = physxMesh->_getTriangle();
  50. setGeometry(geometry);
  51. }
  52. }
  53. void PhysXMeshCollider::setGeometry(const PxGeometry& geometry)
  54. {
  55. PxShape* shape = getInternal()->_getShape();
  56. if (shape->getGeometryType() != geometry.getType())
  57. {
  58. PxShape* newShape = gPhysX().getPhysX()->createShape(geometry, *gPhysX().getDefaultMaterial(), true);
  59. getInternal()->_setShape(newShape);
  60. }
  61. else
  62. getInternal()->_getShape()->setGeometry(geometry);
  63. }
  64. FPhysXCollider* PhysXMeshCollider::getInternal() const
  65. {
  66. return static_cast<FPhysXCollider*>(mInternal);
  67. }
  68. }