0

UPDATE

I have a hint. That is because the center of the screen in JBox2D is (0, 0). This is so weird because x and y are not from -1 to 1, it depends of the screen size.
This means the Util class I copied from the internet (everybody shared this code) is not up to date. 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. 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 :

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 : https://i.stack.imgur.com/WgQPK.png

some frames later : https://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.
Please note that in JBox2D, it worked with pixels measurements (that was not accurate at all but collisions worked well)

1 Answer 1

0

That was because of the negative size. (toPoxY reverse the y pos)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.