LibGDX reset or remove Orthographic camera - java

I'm quite new to LibGDX but not a beginner in java programming. I was making a Flappy bird like game. I used Orthographic camera in my play state so it would follow the bird, but now i want to make a game over screen. I created a class with all the code i need but when i try it still uses the Orthographic camera at the birds position before it died, so the textures on that game over screen are heavily out of place, although isTouched() method is at the right position, so if i press that region i set earlier it restarts the game as it should. The only problem are the textures. Could someone help me solve the problem as i can't figure it out.

I think you want something similar to HUD, which is always displayed on the screen, right?
If so, you can use another orthographic camera, name it hudCam or something you remember.
Not sure if necessary but I changed Y-down of the camera to false like this:
hudCamera.setToOrtho(false, WIDTH / 2, HEIGHT / 2); // width and height being the screen size
Then you can just switch between the cameras when drawing.
batch.setProjectionMatrix(camera.combined);
draw...
batch.setProjectionMatrix(hudCamera.combined);
draw HUD stuff...
To get the correct mouse coordinates, just do like you did with the other camera, unproject it.

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.

Box2d lights - lights over sprite

I wanted to ask if you can use box2d lights so you can see only objects that are in the lights area. For example i have a flashlight and only want to see game objects in the light. I managed to do something like this but the problem is that the sprites of the game objects lose their color intensity because I render lights on top of sprites and the game itself doesn't look good because of this (even though it is the effect that i want).I used box2d bodies with user data containing sprites. I can't figure any way out. Is there any proper way to use box2d lights library to make these objects visible and with their real color? (I am setting lights to X-rays to do this; also I am using it with libgdx in java).
It may be because the default setting is to not use diffuseLight. You have to set rayHandler.useDiffuseLight(true).
Libgdx and Box2DLights - too bright + colors grayed out

box2dlights set scale from box2d

I'm making a game in libGDX and I decided to use box2dlights to render the lights. I did not used cameras so much up to this point, because I already had most of the code done in pure LWJGL. There are two main operations that I need to do with the coordinates of everything.
The first is to translate the screen to the position of the map (the map is bigger than the screen, and the position of the player defines what portion of the map is visible). So for example, if the player is at (50, 30), I translate everything by (-50, -30), so that the player is in the middle.
The second thing is to multiply everything by a constant, that is the conversion from box2d meters to pixels on screen.
However, since I do not have access to box2dlights rendering, I need to pass these two information to the ray handler, and the only way to do that is via Camera. So I created an Orthographic Camera and translate it in deltaS every tick before drawing, instead of manually subtracting deltaS from every coordinate. That part works perfectly. On the other hand, the zoom thingy does not seem to work, because it zooms in and out based in the middle of the screen. For example, if I set zoom = 2, the screen is reduced twice, but it is centered on the screen. The coordinate (0,0) is not (0,0), as I would expect, but instead is screen.width/4.
Is there any way to set the camera so that it multiplies every coordinate by a number, you would assume zoom function should do OR is there any way to do it directly on box2dlights?
I don't know if my problem is very clear or common, but I can't find anything anywhere.
I finally figured it out! The problem was that I needed to set the zoom before I used
camera.setToOrtho(true, SCREEN_WIDTH, SCREEN_HEIGHT);
Because that method uses the current zoom to set its properties. Hope this helps!

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).

how to have object continuously follow users finger coordinates? not draging object. object following finger

I have tried to use different methods but to no avail. So I come looking for a fresh point of view.
I want to have a sprite follow the touch coordinates of the user. So if they dragged their finger across the screen the sprite would move or tween towards the touch coordinates but continuously update to 'follow' the users finger. If the user stopped moving their finger, the sprite would eventually come to a stop at the final touch coordinates.
I can get the sprite to move and tween to a single set of coordinates but not continuously update to follow along. This is for an android game. its in java. this is one of the basic functions i need to use so im not too far along. that being said I can accept direction for open gl or for canvas.
Any help or direction would be much appreciated. thank you!
On every frame, do
Pos = (Pos * 0.95) + (LastTouch * 0.05);
Adjust the ratios until it feels right. Just make sure they add up to 1.

Categories