Handling destructable bodies

Topics: User Forum
Mar 2, 2013 at 7:25 PM
Edited Mar 2, 2013 at 9:34 PM
Hi, i'm pretty new to Farseer Physics and i'm trying to use it to build a worms-like game.
I downloaded the TestBed project and robert's patch for destructable body and tried to use it with the Texture2polygon method found in samples files.
The ground (terrain) vertices are calculated this way:
            uint[] data = new uint[_polygonTexture.Width * _polygonTexture.Height];

            textureVertices = PolygonTools.CreatePolygon(data, _polygonTexture.Width, false);
            Vector2 centroid = -textureVertices.GetCentroid();
            textureVertices.Translate(ref centroid);

            // draw the texture the correct place.
            _origin = -centroid;

            //We simplify the vertices found in the texture.
            textureVertices = SimplifyTools.ReduceByDistance(textureVertices, 6f);

            //We create a new destructable body for the whole polygon
            // no need to decompose the vertices, it'll be done with the creation of the
            // destructable body
            DestructableBody db = new DestructableBody(world, textureVertices);
            _ground = db.Body;

used this way, the destructable body scales at a very strange size so, in the DestructableBody.CreateFixture i've forced vertices to be scaled the size I want by using verties.Scale(ref _myscale). This cause the Scale to be applied everytime a shape is subtracted to the destructable body.

The problem is, now the subtracted shape is not calculated the correct place : the algorithm is subtracting shape at the place the _ground would be if I weren't scaling it. Otherwise, the algorithm seems to be working pretty well, unless there is no holes gestion (which i don't need)

I think the problem comes from the fact we scale the polygon everytime but if i got the texture scaled only once, before converting it to destructable body, it loss its scales as soon as I subtract a new shape...

This problem is giving me headache at the moment, please help, any ideas or sugestions are welcomed, I can post more source/screenshots if needed.
Thanks !


EDIT: Moreover, when the body is splited up, i can no longer subtract a shape from the new bodies from the bullet OnCollision eventHandler : I got a NullReferenceException in the Update method of the Contact class.
Mar 4, 2013 at 7:45 PM
This is just a guess, but you probably don't want to scale in your physics simulation. A common mistake is to expect your physics simulation scale and your screen scale to be the same (i.e. if I want a 100x100 pixel box on the screen I create a 100x100 shape in my physics simulation). Farseer units represent meters in the real world (you're probably not trying to model a 100 meter by 100 meter box -- that's the size of a giant building). It is up to you to scale from meters to pixels on your screen (though there is the handy ConvertUnits class).

So when you say "the destructable body scales at a very strange size", what does that mean? Are you thinking size in pixels or in meters? Again, you want to keep your simulation scale separate from your screen-scale; only do the scaling when you draw, not in your simulation.

Hope that helps,
Mar 5, 2013 at 2:35 AM
Yes, in fat i want the physics simulation to be scaled so that i can apply the Texture the correct scale on it.
But I've gave up the use of this old technique and now i use MSTerrain and that's fine.

Thank anyway !