PhysicsCollisionShape.cpp 2.3 KB

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