I am currently working on a LibGDX mobile game app. The problem I am having is that I want the world width and height to be 480x480 and the screen width and height to be 640x480. The extra 240 units on the screen would be used for rendering UI, kind of like in ROTMG or Clash Royale(The cards view while in game).
I have thought about rendering the UI over the game with another camera, but it is not the design style I am going for. I want the game's logic and coordinates to be within the world's width and height and not take in the extra space for UI.
How would I do this?
P.S. the game is 2d
P.P.S. I would like for the game to keep this config on resize.
Denfeet, create a fullscreen Stage with 2 Group instances - one of them for UI, the other for the game. Groups: Game's bounds are 0, 0, 480, 480. UI's bounds are 480, 0, 240, 480. Now if you want to work in coordinates relative to the game Group for example, call Group.localToStageCoordinates(Vector2 localCoords) on the game Group (or make yourself a helper method so you don't have to keep writing this long name). Also the UI and game logic can stay completely separated this way - magic :-).
I always like to separate my game from UI. You can just create a camera full screen and put your UI over it with a Scene2D stage. Unless you want the exact dimensions you don't have to worry to much about positioning since you just overlay your UI elements. If you want to use a stage for your game as well then just create a 2nd stage, one for the game and one for the UI.
Anyway, looks like you are thinking in pixels. Your game does not need to know about pixels, just how much to render of your game world. If I would have a tile that is 10 x 10 units large and I want to show a square of 10x10 tiles to the player I would create a new FitViewport(100, 100) for it. The Viewport calculates world units back to pixels on the device it is running. This makes things more logical for you or anyone who works on your program. My player is not 256 pixels high because that is how hight the image is, no my player is 1,80m high and uses a image to display. If he wants to jump like a ninja he should jump over another player say 2m and he falls at 9.8m/s.
Related
I have a Texture sheet that was created with a texture packer app, it removes all of the unnecessary transparent pixels to save space in the final texture sheet. The problem is my animation is a guy with a sword, so when he attacks he swings the sword over his head, meaning it changes the width and height of the sprite. Example...
characterAttack1 = new TextureRegion(texture, 863, 979, 152, 149);
//more animation frames...
characterAttack8 = new TextureRegion(texture, 1, 2, 340, 256);
As you can see the first frame is 152x149, but by the 8th frame it is now 340x256.
So when the animation runs if I use the normal getWidth() getHeight() methods within my Character class the character will shrink as he attacks, since the height and width of the attack is larger as the sword swings. On the other hand if I use the method...
((TextureRegion)AssetLoader.characterAttack.getKeyFrame(runTime)).getRegionWidth();
and
((TextureRegion)AssetLoader.characterAttack.getKeyFrame(runTime)).getRegionHeight();
The character will bounce around the screen when the attack animation is run. I know I can't be the only person that has encountered something like this, but I can't seem to find any information on this. Is my only course of action to repack the texture file and make each one the same width and height even though there will be a ton of transparent pixels in the final packed texture?
If you need to see the AssetLoader and the batcher.draw methods or anything else I am more than happy to provide code samples, I am just trying to keep the post from being too large. Thank you.
Unless you are tying to make your game run on something with extremely low RAM, there is really no need to pack the textures so tightly. Sprite sheets are really quite small when you think about the grand scale of things. I imagine your game would require around 100MB of RAM when running, given that most computers now have upwards of 4GB of RAM, what's a few more MB?
A sprite-sheet for your character should be the maximum size of any one of the frames. e.g. If the largest frame is 340x256, every frame needs to be that size.
Now the character will be in the same position every frame because the render position will not change.
If the centre of the character is always in the middle of the sprite, you could render from the centre of the image, which would fix your problem.
i'm learning libgdx in purpose of building a realtime multiplayer painting game. Game will last in 120 second and 4 people with 4 different colors will race to paint more portion of the screen.
Now i'm trying to implement circular painting as player moves. As i made some researches, i saw that i can use pixmaps to implement such behaviour. As game updates, i will fill a circle at players x and y with radius r and draw a texture that scales the screen from pixmap( which is also game map). Is there a better way to achieve this?
And i must make it for all resolutions. 1024X1024 resolution versus 240x240 must be able to play. Can i do this by using lowest resolution pixmap and drawing a scaled texture from it? Thank you...
Thats the game im trying to do..
https://www.youtube.com/watch?v=EU8Eyh0tZOo&t=270s
You need to use Framebuffer for this kind of game. Pixmap is extremely slow for using every frame.
https://github.com/mattdesl/lwjgl-basics/wiki/framebufferobjects
And yes you can make all resolutions play together like this.
A friend and myself are new to game development, and we had a discussion regarding World Coordinates and Screen Coordinates.
We are following a wonderful online tutorial series for libGDX and they are using a 100 PPM (pixels per meter) scaling factor. If you re-size the screen, the scaling of objects no longer works. My friend is convinced that it is not a problem, and he may be right. But, I'm under the impression that when developing a game, the developers should typically only work with the pre-defined world coordinate system and let the camera transform it to the chosen screen coordinates. I do understand the need for reverse transformations when using mouseclicks, etc. But, the placing and scaling of objects in the world space is my concern.
I would like to reach out to this community for some professional feedback.
Thats one of the bigest problem of almost all Libgdx tutorials. They are great, but the pixel to meter/units conversation is just wrong.
Libgdx offers a great solution for that with Camera and an even better solution with the new Viewport classes (which under the hood work with Camera).
Its is really simple and will solve the problem of different screen sizes/aspect rations.
Just choose a Virtual_Width and Virtual_Height (think about it in meters or similar units).
For exampl, you have humans fighting each other in a 2D platformer game. LEts say our humans are 2m tall, so think about, how much screenspace should one human use? If we say, a human should take 1/10 of the screen space, our virtual height is 10*2=20. Now think about the primary aspect ration you are targeting. Lets say it is 16/9, so you have a virtual width of about 35.
Next, you need to think about what kind of Viewport you want. You sure want to use a Viewport, which supports Virtual_Width and Virtual_Heigth.
You may want a Viewport, which keeps the aspect ratio and fills the rest of the screen (if the screen has different aspect ratio) with black bars (FitViewport) or you may want the Viewport to fill the whole screen by stretching the units (StretchViewport).
Now just create the Viewport with your virtual width and heigth and update it in the resize() method with the given width and height.
Hope it helps.
It's be better name as Units per meter
And when you resize your screen you just set a new projective matrix, so everything works fine )
What you should worry about it's a aspect ratio.
Everything rest is doesn't matter.
So answering your question - Stay with world coordinates.
It also make simple add physics, light calculations, any dimensions ( 1.8 units instead 243 pixels )
I have created a game that is 800*600. How can i make it so that it will fit all computer screens? How would this normally be done? Other sites have said too set everything to a certain ratio depending what the screen resolution is. But if each image in the game changes size in comparison to the screen resolution it would mean that the images would not be in the right places and cause other problems. Is it possible to just "Stretch out" my 800*600 game so it fits any size? Thanks
You need to make the game resolution-independent. That means you create your own concept of 'game units' (which might be exactly the same as pixels relative to an 800x600 screen if you want), and then operate exclusively on those game units.
Whenever you draw something to screen you convert co-ordinates and sizes from game units into pixels. You will need to have functions that can convert both ways, because you may also want to translate click-events from screen space to game space.
When you start your game, you need to decide on the screen resolution and aspect ratio. If the aspect ratio is not the same (eg widescreen), you might choose to either stretch the game area or letterbox it. By moving all the translation code to a lower level and operating only within your game units, you save yourself a lot of pain in the long run.
Yes, you can stretch it out. Make the Games' main components on a JPanel that is 800 by 600, and add that JPanel to your JFrame. Then the player can resize your JFrame and the components won't go offscreen, they will change shape and size based on the new size of the JFrame. Remember that the mouse's position in relation to the JFrame will change a bit, but divide the mouses position by the JFrames height/width and multiply it by the JPanel height/width to get the position in relation to the JPanel. With this method you can let the user maximize your game and still have components look normal.
Trying to port a java2d demo app to a dekstop libgdx implementation.
This app plots data that is in inches. The plot is like a map in that it is an overhead view of a terrain. I use OrthographicCamera in libgdx and I am able to move left/right/up/down/zoom, all of that works great. I'm struggling with window resizes.
What I want to happen is that a window resize just allows the user to view more of the map at once. What I'm seeing is that the entire scene scales to fit the new window-size.
For example: If I have an object that is 50inches x 50inches in world coordinates AND at the current camera zoom level is 50pixels x 50 pixels on the screen, when the user then resizes the window, I want the object to stay 50pixes x 50pixels and more of the world to be revealed.
How is this accomplished in straight opengl terms?
How do I configure libgdx's camera to facilitate this?
What is right term for the kind of scene I am trying to setup (my google foo has failed me here)?
In plain OpenGL you would do something like:
glOrtho( 0, windowWidth, 0, windowHeight, 0, 1 )
on each window resize or redraw. This makes sure that one OpenGL unit corresponds to one pixel and window resize's don't scale the scene.