开发者

How to create a dynamic body in Box2D?

开发者 https://www.devze.com 2023-02-06 01:00 出处:网络
I am totally new to Box2D in Android. I am trying to create a body in ANDROID, but it never seems to work.

I am totally new to Box2D in Android. I am trying to create a body in ANDROID, but it never seems to work. I imported the library to the lib folder. 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开发者_StackOverflow社区 = 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. My main problem was some of the things like "FixtureDef" was not imported. Actually I tried to port the code which I used in Iphone Box2D to android.

Please anyone help. Thanks for reading.


You can try this URL tutorial first!

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.

  • Java version
  • C++ version
0

精彩评论

暂无评论...
验证码 换一张
取 消