So I was working on my platformer project using the Slick and JBox2D engines, when suddenly the dynamic bodies seemed to stop colliding with each other. I've tried everything I can think of, and the only thing I've come up with so far is that they will only collide with static bodies. That is, a dynamic body will stop falling when it hits a static body, but will just fall through another dynamic one as if it wasn't even there.
I remember accidentally two dynamic bodies at the same position, and they pushed each other to the sides, which obviously doesn't happen any more. So apparently, it did work before.
Another thing that is strange is that when I tried to make one of the bodies fall by rotating off a static body, by making only its edge hit the static and nothing more, it just stayed on as if it had rotation turned off. I'm starting to think it's a bug caused by installing the engine wrong or so, or maybe messing with it too much (I tried moving my project to DropBox, causing the whole engine to disappear from the newly created workspace). But when I reinstalled it, nothing seemed different.
Here's some code from my project:
Where I initialize the objects with physical bodies:
Code:
Image image = null;
try {
image = new Image("sprites/body_base.png");
} catch (SlickException e) {
e.printStackTrace();
}
PolygonShape shape = new PolygonShape();
shape.setAsBox(gs.toMeters(image.getWidth()/2),
-gs.toMeters(image.getHeight()/2));
PolygonShape shape2 = new PolygonShape();
shape2.setAsBox(gs.toMeters(image.getWidth()/2),
-gs.toMeters(image.getHeight()/2));
//This one's got a dynamic body
Actor testActor = new Actor(gs, new Vec2(5f, 30f),
shape, image);
//This one's static
Solid testSolid = new Solid(gs, new Vec2(5f, 5f),
shape, image);
//Dynamic
Item testItem = new Item(gs, new Vec2(5f, 20f),
shape, image);
addActor(testActor);
addSolid(testSolid);
addItem(testItem);
The class which all the classes above (actor, solid, item) inherit:
Code:
public class PhysicsEntity implements Entity {
protected Body body;
protected GameplayState gs;
protected Image image = null;
protected Vec2 position;
public PhysicsEntity(GameplayState gs, Vec2 position, BodyDef bd,
Shape shape, Image image) {
this.gs = gs;
this.position = position;
if(image != null) {
this.image = image;
}
FixtureDef fd = new FixtureDef();
fd.shape = shape;
fd.density = 1.0f;
body = gs.getPhysics().getWorld().createBody(bd);
body.createFixture(fd);
}
public void init() {
}
public void update() {
position = body.getPosition();
}
public void render() {
if(image != null) {
image.draw(
gs.toPixels(position.x),
-gs.toPixels(position.y)+gs.getHeight());
}
}
}
The method which all of the classes use to generate the BodyDef. The variations are so minor I'll only include the one for Actor (note the fixedRotation=true), and I've even tried varying them but come up with that the only thing that makes a difference is the BodyType being static.
Code:
protected static BodyDef makeBodyDef(Vec2 position, BodyType type) {
BodyDef bd = new BodyDef();
bd.position = position;
bd.type = type;
bd.fixedRotation = true;
bd.allowSleep = false;
return bd;
}
Sorry for the long code samples, if there's a way to minimize the length of the post by for example using one of those "spoiler" buttons, I'd like to know. <:
Thanks for any replies.