-
-
Notifications
You must be signed in to change notification settings - Fork 260
Expand file tree
/
Copy pathCollider.cpp
More file actions
323 lines (263 loc) · 13 KB
/
Copy pathCollider.cpp
File metadata and controls
323 lines (263 loc) · 13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/********************************************************************************
* ReactPhysics3D physics library, http://www.reactphysics3d.com *
* Copyright (c) 2010-2024 Daniel Chappuis *
*********************************************************************************
* *
* This software is provided 'as-is', without any express or implied warranty. *
* In no event will the authors be held liable for any damages arising from the *
* use of this software. *
* *
* Permission is granted to anyone to use this software for any purpose, *
* including commercial applications, and to alter it and redistribute it *
* freely, subject to the following restrictions: *
* *
* 1. The origin of this software must not be misrepresented; you must not claim *
* that you wrote the original software. If you use this software in a *
* product, an acknowledgment in the product documentation would be *
* appreciated but is not required. *
* *
* 2. Altered source versions must be plainly marked as such, and must not be *
* misrepresented as being the original software. *
* *
* 3. This notice may not be removed or altered from any source distribution. *
* *
********************************************************************************/
// Libraries
#include <reactphysics3d/collision/Collider.h>
#include <reactphysics3d/utils/Logger.h>
#include <reactphysics3d/collision/RaycastInfo.h>
#include <reactphysics3d/memory/MemoryManager.h>
#include <reactphysics3d/engine/PhysicsWorld.h>
#include <reactphysics3d/engine/PhysicsCommon.h>
using namespace reactphysics3d;
// Constructor
/**
* @param entity Entity of the collider
* @param body Pointer to the body
* @param memoryManager Reference to the memory manager
*/
Collider::Collider(Entity entity, Body* body, MemoryManager& memoryManager)
:mMemoryManager(memoryManager), mEntity(entity), mBody(body),
mUserData(nullptr) {
}
// Destructor
Collider::~Collider() {
}
// Return true if a point is inside the collision shape
/**
* @param worldPoint Point to test in world-space coordinates
* @return True if the point is inside the collision shape
*/
bool Collider::testPointInside(const Vector3& worldPoint) {
const Transform localToWorld = mBody->mWorld.mTransformComponents.getTransform(mBody->getEntity()) *
mBody->mWorld.mCollidersComponents.getLocalToBodyTransform(mEntity);
const Vector3 localPoint = localToWorld.getInverse() * worldPoint;
const CollisionShape* collisionShape = mBody->mWorld.mCollidersComponents.getCollisionShape(mEntity);
return collisionShape->testPointInside(localPoint, this);
}
// Set the collision category bits
/**
* @param collisionCategoryBits The collision category bits mask of the collider
*/
void Collider::setCollisionCategoryBits(unsigned short collisionCategoryBits) {
mBody->mWorld.mCollidersComponents.setCollisionCategoryBits(mEntity, collisionCategoryBits);
int broadPhaseId = mBody->mWorld.mCollidersComponents.getBroadPhaseId(mEntity);
// Ask the broad-phase collision detection to test this collider next frame
mBody->mWorld.mCollisionDetection.askForBroadPhaseCollisionCheck(this);
RP3D_LOG(mBody->mWorld.mConfig.worldName, Logger::Level::Information, Logger::Category::Collider,
"Collider " + std::to_string(broadPhaseId) + ": Set collisionCategoryBits=" +
std::to_string(collisionCategoryBits), __FILE__, __LINE__);
}
// Set the collision bits mask
/**
* @param collideWithMaskBits The bits mask that specifies with which collision category this shape will collide
*/
void Collider::setCollideWithMaskBits(unsigned short collideWithMaskBits) {
mBody->mWorld.mCollidersComponents.setCollideWithMaskBits(mEntity, collideWithMaskBits);
int broadPhaseId = mBody->mWorld.mCollidersComponents.getBroadPhaseId(mEntity);
// Ask the broad-phase collision detection to test this collider next frame
mBody->mWorld.mCollisionDetection.askForBroadPhaseCollisionCheck(this);
RP3D_LOG(mBody->mWorld.mConfig.worldName, Logger::Level::Information, Logger::Category::Collider,
"Collider" + std::to_string(broadPhaseId) + ": Set collideWithMaskBits=" +
std::to_string(collideWithMaskBits), __FILE__, __LINE__);
}
// Set the local to body transform
/**
* @param transform The transform from local-space of the collider into the local-space of the body
*/
void Collider::setLocalToBodyTransform(const Transform& transform) {
mBody->mWorld.mCollidersComponents.setLocalToBodyTransform(mEntity, transform);
// Update the local-to-world transform
const Transform& bodyTransform = mBody->mWorld.mTransformComponents.getTransform(mBody->getEntity());
mBody->mWorld.mCollidersComponents.setLocalToWorldTransform(mEntity, bodyTransform * transform);
RigidBody* rigidBody = dynamic_cast<RigidBody*>(mBody);
if (rigidBody != nullptr) {
rigidBody->setIsSleeping(false);
}
mBody->mWorld.mCollisionDetection.updateCollider(mEntity);
RP3D_LOG(mBody->mWorld.mConfig.worldName, Logger::Level::Information, Logger::Category::Collider,
"Collider " + std::to_string(getBroadPhaseId()) + ": Set localToBodyTransform=" +
transform.to_string(), __FILE__, __LINE__);
}
// Return the AABB of the collider in world-space
/**
* @return The AABB of the collider in world-space
*/
const AABB Collider::getWorldAABB() const {
CollisionShape* collisionShape = mBody->mWorld.mCollidersComponents.getCollisionShape(mEntity);
const AABB aabb = collisionShape->computeTransformedAABB(getLocalToWorldTransform());
return aabb;
}
// Return a pointer to the collision shape
/**
* @return Pointer to the collision shape
*/
CollisionShape* Collider::getCollisionShape() {
return mBody->mWorld.mCollidersComponents.getCollisionShape(mEntity);
}
// Return a const pointer to the collision shape
/**
* @return Pointer to the collision shape
*/
const CollisionShape* Collider::getCollisionShape() const {
return mBody->mWorld.mCollidersComponents.getCollisionShape(mEntity);
}
// Return the broad-phase id
int Collider::getBroadPhaseId() const {
return mBody->mWorld.mCollidersComponents.getBroadPhaseId(mEntity);
}
// Return the local to parent body transform
/**
* @return The transformation that transforms the local-space of the collider
* to the local-space of the body
*/
const Transform& Collider::getLocalToBodyTransform() const {
return mBody->mWorld.mCollidersComponents.getLocalToBodyTransform(mEntity);
}
// Raycast method with feedback information
/**
* @param ray Ray to use for the raycasting in world-space
* @param[out] raycastInfo Result of the raycasting that is valid only if the
* methods returned true
* @return True if the ray hits the collision shape
*/
bool Collider::raycast(const Ray& ray, RaycastInfo& raycastInfo) {
// If the corresponding body is not active, it cannot be hit by rays
if (!mBody->isActive()) return false;
// Convert the ray into the local-space of the collision shape
const Transform& localToWorldTransform = mBody->mWorld.mCollidersComponents.getLocalToWorldTransform(mEntity);
const Transform worldToLocalTransform = localToWorldTransform.getInverse();
Ray rayLocal(worldToLocalTransform * ray.point1, worldToLocalTransform * ray.point2, ray.radius, ray.maxFraction);
const CollisionShape* collisionShape = mBody->mWorld.mCollidersComponents.getCollisionShape(mEntity);
bool isHit = collisionShape->raycast(rayLocal, raycastInfo, this, mMemoryManager.getPoolAllocator());
// Convert the raycast info into world-space
raycastInfo.worldPoint = localToWorldTransform * raycastInfo.worldPoint;
raycastInfo.worldNormal = localToWorldTransform.getOrientation() * raycastInfo.worldNormal;
raycastInfo.worldNormal.normalize();
return isHit;
}
// Return the collision category bits
/**
* @return The collision category bits mask of the collider
*/
unsigned short Collider::getCollisionCategoryBits() const {
return mBody->mWorld.mCollidersComponents.getCollisionCategoryBits(mEntity);
}
// Return the collision bits mask
/**
* @return The bits mask that specifies with which collision category this shape will collide
*/
unsigned short Collider::getCollideWithMaskBits() const {
return mBody->mWorld.mCollidersComponents.getCollideWithMaskBits(mEntity);
}
// Notify the collider that the size of the collision shape has been changed by the user
void Collider::setHasCollisionShapeChangedSize(bool hasCollisionShapeChangedSize) {
mBody->mWorld.mCollidersComponents.setHasCollisionShapeChangedSize(mEntity, hasCollisionShapeChangedSize);
}
// Set a new material for this rigid body
/**
* @param material The material you want to set to the body
*/
void Collider::setMaterial(const Material& material) {
mBody->mWorld.mCollidersComponents.setMaterial(mEntity, material);
RP3D_LOG(mBody->mWorld.mConfig.worldName, Logger::Level::Information, Logger::Category::Collider,
"Collider " + std::to_string(mEntity.id) + ": Set Material" + material.to_string(), __FILE__, __LINE__);
}
// Return the local to world transform
/**
* @return The transformation that transforms the local-space of the collision
* shape to the world-space
*/
const Transform Collider::getLocalToWorldTransform() const {
return mBody->mWorld.mCollidersComponents.getLocalToWorldTransform(mEntity);
}
// Return true if the collider is a trigger
/**
* @return True if this collider is a trigger and false otherwise
*/
bool Collider::getIsTrigger() const {
return mBody->mWorld.mCollidersComponents.getIsTrigger(mEntity);
}
// Set whether the collider is a trigger
/// Note that a collider cannot be a simulation collider and a trigger at the same time. If you set
/// this to true, this collider will stop being a simulation collider.
/**
* @param isTrigger True if you want to set this collider as a trigger and false otherwise
*/
void Collider::setIsTrigger(bool isTrigger) const {
mBody->mWorld.mCollidersComponents.setIsTrigger(mEntity, isTrigger);
// The collider cannot be a simulation collider anymore
if (isTrigger && getIsSimulationCollider()) {
setIsSimulationCollider(false);
}
}
// Return true if the collider can generate contacts for the simulation of the associated body
bool Collider::getIsSimulationCollider() const {
return mBody->mWorld.mCollidersComponents.getIsSimulationCollider(mEntity);
}
// Set whether the collider can generate contacts for the simulation of the associated body
/// Note that a collider cannot be a simulation collider and a trigger at the same time. If you set
/// this to true, this collider will stop being a trigger.
/**
* @param isSimulationCollider True if this collider can generate contacts for the simulation of the
* associated body.
*/
void Collider::setIsSimulationCollider(bool isSimulationCollider) const {
mBody->mWorld.mCollidersComponents.setIsSimulationCollider(mEntity, isSimulationCollider);
if (isSimulationCollider) {
mBody->mWorld.mBodyComponents.setHasSimulationCollider(mBody->getEntity(), true);
}
else {
mBody->updateHasSimulationCollider();
}
// The collider cannot be a trigger anymore
if (isSimulationCollider && getIsTrigger()) {
setIsTrigger(false);
}
}
// Return true if the collider will be part of results of queries on the PhysicsWorld
bool Collider::getIsWorldQueryCollider() const {
return mBody->mWorld.mCollidersComponents.getIsWorldQueryCollider(mEntity);
}
// Set whether the collider will be part of results of queries on the PhysicsWorld
/**
* @param isWorldQueryCollider True if this collider will be part of results of queries on the PhysicsWorld
*/
void Collider::setIsWorldQueryCollider(bool isWorldQueryCollider) const {
mBody->mWorld.mCollidersComponents.setIsWorldQueryCollider(mEntity, isWorldQueryCollider);
}
// Return a reference to the material properties of the collider
/**
* @return A reference to the material of the body
*/
Material& Collider::getMaterial() {
return mBody->mWorld.mCollidersComponents.getMaterial(mEntity);
}
#ifdef IS_RP3D_PROFILING_ENABLED
// Set the profiler
void Collider::setProfiler(Profiler* profiler) {
mProfiler = profiler;
CollisionShape* collisionShape = mBody->mWorld.mCollidersComponents.getCollisionShape(mEntity);
collisionShape->setProfiler(profiler);
}
#endif