jbox2d tutorial [closed]
Asked Answered
S

3

7

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

Shelia answered 16/4, 2011 at 11:33 Comment(0)
P
6

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);
    }
Portia answered 19/1, 2012 at 16:0 Comment(2)
Except this code is broken, world no longer supports "doSleep" in jBox2D.Aerograph
@iLoveUnicorns I have rectified that. (Waiting for peer-review).Fluent
F
5

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

Fitting answered 3/12, 2011 at 19:12 Comment(0)
W
4

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!

Watchmaker answered 16/4, 2011 at 11:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.