Projection for tiledmaprenderer vs shaperenderer

Anything libgdx related goes here!

Projection for tiledmaprenderer vs shaperenderer

Postby carterza » Fri Nov 04, 2016 5:17 pm

Hello,

I'm rendering a tiledmap using the tiledmaprenderer class and I'm trying to draw shapes on top of the tiled map using the shaperenderer libgdx offers.

It seems the two are using different projection matrices or something - as when I give coordinates to my shaperenderer, they don't translate to my tiled map coordinates.

For instance, if I have a tile at position (1,1) and my tile size is 24, when I try to render a shape at 1 * 24 - it's rendered no where near the tile at all.

Here's a screenshot of what I'm talking about -

Image

There I'm using the tile coordinates scaled by a factor of 2 - and as you can see the projection for my shaperenderer is not the same as my tiled map. I expect each of those circles to be above a room of my tiled map (if I were to scale by the same size as the tile that is).

How can I achieve the same projection for both renderers?

Thanks!
carterza
 
Posts: 5
Joined: Wed Sep 21, 2016 1:35 pm

Re: Projection for tiledmaprenderer vs shaperenderer

Postby evilentity » Fri Nov 04, 2016 10:00 pm

Well, did you set it to be the same? Dont use pixels. Pixels are bad.
Looking for a freelancer? PM me!
Check out libgdx discord server!
evilentity
 
Posts: 4867
Joined: Wed Aug 24, 2011 11:37 am

Re: Projection for tiledmaprenderer vs shaperenderer

Postby rafiki » Sat Nov 05, 2016 2:08 am

in theory they should align.
hare a code that works but i don't know if is the rigth/bets way to do it.

Code: Select all

//import etc ...

public class MyTest extends ScreenAdapter{
   
    private SpriteBatch batch;
    private ShapeRenderer shapeRdr;
    private TiledMap map;
    private OrthographicCamera camera;
    private OrthogonalTiledMapRenderer mprdr;
    private FitViewport viewp;

    @Override
    public void show() {

        batch = new SpriteBatch();
        shapeRdr = new ShapeRenderer();
        camera = new OrthographicCamera(); //create a trusted OrthographicCamera
        viewp = new FitViewport(20f,15f,camera);//let's create a viewport that show an area of 20 * 15 tiles
        viewp.apply(true);  //apply the viewport to the camera and center it
        map = //create your map
        mprdr=new OrthogonalTiledMapRenderer(map, 1f/20f,batch);
        //create a renderer and say him that 1 unit
        //show 20px (in your case) of the map

        mprdr.setView(camera);
        camera.update();
        batch.setProjectionMatrix(camera.combined);
        shapeRdr.setProjectionMatrix(camera.combined);
       
    }

    @Override
    public void resize(int width, int height) {
        viewp.update(width, height);
        //don't forget this  maybe the cause of your issue !!
    }

    @Override
    public void render(float delta) {
        //clear the screen
       
        camera.update();
        mprdr.setView(camera);
        batch.setProjectionMatrix(camera.combined);
        mprdr.render();
       
        shapeRdr.setProjectionMatrix(camera.combined);
        shapeRdr.begin(ShapeRenderer.ShapeType.Line);
        //go crazy with the shapeRender
        shapeRdr.end();
    }
   
    //HIDE OR DISPOSE STUFF !!
   
}

rafiki
 
Posts: 3
Joined: Sun Oct 30, 2016 12:28 pm

Re: Projection for tiledmaprenderer vs shaperenderer

Postby evilentity » Sat Nov 05, 2016 6:30 pm

Hard to tell as you cut off most of the stuff. Heres a simple thing to test, draw the map, dont touch the camera, draw a circle, renderer.circle(0.5f, 0.5f, .5f, 16);
If the circle is not the size of a tile and is not centered on lower left tile of the map, you messed up something. Use extend viewport like a reasonable person. Black bars, what year is it, 1900?
Looking for a freelancer? PM me!
Check out libgdx discord server!
evilentity
 
Posts: 4867
Joined: Wed Aug 24, 2011 11:37 am


Return to Libgdx

Who is online

Users browsing this forum: No registered users and 1 guest