BsPhysXSphereCollider.cpp 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #include "BsPhysXSphereCollider.h"
  2. #include "BsPhysX.h"
  3. #include "PxPhysics.h"
  4. #include "BsFPhysXCollider.h"
  5. using namespace physx;
  6. namespace bs
  7. {
  8. PhysXSphereCollider::PhysXSphereCollider(PxPhysics* physx, const Vector3& position, const Quaternion& rotation,
  9. float radius)
  10. :mRadius(radius)
  11. {
  12. PxSphereGeometry geometry(radius);
  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. PhysXSphereCollider::~PhysXSphereCollider()
  20. {
  21. bs_delete(mInternal);
  22. }
  23. void PhysXSphereCollider::setScale(const Vector3& scale)
  24. {
  25. SphereCollider::setScale(scale);
  26. applyGeometry();
  27. }
  28. void PhysXSphereCollider::setRadius(float radius)
  29. {
  30. mRadius = radius;
  31. applyGeometry();
  32. }
  33. float PhysXSphereCollider::getRadius() const
  34. {
  35. return mRadius;
  36. }
  37. void PhysXSphereCollider::applyGeometry()
  38. {
  39. float radius = std::max(0.01f, mRadius * std::max(std::max(mScale.x, mScale.y), mScale.z));
  40. PxSphereGeometry geometry(radius);
  41. getInternal()->_getShape()->setGeometry(geometry);
  42. }
  43. FPhysXCollider* PhysXSphereCollider::getInternal() const
  44. {
  45. return static_cast<FPhysXCollider*>(mInternal);
  46. }
  47. }