Spinning Topp Logo BlackTopp Studios
inc
collision.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 _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(ExceptionBase::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() > static_cast<int>( 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  Boole ContainsA = ( A == ObjA ) || ( A == ObjB );
169  Boole ContainsB = ( B == ObjA ) || ( B == ObjB );
170  return (ContainsA && ContainsB);
171  }else{
172  return false;
173  }
174  }
175 
177  {
178  Boole ContainsA = ( A == this->GetProxyA() ) || ( A == this->GetProxyB() );
179  Boole ContainsB = ( B == this->GetProxyA() ) || ( B == this->GetProxyB() );
180  return (ContainsA && ContainsB);
181  }
182 
184  {
185  this->InternalData->Manifolds.clear();
186  this->InternalAlgo->getAllContactManifolds(this->InternalData->Manifolds);
187  Whole NumManifolds = this->InternalData->Manifolds.size();
188  if( NumManifolds != 0 && this->PenetrationDistances.size() != NumManifolds )
189  {
191 
192  WorldObject* ObjectA = this->ProxyA->GetParentObject();
193  if( ObjectA ) {
194  ObjectA->_NotifyCollisionState(this,Physics::Col_Contacts_Updated);
195  }
196 
197  WorldObject* ObjectB = this->ProxyB->GetParentObject();
198  if( ObjectB ) {
199  ObjectB->_NotifyCollisionState(this,Physics::Col_Contacts_Updated);
200  }
201 
202  return;
203  }
204  for( Whole X = 0 ; X < NumManifolds ; ++X )
205  {
206  if( this->PenetrationDistances[X] != this->GetDistance(X) )
207  {
209 
210  WorldObject* ObjectA = this->ProxyA->GetParentObject();
211  if( ObjectA ) {
212  ObjectA->_NotifyCollisionState(this,Physics::Col_Contacts_Updated);
213  }
214 
215  WorldObject* ObjectB = this->ProxyB->GetParentObject();
216  if( ObjectB ) {
217  ObjectB->_NotifyCollisionState(this,Physics::Col_Contacts_Updated);
218  }
219 
220  return;
221  }
222  }
223  }
224 
225  ///////////////////////////////////////////////////////////////////////////////
226  // Contact Query
227 
229  { return (Whole)this->InternalData->Manifolds.size(); }
230 
232  {
233  btVector3 PointA = this->GetManifoldPoint(Point).m_localPointA;
234  btVector3 PointB = this->GetManifoldPoint(Point).m_localPointB;
235  return Vector3((PointA+PointB) /= 2);
236  }
237 
239  { return Vector3(this->GetManifoldPoint(Point).m_localPointA); }
240 
242  { return Vector3(this->GetManifoldPoint(Point).m_localPointB); }
243 
245  { return Vector3(this->GetManifoldPoint(Point).m_normalWorldOnB); }
246 
248  { return this->GetManifoldPoint(Point).m_appliedImpulse; }
249 
251  { return this->GetManifoldPoint(Point).m_distance1; }
252 
254  { return (Whole)this->GetManifoldPoint(Point).m_lifeTime; }
255 
256  ///////////////////////////////////////////////////////////////////////////////
257  // Internal Methods
258 
260  {
261  if( !this->ProxyA ) {
262  this->ProxyA = A;
263  WorldObject* ObjectA = this->ProxyA->GetParentObject();
264  if( ObjectA ) {
265  ObjectA->_NotifyCollisionState(this,Physics::Col_Begin);
266  }
267  }
268  }
269 
271  {
272  if( !this->ProxyB ) {
273  this->ProxyB = B;
274  WorldObject* ObjectB = this->ProxyB->GetParentObject();
275  if( ObjectB ) {
276  ObjectB->_NotifyCollisionState(this,Physics::Col_Begin);
277  }
278  }
279  }
280  }//Physics
281 }//Mezzanine
282 
283 #endif
virtual void _NotifyCollisionState(Physics::Collision *Col, const Physics::CollisionState State)
Notifies this World Object of a collision that is occuring with it.
virtual Whole GetAge(const Whole &Point)
Gets the number of simulation steps the contact point has existed.
Definition: collision.cpp:253
bool Boole
Generally acts a single bit, true or false.
Definition: datatypes.h:173
virtual Real GetAppliedImpulse(const Whole &Point)
Gets the amount of force of the collision.
Definition: collision.cpp:247
Collision()
Default Constructor.
Definition: collision.cpp:87
virtual Real GetDistance(const Whole &Point)
Gets the penetration depth of the collision.
Definition: collision.cpp:250
#define MEZZ_EXCEPTION(num, desc)
An easy way to throw exceptions with rich information.
Definition: exception.h:3048
virtual void _SetProxyA(CollidableProxy *A)
Sets the first Collidable this collision applies to.
Definition: collision.cpp:259
int Integer
A datatype used to represent any integer close to.
Definition: datatypes.h:154
virtual WorldObject * GetObjectA() const
Gets the parent Object of CollidableProxy A.
Definition: collision.cpp:157
virtual Vector3 GetWorldLocation(const Whole &Point)
Gets the location in the world where the collision occured.
Definition: collision.cpp:231
btManifoldPoint & GetManifoldPoint(const Whole &Index)
Internal function responsible for fetching the appropriate contact point.
Definition: collision.cpp:123
float Real
A Datatype used to represent a real floating point number.
Definition: datatypes.h:141
virtual CollidableProxy * GetProxyA() const
Gets the first CollidableProxy this collision applies to.
Definition: collision.cpp:151
This is the base class from which classes that are insertable into the physical world.
Definition: worldobject.h:60
std::vector< Real > PenetrationDistances
This stores the distance of each contact point in this collision, for using to track updates...
Definition: collision.h:93
virtual void Update()
Updates this collisions contact point data if it needs updating.
Definition: collision.cpp:183
Used to insulate the interface from the implementation details for bullet.
Definition: collision.cpp:61
virtual Boole PairsMatch(WorldObject *A, WorldObject *B) const
Convenience function to see if the provided WorldObject pair match the pair in this class...
Definition: collision.cpp:163
void UpdatePenetrationDistances()
Updates the PenetrationDistances vector on this object.
Definition: collision.cpp:137
This is an event class used to track collsions in the physics world.
Definition: collision.h:72
virtual WorldObject * GetObjectB() const
Gets the parent Object of CollidableProxy B.
Definition: collision.cpp:160
CollidableProxy * ProxyB
The second CollidableProxy invovled in the collision.
Definition: collision.h:90
virtual Vector3 GetNormal(const Whole &Point)
GEts the collision normal for a contact point.
Definition: collision.cpp:244
btManifoldArray Manifolds
Wrapped Data, An array of Collision Manifolds.
Definition: collision.cpp:64
This is a proxy from which physics objects that can collide with each other are handled.
virtual ~Collision()
Class Destructor.
Definition: collision.cpp:108
virtual CollidableProxy * GetProxyB() const
Gets the second CollidableProxy this collision applies to.
Definition: collision.cpp:154
CollidableProxy * ProxyA
The first CollidableProxy involved in the collision.
Definition: collision.h:87
This is used to represent a point in space, or a vector through space.
Definition: vector3.h:77
virtual Whole GetNumContactPoints()
Gets the number of contact points this collision is storing.
Definition: collision.cpp:228
CollisionInternalData * InternalData
Array of manifolds that apply to this collision.
Definition: collision.h:84
The bulk of the engine components go in this namspace.
Definition: actor.cpp:56
unsigned long Whole
Whole is an unsigned integer, it will be at least 32bits in size.
Definition: datatypes.h:151
virtual WorldObject * GetParentObject() const
Gets a pointer to the parent object controlling this proxy.
Definition: worldproxy.cpp:75
btCollisionAlgorithm * InternalAlgo
The internal collision class this event is based on.
Definition: collision.h:81
virtual Vector3 GetLocalALocation(const Whole &Point)
Gets the location in ObjectA's local space where the collision occured.
Definition: collision.cpp:238
Thrown when attempted to access something that really should note be accessed.
Definition: exception.h:98
virtual Vector3 GetLocalBLocation(const Whole &Point)
Gets the location in ObjectB's local space where the collision occured.
Definition: collision.cpp:241
virtual void _SetProxyB(CollidableProxy *B)
Sets the second Collidable this collision applies to.
Definition: collision.cpp:270