b2_distance.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  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. #include "box2d/b2_circle_shape.h"
  19. #include "box2d/b2_distance.h"
  20. #include "box2d/b2_edge_shape.h"
  21. #include "box2d/b2_chain_shape.h"
  22. #include "box2d/b2_polygon_shape.h"
  23. // GJK using Voronoi regions (Christer Ericson) and Barycentric coordinates.
  24. B2_API int32 b2_gjkCalls, b2_gjkIters, b2_gjkMaxIters;
  25. void b2DistanceProxy::Set(const b2Shape* shape, int32 index)
  26. {
  27. switch (shape->GetType())
  28. {
  29. case b2Shape::e_circle:
  30. {
  31. const b2CircleShape* circle = static_cast<const b2CircleShape*>(shape);
  32. m_vertices = &circle->m_p;
  33. m_count = 1;
  34. m_radius = circle->m_radius;
  35. }
  36. break;
  37. case b2Shape::e_polygon:
  38. {
  39. const b2PolygonShape* polygon = static_cast<const b2PolygonShape*>(shape);
  40. m_vertices = polygon->m_vertices;
  41. m_count = polygon->m_count;
  42. m_radius = polygon->m_radius;
  43. }
  44. break;
  45. case b2Shape::e_chain:
  46. {
  47. const b2ChainShape* chain = static_cast<const b2ChainShape*>(shape);
  48. b2Assert(0 <= index && index < chain->m_count);
  49. m_buffer[0] = chain->m_vertices[index];
  50. if (index + 1 < chain->m_count)
  51. {
  52. m_buffer[1] = chain->m_vertices[index + 1];
  53. }
  54. else
  55. {
  56. m_buffer[1] = chain->m_vertices[0];
  57. }
  58. m_vertices = m_buffer;
  59. m_count = 2;
  60. m_radius = chain->m_radius;
  61. }
  62. break;
  63. case b2Shape::e_edge:
  64. {
  65. const b2EdgeShape* edge = static_cast<const b2EdgeShape*>(shape);
  66. m_vertices = &edge->m_vertex1;
  67. m_count = 2;
  68. m_radius = edge->m_radius;
  69. }
  70. break;
  71. default:
  72. b2Assert(false);
  73. }
  74. }
  75. void b2DistanceProxy::Set(const b2Vec2* vertices, int32 count, float radius)
  76. {
  77. m_vertices = vertices;
  78. m_count = count;
  79. m_radius = radius;
  80. }
  81. struct b2SimplexVertex
  82. {
  83. b2Vec2 wA; // support point in proxyA
  84. b2Vec2 wB; // support point in proxyB
  85. b2Vec2 w; // wB - wA
  86. float a; // barycentric coordinate for closest point
  87. int32 indexA; // wA index
  88. int32 indexB; // wB index
  89. };
  90. struct b2Simplex
  91. {
  92. void ReadCache( const b2SimplexCache* cache,
  93. const b2DistanceProxy* proxyA, const b2Transform& transformA,
  94. const b2DistanceProxy* proxyB, const b2Transform& transformB)
  95. {
  96. b2Assert(cache->count <= 3);
  97. // Copy data from cache.
  98. m_count = cache->count;
  99. b2SimplexVertex* vertices = &m_v1;
  100. for (int32 i = 0; i < m_count; ++i)
  101. {
  102. b2SimplexVertex* v = vertices + i;
  103. v->indexA = cache->indexA[i];
  104. v->indexB = cache->indexB[i];
  105. b2Vec2 wALocal = proxyA->GetVertex(v->indexA);
  106. b2Vec2 wBLocal = proxyB->GetVertex(v->indexB);
  107. v->wA = b2Mul(transformA, wALocal);
  108. v->wB = b2Mul(transformB, wBLocal);
  109. v->w = v->wB - v->wA;
  110. v->a = 0.0f;
  111. }
  112. // Compute the new simplex metric, if it is substantially different than
  113. // old metric then flush the simplex.
  114. if (m_count > 1)
  115. {
  116. float metric1 = cache->metric;
  117. float metric2 = GetMetric();
  118. if (metric2 < 0.5f * metric1 || 2.0f * metric1 < metric2 || metric2 < b2_epsilon)
  119. {
  120. // Reset the simplex.
  121. m_count = 0;
  122. }
  123. }
  124. // If the cache is empty or invalid ...
  125. if (m_count == 0)
  126. {
  127. b2SimplexVertex* v = vertices + 0;
  128. v->indexA = 0;
  129. v->indexB = 0;
  130. b2Vec2 wALocal = proxyA->GetVertex(0);
  131. b2Vec2 wBLocal = proxyB->GetVertex(0);
  132. v->wA = b2Mul(transformA, wALocal);
  133. v->wB = b2Mul(transformB, wBLocal);
  134. v->w = v->wB - v->wA;
  135. v->a = 1.0f;
  136. m_count = 1;
  137. }
  138. }
  139. void WriteCache(b2SimplexCache* cache) const
  140. {
  141. cache->metric = GetMetric();
  142. cache->count = uint16(m_count);
  143. const b2SimplexVertex* vertices = &m_v1;
  144. for (int32 i = 0; i < m_count; ++i)
  145. {
  146. cache->indexA[i] = uint8(vertices[i].indexA);
  147. cache->indexB[i] = uint8(vertices[i].indexB);
  148. }
  149. }
  150. b2Vec2 GetSearchDirection() const
  151. {
  152. switch (m_count)
  153. {
  154. case 1:
  155. return -m_v1.w;
  156. case 2:
  157. {
  158. b2Vec2 e12 = m_v2.w - m_v1.w;
  159. float sgn = b2Cross(e12, -m_v1.w);
  160. if (sgn > 0.0f)
  161. {
  162. // Origin is left of e12.
  163. return b2Cross(1.0f, e12);
  164. }
  165. else
  166. {
  167. // Origin is right of e12.
  168. return b2Cross(e12, 1.0f);
  169. }
  170. }
  171. default:
  172. b2Assert(false);
  173. return b2Vec2_zero;
  174. }
  175. }
  176. b2Vec2 GetClosestPoint() const
  177. {
  178. switch (m_count)
  179. {
  180. case 0:
  181. b2Assert(false);
  182. return b2Vec2_zero;
  183. case 1:
  184. return m_v1.w;
  185. case 2:
  186. return m_v1.a * m_v1.w + m_v2.a * m_v2.w;
  187. case 3:
  188. return b2Vec2_zero;
  189. default:
  190. b2Assert(false);
  191. return b2Vec2_zero;
  192. }
  193. }
  194. void GetWitnessPoints(b2Vec2* pA, b2Vec2* pB) const
  195. {
  196. switch (m_count)
  197. {
  198. case 0:
  199. b2Assert(false);
  200. break;
  201. case 1:
  202. *pA = m_v1.wA;
  203. *pB = m_v1.wB;
  204. break;
  205. case 2:
  206. *pA = m_v1.a * m_v1.wA + m_v2.a * m_v2.wA;
  207. *pB = m_v1.a * m_v1.wB + m_v2.a * m_v2.wB;
  208. break;
  209. case 3:
  210. *pA = m_v1.a * m_v1.wA + m_v2.a * m_v2.wA + m_v3.a * m_v3.wA;
  211. *pB = *pA;
  212. break;
  213. default:
  214. b2Assert(false);
  215. break;
  216. }
  217. }
  218. float GetMetric() const
  219. {
  220. switch (m_count)
  221. {
  222. case 0:
  223. b2Assert(false);
  224. return 0.0f;
  225. case 1:
  226. return 0.0f;
  227. case 2:
  228. return b2Distance(m_v1.w, m_v2.w);
  229. case 3:
  230. return b2Cross(m_v2.w - m_v1.w, m_v3.w - m_v1.w);
  231. default:
  232. b2Assert(false);
  233. return 0.0f;
  234. }
  235. }
  236. void Solve2();
  237. void Solve3();
  238. b2SimplexVertex m_v1, m_v2, m_v3;
  239. int32 m_count;
  240. };
  241. // Solve a line segment using barycentric coordinates.
  242. //
  243. // p = a1 * w1 + a2 * w2
  244. // a1 + a2 = 1
  245. //
  246. // The vector from the origin to the closest point on the line is
  247. // perpendicular to the line.
  248. // e12 = w2 - w1
  249. // dot(p, e) = 0
  250. // a1 * dot(w1, e) + a2 * dot(w2, e) = 0
  251. //
  252. // 2-by-2 linear system
  253. // [1 1 ][a1] = [1]
  254. // [w1.e12 w2.e12][a2] = [0]
  255. //
  256. // Define
  257. // d12_1 = dot(w2, e12)
  258. // d12_2 = -dot(w1, e12)
  259. // d12 = d12_1 + d12_2
  260. //
  261. // Solution
  262. // a1 = d12_1 / d12
  263. // a2 = d12_2 / d12
  264. void b2Simplex::Solve2()
  265. {
  266. b2Vec2 w1 = m_v1.w;
  267. b2Vec2 w2 = m_v2.w;
  268. b2Vec2 e12 = w2 - w1;
  269. // w1 region
  270. float d12_2 = -b2Dot(w1, e12);
  271. if (d12_2 <= 0.0f)
  272. {
  273. // a2 <= 0, so we clamp it to 0
  274. m_v1.a = 1.0f;
  275. m_count = 1;
  276. return;
  277. }
  278. // w2 region
  279. float d12_1 = b2Dot(w2, e12);
  280. if (d12_1 <= 0.0f)
  281. {
  282. // a1 <= 0, so we clamp it to 0
  283. m_v2.a = 1.0f;
  284. m_count = 1;
  285. m_v1 = m_v2;
  286. return;
  287. }
  288. // Must be in e12 region.
  289. float inv_d12 = 1.0f / (d12_1 + d12_2);
  290. m_v1.a = d12_1 * inv_d12;
  291. m_v2.a = d12_2 * inv_d12;
  292. m_count = 2;
  293. }
  294. // Possible regions:
  295. // - points[2]
  296. // - edge points[0]-points[2]
  297. // - edge points[1]-points[2]
  298. // - inside the triangle
  299. void b2Simplex::Solve3()
  300. {
  301. b2Vec2 w1 = m_v1.w;
  302. b2Vec2 w2 = m_v2.w;
  303. b2Vec2 w3 = m_v3.w;
  304. // Edge12
  305. // [1 1 ][a1] = [1]
  306. // [w1.e12 w2.e12][a2] = [0]
  307. // a3 = 0
  308. b2Vec2 e12 = w2 - w1;
  309. float w1e12 = b2Dot(w1, e12);
  310. float w2e12 = b2Dot(w2, e12);
  311. float d12_1 = w2e12;
  312. float d12_2 = -w1e12;
  313. // Edge13
  314. // [1 1 ][a1] = [1]
  315. // [w1.e13 w3.e13][a3] = [0]
  316. // a2 = 0
  317. b2Vec2 e13 = w3 - w1;
  318. float w1e13 = b2Dot(w1, e13);
  319. float w3e13 = b2Dot(w3, e13);
  320. float d13_1 = w3e13;
  321. float d13_2 = -w1e13;
  322. // Edge23
  323. // [1 1 ][a2] = [1]
  324. // [w2.e23 w3.e23][a3] = [0]
  325. // a1 = 0
  326. b2Vec2 e23 = w3 - w2;
  327. float w2e23 = b2Dot(w2, e23);
  328. float w3e23 = b2Dot(w3, e23);
  329. float d23_1 = w3e23;
  330. float d23_2 = -w2e23;
  331. // Triangle123
  332. float n123 = b2Cross(e12, e13);
  333. float d123_1 = n123 * b2Cross(w2, w3);
  334. float d123_2 = n123 * b2Cross(w3, w1);
  335. float d123_3 = n123 * b2Cross(w1, w2);
  336. // w1 region
  337. if (d12_2 <= 0.0f && d13_2 <= 0.0f)
  338. {
  339. m_v1.a = 1.0f;
  340. m_count = 1;
  341. return;
  342. }
  343. // e12
  344. if (d12_1 > 0.0f && d12_2 > 0.0f && d123_3 <= 0.0f)
  345. {
  346. float inv_d12 = 1.0f / (d12_1 + d12_2);
  347. m_v1.a = d12_1 * inv_d12;
  348. m_v2.a = d12_2 * inv_d12;
  349. m_count = 2;
  350. return;
  351. }
  352. // e13
  353. if (d13_1 > 0.0f && d13_2 > 0.0f && d123_2 <= 0.0f)
  354. {
  355. float inv_d13 = 1.0f / (d13_1 + d13_2);
  356. m_v1.a = d13_1 * inv_d13;
  357. m_v3.a = d13_2 * inv_d13;
  358. m_count = 2;
  359. m_v2 = m_v3;
  360. return;
  361. }
  362. // w2 region
  363. if (d12_1 <= 0.0f && d23_2 <= 0.0f)
  364. {
  365. m_v2.a = 1.0f;
  366. m_count = 1;
  367. m_v1 = m_v2;
  368. return;
  369. }
  370. // w3 region
  371. if (d13_1 <= 0.0f && d23_1 <= 0.0f)
  372. {
  373. m_v3.a = 1.0f;
  374. m_count = 1;
  375. m_v1 = m_v3;
  376. return;
  377. }
  378. // e23
  379. if (d23_1 > 0.0f && d23_2 > 0.0f && d123_1 <= 0.0f)
  380. {
  381. float inv_d23 = 1.0f / (d23_1 + d23_2);
  382. m_v2.a = d23_1 * inv_d23;
  383. m_v3.a = d23_2 * inv_d23;
  384. m_count = 2;
  385. m_v1 = m_v3;
  386. return;
  387. }
  388. // Must be in triangle123
  389. float inv_d123 = 1.0f / (d123_1 + d123_2 + d123_3);
  390. m_v1.a = d123_1 * inv_d123;
  391. m_v2.a = d123_2 * inv_d123;
  392. m_v3.a = d123_3 * inv_d123;
  393. m_count = 3;
  394. }
  395. void b2Distance(b2DistanceOutput* output,
  396. b2SimplexCache* cache,
  397. const b2DistanceInput* input)
  398. {
  399. ++b2_gjkCalls;
  400. const b2DistanceProxy* proxyA = &input->proxyA;
  401. const b2DistanceProxy* proxyB = &input->proxyB;
  402. b2Transform transformA = input->transformA;
  403. b2Transform transformB = input->transformB;
  404. // Initialize the simplex.
  405. b2Simplex simplex;
  406. simplex.ReadCache(cache, proxyA, transformA, proxyB, transformB);
  407. // Get simplex vertices as an array.
  408. b2SimplexVertex* vertices = &simplex.m_v1;
  409. const int32 k_maxIters = 20;
  410. // These store the vertices of the last simplex so that we
  411. // can check for duplicates and prevent cycling.
  412. int32 saveA[3], saveB[3];
  413. int32 saveCount = 0;
  414. // Main iteration loop.
  415. int32 iter = 0;
  416. while (iter < k_maxIters)
  417. {
  418. // Copy simplex so we can identify duplicates.
  419. saveCount = simplex.m_count;
  420. for (int32 i = 0; i < saveCount; ++i)
  421. {
  422. saveA[i] = vertices[i].indexA;
  423. saveB[i] = vertices[i].indexB;
  424. }
  425. switch (simplex.m_count)
  426. {
  427. case 1:
  428. break;
  429. case 2:
  430. simplex.Solve2();
  431. break;
  432. case 3:
  433. simplex.Solve3();
  434. break;
  435. default:
  436. b2Assert(false);
  437. }
  438. // If we have 3 points, then the origin is in the corresponding triangle.
  439. if (simplex.m_count == 3)
  440. {
  441. break;
  442. }
  443. // Get search direction.
  444. b2Vec2 d = simplex.GetSearchDirection();
  445. // Ensure the search direction is numerically fit.
  446. if (d.LengthSquared() < b2_epsilon * b2_epsilon)
  447. {
  448. // The origin is probably contained by a line segment
  449. // or triangle. Thus the shapes are overlapped.
  450. // We can't return zero here even though there may be overlap.
  451. // In case the simplex is a point, segment, or triangle it is difficult
  452. // to determine if the origin is contained in the CSO or very close to it.
  453. break;
  454. }
  455. // Compute a tentative new simplex vertex using support points.
  456. b2SimplexVertex* vertex = vertices + simplex.m_count;
  457. vertex->indexA = proxyA->GetSupport(b2MulT(transformA.q, -d));
  458. vertex->wA = b2Mul(transformA, proxyA->GetVertex(vertex->indexA));
  459. vertex->indexB = proxyB->GetSupport(b2MulT(transformB.q, d));
  460. vertex->wB = b2Mul(transformB, proxyB->GetVertex(vertex->indexB));
  461. vertex->w = vertex->wB - vertex->wA;
  462. // Iteration count is equated to the number of support point calls.
  463. ++iter;
  464. ++b2_gjkIters;
  465. // Check for duplicate support points. This is the main termination criteria.
  466. bool duplicate = false;
  467. for (int32 i = 0; i < saveCount; ++i)
  468. {
  469. if (vertex->indexA == saveA[i] && vertex->indexB == saveB[i])
  470. {
  471. duplicate = true;
  472. break;
  473. }
  474. }
  475. // If we found a duplicate support point we must exit to avoid cycling.
  476. if (duplicate)
  477. {
  478. break;
  479. }
  480. // New vertex is ok and needed.
  481. ++simplex.m_count;
  482. }
  483. b2_gjkMaxIters = b2Max(b2_gjkMaxIters, iter);
  484. // Prepare output.
  485. simplex.GetWitnessPoints(&output->pointA, &output->pointB);
  486. output->distance = b2Distance(output->pointA, output->pointB);
  487. output->iterations = iter;
  488. // Cache the simplex.
  489. simplex.WriteCache(cache);
  490. // Apply radii if requested
  491. if (input->useRadii)
  492. {
  493. if (output->distance < b2_epsilon)
  494. {
  495. // Shapes are too close to safely compute normal
  496. b2Vec2 p = 0.5f * (output->pointA + output->pointB);
  497. output->pointA = p;
  498. output->pointB = p;
  499. output->distance = 0.0f;
  500. }
  501. else
  502. {
  503. // Keep closest points on perimeter even if overlapped, this way
  504. // the points move smoothly.
  505. float rA = proxyA->m_radius;
  506. float rB = proxyB->m_radius;
  507. b2Vec2 normal = output->pointB - output->pointA;
  508. normal.Normalize();
  509. output->distance = b2Max(0.0f, output->distance - rA - rB);
  510. output->pointA += rA * normal;
  511. output->pointB -= rB * normal;
  512. }
  513. }
  514. }
  515. // GJK-raycast
  516. // Algorithm by Gino van den Bergen.
  517. // "Smooth Mesh Contacts with GJK" in Game Physics Pearls. 2010
  518. bool b2ShapeCast(b2ShapeCastOutput * output, const b2ShapeCastInput * input)
  519. {
  520. output->iterations = 0;
  521. output->lambda = 1.0f;
  522. output->normal.SetZero();
  523. output->point.SetZero();
  524. const b2DistanceProxy* proxyA = &input->proxyA;
  525. const b2DistanceProxy* proxyB = &input->proxyB;
  526. float radiusA = b2Max(proxyA->m_radius, b2_polygonRadius);
  527. float radiusB = b2Max(proxyB->m_radius, b2_polygonRadius);
  528. float radius = radiusA + radiusB;
  529. b2Transform xfA = input->transformA;
  530. b2Transform xfB = input->transformB;
  531. b2Vec2 r = input->translationB;
  532. b2Vec2 n(0.0f, 0.0f);
  533. float lambda = 0.0f;
  534. // Initial simplex
  535. b2Simplex simplex;
  536. simplex.m_count = 0;
  537. // Get simplex vertices as an array.
  538. b2SimplexVertex* vertices = &simplex.m_v1;
  539. // Get support point in -r direction
  540. int32 indexA = proxyA->GetSupport(b2MulT(xfA.q, -r));
  541. b2Vec2 wA = b2Mul(xfA, proxyA->GetVertex(indexA));
  542. int32 indexB = proxyB->GetSupport(b2MulT(xfB.q, r));
  543. b2Vec2 wB = b2Mul(xfB, proxyB->GetVertex(indexB));
  544. b2Vec2 v = wA - wB;
  545. // Sigma is the target distance between polygons
  546. float sigma = b2Max(b2_polygonRadius, radius - b2_polygonRadius);
  547. const float tolerance = 0.5f * b2_linearSlop;
  548. // Main iteration loop.
  549. const int32 k_maxIters = 20;
  550. int32 iter = 0;
  551. while (iter < k_maxIters && v.Length() - sigma > tolerance)
  552. {
  553. b2Assert(simplex.m_count < 3);
  554. output->iterations += 1;
  555. // Support in direction -v (A - B)
  556. indexA = proxyA->GetSupport(b2MulT(xfA.q, -v));
  557. wA = b2Mul(xfA, proxyA->GetVertex(indexA));
  558. indexB = proxyB->GetSupport(b2MulT(xfB.q, v));
  559. wB = b2Mul(xfB, proxyB->GetVertex(indexB));
  560. b2Vec2 p = wA - wB;
  561. // -v is a normal at p
  562. v.Normalize();
  563. // Intersect ray with plane
  564. float vp = b2Dot(v, p);
  565. float vr = b2Dot(v, r);
  566. if (vp - sigma > lambda * vr)
  567. {
  568. if (vr <= 0.0f)
  569. {
  570. return false;
  571. }
  572. lambda = (vp - sigma) / vr;
  573. if (lambda > 1.0f)
  574. {
  575. return false;
  576. }
  577. n = -v;
  578. simplex.m_count = 0;
  579. }
  580. // Reverse simplex since it works with B - A.
  581. // Shift by lambda * r because we want the closest point to the current clip point.
  582. // Note that the support point p is not shifted because we want the plane equation
  583. // to be formed in unshifted space.
  584. b2SimplexVertex* vertex = vertices + simplex.m_count;
  585. vertex->indexA = indexB;
  586. vertex->wA = wB + lambda * r;
  587. vertex->indexB = indexA;
  588. vertex->wB = wA;
  589. vertex->w = vertex->wB - vertex->wA;
  590. vertex->a = 1.0f;
  591. simplex.m_count += 1;
  592. switch (simplex.m_count)
  593. {
  594. case 1:
  595. break;
  596. case 2:
  597. simplex.Solve2();
  598. break;
  599. case 3:
  600. simplex.Solve3();
  601. break;
  602. default:
  603. b2Assert(false);
  604. }
  605. // If we have 3 points, then the origin is in the corresponding triangle.
  606. if (simplex.m_count == 3)
  607. {
  608. // Overlap
  609. return false;
  610. }
  611. // Get search direction.
  612. v = simplex.GetClosestPoint();
  613. // Iteration count is equated to the number of support point calls.
  614. ++iter;
  615. }
  616. if (iter == 0)
  617. {
  618. // Initial overlap
  619. return false;
  620. }
  621. // Prepare output.
  622. b2Vec2 pointA, pointB;
  623. simplex.GetWitnessPoints(&pointB, &pointA);
  624. if (v.LengthSquared() > 0.0f)
  625. {
  626. n = -v;
  627. n.Normalize();
  628. }
  629. output->point = pointA + radiusA * n;
  630. output->normal = n;
  631. output->lambda = lambda;
  632. output->iterations = iter;
  633. return true;
  634. }