我用物理 box2d 和引擎扩展创建了一个三角形 body..如何将面或精灵添加到 body
I have created a triangular body with physicsbox2d andengine Extention..how to add face or sprite to the body
我用 physicsbox2d 和引擎扩展创建了一个三角形 body。看起来很不错..现在我无法为这个三角形添加面或精灵 body ...这是代码..提前感谢。`
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);
`
您需要使用物理连接器将 body 绑定到所需的精灵,然后将该物理连接器注册到物理世界 object。例如:
physicsWorld.registerPhysicsConnector(new PhysicsConnector(sprite, body, true, true));
查看 this tutorial 了解更多信息(尤其是 "Creating Moving Body" 部分)。
你想将你的 uniquebody 与 UniqueBodyMesh(类似于 sprite 的实体)连接起来。从您的代码来看,您应该能够看到您的 sprite 覆盖了您的 body。但是它没有连接所以接触后它不会随着你的body移动。您还需要:
mphysicsWorld.registerPhysicsConnector(new PhysicsConnector(UniqueBodyMesh, uniqueBody, true, true));
我用 physicsbox2d 和引擎扩展创建了一个三角形 body。看起来很不错..现在我无法为这个三角形添加面或精灵 body ...这是代码..提前感谢。`
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);
`
您需要使用物理连接器将 body 绑定到所需的精灵,然后将该物理连接器注册到物理世界 object。例如:
physicsWorld.registerPhysicsConnector(new PhysicsConnector(sprite, body, true, true));
查看 this tutorial 了解更多信息(尤其是 "Creating Moving Body" 部分)。
你想将你的 uniquebody 与 UniqueBodyMesh(类似于 sprite 的实体)连接起来。从您的代码来看,您应该能够看到您的 sprite 覆盖了您的 body。但是它没有连接所以接触后它不会随着你的body移动。您还需要:
mphysicsWorld.registerPhysicsConnector(new PhysicsConnector(UniqueBodyMesh, uniqueBody, true, true));