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