Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jbox2d tutorial [closed]

Tags:

java

box2d

jbox2d

can you tell me: where can I find tutorials "programming games in jbox2d"?

like image 841
Piotrek K Avatar asked Apr 16 '11 11:04

Piotrek K


3 Answers

I have developed one simple application using jBox2D and javaFX 2. You can find tutorial and source code for this application here.

Also you can watch application sample demo video here

like image 166
dilip Avatar answered Oct 16 '22 06:10

dilip


I have ported the Hello World sample from the C++ manual to jbox2d. This is just a line by line port. Obviously you need to write a basic java program and call this code. You will also need to import a number of libraries, I had trouble with the formatting of my imports in StackOverflow so I am excluding them. Hopefully your IDE will take care of the imports for you.

    // Static Body
    Vec2  gravity = new Vec2(0,-10);
    World world = new World(gravity);
    BodyDef groundBodyDef = new BodyDef();
    groundBodyDef.position.set(0, -10);
    Body groundBody = world.createBody(groundBodyDef);
    PolygonShape groundBox = new PolygonShape();
    groundBox.setAsBox(50, 10);
    groundBody.createFixture(groundBox, 0);

    // Dynamic Body
    BodyDef bodyDef = new BodyDef();
    bodyDef.type = BodyType.DYNAMIC;
    bodyDef.position.set(0, 4);
    Body body = world.createBody(bodyDef);
    PolygonShape dynamicBox = new PolygonShape();
    dynamicBox.setAsBox(1, 1);
    FixtureDef fixtureDef = new FixtureDef();
    fixtureDef.shape = dynamicBox;
    fixtureDef.density = 1;
    fixtureDef.friction = 0.3f;
    body.createFixture(fixtureDef);

    // Setup world
    float timeStep = 1.0f/60.0f;
    int velocityIterations = 6;
    int positionIterations = 2;

    // Run loop
    for (int i = 0; i < 60; ++i) {
        world.step(timeStep, velocityIterations, positionIterations);
        Vec2 position = body.getPosition();
        float angle = body.getAngle();
        System.out.printf("%4.2f %4.2f %4.2f\n", position.x, position.y, angle);
    }
like image 44
Matthew Zip Avatar answered Oct 16 '22 07:10

Matthew Zip


This is not exactly a direct programming-related question, and thus will probably be closed soon.

Regardless, one does not program games in JBox2D, one programs games with JBox2D. If you're looking for help in using the jbox2d library, a quick Google search turned up one tutorial (for android, but I would expect the general use of the library to be quite general) that looks like it may be helpful - alternatively, try the JBox2D user manual.

On the other hand, if what you really want to know is how to program games in general... well, that's a far larger topic and one that is best solved with a Google search for game tutorials. Just remember that game-making is a general topic, not platform specific, so don't shy away from tutorials not written for Java - try and adapt their example code instead, and you may learn even more!

like image 25
Stephen Avatar answered Oct 16 '22 06:10

Stephen