0
votes

Short version:

How do I get a basic com.badlogic.math.gdx.Polygon from the Physics Body Editor?

Long version:

I have implemented the loader for LibGDX but the loader uses it's own internal PolygonModel class via it's internal RigidBody class. Eventually I can get each vertex from each individual polygon and build my own polygons from there. But this seems not very "trivial". Something I have in mind looks like this:

        List<Polygon> myPolygons = new ArrayList<Polygon>();    
        Map<String, BodyEditorLoader.RigidBodyModel> bodyMap = loader.getInternalModel().rigidBodies;    
        for (BodyEditorLoader.RigidBodyModel m : bodyMap.values())
        {
            for (BodyEditorLoader.PolygonModel pm : m.polygons)
            {
                float[] vertArray = new float[pm.vertices.size() * 2];
                int count = 0;
                for (Vector2 v2 : pm.vertices)
                {
                    vertArray[count] = v2.x;
                    vertArray[count + 1] = v2.y;
                    count += 2;
                }
                myPolygons.add(new Polygon(vertArray));
            }
        }

This is rather cumbersome I have this complete loader just to peel everything it builds off till the indices and build my own polygons from these. However the editor does supply me with a json but unfortunately it's a single line and therefor the benefit of json goes to waste. If I could manually fix the layout and decipher what needs to be parsed but I'm wondering if I'm not reinventing the wheel here.

Other then that there might just be a very simple solution to get my polygon.

What do I need it for? Well I'm looking for some precise collision detection for my sprites. Rectangles and circles will not do the job. Polygons are perfect for this, they have a rectangle for cheap detection and Intersector can handle collision of two polygons. The only way I can think off creating these polygons in a somewhat efficient way is with the editor I linked to.

If anyone has better suggestions I'm all ears.

This is what my current Json output looks like. It has redundant nodes for me but decyphering is a pain with just a single line without whitespace and line endings.

{"rigidBodies":[{"name":"terrain","imagePath":"groundGrass.png","origin":{"x":6.250441074371338E-4,"y":-6.705522537231445E-8},"polygons":[[{"x":0.375,"y":0.05569307133555412},{"x":0.3087501525878906,"y":0.0837499126791954},{"x":0.19375014305114746,"y":0.07812491804361343},{"x":0.16212871670722961,"y":0.0445544607937336}],[{"x":0.16212871670722961,"y":0.0445544607937336},{"x":0.5792078971862793,"y":0.034653469920158386},{"x":0.426980197429657,"y":0.05445544794201851},{"x":0.375,"y":0.05569307133555412}],[{"x":0.5792078971862793,"y":0.034653469920158386},{"x":0.5406249761581421,"y":0.07374989986419678},{"x":0.46039605140686035,"y":0.0717821791768074},{"x":0.426980197429657,"y":0.05445544794201851}],[{"x":0.05198019742965698,"y":0.035891093313694},{"x":0.0445544570684433,"y":0.04207921028137207},{"x":0.007425742689520121,"y":0.043316833674907684},{"x":0,"y":0.037128716707229614}],[{"x":0,"y":0.037128716707229614},{"x":8.940696716308594E-8,"y":6.249025464057922E-4},{"x":0.10891088843345642,"y":0.03217822313308716},{"x":0.05198019742965698,"y":0.035891093313694}],[{"x":8.940696716308594E-8,"y":6.249025464057922E-4},{"x":0.6596534848213196,"y":0.01980198174715042},{"x":0.6274752616882324,"y":0.03341584652662277},{"x":0.5792078971862793,"y":0.034653469920158386},{"x":0.16212871670722961,"y":0.0445544607937336},{"x":0.10891088843345642,"y":0.03217822313308716}],[{"x":8.940696716308594E-8,"y":6.249025464057922E-4},{"x":0.9987500905990601,"y":-6.250739097595215E-4},{"x":0.7093750238418579,"y":0.01874990016222},{"x":0.6596534848213196,"y":0.01980198174715042}],[{"x":0.9987500905990601,"y":-6.250739097595215E-4},{"x":0.9987500905990601,"y":0.04374990612268448},{"x":0.9418317079544067,"y":0.05816832184791565},{"x":0.7846534848213196,"y":0.05816832184791565},{"x":0.7400990128517151,"y":0.053217824548482895},{"x":0.7093750238418579,"y":0.01874990016222}],[{"x":0.9418317079544067,"y":0.05816832184791565},{"x":0.9212501049041748,"y":0.08749989420175552},{"x":0.8131188154220581,"y":0.08292079716920853},{"x":0.7846534848213196,"y":0.05816832184791565}]],"circles":[],"shapes":[{"type":"POLYGON","vertices":[{"x":0.9212501049041748,"y":0.08749989420175552},{"x":0.9418317079544067,"y":0.05816832184791565},{"x":0.9987500905990601,"y":0.04374990612268448},{"x":0.9987500905990601,"y":-6.250739097595215E-4},{"x":8.940696716308594E-8,"y":6.249025464057922E-4},{"x":0,"y":0.037128716707229614},{"x":0.007425742689520121,"y":0.043316833674907684},{"x":0.0445544570684433,"y":0.04207921028137207},{"x":0.05198019742965698,"y":0.035891093313694},{"x":0.10891088843345642,"y":0.03217822313308716},{"x":0.16212871670722961,"y":0.0445544607937336},{"x":0.19375014305114746,"y":0.07812491804361343},{"x":0.3087501525878906,"y":0.0837499126791954},{"x":0.375,"y":0.05569307133555412},{"x":0.426980197429657,"y":0.05445544794201851},{"x":0.46039605140686035,"y":0.0717821791768074},{"x":0.5406249761581421,"y":0.07374989986419678},{"x":0.5792078971862793,"y":0.034653469920158386},{"x":0.6274752616882324,"y":0.03341584652662277},{"x":0.6596534848213196,"y":0.01980198174715042},{"x":0.7093750238418579,"y":0.01874990016222},{"x":0.7400990128517151,"y":0.053217824548482895},{"x":0.7846534848213196,"y":0.05816832184791565},{"x":0.8131188154220581,"y":0.08292079716920853}]}]}],"dynamicObjects":[]}
1
stackoverflow.com/questions/32808014/… Check out my answer to that, I post the body editor loader I made. JSON is generally represented in a simple line, it's deciphered pragmatically or outputted with some JSON pretty printer. - Johnathon Havens
@JohnathonHavens I will look into that. But the JSON output of the editor I got consists of a huge single line with no whitespace or line endings. Json should be readable afaik. And in it's current state it is not. - Madmenyo
No JSON can, and is frequently, outputted in one line. JSON parser's done care. Whitespace has no effect except in values in the key value pairs. jsonprettyprint.com can be used for human readability. - Johnathon Havens
@JohnathonHavens Yeah, I added my own answer where I converted the single line to something readable. I'ts all working now. I can use the Physics Body Editor, generate raw gdx.math.Polygon's from it and do collision detection with those. I will be writing a tutorial on this when I have time. I have come across many topics that asked for this and the only answer to it was to actually use Box2D, which does not work properly without velocities, mass, etc and is pretty much overkill for just 2D collision without physics. - Madmenyo
Cool, did you use any of my code? I'm just curious if there's any issues with it as we are all learning here. - Johnathon Havens

1 Answers

0
votes

I want to partially answer my own question. Since the output of the Json was unreadable I went to jsoneditoronline.org/ and pasted in the single line of Json. By copying the code to the tree editor and back to the code editor I now have a readable version of the Json.

It still has redundant nodes in it, a tool that just gives me origin and the indices/vertices would be perfect. But I can continue using that great tool for my assets with this work around.