At the moment I'm experimenting with character movement, using a rectangular body to represent a torso, and a 'feeler' extending below the body to check what the character is standing on. The feeler is a b2Segment, which is checked against objects in the world with the b2PolygonShape::TestSegment function.
Occasionally I noticed my character standing on nothing, and eventually narrowed down the problem to the case where the segment is perfectly parallel to one of the sides of the shape. Admittedly this is quite an unlikely occurence, except for at the beginning of the simulation when many objects still have their initial angle of a perfect zero.
To remedy this I added a third case to the if/else block in TestSegment as below. Perhaps there is a better way to address the problem, but it seems to be working well enough for me so far.
Before:
After:
Occasionally I noticed my character standing on nothing, and eventually narrowed down the problem to the case where the segment is perfectly parallel to one of the sides of the shape. Admittedly this is quite an unlikely occurence, except for at the beginning of the simulation when many objects still have their initial angle of a perfect zero.
To remedy this I added a third case to the if/else block in TestSegment as below. Perhaps there is a better way to address the problem, but it seems to be working well enough for me so far.
Before:
Code:
if (denominator < 0.0f && numerator < lower * denominator)
{
// Increase lower.
// The segment enters this half-space.
lower = numerator / denominator;
index = i;
}
else if (denominator > 0.0f && numerator < upper * denominator)
{
// Decrease upper.
// The segment exits this half-space.
upper = numerator / denominator;
}
{
// Increase lower.
// The segment enters this half-space.
lower = numerator / denominator;
index = i;
}
else if (denominator > 0.0f && numerator < upper * denominator)
{
// Decrease upper.
// The segment exits this half-space.
upper = numerator / denominator;
}
After:
Code:
if (denominator < 0.0f && numerator < lower * denominator)
{
// Increase lower.
// The segment enters this half-space.
lower = numerator / denominator;
index = i;
}
else if (denominator > 0.0f && numerator < upper * denominator)
{
// Decrease upper.
// The segment exits this half-space.
upper = numerator / denominator;
}
else if (denominator == 0.0f) {
// The segment is parallel to this edge
// Check if it is outside the half-space
float32 normalDotp1 = b2Dot(m_normals[i], p1);
float32 normalDotVertex = b2Dot(m_normals[i], m_vertices[i]);
if ( normalDotp1 > normalDotVertex )
return false;
}
{
// Increase lower.
// The segment enters this half-space.
lower = numerator / denominator;
index = i;
}
else if (denominator > 0.0f && numerator < upper * denominator)
{
// Decrease upper.
// The segment exits this half-space.
upper = numerator / denominator;
}
else if (denominator == 0.0f) {
// The segment is parallel to this edge
// Check if it is outside the half-space
float32 normalDotp1 = b2Dot(m_normals[i], p1);
float32 normalDotVertex = b2Dot(m_normals[i], m_vertices[i]);
if ( normalDotp1 > normalDotVertex )
return false;
}

