gjk.lua 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. --[[
  2. Copyright (c) 2012 Matthias Richter
  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
  10. all copies or substantial portions of the Software.
  11. Except as contained in this notice, the name(s) of the above copyright holders
  12. shall not be used in advertising or otherwise to promote the sale, use or
  13. other dealings in this Software without prior written authorization.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. THE SOFTWARE.
  21. ]]--
  22. local _PACKAGE = (...):match("^(.+)%.[^%.]+")
  23. local vector = require(_PACKAGE .. '.vector-light')
  24. local huge, abs = math.huge, math.abs
  25. local function support(shape_a, shape_b, dx, dy)
  26. local x,y = shape_a:support(dx,dy)
  27. return vector.sub(x,y, shape_b:support(-dx, -dy))
  28. end
  29. -- returns closest edge to the origin
  30. local function closest_edge(simplex)
  31. local e = {dist = huge}
  32. local i = #simplex-1
  33. for k = 1,#simplex-1,2 do
  34. local ax,ay = simplex[i], simplex[i+1]
  35. local bx,by = simplex[k], simplex[k+1]
  36. i = k
  37. local ex,ey = vector.perpendicular(bx-ax, by-ay)
  38. local nx,ny = vector.normalize(ex,ey)
  39. local d = vector.dot(ax,ay, nx,ny)
  40. if d < e.dist then
  41. e.dist = d
  42. e.nx, e.ny = nx, ny
  43. e.i = k
  44. end
  45. end
  46. return e
  47. end
  48. local function EPA(shape_a, shape_b, simplex)
  49. -- make sure simplex is oriented counter clockwise
  50. local cx,cy, bx,by, ax,ay = unpack(simplex)
  51. if vector.dot(ax-bx,ay-by, cx-bx,cy-by) < 0 then
  52. simplex[1],simplex[2] = ax,ay
  53. simplex[5],simplex[6] = cx,cy
  54. end
  55. -- the expanding polytype algorithm
  56. local is_either_circle = shape_a._center or shape_b._center
  57. local last_diff_dist = huge
  58. while true do
  59. local e = closest_edge(simplex)
  60. local px,py = support(shape_a, shape_b, e.nx, e.ny)
  61. local d = vector.dot(px,py, e.nx, e.ny)
  62. local diff_dist = d - e.dist
  63. if diff_dist < 1e-6 or (is_either_circle and abs(last_diff_dist - diff_dist) < 1e-10) then
  64. return -d*e.nx, -d*e.ny
  65. end
  66. last_diff_dist = diff_dist
  67. -- simplex = {..., simplex[e.i-1], px, py, simplex[e.i]
  68. table.insert(simplex, e.i, py)
  69. table.insert(simplex, e.i, px)
  70. end
  71. end
  72. -- : : origin must be in plane between A and B
  73. -- B o------o A since A is the furthest point on the MD
  74. -- : : in direction of the origin.
  75. local function do_line(simplex)
  76. local bx,by, ax,ay = unpack(simplex)
  77. local abx,aby = bx-ax, by-ay
  78. local dx,dy = vector.perpendicular(abx,aby)
  79. if vector.dot(dx,dy, -ax,-ay) < 0 then
  80. dx,dy = -dx,-dy
  81. end
  82. return simplex, dx,dy
  83. end
  84. -- B .'
  85. -- o-._ 1
  86. -- | `-. .' The origin can only be in regions 1, 3 or 4:
  87. -- | 4 o A 2 A lies on the edge of the MD and we came
  88. -- | _.-' '. from left of BC.
  89. -- o-' 3
  90. -- C '.
  91. local function do_triangle(simplex)
  92. local cx,cy, bx,by, ax,ay = unpack(simplex)
  93. local aox,aoy = -ax,-ay
  94. local abx,aby = bx-ax, by-ay
  95. local acx,acy = cx-ax, cy-ay
  96. -- test region 1
  97. local dx,dy = vector.perpendicular(abx,aby)
  98. if vector.dot(dx,dy, acx,acy) > 0 then
  99. dx,dy = -dx,-dy
  100. end
  101. if vector.dot(dx,dy, aox,aoy) > 0 then
  102. -- simplex = {bx,by, ax,ay}
  103. simplex[1], simplex[2] = bx,by
  104. simplex[3], simplex[4] = ax,ay
  105. simplex[5], simplex[6] = nil, nil
  106. return simplex, dx,dy
  107. end
  108. -- test region 3
  109. dx,dy = vector.perpendicular(acx,acy)
  110. if vector.dot(dx,dy, abx,aby) > 0 then
  111. dx,dy = -dx,-dy
  112. end
  113. if vector.dot(dx,dy, aox, aoy) > 0 then
  114. -- simplex = {cx,cy, ax,ay}
  115. simplex[3], simplex[4] = ax,ay
  116. simplex[5], simplex[6] = nil, nil
  117. return simplex, dx,dy
  118. end
  119. -- must be in region 4
  120. return simplex
  121. end
  122. local function GJK(shape_a, shape_b)
  123. local ax,ay = support(shape_a, shape_b, 1,0)
  124. if ax == 0 and ay == 0 then
  125. -- only true if shape_a and shape_b are touching in a vertex, e.g.
  126. -- .--- .---.
  127. -- | A | .-. | B | support(A, 1,0) = x
  128. -- '---x---. or : A :x---' support(B, -1,0) = x
  129. -- | B | `-' => support(A,B,1,0) = x - x = 0
  130. -- '---'
  131. -- Since CircleShape:support(dx,dy) normalizes dx,dy we have to opt
  132. -- out or the algorithm blows up. In accordance to the cases below
  133. -- choose to judge this situation as not colliding.
  134. return false
  135. end
  136. local simplex = {ax,ay}
  137. local n = 2
  138. local dx,dy = -ax,-ay
  139. -- first iteration: line case
  140. ax,ay = support(shape_a, shape_b, dx,dy)
  141. if vector.dot(ax,ay, dx,dy) <= 0 then
  142. return false
  143. end
  144. simplex[n+1], simplex[n+2] = ax,ay
  145. simplex, dx, dy = do_line(simplex, dx, dy)
  146. n = 4
  147. -- all other iterations must be the triangle case
  148. while true do
  149. ax,ay = support(shape_a, shape_b, dx,dy)
  150. if vector.dot(ax,ay, dx,dy) <= 0 then
  151. return false
  152. end
  153. simplex[n+1], simplex[n+2] = ax,ay
  154. simplex, dx, dy = do_triangle(simplex, dx,dy)
  155. n = #simplex
  156. if n == 6 then
  157. return true, EPA(shape_a, shape_b, simplex)
  158. end
  159. end
  160. end
  161. return GJK