Creating slices

I was able to "port" (i.e. make a few necessary WCK changes) Emanuele Feronato's object slicing. http://www.emanueleferonato.com/2011/06 ... x2d-part-2

With debugDraw on everthing works fine. But when I followed part 3 (adding your own sprites), the sprites were no longer moving with their physics bodies. I can align the bodies with the sprites with a StepEvent update in my Main class (extending WCK).

However, "normally" when I create my own classes e.g. MyRectangle extends Box (that has a corresponding movieclip in the flash library) WCK seem to take care of the StepEvent alignment. Am I missing something here or for sliced objects I just have to do it the way I described above?

Thanks,
Yeah WCK isn't really setup for easy "splitting". You have a couple options:

Pre-splitting: Let me explain by example. In my latest game I had rectangular watermelons you could blow up, where it would split the watermelon into chunks and they'd go flying. I actually just had two symbols, an intact watermelon and another that was just a container for the individual parts that went flying. When blown up the intact symbol would be replaced by the "split" symbol. So visually they looked the same but since the 2nd symbol was actually several BodyShapes it would fall apart (or fly apart once force is applied). The downside of this is you don't have dynamic, run-time splitting - everything is pre-split.

Run Time Splitting: You could override the BodyShape class to create a "SplitBodyShape" that takes the polygon vertices in it's constructor function then overrides the "shapes" function to build that polygon. Now you just need to copy the appropriate pixels from the original object. I'm not sure if image pixels can be copied for non-rectangular regions though. perhaps you could copy the complete original bitmap / symbol, build a mask out of the polygon information, then mask the original image... might work, haven't tried it.
Thanks for the quick reply mayo! 🙂

Yeah, I definitely need run time slicing (environment, "enemies" 😉 ) Before I saw your answer, I tried using the new slice polygon as a mask for my image/symbol (for now I'm using a movieclip) and seem to work 🙂 But my main issue was creating the slice in a manner that is consistent with WCK. That's where your suggestion of passing the vertices in SplitBodyShape constructor saved the day! 😀 Though, you do have to make sure that you orient your image. Part 3 of Emanuele Feronato's slicing tutorial just took the global coordinates and used those and only added single color sprites to them. Part 4, Antoan Angelov's code does take image orientation into account.

During testing, occasionally the c++ code threw back some kind of error. I found out that the errors happened when I tried to slice 'Circles'. Seems like 'Circles' are not made of polygons so slicing will not work on those.

A related question: Is there an easy/quick way to dynamically change/update the shape of an object during runtime (maybe for poly only) by giving it new vertices or is it usually done as: destroy old/create new object?
Yeah you can't slice circles. But if you approximate your circle as a polygon you can then slice that. If you're currently extending shapes.Circle, you can switch to extending shapes.Oval, which is a circle approximated by polygons. It may create more than one polygon though (because of the max # points a poly can have) so not sure if the split code handles that.

To change the shape you don't have to destroy/create the WCK object necessarily (although that may be the easiest way to do it). Your shapes/fixtures are stored in the b2fixtures array and your b2body is stored in the b2body property of BodyShapes. So you just have to use the lower-level pure Box2D calls to manipulate those.
You're right, the ovals do seem use more than one polygon and the code only handles the first object it finds. I was connecting objects with joints and had to figure out a way to make sure that the new SlicedBodyShapes remain connected properly. Even managed to slice the bridge in the WCK demo 😀
Funny that I did add a comment that if a body consists of more than one part, then I need to update the code. At this point, I don't think I'm that interested in splitting perfect circles. Close approximations are enough. If I really need to then I can update the code.

I did create and test a few polygons with the really handy Decompose Polygon script. I added an extra notification in case no shape is selected. The only thing missing from it is 'save to clipboard'. But even without that it's such a nice timesaver. Thanks for sharing.

Also, thanks for the tip on "updating" the shapes/fixtures. As I was looking for certain solutions on this forum I thought I saw some "unsupported" way but wasn't sure. After your comment, I searched for those again I did find the simple circle update:
Code:
b2fixtures[i].m_shape.m_radius *=.999;
b2body.ResetMassData();
This is something that was already on my "wishlist". 😀

But updating the full fixture / shape seems a bit tricky (at least for now). I did get it "working" but if I do the following, then b2fixtures will be empty. 😮
Code:
for(var i:int = 0; i < b2fixtures.length; ++i) {
   b2fixtures[i].destroy();
}
b2fixtures = new Vector.<b2Fixture>;

var points:Vector.<V2> = new Vector.<V2>;
points.push(new V2(0,0))
points.push(new V2(1,0))
points.push(new V2(0,1))

b2Def.polygon.Set(points);

b2Def.fixture.shape = b2Def.polygon;
b2Def.body.type = b2Body.b2_dynamicBody;

b2body.CreateFixture(b2Def.fixture);
b2body.ResetMassData();

Though some things are clearer, there are still quite a few things that are confusing. Perhaps, as usual, I'll just have to let it all sink in and crystallize. Then it'll make more sense.🙂

I have a few related questions about performance but I'll make a separate post for those. That way it may easier for others to find: viewtopic.php?f=19&t=8257
Quote:
then b2fixtures will be empty

Manually add your new shapes to the b2fixtures array. That way WCK can track them and know when to destroy them / link them to joints / etc. when appropriate.

Yeah, should've mentioned you can increase circle radiuses. You could probably change polygon points to but I have never tried it, since it's easy enough just to destroy / recreate.
Okay, finally, I'm beginning to see "who's doing what" 😀
Quote:
Manually add your new shapes to the b2fixtures array. That way WCK can track them and know when to destroy them / link them to joints / etc. when appropriate.
Thanks again for another great feedback/suggestion. 🙂 Yesterday, I though that createFixture should take care of adding the fixtures to the b2fixtures array but today I see that both b2body and BodyShape has their own createFixture. Of course, b2body's starts with 'C' and takes an argument, but when you're tired/confused ... 🙄 😆

If I understand it correctly, after I set up my new fixture, createFixture() should take care of adding the fixture to the b2fixtures array? It seems to work for the most part. The new shape is updated on screen and collisions work. However, something seems strange because my slicing code flails to slice the new polygon. Both entry and exit point are the same. I tried to debug but dont' have access to the c++ side. However, if I update the shape with fixture.GetShape().Set(newPolyVertices) then even slicing works. This, just like changing the cirlce radius in my previous post, seems like a simpler method of updating rather than fully replacing the fixture.
Quote:
Yeah, should've mentioned you can increase circle radiuses. You could probably change polygon points to but I have never tried it, since it's easy enough just to destroy / recreate.
And as others mentioned it:
b2body.ResetMassData();
otherwise, only the shape will change but not the mass, and
b2body.SetAwake(true);
in case the body is asleep.

If I do it this way, then I also have to update the corresponding symbol/image manually, right? The userData "connection" takes care of the movement and rotation but not scaling.

Original thread: https://box2d.org/forum/viewtopic.php?t=8246