Need some help learning jbox2d

211 views Asked by At

first of all thanks a lot for your time :)

I'm currently trying to understand how jbox2d works and I'm having some issues. The code I wrote makes sense to me, but there should be something I didn't understand at all. Basically what I want to do at the moment is making the main character (controlled by the player) collide with the walls.

Without going too much into details I have a dynamic entity class called Player and a static entity class called Wall. I also have a class called Map which handles the level. The coordinates of the entities are represented by the pixels in the screen.

now this is the part regarding jbox2d

in the class Map I have:

// ... other fields
private static final float TIME_STEP = 1.0f / 60.f;
private static final int VELOCITY_ITERATIONS = 6;
private static final int POSITION_ITERATIONS = 3;
private World world;

// constructor
public Map(int startPosX, int startPosY)
{
        // ...other stuffs
    Vec2 gravity = new Vec2(0, -10f);
    world = new World(gravity);
        // ...other stuffs
}

// update method that is called every 30 ms
public void update(int delta)
{
        // ...other stuffs
    world.step(TIME_STEP, VELOCITY_ITERATIONS, POSITION_ITERATIONS);
}

now this is how the static entity looks like:

private Map map;
private Body body;
private Fixture fixture;
private PolygonShape shape;

public Wall(int x, int y, Map map)
{
    super(x, y);
    this.map = map;
    BodyDef bodyDef = new BodyDef();
    bodyDef.position.set(x, y);
    bodyDef.type = BodyType.STATIC;
    shape = new PolygonShape();
    shape.setAsBox(CELL_HEIGHT, CELL_WIDTH);
    FixtureDef fixtureDef = new FixtureDef(); 
    fixtureDef.shape = shape;
    body = map.getWorld().createBody(bodyDef);
    fixture = body.createFixture(fixtureDef);
}

and finally the player:

private Map map;
private PolygonShape shape;
private Body body;
private Fixture fixture;

public MovingEntity(float x, float y, Map map)
{
    super.setX(x);
    super.setY(y);
    animation = Animation.IDLE;
    layer = graphics().createImmediateLayer(new EntityRenderer(this));
    layer.setVisible(false);
    graphics().rootLayer().add(layer);

    BodyDef bodyDef = new BodyDef();
    bodyDef.position.set(x, y);
    bodyDef.type = BodyType.DYNAMIC;
    shape = new PolygonShape();
    shape.setAsBox(getFrameSize().height(), getFrameSize().width());
    FixtureDef fixtureDef = new FixtureDef(); 
    fixtureDef.shape = shape;
    body = map.getWorld().createBody(bodyDef);
    fixture = body.createFixture(shape, 2.0f);
}

do you guys now what am I doing wrong? The entities don't collide at all. Also if I try to print the current position of the player's body in my update method I'm getting coordinates changing even if I don't move (I guess like it is going down because of gravity, which I don't need in my game).

Thanks again a lot!

1

There are 1 answers

0
Paul Weibert On

I think your entities do not collide because you are using empty polygon shapes.

shape = new PolygonShape();

You have to define the points of your Polygone shape so that jbox can test collision of the shapes. Something like this:

Vec2[] vertices = {
                new Vec2(0.0f, - 10.0f),
                new Vec2(+ 10.0f, + 10.0f),
                new Vec2(- 10.0f, + 10.0f)
        };

        PolygonShape shape = new PolygonShape();
        shape.set(vertices, vertices.length);

Further if you don't need gravity then just set the gravity vector to 0,0

Vec2 gravity = new Vec2(0f, 0f);