Spinning Topp Logo BlackTopp Studios
inc
softproxy.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 _softproxy_cpp
41 #define _softproxy_cpp
42 
43 #include "Physics/softproxy.h"
44 #include "Physics/physicsmanager.h"
45 
46 #include "enumerations.h"
47 #include "serialization.h"
48 #include "stringtool.h"
49 
50 //#include "Internal/meshtools.h.cpp"
51 
52 #include <btBulletDynamicsCommon.h>
53 #include <BulletSoftBody/btSoftRigidDynamicsWorld.h>
54 
55 namespace Mezzanine
56 {
57  namespace Physics
58  {
59  /*void _Update()
60  {
61  //Ogre::VertexData* vertexData = entity->getMesh()->getSubMesh(0)->vertexData;
62 
63  // const Ogre::VertexElement* posElem = vertexData->vertexDeclaration->findElementBySemantic(Ogre::VES_POSITION);
64  //Ogre::HardwareVertexBufferSharedPtr vBuffer = vertexData->vertexBufferBinding->getBuffer(posElem->getSource());
65 
66  //unsigned char* vertex = static_cast<unsigned char*>(vBuffer->lock(Ogre::HardwareBuffer::HBL_NORMAL));
67  //float* pReal = NULL;
68  //for (size_t j = 0; j < vertexData->vertexCount; j++ )
69  //{
70  // posElem->baseVertexPointerToElement(vertex, &pReal);
71  // *pReal++ = this->PhysicsSoftBody->m_nodes[j].m_x.x();
72  // *pReal++ = this->PhysicsSoftBody->m_nodes[j].m_x.y();
73  // *pReal++ = this->PhysicsSoftBody->m_nodes[j].m_x.z();
74  // vertex += vBuffer->getVertexSize();
75  //}
76  //vBuffer->unlock();
77 
78  //btVector3 position = this->PhysicsSoftBody->getWorldTransform().getOrigin();
79  //btQuaternion rotation = this->PhysicsSoftBody->getWorldTransform().getRotation();
80  btVector3 position = this->PhysicsSoftBody->m_clusters[0]->m_framexform.getOrigin();
81  btQuaternion rotation = this->PhysicsSoftBody->m_clusters[0]->m_framexform.getRotation();
82  this->GraphicsNode->setPosition(position.x(), position.y(), position.z());
83  this->GraphicsNode->setOrientation(rotation.w(), rotation.x(), rotation.y(), rotation.z());
84  }// */
85 
86 
87  SoftProxy::SoftProxy(const UInt32 ID, const Real Mass, PhysicsManager* Creator) :
88  CollidableProxy(ID,Creator),
89  PhysicsSoftBody(NULL)
90  {
91  MEZZ_EXCEPTION(ExceptionBase::NOT_IMPLEMENTED_EXCEPTION,"Soft body support is still under construction and not complete.");
92  this->CreateSoftObject(Mass);
93  }
94 
95  SoftProxy::SoftProxy(const XML::Node& SelfRoot, PhysicsManager* Creator) :
96  CollidableProxy(Creator),
97  PhysicsSoftBody(NULL)
98  {
99  MEZZ_EXCEPTION(ExceptionBase::NOT_IMPLEMENTED_EXCEPTION,"Soft body support is still under construction and not complete.");
100  this->CreateSoftObject(1);
101  this->ProtoDeSerialize(SelfRoot);
102  }
103 
105  {
106  if( this->IsInWorld() )
107  this->RemoveFromWorld();
108 
109  delete this->PhysicsSoftBody;
110  }
111 
113  {
114  /*Internal::MeshInfo CurMesh;
115  Internal::MeshTools::GetMeshVerticies(GraphicsObject,CurMesh);
116  Internal::MeshTools::GetMeshIndicies(GraphicsObject,CurMesh);
117  Internal::MeshTools::GetMeshNormals(GraphicsObject,CurMesh);
118  Internal::MeshTools::GetMeshTextures(GraphicsObject,CurMesh);
119  Internal::MeshTools::GetOtherMeshInfo(GraphicsObject,CurMesh);
120 
121  this->PhysicsSoftBody = btSoftBodyHelpers::CreateFromTriMesh(Entresol::GetSingletonPtr()->GetPhysicsManager()->_GetPhysicsWorldPointer()->getWorldInfo(), &CurMesh.Verticies[0].x, &CurMesh.Indicies[0], CurMesh.ICount/3);
122  PhysicsObject=PhysicsSoftBody;
123  PhysicsObject->setUserPointer( (WorldObject*)this );
124  PhysicsShape = PhysicsSoftBody->getCollisionShape();
125  PhysicsSoftBody->setTotalMass(mass, true);
126  PhysicsSoftBody->m_cfg.collisions = btSoftBody::fCollision::CL_SS + btSoftBody::fCollision::CL_RS;
127  PhysicsSoftBody->m_cfg.piterations = 5;
128  PhysicsSoftBody->randomizeConstraints();
129  PhysicsSoftBody->generateBendingConstraints(2);
130  PhysicsSoftBody->generateClusters(20);
131 
132  CreateManualMesh(CurMesh);
133 
134  this->GraphicsObject = Entresol::GetSingletonPtr()->GetSceneManager()->GetGraphicsWorldPointer()->createEntity(CurMesh.Name, CurMesh.Name + "M", CurMesh.Group);
135  Ogre::Any OgreRef( (WorldObject*)this );
136  GraphicsObject->setUserAny(OgreRef);
137 
138  this->PhysicsSoftBody->m_clusters[0]->m_collide = true;// */
139 
140  // CreateManualMesh
141  /*Ogre::ManualObject* ManualEntity = new Ogre::ManualObject(TheMesh.Name);
142  ManualEntity->setDynamic(true);
143  ManualEntity->estimateVertexCount(TheMesh.VCount);
144  ManualEntity->estimateIndexCount(TheMesh.ICount);
145 
146  ManualEntity->begin(TheMesh.Material, TheMesh.RendOp, TheMesh.Group);
147  for( int x=0 ; x < TheMesh.VCount ; x++ )
148  {
149  ManualEntity->position(TheMesh.Verticies[x]);
150  ManualEntity->normal(TheMesh.Normals[x]);
151  ManualEntity->textureCoord(TheMesh.Textures[x]);
152  }
153  for( int y=0 ; y < TheMesh.ICount ; y++ )
154  {
155  ManualEntity->index(TheMesh.Indicies[y]);
156  }
157  ManualEntity->end();
158  ManualEntity->convertToMesh(TheMesh.Name + "M", TheMesh.Group);
159  delete ManualEntity;// */
160  }
161 
162  ///////////////////////////////////////////////////////////////////////////////
163  // Utility
164 
166  {
167  return Mezzanine::PT_Physics_SoftProxy;
168  }
169 
171  {
172  if( !this->IsInWorld() ) {
173  this->Manager->_GetPhysicsWorldPointer()->addSoftBody( this->PhysicsSoftBody, this->CollisionGroup, this->CollisionMask );
174  }
175  }
176 
178  {
179  if( this->IsInWorld() ) {
180  this->Manager->_GetPhysicsWorldPointer()->removeSoftBody( this->PhysicsSoftBody );
181  }
182  }
183 
184  ///////////////////////////////////////////////////////////////////////////////
185  // Collision Settings
186 
188  {
189  // Do nothing. Soft bodies have their own shapes specific to the nodes and positions of the individual soft body.
190  }
191 
192  ///////////////////////////////////////////////////////////////////////////////
193  // Transform Methods
194 
196  {
197  //this->PhysicsSoftBody->m_clusters[0]->m_framexform.setOrigin(Location.GetBulletVector3());
198  }
199 
200  void SoftProxy::SetLocation(const Real X, const Real Y, const Real Z)
201  {
202 
203  }
204 
206  {
207  return Vector3(0,0,0);
208  //Vector3 temp(this->PhysicsSoftBody->m_clusters[0]->m_framexform.getOrigin());
209  //return temp;
210  }
211 
213  {
214  //this->PhysicsSoftBody->m_clusters[0]->m_framexform.setRotation(Rotation.GetBulletQuaternion(true));
215  }
216 
217  void SoftProxy::SetOrientation(const Real X, const Real Y, const Real Z, const Real W)
218  {
219 
220  }
221 
223  {
224  return Quaternion(0,0,0,1);
225  }
226 
227  void SoftProxy::SetScale(const Vector3& Sc)
228  {
229 
230  }
231 
232  void SoftProxy::SetScale(const Real X, const Real Y, const Real Z)
233  {
234 
235  }
236 
238  {
239  return Vector3(1,1,1);
240  }
241 
242  ///////////////////////////////////////////////////////////////////////////////
243  // Serialization
244 
245  void SoftProxy::ProtoSerialize(XML::Node& ParentNode) const
246  {
247  XML::Node SelfRoot = ParentNode.AppendChild(this->GetDerivedSerializableName());
248  if( !SelfRoot.AppendAttribute("InWorld").SetValue( this->IsInWorld() ? "true" : "false" ) ) {
249  SerializeError("Create XML Attribute Values",SoftProxy::GetSerializableName(),true);
250  }
251 
252  this->ProtoSerializeProperties(SelfRoot);
253  this->ProtoSerializeShape(SelfRoot);
254  }
255 
257  {
258 
259  }
260 
262  {
263 
264  }
265 
267  {
268  Boole WasInWorld = false;
269  XML::Attribute InWorldAttrib = SelfRoot.GetAttribute("InWorld");
270  if( !InWorldAttrib.Empty() ) {
271  WasInWorld = StringTools::ConvertToBool( InWorldAttrib.AsString() );
272  }
273 
274  this->ProtoDeSerializeProperties(SelfRoot);
275  this->ProtoDeSerializeShape(SelfRoot);
276 
277  if( WasInWorld ) {
278  this->AddToWorld();
279  }
280  }
281 
283  {
284 
285  }
286 
288  {
289 
290  }
291 
293  { return SoftProxy::GetSerializableName(); }
294 
296  { return "SoftProxy"; }
297 
298  ///////////////////////////////////////////////////////////////////////////////
299  // Internal Methods
300 
301  btSoftBody* SoftProxy::_GetPhysicsObject() const
302  { return this->PhysicsSoftBody; }
303 
304  btCollisionObject* SoftProxy::_GetBasePhysicsObject() const
305  { return this->PhysicsSoftBody; }
306  }//Physics
307 }//Mezzanine
308 
309 #endif
virtual Mezzanine::ProxyType GetProxyType() const
Accessor for the type of proxy.
Definition: softproxy.cpp:165
This is the base class for all collision shapes.
virtual void ProtoDeSeriailzeNodes(XML::Node &SelfRoot) const
Take the data stored in an XML Node and overwrite the nodes (and their specific properties) of this o...
Definition: softproxy.cpp:287
Attribute AppendAttribute(const Char8 *Name)
Creates an Attribute and puts it at the end of this Nodes attributes.
A light-weight handle for manipulating attributes in DOM tree.
Definition: attribute.h:74
virtual void ProtoDeSerializeShape(const XML::Node &SelfRoot)
Take the data stored in an XML Node and overwrite the shape of this object with it.
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: softproxy.cpp:282
bool Boole
Generally acts a single bit, true or false.
Definition: datatypes.h:173
virtual void ProtoSerializeProperties(XML::Node &SelfRoot) const
Convert the properties of this class to an XML::Node ready for serialization.
Definition: softproxy.cpp:256
virtual btCollisionObject * _GetBasePhysicsObject() const
Accessor for the internal physics object.
Definition: softproxy.cpp:304
PhysicsManager * Manager
This is a pointer to the physics manager that created and owns this proxy.
#define MEZZ_EXCEPTION(num, desc)
An easy way to throw exceptions with rich information.
Definition: exception.h:3048
Thrown when we just have not coded a thing yet, but we knew what the API should look like...
Definition: exception.h:117
virtual void ProtoSeriailzeNodes(XML::Node &SelfRoot) const
Convert the nodes (and their specific properties) of this class to an XML::Node ready for serializati...
Definition: softproxy.cpp:261
virtual String GetDerivedSerializableName() const
Gets the most derived serializable name of this WorldProxy.
Definition: softproxy.cpp:292
const Char8 * AsString(const Char8 *def="") const
Attempts to convert the value of the attribute to a String and returns the results.
virtual void CreateSoftObject(const Real Mass)
Used to create the physics representation of the soft body.
Definition: softproxy.cpp:112
virtual void SetLocation(const Vector3 &Loc)
Sets the location of this object in parent space.
Definition: softproxy.cpp:195
virtual Quaternion GetOrientation() const
Gets this objects current orientation.
Definition: softproxy.cpp:222
Any global enumerations shared between multiple classes is to be declared here.
virtual void RemoveFromWorld()
Unhooks this proxy from it's respective world.
Definition: softproxy.cpp:177
virtual void ProtoDeSerialize(const XML::Node &SelfRoot)
Take the data stored in an XML Node and overwrite this object with it.
Definition: softproxy.cpp:266
virtual void SetOrientation(const Quaternion &Ori)
Sets the orientation of this object in parent space.
Definition: softproxy.cpp:212
virtual void ProtoSerialize(XML::Node &ParentNode) const
Convert this class to an XML::Node ready for serialization.
Definition: softproxy.cpp:245
float Real
A Datatype used to represent a real floating point number.
Definition: datatypes.h:141
The interface for serialization.
bool SetValue(const Char8 *rhs)
Set the value of this.
virtual void SetScale(const Vector3 &Sc)
Sets the scaling to be applied to this object.
Definition: softproxy.cpp:227
Boole ConvertToBool(const String &ToConvert, const Boole Default)
Converts a string into a Boole.
Definition: stringtool.cpp:379
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
SoftProxy(const UInt32 ID, const Real Mass, PhysicsManager *Creator)
Class constructor.
Definition: softproxy.cpp:87
virtual void ProtoSerializeShape(XML::Node &SelfRoot) const
Convert the shape of this class to an XML::Node ready for serialization.
bool Empty() const
Is this storing anything at all?
btSoftBody * PhysicsSoftBody
SoftBody proxy used by the internal physics.
Definition: softproxy.h:60
static String GetSerializableName()
Get the name of the the XML tag the proxy class will leave behind as its instances are serialized...
Definition: softproxy.cpp:295
virtual ~SoftProxy()
Class destructor.
Definition: softproxy.cpp:104
btSoftRigidDynamicsWorld * _GetPhysicsWorldPointer()
This returns a pointer to the bullet physics world. This is for internal use only.
virtual void AddToWorld()
Performs all the necessary task to ensure this object is connected to it's respective world and ready...
Definition: softproxy.cpp:170
This is a proxy from which physics objects that can collide with each other are handled.
virtual btSoftBody * _GetPhysicsObject() const
Accessor for the internal soft body physics proxy.
Definition: softproxy.cpp:301
This is simply a place for storing all the Physics Related functions.
virtual Vector3 GetScale() const
Gets the scaling currently being applied to this object.
Definition: softproxy.cpp:237
This is used to represent a point in space, or a vector through space.
Definition: vector3.h:77
virtual void SetCollisionShape(CollisionShape *Shape)
Sets the collision shape to be used.
Definition: softproxy.cpp:187
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.
virtual Vector3 GetLocation() const
Gets this objects current location.
Definition: softproxy.cpp:205
Int16 CollisionMask
Stores the kind of World Objects that can collide with each other.
This is used to store information about rotation in 3d space.
Definition: quaternion.h:68
void SerializeError(const String &FailedTo, const String &ClassName, Boole SOrD)
Simply does some string concatenation, then throws an Exception.
Node AppendChild(NodeType Type=NodeElement)
Creates a Node and makes it a child of this one.
std::string String
A datatype used to a series of characters.
Definition: datatypes.h:159
Attribute GetAttribute(const Char8 *Name) const
Attempt to get an Attribute on this Node with a given name.