Page 1 of 1

Help Move related bodies

Posted: Sun Apr 07, 2013 12:26 pm
by blakey87
Hi IForce

I need help moving related bodies, as I have copy and pasted model on top of model, but now need to move each model independently, is there any script which could achieve this?
See attachment for further clarity

Re: Help Move related bodies

Posted: Tue Apr 09, 2013 1:22 am
by iforce2d
What do you mean by related bodies? You mean bodies that are connected with joints? Bodies with the same name? Bodies that have some custom property in common?

I made a script that selects all bodies connected in a structure, maybe that would help: viewtopic.php?f=9&t=113

Alternatively, here is a script that will take each 'island' of connected bodies and automatically space them out in horizontally. Change the parameters for the translate call at the end to alter the spacing:

Code: Select all

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

int n = 0; // for offset
while ( bs.length > 0 ) {
	body b = bs[0];
	bs.removeAt(0);
	if ( fb.find(b) != -1 )
		continue;
	cb.insertLast(b);
	body[] island;
	while ( cb.length > 0 ) {
		body b = cb[0];
		cb.removeAt(0);
		if ( fb.find(b) != -1 )
			continue;
		fb.insertLast(b);
		island.insertLast(b);
		joint[] js = b.getJoints();
		for (uint i = 0; i < js.length; i++) {
			b = js[i].getBodyA();
			if ( cb.find(b) == -1 && fb.find(b) == -1 )
				cb.insertLast(b);
			b = js[i].getBodyB();
			if ( cb.find(b) == -1 && fb.find(b) == -1 )
				cb.insertLast(b);
		}
	}
	translate( island, n * mv(2,0) );
	n++;
}
Example usage:
spaceout.png
spaceout.png (58.64 KiB) Viewed 8492 times
After the bodies are made easy to select like this, you should give each island some name or property to identify it with, otherwise your workflow will be chaos ;)

For example, you could add an integer property to bodies to hold the animation frame they belong to (as I guess that is what you are doing here). Then you could use a script like this to select all bodies in a specific frame at a later time:

Code: Select all

int whichFrame = -1;

bool matchingFrameNumber(body b) {
	return b.getCustomInt( "frame" ) == whichFrame;
}

void main() {
	whichFrame = queryNumericValue("Which frame?");
	select( filterBodies( ab(), matchingFrameNumber ) );
}

Re: Help Move related bodies

Posted: Tue Apr 09, 2013 8:11 pm
by blakey87
Yeah related by joints

I am trying the function but the translate function is throwing an error

Line 36, col 25: No matching signatures to 'mv(const uint, const uint)'
Line 36, col 2: No matching signatures to 'translate(body[]&, int)'

Thanks!

Re: Help Move related bodies

Posted: Tue Apr 09, 2013 11:21 pm
by iforce2d
You need version 1.3