Spinning Topp Logo BlackTopp Studios
inc
constraint.h
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 _physicsconstraint_h
41 #define _physicsconstraint_h
42 
43 #include "crossplatformexport.h"
44 #include "datatypes.h"
45 #include "quaternion.h"
46 #include "transform.h"
47 #include "vector3.h"
48 
49 class btRigidBody;
50 class btTypedConstraint;
51 
52 namespace Mezzanine
53 {
54  namespace Physics
55  {
56  class RigidProxy;
57  class PhysicsManager;
58 
59  /// @brief Used by constraints for setting some parameters.
60  /// @details See the constraint class documentation for more details.
62  {
63  Con_ERP = 1, ///< ERP values adjust how fast the errors in the constraints are reduced.
64  Con_Stop_ERP = 2,
65  Con_CFM = 3, ///< CFM values adds some small value to the main diagonal on the constraint matrix to prevent degenerate matrices.
66  Con_Stop_CFM = 4
67  };
68 
69  /// @brief Get a Constraint Parameter as a String.
70  /// @param Param The Parameter to get as a String.
71  /// @return A String That contains the name of a ConstraintParam.
73 
74  /// @brief Convert A string that might be ConstraintParam to one.
75  /// @param Param A String that should be storing, a constraint param name.
76  /// @return if the name matches on of the constraintParams exactly, return that one.
77  /// @throw If the Param does not match a type exactly, then this will throw an exception.
79 
80  /// @brief How many ConstraintParam Exist.
81  /// @details Used in some algorithms and we didn't want it to look like a magic so we defined it here.
82  const int ConstraintParamCount = 4;
83 
84  /// @internal
85  /// @brief used to help convert string to the axis they indicate
86  /// @param it it is the character that is passed in to indicate what the axis is. This should be the [4] character or the 5 character of the string.
87  /// @return this returns an int that indicates the Axis for the string.
88  int char4ToAxis(char it);
89 
90  ///////////////////////////////////////////////////////////////////////////////
91  /// @class Constraint
92  /// @brief This is the base class for all constraints supported.
93  /// @details This class provides the basis for all the other constraints. This is
94  /// also a virtual class and provides no constraint properties of it's own, thus
95  /// shouldn't/can't be called on manually. \n \n
96  /// Any class implementing a this must implement at least these functions to
97  /// create a minimally functiontal constraint: \n
98  /// - GetConstraintBase() const - Return A pointer to the bullet constraint they use
99  /// - SetPivotALocation(const Vector3&) - Set Location of pivot Relative to A
100  /// - SetPivotALocation(const Vector3&) - Set Location of pivot Relative to B
101  /// - GetPivotALocation() const - Get Location of pivot Relative to A
102  /// - GetPivotBLocation() const - Get Location of pivot Relative to B
103  /// - GetValidParamsOnAxis(int Axis) const - What parameters can be changed on the given Axis
104  /// - GetValidLinearAxes() const - Which Axis Suppport Translation
105  /// - GetValidAngularAxes() const - Which axis support Rotation
106  /// - HasParamBeenSet(ConstraintParam Param, int Axis) const - Has a the given param on the given axis been set
107  ///
108  /// It is advisable to re-implement a few more functions as well, these all have implementation writtens in terms of
109  /// of the function that must be implemented, but the genericity of them may impede performance.
110  /// - GetValidAxes() const - Full List of all valid Axis. Combines the lists from GetValidLinearAxes() const and GetValidAngularAxes() const.
111  /// - IsParamValidOnAxis(ConstraintParam, int) const - Uses the other function to check Axis and then check if the param is valid there
112  /// - GetValidParams() const - A list of Parameters every Axis on this constraint implements
113  ///
114  ///
115  ///////////////////////////////////////////////////////////////////////////////
117  {
118  public:
119  /// @brief Used to Store lists of param for return types
120  typedef std::vector<ConstraintParam> ParamList;
121  /// @brief Used to Store lists of Int Axis for return types
122  /// @details In general Axis will come in groups of three, such as 0,1,2, or 0,1,2,3,4,5 which could represent X,Y, and Z or multiple grousp of X,Y, and Z. These Axis
123  /// can represent Linear/Translation or Angular/Rotation information. Some Constraints support values that affect all constraints, this is usually represented a -1.
124  typedef std::vector<int> AxisList;
125  protected:
126  friend class PhysicsManager;
127 
128  /// @internal
129  /// @brief The first Collidable this constraint applies to.
131  /// @internal
132  /// @brief The second Collidable this constraint applies to.
134  /// @internal
135  /// @brief This is a pointer to the physics manager that created and owns this constraint.
137  /// @internal
138  /// @brief The unique ID used to identify this constraint.
140  /// @internal
141  /// @brief Whether or not collisions will be allowed to occure between the constrained bodies.
143  /// @internal
144  /// @brief Whether or not the constraint is currently taking effect.
146 
147  /// @brief Zero body constructor.
148  /// @remarks This should only ever be used for XML deserialization.
149  /// @param ID The unique identifier assigned to this constraint.
150  /// @param Creator A pointer to the manager that created this constraint.
151  Constraint(const UInt32 ID, PhysicsManager* Creator);
152  /// @brief Single body constructor.
153  /// @param ID The unique identifier assigned to this constraint.
154  /// @param Prox1 A pointer to the first/only proxy that will be constrained.
155  /// @param Creator A pointer to the manager that created this constraint.
156  Constraint(const UInt32 ID, RigidProxy* Prox1, PhysicsManager* Creator);
157  /// @brief Dual body constructor.
158  /// @param ID The unique identifier assigned to this constraint.
159  /// @param Prox1 A pointer to the first proxy that will be constrained.
160  /// @param Prox2 A pointer to the second proxy that will be constrained.
161  /// @param Creator A pointer to the manager that created this constraint.
162  Constraint(const UInt32 ID, RigidProxy* Prox1, RigidProxy* Prox2, PhysicsManager* Creator);
163  public:
164  /// @brief Class destructor.
165  virtual ~Constraint();
166 
167  ///////////////////////////////////////////////////////////////////////////////
168  // Utility
169 
170  /// @brief Gets the unique ID of this constraint.
171  /// @return Returns a UInt32 representing the unique ID of this constraint among all other constraints in it's parent manager.
172  virtual UInt32 GetConstraintID() const;
173 
174  /// @brief Enables or disables this constraint.
175  /// @param Enable Whether or not to make this constraint take effect.
176  virtual void EnableConstraint(const Boole Enable);
177  /// @brief Gets whether or not this constraint is enabled.
178  /// @return Returns true if this constraint is currently enabled, false otherwise.
179  virtual Boole IsConstraintEnabled() const;
180 
181  /// @brief Sets whether collisions between the two constrained bodies should occur.
182  /// @note If this is set while the constraint is enabled, it will have to be disabled and then re-enabled to take effect.
183  /// @param Allow Whether or not collisions will be allowed to occure between the constrained bodies.
184  virtual void SetAllowCollisions(const Boole Allow);
185  /// @brief Gets whether or not collisions can/will occur between the two constrained bodies.
186  /// @return Returns true if collisions are enabled between both bodies of this constraint, false otherwise.
187  virtual Boole GetAllowCollisions() const;
188 
189  /// @brief Gets the first Proxy this constraint applies to.
190  /// @return Returns a pointer to the first Proxy this constraint applies to.
191  virtual RigidProxy* GetProxyA() const;
192  /// @brief Gets the second Proxy this constraint applies to.
193  /// @return Returns a pointer to the second Proxy this constraint applies to.
194  virtual RigidProxy* GetProxyB() const;
195 
196  ///////////////////////////////////////////////////////////////////////////////
197  // Constraint Parameters
198 
199  /// @brief Get a sorted (low to high) list of Parameters that are valid on this Axis
200  /// @details Parameters returned from this will work on the given axis even if they are not valid on any other axis. There is no guarantee that the Parameters
201  /// will be uniquely stored per an axis. There is no guarantee that changing one parameter will not change another.
202  /// @param Axis the Axis to check.
203  /// @return A Paramlist with all the valid parameters for this axis.
204  virtual ParamList GetValidParamsOnAxis(int Axis) const = 0;
205  /// @brief Get a sorted (low to high) list of all axis that operate linearly (that lock sliding/translation)
206  /// @warning Odd behaviors, maybe even undefined behavior can happen if This returns a matching Axis to a Linear Axis. Any given axis should only be one or the other
207  /// @return An Axislist with the Axis this constraint linearly supports.
208  virtual AxisList GetValidLinearAxes() const = 0;
209  /// @brief Get A list sorted (low to high) of all axis that operate Angularly (that lock sliding/translation)
210  /// @warning Odd behaviors, maybe even undefined behavior can happen if This returns a matching Axis to a Linear Axis. Any given axis should only be one or the other
211  /// @return An Axislist with the Axis this constraint Angularly supports.
212  virtual AxisList GetValidAngularAxes() const = 0;
213  /// @brief Has the given Param on the Given Axis been set yet.
214  /// @param Param The parameter to Check
215  /// @param Axis The Axis on which to check the param
216  /// @details This will probably have to implement the same logic aas what is in the respective get/setparam function of each constraint http://bulletphysics.com/Bullet/BulletFull/classbtTypedConstraint.html#a11f44dd743b59bc05e25d500456e2559
217  /// @return True if it has been set, false if it has.
218  virtual Boole HasParamBeenSet(ConstraintParam Param, int Axis) const = 0;
219 
220  /// @brief Get a sorted (low to high) list of All Valid Axis
221  /// @details This is implemented using ValidLinearAxis and ValidAngularAxis, Derived versions of this class may wish to make a more specialized
222  /// implementation of this method that doesn't have the overhead of passing around 3 vectors by value.
223  /// @return An Axislist with all the Axis this constraint supports.
224  virtual AxisList GetValidAxes() const;
225  /// @brief Is a certain Parameter valid on a certain axis
226  /// @param Param The Parameter to Check
227  /// @param Axis The Axis to Check
228  /// @details This is implemented using ValidParamOnAxis, Derived versions of this class may wish to make a more specialized
229  /// implementation of this method.
230  /// @return True if Param is valid on Axis, and Axis is valid. If anything is invalid this returns false.
231  virtual Boole IsParamValidOnAxis(ConstraintParam Param, int Axis) const;
232  /// @brief Get A sorted (low to high) list of Parameters that are valid on all Axis
233  /// @details This is implemented using ValidAxis and ValidParamOnAxis, Derived versions of this class may wish to make a more specialized
234  /// implementation of this method that doesn't have the overhead of passing around many vectors by value and executing slow loops. Most
235  /// likely all of these constraint Parameter and axis functions could be replaced with some fairly simple switch statements and statically
236  /// coded values that are specific to the kind of constraint.
237  /// @return A Paramlist with the parameters valid on all axis.
238  virtual ParamList GetValidParams() const;
239 
240  /// @brief Provides override of constraint parameters.
241  /// @details Parameters such as ERP(Error Reduction Parameter) and CFM(Constraint Force Mixing) can be altered with this function. Optionally provide axis.
242  /// @param Param The parameter to override.
243  /// @param Value The new value for the parameter.
244  /// @param Axis Optional axis.
245  virtual void SetParam(ConstraintParam Param, Real Value, int Axis=-1);
246  /// @brief Gets value of constraint parameters.
247  /// @details See SetParam() for clarification. Gets information on constraint parameters.
248  /// @param Para, The parameter to get information for.
249  /// @param Axis Optional axis.
250  /// @return Returns the value for the requested parameter.
251  virtual Real GetParam(ConstraintParam Param, int Axis=-1) const;
252 
253  ///////////////////////////////////////////////////////////////////////////////
254  // Serialization
255 
256  /// @brief Convert this class to an XML::Node ready for serialization.
257  /// @param ParentNode The point in the XML hierarchy that all this renderable should be appended to.
258  virtual void ProtoSerialize(XML::Node& ParentNode) const;
259  /// @brief Convert the data needed to initialize this class to an XML::Node ready for serialization.
260  /// @param SelfRoot The root node containing all the serialized data for this instance.
261  virtual void ProtoSerializeInitData(XML::Node& SelfRoot) const = 0;
262  /// @brief Convert the properties of this class to an XML::Node ready for serialization.
263  /// @param SelfRoot The root node containing all the serialized data for this instance.
264  virtual void ProtoSerializeProperties(XML::Node& SelfRoot) const;
265  /// @brief Convert the global overrides of this class to an XML::Node ready for serialization.
266  /// @param SelfRoot The root node containing all the serialized data for this instance.
267  virtual void ProtoSerializeGlobalOverrides(XML::Node& SelfRoot) const;
268 
269  /// @brief Take the data stored in an XML Node and overwrite this object with it.
270  /// @param SelfRoot An XML::Node containing the data to populate this class with.
271  virtual void ProtoDeSerialize(const XML::Node& SelfRoot);
272  /// @brief Take the data stored in an XML Node and initializes a new internal object with it.
273  /// @param SelfRoot An XML::Node containing the data to populate this class with.
274  virtual void ProtoDeSerializeInitData(const XML::Node& SelfRoot) = 0;
275  /// @brief Take the data stored in an XML Node and overwrite the properties of this object with it.
276  /// @param SelfRoot An XML::Node containing the data to populate this class with.
277  virtual void ProtoDeSerializeProperties(const XML::Node& SelfRoot);
278  /// @brief Take the data stored in an XML Node and overwrite the global overrides of this object with it.
279  /// @warning Internally there is no way to unset a global variable. If you are calling this method without
280  /// first called "ProtoDeSerializeInitData" then you will have the previous global overrides in effect as well.
281  /// @param SelfRoot An XML::Node containing the data to populate this class with.
282  virtual void ProtoDeSerializeGlobalOverrides(const XML::Node& SelfRoot);
283 
284  /// @brief Gets the most derived serializable name of this Constraint.
285  /// @note When creating a new Constraint class verify this method has a valid return for it in order for serialization to work properly.
286  /// @return Returns the name of the XML tag from the most derived class of "this".
287  virtual String GetDerivedSerializableName() const = 0;
288  /// @brief Get the name of the the XML tag the class will leave behind as its instances are serialized.
289  /// @return A string containing the name of this class.
290  static String GetSerializableName();
291 
292  ///////////////////////////////////////////////////////////////////////////////
293  // Internal Methods
294 
295  /// @brief Get the Bullet constraint that this class encapsulates.
296  /// @return A pointer to the btTypedConstraint that stores the underlying constraint.
297  virtual btTypedConstraint* _GetConstraintBase() const = 0;
298  };// Constraint
299  }//Physics
300 }//Mezzanine
301 
302 ///////////////////////////////////////////////////////////////////////////////
303 // Class External << Operators for streaming or assignment
304 
305 #ifndef SWIG
306 /// @brief Convert a constraint to XML and send it out to a stream.
307 /// @param Stream The stream to send it out to.
308 /// @param Con The constraint to stream.
309 /// @return This returns the output stream to allow operator chaining.
310 std::ostream& MEZZ_LIB operator<< (std::ostream& Stream, const Mezzanine::Physics::Constraint* Con);
311 /// @brief Get a constraint from an XML stream.
312 /// @param Stream The stream to get it from.
313 /// @param Con The constraint to stream.
314 /// @return This returns the input stream to allow operator chaining.
315 std::istream& MEZZ_LIB operator>> (std::istream& Stream, Mezzanine::Physics::Constraint* Con);
316 /// @brief Serializes an XML node into a constraint.
317 /// @param OneNode The xml node that the constraint will be serialized to.
318 /// @param Con The constraint to serialize.
320 /// @brief Deserializes an XML node into a constraint.
321 /// @param OneNode The xml node that contains the class instance to be deserialized.
322 /// @param Con The constraint to deserialize to.
324 #endif
325 
326 #endif
std::ostream & operator<<(std::ostream &stream, const Mezzanine::LinearInterpolator< T > &Lint)
Used to Serialize an Mezzanine::LinearInterpolator to a human readable stream.
Definition: interpolator.h:433
std::vector< int > AxisList
Used to Store lists of Int Axis for return types.
Definition: constraint.h:124
ConstraintParam StringAsConstraintParam(String Param)
Convert A string that might be ConstraintParam to one.
CFM values adds some small value to the main diagonal on the constraint matrix to prevent degenerate ...
Definition: constraint.h:65
const int ConstraintParamCount
How many ConstraintParam Exist.
Definition: constraint.h:82
bool Boole
Generally acts a single bit, true or false.
Definition: datatypes.h:173
This file is used on some platforms to determine what data should be read and written to and from a s...
UInt32 ConstraintID
The unique ID used to identify this constraint.
Definition: constraint.h:139
All the definitions for datatypes as well as some basic conversion functions are defined here...
Boole Enabled
Whether or not the constraint is currently taking effect.
Definition: constraint.h:145
String ConstraintParamAsString(ConstraintParam Param)
Get a Constraint Parameter as a String.
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
int char4ToAxis(char it)
used to help convert string to the axis they indicate
float Real
A Datatype used to represent a real floating point number.
Definition: datatypes.h:141
A light-weight handle for manipulating nodes in DOM tree.
Definition: node.h:89
uint32_t UInt32
An 32-bit unsigned integer.
Definition: datatypes.h:126
ConstraintParam
Used by constraints for setting some parameters.
Definition: constraint.h:61
The defintion of the transform is stored in this file.
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
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
#define MEZZ_LIB
Some platforms require special decorations to denote what is exported/imported in a share library...
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
Boole AllowCollisions
Whether or not collisions will be allowed to occure between the constrained bodies.
Definition: constraint.h:142
std::istream & operator>>(std::istream &stream, Mezzanine::LinearInterpolator< T > &Lint)
Used to de-serialize an Mezzanine::LinearInterpolator from a stream.
Definition: interpolator.h:448
ERP values adjust how fast the errors in the constraints are reduced.
Definition: constraint.h:63
std::string String
A datatype used to a series of characters.
Definition: datatypes.h:159