Spinning Topp Logo BlackTopp Studios
inc
ghostproxy.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 _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(ID,Creator),
59  PhysicsGhostBody(NULL)
60  {
61  this->CreateGhostObject();
62  }
63 
65  CollidableProxy(ID,Creator),
66  PhysicsGhostBody(NULL)
67  {
68  this->CreateGhostObject();
69  this->SetCollisionShape(Shape);
70  }
71 
72  GhostProxy::GhostProxy(const XML::Node& SelfRoot, PhysicsManager* Creator) :
73  CollidableProxy(Creator),
74  PhysicsGhostBody(NULL)
75  {
76  this->CreateGhostObject();
77  this->ProtoDeSerialize(SelfRoot);
78  }
79 
81  {
82  if( this->IsInWorld() )
83  this->RemoveFromWorld();
84 
85  delete this->PhysicsGhostBody;
86  }
87 
89  {
90  this->PhysicsGhostBody = new btPairCachingGhostObject();
91  this->PhysicsGhostBody->setCollisionFlags( this->PhysicsGhostBody->getCollisionFlags() | btCollisionObject::CF_NO_CONTACT_RESPONSE );
92  this->PhysicsGhostBody->setUserPointer( this );
93  }
94 
95  ///////////////////////////////////////////////////////////////////////////////
96  // Utility
97 
99  {
100  return Mezzanine::PT_Physics_GhostProxy;
101  }
102 
104  {
105  if( !this->IsInWorld() ) {
106  this->Manager->_GetPhysicsWorldPointer()->addCollisionObject( this->PhysicsGhostBody, this->CollisionGroup, this->CollisionMask );
107  }
108  }
109 
111  {
112  if( this->IsInWorld() ) {
113  this->Manager->_GetPhysicsWorldPointer()->removeCollisionObject( this->PhysicsGhostBody );
114  }
115  }
116 
117  ///////////////////////////////////////////////////////////////////////////////
118  // Overlapping Proxy Access
119 
121  {
122  btCollisionObject* OverlappingObject = this->PhysicsGhostBody->getOverlappingObject( static_cast<int>( Index ) );
123  return static_cast<CollidableProxy*>( OverlappingObject->getUserPointer() );
124  }
125 
127  {
128  return static_cast<UInt32>( this->PhysicsGhostBody->getNumOverlappingObjects() );
129  }
130 
132  {
133  btCollisionWorld* PhysWorld = this->Manager->_GetPhysicsWorldPointer();
134 
135  btBroadphasePairArray& PairArray = this->PhysicsGhostBody->getOverlappingPairCache()->getOverlappingPairArray();
136  const btBroadphasePair& CachePair = PairArray[Index];
137  btBroadphasePair* WorldPair = PhysWorld->getPairCache()->findPair(CachePair.m_pProxy0,CachePair.m_pProxy1);
138 
139  if( WorldPair != NULL )
140  {
141  btManifoldArray ManifoldArray;
142  ManifoldArray.clear();
143 
144  if( WorldPair->m_algorithm )
145  WorldPair->m_algorithm->getAllContactManifolds(ManifoldArray);
146 
147  for( int ManifoldIndex = 0 ; ManifoldIndex < ManifoldArray.size() ; ++ManifoldIndex )
148  {
149  btPersistentManifold* Manifold = ManifoldArray[ManifoldIndex];
150  for( int ContactIndex = 0 ; ContactIndex < Manifold->getNumContacts() ; ++ContactIndex )
151  {
152  // const btManifoldPoint& pt = Manifold->getContactPoint(ContactIndex);
153  //if( pt.m_distance1 > 0 )
154  // return NULL;
155 
156  btCollisionObject* ColObj = ( Manifold->getBody0() != this->PhysicsGhostBody ? (btCollisionObject*)(Manifold->getBody0()) : (btCollisionObject*)(Manifold->getBody1()) );
157  return static_cast<CollidableProxy*>( ColObj->getUserPointer() );
158  }
159  }
160  }
161  return NULL;
162  }
163 
165  {
166  btBroadphasePairArray& PairArray = this->PhysicsGhostBody->getOverlappingPairCache()->getOverlappingPairArray();
167  return static_cast<UInt32>( PairArray.size() );
168  }
169 
170  ///////////////////////////////////////////////////////////////////////////////
171  // Serialization
172 
174  { this->CollidableProxy::ProtoSerializeProperties(SelfRoot); }
175 
178 
180  { return GhostProxy::GetSerializableName(); }
181 
183  { return "GhostProxy"; }
184 
185  ///////////////////////////////////////////////////////////////////////////////
186  // Internal Methods
187 
188  btPairCachingGhostObject* GhostProxy::_GetPhysicsObject() const
189  { return this->PhysicsGhostBody; }
190 
191  btCollisionObject* GhostProxy::_GetBasePhysicsObject() const
192  { return this->PhysicsGhostBody; }
193  }//Physics
194 }//Mezzanine
195 
196 #endif
This is the base class for all collision shapes.
virtual void AddToWorld()
Performs all the necessary task to ensure this object is connected to it's respective world and ready...
Definition: ghostproxy.cpp:103
virtual UInt32 GetNumShapeOverlappingProxies() const
Gets the number of proxies overlapping with the actual collision shape of this ghost.
Definition: ghostproxy.cpp:164
virtual btPairCachingGhostObject * _GetPhysicsObject() const
Accessor for the internal ghost body physics proxy.
Definition: ghostproxy.cpp:188
virtual UInt32 GetNumAABBOverlappingProxies() const
Gets the number of proxies overlapping with the AABB of this ghost.
Definition: ghostproxy.cpp:126
PhysicsManager * Manager
This is a pointer to the physics manager that created and owns this proxy.
virtual void ProtoSerializeProperties(XML::Node &SelfRoot) const
Convert the properties of this class to an XML::Node ready for serialization.
Definition: ghostproxy.cpp:173
virtual void ProtoDeSerialize(const XML::Node &SelfRoot)
Take the data stored in an XML Node and overwrite this object with it.
Definition: worldproxy.cpp:114
virtual CollidableProxy * GetShapeOverlappingProxy(const UInt32 Index)
Gets a proxy overlapping with the collision shape of this ghost.
Definition: ghostproxy.cpp:131
virtual void ProtoDeSerializeProperties(const XML::Node &SelfRoot)
Take the data stored in an XML Node and overwrite the properties of this object with it...
Definition: ghostproxy.cpp:176
Any global enumerations shared between multiple classes is to be declared here.
virtual String GetDerivedSerializableName() const
Gets the most derived serializable name of this WorldProxy.
Definition: ghostproxy.cpp:179
static String GetSerializableName()
Get the name of the the XML tag the proxy class will leave behind as its instances are serialized...
Definition: ghostproxy.cpp:182
btPairCachingGhostObject * PhysicsGhostBody
GhostBody proxy used by the internal physics.
Definition: ghostproxy.h:60
Int16 CollisionGroup
The classifications pertaining to this object in regards to collisions.
A light-weight handle for manipulating nodes in DOM tree.
Definition: node.h:89
ProxyType
Used by all World proxies to describe what their derived types are.
Definition: enumerations.h:91
uint32_t UInt32
An 32-bit unsigned integer.
Definition: datatypes.h:126
virtual Mezzanine::ProxyType GetProxyType() const
Accessor for the type of proxy.
Definition: ghostproxy.cpp:98
virtual void CreateGhostObject()
Used to create the physics representation of the ghost body.
Definition: ghostproxy.cpp:88
virtual ~GhostProxy()
Class destructor.
Definition: ghostproxy.cpp:80
btSoftRigidDynamicsWorld * _GetPhysicsWorldPointer()
This returns a pointer to the bullet physics world. This is for internal use only.
virtual CollidableProxy * GetAABBOverlappingProxy(const UInt32 Index)
Gets a proxy overlapping with the AABB of this ghost.
Definition: ghostproxy.cpp:120
This is a proxy from which physics objects that can collide with each other are handled.
virtual void RemoveFromWorld()
Unhooks this proxy from it's respective world.
Definition: ghostproxy.cpp:110
This is simply a place for storing all the Physics Related functions.
virtual btCollisionObject * _GetBasePhysicsObject() const
Accessor for the internal physics object.
Definition: ghostproxy.cpp:191
The bulk of the engine components go in this namspace.
Definition: actor.cpp:56
virtual Boole IsInWorld() const
Gets whether or not this object is inside of it's world.
Int16 CollisionMask
Stores the kind of World Objects that can collide with each other.
virtual void SetCollisionShape(CollisionShape *Shape)
Sets the collision shape to be used.
virtual void ProtoSerializeProperties(XML::Node &SelfRoot) const
Convert the properties of this class to an XML::Node ready for serialization.
std::string String
A datatype used to a series of characters.
Definition: datatypes.h:159
GhostProxy(const UInt32 ID, PhysicsManager *Creator)
Class constructor.
Definition: ghostproxy.cpp:57
virtual void ProtoDeSerializeProperties(const XML::Node &SelfRoot)
Take the data stored in an XML Node and overwrite the properties of this object with it...