1
\$\begingroup\$

I am confused about pixels and meters in Box2D. I know that Box2D work only in mters so I need SCALING FACTOR. How big I need it? For example I want that from my mouse coordinates position X and Y for example(1527, 30) throw an Box2D object how I need to do that? if my scaling factor is private final static float PPM = 1/100f; so my

x = 152,7, y = 3meters

Here is my code

private void setBox2DFigures(){
        world = new World(new Vector2(0,-9.81f), true);
        rend = new Box2DDebugRenderer();
        cam = new OrthographicCamera(); // Camera 
        body = new BodyDef();
        bodyPhysics = new FixtureDef();

        CircleShape c = new CircleShape();
        body.type = BodyType.DynamicBody;
        c = new CircleShape();
        c.setRadius(20*PPM);
        c.setPosition(new Vector2(Gdx.input.getX()*PPM, Gdx.input.getY()*PPM));
        bodyPhysics.shape = c;
        world.createBody(body).createFixture(bodyPhysics);
}

and here in a draw method I want to coin position set to my mouse position

public void draw(){
    c.setPosition(new Vector2(Gdx.input.getX()*PPM, Gdx.input.getY()*PPM)); // mouse coordinates * PPM
    rend.render(world, cam.combined);

}

But I don't see anything in my screen. Whats wrong? How I need to choose My scaling factor(PPM)?

here's my main

public class DesktopLauncher {
    public static void main (String[] arg) {
        LwjglApplicationConfiguration cfg = new LwjglApplicationConfiguration();

        cfg.title = "Catch Me!";
        cfg.width = 1800; //1800
        cfg.height = 900; //900

        new LwjglApplication(new Game(), cfg);
    }
}
\$\endgroup\$
  • \$\begingroup\$ Is there are any solutions to this problem? I have found maybe I need to change my cam, but when I do this cam = new OrthographicCamera(Gdx.graphics.getWidth()*PPM, Gdx.graphics.getHeight()*PPM); I don't get the result that I need \$\endgroup\$ – user2865835 Nov 29 '14 at 11:19
3
\$\begingroup\$

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!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.