Deformable and breakable body - sample

Mostly I'm just trying to work out what the best approach would be for my game. I hope this isn't too offtopic here, but given that your approach seems to be quite close to what I'm looking for, I hope this is the right thread 😉

Some stuff in my game will be unbreakable. Obviously this is pretty straightforward. Other stuff needs to be breakable, but depending on its physical properties and the effects that happen to/near it, the breaking up of the bodies would need to appear in a variety of different ways. Some things might shatter, like glass in an explosion; some things might sag and melt (like going from a solid shape to something like in your code with loose joints that eventually give way under the weight of the bodes); some things might get sliced in clean lines; some things might get pieces chipped off like someone taking a chisel to soft stone.

One question (and I think it might be one for another thread) is how you go about breaking these entities up into smaller bodies. I guess the question I'm interested in here is how do I get a number of bodies to "stick together" in such a way that they seem solid, even if pieces are removed? My current thinking is that I should go with your approach of stitching lots of bodies together with joints, so that the joints can be tested for stresses above a certain threshold, and break if that threshold is exceeded, but the joints in your sample code stretch and sag under gravity or small forces, and I want some kind of joint that holds solidly up until the time it hits breaking point.
ElectroDruid wrote:...
One question (and I think it might be one for another thread) is how you go about breaking these entities up into smaller bodies. I guess the question I'm interested in here is how do I get a number of bodies to "stick together" in such a way that they seem solid, (2) even if pieces are removed? My current thinking is that I should go with your approach of stitching lots of bodies together with joints, so that the joints can be tested for stresses above a certain threshold, and break if that threshold is exceeded, (1) but the joints in your sample code stretch and sag under gravity or small forces, and I want some kind of joint that holds solidly up until the time it hits breaking point....
(1) You could configure these joints according your needs, as mentioned 2 posts above
(2) If you need different behaviours for the joints - for example thresholds, you could use the box2d userData concept

Maybe, we will have some basic joint type with no degree of freedom - somthing like a "b2WeldedJoint" ... (I'm actuallly not sure, if this joint-type is already directly covered by box2d joints)
I don't know if it's just me, but I'm having some problems with mouse joints in this demo, particularly when the maximum allowable force on the joints is set to a low value (like 5 or 10). I go to grab something with the mouse, and it crashes in one of 3 or 4 different parts of the code (which bit it crashes in appears to be random). Looks like bodies are getting deleted without NULLing the appropriate pointers (the pointers are 0xfdfdfdfd).

I'm using Box2d 2.0.1 (at least I'm pretty sure I am) but I had to fudge the code a tiny bit to get it to compile because b2Joint::GetReactionForce and Test::MouseUp don't take any parameters in the code I have, and DrawString is not a member of DebugDraw.

Is anyone else getting this, or am I just fudging things together weirdly?
you are right, i think thats because i'm deleting all kind of stressed joints in the joint loop 🙂
i'll correct this on a next rainy sunday
meanwhile insert

Code: Select all

if ( j->GetType()!=e_distanceJoint ) continue;
at beginning of the first for loop in Step()
That seems to sort it.

One more question - I've noticed that when you set the triangulator to generate more/smaller triangles, the overall mass of the soft body seems to increase. Is this a side-effect of the code to generate the soft bodies, a bug in Box2D or a flaw in my understanding of physics?

To me, it would seem that if you've got a finite space filled with tesselating polygons, all of which have the same density, the result should have the same mass whether there are 10 triangles making up the body or 1000, but that doesn't seem to be the case.
Yes,the mass must be the same on compound bodies, if the only difference are the number of triangles, trianglemesh cares only about geometry. You may test this by copying CreateSoftBody(...), e.g. the 'b' with a different x start point and a denser mesh, they are falling down equally.

Maybe you were observing, that moving a compund body (mousejoint) using a fine mesh is slower than a mesh with only a few triangles ?
I just tried your test, and you're right, the only difference is that the mousejoint is less effective at holding up or lifting the meshes with more polys - the mass of both soft bodies seems to be the same. I still don't know why the mousejoint behaves differently (more polys = more forces acting downwards on the body?), but I don't use mousejoints in my game anyway, so it's not too much of a problem 😀

Incidentally, the more I play with this code, the more I like it. You've done great work, nimodo.
ElectroDruid wrote:I still don't know why the mousejoint behaves differently (more polys = more forces acting downwards on the body?)

Actually, it's just the way the testbed sets up the mouse joint, nothing about the joint itself - it uses the mass of the body to determine the maximum mouse joint force, mainly so that you can pick up a body of any size without worrying about using either too much or too little force. For game specific purposes (and particularly if a body is made up of several bodies) you'd want to tune this manually.
Breakablebody updated
- ElectroDruids bug
- MousDown (copy from Test.cpp) with small adaptions to consider compound bodies using the userData concept

->download from 1. post viewtopic.php?f=3&t=836

or apply attached patch file

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