0

I have made an engine with LWJGL and I am pleased with it so far. Naturally I am now ready to add physics to the engine so I can actually start creating a game with it. I have chosen to use Jbox2D as it seems fairly flexible and I have imported it into the project and done all the setup needed, however after implementing the body system into my GameObject class and running the game all GameObjects seemed to go to the centre of the window, I believe I may have made some mistakes in the code:

private BodyDef bodyDef = new BodyDef();
private Body box = world.createBody(bodyDef);

public float MOVE_SPEED = 7.0f;

public TAG tag;

public static int direction = 0;
public int jumpsRemaining;

public float xSpeed = MOVE_SPEED;
public float ySpeed = MOVE_SPEED;
public float x, y;
public float sX = 32, sY = 32;

public boolean jumpPressed, jumpWasPressed;
public boolean jumping = false;
public boolean falling = true;

//public int collidingX = 0;
//public int collidingY = 0;
public Time time = new Time();

protected static final int UP = 1;
protected static final int RIGHT = 2;
protected static final int DOWN = 3;
protected static final int LEFT = 4;

private boolean left = true;
private boolean right = true;
private boolean up = true;
private boolean down = true;

private Sprite spr;

public GameObject(float sX, float sY, float posX, float posY, BodyType type) {
    this.sX = sX;
    this.sY = sY;
    this.x = posX;
    this.y = posY;
    initPhysics(type);

}

public GameObject(float posX, float posY, BodyType type) {
    this.x = posX;
    this.y = posY;
    initPhysics(type);
}

public abstract void update();

public abstract void input();

private void initPhysics(BodyType type){
    bodyDef.type = type;
    bodyDef.position.set(x/30,y/30);
    PolygonShape boxShape = new PolygonShape();
    boxShape.setAsBox(sX/30/2, sY/30/2);
    FixtureDef fixture = new FixtureDef();
    fixture.density = 1;
    fixture.shape = boxShape;
    box.createFixture(fixture);
    physicsBodies.add(box);
}

/**protected GameObject Colliding() {
    for (GameObject gob : gameObjects) {
        if (gob != this) {
            if (Collision.checkCollision(gob, this) != null) {
                return gob;
            }
        }
    }
    return null;
}**/

public void render() {

    if (spr != null) {
        glPushMatrix();
        {
            glTranslatef(box.getPosition().x * 30, box.getPosition().y * 30, 0);
            spr.renderSprite();
        }
        glPopMatrix();
    }
}

public void setTexture(String tex) {
    spr = new Sprite(sX, sY, tex);
}

the world and body hashSet are in the main class if you are wondering, I think I have severely screwed up the code somehow. Also I am using the legacy pipeline of lwjgl as it is simpler and fits the engine better.

  • Are you considering the fact that Box2D uses meters instead of pixels? – Alberto Alegria Jul 06 '16 at 22:05
  • yes that's why I am dividing or multiplying by 30 of which is the supposed algorithm of exchange but I am doubtful.... – zipper9998 Jul 07 '16 at 08:18
  • perhaps it is the fact that box2D uses the centre of a body as its origin (which is why I am dividing the size by 2) and lwjgl uses another point... – zipper9998 Jul 07 '16 at 08:20
  • http://stackoverflow.com/questions/28028121/how-many-pixels-is-a-meter-in-box2d oh jeez.... – zipper9998 Jul 07 '16 at 08:36
  • is it possible that my GameObject class has to extend the box2D body class? – zipper9998 Jul 07 '16 at 14:11
  • Maybe you need to check all the documentation. Have you tried to experiment with your code until works as you want? Once I tried to make a minigame using C++ Box2D for my class, and almos all the problems I have are solved changing some values, trying another way to do X thing, etc. BTW, in C++ when we create the world we assign a Vec2d vector who represents gravity. I don't see this in your code. – Alberto Alegria Jul 07 '16 at 17:04
  • ok I shall look through the documentation, the gravity etc is declared in a different class, thanks for your help so far :) – zipper9998 Jul 10 '16 at 18:58
  • @Alberto ok so I printed out the all the positions of bodies to the console and found that they are all stuck at 0,0 despite the fact that they were not originally placed there, they are all unmoving...the world is definitely being updated – zipper9998 Jul 11 '16 at 07:41
  • @Alberto FIXED (hopefully) the bodies now move at least – zipper9998 Jul 11 '16 at 08:17
  • @Alberto I am just struggling now to convert from LWJGL Coordinates to box2d coordinates. – zipper9998 Jul 11 '16 at 10:02

1 Answers1

0

I simply set the origin of my sprites to their centre rather than the bottom left and that solved the problem, like this:

 glVertex2f(-sX / 2, -sY / 2);
 glTexCoord2f(0, 0);

 glVertex2f(-sX / 2, sY / 2);
 glTexCoord2f(1, 0);

 glVertex2f(sX / 2, sY / 2);
 glTexCoord2f(1, 1);

 glVertex2f(sX / 2, -sY / 2);
 glTexCoord2f(0, 1);

I hope this helps anyone in a similar situation (sX is size X, sY is sizeY)