Spinning Topp Logo BlackTopp Studios
inc
collisiondispatcher.cpp
1 // © Copyright 2010 - 2016 BlackTopp Studios Inc.
2 /* This file is part of The Mezzanine Engine.
3 
4  The Mezzanine Engine is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  The Mezzanine Engine is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with The Mezzanine Engine. If not, see <http://www.gnu.org/licenses/>.
16 */
17 /* The original authors have included a copy of the license specified above in the
18  'Docs' folder. See 'gpl.txt'
19 */
20 /* We welcome the use of the Mezzanine engine to anyone, including companies who wish to
21  Build professional software and charge for their product.
22 
23  However there are some practical restrictions, so if your project involves
24  any of the following you should contact us and we will try to work something
25  out:
26  - DRM or Copy Protection of any kind(except Copyrights)
27  - Software Patents You Do Not Wish to Freely License
28  - Any Kind of Linking to Non-GPL licensed Works
29  - Are Currently In Violation of Another Copyright Holder's GPL License
30  - If You want to change our code and not add a few hundred MB of stuff to
31  your distribution
32 
33  These and other limitations could cause serious legal problems if you ignore
34  them, so it is best to simply contact us or the Free Software Foundation, if
35  you have any questions.
36 
37  Joseph Toppi - toppij@gmail.com
38  John Blackwood - makoenergy02@gmail.com
39 */
40 #ifndef _physicscollisiondispatcher_cpp
41 #define _physicscollisiondispatcher_cpp
42 
43 #include "Physics/collisiondispatcher.h.cpp"
44 #include "Physics/physicsmanager.h"
45 #include "Physics/collision.h"
46 #include "entresol.h"
47 #include "world.h"
48 
49 namespace Mezzanine
50 {
51  namespace Physics
52  {
53  ///////////////////////////////////////////////////////////
54  // CollisionDispatcher functions
55 
56  CollisionDispatcher::CollisionDispatcher(PhysicsManager * PhysMan, btCollisionConfiguration* CollisionConfig) :
57  btCollisionDispatcher(CollisionConfig),
58  PhysMan( PhysMan )
59  { }
60 
62  { }
63 
64  ///////////////////////////////////////////////////////////////////////////////
65  // New Implementation based on Algorithm creation
66 
68  {
69  void* ToReturn = btCollisionDispatcher::allocateCollisionAlgorithm(size);
70  btCollisionAlgorithm* Casted = (btCollisionAlgorithm*)ToReturn;
71  AlgoCreationQueue.push_back(Casted);
72  return ToReturn;
73  }
75  {
76  btCollisionAlgorithm* Casted = (btCollisionAlgorithm*)ptr;
77  // first check the queue
78  if(!AlgoCreationQueue.empty())
79  {
80  for(AlgoList::iterator QueIt = AlgoCreationQueue.begin() ; QueIt != AlgoCreationQueue.end() ; QueIt++ )
81  {
82  if(Casted == (*QueIt))
83  {
84  AlgoCreationQueue.erase(QueIt);
85  btCollisionDispatcher::freeCollisionAlgorithm(ptr);
86  return;
87  }
88  }
89  }
90  // now check the already generated collisions
91  for( Physics::PhysicsManager::CollisionMapIterator ColIt = this->PhysMan->Collisions.begin() ; ColIt != PhysMan->Collisions.end() ; ++ColIt )
92  {
93  if(Casted == (*ColIt).second->InternalAlgo)
94  {
95  //ManifoldDestructionQueue.push_back(ColIt);
96  //ToBeDestroyed->GetActorA()->_NotifyCollisionState(ToBeDestroyed,Collision::Col_End);
97  //ToBeDestroyed->GetActorB()->_NotifyCollisionState(ToBeDestroyed,Collision::Col_End);
98  delete (*ColIt).second;
99  this->PhysMan->Collisions.erase(ColIt);
100  break;
101  }
102  }
103  btCollisionDispatcher::freeCollisionAlgorithm(ptr);
104  }
106  {
107  return &AlgoCreationQueue;
108  }
109 
110  ///////////////////////////////////////////////////////////////////////////////
111  // Old Implementation based on Manifold creation
112 
113  /*btPersistentManifold* CollisionDispatcher::getNewManifold(void* b0, void* b1)
114  {
115  // Get the manifold
116  btPersistentManifold* NewManifold = btCollisionDispatcher::getNewManifold(b0,b1);
117  // Store the manifold for processing later
118  ManifoldCreationQueue.push_back(NewManifold);
119  return NewManifold;
120  }
121  void CollisionDispatcher::releaseManifold(btPersistentManifold* manifold)
122  {
123  // first check the queue
124  if(!ManifoldCreationQueue.empty())
125  {
126  for(std::list<btPersistentManifold*>::iterator QueIt = ManifoldCreationQueue.begin() ; QueIt != ManifoldCreationQueue.end() ; QueIt++ )
127  {
128  if(manifold == (*QueIt))
129  {
130  ManifoldCreationQueue.erase(QueIt);
131  btCollisionDispatcher::releaseManifold(manifold);
132  return;
133  }
134  }
135  }
136  // now check the already generated collisions
137  PhysicsManager* PhysMan = Entresol::GetSingletonPtr()->GetPhysicsManager();
138  for( PhysicsManager::CollisionIterator ColIt = PhysMan->Collisions.begin() ; ColIt != PhysMan->Collisions.end() ; ++ColIt )
139  {
140  if(manifold == (*ColIt).second->InternalAlgo)
141  {
142  //ManifoldDestructionQueue.push_back(ColIt);
143  // Collision* ToBeDestroyed = (*ColIt).second;
144  //ToBeDestroyed->GetActorA()->_NotifyCollisionState(ToBeDestroyed,Collision::Col_End);
145  //ToBeDestroyed->GetActorB()->_NotifyCollisionState(ToBeDestroyed,Collision::Col_End);
146  delete (*ColIt).second;
147  PhysMan->Collisions.erase(ColIt);
148  //delete ToBeDestroyed;
149  break;
150  }
151  }
152  btCollisionDispatcher::releaseManifold(manifold);
153  }
154  void CollisionDispatcher::releaseManifoldManual(btPersistentManifold* manifold)
155  {
156  PhysicsManager* PhysMan = Entresol::GetSingletonPtr()->GetPhysicsManager();
157  for( PhysicsManager::CollisionIterator ColIt = PhysMan->Collisions.begin() ; ColIt != PhysMan->Collisions.end() ; ++ColIt )
158  {
159  if(manifold == (*ColIt).second->Manifold)
160  {
161  Collision* ToBeDestroyed = (*ColIt).second;
162  //ToBeDestroyed->GetActorA()->_NotifyCollisionState(ToBeDestroyed,Collision::Col_End);
163  //ToBeDestroyed->GetActorB()->_NotifyCollisionState(ToBeDestroyed,Collision::Col_End);
164  PhysMan->Collisions.erase(ColIt);
165  delete ToBeDestroyed;
166  break;
167  }
168  }
169  btCollisionDispatcher::releaseManifold(manifold);
170  }// */
171 
172  ///////////////////////////////////////////////////////////
173  // ParallelCollisionDispatcher functions
174 
175  ParallelCollisionDispatcher::ParallelCollisionDispatcher(PhysicsManager * PhysMan, btThreadSupportInterface* ThreadInterface, unsigned int MaxNumTasks, btCollisionConfiguration* CollisionConfig) :
176  PhysMan( PhysMan ),
177  SpuGatheringCollisionDispatcher(ThreadInterface,MaxNumTasks,CollisionConfig)
178  {
179  }
180 
182  {
183  }
184 
185  ///////////////////////////////////////////////////////////////////////////////
186  // New Implementation based on Algorithm creation
187 
189  {
190  void* ToReturn = btCollisionDispatcher::allocateCollisionAlgorithm(size);
191  btCollisionAlgorithm* Casted = (btCollisionAlgorithm*)ToReturn;
192  AlgoCreationQueue.push_back(Casted);
193  return ToReturn;
194  }
196  {
197  btCollisionAlgorithm* Casted = (btCollisionAlgorithm*)ptr;
198  // first check the queue
199  if(!AlgoCreationQueue.empty())
200  {
201  for(AlgoList::iterator QueIt = AlgoCreationQueue.begin() ; QueIt != AlgoCreationQueue.end() ; QueIt++ )
202  {
203  if(Casted == (*QueIt))
204  {
205  AlgoCreationQueue.erase(QueIt);
206  btCollisionDispatcher::freeCollisionAlgorithm(ptr);
207  return;
208  }
209  }
210  }
211  // now check the already generated collisions
212  for( Physics::PhysicsManager::CollisionMapIterator ColIt = this->PhysMan->Collisions.begin() ; ColIt != PhysMan->Collisions.end() ; ++ColIt )
213  {
214  if(Casted == (*ColIt).second->InternalAlgo)
215  {
216  //ManifoldDestructionQueue.push_back(ColIt);
217  //ToBeDestroyed->GetActorA()->_NotifyCollisionState(ToBeDestroyed,Collision::Col_End);
218  //ToBeDestroyed->GetActorB()->_NotifyCollisionState(ToBeDestroyed,Collision::Col_End);
219  delete (*ColIt).second;
220  this->PhysMan->Collisions.erase(ColIt);
221  break;
222  }
223  }
224  btCollisionDispatcher::freeCollisionAlgorithm(ptr);
225  }
227  {
228  return &AlgoCreationQueue;
229  }
230 
231  ///////////////////////////////////////////////////////////////////////////////
232  // Old Implementation based on Manifold creation
233 
234  /*btPersistentManifold* ParallelCollisionDispatcher::getNewManifold(void* b0, void* b1)
235  {
236  // Get the manifold
237  btPersistentManifold* NewManifold = btCollisionDispatcher::getNewManifold(b0,b1);
238  // Store the manifold for processing later
239  ManifoldCreationQueue.push_back(NewManifold);
240  return NewManifold;
241  }
242  void ParallelCollisionDispatcher::releaseManifold(btPersistentManifold* manifold)
243  {
244  // first check the queue
245  if(!ManifoldCreationQueue.empty())
246  {
247  for(std::list<btPersistentManifold*>::iterator QueIt = ManifoldCreationQueue.begin() ; QueIt != ManifoldCreationQueue.end() ; QueIt++ )
248  {
249  if(manifold == (*QueIt))
250  {
251  ManifoldCreationQueue.erase(QueIt);
252  btCollisionDispatcher::releaseManifold(manifold);
253  return;
254  }
255  }
256  }
257  // now check the already generated collisions
258  PhysicsManager* PhysMan = Entresol::GetSingletonPtr()->GetPhysicsManager();
259  for( PhysicsManager::CollisionIterator ColIt = PhysMan->Collisions.begin() ; ColIt != PhysMan->Collisions.end() ; ++ColIt )
260  {
261  if(manifold == (*ColIt).second->InternalAlgo)
262  {
263  //ManifoldDestructionQueue.push_back(ColIt);
264  // Collision* ToBeDestroyed = (*ColIt).second;
265  //ToBeDestroyed->GetActorA()->_NotifyCollisionState(ToBeDestroyed,Collision::Col_End);
266  //ToBeDestroyed->GetActorB()->_NotifyCollisionState(ToBeDestroyed,Collision::Col_End);
267  delete (*ColIt).second;
268  PhysMan->Collisions.erase(ColIt);
269  //delete ToBeDestroyed;
270  break;
271  }
272  }
273  btCollisionDispatcher::releaseManifold(manifold);
274  }
275  void ParallelCollisionDispatcher::releaseManifoldManual(btPersistentManifold* manifold)
276  {
277  PhysicsManager* PhysMan = Entresol::GetSingletonPtr()->GetPhysicsManager();
278  for( PhysicsManager::CollisionIterator ColIt = PhysMan->Collisions.begin() ; ColIt != PhysMan->Collisions.end() ; ++ColIt )
279  {
280  if(manifold == (*ColIt).second->Manifold)
281  {
282  Collision* ToBeDestroyed = (*ColIt).second;
283  //ToBeDestroyed->GetActorA()->_NotifyCollisionState(ToBeDestroyed,Collision::Col_End);
284  //ToBeDestroyed->GetActorB()->_NotifyCollisionState(ToBeDestroyed,Collision::Col_End);
285  PhysMan->Collisions.erase(ColIt);
286  delete ToBeDestroyed;
287  break;
288  }
289  }
290  btCollisionDispatcher::releaseManifold(manifold);
291  }// */
292  }//Physics
293 }//Mezzanine
294 
295 #endif
void freeCollisionAlgorithm(void *ptr)
Frees up the space belonging to a Collision Algorithm that is no longer needed.
void * allocateCollisionAlgorithm(int size)
Allocates space for a new Collision Algorithm between two objects.
AlgoList AlgoCreationQueue
A list of all the algorithms that have been created and need processing.
void freeCollisionAlgorithm(void *ptr)
Frees up the space belonging to a Collision Algorithm that is no longer needed.
AlgoList * GetAlgoCreationQueue()
Gets the list of algorithms that have been created and need processing.
ParallelCollisionDispatcher(PhysicsManager *PhysMan, btThreadSupportInterface *ThreadInterface, unsigned int MaxNumTasks, btCollisionConfiguration *CollisionConfig)
Class constructor.
CollisionMap::iterator CollisionMapIterator
Iterator type for sorted Collision instances.
std::list< btCollisionAlgorithm * > AlgoList
Convenience datatype for a collection of Collision Algorithms.
This is simply a place for storing all the Physics Related functions.
PhysicsManager * PhysMan
Physics Manager.
AlgoList * GetAlgoCreationQueue()
Gets the list of algorithms that have been created and need processing.
The bulk of the engine components go in this namspace.
Definition: actor.cpp:56
AlgoList AlgoCreationQueue
A list of all the algorithms that have been created and need processing.
virtual ~CollisionDispatcher()
Class destructor.
void * allocateCollisionAlgorithm(int size)
Allocates space for a new Collision Algorithm between two objects.
CollisionMap Collisions
A container tracking all of the existing collisions in the physics world.
CollisionDispatcher(PhysicsManager *PhysMan, btCollisionConfiguration *CollisionConfig)
Class constructor.