Spinning Topp Logo BlackTopp Studios
inc
dualtransformconstraint.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 _physicsdualtransformconstraint_cpp
41 #define _physicsdualtransformconstraint_cpp
42 
43 #include "Physics/dualtransformconstraint.h"
44 #include "Physics/physicsmanager.h"
45 #include "Physics/rigidproxy.h"
46 
47 #include "stringtool.h"
48 #include "serialization.h"
49 
50 namespace Mezzanine
51 {
52  namespace Physics
53  {
55  Constraint(ID,Prox1,Creator)
56  { }
57 
59  Constraint(ID,Prox1,Prox2,Creator)
60  { }
61 
63  { }
64 
65  ///////////////////////////////////////////////////////////////////////////////
66  // Transform Methods
67 
69  { this->SetPivotATransform( TransA ); this->SetPivotBTransform( TransB ); }
70 
72  { this->SetPivotATransform( Transform(Location,this->GetPivotARotation()) ); }
73 
75  { this->SetPivotBTransform( Transform(Location,this->GetPivotBRotation()) ); }
76 
78  { return this->GetPivotATransform().Location; }
79 
81  { return this->GetPivotBTransform().Location; }
82 
84  { this->SetPivotATransform( Transform(this->GetPivotALocation(),Rotation) ); }
85 
87  { this->SetPivotBTransform( Transform(this->GetPivotBLocation(),Rotation) ); }
88 
90  { return this->GetPivotATransform().Rotation; }
91 
93  { return this->GetPivotBTransform().Rotation; }
94 
95  ///////////////////////////////////////////////////////////////////////////////
96  // Serialization
97 
99  {
100  XML::Node InitDataNode = SelfRoot.AppendChild( DualTransformConstraint::GetSerializableName() + "InitData" );
101 
102  if( InitDataNode.AppendAttribute("Version").SetValue("1") &&
103  ( this->ProxA != NULL ? InitDataNode.AppendAttribute("ProxyA-ID").SetValue( this->ProxA->GetProxyID() ) : false ) &&
104  ( this->ProxB != NULL ? InitDataNode.AppendAttribute("ProxyB-ID").SetValue( this->ProxB->GetProxyID() ) : false ) )
105  {
106  if( this->ProxA != NULL ) {
107  XML::Node PivotATransNode = InitDataNode.AppendChild("PivotATransform");
108  this->GetPivotATransform().ProtoSerialize( PivotATransNode );
109  }
110  if( this->ProxB != NULL ) {
111  XML::Node PivotBTransNode = InitDataNode.AppendChild("PivotBTransform");
112  this->GetPivotBTransform().ProtoSerialize( PivotBTransNode );
113  }
114 
115  return;
116  }else{
117  SerializeError("Create XML Attribute Values",DualTransformConstraint::GetSerializableName() + "InitData",true);
118  }
119  }
120 
122  {
123  this->DestroyConstraint();
124 
125  XML::Attribute CurrAttrib;
126  XML::Node InitDataNode = SelfRoot.GetChild( DualTransformConstraint::GetSerializableName() + "InitData" );
127 
128  if( !InitDataNode.Empty() ) {
129  if(InitDataNode.GetAttribute("Version").AsInt() == 1) {
130  Transform PivotA;
131  Transform PivotB;
132 
133  CurrAttrib = InitDataNode.GetAttribute("ProxyA-ID");
134  if( !CurrAttrib.Empty() )
135  this->ProxA = static_cast<RigidProxy*>( this->Manager->GetProxyByID( CurrAttrib.AsUint() ) );
136 
137  CurrAttrib = InitDataNode.GetAttribute("ProxyB-ID");
138  if( !CurrAttrib.Empty() )
139  this->ProxB = static_cast<RigidProxy*>( this->Manager->GetProxyByID( CurrAttrib.AsUint() ) );
140 
141  XML::Node PivotANode = InitDataNode.GetChild("PivotATransform").GetFirstChild();
142  if( !PivotANode.Empty() ) {
143  PivotA.ProtoDeSerialize(PivotANode);
144  }
145 
146  XML::Node PivotBNode = InitDataNode.GetChild("PivotBTransform").GetFirstChild();
147  if( !PivotBNode.Empty() ) {
148  PivotB.ProtoDeSerialize(PivotBNode);
149  }
150 
151  this->CreateConstraint(this->ProxA,this->ProxB,PivotA,PivotB);
152  }else{
153  MEZZ_EXCEPTION(ExceptionBase::INVALID_VERSION_EXCEPTION,"Incompatible XML Version for " + ( DualTransformConstraint::GetSerializableName() + "InitData" ) + ": Not Version 1.");
154  }
155  }else{
156  MEZZ_EXCEPTION(ExceptionBase::II_IDENTITY_NOT_FOUND_EXCEPTION,DualTransformConstraint::GetSerializableName() + "InitData" + " was not found in the provided XML node, which was expected.");
157  }
158  }
159 
161  { return "DualTransformConstraint"; }
162  }//Physics
163 }//Mezzanine
164 
165 #endif
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 Transform GetPivotATransform() const =0
Gets the current Rotation and Location of ProxyA.
virtual void SetPivotATransform(const Transform &TransA)=0
Sets the Position and Rotation for the first body using a Transform.
virtual UInt32 GetProxyID() const
Gets the unique ID of this proxy.
Definition: worldproxy.cpp:78
virtual void DestroyConstraint()=0
Destroys the internal constraint.
Thrown when the requested identity could not be found.
Definition: exception.h:94
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
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 CreateConstraint(RigidProxy *RigidA, RigidProxy *RigidB, const Transform &TransA, const Transform &TransB)=0
Creates the internal constraint.
void ProtoDeSerialize(const XML::Node &OneNode)
Take the data stored in an XML and overwrite this instance of this object with it.
Definition: transform.cpp:117
virtual void SetPivotBTransform(const Transform &TransB)=0
Sets the Position and Rotation for the second body using a Transform.
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
bool Empty() const
Is this storing anything at all?
The interface for serialization.
bool SetValue(const Char8 *rhs)
Set the value of this.
virtual void SetPivotBLocation(const Vector3 &Location)
Sets The relative location of the pivot from ProxB's Center of gravity.
static String GetSerializableName()
Get the name of the the XML tag the class will leave behind as its instances are serialized.
A light-weight handle for manipulating nodes in DOM tree.
Definition: node.h:89
Quaternion Rotation
The Rotation or relative rotation is stored in a Quaternion.
Definition: transform.h:71
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
bool Empty() const
Is this storing anything at all?
This is a proxy from which rigid body proxys are handled.
Definition: rigidproxy.h:102
Stores information about relative location and rotation in 3d space.
Definition: transform.h:62
virtual Vector3 GetPivotBLocation() const
Gets the location of the pivot relative to ProxB's Center of gravity.
virtual void SetPivotARotation(const Quaternion &Rotation)
Sets The relative rotation of ProxA.
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
virtual void ProtoDeSerializeInitData(const XML::Node &SelfRoot)
Take the data stored in an XML Node and initializes a new internal object with it.
virtual Transform GetPivotBTransform() const =0
Gets the current Rotation and Location of ProxyB.
Vector3 Location
The location or relative location is stored in a Vector3.
Definition: transform.h:68
virtual void SetPivotTransforms(const Transform &TransA, const Transform &TransB)
Sets the Position and Rotation for the first and second body using a Transform.
This is used to represent a point in space, or a vector through space.
Definition: vector3.h:77
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
DualTransformConstraint(const UInt32 ID, RigidProxy *Prox1, PhysicsManager *Creator)
Single body inheritance constructor.
void ProtoSerialize(XML::Node &CurrentRoot) const
Convert this class to an XML::Node ready for serialization.
Definition: transform.cpp:102
virtual Quaternion GetPivotBRotation() const
Gets the relative rotation for ProxB.
virtual void ProtoSerializeInitData(XML::Node &SelfRoot) const
Convert the data needed to initialize this class to an XML::Node ready for serialization.
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.
virtual void SetPivotALocation(const Vector3 &Location)
Sets The relative location of the pivot from ProxA's Center of gravity.
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.
virtual Quaternion GetPivotARotation() const
Gets the relative rotation for ProxA.
virtual void SetPivotBRotation(const Quaternion &Rotation)
Sets The relative rotation of ProxB.
Node GetChild(const Char8 *Name) const
Attempt to get a child Node with a given name.
virtual Vector3 GetPivotALocation() const
Gets the location of the pivot relative to ProxA's Center of gravity.