Switch fixture's body

What would you like to see in future versions of the editor?
Post Reply
tescott
Posts: 68
Joined: Wed Feb 06, 2013 6:32 pm

Switch fixture's body

Post by tescott » Mon Jul 15, 2013 10:15 pm

I'd like to see an option to switch a fixture's 'parent' body. It doesn't look I can do this via scripts as far as I can tell. With the recent split script I created, I found the need to switch a fixture to a brand new body. The workaround I came up with involved copying the original body and all of its associated fixtures to a new scene, then deleting all other fixtures from the body... and THEN copying that body back to the original scene. Pretty tedious.

If there's a way to do this that I'm not seeing, I'd be interested to know.

Thanks,
--tim

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

Re: Switch fixture's body

Post by iforce2d » Tue Jul 16, 2013 2:49 am

Via the GUI there is copy+paste, then delete the original. I suppose a button to select the body of a fixture would be handy too, like how the body of an image can be changed. But I guess you are mainly talking scripts right? In that case no, there is no simple function to do that. I am not sure why you would need a separate scene to do this, but I can show you one way it could be done using the jsonEncode(dictionary) function that was added in the last update.

I didn't make a big deal out of this function since there is no corresponding jsonDecode function yet, and it is not likely to be commonly used apart from by 133t scripters like yourself and the folks who requested it. It lets you create a structure of 'associative arrays' with the Angelscript 'dictionary' type, and then convert this to a JSON string which can be given to RUBE functions like addBody, addFixture etc.

This lets you create things in a very similar way to how you would when programming everything manually... not exactly in line with the goal of a graphical editor, but handy as a fallback :) Here is an example of how you would create a fixture definition, and then use that to add a fixture to a body:

Code: Select all

//vertices for shape
vec2[] vs;
vs.insertLast( mv(-0.5,-0.5) );
vs.insertLast( mv( 0.5,-0.5) );
vs.insertLast( mv( 0.5, 0.5) );

//fixture def
dictionary fd;
fd.set("density",1);
fd.set("friction",0.5);
fd.set("vertices", vs);

//shape def
dictionary sd;
sd.set("type","polygon");

//fixtures can have multiple shapes, so need an array
//use references so we can alter the shape properties later
dictionary@[] ss;
ss.insertLast(@sd);
fd.set("shapes",ss);

//add the fixture to selected body
sb()[0].addFixture(-1, jsonEncode(fd) );
Internally RUBE does duplicating and copy+paste etc by passing around JSON strings, so it would not be too much work to add a second version, eg. jsonEncode(fixture) that returns the JSON string of a given fixture, body etc. which would allow much simpler direct copying like you're talking about. However, that would not be nearly as useful as a function that returns a dictionary representation of an item, so that individual parts of it could be easily modified. Here is what I would ideally like to be able to do:

Code: Select all

fixture f = sf()[0];
dictionary fd = makeDictionary( f );
fd.set("friction", 0.5);
sb()[0].addFixture(-1, jsonEncode(fd) );
I've attached a scene with a sample usage of the jsonEncode function. Run the script to create some bodies with varying shape types, restitution etc. Incidentally, the use of @ in these examples is not really necessary, but it allows you to modify parts of the structure after adding them, instead of merely assigning by value. For example, continuing on from the first code block above, you could do this:

Code: Select all

sd.set("type", "loop");
sb()[0].addFixture(-1, jsonEncode(fd) );
...to change just the shape type and create another fixture with all other properties the same.
Attachments
jsonEncodeTest.rube
(2.7 KiB) Downloaded 803 times

tescott
Posts: 68
Joined: Wed Feb 06, 2013 6:32 pm

Re: Switch fixture's body

Post by tescott » Tue Jul 16, 2013 3:35 am

Yeah, I probably didn't need an entirely new scene to do my work. However, I have a 'ground' body where pretty much all of my static fixtures are tied to. I needed to cut off a single fixture from this -- so I was C&P'ing quite a few fixtures. A new scene provided a nice clean slate to easily delete the unnecessary textures without accidentally nuking something I wanted to keep... and I guess I could have gotten around that by shifting things off far enough before moving the isolated body/fixture back into position.

Oh well.

Thanks for the tip on the jsonEncode functionality.

--tim

Post Reply