JBox2D + Slick2D - 不会发生碰撞

时间:2015-02-11 17:22:50

标签: java slick2d jbox2d


更新

我有一个提示。这是因为JBox2D中的屏幕中心是(0, 0)。这很奇怪,因为xy不是-11,,而是取决于屏幕尺寸。
这意味着我从互联网上复制的Util类(每个人共享此代码)都不是最新的。我仍然需要帮助,因为我无法找到任何'算法'具有宽高比+屏幕尺寸依赖性。


基本问题

因为我已将" slick2D添加到jbox2D位置"反之亦然(它在屏幕中的位置=屏幕中的位置/ JBox的100),我没有得到好结果。屏幕截图如下

我用代码解释:

public class Util {
    private static final float SCALE = 0.01f;   // 1/100 pixels
    public static float toPosX(float posX) { return (posX * SCALE); }
    public static float toPosY(float posY) { return (-posY * SCALE); }
    public static float toScreenX(float posX) { return (posX / SCALE); }
    public static float toScreenY(float posY) { return (-posY / SCALE); }
}

Square.java:

public class Square {
    private Body body;
    private Rectangle rectangle;

    Square(World world, BodyType type, Vec2 pos, Vec2 size) {
        float x = Util.toPosX(pos.x);
        float y = Util.toPosY(pos.y);
        float sx = Util.toPosX(size.x);
        float sy = Util.toPosY(size.y);

        //body definition
        BodyDef bd = new BodyDef();
        bd.position.set(x, y);
        bd.type = type;

        //define shape of the body.
        PolygonShape cs = new PolygonShape();
        cs.setAsBox(sx, sy);

        //define fixture of the body.
        FixtureDef fd = new FixtureDef();
        fd.shape = cs;
        fd.density = 1f;
        fd.friction = 0.3f;
        fd.restitution = 1.0f;

        //create the body and add fixture to it
        body = world.createBody(bd);
        body.createFixture(fd);

        rectangle = new Rectangle(0, 0, size.x, size.y);
        rectangle.setLocation(pos.x, pos.y);
    }

    public Body getBody() { return this.body; }
    public Rectangle getRectangle() { return this.rectangle; }
}

最后我的展示班:

public class DisplayManager extends BasicGame {

    private GameManager gameManager;
    private Body b1, b2;
    private Rectangle r1, r2;
    private World world;

    public DisplayManager(GameManager game) {
        super("Title");
        this.gameManager = game;
    }

    @Override
    public void init(GameContainer container) throws SlickException {
        this.gameManager.initPlayersSprites();

        world = new World(new Vec2(0, -10));
        Square s1, s2;
        s1 = new Square(world, BodyType.STATIC, new Vec2(10, 800), new Vec2(1900, 30));
        b1 = s1.getBody();
        r1 = s1.getRectangle();
        s2 = new Square(world, BodyType.DYNAMIC, new Vec2(945, 200), new Vec2(30, 30));
        b2 = s2.getBody();
        r2 = s2.getRectangle();
    }

    public void render(GameContainer container, Graphics g) throws SlickException {
        float step = 1.0f / 600.0f;
        world.step(step, 6, 2);
        r1.setLocation(Util.toScreenX(b1.getPosition().x), Util.toScreenY(b1.getPosition().y));
        r2.setLocation(Util.toScreenX(b2.getPosition().x), Util.toScreenY(b2.getPosition().y));
        g.draw(r1);
        g.draw(r2);
    }
}

结果如下: 第一帧: http://i.stack.imgur.com/WgQPK.png

以后的一些帧: http://i.stack.imgur.com/a1XyL.png

(地面没有移动,它只是我手上的另一张截图)

换句话说,立方体像往常一样倒下。我调试了立方体的位置,它不会停下来。

使用Slick2D DebugDraw它无济于事,因为无论如何立方体都会穿过地面。
请注意,在JBox2D中,它适用于像素测量(根本不准确,但碰撞效果很好)

1 个答案:

答案 0 :(得分:0)

那是因为负面的大小。 (toPoxY反转y pos)

相关问题