MezzanineEngine 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ghostproxy.cpp
1 // © Copyright 2010 - 2012 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 _ghostproxy_cpp
41 #define _ghostproxy_cpp
42 
43 #include "Physics/ghostproxy.h"
44 #include "Physics/physicsmanager.h"
45 
46 #include "enumerations.h"
47 #include "stringtool.h"
48 
49 #include <btBulletDynamicsCommon.h>
50 #include <BulletSoftBody/btSoftRigidDynamicsWorld.h>
51 #include <BulletCollision/CollisionDispatch/btGhostObject.h>
52 
53 namespace Mezzanine
54 {
55  namespace Physics
56  {
58  CollidableProxy(Creator),
59  PhysicsGhostBody(NULL)
60  {
61  this->CreateGhostObject();
62  }
63 
64  GhostProxy::GhostProxy(const XML::Node& SelfRoot, PhysicsManager* Creator) :
65  CollidableProxy(Creator),
66  PhysicsGhostBody(NULL)
67  {
68  this->CreateGhostObject();
69  this->ProtoDeSerialize(SelfRoot);
70  }
71 
73  {
74  if( this->IsInWorld() )
75  this->RemoveFromWorld();
76 
77  delete this->PhysicsGhostBody;
78  }
79 
81  {
82  this->PhysicsGhostBody = new btPairCachingGhostObject();
83  this->PhysicsGhostBody->setCollisionFlags( this->PhysicsGhostBody->getCollisionFlags() | btCollisionObject::CF_NO_CONTACT_RESPONSE );
84  this->PhysicsGhostBody->setUserPointer( this );
85  }
86 
87  ///////////////////////////////////////////////////////////////////////////////
88  // Utility
89 
91  {
92  return Mezzanine::PT_Physics_GhostProxy;
93  }
94 
96  {
97  if( !this->IsInWorld() ) {
98  this->Manager->_GetPhysicsWorldPointer()->addCollisionObject( this->PhysicsGhostBody, this->CollisionGroup, this->CollisionMask );
99  }
100  }
101 
103  {
104  if( this->IsInWorld() ) {
105  this->Manager->_GetPhysicsWorldPointer()->removeCollisionObject( this->PhysicsGhostBody );
106  }
107  }
108 
109  ///////////////////////////////////////////////////////////////////////////////
110  // Overlapping Proxy Access
111 
113  {
114  btCollisionObject* OverlappingObject = this->PhysicsGhostBody->getOverlappingObject( static_cast<int>( Index ) );
115  return static_cast<CollidableProxy*>( OverlappingObject->getUserPointer() );
116  }
117 
119  {
120  return static_cast<UInt32>( this->PhysicsGhostBody->getNumOverlappingObjects() );
121  }
122 
124  {
125  btCollisionWorld* PhysWorld = this->Manager->_GetPhysicsWorldPointer();
126 
127  btBroadphasePairArray& PairArray = this->PhysicsGhostBody->getOverlappingPairCache()->getOverlappingPairArray();
128  const btBroadphasePair& CachePair = PairArray[Index];
129  btBroadphasePair* WorldPair = PhysWorld->getPairCache()->findPair(CachePair.m_pProxy0,CachePair.m_pProxy1);
130 
131  if( WorldPair != NULL )
132  {
133  btManifoldArray ManifoldArray;
134  ManifoldArray.clear();
135 
136  if( WorldPair->m_algorithm )
137  WorldPair->m_algorithm->getAllContactManifolds(ManifoldArray);
138 
139  for( int ManifoldIndex = 0 ; ManifoldIndex < ManifoldArray.size() ; ++ManifoldIndex )
140  {
141  btPersistentManifold* Manifold = ManifoldArray[ManifoldIndex];
142  for( int ContactIndex = 0 ; ContactIndex < Manifold->getNumContacts() ; ++ContactIndex )
143  {
144  // ©onst btManifoldPoint& pt = Manifold->getContactPoint(ContactIndex);
145  //if( pt.m_distance1 > 0 )
146  // return NULL;
147 
148  btCollisionObject* ColObj = ( Manifold->getBody0() != this->PhysicsGhostBody ? (btCollisionObject*)(Manifold->getBody0()) : (btCollisionObject*)(Manifold->getBody1()) );
149  return static_cast<CollidableProxy*>( ColObj->getUserPointer() );
150  }
151  }
152  }
153  return NULL;
154  }
155 
157  {
158  btBroadphasePairArray& PairArray = this->PhysicsGhostBody->getOverlappingPairCache()->getOverlappingPairArray();
159  return static_cast<UInt32>( PairArray.size() );
160  }
161 
162  ///////////////////////////////////////////////////////////////////////////////
163  // Serialization
164 
166  { this->CollidableProxy::ProtoSerializeProperties(SelfRoot); }
167 
170 
172  { return GhostProxy::GetSerializableName(); }
173 
175  { return "GhostProxy"; }
176 
177  ///////////////////////////////////////////////////////////////////////////////
178  // Internal Methods
179 
180  btPairCachingGhostObject* GhostProxy::_GetPhysicsObject() const
181  { return this->PhysicsGhostBody; }
182 
183  btCollisionObject* GhostProxy::_GetBasePhysicsObject() const
184  { return this->PhysicsGhostBody; }
185  }//Physics
186 }//Mezzanine
187 
188 #endif