PhysicsCollisionShape.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. #include <AnKi/Physics/PhysicsCollisionShape.h>
  6. #include <AnKi/Physics/PhysicsWorld.h>
  7. namespace anki
  8. {
  9. PhysicsSphere::PhysicsSphere(PhysicsWorld* world, F32 radius)
  10. : PhysicsCollisionShape(world, ShapeType::SPHERE)
  11. {
  12. m_sphere.init(radius);
  13. m_sphere->setMargin(getWorld().getCollisionMargin());
  14. m_sphere->setUserPointer(static_cast<PhysicsObject*>(this));
  15. }
  16. PhysicsSphere::~PhysicsSphere()
  17. {
  18. m_sphere.destroy();
  19. }
  20. PhysicsBox::PhysicsBox(PhysicsWorld* world, const Vec3& extend)
  21. : PhysicsCollisionShape(world, ShapeType::BOX)
  22. {
  23. m_box.init(toBt(extend));
  24. m_box->setMargin(getWorld().getCollisionMargin());
  25. m_box->setUserPointer(static_cast<PhysicsObject*>(this));
  26. }
  27. PhysicsBox::~PhysicsBox()
  28. {
  29. m_box.destroy();
  30. }
  31. PhysicsTriangleSoup::PhysicsTriangleSoup(PhysicsWorld* world, ConstWeakArray<Vec3> positions,
  32. ConstWeakArray<U32> indices, Bool convex)
  33. : PhysicsCollisionShape(world, ShapeType::TRI_MESH)
  34. {
  35. if(!convex)
  36. {
  37. ANKI_ASSERT((indices.getSize() % 3) == 0);
  38. m_mesh.init();
  39. for(U32 i = 0; i < indices.getSize(); i += 3)
  40. {
  41. m_mesh->addTriangle(toBt(positions[indices[i]]), toBt(positions[indices[i + 1]]),
  42. toBt(positions[indices[i + 2]]));
  43. }
  44. // Create the dynamic shape
  45. m_triMesh.m_dynamic.init(m_mesh.get());
  46. m_triMesh.m_dynamic->setMargin(getWorld().getCollisionMargin());
  47. m_triMesh.m_dynamic->updateBound();
  48. m_triMesh.m_dynamic->setUserPointer(static_cast<PhysicsObject*>(this));
  49. // And the static one
  50. m_triMesh.m_static.init(m_mesh.get(), true);
  51. m_triMesh.m_static->setMargin(getWorld().getCollisionMargin());
  52. m_triMesh.m_static->setUserPointer(static_cast<PhysicsObject*>(this));
  53. }
  54. else
  55. {
  56. m_type = ShapeType::CONVEX; // Fake the type
  57. m_convex.init(&positions[0][0], I32(positions.getSize()), U32(sizeof(Vec3)));
  58. m_convex->setMargin(getWorld().getCollisionMargin());
  59. m_convex->setUserPointer(static_cast<PhysicsObject*>(this));
  60. }
  61. }
  62. PhysicsTriangleSoup::~PhysicsTriangleSoup()
  63. {
  64. if(m_type == ShapeType::TRI_MESH)
  65. {
  66. m_triMesh.m_dynamic.destroy();
  67. m_triMesh.m_static.destroy();
  68. m_mesh.destroy();
  69. }
  70. else
  71. {
  72. ANKI_ASSERT(m_type == ShapeType::CONVEX);
  73. m_convex.destroy();
  74. }
  75. }
  76. } // end namespace anki