Skip to main content
added 232 characters in body
Source Link

When using box2D, the camera you use to render your world should not be your resolution size, it should be your resolution divided by PPM. So when you are making your cam, do something like this:

public static final int PPM = 16; //This means that for every meter in your box2D world there are 16 pixels on your camera. If you have a body at position 1,1 and you draw an image at the location of this body the image will be drawn 16 pixels over and 16 pixels up

OrthographicCamera camera = new OrthographicCamera(Gdx.graphics.getWidth() / PPM, Gdx.graphics.getHeight() / PPM);

Now, when you are setting the position of bodies in your world, you should not use PPM at all. PPM should only be used when you make the camera.

If you wanted to set a body's position to the coordinates of the mouse, you would set it using a vector that has been projected onto the camera. For example:

Vector3 mousePos = new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0);

Vector3 worldPos = camera.project(mousePos); //Now your mousePos is in world coordinates instead of screen coordinates

Then do:

body.transform(worldPos.x, worldPos.y, body.getAngle());***

*** see below for notes on the body

A few things to note:

When you create your body in setBox2DFigures, you should call shape.dispose() as soon as your are finished with it. This means as soon as your body has been created in the world you dispose it. The object you should be working with in the box2D world is NOT the shape, it is the Body object returned from the world.createBody(...) method.

So

BodyDef bDef = new BodyDef();
bDef.type = BodyType.DynamicBody;
bDef.position.set(initPosX, initPosY);

CircleShape shape = new CircleShape();
shape.setRadius(0.5f);//half a meter radius

FixtureDef fDef = new FixtureDef();
fDef.shape = shape;

Body body = world.createBody(bodyDef);
body.createFixture(fDef);

shape.dispose();//Since you have created the fixture in your body, you are finished with the shape

I hope this helps!

When using box2D, the camera you use to render your world should not be your resolution size, it should be your resolution divided by PPM. So when you are making your cam, do something like this:

public static final int PPM = 16;

OrthographicCamera camera = new OrthographicCamera(Gdx.graphics.getWidth() / PPM, Gdx.graphics.getHeight() / PPM);

Now, when you are setting the position of bodies in your world, you should not use PPM at all. PPM should only be used when you make the camera.

If you wanted to set a body's position to the coordinates of the mouse, you would set it using a vector that has been projected onto the camera. For example:

Vector3 mousePos = new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0);

Vector3 worldPos = camera.project(mousePos); //Now your mousePos is in world coordinates instead of screen coordinates

Then do:

body.transform(worldPos.x, worldPos.y, body.getAngle());***

*** see below for notes on the body

A few things to note:

When you create your body in setBox2DFigures, you should call shape.dispose() as soon as your are finished with it. This means as soon as your body has been created in the world you dispose it. The object you should be working with in the box2D world is NOT the shape, it is the Body object returned from the world.createBody(...) method.

So

BodyDef bDef = new BodyDef();
bDef.type = BodyType.DynamicBody;
bDef.position.set(initPosX, initPosY);

CircleShape shape = new CircleShape();
shape.setRadius(0.5f);//half a meter radius

FixtureDef fDef = new FixtureDef();
fDef.shape = shape;

Body body = world.createBody(bodyDef);
body.createFixture(fDef);

shape.dispose();//Since you have created the fixture in your body, you are finished with the shape

I hope this helps!

When using box2D, the camera you use to render your world should not be your resolution size, it should be your resolution divided by PPM. So when you are making your cam, do something like this:

public static final int PPM = 16; //This means that for every meter in your box2D world there are 16 pixels on your camera. If you have a body at position 1,1 and you draw an image at the location of this body the image will be drawn 16 pixels over and 16 pixels up

OrthographicCamera camera = new OrthographicCamera(Gdx.graphics.getWidth() / PPM, Gdx.graphics.getHeight() / PPM);

Now, when you are setting the position of bodies in your world, you should not use PPM at all. PPM should only be used when you make the camera.

If you wanted to set a body's position to the coordinates of the mouse, you would set it using a vector that has been projected onto the camera. For example:

Vector3 mousePos = new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0);

Vector3 worldPos = camera.project(mousePos); //Now your mousePos is in world coordinates instead of screen coordinates

Then do:

body.transform(worldPos.x, worldPos.y, body.getAngle());***

*** see below for notes on the body

A few things to note:

When you create your body in setBox2DFigures, you should call shape.dispose() as soon as your are finished with it. This means as soon as your body has been created in the world you dispose it. The object you should be working with in the box2D world is NOT the shape, it is the Body object returned from the world.createBody(...) method.

So

BodyDef bDef = new BodyDef();
bDef.type = BodyType.DynamicBody;
bDef.position.set(initPosX, initPosY);

CircleShape shape = new CircleShape();
shape.setRadius(0.5f);//half a meter radius

FixtureDef fDef = new FixtureDef();
fDef.shape = shape;

Body body = world.createBody(bodyDef);
body.createFixture(fDef);

shape.dispose();//Since you have created the fixture in your body, you are finished with the shape

I hope this helps!

Source Link

When using box2D, the camera you use to render your world should not be your resolution size, it should be your resolution divided by PPM. So when you are making your cam, do something like this:

public static final int PPM = 16;

OrthographicCamera camera = new OrthographicCamera(Gdx.graphics.getWidth() / PPM, Gdx.graphics.getHeight() / PPM);

Now, when you are setting the position of bodies in your world, you should not use PPM at all. PPM should only be used when you make the camera.

If you wanted to set a body's position to the coordinates of the mouse, you would set it using a vector that has been projected onto the camera. For example:

Vector3 mousePos = new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0);

Vector3 worldPos = camera.project(mousePos); //Now your mousePos is in world coordinates instead of screen coordinates

Then do:

body.transform(worldPos.x, worldPos.y, body.getAngle());***

*** see below for notes on the body

A few things to note:

When you create your body in setBox2DFigures, you should call shape.dispose() as soon as your are finished with it. This means as soon as your body has been created in the world you dispose it. The object you should be working with in the box2D world is NOT the shape, it is the Body object returned from the world.createBody(...) method.

So

BodyDef bDef = new BodyDef();
bDef.type = BodyType.DynamicBody;
bDef.position.set(initPosX, initPosY);

CircleShape shape = new CircleShape();
shape.setRadius(0.5f);//half a meter radius

FixtureDef fDef = new FixtureDef();
fDef.shape = shape;

Body body = world.createBody(bodyDef);
body.createFixture(fDef);

shape.dispose();//Since you have created the fixture in your body, you are finished with the shape

I hope this helps!