从Physics Body Editor中获取一个多边形

时间:2015-10-15 21:24:54

标签: java libgdx collision-detection polygons

简短版:

如何从Physics Body Editor获得基本的com.badlogic.math.gdx.Polygon

长版:

我已经为LibGDX实现了加载器,但是加载器通过它的内部PolygonModel类使用它自己的内部RigidBody类。最终,我可以从每个单独的多边形中获取每个顶点,并从那里构建我自己的多边形。但这似乎并非“微不足道”。我想到的东西看起来像这样:

        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));
            }
        }

这是相当麻烦的我有这个完整的装载机只是剥离它建立的一切,直到索引和从这些建立我自己的多边形。然而,editor确实为我提供了一个json,但不幸的是它是一条线,因此json的好处变得浪费。如果我可以手动修复布局并破译需要解析的内容,但我想知道我是不是在这里重新发明轮子。

除此之外,可能只有一个非常简单的解决方案来获取我的多边形。

我需要什么? 好吧,我正在为我的精灵寻找一些精确的碰撞检测。矩形和圆形不起作用。多边形是完美的,它们有一个便宜检测的矩形,Intersector可以处理两个多边形的碰撞。我能以一种有效的方式考虑创建这些多边形的唯一方法是使用我链接的editor

如果有人有更好的建议,我会全力以赴。

这就是我目前的Json输出。它对我来说有冗余节点,但是只有一行没有空格和行结尾的decyphering很痛苦。

{"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 个答案:

答案 0 :(得分:0)

我想部分回答我自己的问题。由于Json的输出是不可读的,我去了jsoneditoronline.org/并粘贴在Json的单行中。通过将代码复制到树编辑器并返回到代码编辑器,我现在拥有了Json的可读版本。

它仍然有冗余节点,一个只给我原点的工具,索引/顶点将是完美的。但是我可以通过这项工作继续使用that great tool作为我的资产。