Java-Gaming.org Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (804)
Games in Android Showcase (239)
games submitted by our members
Games in WIP (868)
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 coordinates to screen coordinates?  (Read 3979 times)
0 Members and 1 Guest are viewing this topic.
Offline ral0r2
« Posted 2019-08-07 11:36:34 »

Hey guys, I hope the title for this topic is correct..

Some days ago I started playing around with libgdx with the help of some tutorials. I'm not quite sure if I understand the concepts of PPM, viewports and the libgdx orthographic camera correctly.

My player body is always "spawning" at 0,0.


1  
mPlayer = ShapeFactory.createRectangle(new Vector2(0,0), new Vector2(16,16), BodyType.DynamicBody, mWorld, 100f)

Parameters are: position, size, bodytype, world, density, just for clarification Smiley


this is my camera / maploading / rendering setup:

1  
2  
3  
4  
5  
6  
7  
8  
mB2dr = new Box2DDebugRenderer();
mCamera = new OrthographicCamera(Gdx.graphics.getWidth() / 32(PPM), Gdx.graphics.getHeight() / 32(PPM));
mViewport = new FitViewport(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), mCamera);
map = new TmxMapLoader().load("maps/test.tmx");
tmr = new OrthogonalTiledMapRenderer(map);

TiledObjectUtil.parseTiledObjectLayer(mWorld, map.getLayers().get("collision-layer").getObjects());
TiledObjectUtil.createMapBoundaries(mWorld, map);


and this is my draw method:

1  
2  
3  
4  
5  
6  
7  
8  
    private void draw() {
        Gdx.gl.glClearColor(0, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        tmr.setView(mCamera);
        tmr.render();
        mBatch.setProjectionMatrix(mCamera.combined);
        mB2dr.render(mWorld, mCamera.combined);
    }


What I'm not understanding is the position of the tiledmap rendering? So my player is at 0,0 it looks like this if I render my tiledmap:



My assumption was that the rectangle should be at the bottom left corner of the tiledmap because the tiledmap is also rendered at 0,0? Or is this because the position is applied to the center of the rectangle? I could set the position of the rectangle to 16,16 and it would fit to the bottom left of the map. How ever I'm not certain wether this is the right way or whether there is just a misunderstanding in my mind. I think this is because my rectangle has a size of 16,16 but rendered in my game it fits a 32,32 big tile? Could anyone help me out here? I'm not really sure how I should use "meters" instead of pixels when developing my game.

I think my misunderstanding is related to the concept of PPM in box2d/libgdx since I'm not really sure at what operations I have to apply PPM. I read somewhere that I'm fine if I only apply this conversion in my camera method? Can anyone point me towards a helpful tutorial or give me some hints? Much appreciated!

I think this was quite good (https://xoppa.github.io/blog/pixels/) but I'm not sure if I understood everything correctly and where my inconsistency comes from.

Offline SugarBlood
« Reply #1 - Posted 2019-08-07 13:42:18 »

Hi, can you post the link for that tutorials, I don't understeand how the camera is set here, but it looks like, the black area is where negative coordinates are

PS

What gona happen if you do it like this?

mCamera = new OrthographicCamera;
mCamera.setToOrtho(false, viewWidth, viewHeight);

mViewport = new FitViewport(viewWidth, viewHeight, mCamera);

Where viewWidth and viewHeight, for example 1024 x 720
Offline FabulousFellini
« Reply #2 - Posted 2019-08-07 16:58:25 »

Im pretty sure in LibGDX the camera is by default positioned with the bottom left corner at 0, 0.  So you have to manipulate the viewport to make the camera at the actual 0,0 (where you want it).  If you wanna take a look, my game screen code is here: https://github.com/IanFell/GameTemplate/blob/master/MyGame-core/src/screens/GameScreen.java

This class extends my Screens class (so go back and look at that to set it up) but it's centered on the player.

And yeah, camera/viewport/game coordinates are confusing as shit.

-FabulousFellini
www.fabulousfellini.com
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ral0r2
« Reply #3 - Posted 2019-08-08 10:34:07 »

Thanks for the replies guys! I think I figured it out a little bit more. Your Github link was quite helpful!
Eventhough I'm quite sure that I did not understand the camera / viewport etc. stuff in full depth lol.
Offline SugarBlood
« Reply #4 - Posted 2019-08-16 09:27:39 »

I have something else to that, link shared by @VaTTeRGeR https://gamedev.stackexchange.com/questions/120722/screensize-vs-worldsize. It looks like same/similar problem, and there's some details and explanation(I forgot about it)). I'll leave it here for someone else, who will seek that.
Offline FabulousFellini
« Reply #5 - Posted 2019-12-05 21:54:06 »

To be honest, I didn't either.   But it works for now lol.

-FabulousFellini
www.fabulousfellini.com
Offline lihl

Junior Newbie


Medals: 1



« Reply #6 - Posted 2020-01-14 03:12:54 »

Hi,i think your OrthographicCamera have some problems when it initialize.
This is a Example for me:
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  
public class FollowCamera{
   private OrthographicCamera gameCam;
   private Viewport gamePort;

   public FollowCamera(){
      //create camera used to follow hero through camera world
      gameCam = new OrthographicCamera();
      //create a FitViewport to maintain virtual aspect ratio despite screen size
      gamePort = new FitViewport(Configs.viewportWidth / Configs.PPM, Configs.viewportHeight / Configs.PPM, gameCam);
   }

   public void update(Vector2 pos, float zoom){
      // change gameCam zoom
      gameCam.zoom = zoom;
      //attach our gameCam to our players coordinate
      gameCam.position.set(pos.x, pos.y, 0);
      //update our gameCam with correct coordinates after changes
      gameCam.update();
   }

   public OrthographicCamera getCamera(){
      return gameCam;
   }

}

1  
2  
3  
4  
   // Virtual Screen size and Box2D Scale(Pixels Per Meter)
   public static final int viewportWidth = 400;//Pixels
   public static final int viewportHeight = 200;//Pixels
   public static final float PPM = 100;

Configs.viewportWidth / Configs.PPM  --> Viewport width that the camera can see. Unit Meter.
Configs.viewportHeight / Configs.PPM --> Viewport height that the camera can see. Unit Meter.
Virtual Screen and Physical Screen, they're not a same concept.
Virtual Screen   -> Unit Meter.
Physical Screen -> Unit Pixels.
Element's position in the game must be use the 'Meter' such as Hero's position.
Only you are ready to render them, you can transform the 'Virtual Position in Meter' into 'Physical position in Pixels';
Pages: [1]
  ignore  |  Print  
 
 

 
Riven (579 views)
2019-09-04 15:33:17

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

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

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

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

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

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

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

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

nelsongames (5114 views)
2018-04-24 18:15:36
A NON-ideal modular configuration for Eclipse with JavaFX
by philfrei
2019-12-19 19:35:12

Java Gaming Resources
by philfrei
2019-05-14 16:15:13

Deployment and Packaging
by philfrei
2019-05-08 15:15:36

Deployment and Packaging
by philfrei
2019-05-08 15:13:34

Deployment and Packaging
by philfrei
2019-02-17 20:25:53

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
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!