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:
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.
Also, if needed, here is the code I use to control the body (credits to Bad Logic Games for a standard base):
If you could help out in any way or need more code from me, please do reply. Hope you can help out, thanks.
- Senthior
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);
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);
}
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));
}
}
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