BsPhysXMeshCollider.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 BansheeEngine
  8. {
  9. PhysXMeshCollider::PhysXMeshCollider(PxPhysics* physx, const Vector3& position, const Quaternion& rotation)
  10. {
  11. PxConvexMeshGeometry geometry;
  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. PxConvexMeshGeometry geometry;
  35. getInternal()->_getShape()->setGeometry(geometry);
  36. return;
  37. }
  38. SPtr<PhysXMesh> physxMesh = std::static_pointer_cast<PhysXMesh>(mMesh.getInternalPtr());
  39. if (physxMesh->getType() == PhysicsMeshType::Convex)
  40. {
  41. PxConvexMeshGeometry geometry;
  42. geometry.scale = PxMeshScale(toPxVector(getScale()), PxIdentity);
  43. geometry.convexMesh = physxMesh->_getConvex();
  44. getInternal()->_getShape()->setGeometry(geometry);
  45. }
  46. else // Triangle
  47. {
  48. PxTriangleMeshGeometry geometry;
  49. geometry.scale = PxMeshScale(toPxVector(getScale()), PxIdentity);
  50. geometry.triangleMesh = physxMesh->_getTriangle();
  51. getInternal()->_getShape()->setGeometry(geometry);
  52. }
  53. }
  54. FPhysXCollider* PhysXMeshCollider::getInternal() const
  55. {
  56. return static_cast<FPhysXCollider*>(mInternal);
  57. }
  58. }