BsCCapsuleCollider.cpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "Components/BsCCapsuleCollider.h"
  4. #include "Scene/BsSceneObject.h"
  5. #include "Components/BsCRigidbody.h"
  6. #include "RTTI/BsCCapsuleColliderRTTI.h"
  7. namespace bs
  8. {
  9. CCapsuleCollider::CCapsuleCollider(const HSceneObject& parent, float radius, float halfHeight)
  10. : CCollider(parent), mRadius(radius), mHalfHeight(halfHeight)
  11. {
  12. setName("CapsuleCollider");
  13. }
  14. void CCapsuleCollider::setNormal(const Vector3& normal)
  15. {
  16. if (mNormal == normal)
  17. return;
  18. mNormal = normal;
  19. mNormal.normalize();
  20. mLocalRotation = Quaternion::getRotationFromTo(Vector3::UNIT_X, normal);
  21. if (mInternal != nullptr)
  22. updateTransform();
  23. }
  24. void CCapsuleCollider::setCenter(const Vector3& center)
  25. {
  26. if (mLocalPosition == center)
  27. return;
  28. mLocalPosition = center;
  29. if (mInternal != nullptr)
  30. updateTransform();
  31. }
  32. void CCapsuleCollider::setHalfHeight(float halfHeight)
  33. {
  34. float clampedHalfHeight = std::max(halfHeight, 0.01f);
  35. if (mHalfHeight == clampedHalfHeight)
  36. return;
  37. mHalfHeight = clampedHalfHeight;
  38. if (mInternal != nullptr)
  39. {
  40. _getInternal()->setHalfHeight(clampedHalfHeight);
  41. if (mParent != nullptr)
  42. mParent->_updateMassDistribution();
  43. }
  44. }
  45. void CCapsuleCollider::setRadius(float radius)
  46. {
  47. float clampedRadius = std::max(radius, 0.01f);
  48. if (mRadius == clampedRadius)
  49. return;
  50. mRadius = clampedRadius;
  51. if (mInternal != nullptr)
  52. {
  53. _getInternal()->setRadius(clampedRadius);
  54. if (mParent != nullptr)
  55. mParent->_updateMassDistribution();
  56. }
  57. }
  58. SPtr<Collider> CCapsuleCollider::createInternal()
  59. {
  60. const Transform& tfrm = SO()->getTransform();
  61. SPtr<Collider> collider = CapsuleCollider::create(mRadius, mHalfHeight, tfrm.getPosition(),
  62. tfrm.getRotation());
  63. collider->_setOwner(PhysicsOwnerType::Component, this);
  64. return collider;
  65. }
  66. RTTITypeBase* CCapsuleCollider::getRTTIStatic()
  67. {
  68. return CCapsuleColliderRTTI::instance();
  69. }
  70. RTTITypeBase* CCapsuleCollider::getRTTI() const
  71. {
  72. return CCapsuleCollider::getRTTIStatic();
  73. }
  74. }