Dani007
Dani007

Reputation: 151

libGDX-scaling by PPM for box2d, stops tiled map from rendering

I'm making a game with a tiled map I've made an implemented, and I've added all the static bodies to my Box2d world. However, when I added my dynamic body, it was too slow no matter how high the velocity. After some research I found out I need to scale my map using pixel per meter(PPM). However, when I try to scale the map by passing the unit-ratio in my Orthogonal map renderer, along side with the map as follow:

        mapLoader = new TmxMapLoader();
        map = mapLoader.load("game_map1.tmx");
        mapRenderer = new OrthogonalTiledMapRenderer(map, 1/PPM);

unless PPM 1, I get a black screen instead of the map. I'm also scaling the viewport, but that doesn't seem to be the problem; since when I tried just doing the viewports, the map rendered and the graphics were larger.

I also tried moving by the setTransform(), method as follow

    private void handleInput(float delta) {
        if(Gdx.input.isTouched()){
                float diff_x = Gdx.input.getX() - (gameport.getScreenWidth()/2);
                float diff_y = Gdx.input.getY() - (gameport.getScreenHeight()/2);

                if(diff_x != 0) {
                    float new_x = player.body.getPosition().x + diff_x / Math.abs(diff_x);
                    float new_y = player.body.getPosition().y - diff_y / Math.abs(diff_x);
                    player.body.setTransform(new_x, new_y, 1);
                }

But this way the collisions aren't perfect, the dynamic object sometimes goes through walls(static objects) or gets stuck in them. Any help with scaling or fixing the set-transform method will be appreciated.

My goal right now is to make my object move through the map while colliding with walls if it tries to go through them.

Upvotes: 1

Views: 297

Answers (2)

EL.ALEX78UK
EL.ALEX78UK

Reputation: 109

I believe you are making right. The scale of your map comparing to the B2Box world is different and you always need to adjust the positions and sizes.

To learn more and don't forget any detail regarding map / viewerport / b2bodies, follow this tutorial that I use as my guide for B2Box (Youtube / Github), it is very quick and easy to follow and you are going to love use B2Box for everything :-) and also continue with LibGdx Framework developments.

Click here Youtube Tutorial.

Click here GitHub codes.

I hope it helps you.

Cheers!!

Upvotes: 1

Dani007
Dani007

Reputation: 151

The problem seemed to be that the "Unitscale" parameter in the maprenderer's constructor must be a float and not a double, so you must define your PPM as a float or put an f next to your variable you are passing in.

Upvotes: 1

Related Questions