PhysicsCollisionShape.h 2.9 KB

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