Spinning Topp Logo BlackTopp Studios
inc
collisionshape.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 _collisionshape_h
41 #define _collisionshape_h
42 
43 #include "datatypes.h"
44 #ifndef SWIG
45  #include "serialization.h"
46  #include "transform.h"
47 #endif
48 
49 class btCollisionShape;
50 
51 namespace Mezzanine
52 {
53  namespace Physics
54  {
55  class CollisionShapeManager;
56  ///////////////////////////////////////////////////////////////////////////////
57  /// @class CollisionShape
58  /// @headerfile collisionshape.h
59  /// @brief This is the base class for all collision shapes.
60  /// @details Currently there are a total of 13 collision shape classes inheriting from
61  /// 3 other base classes. Collision shapes are shape representations for Actors, AreaEffects,
62  /// and other classes with bodies in the physics engine. @n @n
63  /// It's important to note that Collision shapes can be created and then re-used in as many World
64  /// objects(at the same time) as you need, and it is encouraged to do this.
65  ///////////////////////////////////////
67  {
68  public:
69  /// @brief This enum describes what kind of shape you are currently working with.
70  /// @note These are number primarily for Serialization purposes. These corresponding numbers could vary wildly. Any use of corresponding raw number in serialization will be done with object serialization version in mind.
71  enum ShapeType
72  {
73  ST_Box = 0, ///< Indicates the class is a BoxCollisionShape
74  ST_Capsule = 1, ///< Indicates the class is a CapsuleCollisionShape
75  ST_Compound = 2, ///< Indicates the class is a CompoundCollisionShape
76  ST_Cone = 3, ///< Indicates the class is a ConeCollisionShape
77  ST_ConvexHull = 4, ///< Indicates the class is a ConvexHullCollisionShape
78  ST_Cylinder = 5, ///< Indicates the class is a CylinderCollisionShape
79  ST_MultiSphere = 6, ///< Indicates the class is a MultiSphereCollisionShape
80  ST_Sphere = 7, ///< Indicates the class is a SphereCollisionShape
81  ST_DynamicTriMesh = 8, ///< Indicates the class is a DynamicMeshCollisionShape
82  ST_Heightfield = 9, ///< Indicates the class is a HeightfieldCollisionShape
83  ST_Plane = 10, ///< Indicates the class is a PlaneCollisionShape
84  ST_Soft = 11, ///< Indicates the class is a SoftCollisionShape
85  ST_StaticTriMesh = 12 ///< Indicates the class is a StaticMeshCollisionShape
86  };
87  protected:
88  /// @brief A pointer to the bullet collision this uses.
89  btCollisionShape* ShapeBase;
90  /// @brief Storage for the name of this class instance.
92  public:
93  /// @brief Class Constructor.
95  /// @brief Class Destructor.
96  virtual ~CollisionShape();
97 
98  ///////////////////////////////////////////////////////////////////////////////
99  // Utility
100 
101  /// @brief Gets the name of this shape.
102  /// @return Returns a const reference string containing the name of this collision shape.
103  virtual const String& GetName() const;
104  /// @brief Gets the type of Collision shape this is.
105  /// @return Returns an enum value indicating the type of collision shape this is.
106  virtual CollisionShape::ShapeType GetType() const = 0;
107 
108  ///////////////////////////////////////////////////////////////////////////////
109  // Configuration Methods
110 
111  /// @brief Sets the padding that will be applied when checking for collisions.
112  /// @param Margin A real in world units representing how much padding is to be applied to this shape.
113  virtual void SetMargin(const Real& Margin);
114  /// @brief Gets the amount of padding currently being applied to the collision shape.
115  /// @return Returns the amount of padding, in world units, is being applied to the collision shape.
116  virtual Real GetMargin() const;
117  /// @brief Scales the collision shape on each of it's axes.
118  /// @param Scaling A vector3 representing how much scaling should be applied on each of the shapes 3 axes.
119  virtual void SetScaling(const Vector3& Scaling);
120  /// @brief Gets the current scaling being applied to the collision shape.
121  /// @return Returns a vector3 representing the amount of scaling being applied to the shape.
122  virtual Vector3 GetScaling() const;
123 
124  ///////////////////////////////////////////////////////////////////////////////
125  // Serialization
126 
127  /// @brief Convert this class to an XML::Node ready for serialization
128  /// @param CurrentRoot The point in the XML hierarchy that all this collision shape should be appended to.
129  virtual void ProtoSerialize(XML::Node& CurrentRoot) const;
130  /// @brief Take the data stored in an XML and overwrite this instance of this object with it
131  /// @param OneNode and XML::Node containing the data.
132  /// @warning A precondition of using this is that all of the actors intended for use must already be Deserialized.
133  virtual void ProtoDeSerialize(const XML::Node& OneNode);
134  /// @brief Get the name of the the XML tag this class will leave behind as its instances are serialized.
135  /// @return A string containing "CollisionShape"
136  static String GetSerializableName();
137 
138  ///////////////////////////////////////////////////////////////////////////////
139  // Internal Methods
140 
141  /// @internal
142  /// @brief Sets the name of this collision shape.
143  /// @remarks This method should be used with extreme care when it is stored by the collision shape manager.
144  /// @param NewName The new name to be given to this shape.
145  virtual void _SetShapeName(const String& NewName);
146  /// @internal
147  /// @brief Gets the internal shape pointer this collision shape is based on.
148  /// @return Returns a pointer to the internal collision shape.
149  virtual btCollisionShape* _GetInternalShape() const;
150  };//CollisionShape
151 
152  ///////////////////////////////////////////////////////////////////////////////
153  // Utility Functions
154  ///////////////////////////////////////
155  /// @internal
156  /// @brief Create A shape of a type and optionally model it after an example.
157  /// @param ShapeToCreate The Type of Shape to create.
158  /// @param Name the name of the new shape.
159  /// @param ShapeToModel An optional pointer to a bullet shape to include instead of creating one.
160  /// @return A new Mezzanine::CollisionShape with either default Values or the values of the bullet shape if one is provided.
161  /// @warning The only checking done on the bullet shape is to verify it is not a null pointer. If the Internal shape fails to be of the appropriate kind correlating to ShapeToCreate then *undefined behavior* will occur.
162  CollisionShape* MEZZ_LIB CreateShape(CollisionShape::ShapeType ShapeToCreate, const String& Name_, btCollisionShape* ShapeToModel);
163  /// @brief Create a CollisionShape from a snippet of xml
164  /// @param OneNode A Node for any Collision Shape that can be instanstiated
165  /// @return A pointer to a CollisionShape of the Correct Type with the values stored in the XML.
167  /// @internal
168  /// @brief Convert from a Internal Collision Shape to a CollisionShape::ShapeType
169  /// @param BulletShapeType The ShapeType to Convert
170  /// @return The corresponding CollisionShape::ShapeType to the passed in bullet Shape Type if asuitable match exists
171  /// @throw The Mezzanine engine only uses a subset of Bullets shapes, a Mezzanine::Exception with be thrown in the event an unsupported one is passed in.
173  /// @brief Get a string suitable for human eyes from a CollisionShape::ShapeType, may not be suitable for endusers.
174  /// @param ShapeToConvert The kind of shape you want a string for.
175  /// @return A String with human readable contents corresponding to the passed in type.
177  /// @brief Convert a human readable string (as provided by ShapeTypeToString()) and convert it to a CollisionShape::ShapeType
178  /// @param TypeName A String that matches exactly what returns from ShapeTypeToString().
179  /// @return A valid CollisionShape::ShapeType if possible
180  /// @throw This throws a Mezzanine::Exception in the event of a gibberish name.
182 
183  ///////////////////////////////////////////////////////////////////////////////
184  /// @class CollisionShapeDeSerializer
185  /// @brief A tool to aid in deserialization for the specific instances that DeSerialization CollisionShapes other ways does not make sense
186  ///////////////////////////////////////
187  class MEZZ_LIB CollisionShapeDeSerializer : public DeSerializer <CollisionShape>
188  {
189  protected:
190  /// @internal
191  /// @brief This Performs the work of Deserializing that DeSerialize and DeSerializeAndRetrieve need to do
192  /// @param Stream the stream to deserialize from.
193  /// @return a pointer to the shape just created, this may or may not be added to the collision shape manager depending on implementation details
194  virtual CollisionShape* PerformDeSerialization(std::istream& Stream);
195  public:
196  /// @brief Convert An XML Node containing and one collision shape into a CollisionShape of the corresponding type
197  /// @param OneNode A reference to the XML node to reconstitute into a live class instance.
198  /// @details All items deserialized here will be added to the collision shape manager.
199  /// @return A pointer to the freshly deserialized and created class instance.
200  virtual CollisionShape* ProtoDeSerialize(const XML::Node& OneNode);
201  /// @brief Create a collision shape from the serialized version in a stream.
202  /// @param Stream The std::istream to get the data from.
203  /// @details This performs less checking than the original to allow for DeSerialization of multiple kinds
204  /// of xml elements. Rather all the specific checking is done closer to the actual instantion of classes.
205  /// This add the DeSerialized shape to the collsion shape manager.
206  /// @return This returns the input stream after the xml document has been extracted from it.
207  virtual std::istream& DeSerialize(std::istream& Stream);
208  /// @brief Create a collision shape from the serialized version in a stream.
209  /// @param Stream The std::istream to get the data from.
210  /// @details This adds the DeSerialized shape to the collsion shape manager.
211  /// @return This returns a pointer to the freshly created collsion shape
212  virtual CollisionShape* DeSerializeAndRetrieve(std::istream& Stream);
213  /// @brief This will return the Name of the element that Contains multiple of the items to be DeSerialized
214  /// @return A String containing "Shapes"
215  virtual String ContainerName() const;
216  };// CollisionShapeDeSerializer
217  }//Physics
218 }//Mezzanine
219 
220 #ifndef SWIG
221 /// @brief Serialize an CollisionShape and send it to a stream
222 /// @param ShapeToSerialize The CollisionShape serialize
223 /// @param stream the std::ostream to send the CollisionShape xml to.
224 /// @return The ostream after the new data has been inserted.
225 std::ostream& MEZZ_LIB operator << (std::ostream& stream, const Mezzanine::Physics::CollisionShape& ShapeToSerialize);
226 /// @brief Get an actor from an XML stream.
227 /// @param stream The stream to get it out of.
228 /// @param x The it you will get out of the stream.
229 /// @return This returns the input stream to allow operator chaining.
230 std::istream& MEZZ_LIB operator >> (std::istream& stream, Mezzanine::Physics::CollisionShape& x);
231 /// @brief Converts an XML Node into a functional in memory construct.
232 /// @param OneNode The xml node that contains the deserialize class instance.
233 /// @param x The class instance to overwrite witht the proto serialized version in the node.
235 #endif
236 
237 
238 #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
This is the base class for all collision shapes.
CollisionShape * CreateShape(CollisionShape::ShapeType ShapeToCreate, const String &Name_, btCollisionShape *ShapeToModel)
Create A shape of a type and optionally model it after an example.
All the definitions for datatypes as well as some basic conversion functions are defined here...
String ShapeTypeToString(CollisionShape::ShapeType ShapeToConvert)
Get a string suitable for human eyes from a CollisionShape::ShapeType, may not be suitable for enduse...
float Real
A Datatype used to represent a real floating point number.
Definition: datatypes.h:141
The interface for serialization.
A light-weight handle for manipulating nodes in DOM tree.
Definition: node.h:89
ShapeType
This enum describes what kind of shape you are currently working with.
The defintion of the transform is stored in this file.
std::istream & DeSerialize(std::istream &Stream, T &Converted)
Deserialize the next xml tag in the stream into a specific in memory class instance.
A tool for deserializing classes with specific issues deserializing them.
This is used to represent a point in space, or a vector through space.
Definition: vector3.h:77
#define MEZZ_LIB
Some platforms require special decorations to denote what is exported/imported in a share library...
A tool to aid in deserialization for the specific instances that DeSerialization CollisionShapes othe...
String Name
Storage for the name of this class instance.
The bulk of the engine components go in this namspace.
Definition: actor.cpp:56
std::istream & operator>>(std::istream &stream, Mezzanine::LinearInterpolator< T > &Lint)
Used to de-serialize an Mezzanine::LinearInterpolator from a stream.
Definition: interpolator.h:448
btCollisionShape * ShapeBase
A pointer to the bullet collision this uses.
CollisionShape::ShapeType StringToShapeType(const String &TypeName)
Convert a human readable string (as provided by ShapeTypeToString()) and convert it to a CollisionSha...
CollisionShape::ShapeType InternalShapeTypeToShapeType(int InternalShapeType)
Convert from a Internal Collision Shape to a CollisionShape::ShapeType.
std::string String
A datatype used to a series of characters.
Definition: datatypes.h:159