MezzanineEngine 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
collision.cpp
1 // © Copyright 2010 - 2014 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 _physicscollision_cpp
41 #define _physicscollision_cpp
42 
43 #include "collision.h"
44 #include "worldobject.h"
45 
46 #include "entresol.h"
47 #include "stringtool.h"
48 
49 #include "Physics/physicsenumerations.h"
50 #include "Physics/collidableproxy.h"
51 
52 #include <btBulletDynamicsCommon.h>
53 #include <memory>
54 
55 namespace Mezzanine
56 {
57  namespace Physics
58  {
59  /// @internal
60  /// @brief Used to insulate the interface from the implementation details for bullet.
62  {
63  /// @brief Wrapped Data, An array of Collision Manifolds
64  btManifoldArray Manifolds;
65  };
66 
67  Collision::Collision(CollidableProxy* A, CollidableProxy* B, btCollisionAlgorithm* PhysicsAlgo) :
68  InternalAlgo(PhysicsAlgo),
69  ProxyA(A),
70  ProxyB(B)
71  {
72  this->InternalData = new CollisionInternalData();
73 
74  WorldObject* ObjectA = this->ProxyA->GetParentObject();
75  if( ObjectA ) {
76  ObjectA->_NotifyCollisionState(this,Physics::Col_Begin);
77  }
78 
79  WorldObject* ObjectB = this->ProxyB->GetParentObject();
80  if( ObjectB ) {
81  ObjectB->_NotifyCollisionState(this,Physics::Col_Begin);
82  }
83 
84  this->InternalAlgo->getAllContactManifolds(this->InternalData->Manifolds);
85  }
86 
88  InternalAlgo(NULL),
89  ProxyA(NULL),
90  ProxyB(NULL)
91  {
92  this->InternalData = new CollisionInternalData();
93  }
94 
96  InternalAlgo(Other.InternalAlgo),
97  ProxyA(Other.ProxyA),
98  ProxyB(Other.ProxyB)
99  {
100  for( Whole X = 0 ; X < Other.InternalData->Manifolds.size() ; ++X )
101  this->InternalData->Manifolds.push_back(Other.InternalData->Manifolds[X]);
102 
103  // Double notifies seems like a bad idea.
104  //this->ProxyA->_NotifyCollisionState(this,Physics::Col_Begin);
105  //this->ProxyB->_NotifyCollisionState(this,Physics::Col_Begin);
106  }
107 
109  {
110  WorldObject* ObjectA = this->ProxyA->GetParentObject();
111  if( ObjectA ) {
112  ObjectA->_NotifyCollisionState(this,Physics::Col_End);
113  }
114 
115  WorldObject* ObjectB = this->ProxyB->GetParentObject();
116  if( ObjectB ) {
117  ObjectB->_NotifyCollisionState(this,Physics::Col_End);
118  }
119 
120  delete this->InternalData;
121  }
122 
123  btManifoldPoint& Collision::GetManifoldPoint(const Whole& Index)
124  {
125  if( static_cast<Integer>( Index ) >= this->InternalData->Manifolds.size() )
126  MEZZ_EXCEPTION(Exception::MM_OUT_OF_BOUNDS_EXCEPTION,"Attempting to access invalid index in Collision.");
127 
128  if( Index > 3 ) {
129  Whole SuperIndex = Index/4;
130  Whole SubIndex = Index%4;
131  return (this->InternalData->Manifolds.at(SuperIndex)->getContactPoint(SubIndex));
132  }else{
133  return (this->InternalData->Manifolds.at(0)->getContactPoint(Index));
134  }
135  }
136 
138  {
139  if( this->InternalData->Manifolds.size() > this->PenetrationDistances.size() )
140  this->PenetrationDistances.resize( this->InternalData->Manifolds.size() );
141  this->PenetrationDistances.clear();
142  for( Integer X = 0 ; X < this->InternalData->Manifolds.size() ; ++X )
143  {
144  this->PenetrationDistances[X] = this->GetManifoldPoint(X).m_distance1;
145  }
146  }
147 
148  ///////////////////////////////////////////////////////////////////////////////
149  // Utility
150 
152  { return this->ProxyA; }
153 
155  { return this->ProxyB; }
156 
158  { return this->ProxyA->GetParentObject(); }
159 
161  { return this->ProxyB->GetParentObject(); }
162 
164  {
165  WorldObject* ObjA = this->GetObjectA();
166  WorldObject* ObjB = this->GetObjectB();
167  if( ObjA && ObjB ) {
168  bool ContainsA = ( A == ObjA ) || ( A == ObjB );
169  bool ContainsB = ( B == ObjA ) || ( B == ObjB );
170  return (ContainsA && ContainsB);
171  }else{
172  return false;
173  }
174  }
175 
177  {
178  bool ContainsA = ( A == this->GetProxyA() ) || ( A == this->GetProxyB() );
179  bool ContainsB = ( B == this->GetProxyA() ) || ( B == this->GetProxyB() );
180  return (ContainsA && ContainsB);
181  }
182 
184  {
185  this->InternalAlgo->getAllContactManifolds(this->InternalData->Manifolds);
186  Whole NumManifolds = this->InternalData->Manifolds.size();
187  if( this->PenetrationDistances.size() != NumManifolds )
188  {
190 
191  WorldObject* ObjectA = this->ProxyA->GetParentObject();
192  if( ObjectA ) {
193  ObjectA->_NotifyCollisionState(this,Physics::Col_Contacts_Updated);
194  }
195 
196  WorldObject* ObjectB = this->ProxyB->GetParentObject();
197  if( ObjectB ) {
198  ObjectB->_NotifyCollisionState(this,Physics::Col_Contacts_Updated);
199  }
200 
201  return;
202  }
203  for( Whole X = 0 ; X < NumManifolds ; ++X )
204  {
205  if( this->PenetrationDistances[X] != this->GetDistance(X) )
206  {
208 
209  WorldObject* ObjectA = this->ProxyA->GetParentObject();
210  if( ObjectA ) {
211  ObjectA->_NotifyCollisionState(this,Physics::Col_Contacts_Updated);
212  }
213 
214  WorldObject* ObjectB = this->ProxyB->GetParentObject();
215  if( ObjectB ) {
216  ObjectB->_NotifyCollisionState(this,Physics::Col_Contacts_Updated);
217  }
218 
219  return;
220  }
221  }
222  }
223 
224  ///////////////////////////////////////////////////////////////////////////////
225  // Contact Query
226 
228  { return (Whole)this->InternalData->Manifolds.size(); }
229 
231  {
232  btVector3 PointA = this->GetManifoldPoint(Point).m_localPointA;
233  btVector3 PointB = this->GetManifoldPoint(Point).m_localPointB;
234  return Vector3((PointA+PointB) /= 2);
235  }
236 
238  { return Vector3(this->GetManifoldPoint(Point).m_localPointA); }
239 
241  { return Vector3(this->GetManifoldPoint(Point).m_localPointB); }
242 
244  { return Vector3(this->GetManifoldPoint(Point).m_normalWorldOnB); }
245 
247  { return this->GetManifoldPoint(Point).m_appliedImpulse; }
248 
250  { return this->GetManifoldPoint(Point).m_distance1; }
251 
253  { return (Whole)this->GetManifoldPoint(Point).m_lifeTime; }
254 
255  ///////////////////////////////////////////////////////////////////////////////
256  // Internal Methods
257 
259  {
260  if( this->ProxyA ) {
261  Entresol::GetSingletonPtr()->Log("Attepting to change Proxy pointer Member in Collision. This is not permitted.");
262  }else{
263  this->ProxyA = A;
264  WorldObject* ObjectA = this->ProxyA->GetParentObject();
265  if( ObjectA ) {
266  ObjectA->_NotifyCollisionState(this,Physics::Col_Begin);
267  }
268  }
269  }
270 
272  {
273  if( this->ProxyB ) {
274  Entresol::GetSingletonPtr()->Log("Attepting to change Proxy pointer Member in Collision. This is not permitted.");
275  }else{
276  this->ProxyB = B;
277  WorldObject* ObjectB = this->ProxyB->GetParentObject();
278  if( ObjectB ) {
279  ObjectB->_NotifyCollisionState(this,Physics::Col_Begin);
280  }
281  }
282  }
283  }//Physics
284 }//Mezzanine
285 
286 #endif