BsPhysXCapsuleCollider.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #include "BsPhysXCapsuleCollider.h"
  2. #include "BsPhysX.h"
  3. #include "PxPhysics.h"
  4. #include "BsFPhysXCollider.h"
  5. using namespace physx;
  6. namespace bs
  7. {
  8. PhysXCapsuleCollider::PhysXCapsuleCollider(PxPhysics* physx, const Vector3& position, const Quaternion& rotation,
  9. float radius, float halfHeight)
  10. :mRadius(radius), mHalfHeight(halfHeight)
  11. {
  12. PxCapsuleGeometry geometry(radius, halfHeight);
  13. PxShape* shape = physx->createShape(geometry, *gPhysX().getDefaultMaterial(), true);
  14. shape->setLocalPose(toPxTransform(position, rotation));
  15. shape->userData = this;
  16. mInternal = bs_new<FPhysXCollider>(shape);
  17. applyGeometry();
  18. }
  19. PhysXCapsuleCollider::~PhysXCapsuleCollider()
  20. {
  21. bs_delete(mInternal);
  22. }
  23. void PhysXCapsuleCollider::setScale(const Vector3& scale)
  24. {
  25. CapsuleCollider::setScale(scale);
  26. applyGeometry();
  27. }
  28. void PhysXCapsuleCollider::setHalfHeight(float halfHeight)
  29. {
  30. mHalfHeight = halfHeight;
  31. applyGeometry();
  32. }
  33. float PhysXCapsuleCollider::getHalfHeight() const
  34. {
  35. return mHalfHeight;
  36. }
  37. void PhysXCapsuleCollider::setRadius(float radius)
  38. {
  39. mRadius = radius;
  40. applyGeometry();
  41. }
  42. float PhysXCapsuleCollider::getRadius() const
  43. {
  44. return mRadius;
  45. }
  46. void PhysXCapsuleCollider::applyGeometry()
  47. {
  48. PxCapsuleGeometry geometry(std::max(0.01f, mRadius * std::max(mScale.x, mScale.z)),
  49. std::max(0.01f, mHalfHeight * mScale.y));
  50. getInternal()->_getShape()->setGeometry(geometry);
  51. }
  52. FPhysXCollider* PhysXCapsuleCollider::getInternal() const
  53. {
  54. return static_cast<FPhysXCollider*>(mInternal);
  55. }
  56. }