Java-Gaming.org Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (799)
Games in Android Showcase (235)
games submitted by our members
Games in WIP (865)
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 1581 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.
Pages: [1]
  ignore  |  Print  
 
 

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

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

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

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

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

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

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

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

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

nelsongames (4157 views)
2018-04-24 18:15:36
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

Deployment and Packaging
by gouessej
2018-08-22 08:03:45
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!