[英]I have created a triangular body with physicsbox2d andengine Extention..how to add face or sprite to the body
I have created a triangular body with physicsbox2d andengine Extention. 我用physicsbox2d和engine Extension创建了一个三角形物体。 it looks gud ..now i am not able to add face or sprite to this triangular body ...here is code ..thankx in advance.`
它看起来有点刺眼。现在我无法在这个三角形的物体上添加面孔或精灵……这是预先的代码.thankx。
List<Vector2> UniqueBodyVertices = new ArrayList<Vector2>();
UniqueBodyVertices.addAll((List<Vector2>) ListUtils.toList(
new Vector2[] {
new Vector2(-93f,-75f),
new Vector2(-107f,-14f),
new Vector2(-101f,41f),
new Vector2(-71f,74f),
new Vector2(69f,74f),
new Vector2(98f,41f),
new Vector2(104f,-14f),
new Vector2(51f,-75f),
new Vector2(79f,9f),
new Vector2(43f,34f),
new Vector2(-46f,34f),
new Vector2(-80f,9f)
}));
List<Vector2> UniqueBodyVerticesTriangulated = new EarClippingTriangulator().computeTriangles(UniqueBodyVertices);
float[] MeshTriangles =
new float[UniqueBodyVerticesTriangulated.size() * 3];
for(int i = 0; i < UniqueBodyVerticesTriangulated.size(); i++) {
MeshTriangles[i*3] = UniqueBodyVerticesTriangulated.get(i).x;
MeshTriangles[i*3+1] = UniqueBodyVerticesTriangulated.get(i).y;
UniqueBodyVerticesTriangulated.get(i).
mul(1/PhysicsConstants.PIXEL_TO_METER_RATIO_DEFAULT);
}
Mesh UniqueBodyMesh = new Mesh(400f, 260f, MeshTriangles,
UniqueBodyVerticesTriangulated.size(), DrawMode.TRIANGLES,
this.getVertexBufferObjectManager());
UniqueBodyMesh.setColor(1f, 0f, 0f);
scene.attachChild(UniqueBodyMesh);
FixtureDef uniqueBodyFixtureDef =
PhysicsFactory.createFixtureDef(20f, 0.5f, 0.5f);
Body uniqueBody = PhysicsFactory.createTrianglulatedBody(
mphysicworld, UniqueBodyMesh ,UniqueBodyVerticesTriangulated,
BodyType.DynamicBody, uniqueBodyFixtureDef);
` `
You need to bind the body to the desired sprite by using a physics connector, and then registering that physics connector to the physics world object. 您需要通过使用物理连接器将物体绑定到所需的精灵,然后将该物理连接器注册到物理世界对象。 For example:
例如:
physicsWorld.registerPhysicsConnector(new PhysicsConnector(sprite, body, true, true));
Have a look at this tutorial for more info (especially under "Creating Moving Body" part). 请查看本教程以获取更多信息(尤其是在“创建移动的身体”部分下)。
Do you want to connect your uniquebody with UniqueBodyMesh (which is entity like sprite). 您是否想将Uniquebody与UniqueBodyMesh(类似于Sprite的实体)连接。 Judging from your code you should be able to see your sprite covering your body.
从您的代码来看,您应该能够看到精灵覆盖您的身体。 But it is not connected so after contact it will not move with your body.
但是未连接,因此接触后不会随身体移动。 You need additionally:
您还需要:
mphysicsWorld.registerPhysicsConnector(new PhysicsConnector(UniqueBodyMesh, uniqueBody, true, true));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.