Creating a very specific kind of a breakable body

Topics: Developer Forum, User Forum
Jun 23, 2012 at 5:32 PM

Hello.

I just recently had to dive into the world of vertices and polygons due to my problem. I need to create a very specific kind of a breakable body circle. A picture is worth more than a thousand words so:

https://dl.dropbox.com/u/16341234/demo.png

This is the kind of a breakable body I need to be able to construct somehow.  

My question is:

Is there already functionality to achieve something like this or do I have to build the vertices myself?  

Jun 23, 2012 at 9:29 PM
Edited Jun 23, 2012 at 9:30 PM



Alright, I consulted a mathematician friend of mine and we came up with a solution.

http://i.imgur.com/Nz6Xp.png 

 

That is what the result looks like, and here is the function:

public Body CreateFragmentedCircle(Vector2 center, float radius, int theta_divisions, int radial_divisions)
        {

            Body _body = new Body(_world);
            valmisBody.Position = new Vector2(-37, 0);

            List<Vertices> lista = new List<Vertices>();
            Vector2 a, b, c, d = new Vector2(0, 0);

            for (int i = 0; i < theta_divisions; ++i)
            {
                Vertices v = new Vertices();
                a = center;
                b = new Vector2((radius / radial_divisions) * (float)Math.Cos(i * 2 * (float)Math.PI / theta_divisions), (radius / radial_divisions) * (float)Math.Sin(i * 2 * (float)Math.PI / theta_divisions)) + center;
                c = new Vector2((radius / radial_divisions) * (float)Math.Cos((i + 1) * 2 * (float)Math.PI / theta_divisions), (radius / radial_divisions) * (float)Math.Sin((i + 1) * 2 * (float)Math.PI / theta_divisions)) + center;

                v.Add(a);
                v.Add(b);
                v.Add(c);
                PolygonShape polygonShape = new PolygonShape(v, .25f);
                Fixture fixture = _body.CreateFixture(polygonShape, null);
            }


            for (int i = 1; i < radial_divisions; ++i)
            {
                float modifier = theta_divisions + 10 * i;
                for (int j = 0; j < modifier; ++j)
                {
                    Vertices v = new Vertices();
                    a = new Vector2((i * radius / radial_divisions) * (float)Math.Cos(j * 2 * (float)Math.PI / (modifier)), (i * radius / radial_divisions) * (float)Math.Sin(j * 2 * (float)Math.PI / (modifier))) + center;
                    b = new Vector2(((i + 1) * radius / radial_divisions) * (float)Math.Cos(j * 2 * (float)Math.PI / (modifier)), ((i + 1) * radius / radial_divisions) * (float)Math.Sin(j * 2 * (float)Math.PI / (modifier))) + center;
                    c = new Vector2(((i + 1) * radius / radial_divisions) * (float)Math.Cos((j + 1) * 2 * (float)Math.PI / (modifier)), ((i + 1) * radius / radial_divisions) * (float)Math.Sin((j + 1) * 2 * (float)Math.PI / (modifier))) + center;
                    d = new Vector2((i * radius / radial_divisions) * (float)Math.Cos((j + 1) * 2 * (float)Math.PI / (modifier)), (i * radius / radial_divisions) * (float)Math.Sin((j + 1) * 2 * (float)Math.PI / (modifier))) + center;
                    v.Add(a);
                    v.Add(b);
                    v.Add(c);
                    v.Add(d);
                    PolygonShape polygonShape = new PolygonShape(v, .25f);
                    Fixture fixture = _body.CreateFixture(polygonShape, null);
                }
            }


            return _body;
        }

 

 

The parameters are quite simple: 
- Vector2 center = the center point where to build the fragmented circle around
- float radius = the radius of the circle
- int theta_divisions = the amount of edges for the circle (I strongly suggest to use atleast 8)

- int radial_divisions = the amount of "layers" of fragmented pieces (anything above 2 works, tho 2 itself looks very stupid if the theta_divisions is small)

 

Feel free to use and experiment with the code as you desire, I hope it's help for anyone who might've been struggling with the same thing.


*** EDIT ***:

The code isn't usable straight out of the pack apparentally, you might want to include a parameter for the world in to the parameter list as that code uses a public World _world variable