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/BsCPlaneCollider.h"
4#include "Scene/BsSceneObject.h"
5#include "Components/BsCRigidbody.h"
6#include "Private/RTTI/BsCPlaneColliderRTTI.h"
7#include "Scene/BsSceneManager.h"
8
9namespace bs
10{
11 CPlaneCollider::CPlaneCollider()
12 {
13 setName("PlaneCollider");
14
15 mLocalRotation = Quaternion::getRotationFromTo(Vector3::UNIT_X, mNormal);
16 }
17
18 CPlaneCollider::CPlaneCollider(const HSceneObject& parent)
19 : CCollider(parent)
20 {
21 setName("PlaneCollider");
22
23 mLocalRotation = Quaternion::getRotationFromTo(Vector3::UNIT_X, mNormal);
24 }
25
26 void CPlaneCollider::setNormal(const Vector3& normal)
27 {
28 if (mNormal == normal)
29 return;
30
31 mNormal = normal;
32 mNormal.normalize();
33
34 mLocalRotation = Quaternion::getRotationFromTo(Vector3::UNIT_X, normal);
35 mLocalPosition = mNormal * mDistance;
36
37 if(mInternal != nullptr)
38 updateTransform();
39 }
40
41 void CPlaneCollider::setDistance(float distance)
42 {
43 if (mDistance == distance)
44 return;
45
46 mDistance = distance;
47 mLocalPosition = mNormal * distance;
48
49 if (mInternal != nullptr)
50 updateTransform();
51 }
52
53 SPtr<Collider> CPlaneCollider::createInternal()
54 {
55 const SPtr<SceneInstance>& scene = SO()->getScene();
56 const Transform& tfrm = SO()->getTransform();
57
58 SPtr<Collider> collider = PlaneCollider::create(*scene->getPhysicsScene(), tfrm.getPosition(), tfrm.getRotation());
59
60 collider->_setOwner(PhysicsOwnerType::Component, this);
61 return collider;
62 }
63
64 bool CPlaneCollider::isValidParent(const HRigidbody& parent) const
65 {
66 // Planes cannot be added to non-kinematic rigidbodies
67 return parent->getIsKinematic();
68 }
69
70 RTTITypeBase* CPlaneCollider::getRTTIStatic()
71 {
72 return CPlaneColliderRTTI::instance();
73 }
74
75 RTTITypeBase* CPlaneCollider::getRTTI() const
76 {
77 return CPlaneCollider::getRTTIStatic();
78 }
79}
80