Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for b2PolygonShape::ComputeDistance #124

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 19 additions & 26 deletions liquidfun/Box2D/Box2D/Collision/Shapes/b2PolygonShape.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -262,43 +262,36 @@ void b2PolygonShape::ComputeDistance(const b2Transform& xf, const b2Vec2& p, flo
B2_NOT_USED(childIndex);

b2Vec2 pLocal = b2MulT(xf.q, p - xf.p);
float32 maxDistance = -FLT_MAX;
b2Vec2 normalForMaxDistance = pLocal;

for (int32 i = 0; i < m_count; ++i)
{
float32 dot = b2Dot(m_normals[i], pLocal - m_vertices[i]);
if (dot > maxDistance)
const b2Vec2& p0 = m_vertices[i - 1 >= 0 ? i - 1 : m_count - 1];
const b2Vec2& p1 = m_vertices[i];
const b2Vec2& p2 = m_vertices[i + 1 < m_count ? i + 1 : 0];
b2Vec2 dist = pLocal - p1;
if (b2Dot(dist, p1 - p0) > 0.0f && b2Dot(dist, p1 - p2) > 0.0f)
{
maxDistance = dot;
normalForMaxDistance = m_normals[i];
*distance = dist.Length();
*normal = b2Mul(xf.q, dist);
if (*distance > 0.0f)
*normal /= *distance;
return;
}
}

if (maxDistance > 0)
float32 maxDistance = -FLT_MAX;
b2Vec2 normalForMaxDistance = pLocal;
for (int32 i = 0; i < m_count; ++i)
{
b2Vec2 minDistance = normalForMaxDistance;
float32 minDistance2 = maxDistance * maxDistance;
for (int32 i = 0; i < m_count; ++i)
float32 dot = b2Dot(m_normals[i], pLocal - m_vertices[i]);
if (dot > maxDistance)
{
b2Vec2 distance = pLocal - m_vertices[i];
float32 distance2 = distance.LengthSquared();
if (minDistance2 > distance2)
{
minDistance = distance;
minDistance2 = distance2;
}
maxDistance = dot;
normalForMaxDistance = m_normals[i];
}

*distance = b2Sqrt(minDistance2);
*normal = b2Mul(xf.q, minDistance);
normal->Normalize();
}
else
{
*distance = maxDistance;
*normal = b2Mul(xf.q, normalForMaxDistance);
}
*distance = maxDistance;
*normal = b2Mul(xf.q, normalForMaxDistance);
}

bool b2PolygonShape::RayCast(b2RayCastOutput* output, const b2RayCastInput& input,
Expand Down