Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (777)
Games in Android Showcase (231)
games submitted by our members
Games in WIP (856)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  [LibGDX] TiledMap, Textures and Zoom  (Read 542 times)
0 Members and 1 Guest are viewing this topic.
Offline fenaryh
« Posted 2018-04-15 18:30:35 »

Hi guys!

I'm creating a builder style game and using TiledMap and LIBGDX. So, I'm just testing the camera and translation and having some issues with zoom. All works perfect without changing the zoom. But when I change it, the Texture/Sprite becomes misaligned with the Tiled. The actors continue just fine, but the textures/sprites not. I don't know how to correct the scale in the update.

So, how could I correct the zoom for the TextureRegion (and Sprites)? My code is above:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
public class TiledTest extends ApplicationAdapter {
   
    private static final String TAG = TiledTest.class.getName();
   
    Texture img;
    TiledMap tiledMap;
    OrthographicCamera camera;    
    TiledMapRenderer tiledMapRenderer;
    SpriteBatch batch;
    Sprite sprite;
    Actor actor;
    Stage stage;
    boolean init = false;
   
    @Override
    public void create () {
        float w = Gdx.graphics.getWidth();
        float h = Gdx.graphics.getHeight();

        batch = new SpriteBatch();
        camera = new OrthographicCamera();

       
        camera.setToOrtho(false,w,h);
        camera.update();
       
       
        InternalFileHandleResolver handle = new InternalFileHandleResolver();
        handle.resolve("mapa_teste.tmx");
        TmxMapLoader loader = new TmxMapLoader(handle);        
        tiledMap = loader.load("img/mapas/mapa_teste.tmx");
       
        tiledMapRenderer = new OrthogonalTiledMapRenderer(tiledMap);        
       
        sprite = new Sprite(new Texture(Gdx.files.internal("img/tanque_192x192.png")));
        sprite.setBounds(128, 128, sprite.getTexture().getWidth(), sprite.getTexture().getHeight());
        stage = new Stage();
        actor = new Actor();
        actor.setBounds(128,128, 192, 192);
        actor.addListener(new ClickListener(){
            @Override
            public void clicked(InputEvent event, float x, float y) {
                super.clicked(event, x, y);
                Gdx.app.log(TAG, "Tanque clicado!");
            }

            @Override
            public void exit(InputEvent event, float x, float y, int pointer, Actor toActor) {
                super.exit(event, x, y, pointer, toActor);
                if(pointer == -1)
                    Gdx.app.log(TAG, "Mouse saindo do actor!");                
            }

            @Override
            public void enter(InputEvent event, float x, float y, int pointer, Actor fromActor) {
                super.enter(event, x, y, pointer, fromActor);
                if(pointer == -1)
                    Gdx.app.log(TAG, "Mouse entrando no actor!");                
            }
                     
        });
        stage.addActor(actor);                
        Gdx.input.setInputProcessor(stage);
        stage.getViewport().setCamera(camera);
        stage.setDebugAll(true);
       
       
    }

    @Override
    public void render () {

        input();
       
        Gdx.gl.glClearColor(0, 0.5f, 0.5f, 1);
        Gdx.gl.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        camera.update();
        tiledMapRenderer.setView(camera);
        tiledMapRenderer.render();
       
        batch.begin();
        sprite.draw(batch);
        batch.end();
       
        stage.act();
        stage.draw();        

    }

   
    public void input(){
       
        float cameraSpeed = 7f;
       
        if(Gdx.input.isKeyPressed(Input.Keys.S)){
            camera.translate(0f, +cameraSpeed);
            sprite.translate(0f, -cameraSpeed);
        }
        if(Gdx.input.isKeyPressed(Input.Keys.W)){
            camera.translate(0f, -cameraSpeed);            
            sprite.translate(0f, +cameraSpeed);            
        }
        if(Gdx.input.isKeyPressed(Input.Keys.A)){
            camera.translate(-cameraSpeed, 0f);            
            sprite.translate(+cameraSpeed, 0f);            
        }
        if(Gdx.input.isKeyPressed(Input.Keys.D)){
            camera.translate(+cameraSpeed, 0f);            
            sprite.translate(-cameraSpeed, 0f);            
        }
        if(Gdx.input.isKeyPressed(Input.Keys.F2)){
            camera.zoom += 0.01f;
            //sprite.scale(camera.zoom);
        }if(Gdx.input.isKeyPressed(Input.Keys.F3)){
            camera.zoom -= 0.01f;            
            //sprite.scale(camera.zoom);
        }
        if(Gdx.input.isKeyJustPressed(Input.Keys.ESCAPE)){
            stage.dispose();
            batch.dispose();
            tiledMap.dispose();
            sprite.getTexture().dispose();
            Gdx.app.exit();
        }
       
    }
   
}
Offline fenaryh
« Reply #1 - Posted 2018-04-16 19:52:44 »

So, I finally figure out a solution. First, not using a Texture. Now I'm using a TextureMapObject. That way, is a must to create a MapObjectLayer also in Tiled.

Include the batch.setProjectionMatrix(camera.combined) before batch.begin().

And also draw manually using batch.draw(). It follows the final code for anyone struggling:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
public class TiledTest extends ApplicationAdapter {
   
    private static final String TAG = TiledTest.class.getName();
   
    TextureMapObject tmo;
    TiledMap tiledMap;    
    OrthographicCamera camera;    
    TiledMapRenderer tiledMapRenderer;
    SpriteBatch batch;    
    Texture texture;
    Actor actor;
    Stage stage;

   
   
    @Override
    public void create () {
        float w = Gdx.graphics.getWidth();
        float h = Gdx.graphics.getHeight();
       
        batch = new SpriteBatch();
        camera = new OrthographicCamera();
       
        camera.setToOrtho(false,w,h);
        camera.update();        
       
       
        InternalFileHandleResolver handle = new InternalFileHandleResolver();
        handle.resolve("mapa_teste.tmx");
        TmxMapLoader loader = new TmxMapLoader(handle);        
        tiledMap = loader.load("img/mapas/mapa_teste.tmx");
       
        tiledMapRenderer = new OrthogonalTiledMapRenderer(tiledMap);
       
        //TextureMapObject
        texture = new Texture(Gdx.files.internal("img/tanque_192x192.png"));
        tmo = new TextureMapObject(new TextureRegion(texture,192,192));
        tmo.setX(128);
        tmo.setY(128);
        tiledMap.getLayers().get("Objects").getObjects().add(tmo);
        stage = new Stage();
        actor = new Actor();
        actor.setBounds(128,128, tmo.getTextureRegion().getRegionWidth(), tmo.getTextureRegion().getRegionHeight());
        actor.addListener(new ClickListener(){
            @Override
            public void clicked(InputEvent event, float x, float y) {
                super.clicked(event, x, y);
                Gdx.app.log(TAG, "Tanque clicado!");
            }

            @Override
            public void exit(InputEvent event, float x, float y, int pointer, Actor toActor) {
                super.exit(event, x, y, pointer, toActor);
                if(pointer == -1)
                    Gdx.app.log(TAG, "Mouse saindo do actor!");                
            }

            @Override
            public void enter(InputEvent event, float x, float y, int pointer, Actor fromActor) {
                super.enter(event, x, y, pointer, fromActor);
                if(pointer == -1)
                    Gdx.app.log(TAG, "Mouse entrando no actor!");                
            }
                     
        });
        stage.addActor(actor);                
        Gdx.input.setInputProcessor(stage);
        stage.getViewport().setCamera(camera);
        stage.setDebugAll(true);        

       
    }

    @Override
    public void render () {

        input();
       
        Gdx.gl.glClearColor(0, 0.5f, 0.5f, 1);
        Gdx.gl.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        camera.update();
        tiledMapRenderer.setView(camera);
        tiledMapRenderer.render();
       
        batch.setProjectionMatrix(camera.combined);
        batch.begin();        
       
        batch.draw(tmo.getTextureRegion(), tmo.getX(), tmo.getY(),
                tmo.getOriginX(), tmo.getOriginY(), tmo.getTextureRegion().getRegionWidth(), tmo.getTextureRegion().getRegionHeight(),
                tmo.getScaleX(), tmo.getScaleY(), tmo.getRotation());      
       
        batch.end();
       
       
        stage.act();
        stage.draw();        


    }

   
    public void input(){
       
        float cameraSpeed = 7f;
       
        if(Gdx.input.isKeyPressed(Input.Keys.S)){
            camera.translate(0f, +cameraSpeed);            
        }
        if(Gdx.input.isKeyPressed(Input.Keys.W)){
            camera.translate(0f, -cameraSpeed);                        
        }
        if(Gdx.input.isKeyPressed(Input.Keys.A)){
            camera.translate(-cameraSpeed, 0f);                        
        }
        if(Gdx.input.isKeyPressed(Input.Keys.D)){
            camera.translate(+cameraSpeed, 0f);                        
        }
        if(Gdx.input.isKeyPressed(Input.Keys.F2)){
            camera.zoom += 0.01f;
        }if(Gdx.input.isKeyPressed(Input.Keys.F3)){
            camera.zoom -= 0.01f;
        }
        if(Gdx.input.isKeyJustPressed(Input.Keys.ESCAPE)){
            stage.dispose();
            batch.dispose();
            tiledMap.dispose();            
            Gdx.app.exit();
        }
       
    }
   
}
Pages: [1]
  ignore  |  Print  
 
 

 
hadezbladez (330 views)
2018-11-16 13:46:03

hadezbladez (169 views)
2018-11-16 13:41:33

hadezbladez (328 views)
2018-11-16 13:35:35

hadezbladez (78 views)
2018-11-16 13:32:03

EgonOlsen (2168 views)
2018-06-10 19:43:48

EgonOlsen (2195 views)
2018-06-10 19:43:44

EgonOlsen (1372 views)
2018-06-10 19:43:20

DesertCoockie (1998 views)
2018-05-13 18:23:11

nelsongames (1641 views)
2018-04-24 18:15:36

nelsongames (2291 views)
2018-04-24 18:14:32
Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45

Deployment and Packaging
by philfrei
2018-08-20 02:33:38

Deployment and Packaging
by philfrei
2018-08-20 02:29:55

Deployment and Packaging
by philfrei
2018-08-19 23:56:20

Deployment and Packaging
by philfrei
2018-08-19 23:54:46
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!