BsCCapsuleCollider.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsCCapsuleCollider.h"
  4. #include "BsSceneObject.h"
  5. #include "BsCRigidbody.h"
  6. #include "BsCCapsuleColliderRTTI.h"
  7. namespace BansheeEngine
  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. SPtr<Collider> collider = CapsuleCollider::create(mRadius, mHalfHeight, SO()->getWorldPosition(),
  61. SO()->getWorldRotation());
  62. collider->_setOwner(PhysicsOwnerType::Component, this);
  63. return collider;
  64. }
  65. RTTITypeBase* CCapsuleCollider::getRTTIStatic()
  66. {
  67. return CCapsuleColliderRTTI::instance();
  68. }
  69. RTTITypeBase* CCapsuleCollider::getRTTI() const
  70. {
  71. return CCapsuleCollider::getRTTIStatic();
  72. }
  73. }