b2_distance.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. // MIT License
  2. // Copyright (c) 2019 Erin Catto
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy
  4. // of this software and associated documentation files (the "Software"), to deal
  5. // in the Software without restriction, including without limitation the rights
  6. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. // copies of the Software, and to permit persons to whom the Software is
  8. // furnished to do so, subject to the following conditions:
  9. // The above copyright notice and this permission notice shall be included in all
  10. // copies or substantial portions of the Software.
  11. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  17. // SOFTWARE.
  18. #ifndef B2_DISTANCE_H
  19. #define B2_DISTANCE_H
  20. #include "b2_api.h"
  21. #include "b2_math.h"
  22. class b2Shape;
  23. /// A distance proxy is used by the GJK algorithm.
  24. /// It encapsulates any shape.
  25. struct B2_API b2DistanceProxy
  26. {
  27. b2DistanceProxy() : m_vertices(nullptr), m_count(0), m_radius(0.0f) {}
  28. /// Initialize the proxy using the given shape. The shape
  29. /// must remain in scope while the proxy is in use.
  30. void Set(const b2Shape* shape, int32 index);
  31. /// Initialize the proxy using a vertex cloud and radius. The vertices
  32. /// must remain in scope while the proxy is in use.
  33. void Set(const b2Vec2* vertices, int32 count, float radius);
  34. /// Get the supporting vertex index in the given direction.
  35. int32 GetSupport(const b2Vec2& d) const;
  36. /// Get the supporting vertex in the given direction.
  37. const b2Vec2& GetSupportVertex(const b2Vec2& d) const;
  38. /// Get the vertex count.
  39. int32 GetVertexCount() const;
  40. /// Get a vertex by index. Used by b2Distance.
  41. const b2Vec2& GetVertex(int32 index) const;
  42. b2Vec2 m_buffer[2];
  43. const b2Vec2* m_vertices;
  44. int32 m_count;
  45. float m_radius;
  46. };
  47. /// Used to warm start b2Distance.
  48. /// Set count to zero on first call.
  49. struct B2_API b2SimplexCache
  50. {
  51. float metric; ///< length or area
  52. uint16 count;
  53. uint8 indexA[3]; ///< vertices on shape A
  54. uint8 indexB[3]; ///< vertices on shape B
  55. };
  56. /// Input for b2Distance.
  57. /// You have to option to use the shape radii
  58. /// in the computation. Even
  59. struct B2_API b2DistanceInput
  60. {
  61. b2DistanceProxy proxyA;
  62. b2DistanceProxy proxyB;
  63. b2Transform transformA;
  64. b2Transform transformB;
  65. bool useRadii;
  66. };
  67. /// Output for b2Distance.
  68. struct B2_API b2DistanceOutput
  69. {
  70. b2Vec2 pointA; ///< closest point on shapeA
  71. b2Vec2 pointB; ///< closest point on shapeB
  72. float distance;
  73. int32 iterations; ///< number of GJK iterations used
  74. };
  75. /// Compute the closest points between two shapes. Supports any combination of:
  76. /// b2CircleShape, b2PolygonShape, b2EdgeShape. The simplex cache is input/output.
  77. /// On the first call set b2SimplexCache.count to zero.
  78. B2_API void b2Distance(b2DistanceOutput* output,
  79. b2SimplexCache* cache,
  80. const b2DistanceInput* input);
  81. /// Input parameters for b2ShapeCast
  82. struct B2_API b2ShapeCastInput
  83. {
  84. b2DistanceProxy proxyA;
  85. b2DistanceProxy proxyB;
  86. b2Transform transformA;
  87. b2Transform transformB;
  88. b2Vec2 translationB;
  89. };
  90. /// Output results for b2ShapeCast
  91. struct B2_API b2ShapeCastOutput
  92. {
  93. b2Vec2 point;
  94. b2Vec2 normal;
  95. float lambda;
  96. int32 iterations;
  97. };
  98. /// Perform a linear shape cast of shape B moving and shape A fixed. Determines the hit point, normal, and translation fraction.
  99. /// @returns true if hit, false if there is no hit or an initial overlap
  100. B2_API bool b2ShapeCast(b2ShapeCastOutput* output, const b2ShapeCastInput* input);
  101. //////////////////////////////////////////////////////////////////////////
  102. inline int32 b2DistanceProxy::GetVertexCount() const
  103. {
  104. return m_count;
  105. }
  106. inline const b2Vec2& b2DistanceProxy::GetVertex(int32 index) const
  107. {
  108. b2Assert(0 <= index && index < m_count);
  109. return m_vertices[index];
  110. }
  111. inline int32 b2DistanceProxy::GetSupport(const b2Vec2& d) const
  112. {
  113. int32 bestIndex = 0;
  114. float bestValue = b2Dot(m_vertices[0], d);
  115. for (int32 i = 1; i < m_count; ++i)
  116. {
  117. float value = b2Dot(m_vertices[i], d);
  118. if (value > bestValue)
  119. {
  120. bestIndex = i;
  121. bestValue = value;
  122. }
  123. }
  124. return bestIndex;
  125. }
  126. inline const b2Vec2& b2DistanceProxy::GetSupportVertex(const b2Vec2& d) const
  127. {
  128. int32 bestIndex = 0;
  129. float bestValue = b2Dot(m_vertices[0], d);
  130. for (int32 i = 1; i < m_count; ++i)
  131. {
  132. float value = b2Dot(m_vertices[i], d);
  133. if (value > bestValue)
  134. {
  135. bestIndex = i;
  136. bestValue = value;
  137. }
  138. }
  139. return m_vertices[bestIndex];
  140. }
  141. #endif