Help Move related bodies

General discussion about the R.U.B.E editor
Post Reply
blakey87
Posts: 28
Joined: Wed Jan 09, 2013 7:57 pm

Help Move related bodies

Post 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
Attachments
bodies.jpg
bodies.jpg (30.1 KiB) Viewed 8470 times
iforce2d
Site Admin
Posts: 861
Joined: Sat Dec 22, 2012 7:20 pm

Re: Help Move related bodies

Post 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 8464 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 ) );
}
blakey87
Posts: 28
Joined: Wed Jan 09, 2013 7:57 pm

Re: Help Move related bodies

Post 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!
iforce2d
Site Admin
Posts: 861
Joined: Sat Dec 22, 2012 7:20 pm

Re: Help Move related bodies

Post by iforce2d »

You need version 1.3
Post Reply