AndEngine Physics Editor loading level
Posted
by
Khawar Raza
on Stack Overflow
See other posts from Stack Overflow
or by Khawar Raza
Published on 2012-09-11T09:36:32Z
Indexed on
2012/09/11
9:37 UTC
Read the original article
Hit count: 532
I have created a .pes file using PhysicsEditor and imported as xml and have added to my project. When I parsed it and created bodies, it is showing strange behavior. The mapping of bodies that I created in PhysicsEditor is totally different what I see in my application means the shapes I draw in PhysicsEditor are rendering differently in my app. Here is my xml and code to parse and add bodies to scene.
PhysicsEditor XML file:
<?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>
And here is my code:
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();
}
}
Any idea where I am going wrong?
© Stack Overflow or respective owner