Spinning Topp Logo BlackTopp Studios
inc
generic6dofspringconstraint.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 _physicsgeneric6dofspringconstraint_cpp
41 #define _physicsgeneric6dofspringconstraint_cpp
42 
43 #include "Physics/generic6dofspringconstraint.h"
44 #include "Physics/rigidproxy.h"
45 
46 #include "stringtool.h"
47 #include "serialization.h"
48 
49 #include <btBulletDynamicsCommon.h>
50 
51 namespace Mezzanine
52 {
53  namespace Physics
54  {
55  ////////////////////////////////////////////////////////////////////////////////
56  // Generic6Dof Spring Constraint Functions
57 
59  Generic6DofConstraint(ID,Prox1,Creator)
60  { }
61 
63  Generic6DofConstraint(ID,Prox1,Prox2,Creator)
64  { }
65 
66  Generic6DofSpringConstraint::Generic6DofSpringConstraint(const UInt32 ID, RigidProxy* ProxyA, RigidProxy* ProxyB, const Transform& TransA, const Transform& TransB, PhysicsManager* Creator) :
67  Generic6DofConstraint(ID,ProxyA,ProxyB,Creator)
68  { this->CreateConstraint(ProxyA,ProxyB,TransA,TransB); }
69 
71  Generic6DofConstraint(0,NULL,Creator)
72  { this->ProtoDeSerialize(SelfRoot); }
73 
75  { /* Generic6Dof class will handle the cleanup */ }
76 
77  btGeneric6DofSpringConstraint* Generic6DofSpringConstraint::Generic6dofSpring() const
78  { return static_cast<btGeneric6DofSpringConstraint*>( this->Generic6dof ); }
79 
80  void Generic6DofSpringConstraint::CreateConstraint(RigidProxy* RigidA, RigidProxy* RigidB, const Transform& TransA, const Transform& TransB)
81  {
82  if( this->Generic6dof ) {
83  this->Generic6dof = new btGeneric6DofSpringConstraint(*(RigidA->_GetPhysicsObject()),*(RigidB->_GetPhysicsObject()),TransA.GetBulletTransform(),TransB.GetBulletTransform(),false);
84  }
85  }
86 
87  ////////////////////////////////////////////////////////////////////////////////
88  // Utility
89 
91  { this->Generic6dofSpring()->setEquilibriumPoint(); }
92 
93  ////////////////////////////////////////////////////////////////////////////////
94  // Linear Spring Settings
95 
97  { this->SetSpringStiffness(LinearX, Stiffness.X); this->SetSpringStiffness(LinearY, Stiffness.Y); this->SetSpringStiffness(LinearZ, Stiffness.Z); }
98 
101 
103  { this->Generic6dofSpring()->setStiffness( TranslationAxis, Stiffness ); }
104 
106  { return this->Generic6dofSpring()->getStiffness( TranslationAxis ); }
107 
109  { this->SetSpringDamping(LinearX, Damps.X); this->SetSpringDamping(LinearY, Damps.Y); this->SetSpringDamping(LinearZ, Damps.Z); }
110 
113 
115  { this->Generic6dofSpring()->setDamping( TranslationAxis, Damping ); }
116 
118  { return this->Generic6dofSpring()->getDamping( TranslationAxis ); }
119 
121  { this->SetSpringEnabled(LinearX, Enableness.X); this->SetSpringEnabled(LinearY, Enableness.Y); this->SetSpringEnabled(LinearZ, Enableness.Z); }
122 
125 
127  { this->Generic6dofSpring()->enableSpring( TranslationAxis, Enabled ); }
128 
130  { return this->Generic6dofSpring()->isSpringEnabled( TranslationAxis ); }
131 
134 
135  ////////////////////////////////////////////////////////////////////////////////
136  // Angular Spring Settings
137 
139  { this->SetSpringStiffness(AngularX, Stiffness.X); this->SetSpringStiffness(AngularY, Stiffness.Y); this->SetSpringStiffness(AngularZ, Stiffness.Z); }
140 
143 
145  { this->Generic6dofSpring()->setStiffness( ConvertAngularTo6Axis(RotationAxis), Stiffness ); }
146 
148  { return this->Generic6dofSpring()->getStiffness( ConvertAngularTo6Axis(RotationAxis) ); }
149 
151  { this->SetSpringDamping(AngularX, Damps.X); this->SetSpringDamping(AngularY, Damps.Y); this->SetSpringDamping(AngularZ, Damps.Z); }
152 
155 
157  { this->Generic6dofSpring()->setDamping( ConvertAngularTo6Axis(RotationAxis), Damping ); }
158 
160  { return this->Generic6dofSpring()->getDamping( ConvertAngularTo6Axis(RotationAxis) ); }
161 
163  { this->SetSpringEnabled(AngularX, Enableness.X); this->SetSpringEnabled(AngularY, Enableness.Y); this->SetSpringEnabled(AngularZ, Enableness.Z); }
164 
167 
169  { this->Generic6dofSpring()->enableSpring( ConvertAngularTo6Axis(RotationAxis), Enabled ); }
170 
172  { return this->Generic6dofSpring()->isSpringEnabled( ConvertAngularTo6Axis(RotationAxis) ); }
173 
176 
177  ////////////////////////////////////////////////////////////////////////////////
178  // Combined Linear and Angular Axis Spring Settings
179 
181  { this->Generic6dofSpring()->setStiffness(Index, Stiffness); }
182 
184  { return this->Generic6dofSpring()->getStiffness(Index); }
185 
187  { this->Generic6dofSpring()->setDamping(Index, Damping); }
188 
190  { return this->Generic6dofSpring()->getDamping(Index); }
191 
193  { this->Generic6dofSpring()->enableSpring(Index, Enable); }
194 
196  { return this->Generic6dofSpring()->isSpringEnabled(Index); }
197 
199  { this->Generic6dofSpring()->setEquilibriumPoint(Index); }
200 
202  { this->Generic6dofSpring()->setEquilibriumPoint(Index,Point); }
203 
205  { return this->Generic6dofSpring()->getEquilibriumPoint(Index); }
206 
207  ////////////////////////////////////////////////////////////////////////////////
208  // Serialization
209 
211  {
213 
214  XML::Node PropertiesNode = SelfRoot.AppendChild( Generic6DofSpringConstraint::GetSerializableName() + "Properties" );
215 
216  if( PropertiesNode.AppendAttribute("Version").SetValue("1") ) {
217  XML::Node LinearAxesNode = PropertiesNode.AppendChild("LinearAxes");
218  for( Whole LinAxis = Generic6DofConstraint::LinearStart ; LinAxis < Generic6DofConstraint::LinearEnd ; ++LinAxis )
219  {
220  XML::Node LinearAxisNode = LinearAxesNode.AppendChild("LinearAxis");
221  if( LinearAxisNode.AppendAttribute("Version").SetValue("1") &&
222  LinearAxisNode.AppendAttribute("Axis").SetValue( LinAxis ) &&
223  LinearAxisNode.AppendAttribute("SpringStiffness").SetValue( this->GetLinearSpringStiffnessOnAxis( LinAxis ) ) &&
224  LinearAxisNode.AppendAttribute("SpringDamping").SetValue( this->GetLinearSpringDampingOnAxis( LinAxis ) ) &&
225  LinearAxisNode.AppendAttribute("SpringEnabled").SetValue( this->GetLinearSpringEnabledOnAxis( LinAxis ) ) )
226  {
227  continue;
228  }else{
229  SerializeError("Create XML Attribute Values",Generic6DofSpringConstraint::GetSerializableName() + " Linear Axes",true);
230  }
231  }
232 
233  XML::Node AngularAxesNode = PropertiesNode.AppendChild("AngularAxes");
234  for( Whole AngAxis = Generic6DofConstraint::AngularStart ; AngAxis < Generic6DofConstraint::AngularEnd ; ++AngAxis )
235  {
236  XML::Node AngularAxisNode = AngularAxesNode.AppendChild("AngularAxis");
237  if( AngularAxisNode.AppendAttribute("Version").SetValue("1") &&
238  AngularAxisNode.AppendAttribute("Axis").SetValue( AngAxis ) &&
239  AngularAxisNode.AppendAttribute("SpringStiffness").SetValue( this->GetAngularSpringStiffnessOnAxis( AngAxis ) ) &&
240  AngularAxisNode.AppendAttribute("SpringDamping").SetValue( this->GetAngularSpringDampingOnAxis( AngAxis ) ) &&
241  AngularAxisNode.AppendAttribute("SpringEnabled").SetValue( this->GetAngularSpringEnabledOnAxis( AngAxis ) ) )
242  {
243  continue;
244  }else{
245  SerializeError("Create XML Attribute Values",Generic6DofSpringConstraint::GetSerializableName() + " Angular Axes",true);
246  }
247  }
248 
249  return;
250  }else{
251  SerializeError("Create XML Attribute Values",Generic6DofSpringConstraint::GetSerializableName() + "Properties",true);
252  }
253  }
254 
256  {
258 
259  XML::Attribute CurrAttrib;
260  XML::Node PropertiesNode = SelfRoot.GetChild( Generic6DofSpringConstraint::GetSerializableName() + "Properties" );
261 
262  if( !PropertiesNode.Empty() ) {
263  if( PropertiesNode.GetAttribute("Version").AsInt() == 1 ) {
264  XML::Node LinearAxesNode = PropertiesNode.GetChild("LinearAxes");
265  for( XML::NodeIterator LinearAxesNodeIt = LinearAxesNode.begin() ; LinearAxesNodeIt != LinearAxesNode.end() ; ++LinearAxesNodeIt )
266  {
267  if( !(*LinearAxesNodeIt).Empty() ) {
268  if( (*LinearAxesNodeIt).GetAttribute("Version").AsInt() == 1 ) {
269  Integer Axis = -1;
270 
271  CurrAttrib = (*LinearAxesNodeIt).GetAttribute("Axis");
272  if( !CurrAttrib.Empty() )
273  Axis = CurrAttrib.AsInt();
274 
275  if( Axis < 0 || Axis > 2 )
276  continue;
277 
278  CurrAttrib = (*LinearAxesNodeIt).GetAttribute("SpringStiffness");
279  if( !CurrAttrib.Empty() )
280  this->SetLinearSpringStiffnessOnAxis( CurrAttrib.AsReal(), Axis );
281 
282  CurrAttrib = (*LinearAxesNodeIt).GetAttribute("SpringDamping");
283  if( !CurrAttrib.Empty() )
284  this->SetLinearSpringDampingOnAxis( CurrAttrib.AsReal(), Axis );
285 
286  CurrAttrib = (*LinearAxesNodeIt).GetAttribute("SpringEnabled");
287  if( !CurrAttrib.Empty() )
288  this->SetLinearSpringEnabledOnAxis( CurrAttrib.AsBool(), Axis );
289  }
290  }
291  }
292 
293  XML::Node AngularAxesNode = PropertiesNode.GetChild("AngularAxes");
294  for( XML::NodeIterator AngularAxesNodeIt = AngularAxesNode.begin() ; AngularAxesNodeIt != AngularAxesNode.end() ; ++AngularAxesNodeIt )
295  {
296  if( !(*AngularAxesNodeIt).Empty() ) {
297  if( (*AngularAxesNodeIt).GetAttribute("Version").AsInt() == 1 ) {
298  Integer Axis = -1;
299 
300  CurrAttrib = (*AngularAxesNodeIt).GetAttribute("Axis");
301  if( !CurrAttrib.Empty() )
302  Axis = CurrAttrib.AsInt();
303 
304  if( Axis < 0 || Axis > 5 )
305  continue;
306 
307  Axis = this->ConvertFrom6AxisTo3Axis(Axis);
308 
309  CurrAttrib = (*AngularAxesNodeIt).GetAttribute("SpringStiffness");
310  if( !CurrAttrib.Empty() )
311  this->SetAngularSpringStiffnessOnAxis( CurrAttrib.AsReal(), Axis );
312 
313  CurrAttrib = (*AngularAxesNodeIt).GetAttribute("SpringDamping");
314  if( !CurrAttrib.Empty() )
315  this->SetAngularSpringDampingOnAxis( CurrAttrib.AsReal(), Axis );
316 
317  CurrAttrib = (*AngularAxesNodeIt).GetAttribute("SpringEnabled");
318  if( !CurrAttrib.Empty() )
319  this->SetAngularSpringEnabledOnAxis( CurrAttrib.AsBool(), Axis );
320  }
321  }
322  }
323  }else{
324  MEZZ_EXCEPTION(ExceptionBase::INVALID_VERSION_EXCEPTION,"Incompatible XML Version for " + ( Generic6DofSpringConstraint::GetSerializableName() + "Properties" ) + ": Not Version 1.");
325  }
326  }else{
327  MEZZ_EXCEPTION(ExceptionBase::II_IDENTITY_NOT_FOUND_EXCEPTION,Generic6DofSpringConstraint::GetSerializableName() + "Properties" + " was not found in the provided XML node, which was expected.");
328  }
329  }
330 
333 
335  { return "Generic6DofSpringConstraint"; }
336  }//Physics
337 }//Mezzanine
338 
339 #endif
virtual void SetLinearSpringStiffnessOnAxis(const Real Stiffness, Whole TranslationAxis)
Sets the stiffness on a specific linear axis on this constraint.
Attribute AppendAttribute(const Char8 *Name)
Creates an Attribute and puts it at the end of this Nodes attributes.
virtual void SetLinearSpringDampingOnAxis(const Real Damping, Whole TranslationAxis)
Sets the damping on a specific linear axis on this constraint.
virtual Boole GetLinearSpringEnabledOnAxis(Whole TranslationAxis) const
Get whether or not a spring is enabled on a specific linear axis on this constraint.
virtual Real GetSpringEquilibriumPoint(int Index) const
Gets the rest point of the spring on the specified axis.
virtual Vector3 GetLinearSpringEquilibriumPoints() const
Gets the rest point of the spring on each Linear axis.
A light-weight handle for manipulating attributes in DOM tree.
Definition: attribute.h:74
virtual void SetSpringDamping(int Index, Real Damping)
Set the spring Damping on a given axis.
bool Boole
Generally acts a single bit, true or false.
Definition: datatypes.h:173
virtual void CalculateSpringEquilibriumPoints()
Autogenerates the equalibrium points on each axis of this constraint.
virtual Vector3 GetAngularSpringEnabled() const
Get the Enabled Status for all Angular Axis.
bool AsBool(bool def=false) const
Attempts to convert the value of the attribute to a float and returns the results.
Real X
Coordinate on the X vector.
Definition: vector3.h:85
virtual Real GetLinearSpringStiffnessOnAxis(Whole TranslationAxis) const
Gets the stiffness on a specific linear axis on this constraint.
virtual void SetAngularSpringEnabled(const Vector3 &Enableness)
Set the Stiffness of the springs on each Angular Axis.
Real Z
Coordinate on the Z vector.
Definition: vector3.h:89
virtual void SetAngularSpringDamping(const Vector3 &Damps)
Set the Damping of the springs on each Angular Axis.
virtual Vector3 GetLinearSpringDamping() const
Get the Damping for all Linear Axis.
virtual Boole GetAngularSpringEnabledOnAxis(Whole RotationAxis) const
Get whether or not a spring is enabled on a specific angular axis on this constraint.
virtual void SetAngularSpringEnabledOnAxis(const Boole Enabled, Whole RotationAxis)
Set whether or not a spring is enabled on a specific angular axis on this constraint.
Thrown when the requested identity could not be found.
Definition: exception.h:94
#define MEZZ_EXCEPTION(num, desc)
An easy way to throw exceptions with rich information.
Definition: exception.h:3048
virtual void SetAngularSpringStiffness(const Vector3 &Stiffness)
Set the Stiffness of the springs on each Angular Axis.
int Integer
A datatype used to represent any integer close to.
Definition: datatypes.h:154
virtual void SetLinearSpringDamping(const Vector3 &Damps)
Set the Damping of the springs on each Linear Axis.
Thrown when a version is accessed/parsed/required and it cannot work correctly or is missing...
Definition: exception.h:112
virtual void ProtoSerializeProperties(XML::Node &SelfRoot) const
Convert the properties of this class to an XML::Node ready for serialization.
virtual Real GetSpringDamping(int Index) const
Retrieve the Damping of the spring on the given axis.
virtual void CalculateSpringEquilibriumPoint(int Index)
Automatically assigns the rest point of the spring on the specified axis based on the current offset ...
Create simple but specific limits on any axis of movement or rotation.
virtual void SetAngularSpringStiffnessOnAxis(const Real Stiffness, Whole RotationAxis)
Sets the stiffness on a specific angular axis on this constraint.
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 SetSpringEquilibriumPoint(int Index, const Real Point)
Sets the rest point of the spring on the specified axis.
bool Empty() const
Is this storing anything at all?
virtual void SetLinearSpringEnabled(const Vector3 &Enabled)
Set whether or not a spring is enabled on a specific linear axis on this constraint.
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 Real GetSpringStiffness(int Index) const
Retrieve the Stiffness of the spring on the given axis.
btTransform GetBulletTransform() const
Gets a Bullet Transform.
Definition: transform.cpp:71
virtual Real GetAngularSpringStiffnessOnAxis(Whole RotationAxis) const
Gets the stiffness on a specific angular axis on this constraint.
virtual void ProtoSerializeProperties(XML::Node &SelfRoot) const
Convert the properties of this class to an XML::Node ready for serialization.
Child node iterator (a bidirectional iterator over a collection of Node)
Definition: nodeiterator.h:77
virtual Vector3 GetAngularSpringDamping() const
Get the Damping for all Angular Axis.
virtual Vector3 GetAngularSpringStiffness() const
Get the Stiffness for all Angular Axis.
A light-weight handle for manipulating nodes in DOM tree.
Definition: node.h:89
iterator begin() const
Get a Child node iterator that references the first child Node.
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
iterator end() const
Get a Child node iterator that references one past the last child Node.
bool Empty() const
Is this storing anything at all?
btGeneric6DofConstraint * Generic6dof
The internal constraint that this class encapsulates.
virtual btRigidBody * _GetPhysicsObject() const
Accessor for the internal rigid body physics proxy.
Definition: rigidproxy.cpp:406
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 String GetDerivedSerializableName() const
Gets the most derived serializable name of this Constraint.
Real AsReal(Real def=0) const
Attempts to convert the value of the attribute to a Real and returns the results. ...
Real Y
Coordinate on the Y vector.
Definition: vector3.h:87
virtual void SetSpringStiffness(int Index, Real Stiffness)
Set the spring stiffness on a given axis.
virtual btGeneric6DofSpringConstraint * Generic6dofSpring() const
virtual void CreateConstraint(RigidProxy *RigidA, RigidProxy *RigidB, const Transform &TransA, const Transform &TransB)
Creates the internal constraint.
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 void SetLinearSpringStiffness(const Vector3 &Stiffness)
Set the Stiffness of the springs on each Linear Axis.
This is simply a place for storing all the Physics Related functions.
virtual Vector3 GetAngularSpringEquilibriumPoints() const
Gets the rest point of the spring on each Angular axis.
virtual Vector3 GetLinearSpringEnabled() const
Get the Enabled Status for all Linear Axis.
static String GetSerializableName()
Get the name of the the XML tag the class will leave behind as its instances are serialized.
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.
virtual Whole ConvertAngularTo6Axis(const Whole Axis) const
Convert an angular Axis from a 3 digit range to a full 6 digit range.
The bulk of the engine components go in this namspace.
Definition: actor.cpp:56
unsigned long Whole
Whole is an unsigned integer, it will be at least 32bits in size.
Definition: datatypes.h:151
virtual Real GetLinearSpringDampingOnAxis(Whole TranslationAxis) const
Gets the damping on a specific linear axis on this constraint.
virtual Vector3 GetLinearSpringStiffness() const
Get the Stiffness for all Linear Axis.
virtual void SetSpringEnabled(int Index, Boole Enable)
Set the spring's enabled status on a given axis.
virtual Real GetAngularSpringDampingOnAxis(Whole RotationAxis) const
Gets the damping on a specific angular axis on this constraint.
virtual void SetLinearSpringEnabledOnAxis(const Boole Enabled, Whole TranslationAxis)
Set whether or not a spring is enabled on a specific linear axis on this constraint.
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 Whole ConvertFrom6AxisTo3Axis(const Whole Axis) const
Convert an Axis from a full 6 digit range to a 3 digit range.
Attribute GetAttribute(const Char8 *Name) const
Attempt to get an Attribute on this Node with a given name.
Generic6DofSpringConstraint(const UInt32 ID, RigidProxy *Prox1, PhysicsManager *Creator)
Single body inheritance constructor.
virtual Boole GetSpringEnabled(int Index) const
Retrieve the EnabledStatus of the spring on the given axis.
virtual void SetAngularSpringDampingOnAxis(const Real Damping, Whole RotationAxis)
Sets the damping on a specific angular axis on this constraint.
Node GetChild(const Char8 *Name) const
Attempt to get a child Node with a given name.