MezzanineEngine 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
point2pointconstraint.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 _physicspoint2pointconstraint_cpp
41 #define _physicspoint2pointconstraint_cpp
42 
43 #include "Physics/point2pointconstraint.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  // Point2PointConstraint Construction and Destruction
57 
58  Point2PointConstraint::Point2PointConstraint(RigidProxy* ProxyA, RigidProxy* ProxyB, const Vector3& PivotA, const Vector3& PivotB)
59  {
60  this->SetBodies(ProxyA,ProxyB);
61 
62  this->Point2Point = new btPoint2PointConstraint(*(ProxA->_GetPhysicsObject()), *(ProxB->_GetPhysicsObject()), PivotA.GetBulletVector3(), PivotB.GetBulletVector3());
63  }
64 
66  {
67  this->SetBodies(ProxyA);
68 
69  this->Point2Point = new btPoint2PointConstraint(*(ProxA->_GetPhysicsObject()), PivotA.GetBulletVector3());
70  }
71 
73  { delete this->Point2Point; }
74 
75  btTypedConstraint* Point2PointConstraint::GetConstraintBase() const
76  { return this->Point2Point; }
77 
78  ////////////////////////////////////////////////////////////////////////////////
79  // Point2PointConstraint Position and Orientation
80 
82  { this->Point2Point->setPivotA(PivotA.GetBulletVector3()); }
83 
85  { this->Point2Point->setPivotB(PivotB.GetBulletVector3()); }
86 
88  { return Vector3(this->Point2Point->getPivotInA()); }
89 
91  { return Vector3(this->Point2Point->getPivotInB()); }
92 
93  ////////////////////////////////////////////////////////////////////////////////
94  // Point2PointConstraint Specific Physics Settings
95 
97  { this->Point2Point->m_setting.m_impulseClamp = Clamping; }
98 
100  { return this->Point2Point->m_setting.m_impulseClamp; }
101 
103  { this->Point2Point->m_setting.m_damping = Damping; }
104 
106  { return this->Point2Point->m_setting.m_damping; }
107 
109  { this->Point2Point->m_setting.m_tau = TAU; }
110 
112  { return this->Point2Point->m_setting.m_tau; }
113 
115  {
116  Constraint::ParamList Results;
117  if(-1==Axis)
118  {
119  Results.push_back(Con_ERP);
120  Results.push_back(Con_Stop_ERP);
121  Results.push_back(Con_CFM);
122  Results.push_back(Con_Stop_CFM);
123  }
124  return Results;
125  }
126 
128  {
129  Constraint::AxisList Results;
130  Results.push_back(-1);
131  return Results;
132  }
133 
135  {
136  Constraint::AxisList Results;
137  return Results;
138  }
139 
141  {
142  //return this->Point2Point->hasParamBeenSet(Param,Axis);
143  // the logic here should match the logic in the source at http://bulletphysics.com/Bullet/BulletFull/btPoint2PointConstraint_8cpp_source.html#l00202
144  if (-1!=Axis)
145  { return false; }
146  return ( (Con_ERP==Param||Con_Stop_ERP==Param) && this->Point2Point->getFlags() & BT_P2P_FLAGS_ERP ) || //If we are checking erp OR we are checking stoperp AND the erp Flag is set OR
147  ( (Con_CFM==Param||Con_Stop_CFM==Param) && this->Point2Point->getFlags() & BT_P2P_FLAGS_CFM ) ; // we are checking cfm OR we are checking stopcfm AND the cfm Flag is set
148  }
149 
151  {
152  XML::Node P2PNode = CurrentRoot.AppendChild(SerializableName()); // The base node all the base constraint stuff will go in
153  if (!P2PNode)
154  { SerializeError("Create P2PNode", SerializableName()); }
155 
156  XML::Attribute VerAttr = P2PNode.AppendAttribute("Version");
157  XML::Attribute TauAttr = P2PNode.AppendAttribute("Tau");
158  XML::Attribute ClaAttr = P2PNode.AppendAttribute("ImpulseClamping");
159  XML::Attribute DamAttr = P2PNode.AppendAttribute("Damping");
160 
161  if( VerAttr && TauAttr && ClaAttr && DamAttr )
162  {
163  VerAttr.SetValue(1);
164  TauAttr.SetValue(this->GetTAU());
165  ClaAttr.SetValue(this->GetImpulseClamping());
166  DamAttr.SetValue(this->GetDamping());
167  }else{
168  SerializeError("Create P2PNode Attributes", SerializableName());
169  }
170 
171  XML::Node ActorANode = P2PNode.AppendChild("ActorA");
172  if (!ActorANode)
173  { SerializeError("Create ActorANode", SerializableName()); }
174  this->GetPivotALocation().ProtoSerialize(ActorANode);
175 
176  XML::Node ActorBNode = P2PNode.AppendChild("ActorB");
177  if (!ActorBNode)
178  { SerializeError("Create ActorBNode", SerializableName()); }
179  this->GetPivotBLocation().ProtoSerialize(ActorBNode);
180 
181  this->Constraint::ProtoSerialize(P2PNode);
182  }
183 
185  {
187  {
188  if(OneNode.GetAttribute("Version").AsInt() == 1)
189  {
190  this->Constraint::ProtoDeSerialize(OneNode.GetChild("Constraint"));
191 
192  this->SetTAU(OneNode.GetAttribute("Tau").AsReal());
193  this->SetImpulseClamping(OneNode.GetAttribute("ImpulseClamping").AsReal());
194  this->SetDamping(OneNode.GetAttribute("Damping").AsReal());
195 
196  XML::Node ActorANode = OneNode.GetChild("ActorA");
197  if(!ActorANode)
198  { DeSerializeError("Could not find ActorA position",SerializableName()); }
199 
200  XML::Node ActorBNode = OneNode.GetChild("ActorB");
201  if(!ActorBNode)
202  { DeSerializeError("Could not find ActorB position",SerializableName()); }
203 
204  Vector3 temp;
205  temp.ProtoDeSerialize(ActorANode.GetFirstChild());
206  SetPivotALocation(temp);
207  temp.ProtoDeSerialize(ActorBNode.GetFirstChild());
208  SetPivotBLocation(temp);
209 
210  }else{
211  DeSerializeError("find usable serialization version",SerializableName());
212  }
213  }else{
214  DeSerializeError(String("find correct class to deserialize, found a ")+OneNode.Name(),SerializableName());
215  }
216  }
217 
219  { return String("Point2PointConstraint"); }
220  }//Physics
221 }//Mezzanine
222 
223 
224 ///////////////////////////////////////////////////////////////////////////////
225 // Class External << Operators for streaming or assignment
226 
227 std::ostream& operator << (std::ostream& stream, const Mezzanine::Physics::Point2PointConstraint& x)
228 {
229  Mezzanine::Serialize(stream,x);
230  return stream;
231 }
232 
233 std::istream& operator >> (std::istream& stream, Mezzanine::Physics::Point2PointConstraint& x)
234  { return Mezzanine::DeSerialize(stream, x); }
235 
236 void operator >> (const Mezzanine::XML::Node& OneNode, Mezzanine::Physics::Point2PointConstraint& x)
237  { x.ProtoDeSerialize(OneNode); }
238 
239 #endif