CollisionCollectorImpl.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #pragma once
  4. #include <Jolt/Physics/Collision/CollisionCollector.h>
  5. #include <Jolt/Core/QuickSort.h>
  6. JPH_NAMESPACE_BEGIN
  7. /// Simple implementation that collects all hits and optionally sorts them on distance
  8. template <class CollectorType>
  9. class AllHitCollisionCollector : public CollectorType
  10. {
  11. public:
  12. /// Redeclare ResultType
  13. using ResultType = typename CollectorType::ResultType;
  14. // See: CollectorType::Reset
  15. virtual void Reset() override
  16. {
  17. CollectorType::Reset();
  18. mHits.clear();
  19. }
  20. // See: CollectorType::AddHit
  21. virtual void AddHit(const ResultType &inResult) override
  22. {
  23. mHits.push_back(inResult);
  24. }
  25. /// Order hits on closest first
  26. void Sort()
  27. {
  28. QuickSort(mHits.begin(), mHits.end(), [](const ResultType &inLHS, const ResultType &inRHS) { return inLHS.GetEarlyOutFraction() < inRHS.GetEarlyOutFraction(); });
  29. }
  30. /// Check if any hits were collected
  31. inline bool HadHit() const
  32. {
  33. return !mHits.empty();
  34. }
  35. Array<ResultType> mHits;
  36. };
  37. /// Simple implementation that collects the closest / deepest hit
  38. template <class CollectorType>
  39. class ClosestHitCollisionCollector : public CollectorType
  40. {
  41. public:
  42. /// Redeclare ResultType
  43. using ResultType = typename CollectorType::ResultType;
  44. // See: CollectorType::Reset
  45. virtual void Reset() override
  46. {
  47. CollectorType::Reset();
  48. mHadHit = false;
  49. }
  50. // See: CollectorType::AddHit
  51. virtual void AddHit(const ResultType &inResult) override
  52. {
  53. float early_out = inResult.GetEarlyOutFraction();
  54. if (!mHadHit || early_out < mHit.GetEarlyOutFraction())
  55. {
  56. // Update early out fraction
  57. CollectorType::UpdateEarlyOutFraction(early_out);
  58. // Store hit
  59. mHit = inResult;
  60. mHadHit = true;
  61. }
  62. }
  63. /// Check if this collector has had a hit
  64. inline bool HadHit() const
  65. {
  66. return mHadHit;
  67. }
  68. ResultType mHit;
  69. private:
  70. bool mHadHit = false;
  71. };
  72. /// Simple implementation that collects any hit
  73. template <class CollectorType>
  74. class AnyHitCollisionCollector : public CollectorType
  75. {
  76. public:
  77. /// Redeclare ResultType
  78. using ResultType = typename CollectorType::ResultType;
  79. // See: CollectorType::Reset
  80. virtual void Reset() override
  81. {
  82. CollectorType::Reset();
  83. mHadHit = false;
  84. }
  85. // See: CollectorType::AddHit
  86. virtual void AddHit(const ResultType &inResult) override
  87. {
  88. // Test that the collector is not collecting more hits after forcing an early out
  89. JPH_ASSERT(!mHadHit);
  90. // Abort any further testing
  91. CollectorType::ForceEarlyOut();
  92. // Store hit
  93. mHit = inResult;
  94. mHadHit = true;
  95. }
  96. /// Check if this collector has had a hit
  97. inline bool HadHit() const
  98. {
  99. return mHadHit;
  100. }
  101. ResultType mHit;
  102. private:
  103. bool mHadHit = false;
  104. };
  105. JPH_NAMESPACE_END