1 /********************************************************************************
2 * ReactPhysics3D physics library, http://www.reactphysics3d.com                 *
3 * Copyright (c) 2010-2020 Daniel Chappuis                                       *
4 *********************************************************************************
5 *                                                                               *
6 * This software is provided 'as-is', without any express or implied warranty.   *
7 * In no event will the authors be held liable for any damages arising from the  *
8 * use of this software.                                                         *
9 *                                                                               *
10 * Permission is granted to anyone to use this software for any purpose,         *
11 * including commercial applications, and to alter it and redistribute it        *
12 * freely, subject to the following restrictions:                                *
13 *                                                                               *
14 * 1. The origin of this software must not be misrepresented; you must not claim *
15 *    that you wrote the original software. If you use this software in a        *
16 *    product, an acknowledgment in the product documentation would be           *
17 *    appreciated but is not required.                                           *
18 *                                                                               *
19 * 2. Altered source versions must be plainly marked as such, and must not be    *
20 *    misrepresented as being the original software.                             *
21 *                                                                               *
22 * 3. This notice may not be removed or altered from any source distribution.    *
23 *                                                                               *
24 ********************************************************************************/
25 
26 // Libraries
27 #include <reactphysics3d/collision/CollisionCallback.h>
28 #include <reactphysics3d/collision/ContactPair.h>
29 #include <reactphysics3d/constraint/ContactPoint.h>
30 #include <reactphysics3d/engine/PhysicsWorld.h>
31 
32 // We want to use the ReactPhysics3D namespace
33 using namespace reactphysics3d;
34 
35 // Constructor
ContactPoint(const reactphysics3d::ContactPoint & contactPoint)36 CollisionCallback::ContactPoint::ContactPoint(const reactphysics3d::ContactPoint& contactPoint) : mContactPoint(contactPoint) {
37 
38 }
39 
40 // Contact Pair Constructor
ContactPair(const reactphysics3d::ContactPair & contactPair,List<reactphysics3d::ContactPoint> * contactPoints,PhysicsWorld & world,bool isLostContactPair)41 CollisionCallback::ContactPair::ContactPair(const reactphysics3d::ContactPair& contactPair,
42                                             List<reactphysics3d::ContactPoint>* contactPoints, PhysicsWorld& world, bool isLostContactPair)
43                                :mContactPair(contactPair), mContactPoints(contactPoints),
44                                 mWorld(world), mIsLostContactPair(isLostContactPair) {
45 
46 }
47 
48 // Return a pointer to the first body in contact
getBody1() const49 CollisionBody* CollisionCallback::ContactPair::getBody1() const {
50     return static_cast<CollisionBody*>(mWorld.mCollisionBodyComponents.getBody(mContactPair.body1Entity));
51 }
52 
53 // Return a pointer to the second body in contact
getBody2() const54 CollisionBody* CollisionCallback::ContactPair::getBody2() const {
55     return static_cast<CollisionBody*>(mWorld.mCollisionBodyComponents.getBody(mContactPair.body2Entity));
56 }
57 
58 // Return a pointer to the first collider in contact (in body 1)
getCollider1() const59 Collider* CollisionCallback::ContactPair::getCollider1() const {
60     return mWorld.mCollidersComponents.getCollider(mContactPair.collider1Entity);
61 }
62 
63 // Return a pointer to the second collider in contact (in body 1)
getCollider2() const64 Collider* CollisionCallback::ContactPair::getCollider2() const {
65     return mWorld.mCollidersComponents.getCollider(mContactPair.collider2Entity);
66 }
67 
68 // Return the corresponding type of event for this contact pair
getEventType() const69 CollisionCallback::ContactPair::EventType CollisionCallback::ContactPair::getEventType() const {
70 
71     if (mIsLostContactPair) return EventType::ContactExit;
72 
73     if (mContactPair.collidingInPreviousFrame) return EventType::ContactStay;
74 
75     return EventType::ContactStart;
76 }
77 
78 // Constructor
CallbackData(List<reactphysics3d::ContactPair> * contactPairs,List<ContactManifold> * manifolds,List<reactphysics3d::ContactPoint> * contactPoints,List<reactphysics3d::ContactPair> & lostContactPairs,PhysicsWorld & world)79 CollisionCallback::CallbackData::CallbackData(List<reactphysics3d::ContactPair>* contactPairs, List<ContactManifold>* manifolds,
80                                               List<reactphysics3d::ContactPoint>* contactPoints, List<reactphysics3d::ContactPair>& lostContactPairs, PhysicsWorld& world)
81                       :mContactPairs(contactPairs), mContactManifolds(manifolds), mContactPoints(contactPoints), mLostContactPairs(lostContactPairs),
82                        mContactPairsIndices(world.mMemoryManager.getHeapAllocator()), mLostContactPairsIndices(world.mMemoryManager.getHeapAllocator()), mWorld(world) {
83 
84     // Filter the contact pairs to only keep the contact events (not the overlap/trigger events)
85     for (uint i=0; i < mContactPairs->size(); i++) {
86 
87         // If the contact pair contains contacts (and is therefore not an overlap/trigger event)
88         if (!(*mContactPairs)[i].isTrigger) {
89            mContactPairsIndices.add(i);
90         }
91     }
92     // Filter the lost contact pairs to only keep the contact events (not the overlap/trigger events)
93     for (uint i=0; i < mLostContactPairs.size(); i++) {
94 
95         // If the contact pair contains contacts (and is therefore not an overlap/trigger event)
96         if (!mLostContactPairs[i].isTrigger) {
97            mLostContactPairsIndices.add(i);
98         }
99     }
100 }
101 
102 // Return a given contact point of the contact pair
103 /// Note that the returned ContactPoint object is only valid during the call of the CollisionCallback::onContact()
104 /// method. Therefore, you need to get contact data from it and make a copy. Do not make a copy of the ContactPoint
105 /// object itself because it won't be valid after the CollisionCallback::onContact() call.
getContactPoint(uint index) const106 CollisionCallback::ContactPoint CollisionCallback::ContactPair::getContactPoint(uint index) const {
107 
108     assert(index < getNbContactPoints());
109 
110     return CollisionCallback::ContactPoint((*mContactPoints)[mContactPair.contactPointsIndex + index]);
111 }
112 
113 // Return a given contact pair
114 /// Note that the returned ContactPair object is only valid during the call of the CollisionCallback::onContact()
115 /// method. Therefore, you need to get contact data from it and make a copy. Do not make a copy of the ContactPair
116 /// object itself because it won't be valid after the CollisionCallback::onContact() call.
getContactPair(uint index) const117 CollisionCallback::ContactPair CollisionCallback::CallbackData::getContactPair(uint index) const {
118 
119     assert(index < getNbContactPairs());
120 
121     if (index < mContactPairsIndices.size()) {
122         // Return a contact pair
123         return CollisionCallback::ContactPair((*mContactPairs)[mContactPairsIndices[index]], mContactPoints, mWorld, false);
124     }
125     else {
126 
127         // Return a lost contact pair
128         return CollisionCallback::ContactPair(mLostContactPairs[mLostContactPairsIndices[index - mContactPairsIndices.size()]], mContactPoints, mWorld, true);
129     }
130 }
131