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#pragma once
4
5#include <cfloat>
6
7#include "BsCorePrerequisites.h"
8#include "Physics/BsPhysicsCommon.h"
9#include "Math/BsVector3.h"
10#include "Math/BsQuaternion.h"
11
12namespace bs
13{
14 /** @addtogroup Physics
15 * @{
16 */
17
18 /**
19 * Collider represents physics geometry that can be in multiple states:
20 * - Default: Static geometry that physics objects can collide with.
21 * - Trigger: Static geometry that can't be collided with but will report touch events.
22 * - Dynamic: Dynamic geometry that is a part of a Rigidbody. A set of colliders defines the shape of the parent
23 * rigidbody.
24 */
25 class BS_CORE_EXPORT Collider
26 {
27 public:
28 Collider() = default;
29 virtual ~Collider() = default;
30
31 /** @copydoc FCollider::getPosition */
32 Vector3 getPosition() const;
33
34 /** @copydoc FCollider::getRotation */
35 Quaternion getRotation() const;
36
37 /** @copydoc FCollider::setTransform */
38 void setTransform(const Vector3& pos, const Quaternion& rot);
39
40 /** Sets the scale of the collider geometry. */
41 virtual void setScale(const Vector3& scale);
42
43 /** Retrieves the scale of the collider geometry. */
44 Vector3 getScale() const;
45
46 /** @copydoc FCollider::setIsTrigger */
47 void setIsTrigger(bool value);
48
49 /** @copydoc FCollider::getIsTrigger */
50 bool getIsTrigger() const;
51
52 /** Determines the Rigidbody that controls this collider (if any). */
53 void setRigidbody(Rigidbody* value);
54
55 /** @copydoc Collider::setRigidbody() */
56 Rigidbody* getRigidbody() const { return mRigidbody; }
57
58 /** @copydoc FCollider::setMass */
59 void setMass(float mass);
60
61 /** @copydoc FCollider::getMass */
62 float getMass() const;
63
64 /** @copydoc FCollider::setMaterial */
65 void setMaterial(const HPhysicsMaterial& material);
66
67 /** @copydoc FCollider::getMaterial */
68 HPhysicsMaterial getMaterial() const;
69
70 /** @copydoc FCollider::setContactOffset */
71 void setContactOffset(float value);
72
73 /** @copydoc FCollider::getContactOffset */
74 float getContactOffset();
75
76 /** @copydoc FCollider::setRestOffset */
77 void setRestOffset(float value);
78
79 /** @copydoc FCollider::getRestOffset */
80 float getRestOffset();
81
82 /** @copydoc FCollider::setLayer */
83 void setLayer(UINT64 layer);
84
85 /** @copydoc FCollider::getLayer */
86 UINT64 getLayer() const;
87
88 /** @copydoc FCollider::setCollisionReportMode */
89 void setCollisionReportMode(CollisionReportMode mode);
90
91 /** @copydoc FCollider::getCollisionReportMode */
92 CollisionReportMode getCollisionReportMode() const;
93
94 /**
95 * Checks does the ray hit this collider.
96 *
97 * @param[in] ray Ray to check.
98 * @param[out] hit Information about the hit. Valid only if the method returns true.
99 * @param[in] maxDist Maximum distance from the ray origin to search for hits.
100 * @return True if the ray has hit the collider.
101 */
102 bool rayCast(const Ray& ray, PhysicsQueryHit& hit, float maxDist = FLT_MAX) const;
103
104 /**
105 * Checks does the ray hit this collider.
106 *
107 * @param[in] origin Origin of the ray to check.
108 * @param[in] unitDir Unit direction of the ray to check.
109 * @param[out] hit Information about the hit. Valid only if the method returns true.
110 * @param[in] maxDist Maximum distance from the ray origin to search for hits.
111 * @return True if the ray has hit the collider.
112 */
113 bool rayCast(const Vector3& origin, const Vector3& unitDir, PhysicsQueryHit& hit,
114 float maxDist = FLT_MAX) const;
115
116 /**
117 * Triggered when some object starts interacting with the collider. Only triggered if proper collision report mode
118 * is turned on.
119 */
120 Event<void(const CollisionDataRaw&)> onCollisionBegin;
121 /**
122 * Triggered for every frame that an object remains interacting with a collider. Only triggered if proper collision
123 * report mode is turned on.
124 */
125 Event<void(const CollisionDataRaw&)> onCollisionStay;
126 /**
127 * Triggered when some object stops interacting with the collider. Only triggered if proper collision report mode
128 * is turned on.
129 */
130 Event<void(const CollisionDataRaw&)> onCollisionEnd;
131
132 /** @name Internal
133 * @{
134 */
135
136 /** Returns the object containing common collider code. */
137 FCollider* _getInternal() const { return mInternal; }
138
139 /**
140 * Sets the object that owns this physics object, if any. Used for high level systems so they can easily map their
141 * high level physics objects from the low level ones returned by various queries and events.
142 */
143 void _setOwner(PhysicsOwnerType type, void* owner) { mOwner.type = type; mOwner.ownerData = owner; }
144
145 /**
146 * Gets the object that owns this physics object, if any. Used for high level systems so they can easily map their
147 * high level physics objects from the low level ones returned by various queries and events.
148 */
149 void* _getOwner(PhysicsOwnerType type) const { return mOwner.type == type ? mOwner.ownerData : nullptr; }
150
151 /** @} */
152 protected:
153 FCollider* mInternal = nullptr;
154 PhysicsObjectOwner mOwner;
155 Rigidbody* mRigidbody = nullptr;
156 Vector3 mScale = Vector3::ONE;
157 };
158
159 /** @} */
160}
161