java – jbox2d教程

前端之家收集整理的这篇文章主要介绍了java – jbox2d教程前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
你能告诉我吗:
我在哪里可以找到“在jBox2d中编程游戏”的教程?

解决方法

我已将C手册中的Hello World示例移植到jBox2d.这只是一个逐行端口.显然你需要编写一个基本的 java程序并调用这段代码.你还需要导入一些库,我在StackOverflow中的导入格式有问题所以我将它们排除在外.希望您的IDE将为您处理导入.
// 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);
    }
原文链接:https://www.f2er.com/java/127736.html

猜你在找的Java相关文章