2
0

BroadPhaseInsertionTest.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #include <TestFramework.h>
  4. #include <Tests/BroadPhase/BroadPhaseInsertionTest.h>
  5. #include <Jolt/Physics/Collision/RayCast.h>
  6. #include <Jolt/Physics/Collision/CastResult.h>
  7. #include <Jolt/Physics/Collision/CollisionCollectorImpl.h>
  8. #include <Jolt/Geometry/RayAABox.h>
  9. #include <Utils/Log.h>
  10. #include <Renderer/DebugRendererImp.h>
  11. JPH_IMPLEMENT_RTTI_VIRTUAL(BroadPhaseInsertionTest)
  12. {
  13. JPH_ADD_BASE_CLASS(BroadPhaseInsertionTest, BroadPhaseTest)
  14. }
  15. void BroadPhaseInsertionTest::Initialize()
  16. {
  17. BroadPhaseTest::Initialize();
  18. CreateBalancedDistribution(mBodyManager, mBodyManager->GetMaxBodies());
  19. }
  20. void BroadPhaseInsertionTest::PrePhysicsUpdate(const PreUpdateParams &inParams)
  21. {
  22. // Check if we need to change direction
  23. if (mDirection == 1 && mCurrentBody >= mBodyManager->GetMaxBodies())
  24. mDirection = -1;
  25. if (mDirection == -1 && mCurrentBody == 0)
  26. mDirection = 1;
  27. int num_this_step = int(mBodyManager->GetMaxBodies() / 10);
  28. if (mDirection < 0)
  29. mCurrentBody -= num_this_step;
  30. Body **body_vector = mBodyManager->GetBodies().data();
  31. // Randomly move bodies around
  32. if (mCurrentBody > 0)
  33. {
  34. const int cNumBodiesToMove = 100;
  35. BodyID *bodies_to_move = new BodyID [cNumBodiesToMove];
  36. uniform_int_distribution<int> body_selector(0, (int)mCurrentBody - 1);
  37. uniform_real_distribution<float> translation_selector(1.0f, 5.0f);
  38. for (int i = 0; i < cNumBodiesToMove; ++i)
  39. {
  40. Body &body = *body_vector[body_selector(mRandomGenerator)];
  41. JPH_ASSERT(body.IsInBroadPhase());
  42. body.SetPositionAndRotationInternal(body.GetPosition() + translation_selector(mRandomGenerator) * Vec3::sRandom(mRandomGenerator), Quat::sIdentity());
  43. bodies_to_move[i] = body.GetID();
  44. }
  45. mBroadPhase->NotifyBodiesAABBChanged(bodies_to_move, cNumBodiesToMove);
  46. delete [] bodies_to_move;
  47. }
  48. // Create batch of bodies
  49. BodyID *bodies_to_add_or_remove = new BodyID [num_this_step];
  50. for (int b = 0; b < num_this_step; ++b)
  51. bodies_to_add_or_remove[b] = body_vector[mCurrentBody + b]->GetID();
  52. // Add/remove them
  53. if (mDirection == 1)
  54. {
  55. // Prepare and abort
  56. BroadPhase::AddState add_state = mBroadPhase->AddBodiesPrepare(bodies_to_add_or_remove, num_this_step);
  57. mBroadPhase->AddBodiesAbort(bodies_to_add_or_remove, num_this_step, add_state);
  58. // Prepare and add
  59. add_state = mBroadPhase->AddBodiesPrepare(bodies_to_add_or_remove, num_this_step);
  60. mBroadPhase->AddBodiesFinalize(bodies_to_add_or_remove, num_this_step, add_state);
  61. }
  62. else
  63. mBroadPhase->RemoveBodies(bodies_to_add_or_remove, num_this_step);
  64. // Delete temp array
  65. delete [] bodies_to_add_or_remove;
  66. // Create ray
  67. default_random_engine random;
  68. Vec3 from = 1000.0f * Vec3::sRandom(random);
  69. RayCast ray { from, -2.0f * from };
  70. // Raycast before update
  71. AllHitCollisionCollector<RayCastBodyCollector> hits_before;
  72. mBroadPhase->CastRay(ray, hits_before);
  73. int num_before = (int)hits_before.mHits.size();
  74. BroadPhaseCastResult *results_before = hits_before.mHits.data();
  75. Trace("Before update: %d results found", num_before);
  76. // Draw results
  77. mDebugRenderer->DrawLine(ray.mOrigin, ray.mOrigin + ray.mDirection, Color::sRed);
  78. for (int i = 0; i < num_before; ++i)
  79. mDebugRenderer->DrawMarker(ray.mOrigin + results_before[i].mFraction * ray.mDirection, Color::sGreen, 10.0f);
  80. // Update the broadphase
  81. mBroadPhase->Optimize();
  82. // Raycast after update
  83. AllHitCollisionCollector<RayCastBodyCollector> hits_after;
  84. mBroadPhase->CastRay(ray, hits_after);
  85. int num_after = (int)hits_after.mHits.size();
  86. BroadPhaseCastResult *results_after = hits_after.mHits.data();
  87. Trace("After update: %d results found", num_after);
  88. // Before update we may have some false hits, check that there are less hits after update than before
  89. if (num_after > num_before)
  90. FatalError("BroadPhaseInsertionTest: After has more hits than before");
  91. for (BroadPhaseCastResult *ra = results_after, *ra_end = results_after + num_after; ra < ra_end; ++ra)
  92. {
  93. bool found = false;
  94. for (BroadPhaseCastResult *rb = results_before, *rb_end = results_before + num_before; rb < rb_end; ++rb)
  95. if (ra->mBodyID == rb->mBodyID)
  96. {
  97. found = true;
  98. break;
  99. }
  100. if (!found)
  101. FatalError("BroadPhaseInsertionTest: Result after not found in result before");
  102. }
  103. // Validate with brute force approach
  104. for (const Body *b : mBodyManager->GetBodies())
  105. {
  106. bool found = false;
  107. for (BroadPhaseCastResult *r = results_after, *r_end = results_after + num_after; r < r_end; ++r)
  108. if (r->mBodyID == b->GetID())
  109. {
  110. found = true;
  111. break;
  112. }
  113. if (b->IsInBroadPhase()
  114. && RayAABoxHits(ray.mOrigin, ray.mDirection, b->GetWorldSpaceBounds().mMin, b->GetWorldSpaceBounds().mMax))
  115. {
  116. if (!found)
  117. FatalError("BroadPhaseInsertionTest: Is intersecting but was not found");
  118. }
  119. else
  120. {
  121. if (found)
  122. FatalError("BroadPhaseInsertionTest: Is not intersecting but was found");
  123. }
  124. }
  125. if (mDirection > 0)
  126. mCurrentBody += num_this_step;
  127. }