PhysicsCollisionShape.cpp 2.3 KB

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