IntrOrientedBox2Circle2.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // David Eberly, Geometric Tools, Redmond WA 98052
  2. // Copyright (c) 1998-2020
  3. // Distributed under the Boost Software License, Version 1.0.
  4. // https://www.boost.org/LICENSE_1_0.txt
  5. // https://www.geometrictools.com/License/Boost/LICENSE_1_0.txt
  6. // Version: 4.0.2019.08.13
  7. #pragma once
  8. #include <Mathematics/IntrAlignedBox2Circle2.h>
  9. #include <Mathematics/DistPointOrientedBox.h>
  10. // The find-intersection query is based on the document
  11. // https://www.geometrictools.com/Documentation/IntersectionMovingCircleRectangle.pdf
  12. namespace WwiseGTE
  13. {
  14. template <typename Real>
  15. class TIQuery<Real, OrientedBox2<Real>, Circle2<Real>>
  16. {
  17. public:
  18. // The intersection query considers the box and circle to be solids;
  19. // that is, the circle object includes the region inside the circular
  20. // boundary and the box object includes the region inside the
  21. // rectangular boundary. If the circle object and rectangle object
  22. // overlap, the objects intersect.
  23. struct Result
  24. {
  25. bool intersect;
  26. };
  27. Result operator()(OrientedBox2<Real> const& box, Circle2<Real> const& circle)
  28. {
  29. DCPQuery<Real, Vector2<Real>, OrientedBox2<Real>> pbQuery;
  30. auto pbResult = pbQuery(circle.center, box);
  31. Result result;
  32. result.intersect = (pbResult.sqrDistance <= circle.radius * circle.radius);
  33. return result;
  34. }
  35. };
  36. template <typename Real>
  37. class FIQuery<Real, OrientedBox2<Real>, Circle2<Real>>
  38. :
  39. public FIQuery<Real, AlignedBox2<Real>, Circle2<Real>>
  40. {
  41. public:
  42. // See the base class for the definition of 'struct Result'.
  43. typename FIQuery<Real, AlignedBox2<Real>, Circle2<Real>>::Result
  44. operator()(OrientedBox2<Real> const& box, Vector2<Real> const& boxVelocity,
  45. Circle2<Real> const& circle, Vector2<Real> const& circleVelocity)
  46. {
  47. // Transform the oriented box to an axis-aligned box centered at
  48. // the origin and transform the circle accordingly. Compute the
  49. // velocity of the circle relative to the box.
  50. Real const zero(0), one(1), minusOne(-1);
  51. Vector2<Real> cdiff = circle.center - box.center;
  52. Vector2<Real> vdiff = circleVelocity - boxVelocity;
  53. Vector2<Real> C, V;
  54. for (int i = 0; i < 2; ++i)
  55. {
  56. C[i] = Dot(cdiff, box.axis[i]);
  57. V[i] = Dot(vdiff, box.axis[i]);
  58. }
  59. // Change signs on components, if necessary, to transform C to the
  60. // first quadrant. Adjust the velocity accordingly.
  61. Real sign[2];
  62. for (int i = 0; i < 2; ++i)
  63. {
  64. if (C[i] >= zero)
  65. {
  66. sign[i] = one;
  67. }
  68. else
  69. {
  70. C[i] = -C[i];
  71. V[i] = -V[i];
  72. sign[i] = minusOne;
  73. }
  74. }
  75. typename FIQuery<Real, AlignedBox2<Real>, Circle2<Real>>::Result result = { 0, zero, { zero, zero } };
  76. this->DoQuery(box.extent, C, circle.radius, V, result);
  77. if (result.intersectionType != 0)
  78. {
  79. // Transform back to the original coordinate system.
  80. result.contactPoint = box.center
  81. + (sign[0] * result.contactPoint[0]) * box.axis[0]
  82. + (sign[1] * result.contactPoint[1]) * box.axis[1];
  83. }
  84. return result;
  85. }
  86. };
  87. }