java – TiledMap Libgdx Box2d Scaling

I think I'm doing something completely wrong but I just can not think of converting box2d pixels per meter. Currently, I have a tiled map with an object layer that I want to use as a collision area. I have a class to take the object layer and transform it into shapes and another to convert units to meters. The problem I have is that I can only get the correct size of the boxes on my map if the number of pixels per meter is set to 1: 1.

In the constructor of my level class, I have this to create all box2d objects:

world = new world (new vector 2 (0f, -9,8f), true);
MapObjects objects = map.getLayers (). Get ("Collisions"). GetObjects ();
for (MapObject object: objects) {
if (TextureMapObject object instance) {
Carry on;
}

Shape form;

if (object instanceof RectangleMapObject) {
shape = ShapeFactory.getRectangle (object (RectangleMapObject));
} else if (instance of object of PolygonMapObject) {
shape = ShapeFactory.getPolygon (object (PolygonMapObject));
} else if (object instance of PolylineMapObject) {
shape = ShapeFactory.getPolyline (object (PolylineMapObject));
} else if (object instance of CircleMapObject) {
shape = ShapeFactory.getCircle (object (CircleMapObject));
} other {
Carry on;
}

BodyDef bodyDef = new BodyDef ();
bodyDef.type = BodyDef.BodyType.StaticBody;
Body body = world.createBody (bodyDef);
body.createFixture (form, 1);

shape.dispose ();
}
BodyDef bodyDef = new BodyDef ();
bodyDef.type = BodyDef.BodyType.DynamicBody;
bodyDef.position.set (x, y);
Body body = world.createBody (bodyDef);

FixtureDef fdef = new FixtureDef ();
PolygonShape form = new PolygonShape ();
shape.setAsBox (16, 16);
fdef.shape = form;
fdef.density = 1.0f;
fdef.friction = 0.3f;
fdef.restitution = 0.6f;
body.createFixture (fdef);


debugRenderer = new Box2DDebugRenderer ();

This calls methods from my shapeFactory class that convert mapobjects to shapes.

Utils package;

import com.badlogic.gdx.maps.objects.CircleMapObject;
import com.badlogic.gdx.maps.objects.PolygonMapObject;
import com.badlogic.gdx.maps.objects.PolylineMapObject;
import com.badlogic.gdx.maps.objects.RectangleMapObject;
import com.badlogic.gdx.math.Circle;
import com.badlogic.gdx.math.Polygon;
import com.badlogic.gdx.math.Polyline;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.ChainShape;
import com.badlogic.gdx.physics.box2d.CircleShape;
import com.badlogic.gdx.physics.box2d.PolygonShape;

public final class ShapeFactory {

private ShapeFactory () {}

PolygonShape static public getRectangle (RectangleMapObject rectangleObject) {
return getRectangle (rectangleObject.getRectangle ());
}

PolygonShape static public getRectangle (Rectangle rectangle) {
PolygonShape polygon = new PolygonShape ();

Vector2 size = new Vector2 (
Physics.toUnits (rectangle.x + rectangle.width * 0.5f),
Physics.toUnits (rectangle.y + rectangle.height * 0.5f)
)

polygon.setAsBox (
Physics.toUnits (rectangle.width * 0.5f),
Physics.toUnits (rectangle.height * 0.5f),
Cut,
0.0f
)

return the polygon;
}

static public CircleShape getCircle (CircleMapObject circleObject) {
returns getCircle (circleObject.getCircle ());
}

public static CircleShape getCircle (circled circle) {
CircleShape circleShape = new CircleShape ();
circleShape.setRadius (Physics.toUnits (circle.radius));
circleShape.setPosition (new Vector2 (Physics.toUnits (circle.x), Physics.toUnits (circle.y)));

return circleShape;
}

static public PolygonShape getPolygon (PolygonMapObject polygonObject) {
returns getPolygon (polygonObject.getPolygon ());
}

static public PolygonShape getPolygon (Polygon Polygon) {
PolygonShape polygonShape = new PolygonShape ();

fleet[] vertices = polygon.getTransformedVertices ();
fleet[] worldVertices = new float[vertices.length];

for (int i = 0; i <vertices.length; ++ i) {
worldVertices[i] = Physics.toUnits (summits[i])
}

polygonShape.set (worldVertices);

return polygonShape;
}

static static ChainShape getPolyline (PolylineMapObject polylineObject) {
returns getPolyline (polylineObject.getPolyline ());
}

public static ChainShape getPolyline (polyline polyline) {
ChainShape chain = new ChainShape ();

fleet[] vertices = polyline.getTransformedVertices ();
Vector2[] worldVertices = new Vector2[vertices.length / 2];

for (int i = 0; i <vertices.length / 2; ++ i) {
worldVertices[i] = new vector2 ();
worldVertices[i].x = Physics.toUnits (vertices[i * 2])
worldVertices[i].y = Physics.toUnits (summits[i * 2 + 1])
}

chain.createChain (worldVertices);

return chain;
}
}

This also calls methods in my physics class that converts units to pixels:

Utils package;


import com.badlogic.gdx.math.Vector;
import com.badlogic.gdx.math.Vector2;

public final class Physics {

final static public float PIXELS_PER_UNIT = 32f;

Private Physics () {}

public static float toUnits (float pixels) {
return pixels / PIXELS_PER_UNIT;
}

static public Vector toUnits (Vector2 pixels) {
returns the new Vector2 (toUnits (pixels.x), toUnits (pixels.y));
}

public float toPixels (float units) {
return units * PIXELS_PER_UNIT;
}

static public Vector2 toPixels (Vector2 units) {
returns the new vector 2 (toPixels (units.x), toPixels (units.y));
}
}

When My pixels per unit equals 1, it aligns perfectly but the physics acts as if everything is huge.

enter the description of the image here

But when My pixels per unit is 32 as I want it, it's too small.
enter the description of the image here