简体   繁体   English

我用physicsbox2d和engine Extension创建了一个三角形物体。如何向该物体添加面或精灵

[英]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.

相关问题 带有andengine physicsbox2d扩展的Android Studio - Android studio with andengine physicsbox2d extension LibGDX PhysicsBox2D,如果我设置线速度,则物理无法正常工作 - LibGDX PhysicsBox2D, if i set linear velocity, physics does not work correctly 如何使用andEngine移动精灵的身体? - how to move body of sprite using andEngine? AndEngine Sprite / Box2D物体移除会移除特定的物体(应如此),但会移除该精灵的所有实例吗? - AndEngine Sprite/Box2D Body removal removes a particluar body (as it should), but removes all instances of the sprite? andengine sprite移动PathModifier和body - andengine sprite move PathModifier and body 如果触摸是AndEngine中的精灵或身体,如何检查触摸事件? - How to check touch event if touch is sprite or body in AndEngine? 在精灵(Android AndEngine)对其执行操作后,如何获取精灵的坐标? - How do I get the coordinates of a sprite after it has been acted on by a body (Android AndEngine)? AndEngine-删除特定的Sprite及其主体 - AndEngine - removing a particular Sprite & Its body 如果我知道尸体,如何将精灵附着到box2d尸体上? - how to get the sprite attached to a box2d body if i knew the body? AndEngine Sprite / Box2D Body删除崩溃我的程序没有错误/异常信息? - AndEngine Sprite/Box2D Body removal crashes my program with no error/exception information?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM