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.