James
James

Reputation: 14091

How to create a dynamic body in Box2D?

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 = 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.

Upvotes: 2

Views: 1686

Answers (2)

Macarse
Macarse

Reputation: 93143

If you are playing with Box2d in Android I would recommend you to check the cocos2d ports.

Upvotes: 0

TheCottonSilk
TheCottonSilk

Reputation: 8812

You can try this URL tutorial first!

Have a look at this code as well!

Hope these would be useful..

Upvotes: 1

Related Questions