Can I zoom just a part of my OrthograficCamera libgdx - java

imagine that, an imaginary minecraft, where you build cubes.
You have a panel where you have the diferents cubes(tool box),
and other big panel, the world where you build, but the world is bigger.
and all of this things are in the same screen, imagine how can i put a orthografic camera that only zoom the panel where you have the whole world, that it allows you to zoom and build cubes with details and presicion, but the tool box panel still the same position. always visible. ?

Use two cameras. One for the "game" and one for the "HUD".
During a render call, use one camera to render the game, then "switch" cameras and then render the HUD. How you switch cameras depends on what API you're using to render objects (for example use setProjectionMatrix on a SpriteBatch).
You could also create more than one SpriteBatch or more than one Stage (as each tracks camera state internally), but they're a bit heavy-weight.
Beware that when switching contexts, you will probably have to explicitly end/flush/complete the first context before starting the second.

You do not use one Camera to show the stuff. For example you use a Camera for the background of your game and you have a second camera for the game Hud that always has a fixed size. But the game in background can be zoomed in and out because its an different camera.
But for simple boxes on the screen you do not use a second camera. You just use one more Stage that get displayed or not where the new "ui" stuff is shown.
Hope this helps a bit.

Related

Understanding different coordinate systems, Tiled, Stage, Screen,

I am very confused about all the different coordinate systems.
I am using LibGDX with Tiled.
These all have their own coordinate system (sortof).
LibGDX screen
Tiled map
UIcamera
Orthogonal TiledMapCamera
UIStage
TiledMapStage
It's too many concepts and I can no longer mentally understand how they affect each other in complex scenarios, like
having different screen dimensions than the tiled map dimensions
when resizing the screen.
Can someone shed some light on this?
Many thanks!
In a 2D game, you really only have to think about the coordinate system of the orthographic camera. Whatever is drawn with a certain camera's combined matrix is fit to the rectangle of the screen (and if you set up the camera correctly, it will not be distorted).
LibGDX provides the Viewport classes for helping to set up your camera. You can think of them as camera managers that will size the camera to meet the arrangement you want. You instantiate them with a desired size "window" you want to see of the game world. And the only place you have to consider the actual screen dimensions is in the resize method, where you pass the dimensions to the Viewport class and let it handle sizing your camera for you so the scene won't be distorted.
You might have more than one camera. Typically your UI will have its own, and the gameplay world will have another (because you want it to move around in the world).
When it comes to input, the raw X and Y are given in screen pixel coordinates, but you just pass these coordinates to the camera.unproject method to have them converted to the same coordinates as your game world.
I don't use tiles, so I can't get specific there, but the same principles should apply.

Is there a proper way to scale actors on the Stage?(Libgdx)

I'm making an android app which has Libgdx Stage handling the HUD. But how can I scale things I add to Stage?
I've tried some manipulations with Camera, Viewport, and none of the work properly. Let's say I want my ImageButton placed in the right top corner of the screen. I managed to do that manipulating with table positioning, it seems to be fine. But the problem appear when I run my app on my phone. The ImageButton positioned right but it's kinda small :-)
Thing is I want ImageButton look like on (540,800) resolution screen.
ImageButton is (65,65).
When it comes to bigger screensizes ImageButton starts to look small because it does not scale(or the viewport and camera stuff dont work well, I dont think the actual scaling is truly necessary)
I suggest you to check the documentation about Viewports, which is pretty clear, although a bit dated : https://github.com/libgdx/libgdx/wiki/Viewports
If you don't need to mess with the camera, don't bother to declare one. Simply use a stage, with includes automatically an orthographic camera. If you declare an Actor within the bounds of the viewport, it should scale properly depending on the type of viewport you selected. For instance :
FillViewport vp = new FillViewport(800, 1280); // fills the screen, adapting itself to actual screen ratio
Stage stage = new Stage(vp);

How can I support multiple screen sizes in moblie game deveopment

For this question, I am specifically referring to android.
I want to learn how to make a game and ensure that it will work on devices with different screen sizes. I test the apps on my phone, which has a huge screen. But if a user downloads it and has a smaller screen size, how will it affect the game? I'm using libGDX.
Usually you set the dimension of a "virtual screen" that you will consider the real screen inside the game will be rendered.
This way you can easily project the dimensions of your actors ecc.
Then you use a ViewPort that scale your "virtual screen" to fill the real screen (that has its own real dimension in pixels).
Obviously there's a problem about the aspect ration.
Typically it's better to choose the dimensions of the "virtual screen" properly; for example if you choose a virtual ratio of 800x480 the game won't be look stretched on devices that has the same aspect ratio of the screen.
This in not the only solution...maybe it's better if you take a look here [https://github.com/libgdx/libgdx/wiki/Scene2d][1].
Luca

Libgdx: Infinity world. How to?

I write simple game with libGdx. I have a hero, which always is in screen center and I must move my background sprite (or region?) to make move illusion. But my background sprite isn't infinity.
How can I create illusion of seamless infinity world?
Of course I can add several background sprites to try to cover all empty space of screen. But I must to draw out of the sceen a lot of all another objects: Houses, monsters, others heroes, etc. So I have a second question:
When I try to draw other object (a lot of objects!) out of the screen, how badly it affects memory? How to draw it correctly?
I know that OrthographicCamera in libgdx draw only viewportWidth-viewportHeight area. If it's right, then I must to move my camera and all my sprites too. I think it's not correctly.
How can I render infinity world in libgdx with OrthographicCamera?
How can I create illusion of seamless infinity world?
Create a tile background. Tile background means that if it was besides or top or bottom of itself, the edges of sticking line will not be visible to viewer.
To do this open your background image in photoshop and go to Filters > Other > Offset.
Set the offset filter to offset the background to center then try using photoshop tools to hide the edges (the + shape in image). Now again go to offset and return to 0, 0 and save your background.
When I try to draw other object (a lot of objects!) out of the screen,
how badly it affects memory? How to draw it correctly?
I have checked this and that was not much fps loosing on my test. So don't worry about it.
How can I render infinity world in libgdx with OrthographicCamera?
Move camera where-ever you want any x, y. Every time see where is camera and calculate needing tile backgrounds to draw (for example every time draw 3x3=9 backgrounds sticking together).

Android UI Design Suggestion

This is the scenario:
I have one image background set on an activity. On this background, a rectangle has been drawn (in other words, one image depicting a rectangle). I need to display text WITHIN this rectangle.
Right now, I have one solution in mind: since I'm going to optimize the UI for most screens (incl. tablets), I'm going to customize the main .xml layout for each screen size (multiple .xml layouts). Thus, I can manually set the place where the text area goes within the rectangle (+ its size).
I am most certain that this solution is NOT good. I'd like to hear some suggestions from more advanced developers. What would an elegant way of placing text over a background image showing a rectangle, so that the text stays within the rectangle's borders be?
Because I need to set particular positions for other UI elements (centered buttons (vertically/horizontally), I am currently using a Relative Layout.
By default if you have used dp as dimensional measure, your app should in theory work fine for all resoultions. Android by default scales the screen to fit different screens. AndroidDeveloper You just have to make sure that you have different images for resources (Rectangle image). There is another post in SO which you might be intrested to look into link
Cheers
RIchie

Categories