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/OverlapCallback.h>
28 #include <reactphysics3d/engine/PhysicsWorld.h>
29 
30 // We want to use the ReactPhysics3D namespace
31 using namespace reactphysics3d;
32 
33 // Contact Pair Constructor
OverlapPair(ContactPair & contactPair,PhysicsWorld & world,bool isLostOverlappingPair)34 OverlapCallback::OverlapPair::OverlapPair(ContactPair& contactPair, PhysicsWorld& world, bool isLostOverlappingPair)
35                              : mContactPair(contactPair), mWorld(world), mIsLostOverlapPair(isLostOverlappingPair) {
36 
37 }
38 
39 // Return a pointer to the first collider in contact
getCollider1() const40 Collider* OverlapCallback::OverlapPair::getCollider1() const {
41     return static_cast<Collider*>(mWorld.mCollidersComponents.getCollider(mContactPair.collider1Entity));
42 }
43 
44 // Return a pointer to the second collider in contact
getCollider2() const45 Collider* OverlapCallback::OverlapPair::getCollider2() const {
46     return static_cast<Collider*>(mWorld.mCollidersComponents.getCollider(mContactPair.collider2Entity));
47 }
48 
49 // Return a pointer to the first body in contact
getBody1() const50 CollisionBody* OverlapCallback::OverlapPair::getBody1() const {
51     return static_cast<CollisionBody*>(mWorld.mCollisionBodyComponents.getBody(mContactPair.body1Entity));
52 }
53 
54 // Return a pointer to the second body in contact
getBody2() const55 CollisionBody* OverlapCallback::OverlapPair::getBody2() const {
56     return static_cast<CollisionBody*>(mWorld.mCollisionBodyComponents.getBody(mContactPair.body2Entity));
57 }
58 
59 // Return the corresponding type of event for this overlapping pair
getEventType() const60 OverlapCallback::OverlapPair::EventType OverlapCallback::OverlapPair::getEventType() const {
61 
62     if (mIsLostOverlapPair) return EventType::OverlapExit;
63 
64     if (mContactPair.collidingInPreviousFrame) return EventType::OverlapStay;
65 
66     return EventType::OverlapStart;
67 }
68 
69 // CollisionCallbackData Constructor
CallbackData(List<ContactPair> & contactPairs,List<ContactPair> & lostContactPairs,bool onlyReportTriggers,PhysicsWorld & world)70 OverlapCallback::CallbackData::CallbackData(List<ContactPair>& contactPairs, List<ContactPair>& lostContactPairs, bool onlyReportTriggers, PhysicsWorld& world)
71                 :mContactPairs(contactPairs), mLostContactPairs(lostContactPairs),
72                  mContactPairsIndices(world.mMemoryManager.getHeapAllocator()), mLostContactPairsIndices(world.mMemoryManager.getHeapAllocator()), mWorld(world) {
73 
74     // Filter the contact pairs to only keep the overlap/trigger events (not the contact events)
75     for (uint i=0; i < mContactPairs.size(); i++) {
76 
77         // If the contact pair contains contacts (and is therefore not an overlap/trigger event)
78         if (!onlyReportTriggers || mContactPairs[i].isTrigger) {
79            mContactPairsIndices.add(i);
80         }
81     }
82     // Filter the lost contact pairs to only keep the overlap/trigger events (not the contact events)
83     for (uint i=0; i < mLostContactPairs.size(); i++) {
84 
85         // If the contact pair contains contacts (and is therefore not an overlap/trigger event)
86         if (!onlyReportTriggers || mLostContactPairs[i].isTrigger) {
87            mLostContactPairsIndices.add(i);
88         }
89     }
90 }
91