1//************************************ bs::framework - Copyright 2018 Marko Pintera **************************************//
2//*********** Licensed under the MIT license. See LICENSE.md for full terms. This notice is not to be removed. ***********//
3#include "Components/BsCSphereCollider.h"
4#include "Scene/BsSceneObject.h"
5#include "Components/BsCRigidbody.h"
6#include "Private/RTTI/BsCSphereColliderRTTI.h"
7#include "Scene/BsSceneManager.h"
8
9namespace bs
10{
11 CSphereCollider::CSphereCollider()
12 {
13 setName("SphereCollider");
14 }
15
16 CSphereCollider::CSphereCollider(const HSceneObject& parent, float radius)
17 : CCollider(parent), mRadius(radius)
18 {
19 setName("SphereCollider");
20 }
21
22 void CSphereCollider::setRadius(float radius)
23 {
24 float clampedRadius = std::max(radius, 0.01f);
25 if (mRadius == clampedRadius)
26 return;
27
28 mRadius = clampedRadius;
29
30 if (mInternal != nullptr)
31 {
32 _getInternal()->setRadius(clampedRadius);
33
34 if (mParent != nullptr)
35 mParent->_updateMassDistribution();
36 }
37 }
38
39 void CSphereCollider::setCenter(const Vector3& center)
40 {
41 if (mLocalPosition == center)
42 return;
43
44 mLocalPosition = center;
45
46 if (mInternal != nullptr)
47 updateTransform();
48 }
49
50 SPtr<Collider> CSphereCollider::createInternal()
51 {
52 const SPtr<SceneInstance>& scene = SO()->getScene();
53 const Transform& tfrm = SO()->getTransform();
54
55 SPtr<Collider> collider = SphereCollider::create(*scene->getPhysicsScene(), mRadius, tfrm.getPosition(),
56 tfrm.getRotation());
57
58 collider->_setOwner(PhysicsOwnerType::Component, this);
59 return collider;
60 }
61
62 RTTITypeBase* CSphereCollider::getRTTIStatic()
63 {
64 return CSphereColliderRTTI::instance();
65 }
66
67 RTTITypeBase* CSphereCollider::getRTTI() const
68 {
69 return CSphereCollider::getRTTIStatic();
70 }
71}
72