One limitation: results are undefined if you try to split using two vertices that have an axis crossing a concave section.

--tim

Code: Select all

```
//
// This script accepts 2 vertices on a single fixture and "splits" the fixture along those vertices.
// In reality, it duplicates the source fixture twice, and deletes either 'half' of the fixture to
// get two fixtures, then it deletes the source fixture.
//
vertex [] vertices = getSelectedVertices();
if (vertices[0].next() == vertices[1])
{
print("Can't split at an edge. Aborting...");
return;
}
else if (vertices.length != 2)
{
print("Can only split along two vertices. Aborting...");
return;
}
else if ((vertices[0].x == vertices[1].x) && (vertices[0].y == vertices[1].y))
{
print("Vertices have same coordinates. Aborting...");
return;
}
else if (vertices[0].getFixture() != vertices[1].getFixture())
{
print("Vertices must be selected from common fixture. Aborting...");
return;
}
fixture parentFix = vertices[0].getFixture();
// First, determine the start and end indices...
uint startIndex = 0;
uint endIndex = 0;
vertex [] allVertices = parentFix.getVertices();
for (uint i = 0; i < allVertices.length; i++)
{
if ((allVertices[i].x == vertices[0].x) && (allVertices[i].y == vertices[0].y))
{
startIndex = i;
}
if ((allVertices[i].x == vertices[1].x) && (allVertices[i].y == vertices[1].y))
{
endIndex = i;
}
}
//
// Duplicate the parent
// Delete child vertices from (startIndex..endIndex) (exclusive)
//
fixture newFix1 = duplicate(parentFix);
int count = abs(endIndex - startIndex) - 1;
for (int i = 0; i < count; i++)
{
newFix1.deleteVertex(startIndex + 1); // do not delete the vertex at the starting point!
}
//
// Duplicate the parent
// Delete child vertices from [0..startIndex) (exclusive)
// from (endIndex..lastIndex] (exclusive)
fixture newFix2 = duplicate(parentFix);
count = newFix2.getNumVertices() - endIndex - 1;
for (int i = 0; i < count; i++)
{
newFix2.deleteVertex(endIndex + 1); // do not delete the vertex at the ending point
}
count = startIndex;
for (int i = 0; i < count; i++)
{
newFix2.deleteVertex(0); // delete vertices up to the starting index node
}
// delete the parent
parentFix.delete();
```