MezzanineEngine 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
conecollisionshape.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 _physicsconecollisionshape_cpp
41 #define _physicsconecollisionshape_cpp
42 
43 #include "Physics/conecollisionshape.h"
44 #include "collisionshapemanager.h"
45 #include "stringtool.h"
46 
47 #include "btBulletDynamicsCommon.h"
48 
49 namespace Mezzanine
50 {
51  namespace Physics
52  {
53  /////////////////////////////////////////
54  // ConeCollisionShape Functions
55 
56  void ConeCollisionShape::Construct(const String& Name, const Real& Radius, const Real& Height, StandardAxis UpAxis)
57  {
58  this->Name = Name;
59  btConeShape* ConeShape;
60  if(Axis_Y == UpAxis) ConeShape = new btConeShape(Radius,Height);
61  else if(Axis_X == UpAxis) ConeShape = new btConeShapeX(Radius,Height);
62  else if(Axis_Z == UpAxis) ConeShape = new btConeShapeZ(Radius,Height);
63  else { MEZZ_EXCEPTION(Exception::PARAMETERS_EXCEPTION,"Non-supported up StandardAxis passed into ConeCollisionShape constructor."); }
64  SetPointers(ConeShape);
65  this->GetBulletConeShape()->setImplicitShapeDimensions(Vector3(0,0,0).GetBulletVector3());
66  }
67 
69  {
70  Vector3 Results;
71  switch (GetUpStandardAxis())
72  {
73  case 0:
74  Results[0] = 1;
75  Results[1] = 0;
76  Results[2] = 2;
77  break;
78  case 1:
79  Results[0] = 0;
80  Results[1] = 1;
81  Results[2] = 2;
82  break;
83  case 2:
84  Results[0] = 0;
85  Results[1] = 2;
86  Results[2] = 1;
87  break;
88  default:
89  { MEZZ_EXCEPTION(Exception::PARAMETERS_EXCEPTION,"Non-supported up StandardAxis passed into ConeCollisionShape::GetAxisMathBS()."); }
90  }
91 
92  return Results;
93  }
94 
95  ConeCollisionShape::ConeCollisionShape(const String& Name, const Real& Radius, const Real& Height, const Vector3& UpAxis)
96  { Construct(Name, Radius, Height, UpAxis.IsStandardUnitAxis()); }
97 
98  ConeCollisionShape::ConeCollisionShape(const String& Name, const Real& Radius, const Real& Height, StandardAxis UpAxis)
99  { Construct(Name, Radius, Height, UpAxis); }
100 
102  {
103  if(OneNode.GetAttribute("Version").AsInt() == 1)
104  {
105  XML::Attribute OneName = OneNode.GetChild("PrimitiveCollisionShape").GetChild("CollisionShape").GetAttribute("Name"); // get name
106  if(!OneName) { MEZZ_EXCEPTION(Exception::PARAMETERS_EXCEPTION,"Could not find Name Attribute on CollsionShape Node during preparation for deserialization"); }
107  String Name_(OneName.AsString());
108 
109  XML::Attribute Radius = OneNode.GetAttribute("Radius"); // Find Attributes
110  if (!Radius) { DeSerializeError("find Radius Attribute",ConeCollisionShape::SerializableName()); }
111  XML::Attribute Axis = OneNode.GetAttribute("Axis");
112  if (!Axis) { DeSerializeError("find Axis Attribute",ConeCollisionShape::SerializableName()); }
113  XML::Attribute Height = OneNode.GetAttribute("Height");
114  if (!Height) { DeSerializeError("find Height Attribute",ConeCollisionShape::SerializableName()); }
115 
116  this->Construct(Name_,Radius.AsReal(),Height.AsReal(), (StandardAxis)Axis.AsInteger()); // make and deserialize the shape
117  this->ProtoDeSerialize(OneNode);
118  }else{
119  DeSerializeError("find usable serialization version",ConeCollisionShape::SerializableName());
120  }
121  }
122 
123  ConeCollisionShape::ConeCollisionShape(const String& Name, btConeShape* BulletShape)
124  {
125  this->Name = Name;
126  SetPointers(BulletShape);
127  this->GetBulletConeShape()->setImplicitShapeDimensions(Vector3(0,0,0).GetBulletVector3());
128  }
129 
131  {
132  delete GetBulletConeShape();
133  }
134 
136  {
137  return GetBulletConeShape()->getRadius();
138  }
139 
141  {
142  return GetBulletConeShape()->getHeight();
143  }
144 
146  {
147  return (GetRadius()) / GetRadiusScaling();
148  }
149 
151  {
152  return GetHeight() / GetHeightScaling();
153  }
154 
156  { return (GetScaling()[GetAxisMathBS()[0]]+GetScaling()[GetAxisMathBS()[2]])/2.0; }
157 
159  { return GetScaling()[GetUpStandardAxis()]; }
160 
162  { return Vector3::UnitOnAxis( (StandardAxis)GetBulletConeShape()->getConeUpIndex() ); }
163 
165  { return (StandardAxis)GetBulletConeShape()->getConeUpIndex(); }
166 
167 
169  {
171  }
172 
174  { return static_cast<btConeShape*>(ShapeBase); }
175 
177  {
178  XML::Node CollisionNode = CurrentRoot.AppendChild(this->ConeCollisionShape::SerializableName());
179  if (!CollisionNode) { SerializeError("create CollisionNode",this->ConeCollisionShape::SerializableName());}
180 
181  XML::Attribute Version = CollisionNode.AppendAttribute("Version");
182  if (Version)
183  { Version.SetValue(1); }
184  else
185  { SerializeError("Create Version Attribute", SerializableName()); }
186 
187  XML::Attribute RadiusAttr = CollisionNode.AppendAttribute("Radius");
188  if (RadiusAttr)
189  { RadiusAttr.SetValue(this->GetCleanRadius()); }
190  else
191  { SerializeError("Create RadiusAttr Attribute", SerializableName()); }
192 
193  /*XML::Attribute DirtyRadius = CollisionNode.AppendAttribute("DirtyRadius");
194  if (DirtyRadius)
195  { DirtyRadius.SetValue(this->GetRadius()); }
196  else
197  { SerializeError("Create DirtyRadius Attribute", SerializableName()); }*/
198 
199  XML::Attribute HeightAttr = CollisionNode.AppendAttribute("Height");
200  if (HeightAttr)
201  { HeightAttr.SetValue(this->GetCleanHeight()); }
202  else
203  { SerializeError("Create HeightAttr Attribute", SerializableName()); }
204 
205  XML::Attribute AxisAttr = CollisionNode.AppendAttribute("Axis");
206  if (AxisAttr)
207  { AxisAttr.SetValue(this->GetUpStandardAxis()); }
208  else
209  { SerializeError("Create AxisAttr Attribute", SerializableName()); }
210 
211  this->PrimitiveCollisionShape::ProtoSerialize(CollisionNode);
212  }
213 
215  {
217  {
218  if(OneNode.GetAttribute("Version").AsInt() == 1)
219  {
220  XML::Node CollisionNode = OneNode.GetChild(this->PrimitiveCollisionShape::SerializableName());
221  if(!CollisionNode)
222  { DeSerializeError("locate PrimitiveCollisionShape node",SerializableName()); }
223  this->PrimitiveCollisionShape::ProtoDeSerialize(CollisionNode);
224  }else{
225  DeSerializeError("find usable serialization version",SerializableName());
226  }
227  }else{
228  DeSerializeError(String("find correct class to deserialize, found a ")+OneNode.Name(),SerializableName());
229  }
230  }
231 
233  { return String("ConeCollisionShape"); }
234  }//Physics
235 }//Mezzanine
236 
237  std::ostream& operator << (std::ostream& stream, const Mezzanine::Physics::ConeCollisionShape& ShapeToSerialize)
238  { Mezzanine::Serialize(stream, ShapeToSerialize); return stream; }
239 
240  std::istream& operator >> (std::istream& stream, Mezzanine::Physics::ConeCollisionShape& x)
241  { return Mezzanine::DeSerialize(stream, x); }
242 
243  void operator >> (const Mezzanine::XML::Node& OneNode, Mezzanine::Physics::ConeCollisionShape& x)
244  { x.ProtoDeSerialize(OneNode); }
245 
246 #endif