CollisionCollectorImpl.h 2.8 KB

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