MutableCompoundShapeTests.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2024 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #include "UnitTestFramework.h"
  5. #include "PhysicsTestContext.h"
  6. #include <Jolt/Physics/Collision/Shape/SphereShape.h>
  7. #include <Jolt/Physics/Collision/Shape/BoxShape.h>
  8. #include <Jolt/Physics/Collision/Shape/MutableCompoundShape.h>
  9. #include <Jolt/Physics/Collision/CollisionCollectorImpl.h>
  10. #include <Jolt/Physics/Collision/CollidePointResult.h>
  11. TEST_SUITE("MutableCompoundShapeTests")
  12. {
  13. TEST_CASE("TestMutableCompoundShapeAddRemove")
  14. {
  15. MutableCompoundShapeSettings settings;
  16. Ref<Shape> sphere1 = new SphereShape(1.0f);
  17. settings.AddShape(Vec3::sZero(), Quat::sIdentity(), sphere1);
  18. Ref<MutableCompoundShape> shape = StaticCast<MutableCompoundShape>(settings.Create().Get());
  19. auto check_shape_hit = [shape] (Vec3Arg inPosition) {
  20. AllHitCollisionCollector<CollidePointCollector> collector;
  21. shape->CollidePoint(inPosition - shape->GetCenterOfMass(), SubShapeIDCreator(), collector);
  22. SubShapeID remainder;
  23. CHECK(collector.mHits.size() <= 1);
  24. return !collector.mHits.empty()? shape->GetSubShape(shape->GetSubShapeIndexFromID(collector.mHits[0].mSubShapeID2, remainder)).mShape : nullptr;
  25. };
  26. CHECK(shape->GetNumSubShapes() == 1);
  27. CHECK(shape->GetSubShape(0).mShape == sphere1);
  28. CHECK(shape->GetLocalBounds() == AABox(Vec3(-1, -1, -1), Vec3(1, 1, 1)));
  29. CHECK(check_shape_hit(Vec3::sZero()) == sphere1);
  30. Ref<Shape> sphere2 = new SphereShape(2.0f);
  31. shape->AddShape(Vec3(10, 0, 0), Quat::sIdentity(), sphere2, 0, 0); // Insert at the start
  32. CHECK(shape->GetNumSubShapes() == 2);
  33. CHECK(shape->GetSubShape(0).mShape == sphere2);
  34. CHECK(shape->GetSubShape(1).mShape == sphere1);
  35. CHECK(shape->GetLocalBounds() == AABox(Vec3(-1, -2, -2), Vec3(12, 2, 2)));
  36. CHECK(check_shape_hit(Vec3::sZero()) == sphere1);
  37. CHECK(check_shape_hit(Vec3(10, 0, 0)) == sphere2);
  38. Ref<Shape> sphere3 = new SphereShape(3.0f);
  39. shape->AddShape(Vec3(20, 0, 0), Quat::sIdentity(), sphere3, 0, 2); // Insert at the end
  40. CHECK(shape->GetNumSubShapes() == 3);
  41. CHECK(shape->GetSubShape(0).mShape == sphere2);
  42. CHECK(shape->GetSubShape(1).mShape == sphere1);
  43. CHECK(shape->GetSubShape(2).mShape == sphere3);
  44. CHECK(shape->GetLocalBounds() == AABox(Vec3(-1, -3, -3), Vec3(23, 3, 3)));
  45. CHECK(check_shape_hit(Vec3::sZero()) == sphere1);
  46. CHECK(check_shape_hit(Vec3(10, 0, 0)) == sphere2);
  47. CHECK(check_shape_hit(Vec3(20, 0, 0)) == sphere3);
  48. shape->RemoveShape(1);
  49. CHECK(shape->GetNumSubShapes() == 2);
  50. CHECK(shape->GetSubShape(0).mShape == sphere2);
  51. CHECK(shape->GetSubShape(1).mShape == sphere3);
  52. CHECK(shape->GetLocalBounds() == AABox(Vec3(8, -3, -3), Vec3(23, 3, 3)));
  53. CHECK(check_shape_hit(Vec3(0, 0, 0)) == nullptr);
  54. CHECK(check_shape_hit(Vec3(10, 0, 0)) == sphere2);
  55. CHECK(check_shape_hit(Vec3(20, 0, 0)) == sphere3);
  56. Ref<Shape> sphere4 = new SphereShape(4.0f);
  57. shape->AddShape(Vec3(0, 0, 0), Quat::sIdentity(), sphere4, 0); // Insert at the end
  58. CHECK(shape->GetNumSubShapes() == 3);
  59. CHECK(shape->GetSubShape(0).mShape == sphere2);
  60. CHECK(shape->GetSubShape(1).mShape == sphere3);
  61. CHECK(shape->GetSubShape(2).mShape == sphere4);
  62. CHECK(shape->GetLocalBounds() == AABox(Vec3(-4, -4, -4), Vec3(23, 4, 4)));
  63. CHECK(check_shape_hit(Vec3::sZero()) == sphere4);
  64. CHECK(check_shape_hit(Vec3(10, 0, 0)) == sphere2);
  65. CHECK(check_shape_hit(Vec3(20, 0, 0)) == sphere3);
  66. Ref<Shape> sphere5 = new SphereShape(1.0f);
  67. shape->AddShape(Vec3(15, 0, 0), Quat::sIdentity(), sphere5, 0, 1); // Insert in the middle
  68. CHECK(shape->GetNumSubShapes() == 4);
  69. CHECK(shape->GetSubShape(0).mShape == sphere2);
  70. CHECK(shape->GetSubShape(1).mShape == sphere5);
  71. CHECK(shape->GetSubShape(2).mShape == sphere3);
  72. CHECK(shape->GetSubShape(3).mShape == sphere4);
  73. CHECK(shape->GetLocalBounds() == AABox(Vec3(-4, -4, -4), Vec3(23, 4, 4)));
  74. CHECK(check_shape_hit(Vec3::sZero()) == sphere4);
  75. CHECK(check_shape_hit(Vec3(10, 0, 0)) == sphere2);
  76. CHECK(check_shape_hit(Vec3(15, 0, 0)) == sphere5);
  77. CHECK(check_shape_hit(Vec3(20, 0, 0)) == sphere3);
  78. shape->RemoveShape(3);
  79. CHECK(shape->GetNumSubShapes() == 3);
  80. CHECK(shape->GetSubShape(0).mShape == sphere2);
  81. CHECK(shape->GetSubShape(1).mShape == sphere5);
  82. CHECK(shape->GetSubShape(2).mShape == sphere3);
  83. CHECK(shape->GetLocalBounds() == AABox(Vec3(8, -3, -3), Vec3(23, 3, 3)));
  84. CHECK(check_shape_hit(Vec3::sZero()) == nullptr);
  85. CHECK(check_shape_hit(Vec3(10, 0, 0)) == sphere2);
  86. CHECK(check_shape_hit(Vec3(15, 0, 0)) == sphere5);
  87. CHECK(check_shape_hit(Vec3(20, 0, 0)) == sphere3);
  88. shape->RemoveShape(1);
  89. CHECK(shape->GetNumSubShapes() == 2);
  90. CHECK(shape->GetSubShape(0).mShape == sphere2);
  91. CHECK(shape->GetSubShape(1).mShape == sphere3);
  92. CHECK(shape->GetLocalBounds() == AABox(Vec3(8, -3, -3), Vec3(23, 3, 3)));
  93. CHECK(check_shape_hit(Vec3::sZero()) == nullptr);
  94. CHECK(check_shape_hit(Vec3(10, 0, 0)) == sphere2);
  95. CHECK(check_shape_hit(Vec3(15, 0, 0)) == nullptr);
  96. CHECK(check_shape_hit(Vec3(20, 0, 0)) == sphere3);
  97. shape->RemoveShape(1);
  98. CHECK(shape->GetNumSubShapes() == 1);
  99. CHECK(shape->GetSubShape(0).mShape == sphere2);
  100. CHECK(shape->GetLocalBounds() == AABox(Vec3(8, -2, -2), Vec3(12, 2, 2)));
  101. CHECK(check_shape_hit(Vec3::sZero()) == nullptr);
  102. CHECK(check_shape_hit(Vec3(10, 0, 0)) == sphere2);
  103. CHECK(check_shape_hit(Vec3(15, 0, 0)) == nullptr);
  104. CHECK(check_shape_hit(Vec3(20, 0, 0)) == nullptr);
  105. shape->RemoveShape(0);
  106. CHECK(shape->GetNumSubShapes() == 0);
  107. CHECK(!shape->GetLocalBounds().IsValid());
  108. CHECK(check_shape_hit(Vec3::sZero()) == nullptr);
  109. CHECK(check_shape_hit(Vec3(10, 0, 0)) == nullptr);
  110. CHECK(check_shape_hit(Vec3(15, 0, 0)) == nullptr);
  111. CHECK(check_shape_hit(Vec3(20, 0, 0)) == nullptr);
  112. }
  113. TEST_CASE("TestMutableCompoundShapeAdjustCenterOfMass")
  114. {
  115. // Start with a box at (-1 0 0)
  116. MutableCompoundShapeSettings settings;
  117. Ref<Shape> box_shape1 = new BoxShape(Vec3::sReplicate(1.0f));
  118. box_shape1->SetUserData(1);
  119. settings.AddShape(Vec3(-1.0f, 0.0f, 0.0f), Quat::sIdentity(), box_shape1);
  120. Ref<MutableCompoundShape> shape = StaticCast<MutableCompoundShape>(settings.Create().Get());
  121. CHECK(shape->GetCenterOfMass() == Vec3(-1.0f, 0.0f, 0.0f));
  122. CHECK(shape->GetLocalBounds() == AABox(Vec3::sReplicate(-1.0f), Vec3::sReplicate(1.0f)));
  123. // Check that we can hit the box
  124. AllHitCollisionCollector<CollidePointCollector> collector;
  125. shape->CollidePoint(Vec3(-0.5f, 0.0f, 0.0f) - shape->GetCenterOfMass(), SubShapeIDCreator(), collector);
  126. CHECK((collector.mHits.size() == 1 && shape->GetSubShapeUserData(collector.mHits[0].mSubShapeID2) == 1));
  127. collector.Reset();
  128. CHECK(collector.mHits.empty());
  129. // Now add another box at (1 0 0)
  130. Ref<Shape> box_shape2 = new BoxShape(Vec3::sReplicate(1.0f));
  131. box_shape2->SetUserData(2);
  132. shape->AddShape(Vec3(1.0f, 0.0f, 0.0f), Quat::sIdentity(), box_shape2);
  133. CHECK(shape->GetCenterOfMass() == Vec3(-1.0f, 0.0f, 0.0f));
  134. CHECK(shape->GetLocalBounds() == AABox(Vec3(-1.0f, -1.0f, -1.0f), Vec3(3.0f, 1.0f, 1.0f)));
  135. // Check that we can hit both boxes
  136. shape->CollidePoint(Vec3(-0.5f, 0.0f, 0.0f) - shape->GetCenterOfMass(), SubShapeIDCreator(), collector);
  137. CHECK((collector.mHits.size() == 1 && shape->GetSubShapeUserData(collector.mHits[0].mSubShapeID2) == 1));
  138. collector.Reset();
  139. shape->CollidePoint(Vec3(0.5f, 0.0f, 0.0f) - shape->GetCenterOfMass(), SubShapeIDCreator(), collector);
  140. CHECK((collector.mHits.size() == 1 && shape->GetSubShapeUserData(collector.mHits[0].mSubShapeID2) == 2));
  141. collector.Reset();
  142. // Adjust the center of mass
  143. shape->AdjustCenterOfMass();
  144. CHECK(shape->GetCenterOfMass() == Vec3::sZero());
  145. CHECK(shape->GetLocalBounds() == AABox(Vec3(-2.0f, -1.0f, -1.0f), Vec3(2.0f, 1.0f, 1.0f)));
  146. // Check that we can hit both boxes
  147. shape->CollidePoint(Vec3(-0.5f, 0.0f, 0.0f) - shape->GetCenterOfMass(), SubShapeIDCreator(), collector);
  148. CHECK((collector.mHits.size() == 1 && shape->GetSubShapeUserData(collector.mHits[0].mSubShapeID2) == 1));
  149. collector.Reset();
  150. shape->CollidePoint(Vec3(0.5f, 0.0f, 0.0f) - shape->GetCenterOfMass(), SubShapeIDCreator(), collector);
  151. CHECK((collector.mHits.size() == 1 && shape->GetSubShapeUserData(collector.mHits[0].mSubShapeID2) == 2));
  152. collector.Reset();
  153. }
  154. }