MezzanineEngine 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
generic6dofspringconstraint.cpp
1 // © Copyright 2010 - 2014 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  { }
60 
61  btGeneric6DofSpringConstraint* Generic6DofSpringConstraint::Generic6dofSpring() const
62  { return static_cast<btGeneric6DofSpringConstraint*>( this->Generic6dof ); }
63 
64  Generic6DofSpringConstraint::Generic6DofSpringConstraint(RigidProxy* ProxyA, RigidProxy* ProxyB, const Vector3& VectorA, const Vector3& VectorB, const Quaternion& QuaternionA, const Quaternion& QuaternionB, bool UseLinearReferenceA)
65  {
66  this->SetBodies(ProxyA,ProxyB);
67 
68  btTransform transa(QuaternionA.GetBulletQuaternion(), VectorA.GetBulletVector3());
69  btTransform transb(QuaternionB.GetBulletQuaternion(), VectorB.GetBulletVector3());
70  this->Generic6dof = new btGeneric6DofSpringConstraint(*(ProxA->_GetPhysicsObject()), *(ProxB->_GetPhysicsObject()), transa, transb, UseLinearReferenceA);
71  }
72 
73  Generic6DofSpringConstraint::Generic6DofSpringConstraint(RigidProxy* ProxyA, RigidProxy* ProxyB, const Transform& TransformA, const Transform& TransformB, bool UseLinearReferenceA)
74  {
75  this->SetBodies(ProxyA,ProxyB);
76 
77  this->Generic6dof = new btGeneric6DofSpringConstraint(*(ProxA->_GetPhysicsObject()), *(ProxB->_GetPhysicsObject()), TransformA.GetBulletTransform(), TransformB.GetBulletTransform(), UseLinearReferenceA);
78  }
79 
80 
82  {
83  if(this->Generic6dof)
84  {
85  delete this->Generic6dof;
86  this->Generic6dof = NULL;
87  }
88  }
89 
90  ////////////////////////////////////////////////////////////////////////////////
91  // Generic6DofSpringConstraint Linear Spring Settings
92 
94  { this->SetSpringStiffness(LinearX, Stiffies.X); this->SetSpringStiffness(LinearY, Stiffies.Y); this->SetSpringStiffness(LinearZ, Stiffies.Z); }
95 
97  { this->SetSpringDamping(LinearX, Damps.X); this->SetSpringDamping(LinearY, Damps.Y); this->SetSpringDamping(LinearZ, Damps.Z); }
98 
100  { this->SetSpringEnabled(LinearX, Enableness.X); this->SetSpringEnabled(LinearY, Enableness.Y); this->SetSpringEnabled(LinearZ, Enableness.Z); }
101 
104 
107 
110 
111  ////////////////////////////////////////////////////////////////////////////////
112  // Generic6DofSpringConstraint Angular Spring Settings
113 
115  { this->SetSpringStiffness(AngularX, Stiffies.X); this->SetSpringStiffness(AngularY, Stiffies.Y); this->SetSpringStiffness(AngularZ, Stiffies.Z); }
116 
118  { this->SetSpringDamping(AngularX, Damps.X); this->SetSpringDamping(AngularY, Damps.Y); this->SetSpringDamping(AngularZ, Damps.Z); }
119 
121  { this->SetSpringEnabled(AngularX, Enableness.X); this->SetSpringEnabled(AngularY, Enableness.Y); this->SetSpringEnabled(AngularZ, Enableness.Z); }
122 
125 
128 
131 
132  ////////////////////////////////////////////////////////////////////////////////
133  // Generic6DofSpringConstraint Per Axis Spring Settings
134 
136  { this->Generic6dofSpring()->setStiffness(Index, Stiffness); }
137 
139  { this->Generic6dofSpring()->setDamping(Index, Damping); }
140 
142  { this->Generic6dofSpring()->enableSpring(Index, Enable); }
143 
145  { return this->Generic6dofSpring()->getStiffness(Index); }
146 
148  { return this->Generic6dofSpring()->getDamping(Index); }
149 
151  { return this->Generic6dofSpring()->isSpringEnabled(Index); }
152 
153  ////////////////////////////////////////////////////////////////////////////////
154  // Generic6DofSpringConstraint Calculated Items
155 
157  { this->Generic6dofSpring()->setEquilibriumPoint(); }
158 
160  { this->Generic6dofSpring()->setEquilibriumPoint(Index); }
161 
164 
167 
169  { return this->Generic6dofSpring()->getEquilibriumPoint(Index); }
170 
171  ////////////////////////////////////////////////////////////////////////////////
172  // Generic6DofSpringConstraint Serialization
173 
175  {
176  XML::Node G6dofSpringNode = CurrentRoot.AppendChild(SerializableName()); // The base node all the base constraint stuff will go in
177  if (!G6dofSpringNode)
178  { SerializeError("Create G6dofSpringNode", SerializableName()); }
179 
180  Mezzanine::XML::Attribute Version = G6dofSpringNode.AppendAttribute("Version"); // Version
181  if (!Version)
182  { SerializeError("Create Version", SerializableName()); }
183  Version.SetValue(1);
184 
185  this->Generic6DofConstraint::ProtoSerialize(G6dofSpringNode); // Serialize the 6dof, dualtransform and the constraint
186 
187  XML::Node SpringStiffness = G6dofSpringNode.AppendChild("SpringStiffness");
188  if (!SpringStiffness)
189  { SerializeError("Create SpringStiffness", SerializableName()); }
190  XML::Node SpringDamping = G6dofSpringNode.AppendChild("SpringDamping");
191  if (!SpringDamping)
192  { SerializeError("Create SpringDamping", SerializableName()); }
193  XML::Node SpringEnabled = G6dofSpringNode.AppendChild("SpringEnabled");
194  if (!SpringEnabled)
195  { SerializeError("Create SpringEnabled", SerializableName()); }
196 
197 
198  for (int c=0; c<6; ++c) // Each of the spring attributes
199  {
200  String AttrName("Axis"+StringTools::ConvertToString(c));
201 
202  Mezzanine::XML::Attribute AxisStiffness = SpringStiffness.AppendAttribute(AttrName);
203  if (!AxisStiffness)
204  { SerializeError("Create AxisStiffness-"+AttrName, SerializableName()); }
205  AxisStiffness.SetValue(this->GetSpringStiffness(c));
206 
207  Mezzanine::XML::Attribute AxisDamping = SpringDamping.AppendAttribute(AttrName);
208  if (!AxisDamping)
209  { SerializeError("Create AxisDamping-"+AttrName, SerializableName()); }
210  AxisDamping.SetValue(this->GetSpringDamping(c));
211 
212  Mezzanine::XML::Attribute AxisEnabled = SpringEnabled.AppendAttribute(AttrName);
213  if (!AxisEnabled)
214  { SerializeError("Create AxisEnabled-"+AttrName, SerializableName()); }
215  AxisEnabled.SetValue(this->GetSpringEnabled(c));
216  }
217  }
218 
220  {
222  {
223  if(OneNode.GetAttribute("Version").AsInt() == 1)
224  {
225  XML::Node g6dof = OneNode.GetChild("Generic6DofConstraint");
226  if(!g6dof)
227  { DeSerializeError("locate Generic6DofConstraint node",SerializableName()); }
229 
230  XML::Node SpringStiffness = OneNode.GetChild("SpringStiffness");
231  if (!SpringStiffness)
232  { DeSerializeError("Find SpringStiffness Node", SerializableName()); }
233  XML::Node SpringDamping = OneNode.GetChild("SpringDamping");
234  if (!SpringDamping)
235  { DeSerializeError("Find SpringDamping Node", SerializableName()); }
236  XML::Node SpringEnabled = OneNode.GetChild("SpringEnabled");
237  if (!SpringEnabled)
238  { DeSerializeError("Find SpringEnabled Node", SerializableName()); }
239 
240  XML::Attribute SpringStiffnessAttr = SpringStiffness.GetFirstAttribute();
241  while(SpringStiffnessAttr)
242  {
243  String CurrentName(SpringStiffnessAttr.Name());
244  if (4>CurrentName.size())
245  { DeSerializeError("Invalid axis attribute", SerializableName()); }
246  this->SetSpringStiffness(char4ToAxis(CurrentName[4]),SpringStiffnessAttr.AsReal());
247  SpringStiffnessAttr = SpringStiffnessAttr.GetNextAttribute();
248  }
249 
250  XML::Attribute SpringDampingAttr = SpringDamping.GetFirstAttribute();
251  while(SpringDampingAttr)
252  {
253  String CurrentName(SpringDampingAttr.Name());
254  if (4>CurrentName.size())
255  { DeSerializeError("Invalid axis attribute", SerializableName()); }
256  this->SetSpringDamping(char4ToAxis(CurrentName[4]),SpringDampingAttr.AsReal());
257  SpringDampingAttr = SpringDampingAttr.GetNextAttribute();
258  }
259 
260  XML::Attribute SpringEnabledAttr = SpringEnabled.GetFirstAttribute();
261  while(SpringEnabledAttr)
262  {
263  String CurrentName(SpringEnabledAttr.Name());
264  if (4>CurrentName.size())
265  { DeSerializeError("Invalid axis attribute", SerializableName()); }
266  this->SetSpringEnabled(char4ToAxis(CurrentName[4]),SpringEnabledAttr.AsBool());
267  SpringEnabledAttr = SpringEnabledAttr.GetNextAttribute();
268  }
269  }else{
270  DeSerializeError("find usable serialization version",SerializableName());
271  }
272  }else{
273  DeSerializeError(String("find correct class to deserialize, found a ")+OneNode.Name(),SerializableName());
274  }
275  }
276 
278  { return String("Generic6DofSpringConstraint"); }
279  }//Physics
280 }//Mezzanine
281 
282 ///////////////////////////////////////////////////////////////////////////////
283 // Class External << Operators for streaming or assignment
284 
285 std::ostream& operator << (std::ostream& stream, const Mezzanine::Physics::Generic6DofSpringConstraint& x)
286 {
287  Mezzanine::Serialize(stream,x);
288  return stream;
289 }
290 
291 std::istream& operator >> (std::istream& stream, Mezzanine::Physics::Generic6DofSpringConstraint& x)
292  { return Mezzanine::DeSerialize(stream, x); }
293 
294 void operator >> (const Mezzanine::XML::Node& OneNode, Mezzanine::Physics::Generic6DofSpringConstraint& x)
295  { x.ProtoDeSerialize(OneNode); }
296 
297 #endif