CollisionCollectorImpl.h 2.7 KB

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