简体   繁体   English

JBox2D + Slick2D - 不会发生碰撞

[英]JBox2D + Slick2D - Doesn't collide


UPDATE UPDATE

I have a hint. 我有一个提示。 That is because the center of the screen in JBox2D is (0, 0) . 这是因为JBox2D中屏幕的中心是(0, 0) This is so weird because x and y are not from -1 to 1, it depends of the screen size. 这很奇怪,因为xy不在-11,这取决于屏幕尺寸。
This means the Util class I copied from the internet (everybody shared this code) is not up to date. 这意味着我从互联网上复制的Util类(每个人都共享此代码)不是最新的。 I still need help because I can't find any 'algorithm' to have aspect ratio + screen size dependency. 我仍然需要帮助,因为我找不到任何具有宽高比+屏幕尺寸依赖性的“算法”。


BASE QUESTION 基础问题

Since I have added the "slick2D to jbox2D position" and vice versa (it's like position in screen = position in screen / 100 for JBox), I don't get a good result. 由于我已经将“slick2D添加到jbox2D位置”,反之亦然(就像屏幕中的位置=屏幕中的位置/ JBox中的100位置),我得不到好的结果。 Screenshots below 截图如下

I'm explaining with code: 我用代码解释:

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 : 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; }
}

And finally my display 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);
    }
}

Here is the result : first frame : http://i.stack.imgur.com/WgQPK.png 结果是:第一帧: http : //i.stack.imgur.com/WgQPK.png

some frames later : http://i.stack.imgur.com/a1XyL.png 一些帧以后: http : //i.stack.imgur.com/a1XyL.png

(the ground didn't move, it's just another screenshot took from my hand) (地面没有动,这只是我手里的另一个屏幕截图)

In other words, the cube falls down like for ever. 换句话说,立方体像永远一样掉落。 I debugged the position of the cube and it doesn't stop to go down. 我调试了立方体的位置,它并没有停下来。

With Slick2D DebugDraw it doesn't help because the cube goes through the ground anyway. 使用Slick2D DebugDraw并没有帮助,因为立方体仍然会穿过地面。
Please note that in JBox2D, it worked with pixels measurements (that was not accurate at all but collisions worked well) 请注意,在JBox2D中,它适用于像素测量(根本不准确,但碰撞效果很好)

That was because of the negative size. 那是因为大小为负。 (toPoxY reverse the y pos) (toPoxY反转y pos)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM