Splitting fixtures

Let us know if you have a nice R.U.B.E script to share!
Post Reply
tescott
Posts: 68
Joined: Wed Feb 06, 2013 6:32 pm

Splitting fixtures

Post by tescott » Sun Jul 14, 2013 7:35 pm

Here's a script to "split" fixtures based on two selected vertices. Select two non-adjacent vertices on a single fixture. Invoke the script. Creates two new fixtures associated with the body, split between the two vertices. See below for example screenshots.

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();
step1.png
Vertices selected
step1.png (17.08 KiB) Viewed 12483 times
step2.png
Script invoked and split occurs
step2.png (19.7 KiB) Viewed 12483 times
step3.png
Example of fixtures pulled apart
step3.png (22.93 KiB) Viewed 12483 times

iforce2d
Site Admin
Posts: 860
Joined: Sat Dec 22, 2012 7:20 pm

Re: Splitting fixtures

Post by iforce2d » Mon Jul 15, 2013 2:12 pm

Now that is a pretty sweet script...!

Post Reply