PhysicsCollisionShape.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. // Copyright (C) 2009-2021, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #pragma once
  6. #include <AnKi/Physics/PhysicsObject.h>
  7. #include <AnKi/Util/WeakArray.h>
  8. #include <AnKi/Util/ClassWrapper.h>
  9. namespace anki {
  10. /// @addtogroup physics
  11. /// @{
  12. /// The base of all collision shapes.
  13. class PhysicsCollisionShape : public PhysicsObject
  14. {
  15. ANKI_PHYSICS_OBJECT(PhysicsObjectType::COLLISION_SHAPE)
  16. public:
  17. ANKI_INTERNAL const btCollisionShape* getBtShape(Bool forDynamicBodies = false) const
  18. {
  19. return getBtShapeInternal(forDynamicBodies);
  20. }
  21. ANKI_INTERNAL btCollisionShape* getBtShape(Bool forDynamicBodies = false)
  22. {
  23. return const_cast<btCollisionShape*>(getBtShapeInternal(forDynamicBodies));
  24. }
  25. protected:
  26. enum class ShapeType : U8
  27. {
  28. BOX,
  29. SPHERE,
  30. CONVEX,
  31. TRI_MESH
  32. };
  33. class TriMesh
  34. {
  35. public:
  36. ClassWrapper<btGImpactMeshShape> m_dynamic;
  37. ClassWrapper<btBvhTriangleMeshShape> m_static;
  38. };
  39. // All shapes
  40. union
  41. {
  42. ClassWrapper<btBoxShape> m_box;
  43. ClassWrapper<btSphereShape> m_sphere;
  44. ClassWrapper<btConvexHullShape> m_convex;
  45. TriMesh m_triMesh;
  46. };
  47. ShapeType m_type;
  48. PhysicsCollisionShape(PhysicsWorld* world, ShapeType type)
  49. : PhysicsObject(CLASS_TYPE, world)
  50. , m_type(type)
  51. {
  52. }
  53. const btCollisionShape* getBtShapeInternal(Bool forDynamicBodies) const
  54. {
  55. switch(m_type)
  56. {
  57. case ShapeType::BOX:
  58. return m_box.get();
  59. case ShapeType::SPHERE:
  60. return m_sphere.get();
  61. case ShapeType::CONVEX:
  62. return m_convex.get();
  63. case ShapeType::TRI_MESH:
  64. if(forDynamicBodies)
  65. {
  66. return m_triMesh.m_dynamic.get();
  67. }
  68. else
  69. {
  70. return m_triMesh.m_static.get();
  71. }
  72. default:
  73. ANKI_ASSERT(0);
  74. return nullptr;
  75. }
  76. }
  77. void registerToWorld() override
  78. {
  79. }
  80. void unregisterFromWorld() override
  81. {
  82. }
  83. };
  84. /// Sphere collision shape.
  85. class PhysicsSphere final : public PhysicsCollisionShape
  86. {
  87. ANKI_PHYSICS_OBJECT(PhysicsObjectType::COLLISION_SHAPE)
  88. private:
  89. PhysicsSphere(PhysicsWorld* world, F32 radius);
  90. ~PhysicsSphere();
  91. };
  92. /// Box collision shape.
  93. class PhysicsBox final : public PhysicsCollisionShape
  94. {
  95. ANKI_PHYSICS_OBJECT(PhysicsObjectType::COLLISION_SHAPE)
  96. private:
  97. PhysicsBox(PhysicsWorld* world, const Vec3& extend);
  98. ~PhysicsBox();
  99. };
  100. /// Convex hull collision shape.
  101. class PhysicsConvexHull final : public PhysicsCollisionShape
  102. {
  103. ANKI_PHYSICS_OBJECT(PhysicsObjectType::COLLISION_SHAPE)
  104. private:
  105. PhysicsConvexHull(PhysicsWorld* world, const Vec3* positions, U32 positionsCount, U32 positionsStride);
  106. ~PhysicsConvexHull();
  107. };
  108. /// Static triangle mesh shape.
  109. class PhysicsTriangleSoup final : public PhysicsCollisionShape
  110. {
  111. ANKI_PHYSICS_OBJECT(PhysicsObjectType::COLLISION_SHAPE)
  112. private:
  113. ClassWrapper<btTriangleMesh> m_mesh;
  114. PhysicsTriangleSoup(PhysicsWorld* world, ConstWeakArray<Vec3> positions, ConstWeakArray<U32> indices,
  115. Bool convex = false);
  116. ~PhysicsTriangleSoup();
  117. };
  118. /// @}
  119. } // end namespace anki