简体   繁体   English

如何在Box2D中创建动态实体?

[英]How to create a dynamic body in Box2D?

I am totally new to Box2D in Android. 我对Android中的Box2D完全陌生。 I am trying to create a body in ANDROID, but it never seems to work. 我正在尝试在ANDROID中创建一个主体,但是它似乎从未起作用。 I imported the library to the lib folder. 我将库导入了lib文件夹。 And I tried somthing like this. 我试过这样的东西。

            BodyDef bodyDef  = new BodyDef();
        Vec2 initVel = new Vec2();
        PolygonShape shape = new PolygonShape();
        FixtureDef fd = new FixtureDef();



                bodyDef.position.set(3.303636f, 7.712577f);
            bodyDef.userData = block1;
            bodyDef.angle = 0.000000f;
            Body rectangle = world.createBody(bodyDef);
            initVel.set(0.000000f, 0.000000f);
            rectangle.setLinearVelocity(initVel);
            rectangle.setAngularVelocity(0.000000f);

            Vec2[] rectangle_vertices = new Vec2[4];
            rectangle_vertices[0].set(-0.921088f, -0.614059f);
            rectangle_vertices[1].set(0.921088f, -0.614059f);
            rectangle_vertices[2].set(0.921088f, 0.614059f);
            rectangle_vertices[3].set(-0.921088f, 0.614059f);

            shape.set(rectangle_vertices, 4);
            fd.shape = shape;
            fd.density = 0.015000f;
            fd.friction = 0.300000f;
            fd.restitution = 0.600000f;
            rectangle.createFixture(fd);  

can anyone help me on this. 谁可以帮我这个事。 Or PLease provide me with a sample code or how to start with Box2D. 或请给我提供示例代码或如何从Box2D开始。 My main problem was some of the things like "FixtureDef" was not imported. 我的主要问题是未导入“ FixtureDef”之类的东西。 Actually I tried to port the code which I used in Iphone Box2D to android. 实际上,我尝试将Iphone Box2D中使用的代码移植到android。

Please anyone help. 请任何人帮助。 Thanks for reading. 谢谢阅读。

You can try this URL tutorial first! 您可以先尝试此URL教程

Have a look at this code as well! 也看一下这段代码

Hope these would be useful.. 希望这些会有用。

If you are playing with Box2d in Android I would recommend you to check the cocos2d ports. 如果您在Android中使用Box2d ,则建议您检查cocos2d端口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM