Select all bodies connected to selected bodies

Let us know if you have a nice R.U.B.E script to share!
Post Reply
iforce2d
Site Admin
Posts: 860
Joined: Sat Dec 22, 2012 7:20 pm

Select all bodies connected to selected bodies

Post by iforce2d » Tue Apr 09, 2013 12:46 am

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);
	}
}

Post Reply