Spinning Topp Logo BlackTopp Studios
inc
gearconstraint.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 _physicsgearconstraint_cpp
41 #define _physicsgearconstraint_cpp
42 
43 #include "Physics/gearconstraint.h"
44 #include "Physics/physicsmanager.h"
45 #include "Physics/rigidproxy.h"
46 
47 #include "stringtool.h"
48 #include "serialization.h"
49 
50 #include <btBulletDynamicsCommon.h>
51 
52 namespace Mezzanine
53 {
54  namespace Physics
55  {
56  GearConstraint::GearConstraint(const UInt32 ID, RigidProxy* ProxyA, RigidProxy* ProxyB, const Vector3& AxisA, const Vector3& AxisB, PhysicsManager* Creator) :
57  Constraint(ID,ProxyA,ProxyB,Creator)
58  { this->CreateConstraint(ProxyA,ProxyB,AxisA,AxisB); }
59 
60  GearConstraint::GearConstraint(const UInt32 ID, RigidProxy* ProxyA, RigidProxy* ProxyB, const Vector3& AxisA, const Vector3& AxisB, const Real Ratio, PhysicsManager* Creator) :
61  Constraint(ID,ProxyA,ProxyB,Creator)
62  { this->Gear = new btGearConstraint(*(this->ProxA->_GetPhysicsObject()), *(this->ProxB->_GetPhysicsObject()), AxisA.GetBulletVector3(), AxisB.GetBulletVector3(), Ratio); }
63 
65  Constraint(0,NULL,Creator)
66  { this->ProtoDeSerialize(SelfRoot); }
67 
69  { this->DestroyConstraint(); }
70 
71  void GearConstraint::CreateConstraint(RigidProxy* RigidA, RigidProxy* RigidB, const Vector3& AxisA, const Vector3& AxisB)
72  {
73  if( this->Gear == NULL ) {
74  this->Gear = new btGearConstraint(*(RigidA->_GetPhysicsObject()), *(RigidB->_GetPhysicsObject()), AxisA.GetBulletVector3(), AxisB.GetBulletVector3(), 0);
75  }
76  }
77 
79  {
80  this->EnableConstraint(false);
81  if( this->Gear != NULL ) {
82  delete this->Gear;
83  this->Gear = NULL;
84  }
85  this->ProxA = NULL;
86  this->ProxB = NULL;
87  }
88 
89  ///////////////////////////////////////////////////////////////////////////////
90  // Axis configuration
91 
93  { this->Gear->setAxisInA(Axis.GetBulletVector3()); }
94 
96  { this->Gear->setAxisInB(Axis.GetBulletVector3()); }
97 
99  { return Vector3( this->Gear->getAxisInA() ); }
100 
102  { return Vector3( this->Gear->getAxisInB() ); }
103 
104  ///////////////////////////////////////////////////////////////////////////////
105  // Ratio Configuration
106 
108  { this->Gear->setRatio(Ratio); }
109 
111  { return this->Gear->getRatio(); }
112 
113  ///////////////////////////////////////////////////////////////////////////////
114  // Parameter Configuration
115 
117  {
118  Constraint::ParamList Results;
119  return Results;
120  }
121 
123  {
124  Constraint::AxisList Results;
125  return Results;
126  }
127 
129  {
130  Constraint::AxisList Results;
131  return Results;
132  }
133 
135  {
136  return false;
137  }
138 
139  ///////////////////////////////////////////////////////////////////////////////
140  // Serialization
141 
143  {
144  XML::Node InitDataNode = SelfRoot.AppendChild( GearConstraint::GetSerializableName() + "InitData" );
145 
146  if( InitDataNode.AppendAttribute("Version").SetValue("1") &&
147  ( this->ProxA != NULL ? InitDataNode.AppendAttribute("ProxyA-ID").SetValue( this->ProxA->GetProxyID() ) : false ) &&
148  ( this->ProxB != NULL ? InitDataNode.AppendAttribute("ProxyB-ID").SetValue( this->ProxB->GetProxyID() ) : false ) )
149  {
150  if( this->ProxA != NULL ) {
151  XML::Node AxisANode = InitDataNode.AppendChild("AxisA");
152  this->GetAxisA().ProtoSerialize( AxisANode );
153  }
154  if( this->ProxB != NULL ) {
155  XML::Node AxisBNode = InitDataNode.AppendChild("AxisB");
156  this->GetAxisB().ProtoSerialize( AxisBNode );
157  }
158 
159  return;
160  }else{
161  SerializeError("Create XML Attribute Values",GearConstraint::GetSerializableName() + "InitData",true);
162  }
163  }
164 
166  {
167  this->Constraint::ProtoSerializeProperties(SelfRoot);
168 
169  XML::Node PropertiesNode = SelfRoot.AppendChild( GearConstraint::GetSerializableName() + "Properties" );
170 
171  if( PropertiesNode.AppendAttribute("Version").SetValue("1") &&
172  PropertiesNode.AppendAttribute("RotationRatio").SetValue( this->GetRotationRatio() ) )
173  {
174  return;
175  }else{
176  SerializeError("Create XML Attribute Values",GearConstraint::GetSerializableName() + "Properties",true);
177  }
178  }
179 
181  {
182  this->DestroyConstraint();
183 
184  XML::Attribute CurrAttrib;
185  XML::Node InitDataNode = SelfRoot.GetChild( GearConstraint::GetSerializableName() + "InitData" );
186 
187  if( !InitDataNode.Empty() ) {
188  if(InitDataNode.GetAttribute("Version").AsInt() == 1) {
189  Vector3 AxisA;
190  Vector3 AxisB;
191 
192  CurrAttrib = InitDataNode.GetAttribute("ProxyA-ID");
193  if( !CurrAttrib.Empty() )
194  this->ProxA = static_cast<RigidProxy*>( this->Manager->GetProxyByID( CurrAttrib.AsUint() ) );
195 
196  CurrAttrib = InitDataNode.GetAttribute("ProxyB-ID");
197  if( !CurrAttrib.Empty() )
198  this->ProxB = static_cast<RigidProxy*>( this->Manager->GetProxyByID( CurrAttrib.AsUint() ) );
199 
200  XML::Node AxisANode = InitDataNode.GetChild("AxisA").GetFirstChild();
201  if( !AxisANode.Empty() ) {
202  AxisA.ProtoDeSerialize(AxisANode);
203  }
204 
205  XML::Node AxisBNode = InitDataNode.GetChild("AxisB").GetFirstChild();
206  if( !AxisBNode.Empty() ) {
207  AxisB.ProtoDeSerialize(AxisBNode);
208  }
209 
210  this->CreateConstraint(this->ProxA,this->ProxB,AxisA,AxisB);
211  }else{
212  MEZZ_EXCEPTION(ExceptionBase::INVALID_VERSION_EXCEPTION,"Incompatible XML Version for " + ( GearConstraint::GetSerializableName() + "InitData" ) + ": Not Version 1.");
213  }
214  }else{
215  MEZZ_EXCEPTION(ExceptionBase::II_IDENTITY_NOT_FOUND_EXCEPTION,GearConstraint::GetSerializableName() + "InitData" + " was not found in the provided XML node, which was expected.");
216  }
217  }
218 
220  {
222 
223  XML::Attribute CurrAttrib;
224  XML::Node PropertiesNode = SelfRoot.GetChild( GearConstraint::GetSerializableName() + "Properties" );
225 
226  if( !PropertiesNode.Empty() ) {
227  if(PropertiesNode.GetAttribute("Version").AsInt() == 1) {
228  CurrAttrib = PropertiesNode.GetAttribute("RotationRatio");
229  if( !CurrAttrib.Empty() )
230  this->SetRotationRatio( CurrAttrib.AsReal() );
231  }else{
232  MEZZ_EXCEPTION(ExceptionBase::INVALID_VERSION_EXCEPTION,"Incompatible XML Version for " + ( GearConstraint::GetSerializableName() + "Properties" ) + ": Not Version 1.");
233  }
234  }else{
235  MEZZ_EXCEPTION(ExceptionBase::II_IDENTITY_NOT_FOUND_EXCEPTION,GearConstraint::GetSerializableName() + "Properties" + " was not found in the provided XML node, which was expected.");
236  }
237  }
238 
241 
243  { return "GearConstraint"; }
244 
245  ///////////////////////////////////////////////////////////////////////////////
246  // Internal Methods
247 
248  btTypedConstraint* GearConstraint::_GetConstraintBase() const
249  { return this->Gear; }
250  }//Physics
251 }//Mezzanine
252 
253 #endif
std::vector< int > AxisList
Used to Store lists of Int Axis for return types.
Definition: constraint.h:124
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 Vector3 GetAxisB() const
Gets the axis in ActorB's local space which will be manipulated.
bool Boole
Generally acts a single bit, true or false.
Definition: datatypes.h:173
virtual Constraint::AxisList GetValidLinearAxes() const
Get a sorted (low to high) list of all axis that operate linearly (that lock sliding/translation) ...
virtual void SetRotationRatio(const Real Ratio)
Sets the ratio at which ActorA's rotation will be applied to ActorB.
virtual UInt32 GetProxyID() const
Gets the unique ID of this proxy.
Definition: worldproxy.cpp:78
Thrown when the requested identity could not be found.
Definition: exception.h:94
virtual void ProtoSerializeInitData(XML::Node &SelfRoot) const
Convert the data needed to initialize this class to an XML::Node ready for serialization.
Node GetFirstChild() const
Get the first child Node of this Node.
#define MEZZ_EXCEPTION(num, desc)
An easy way to throw exceptions with rich information.
Definition: exception.h:3048
virtual void CreateConstraint(RigidProxy *RigidA, RigidProxy *RigidB, const Vector3 &AxisA, const Vector3 &AxisB)
Creates the internal constraint.
Thrown when a version is accessed/parsed/required and it cannot work correctly or is missing...
Definition: exception.h:112
CollidableProxy * GetProxyByID(const UInt32 ID) const
Gets the CollidableProxy via its ID.
virtual void ProtoDeSerializeInitData(const XML::Node &SelfRoot)
Take the data stored in an XML Node and initializes a new internal 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...
virtual void EnableConstraint(const Boole Enable)
Enables or disables this constraint.
PhysicsManager * Manager
This is a pointer to the physics manager that created and owns this constraint.
Definition: constraint.h:136
This is the base class for all constraints supported.
Definition: constraint.h:116
virtual void SetAxisA(const Vector3 &Axis)
Sets the axis in ActorA's local space which will translate to ActorB.
virtual void ProtoSerializeProperties(XML::Node &SelfRoot) const
Convert the properties of this class to an XML::Node ready for serialization.
bool Empty() const
Is this storing anything at all?
virtual Boole HasParamBeenSet(ConstraintParam Param, int Axis) const
virtual Constraint::ParamList GetValidParamsOnAxis(int Axis) const
Get a sorted (low to high) list of Parameters that are valid on this Axis.
virtual void SetAxisB(const Vector3 &Axis)
Sets the axis in ActorB's local space which will be manipulated.
float Real
A Datatype used to represent a real floating point number.
Definition: datatypes.h:141
GearConstraint(const UInt32 ID, RigidProxy *ProxyA, RigidProxy *ProxyB, const Vector3 &AxisA, const Vector3 &AxisB, PhysicsManager *Creator)
Double body constructor. Binds the two bodies.
The interface for serialization.
virtual ~GearConstraint()
Class destructor.
bool SetValue(const Char8 *rhs)
Set the value of this.
virtual void ProtoDeSerializeProperties(const XML::Node &SelfRoot)
Take the data stored in an XML Node and overwrite the properties of this object with it...
virtual void DestroyConstraint()
Destroys the internal constraint.
btVector3 GetBulletVector3() const
Gets a Bullet vector3.
Definition: vector3.cpp:555
A light-weight handle for manipulating nodes in DOM tree.
Definition: node.h:89
unsigned int AsUint(unsigned int def=0) const
Attempts to convert the value of the attribute to an unsigned int and returns the results...
int AsInt(int def=0) const
Attempts to convert the value of the attribute to an int and returns the results. ...
uint32_t UInt32
An 32-bit unsigned integer.
Definition: datatypes.h:126
btGearConstraint * Gear
Bullet constraint that this class encapsulates.
bool Empty() const
Is this storing anything at all?
virtual btRigidBody * _GetPhysicsObject() const
Accessor for the internal rigid body physics proxy.
Definition: rigidproxy.cpp:406
Real AsReal(Real def=0) const
Attempts to convert the value of the attribute to a Real and returns the results. ...
ConstraintParam
Used by constraints for setting some parameters.
Definition: constraint.h:61
virtual Constraint::AxisList GetValidAngularAxes() const
Get A list sorted (low to high) of all axis that operate Angularly (that lock sliding/translation) ...
static String GetSerializableName()
Get the name of the the XML tag the class will leave behind as its instances are serialized.
virtual Real GetRotationRatio() const
Gets the ratio at which ActorA's rotation will be applied to ActorB.
This is a proxy from which rigid body proxys are handled.
Definition: rigidproxy.h:102
std::vector< ConstraintParam > ParamList
Used to Store lists of param for return types.
Definition: constraint.h:120
virtual btTypedConstraint * _GetConstraintBase() const
Get the Bullet constraint that this class encapsulates.
virtual Vector3 GetAxisA() const
Gets the axis in ActorA's local space which will translate to ActorB.
This is simply a place for storing all the Physics Related functions.
RigidProxy * ProxA
The first Collidable this constraint applies to.
Definition: constraint.h:130
void ProtoDeSerialize(const XML::Node &OneNode)
Take the data stored in an XML and overwrite this instance of this object with it.
Definition: vector3.cpp:614
This is used to represent a point in space, or a vector through space.
Definition: vector3.h:77
virtual void ProtoDeSerialize(const XML::Node &SelfRoot)
Take the data stored in an XML Node and overwrite this object with it.
RigidProxy * ProxB
The second Collidable this constraint applies to.
Definition: constraint.h:133
The bulk of the engine components go in this namspace.
Definition: actor.cpp:56
virtual void ProtoSerializeProperties(XML::Node &SelfRoot) const
Convert the properties of this class to an XML::Node ready for serialization.
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
virtual String GetDerivedSerializableName() const
Gets the most derived serializable name of this Constraint.
Attribute GetAttribute(const Char8 *Name) const
Attempt to get an Attribute on this Node with a given name.
void ProtoSerialize(XML::Node &CurrentRoot) const
Convert this class to an XML::Node ready for serialization.
Definition: vector3.cpp:588
Node GetChild(const Char8 *Name) const
Attempt to get a child Node with a given name.