AndEngine物理编辑器加载级别

时间:2012-09-11 09:36:33

标签: android xml-parsing box2d andengine

我使用PhysicsEditor创建了一个.pes文件并导入为xml并已添加到我的项目中。当我解析它并创建实体时,它显示出奇怪的行为。我在PhysicsEditor中创建的实体的映射与我在应用程序中看到的完全不同意味着我在PhysicsEditor中绘制的形状在我的应用中呈现的方式不同。这是我的xml和代码,用于解析和添加实体到场景。

PhysicsEditor XML文件:

<?xml version="1.0" encoding="UTF-8"?>
<!-- created with http://www.physicseditor.de -->
<bodydef version="1.0">
    <bodies numBodies="1">

        <body name="car_path" dynamic="false" numFixtures="1">

            <fixture
                density="2"
                friction="1"
                restitution="0"
                filter_categoryBits="1"
                filter_groupIndex="0"
                filter_maskBits="65535"
                isSensor="false"
                type="POLYGON"
                numPolygons="20"
                >


                <polygon numVertexes="6">

                    <vertex x="277.0000" y="152.0000" />

                    <vertex x="356.0000" y="172.0000" />

                    <vertex x="413.0000" y="194.0000" />

                    <vertex x="476.0000" y="223.0000" />

                    <vertex x="173.0000" y="232.0000" />

                    <vertex x="174.0000" y="148.0000" />

                </polygon>

                <polygon numVertexes="4">

                    <vertex x="1556.0000" y="221.0000" />

                    <vertex x="1142.0000" y="94.0000" />

                    <vertex x="1255.0000" y="-15.0000" />

                    <vertex x="1554.0000" y="-14.0000" />

                </polygon>

                <polygon numVertexes="3">

                    <vertex x="-192.0000" y="177.0000" />

                    <vertex x="-888.0000" y="139.0000" />

                    <vertex x="-549.0000" y="-125.0000" />

                </polygon>

                <polygon numVertexes="6">

                    <vertex x="1762.0000" y="24.0000" />

                    <vertex x="1862.0000" y="27.0000" />

                    <vertex x="1927.0000" y="68.0000" />

                    <vertex x="2078.0000" y="222.0000" />

                    <vertex x="1643.0000" y="212.0000" />

                    <vertex x="1642.0000" y="38.0000" />

                </polygon>

                <polygon numVertexes="3">

                    <vertex x="-1150.0000" y="146.0000" />

                    <vertex x="-1776.0000" y="140.0000" />

                    <vertex x="-1476.0000" y="-25.0000" />

                </polygon>

                <polygon numVertexes="4">

                    <vertex x="-2799.0000" y="103.0000" />

                    <vertex x="-2684.0000" y="223.0000" />

                    <vertex x="-3112.0000" y="256.0000" />

                    <vertex x="-3108.0000" y="98.0000" />

                </polygon>

                <polygon numVertexes="3">

                    <vertex x="3112.0000" y="255.0000" />

                    <vertex x="2422.0000" y="222.0000" />

                    <vertex x="3120.0000" y="-71.0000" />

                </polygon>

                <polygon numVertexes="4">

                    <vertex x="1142.0000" y="94.0000" />

                    <vertex x="1556.0000" y="221.0000" />

                    <vertex x="709.0000" y="226.0000" />

                    <vertex x="911.0000" y="93.0000" />

                </polygon>

                <polygon numVertexes="6">

                    <vertex x="-2111.0000" y="89.0000" />

                    <vertex x="-2067.0000" y="94.0000" />

                    <vertex x="-2002.0000" y="139.0000" />

                    <vertex x="-2344.0000" y="223.0000" />

                    <vertex x="-2196.0000" y="112.0000" />

                    <vertex x="-2153.0000" y="91.0000" />

                </polygon>

                <polygon numVertexes="4">

                    <vertex x="105.0000" y="233.0000" />

                    <vertex x="-94.0000" y="178.0000" />

                    <vertex x="69.0000" y="106.0000" />

                    <vertex x="91.0000" y="104.0000" />

                </polygon>

                <polygon numVertexes="3">

                    <vertex x="-2002.0000" y="139.0000" />

                    <vertex x="-2067.0000" y="94.0000" />

                    <vertex x="-2032.0000" y="110.0000" />

                </polygon>

                <polygon numVertexes="4">

                    <vertex x="-1150.0000" y="146.0000" />

                    <vertex x="105.0000" y="233.0000" />

                    <vertex x="-2344.0000" y="223.0000" />

                    <vertex x="-2002.0000" y="139.0000" />

                </polygon>

                <polygon numVertexes="3">

                    <vertex x="413.0000" y="194.0000" />

                    <vertex x="356.0000" y="172.0000" />

                    <vertex x="376.0000" y="176.0000" />

                </polygon>

                <polygon numVertexes="3">

                    <vertex x="105.0000" y="233.0000" />

                    <vertex x="-192.0000" y="177.0000" />

                    <vertex x="-94.0000" y="178.0000" />

                </polygon>

                <polygon numVertexes="4">

                    <vertex x="105.0000" y="233.0000" />

                    <vertex x="-1150.0000" y="146.0000" />

                    <vertex x="-888.0000" y="139.0000" />

                    <vertex x="-192.0000" y="177.0000" />

                </polygon>

                <polygon numVertexes="3">

                    <vertex x="3112.0000" y="255.0000" />

                    <vertex x="-3112.0000" y="256.0000" />

                    <vertex x="-2684.0000" y="223.0000" />

                </polygon>

                <polygon numVertexes="3">

                    <vertex x="3112.0000" y="255.0000" />

                    <vertex x="1556.0000" y="221.0000" />

                    <vertex x="1643.0000" y="212.0000" />

                </polygon>

                <polygon numVertexes="3">

                    <vertex x="709.0000" y="226.0000" />

                    <vertex x="173.0000" y="232.0000" />

                    <vertex x="476.0000" y="223.0000" />

                </polygon>

                <polygon numVertexes="3">

                    <vertex x="3112.0000" y="255.0000" />

                    <vertex x="2078.0000" y="222.0000" />

                    <vertex x="2422.0000" y="222.0000" />

                </polygon>

                <polygon numVertexes="3">

                    <vertex x="3112.0000" y="255.0000" />

                    <vertex x="105.0000" y="233.0000" />

                    <vertex x="173.0000" y="232.0000" />

                </polygon>


            </fixture>

        </body>

    </bodies>
    <metadata>
        <format>1</format>
        <ptm_ratio></ptm_ratio>
    </metadata>
</bodydef>

这是我的代码:

private void loadLevel() {
    // TODO Auto-generated method stub
    AssetManager assetManager = getAssets();
    try {
        InputStream stream = assetManager.open("tmx/path1.xml");
        if(stream != null) {
            try {
                DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

                dbf.setValidating(false);
                dbf.setIgnoringComments(false);
                dbf.setIgnoringElementContentWhitespace(true);
                dbf.setNamespaceAware(true);

                DocumentBuilder db = null;
                db = dbf.newDocumentBuilder();

                Document document = db.parse(stream);
                Element root = document.getDocumentElement();

                NodeList bodiesNodeList = root.getElementsByTagName("bodies");
                for(int i = 0; i < bodiesNodeList.getLength(); i++) {
                    BodyDef bodyDef = new BodyDef();
                    bodyDef.type = BodyType.StaticBody;
                    bodyDef.fixedRotation = true;
                    Element bodiesElement = (Element)bodiesNodeList.item(i);
                    NodeList bodyList = bodiesElement.getElementsByTagName("body");
                    for(int j = 0; j < bodyList.getLength(); j++) {
                        Element bodyElement = (Element)bodyList.item(j);
                        Body body = mPhysicsWorld.createBody(bodyDef);
                        NodeList fixtureList = bodyElement.getElementsByTagName("fixture");
                        for(int k = 0; k < fixtureList.getLength(); k++) {
                            Element fixtureElement = (Element)fixtureList.item(k);
                            FixtureDef fixtureDef = new FixtureDef();
                            if(fixtureElement != null) {
                                String density = fixtureElement.getAttribute("density");
                                String friction = fixtureElement.getAttribute("friction");
                                String restitution = fixtureElement.getAttribute("restitution");
                                fixtureDef = PhysicsFactory.createFixtureDef(Float.parseFloat(density), Float.parseFloat(friction), Float.parseFloat(restitution));

                            }
                            NodeList polygonList = fixtureElement.getElementsByTagName("polygon");
                            if(polygonList != null && polygonList.getLength() > 0) {
                                for(int m = 0; m < polygonList.getLength(); m++) {
                                    PolygonShape polyShape = new PolygonShape();
                                    Element polygonElement = (Element)polygonList.item(m);
                                    NodeList vertexList = polygonElement.getElementsByTagName("vertex");
                                    if(vertexList != null && vertexList.getLength() > 0) {
                                        Vector2 [] vectors = new Vector2[vertexList.getLength()];
                                        for(int n = 0; n < vertexList.getLength(); n++) {
                                            Element vertexElement = (Element)vertexList.item(n);
                                            if(vertexElement != null) {
                                                float x = Float.parseFloat(vertexElement.getAttribute("x"));
                                                float y = Float.parseFloat(vertexElement.getAttribute("y"));
                                                vectors[n] = new Vector2(x/PIXEL_TO_METER_RATIO_DEFAULT, y/PIXEL_TO_METER_RATIO_DEFAULT);
                                            }
                                        }
                                        polyShape.set(vectors);
                                        fixtureDef.shape = polyShape;
                                    }
                                    body.createFixture(fixtureDef);
                                }

                            }

                        }
                        mScene.attachChild(bgSprite);
                        mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(bgSprite, body, false, false));


                    }

                }
            }
            catch(Exception e) {
                e.printStackTrace();
            }
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

知道我哪里错了吗?

1 个答案:

答案 0 :(得分:2)

我认为你所做的错误就是重新发明轮子。我建议你使用这个装载机: https://github.com/ANDLABS-Git/AndEngine-PhysicsEditor-Extension