JBox2D Platformer

Hey guys, I am pretty much trying to bind Slick2D and JBox2D, however I have gone into a few problems. I have wanted to try to keep incorporating the use of screen coordinates instead of JBox2D's world coordinates, so I have used the OBBViewportTransform in order to translate these coordinates. However, if I were to render a shape to display these coordinates, I would always have to modify these coordinates in this sense:

Code:
g.drawRect(getPosition().x - (35 / 2), getPosition().y - (80 / 2), 35, 80);


Code:
public Vec2 getPosition() {
        return getWorldToScreen(body.getPosition());
    }

public Vec2 getWorldToScreen(Vec2 argWorld) {
        Vec2 screen = new Vec2();
        viewportTransform.getWorldToScreen(argWorld, screen);
        return screen;
    }

    public Vec2 getScreenToWorld(Vec2 argWorld) {
        Vec2 screen = new Vec2();
        viewportTransform.getScreenToWorld(argWorld, screen);
        return screen;
    }

private OBBViewportTransform viewportTransform;

viewportTransform = new OBBViewportTransform();
        viewportTransform.setYFlip(true);
        viewportTransform.setExtents(Trinithix.instance().getContainer().getWidth() / 2, Trinithix.instance().getContainer().getHeight() / 2);


I was wondering if the method of modifying the translated coordinates this way was fine, or if I did something wrong in translating the coordinates.

Another problem of mine is that the body itself acts very odd (When I attempt to move the character, it would fall straight flat on the floor o_o"). I don't know if this was due to my player's body being setted up oddly, as I had to divide the divide the shape's width and height by two so that it would render properly.

Code:
@Override
    public void createBody(float x, float y, World world) {
        BodyDef def = new BodyDef();
        def.type = BodyType.DYNAMIC;
        Body box = world.createBody(def);

        PolygonShape poly = new PolygonShape();
        poly.setAsBox(35 / 2, 80 / 2);
        playerBody = box.createFixture(poly, 1);
        box.setBullet(true);
        setBody(box);
    }


Also, if needed, here is the code I use to control the body (credits to Bad Logic Games for a standard base):

Code:
@Override
    public void update(GameContainer gc, int delta) {
        Input input = gc.getInput();
        Vec2 vel = getBody().getLinearVelocity();
        Vec2 pos = getBody().getPosition();

        if (Math.abs(vel.x) > MAX_VELOCITY) {
            vel.x = Math.signum(vel.x) * MAX_VELOCITY;
            getBody().setLinearVelocity(new Vec2(vel.x, vel.y));
        }

        if (!input.isKeyDown(Input.KEY_LEFT) && !input.isKeyDown(Input.KEY_RIGHT)) {
            getBody().setLinearVelocity(new Vec2(vel.x * 0.9f, vel.y));
        }

        playerBody.setFriction(0.2f);
        if (input.isKeyDown(Input.KEY_LEFT) && vel.x > -MAX_VELOCITY) {
            getBody().applyLinearImpulse(new Vec2(-2f, 0), new Vec2(pos.x, pos.y));
        } else if (input.isKeyDown(Input.KEY_RIGHT) && vel.x < MAX_VELOCITY) {
            getBody().applyLinearImpulse(new Vec2(2f, 0), new Vec2(pos.x, pos.y));
        }

        if (input.isKeyDown(Input.KEY_UP)) {
            jump = true;
        } else {
            jump = false;
        }

        if (jump) {
            jump = false;
            getBody().setLinearVelocity(new Vec2(vel.x, 0));
            getBody().setTransform(new Vec2(pos.x, pos.y + 0.01f), 0);
            getBody().applyLinearImpulse(new Vec2(0, 30), new Vec2(pos.x, pos.y));
        }

    }


If you could help out in any way or need more code from me, please do reply. Hope you can help out, thanks.

- Senthior
i use Slick and JBox2D, too.
But I dont realy get it how to use/ combine this two...

It would be great if you can give a example, like with a simple Charakter that use JBox2D.
Hope you can help (if your problem is solved 🙂)
That 'extra' transformation is for the size of the rectangle, so I'm assuming you're asking how to find that transformation given the ViewportTransform?

It's pretty simple, use the 'vector' version of the transforms, which doesn't apply the position transform and just applies the scaling/rotation. You would want to give it the size (width, height), and it'll give back a transformed width and height.

Original thread: https://box2d.org/forum/viewtopic.php?t=7833