Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (536)
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] Questions About Cameras  (Read 806 times)
0 Members and 1 Guest are viewing this topic.
Offline tyeeeee1
« Posted 2013-07-02 19:49:16 »

Hey,

I've finally managed to get a tiled map loaded using libgdx and after a little bit of guess-work I figured out how to move around the map using keys. I'm not sure what to look up to answer these questions so I'll ask them here.

Now for the questions...

Why is it that the code below zooms in on the map when I remove the "(w/h) *" from the X value and then zooms out when I put it back?
1  
2  
3  
4  
5  
6  
float w = Gdx.graphics.getWidth();
      float h = Gdx.graphics.getHeight();

      camera = new OrthographicCamera();
      camera.setToOrtho(false, (w/h) * cameraX, cameraY);
      camera.update();


When I stretch (re-size) the window the map becomes zoomed in instead of just showing more of the map. How would I fix this? I'll post the code I'm using at the bottom.

When moving around with the arrow keys/WASD the view moves at an extremely fast speed. The only way to fix this, that I can think of, was to change the values of cameraX and cameraY to doubles and instead of increasing/decreasing by 1 when the key is pressed, I would increase/decrease by a decimal value. This, probably, would have worked but the method "setToOrtho" doesn't allow double values. Is there another way to adjust the speed that you move around the map?

Here is my hacked-together code:
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  
package screens;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.graphics.Camera;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.maps.tiled.TiledMap;
import com.badlogic.gdx.maps.tiled.TiledMapRenderer;
import com.badlogic.gdx.maps.tiled.TmxMapLoader;
import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer;

public class Screen_Game2 implements ApplicationListener {

   private TiledMap map;
   private TiledMapRenderer renderer;
   private OrthographicCamera camera;
   private int cameraX = 10, cameraY = 10;
   private BitmapFont font;
   private SpriteBatch batch;

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

      camera = new OrthographicCamera();
      camera.setToOrtho(false, (w/h) * cameraX, cameraY);
      camera.update();

      font = new BitmapFont();
      batch = new SpriteBatch();

      map = new TmxMapLoader().load("assets/Maps/Map_01.tmx");
      renderer = new OrthogonalTiledMapRenderer(map, 1f / 32f);
   }

   @Override
   public void render()
   {
      Gdx.gl.glClearColor(0.55f, 0.55f, 0.55f, 1f);
      Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
     
      float w = Gdx.graphics.getWidth();
      float h = Gdx.graphics.getHeight();
      if(Gdx.input.isKeyPressed(Keys.LEFT) || Gdx.input.isKeyPressed(Keys.A))
      {
         cameraX -= 1;
         camera.position.x = (w/h) * cameraX;
      }
      if(Gdx.input.isKeyPressed(Keys.RIGHT) || Gdx.input.isKeyPressed(Keys.D))
      {
         cameraX += 1;
         camera.position.x = (w/h) * cameraX;
      }
      if(Gdx.input.isKeyPressed(Keys.UP) || Gdx.input.isKeyPressed(Keys.W))
      {
         cameraY += 1;
         camera.position.y = cameraY;
      }
      if(Gdx.input.isKeyPressed(Keys.DOWN) || Gdx.input.isKeyPressed(Keys.S))
      {
         cameraY -= 1;
         camera.position.y = cameraY;
      }
     
      camera.update();
      renderer.setView(camera);
      renderer.render();
      batch.begin();
      font.draw(batch, "FPS: " + Gdx.graphics.getFramesPerSecond(), 10, 20);
      batch.end();
   }

   @Override
   public void dispose () {
      map.dispose();
   }

   @Override
   public void pause() {
      // TODO Auto-generated method stub
     
   }

   @Override
   public void resize(int arg0, int arg1) {
      // TODO Auto-generated method stub
     
   }

   @Override
   public void resume() {
      // TODO Auto-generated method stub
     
   }
}


Thanks in advance for any answers!
Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 128
Projects: 4
Exp: 3 years



« Reply #1 - Posted 2013-07-04 05:11:36 »

The map will obviously zoom in if (w/h) results in a number less than 1 (ie, if w<h) because the projection width (x) is decreased. Removing it logically zooms it out.
Reading the rest of your post, constantly setting the camera to different ortho values isn't the correct way to do it. You should be adjusting the position vector of the camera object.
E.g.
1  
2  
3  
if(Gdx.input.isKeyPressed(Keys.W){
camera.position.y++;
}

No need to keep a separate cameraY variable. Also, try adding
1  
camera.update();

to your render loop.
Offline tyeeeee1
« Reply #2 - Posted 2013-07-04 16:00:33 »

Thanks, the map now looks properly instead of stretched. It still zooms in when you re-size the window but as long as it's scaling properly, which it is now, it should be fine. I also just noticed that there is a camera.position.z so I can probably set the map to be zoom-able later on.

The only thing that I still need to figure out is how to make the view move slowly instead of fast. I'm currently able to scroll over the whole test map within 5 seconds when it should take 10-20 seconds.

While typing this I figured out that camera.position. can be set to double values; this solved the issue with not being able to scroll slowly.

Thanks again!
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Riven (19 views)
2014-07-29 18:09:19

Riven (13 views)
2014-07-29 18:08:52

Dwinin (12 views)
2014-07-29 10:59:34

E.R. Fleming (31 views)
2014-07-29 03:07:13

E.R. Fleming (12 views)
2014-07-29 03:06:25

pw (42 views)
2014-07-24 01:59:36

Riven (42 views)
2014-07-23 21:16:32

Riven (28 views)
2014-07-23 21:07:15

Riven (29 views)
2014-07-23 20:56:16

ctomni231 (60 views)
2014-07-18 06:55:21
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!