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);
}
如果你想知道的话,world和body hashSet都在主类中,我想我一定是把代码搞砸了。此外,我正在使用lwjgl的传统管道,因为它更简单,更适合发动机
# 1 楼答案
我只是简单地将精灵的原点设置为中心,而不是左下角,这样就解决了问题,如下所示:
我希望这能帮助任何处于类似情况的人(sX是X码,sY是sizeY码)