How to add sprite batch on tile in isometric map in Libgdx

by DEMO -عرض   Last Updated August 01, 2020 11:13 AM - source

I'm using Libgdx to create an isometric game , but now I have problem.

I want to add sprite on tile (3,4) , but I can't do this, so I searched on stackoverflow , and I found this code :-

public Vector2 twoDToIso(Vector2 punto) {
        Vector2 tempP = new Vector2(0, 0);
        tempP.x = (punto.x - punto.y) * 256;
        tempP.y = (punto.x + punto.y) * 128;
        return tempP;
    }

And this code for spritebatch :-

sb.setProjectionMatrix(camera.combined);
        
        sb.begin();
        
        Vector2 pointHero = twoDToIso(new Vector2(3,4));
        
        sb.draw(texture,pointHero.x,pointHero.y);
        sb.end();

And this is all my code :-

 @Override
    public void create () {
        float w = Gdx.graphics.getWidth();
        float h = Gdx.graphics.getHeight();

        camera = new OrthographicCamera();
        camera.setToOrtho(false,w,h);
        tiledMap = new TmxMapLoader().load("es.tmx");//cas444
        renderer = new IsometricTiledMapRenderer(tiledMap);
        camera = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());


        isoTransform = new Matrix4();
        isoTransform.idt();
        isoTransform.translate(0.0f, 0.25f, 0.0f);
        isoTransform.scale((float)(Math.sqrt(2.0) / 2.0), (float)(Math.sqrt(2.0) / 4.0), 1.0f);
        isoTransform.rotate(0.0f, 0.0f, 1.0f, -45.0f);

        //... and the inverse matrix
        invIsotransform = new Matrix4(isoTransform);
        invIsotransform.inv();

        camera.translate(100,30);
        camera.update();

        InputMultiplexer im = new InputMultiplexer();
        GestureDetector gd = new GestureDetector(this);
        im.addProcessor(gd);
        im.addProcessor(this);
        Gdx.input.setInputProcessor(im);
        
        sb = new SpriteBatch();
        texture = new Texture(Gdx.files.internal("castle2.png"));
        font = new BitmapFont();
        
        
        mapWidth = tiledMap.getProperties().get("tilewidth",Integer.class);
        mapHeight = tiledMap.getProperties().get("tileheight",Integer.class);

        

    }

    @Override
    public void render () {
        Gdx.gl.glClearColor(.5f, .7f, .9f, 1);
        Gdx.gl.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        

        camera.update();
        renderer.setView(camera);
        renderer.render();
    sb.setProjectionMatrix(camera.combined);
        
        sb.begin();
        
        //font.draw(sb, "hhbgjuugfff", 4.0f, 3.0f);
        //sb.draw(texture, 4, 5);
        
        //Vector2 pointHero = twoDToIso(new Vector2(3,4));
        Vector2 pointHero = twoDToIso(new Vector2(3,4));
        
        sb.draw(texture,pointHero.x,pointHero.y);
        sb.end();



    }
    public Vector2 twoDToIso(Vector2 punto) {
        Vector2 tempP = new Vector2(0, 0);
        tempP.x = (punto.x - punto.y) * 256;
        tempP.y = (punto.x + punto.y) * 128;
        return tempP;
    }
    
    
    public Vector2 worldToCell(float x, float y) {
        float halfTileWidth = mapWidth * 0.5f;
        float halfTileHeight = mapHeight * 0.5f;

        float row = (1.0f/2) * (x/halfTileWidth + y/halfTileHeight);
        float col = (1.0f/2) * (x/halfTileWidth - y/halfTileHeight);

        return  new Vector2((int)col,(int)row);
    }

    public Vector2 screenToWorld(float x, float y){
        Vector3 touch = new Vector3(x,y,0);
        camera.unproject(touch);
        touch.mul(invIsotransform);
        touch.mul(isoTransform);
        return  new Vector2(touch.x,touch.y);
    }


    public Vector2 screenToCell(float x, float y) {
        Vector2 world = screenToWorld(x,y);
        world.y -= mapHeight *0.5f;
        return worldToCell(world.x,world.y);
    }

    @Override
    public boolean touchDown(int screenX, int screenY, int pointer, int button) {

        return false;
    }

    @Override
    public boolean touchUp(int screenX, int screenY, int pointer, int button) {
        lastx=0;
        lasty=0;
        return false;
    }

    @Override
    public boolean touchDragged(int screenX, int screenY, int pointer) {

        if (lastx != 0) {
            camera.translate(-screenX - lastx, screenY - lasty);
            lastx = -screenX;
            lasty = screenY;

        }

        return false;
    }

    @Override
    public boolean mouseMoved(int screenX, int screenY) {

        return false;
    }

    @Override
    public boolean scrolled(int amount) {

        return false;
    }

    @Override
    public boolean keyDown(int p1)
    {
        // TODO: Implement this method
        return false;
    }

    @Override
    public boolean keyUp(int p1)
    {
        // TODO: Implement this method
        return false;
    }

    @Override
    public boolean keyTyped(char p1)
    {
        // TODO: Implement this method
        return false;
    }

    @Override
    public boolean touchDown(float screenX, float screenY, int p3, int p4)
    {
        cell = screenToCell(screenX,screenY);

       /* TiledMapTileLayer layer = (TiledMapTileLayer)tiledMap.getLayers().get("Layer1");

        TiledMapTileLayer.Cell tileCell = layer.getCell((int) cell.x, (int) cell.y);
        tileCell.setFlipHorizontally(!tileCell.getFlipHorizontally());
        */
        
        //layer.setCell((int)cell.x,(int)cell.y,cell_click);
        
        
        
        
        //c=cell.toString();
        c=cell.x+" / "+cell.y;
        

        lastx=-screenX;
        lasty=screenY;
        Vector3 clickCoordinates = new Vector3(screenX,screenY,0);
        Vector3 position = camera.unproject(clickCoordinates);
        
        translate=position.x;



        return true;
    }

    @Override
    public boolean tap(float p1, float p2, int p3, int p4)
    {
        cell = screenToCell(p1,p2);
        //tileLayer.setCell((int)cell.x,(int)cell.y,cell_click);
        
        newCastle = new castle(tiledMap,camera,(int)cell.x,(int)cell.y);
        
        
        return false;
    }

    @Override
    public boolean longPress(float p1, float p2)
    {
        // TODO: Implement this method
        return false;
    }

    @Override
    public boolean fling(float p1, float p2, int p3)
    {
        // TODO: Implement this method
        return false;
    }

    @Override
    public boolean pan(float x, float y, float deltaX, float deltaY) {
        

        camera.translate(-deltaX * zoom,deltaY * zoom);

        camera.update();
        return true;
    }
    @Override
    public boolean zoom(float initialDistance, float distance) {
        
        camera.zoom = (initialDistance / distance) * zoom;
        camera.update();

        return true;
    }
    @Override
    public boolean panStop(float x, float y, int pointer, int button) {
        Gdx.app.log("INFO", "panStop");
        zoom = camera.zoom;

        return true;
    }

    @Override
    public boolean pinch(Vector2 p1, Vector2 p2, Vector2 p3, Vector2 p4)
    {
        // TODO: Implement this method
        return false;
    }
    @Override
    public void pinchStop()
    {

    }


    @Override
    public void resize(int p1, int p2)
    {
        // TODO: Implement this method
    }

    @Override
    public void pause()
    {
        // TODO: Implement this method
    }

    @Override
    public void resume()
    {
        // TODO: Implement this method
    }

    @Override
    public void dispose()
    {
        sb.dispose();
        texture.dispose();
        font.dispose();
    }

    
}

Now after playing my project the spritebatch isn't on tile (3,4) , it's out of my map.

enter image description here

So what is the problem, how can I add sprite batch on my isometric map tiles? , I just want to add army on my tiles so I can move the army from tile to another so I want to use spritebatch, I found some solutions in stackoverflow but there is no one succed to solve my problem



Related Questions




Draw copy of the game within the game

Updated March 04, 2017 16:13 PM