Libgdx OrthographicCamera zoom hidding part of Shaperenderer - android

I'm trying to make an isometric map on Android using libgdx. I'm basicaly drawing shapes with a ShapeRenderer and handling gestures by moving/zooming the camera. Here is my code.
public class MyGdxGame extends ApplicationAdapter{
private OrthographicCamera cam;
private ShapeRenderer mShapeRenderer;
private InputHandler mInputHandler;
#Override
public void create() {
mShapeRenderer = new ShapeRenderer();
mInputHandler = new AndroidInputHandler();
Gdx.input.setInputProcessor(mInputHandler);
Gdx.graphics.setContinuousRendering(false);
}
#Override
public void render() {
System.out.println("render()");
mInputHandler.handleInput(cam);
cam.update();
// clearing scene
Gdx.graphics.getGL20().glClearColor(1, 1, 1, 1);
Gdx.graphics.getGL20().glClear( GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT );
//drawing updated scene
mShapeRenderer.setProjectionMatrix(cam.combined);
mShapeRenderer.begin(ShapeRenderer.ShapeType.Filled);
mShapeRenderer.setColor( 0.95f, 0.95f, 0.95f,1);
mShapeRenderer.rect(0, 0, 5.1f, 4.8f);
mShapeRenderer.rect( 6.890f,24.014f,2.201f, 6f);
mShapeRenderer.end();
}
#Override
public void resize(int width, int height) {
cam = new OrthographicCamera( 10f,10f * height / width);
cam.position.set(5, 5, 7);
cam.lookAt(0, 0, 0);
Vector3 up =cam.position.cpy();
up.nor();
up.crs((new Vector3(-1, 1, 0)).nor());
cam.up.set(up);
cam.zoom=1f;
cam.far=100000;
cam.near=0;
}
...
}
InputHandler is a custom class that handle gestures like pinch to zoom and camera translation. It only calls camera.translate(Vector3) and camera.zoom = new zoom.
My problem is that whenever i pinch to zoom, some of my shape are cut.
expected drawing
cutted shapes
I don't realy know where this comes from. I think there is something happening with my viewport. I tried modifying the base zoom and camera viewport sizes but I dont realy understand the concept of viewport width and height.
Any help would be apreciated.
Thanks

Related

Libgdx Table Confusion

I'm trying to display in a vertical and centered way an Image, and a button for my Screen Menu.
I've almost accomplished that only using actors but I decided that maybe a layout would be the best practice here.
So I've done:
public MenuScreen(final MyGame myGame) {
this.myGame = myGame;
font = new BitmapFont();
camera = new OrthographicCamera();
camera.setToOrtho(false, 480, 800);
stage = new Stage();
batch = new SpriteBatch();
stage.getViewport() .setCamera(camera);
skin = new Skin(Gdx.files.internal("menu/glassy-ui.json"));
button = new TextButton("NEW GAME", skin);
button.setTransform(true);
button.setScale(0.5f);
button.addListener(new ClickListener(){
#Override
public void clicked(InputEvent event, float x, float y) {
myGame.setScreen(new GameScreen( myGame));
}
});
Image imageLogo = new Image( new Texture(Gdx.files.internal("titleLogo.png")));
// imageLogo.setScaling(Scaling.fit);
Table myTable = new Table();
myTable.setFillParent(true);
myTable .add(imageLogo);// .width(imageLogo.getWidth()).height(imageLogo.getHeight()).row() ;
myTable.row() ;
myTable.add(button);
stage.addActor(myTable);
Gdx.input.setInputProcessor(stage);
batch.setProjectionMatrix(camera.combined);
}
#Override
public void render(float delta) {
Gdx.gl.glClearColor(1, 1, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
camera.update();
batch.begin();
stage.act(delta);
stage.draw();
batch.end();
}
But it seems that the viewport of the stage isn't affecting the table area which goes offscreen and I'm not even sure that the image is displayed...
This is what I see :
Tried to read wiki and approaches of other developers.
For what I've read everyone has different system to accomplish that goal, nobody used a camera, and I suspect that's my real problem.
Thanks!
stage.setDebugAll(true); //enable debug mode and check what's going on
camera.setToOrtho(false, 480, 800);
By this you're setting viewport of camera by a fix value. You are not resizing/scaling any Actor so make sure all your actor should have size in the context of 480, 800.
camera.position.set(Gdx.graphics.getWidth()/2,Gdx.graphics.getHeight()/2,1);
This will move your camera in center.

Libgdx auto-scrolling camera

I'm developing a libgdx game. What I want to do is to create a screen where some squares "falls" from the top to the bottom. So:
1. I have to set a background image.
2. I have to generate some squares randomly.
3. I have to move the screen from the bottom to the top.
In order to get that I'm using a Sprite for the background, a lot of sprites for the squares. Then a camera to move the screen.
In the render method I have:
#Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0, 0);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
Gdx.input.setInputProcessor(stage);
batch.begin();
backgroundSprite.draw(batch);
for(Square square : squareList) {
//Gdx.app.log("[Match]", square.toString());
square.updatePosition();
square.setSize(20, 20);
square.draw(batch);
}
batch.end();
batch.setProjectionMatrix(fixedCamera.view);
fixedCamera.position.y +=1;
System.out.println("Camera position: x:" +fixedCamera.position.x+" y:"+fixedCamera.position.y);
fixedCamera.update();
stage.act(Gdx.graphics.getDeltaTime());
stage.draw();
}
This is the code on the constructor. It's all I have in the class
final static int WIDTH = Gdx.app.getGraphics().getWidth();
final static int HEIGHT = Gdx.app.getGraphics().getHeight();
skin = new Skin(Gdx.files.internal("skin/flat-earth-ui.json"));
fixedCamera = new OrthographicCamera(WIDTH, HEIGHT );
stage = new Stage(new ScreenViewport());
batch = new SpriteBatch();
dynBatch = new SpriteBatch();
backgroundSprite = new Sprite(new Texture(Gdx.files.internal(scenario.getTexturePath())));
backgroundSprite.setPosition(0,0);
backgroundSprite.setSize(WIDTH, HEIGHT);
fixedCamera.position.set(0,0, 0);
fixedCamera.update();
The items are displayed correctly, but when the camera moves, all the sprites disappears...
How can I fix that? Is there any way to handle that more efficiently?
Your requirement can be achieved by these steps :
Create Sprite having screen Size and keep it on bottom.
Generate Square at Top of screen with random horizontal position and change y position of all Square(Sprite)
No need to move your camera. keep it in the middle of the screen.

Need help for adding button in menu screen using LIBGDX

I am having 3 different screens which contains splash screen, after menu screen and game screen. Splash > Menu > Gamestarts.
How can i add an image button ??
I want to implement 3 buttons inside Menu screen, Not getting any idea where to start.
public class MenuScreen implements Screen {
private Spacegame game;
private SpriteBatch batch;
private Sprite sprite;
private Texture texture;
TextureRegion bg,play,spacegamelogo,button;
OrthographicCamera camera;
Vector3 touchPoint;
private Skin buttonskin;
public MenuScreen(Spacegame game)
{
touchPoint = new Vector3();
this.game=game;
batch=new SpriteBatch();
bg=AssetLoader.bg;
spacedebrislogo=AssetLoader.spacedebrislogo;
button=AssetLoader.button;
}
#Override
public void show() {
float w = Gdx.graphics.getWidth();
float h = Gdx.graphics.getHeight();
camera = new OrthographicCamera(1, h / w);
sprite = new Sprite(bg);
sprite.flip(false, true);
sprite.setSize(1.0f,
1.0f * sprite.getHeight() / sprite.getWidth() );
sprite.setOrigin(sprite.getWidth() / 2,
sprite.getHeight() / 2);
sprite.setPosition(-sprite.getWidth() / 2,
-sprite.getHeight() / 2);
}
#Override
public void render(float delta) {
batch.setProjectionMatrix(camera.combined);
batch.begin();
sprite.draw(batch);
batch.draw(spacedebrislogo, 33, 54, 50, 40);
batch.end();
if (Gdx.input.isTouched()) {
game.setScreen(new GameScreen());
dispose();
}
}
THere are a lot of methods to do it..
I will tell you the way I do.
First I create my button image, add it to the assets folder and load the texture region.
Now I make a sprite out of it.
Sprite button1=new Sprite(myTextureRegion);
To check if the button is touched I can use the rectangle from the sprite to check if you touched the image.
In your touchUp method you will do something like
if(button1.getBoundingRectangle.contains(screenX,screenY))
// do your thing
To make my game more interesting i like to add some rotation or scaling of my sprite when is clicked, so it looks better, you can play with it, or you can make 2 textures, one for touched down and one for touched up.

draw a texture when i touch a specific texture in libgdx

I am making a game with libgdx. if i touch the screen then a texture appears, but what i really want to do is that when i touch a specific texture then the other texture must appear.
this is my code for now:
public class MyGame extends InputAdapter implements ApplicationListener {
SpriteBatch batch;
Texture ball;
Texture bat;
#Override
public void create() {
ball = new Texture("ball.png");
bat = new Texture("bat.png");
batch = new SpriteBatch();
}
#Override
public void render() {
batch.begin();
if (Gdx.input.isTouched()) {
batch.draw(ball, Gdx.input.getX(), Gdx.graphics.getHeight()
- Gdx.input.getY());
batch.draw(bat, 50, 50);
batch.end();
}
}
}
it's not the whole code, just the code that is used to appear those textures.
I really appreciate your help.
Thankyou
The code below gives an example of how you can extend your current approach to test if the touch is within the area of your texture, but I would not recommend it for use in a real game.
It is fine as an exercise to understand what is going on, but manually coding the touch regions in this way will quickly become cumbersome as your game becomes more complex.
I would strongly recommend you become familiar with the scene2d package in libGdx. This package has methods to handle all the common 2D behaviors such as touch events, movement and collisions.
Like a lot of the libGdx library, the documentation can be hard to follow if you're just starting out, and there are not many tutorials around either. I'd recommend working through the Java Game Development (LibGDX) series of youtube videos by dermetfan. It helped me understand many areas when I was starting out. Good luck.
SpriteBatch batch;
Texture firstTexture;
Texture secondTexture;
float firstTextureX;
float firstTextureY;
float secondTextureX;
float secondTextureY;
float touchX;
float touchY;
#Override
public void create() {
firstTexture= new Texture("texture1.png");
firstTextureX = 50;
firstTextureY = 50;
secondTexture = new Texture("texture2.png");
secondTextureX = 250;
secondTextureY = 250;
batch = new SpriteBatch();
}
#Override
public void render() {
batch.begin; // begin the batch
// draw our first texture
batch.draw(firstTexture, firstTextureX, firstTextureY);
// is the screen touched?
if (Gdx.input.isTouched()) {
// is the touch within the area of our first texture?
if (touchX > firstTextureX && touchX < (firstTextureX + firstTexture.getWidth())
&& touchY > firstTextureY && touchY < (firstTextureY + firstTexture.getHeight()) {
// the touch is within our first texture so we draw our second texture
batch.draw(secondTexture, secondTextureX, secondTextureY);
}
batch.end; // end the batch
}

9patch looks pixeled when setSize is called too many times

I have a sprite that is supposed to act like a loadbar. I have tried this by using an example image that has been created like a 9patch-type (http://cdn.dibbus.com/wp-content/uploads/2011/03/btn_black.9.png). It seems okay in the start, but as the width of the sprite increases the sprite starts to look pixeled. Anyone know what the problem could be, or have any solution? The code is shown below.
public Sprite loaded;
public void init()
{
atlas = new TextureAtlas(Gdx.files.
internal("data/misc/menu_button.pack"));
loaded = atlas.createSprite("loadbar");
loaded.setPosition((Misc.WIDTH/2) - unloaded.getWidth()/2,
Misc.HEIGTH - unloaded.getHeight());
}
public void draw_load_bar() //render function
{
if(loaded.getWidth() < 600)
{
loaded.setSize(loaded.getWidth()+ 0.5f, loaded.getHeight());
}
loaded.draw(batch);
}
Dont use a Sprite to stretch it. I'd recommend a real Ninepatch from libgdx for it.
public NinePatch loaded;
private float posX, posY, width, height;
public void init()
{
loaded = new NinePatch(the Texture here,10, 10, 10, 10); //bounds outside
//set right pos here...
}
public void draw_load_bar(SpriteBatch batch) //render function
{
if(loaded.getWidth() < 600)
{
//update the size of it here (guess pos is static)
width++;
}
//need to parse the batch and the right sizes.
loaded.draw(batch, posx, posy, width, height);
}
after that you can handle it like a Sprite or Texture but it does stratch right without issues. If you want to the full Picture to be Stretched simply do net set any bounds at the creation new NinePatch(texture, 0,0,0,0)

Categories

Resources