我不确定我如何能够使用当前基于lwjgl的引擎和Jbox2D

时间:2016-07-06 22:00:21

标签: java lwjgl jbox2d

我用LWJGL制作了一个引擎,到目前为止我很满意。当然,我现在已准备好为引擎添加物理,所以我实际上可以开始用它创建一个游戏。我已选择使用Jbox2D,因为它似乎相当灵活,我已将其导入到项目中并完成了所需的所有设置,但是在将body系统实现到我的GameObject类并运行游戏后,所有GameObjects似乎都转到了窗口,我相信我可能在代码中犯了一些错误:

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);
}
如果你想知道的话,世界和主体hashSet都在主类中,我想我已经以某种方式严重搞砸了代码。此外,我正在使用传统的lwjgl管道,因为它更简单,更适合引擎。

1 个答案:

答案 0 :(得分:0)

我只是将我的精灵的原点设置到它们的中心而不是左下角,这解决了问题,如下所示:

 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);

我希望这可以帮助处于类似情况的任何人(sX是X大小,sY是sizeY)