JBox2D + Slick2D - 不碰撞

JBox2D + Slick2D - Doesn't collide


更新

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


基本问题

因为我添加了 "slick2D to jbox2D position" ,反之亦然(对于 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; }
}

最后是我的显示器 class:

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 中,它使用像素测量(一点也不准确,但碰撞效果很好)

那是因为负尺寸。 (toPoxY 反转 y pos)