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/BsCMeshCollider.h"
4#include "Scene/BsSceneObject.h"
5#include "Components/BsCRigidbody.h"
6#include "Physics/BsPhysicsMesh.h"
7#include "Private/RTTI/BsCMeshColliderRTTI.h"
8#include "Scene/BsSceneManager.h"
9
10namespace bs
11{
12 CMeshCollider::CMeshCollider()
13 {
14 setName("MeshCollider");
15 }
16
17 CMeshCollider::CMeshCollider(const HSceneObject& parent)
18 : CCollider(parent)
19 {
20 setName("MeshCollider");
21 }
22
23 void CMeshCollider::setMesh(const HPhysicsMesh& mesh)
24 {
25 if (mMesh == mesh)
26 return;
27
28 if(getIsTrigger() && mesh->getType() == PhysicsMeshType::Triangle)
29 {
30 LOGWRN("Triangle meshes are not supported on Trigger colliders.");
31 return;
32 }
33
34 mMesh = mesh;
35
36 if (mInternal != nullptr)
37 {
38 _getInternal()->setMesh(mesh);
39
40 if (mParent != nullptr)
41 {
42 // If triangle mesh its possible the parent can no longer use this collider (they're not supported for
43 // non-kinematic rigidbodies)
44 if (mMesh.isLoaded() && mMesh->getType() == PhysicsMeshType::Triangle)
45 updateParentRigidbody();
46 else
47 mParent->_updateMassDistribution();
48 }
49 }
50 }
51
52 SPtr<Collider> CMeshCollider::createInternal()
53 {
54 const SPtr<SceneInstance>& scene = SO()->getScene();
55 const Transform& tfrm = SO()->getTransform();
56
57 SPtr<MeshCollider> collider = MeshCollider::create(*scene->getPhysicsScene(), tfrm.getPosition(),
58 tfrm.getRotation());
59 collider->setMesh(mMesh);
60 collider->_setOwner(PhysicsOwnerType::Component, this);
61
62 return collider;
63 }
64
65 bool CMeshCollider::isValidParent(const HRigidbody& parent) const
66 {
67 // Triangle mesh colliders cannot be used for non-kinematic rigidbodies
68 return !mMesh.isLoaded() || mMesh->getType() == PhysicsMeshType::Convex || parent->getIsKinematic();
69 }
70
71 RTTITypeBase* CMeshCollider::getRTTIStatic()
72 {
73 return CMeshColliderRTTI::instance();
74 }
75
76 RTTITypeBase* CMeshCollider::getRTTI() const
77 {
78 return CMeshCollider::getRTTIStatic();
79 }
80}
81