Page 1 of 1

Select all bodies connected to selected bodies

Posted: Tue Apr 09, 2013 12:46 am
by iforce2d
This script will select all bodies that are connected by joints to the currently selected bodies, which is handy when you have a large jointed structure and you want to select all of it without clicking on each body.

Code: Select all

body[] bs = sb();
body[] fb; // finished bodies

while ( bs.length > 0 ) {
	body b = bs[0];
	bs.removeAt(0);
	if ( fb.find(b) != -1 )
		continue;
	fb.insertLast(b);
	b.select();
	joint[] js = b.getJoints();
	for (uint i = 0; i < js.length; i++) {
		b = js[i].getBodyA();
		if ( bs.find(b) == -1 && fb.find(b) == -1 )
			bs.insertLast(b);
		b = js[i].getBodyB();
		if ( bs.find(b) == -1 && fb.find(b) == -1 )
			bs.insertLast(b);
	}
}