Android Question Coordinates in libgdx.

Eme Fibonacci

Well-Known Member
Licensed User
Longtime User
All the coordinates in libgdx are relative to the "world"? Is there any way to keep objects (eg text) always in the fixed position even if the camera moves? Can anyone talk a bit about what is the resolution and coordinate system in libgdx?

Thank you
 

wonder

Expert
Licensed User
Longtime User
By default, the coordinates of Textures and TextureRegions are relative to the bottom-left corner of the screen (x:0, y:0).
You can create as many cameras as you want, so you can have, for example, an ActionCam object that moves and an OverlayCam that is kept in a fixed position.

B4X:
'@LibGDX Render Event

ActionCamera.Update
Batch.ProjectionMatrix = ActionCamera.Combined
Batch.Begin
    'Batch.Draw_GameMap
    'Batch.Draw_GameCharacters
    '...
Batch.End

'OverlayCamera.Update <-- not really necessary if the camera never moves
Batch.ProjectionMatrix = OverlayCamera.Combined
Batch.Begin
    'Batch.Draw_UserInterface
    'Batch.Draw_Text
    '...
Batch.End
 
Upvote 0

Eme Fibonacci

Well-Known Member
Licensed User
Longtime User
By default, the coordinates of Textures and TextureRegions are relative to the bottom-left corner of the screen (x:0, y:0).
You can create as many cameras as you want, so you can have, for example, an ActionCam object that moves and an OverlayCam that is kept in a fixed position.

B4X:
'@LibGDX Render Event

ActionCamera.Update
Batch.ProjectionMatrix = ActionCamera.Combined
Batch.Begin
    'Batch.Draw_GameMap
    'Batch.Draw_GameCharacters
    '...
Batch.End

'OverlayCamera.Update <-- not really necessary if the camera never moves
Batch.ProjectionMatrix = OverlayCamera.Combined
Batch.Begin
    'Batch.Draw_UserInterface
    'Batch.Draw_Text
    '...
Batch.End

Perfect! Thank you.

I already have a tilemap (highway) and a camera that does a vertical scroll.
How could I have a second tilemap (clouds) scrolling has different speed as a parallax effect?
 
Upvote 0

Eme Fibonacci

Well-Known Member
Licensed User
Longtime User
The question was incomplete. Would it be better, to create a parallax should I use the same principle or is there a better way?
 
Upvote 0

wonder

Expert
Licensed User
Longtime User
Upvote 0

sorex

Expert
Licensed User
Longtime User
well, that's the point. it's not really standard. :)

everything I used so far is going from top left to bottom right.
 
Upvote 0

wonder

Expert
Licensed User
Longtime User
that's kind of strange. can it be changed to top left ?
Yes.
B4X:
Camera.SetToOrtho2(True, lGdx.Graphics.Width, lGdx.Graphics.Height)

Of course, you will then have to flip your texture regions:
B4X:
myTextureRegion.Flip(False, True) '(x, y)
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
the camera part works fine.

but there's no flip method on the lgTexture type.
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
ok, I figured it out.

I had to use

B4X:
texSmiley.Initialize("smiley.png")
regSmiley.InitializeWithTexture(texSmiley)
regSmiley.Flip(False,True)

and use drawRegion instead of drawText

B4X:
Batch.DrawRegion(regSmiley,200,50)
 
Upvote 0
Top