I have been annoyed many times be the fact that box2d shapes must be wound in counter-clockwise direction. I can't think of any good reason anyone would ever want the option to make a polygon and have it mysterious;y fall through everything else. I think if people want that to happen, they should have to go out of their way to make an additional functional call in order to let that happen. By default, all custom made polygons ought to be automatically CCW by law.
Anyways, I have written some code (borrowing from the wikipedia entry and a stack overflow post I found on how to calculate the signed area of a polygon) that will automatically solve this issue if appended into the the Box2D code.
I did a search and didn't find anything like this as an outstanding feature request. Anyone think this is a good idea? Beuller?
//take a list of vertices you intend to turn into a polygon
//they, obviously, must be arranged in linear order (derp de derp)
//rewind the array if the points are clockwise
if (!SignedAreaIsPositive(l))
l.reverse();
//postive signed area means counter clockwise
//negative signed area means clockwise.
function SignedAreaIsPositive(vertices:Array):Boolean
{
var centroid:Point = new b2Vec2(0,0);
var signedArea:Number = 0.0;
var x0:Number = 0.0; // Current vertex X
var y0:Number = 0.0; // Current vertex Y
var x1:Number = 0.0; // Next vertex X
var y1:Number = 0.0; // Next vertex Y
var a:Number = 0.0; // Partial signed area
// For all vertices except last
var i:int = 0;
for (i=0; i < vertices.length - 1; i++)
{
x0 = vertices[i].x;
y0 = vertices[i].y;
x1 = vertices[i+1].x;
y1 = vertices[i+1].y;
a = x0*y1 - x1*y0;
signedArea += a;
centroid.x += (x0 + x1)*a;
centroid.y += (y0 + y1)*a;
}
// Do last vertex
x0 = vertices[i].x;
y0 = vertices[i].y;
x1 = vertices[0].x;
y1 = vertices[0].y;
a = x0*y1 - x1*y0;
signedArea += a;
centroid.x += (x0 + x1)*a;
centroid.y += (y0 + y1)*a;
signedArea *= 0.5;
centroid.x /= (6*signedArea);
centroid.y /= (6*signedArea);
return (signedArea > 0);
}
// Do last vertex
x0 = vertices[i].x;
y0 = vertices[i].y;
x1 = vertices[0].x;
y1 = vertices[0].y;
a = x0*y1 - x1*y0;
signedArea += a;
centroid.x += (x0 + x1)*a;
centroid.y += (y0 + y1)*a;
signedArea *= 0.5;
centroid.x /= (6*signedArea);
centroid.y /= (6*signedArea);
return (signedArea > 0);
}
Anyways, I have written some code (borrowing from the wikipedia entry and a stack overflow post I found on how to calculate the signed area of a polygon) that will automatically solve this issue if appended into the the Box2D code.
I did a search and didn't find anything like this as an outstanding feature request. Anyone think this is a good idea? Beuller?
//take a list of vertices you intend to turn into a polygon
//they, obviously, must be arranged in linear order (derp de derp)
//rewind the array if the points are clockwise
if (!SignedAreaIsPositive(l))
l.reverse();
//postive signed area means counter clockwise
//negative signed area means clockwise.
function SignedAreaIsPositive(vertices:Array):Boolean
{
var centroid:Point = new b2Vec2(0,0);
var signedArea:Number = 0.0;
var x0:Number = 0.0; // Current vertex X
var y0:Number = 0.0; // Current vertex Y
var x1:Number = 0.0; // Next vertex X
var y1:Number = 0.0; // Next vertex Y
var a:Number = 0.0; // Partial signed area
// For all vertices except last
var i:int = 0;
for (i=0; i < vertices.length - 1; i++)
{
x0 = vertices[i].x;
y0 = vertices[i].y;
x1 = vertices[i+1].x;
y1 = vertices[i+1].y;
a = x0*y1 - x1*y0;
signedArea += a;
centroid.x += (x0 + x1)*a;
centroid.y += (y0 + y1)*a;
}
// Do last vertex
x0 = vertices[i].x;
y0 = vertices[i].y;
x1 = vertices[0].x;
y1 = vertices[0].y;
a = x0*y1 - x1*y0;
signedArea += a;
centroid.x += (x0 + x1)*a;
centroid.y += (y0 + y1)*a;
signedArea *= 0.5;
centroid.x /= (6*signedArea);
centroid.y /= (6*signedArea);
return (signedArea > 0);
}
// Do last vertex
x0 = vertices[i].x;
y0 = vertices[i].y;
x1 = vertices[0].x;
y1 = vertices[0].y;
a = x0*y1 - x1*y0;
signedArea += a;
centroid.x += (x0 + x1)*a;
centroid.y += (y0 + y1)*a;
signedArea *= 0.5;
centroid.x /= (6*signedArea);
centroid.y /= (6*signedArea);
return (signedArea > 0);
}