@@ -262,43 +262,36 @@ void b2PolygonShape::ComputeDistance(const b2Transform& xf, const b2Vec2& p, flo
262262 B2_NOT_USED (childIndex);
263263
264264 b2Vec2 pLocal = b2MulT (xf.q , p - xf.p );
265- float32 maxDistance = -FLT_MAX;
266- b2Vec2 normalForMaxDistance = pLocal;
267265
268266 for (int32 i = 0 ; i < m_count; ++i)
269267 {
270- float32 dot = b2Dot (m_normals[i], pLocal - m_vertices[i]);
271- if (dot > maxDistance)
268+ const b2Vec2& p0 = m_vertices[i - 1 >= 0 ? i - 1 : m_count - 1 ];
269+ const b2Vec2& p1 = m_vertices[i];
270+ const b2Vec2& p2 = m_vertices[i + 1 < m_count ? i + 1 : 0 ];
271+ b2Vec2 dist = pLocal - p1;
272+ if (b2Dot (dist, p1 - p0) > 0 .0f && b2Dot (dist, p1 - p2) > 0 .0f )
272273 {
273- maxDistance = dot;
274- normalForMaxDistance = m_normals[i];
274+ *distance = dist.Length ();
275+ *normal = b2Mul (xf.q , dist);
276+ if (*distance > 0 .0f )
277+ *normal /= *distance;
278+ return ;
275279 }
276280 }
277281
278- if (maxDistance > 0 )
282+ float32 maxDistance = -FLT_MAX;
283+ b2Vec2 normalForMaxDistance = pLocal;
284+ for (int32 i = 0 ; i < m_count; ++i)
279285 {
280- b2Vec2 minDistance = normalForMaxDistance;
281- float32 minDistance2 = maxDistance * maxDistance;
282- for (int32 i = 0 ; i < m_count; ++i)
286+ float32 dot = b2Dot (m_normals[i], pLocal - m_vertices[i]);
287+ if (dot > maxDistance)
283288 {
284- b2Vec2 distance = pLocal - m_vertices[i];
285- float32 distance2 = distance.LengthSquared ();
286- if (minDistance2 > distance2)
287- {
288- minDistance = distance;
289- minDistance2 = distance2;
290- }
289+ maxDistance = dot;
290+ normalForMaxDistance = m_normals[i];
291291 }
292-
293- *distance = b2Sqrt (minDistance2);
294- *normal = b2Mul (xf.q , minDistance);
295- normal->Normalize ();
296- }
297- else
298- {
299- *distance = maxDistance;
300- *normal = b2Mul (xf.q , normalForMaxDistance);
301292 }
293+ *distance = maxDistance;
294+ *normal = b2Mul (xf.q , normalForMaxDistance);
302295}
303296
304297bool b2PolygonShape::RayCast (b2RayCastOutput* output, const b2RayCastInput& input,
0 commit comments