New to Jbox2d. Got a pair of problems

Hello guys 🙂

I just started using jbox2d today in my game and I'm currently having an issue and I can't find a way to fix it. I have many static bodies and one dynamic body (which is the player) and I want to simulate collisions.

Basically as soon as the method world.step is called my dynamic body moves out of the original position on both the x and y axis (from 18.0 to 19.25 and from 26.0 to 70.76 . I tried to print everything else (the position of static bodies and the movement of the dynamic body) and it's correct.

I though it had to do with the gravity (which I don't need), but after I set the world and dynamic body to 0.0f, it still gives the same problem. The I figured out the problem is the fixture of my dynamic entity (If I remove it the problem doesn't occur anymore)

Also the body and the dynamic entity are moving slightly differently (in pixels, so I guess my setLinearVelocity method might be wrong)

How can I fix this and what's these problems?

This is my code (I avoid useless stuffs and I keep only those regarding jbox2d):

Map class, which contains a map of the game.

//fields

Code: Select all

public static final float METER_IN_PIXELS = 40.0f;
private World world;
...



//constructor

Code: Select all

public Map(int startPosX, int startPosY)
{
   world = new World(new Vec2(0,10.0f));
   ...
}


//called every 33ms

Code: Select all

public void update(int delta)
{
   world.step(1/60.0f, 8, 3);
   player.update(delta); // to update the player's movement
        ...
}


PhysicsMovingEntity the class used by all the moving entities (at the moment it's extended only by the player

//fields

Code: Select all

private Body body;
private float physicsX;
private float physicsY;


//constructor

public PhysicsMovingEntity(float x, float y, World world)

Code: Select all

{
   super.setX(x);
   super.setY(y);
   physicsX = x / Map.METER_IN_PIXELS;
   physicsY = y / Map.METER_IN_PIXELS;
   initPhysicalBody(world);
}


//init method

Code: Select all

public void initPhysicalBody(World world)
{
   BodyDef bd = new BodyDef();
   bd.type = BodyType.DYNAMIC;
   bd.position.set(physicsX, physicsY);
   bd.gravityScale = 0;
   PolygonShape ps = new PolygonShape();
   ps.setAsBox(getFrameSize().width() / Map.METER_IN_PIXELS, getFrameSize().height() / Map.METER_IN_PIXELS);
   FixtureDef fd = new FixtureDef();
   fd.density = 1;
   fd.shape = ps;
   body = world.createBody(bd);
   [color=#FF4000]body.createFixture(fd);[/color] // this toggles the problem
}


//called every 33ms

Code: Select all

public void update(int delta)
{
   super.update(delta);
   [color=#FF4000]body.setLinearVelocity(new Vec2(getLastDirection().getX() * getSpeed(),
         getLastDirection().getY() * getSpeed()));[/color] // probably giving the second problem
}


I really hope you guys can help me solving this problem.

Thanks a lot!
guys I fixed it. The problem was with the position of the walls wrongly set. The player was hitting them when the game was starting.

Another question please 😛
Is there a way to set the origin of the box to the top left corner of it instead of the center when I use the setAsBox method?

Original thread: https://box2d.org/forum/viewtopic.php?t=9454