Thrive
collision_shape.h
1 #pragma once
2 
3 #include <btBulletCollisionCommon.h>
4 #include <cstdint>
5 #include <memory>
6 #include <OgreVector3.h>
7 #include <OgreQuaternion.h>
8 
9 namespace luabind {
10  class scope;
11 }
12 
13 namespace thrive {
14 
15 class StorageContainer;
16 
25 
26 public:
27 
31  using Ptr = std::shared_ptr<CollisionShape>;
32 
38  enum Axis : uint8_t {
39  AXIS_X = 0,
40  AXIS_Y = 1,
41  AXIS_Z = 2
42  };
43 
49  enum ShapeType : uint8_t {
50  EMPTY_SHAPE = 0,
51  BOX_SHAPE = 1,
52  CAPSULE_SHAPE = 2,
53  COMPOUND_SHAPE = 3,
54  CONE_SHAPE = 4,
55  CYLINDER_SHAPE = 5,
56  SPHERE_SHAPE = 6
57  };
58 
68  static std::unique_ptr<CollisionShape>
69  load(
71  );
72 
80  static luabind::scope
81  luaBindings();
82 
86  virtual ~CollisionShape() = 0;
87 
93  virtual btCollisionShape*
94  bulletShape() const = 0;
95 
100  virtual ShapeType
101  shapeType() const = 0;
102 
108  virtual StorageContainer
109  storage() const = 0;
110 
111 };
112 
113 
125 #define SHAPE_CLASS(cls, type, bulletShapeClass) \
126  public: \
127  \
128  \
131  static const ShapeType SHAPE_TYPE = type; \
132  \
133  static std::unique_ptr<cls> \
134  load( \
135  const StorageContainer& storage \
136  ); \
137  \
138  static luabind::scope \
139  luaBindings(); \
140  \
141  btCollisionShape* \
142  bulletShape() const override { \
143  return m_bulletShape.get(); \
144  } \
145  \
146  ShapeType \
147  shapeType() const override { \
148  return SHAPE_TYPE; \
149  } \
150  \
151  StorageContainer \
152  storage() const override; \
153  \
154  private: \
155  \
156  std::unique_ptr<bulletShapeClass> m_bulletShape;
157 
158 
160 // BoxShape
162 
166 class BoxShape : public CollisionShape {
167 
168  SHAPE_CLASS(BoxShape, BOX_SHAPE, btBoxShape)
169 
170 public:
171 
178  BoxShape(
179  const Ogre::Vector3& extents
180  );
181 
182 private:
183 
184  const Ogre::Vector3 m_extents;
185 
186 };
187 
188 
190 // CapsuleShape
192 
196 class CapsuleShape : public CollisionShape {
197 
198  SHAPE_CLASS(CapsuleShape, CAPSULE_SHAPE, btCapsuleShape)
199 
200 public:
201 
212  CapsuleShape(
213  CollisionShape::Axis axis,
214  btScalar radius,
215  btScalar height
216  );
217 
218 private:
219 
220  const CollisionShape::Axis m_axis;
221 
222  const btScalar m_height;
223 
224  const btScalar m_radius;
225 
226 };
227 
228 
230 // CompoundShape
232 
236 class CompoundShape : public CollisionShape {
237 
238  SHAPE_CLASS(CompoundShape, COMPOUND_SHAPE, btCompoundShape)
239 
240 public:
241 
245  CompoundShape();
246 
257  void
258  addChildShape(
259  const Ogre::Vector3& translation,
260  const Ogre::Quaternion& rotation,
261  CollisionShape::Ptr shape
262  );
263 
267  void
268  clear();
269 
276  void
277  removeChildShape(
278  const CollisionShape::Ptr& shape
279  );
280 
281 private:
282 
283  struct ChildShape {
284  Ogre::Vector3 translation;
285  Ogre::Quaternion rotation;
286  CollisionShape::Ptr shape;
287  };
288 
289  std::vector<ChildShape> m_childShapes;
290 
291 };
292 
293 
295 // ConeShape
297 
301 class ConeShape : public CollisionShape {
302 
303  SHAPE_CLASS(ConeShape, CONE_SHAPE, btConeShape)
304 
305 public:
306 
317  ConeShape(
318  CollisionShape::Axis axis,
319  btScalar radius,
320  btScalar height
321  );
322 
323 private:
324 
325  const CollisionShape::Axis m_axis;
326 
327  const btScalar m_height;
328 
329  const btScalar m_radius;
330 
331 };
332 
333 
335 // CylinderShape
337 
341 class CylinderShape : public CollisionShape {
342 
343  SHAPE_CLASS(CylinderShape, CYLINDER_SHAPE, btCylinderShape)
344 
345 public:
346 
358  CollisionShape::Axis axis,
359  btScalar radius,
360  btScalar height
361  );
362 
363 
364 private:
365 
366  const CollisionShape::Axis m_axis;
367 
368  const btScalar m_height;
369 
370  const btScalar m_radius;
371 
372 };
373 
374 
376 // EmptyShape
378 
384 class EmptyShape : public CollisionShape {
385 
386  SHAPE_CLASS(EmptyShape, EMPTY_SHAPE, btEmptyShape)
387 
388 public:
389 
393  EmptyShape();
394 
395 };
396 
397 
399 // SphereShape
401 
405 class SphereShape : public CollisionShape {
406 
407  SHAPE_CLASS(SphereShape, SPHERE_SHAPE, btSphereShape)
408 
409 public:
410 
417  SphereShape(
418  btScalar radius
419  );
420 
421 private:
422 
423  const btScalar m_radius;
424 
425 };
426 
427 
428 }
429 
430 
virtual ShapeType shapeType() const =0
The shape's type.
virtual btCollisionShape * bulletShape() const =0
Returns a pointer to the internal Bullet shape object.
virtual StorageContainer storage() const =0
Serializes the shape into a storage container.
Definition: collision_shape.cpp:57
static luabind::scope luaBindings()
Lua bindings.
Definition: collision_shape.cpp:41
ShapeType
Enumeration for the currently supported shape types.
Definition: collision_shape.h:49
A simple box shape.
Definition: collision_shape.h:166
Wrapper for Bullet's collision shapes.
Definition: collision_shape.h:24
Definition: bullet_to_ogre_system.h:5
Definition: bullet_ogre_conversion.h:6
A cylinder shape.
Definition: collision_shape.h:341
virtual ~CollisionShape()=0
Destructor.
Definition: collision_shape.cpp:53
Definition: debug_drawing.h:8
A shape compounded of multiple other shapes.
Definition: collision_shape.h:236
An empty shape.
Definition: collision_shape.h:384
A sphere shape.
Definition: collision_shape.h:405
Axis
Enumeration for the 3 axes.
Definition: collision_shape.h:38
A cone shape.
Definition: collision_shape.h:301
static std::unique_ptr< CollisionShape > load(const StorageContainer &storage)
Loads a shape from a storage container.
Definition: collision_shape.cpp:20
A capsule shape. Think cylinder with rounded caps.
Definition: collision_shape.h:196
A key-value storage for serialization.
Definition: serialization.h:36
std::shared_ptr< CollisionShape > Ptr
Convenience typedef.
Definition: collision_shape.h:31