0
我已經使用PhysicsEditor創建了一個.pes文件並導入爲xml並添加到了我的項目中。當我解析它並創建實體時,它顯示出奇怪的行爲。我在PhysicsEditor中創建的物體映射與我在應用程序中看到的完全不同意味着我在PhysicsEditor中繪製的形狀在我的應用程序中渲染的方式不同。這裏是我的XML和代碼來解析和添加到場景的身體。AndEngine物理編輯器加載級別
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();
}
}
任何想法,我錯了?
啊......啊!你救了我的命。謝謝... –
不客氣。 – JohnEye