Move a shape to place where my finger is touched - java

#Override
public void create()
{
batch = new SpriteBatch();
shape = new ShapeRenderer();
velocity = new Vector2(100, 0);
position = new Rectangle(0, 5, 100, 100);
font = new BitmapFont();
font.setColor(Color.BLACK);
font.getData().scale(3f);
camera = new OrthographicCamera();
confCamera();
}
#Override
public void render()
{
if(Gdx.input.isTouched())
position.x = Gdx.input.getX() - position.width/2;
position.y = (Gdx.input.getY() - position.height/2);
shape.begin(ShapeRenderer.ShapeType.Filled);
shape.setColor(Color.BLACK);
shape.rect(position.x, position.y, position.width, position.height);
shape.end();
}
It's a simple code, but I'm not undestanding Y axis, my shape moves like a mirror. If I touch on top, my shape goes to bottom. If I touch on bottom, my shape goes to top. How to fix it?

LibGDX (by default) for rendering uses coordinate system where 0 is at bottom of the screen and the more you go up Y coordinate grows.
Also, when you read input coordinates (touches, moves...) you get screen coordinates, but when you render your graphics you are using "world" coordinates. They are in 2 different coordinate system so to convert from screen to world you have to use camera.unproject() call. Should be like:
Vector3 touchPos = new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0);
camera.unproject(touchPos);
and then use touchPos.x and touchPos.y.
The similar question is asked here so you can find more answers there:
Using unProject correctly in Java Libgdx

Related

My tiled map game keeps flickering on the edges of the tiles when the camera moves

There is an awful flickering that happens occasionally on my screen from top to bottom. It only happens when the camera moves around (which follows my player character). It does not matter where I am on the map for this to happen. If I stop moving at the right moment, the lines will remain visible until I start moving again. I've added a screenshot so you can see what it looks like:
If you look closely, you might notice that the lines represent a part of my "mountain wall" tile (That tile is used a little bit lower in the map):
I've tried putting the cam.update() in the beginning of the update() method, as some other people on StackOverflow have suggested, but that doesn't seem to work. I have also made sure that there is absolutely no space in between tiles in the tileset image. Most questions I see about this issue experience a different kind of flickering than I am.
Here is the code of my screen and all the rendering that happens:
public PlayScreen(MyGame game){
cam = new OrthographicCamera();
batch = new SpriteBatch();
gameMap = new TestMap(startMap);
player = new Player(this);
Gdx.input.setInputProcessor(this);
}
public void update(float delta){
player.update(delta);
Rectangle playerRect = player.getNextRectangle(delta);
Rectangle teleportRect = gameMap.teleportTo(playerRect);
if(!gameMap.isColliding(playerRect)){
player.setPosition(playerRect);
}
if(teleportRect != null){
player.setPosition(teleportRect);
}
cam.viewportHeight = Gdx.graphics.getHeight() / zoomLevel;
cam.viewportWidth = Gdx.graphics.getWidth() / zoomLevel;
cam.position.x = player.getX() + (player.getWidth() / 2);
cam.position.y = player.getY() + (player.getHeight() / 2);
cam.update();
}
#Override
public void render(float delta) {
update(delta);
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
gameMap.render(cam);
Gdx.graphics.setTitle("FPS: "+Gdx.graphics.getFramesPerSecond());
gameMap.getMapRenderer().getBatch().begin();
player.draw(gameMap.getMapRenderer().getBatch());
gameMap.getMapRenderer().getBatch().end();
}

Libgdx set ortho camera

I'm having problems setting my orthographic camera to the bottom left part of my screen (0,0)
public GameScreen(Main game) {
this.game = game;
Width = 200;
Height = 300;
view = new ExtendViewport(Width,Height);
camera = new OrthographicCamera(Gdx.graphics.getWidth(),Gdx.graphics.getHeight());
camera.setToOrtho(false,Width/2,Height/2);
camera.position.set(Width,Height,0);
camera.update();
play.Player1();
staple = new Stage();
staple.addActor(play);
staple.addActor(pile);
Gdx.input.setInputProcessor(staple);
}
#Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
Gdx.graphics.getDeltaTime();
game.getBatch().begin();
game.getBatch().setProjectionMatrix(camera.combined);
game.getBatch().end();
staple.act();
staple.draw();
}
#Override
public void resize(int width, int height) {
view.update(width,height);
view.setScreenPosition(width,height);
}
I've set my viewport as extended viewport using my width and height values I have assigned but I'm struggling to move the camera to the bottom left
part of my screen (0,0) where it can focus on my images on my android device.
Here are a little example how to use camera and viewport:
First we must define how big is our world the camera shows:
private static final int WORLD_WIDTH = 300;
private static final int WORLD_HEIGHT = 250;
Our world is now 300 x 250 units (not Pixel!) big.
It's importent to think in units not in pixels!!
Now we need a OrthographicCamera, a Viewport and a SpriteBatch
OrthographicCamera camera;
Viewport viewport;
SpriteBatch batch;
#Override
public void create () {
camera = new OrthographicCamera(); // we create a OrthographicCamera
viewport = new ExtendViewport(WORLD_WIDTH, WORLD_HEIGHT, camera); // we create a new Viewport with our camera and we will display our world 300 x 250 units
batch = new SpriteBatch(); // we create a new SpriteBatch for draw our textures
}
In our render method we say the batch only to draw what we can see in our Viewport with the method setProjectionMatrix()
#Override
public void render (float delta) {
camera.update(); //update our camera every frame
batch.setProjectionMatrix(camera.combined); //say the batch to only draw what we see in our camera
batch.begin();
batch.draw(texture, 0,0); //draw our texture on point 0,0 bottom left corner
batch.end();
}
And in the resize method:
public void resize(int width, int height){
viewport.update(width, height); //update the viewport to recalculate
}
To understand why you have this issue:
In your code you never set the camera to the viewport: view = new ExtendViewport(Width,Height);
So your viewport never apply to the batch.
To render the correct way without Viewport you must know that the position of OrhographicCamera is in the center.
So when you set a Camera to position 0,0 and size 50,50 you see the world from -25 to 25 in each direction;
To use OrthographicCamera without Viewport:
public void create () {
camera = new OrthographicCamera(WORLD_WIDTH, WORLD_HEIGHT); // we create a OrthographicCamera and we will display our world 300 x 250 units
camera.position.set(camera.viewportWidth / 2, camera.viewportHeight / 2, 0); //we set position of camera, our world point 0,0 is now the bottom left corner in the camera
batch = new SpriteBatch(); // we create a new SpriteBatch for draw our textures
texture = new Texture("badlogic.jpg");
}
public void render () {
camera.update();
batch.setProjectionMatrix(camera.combined);
batch.begin();
batch.draw(texture, 0,0);
batch.end();
}
The important point is in the resize method:
public void resize(int width, int height){
camera.viewportWidth = WORLD_WIDTH;
camera.viewportHeight = WORLD_HEIGHT * height / width;
camera.position.set(camera.viewportWidth / 2, camera.viewportHeight / 2, 0);
}
With this calculation you always see a World of 300 width and 250 * ratio of width, and height.
And exactly this calculation does the viewport for you. Depending on which Vieport (FitViewport, ScreenViewport, ExtendViewport) you use this calculation will be different, try it out.
I hope this helps you to understand how camera, viewport and Spritebatch works together.
Here are useful links to the libgdx wiki which descript the Viewport and Camera:
https://github.com/libgdx/libgdx/wiki/Viewports
https://github.com/libgdx/libgdx/wiki/Orthographic-camera
Use camera.position.set(Width, Height, 1); instead of 0
First you set the Camara width and height equal to the amount of pixels of the window.
camera = new OrthographicCamera(Gdx.graphics.getWidth(),Gdx.graphics.getHeight());
Then you center it at 100, 150. And move it again to 200, 300
camera.setToOrtho(false,Width/2,Height/2);
camera.position.set(Width,Height,0);
You are also using a Viewport but never make use of it.
I would recommend just using a Viewport of choice. A Viewport can take a camera so if you insist using your own camera you can create it but then also pass it to the Viewport when you construct it.
EDIT
Following is a tested minimal example.
public class TestScreen extends ScreenAdapter {
private Viewport viewport;
private ShapeRenderer sr;
public TestScreen() {
// Note that extend viewport extends it's camera so you end up with smaller or larger view of your world depending on the aspect ratio of the physical screen.
viewport = new ExtendViewport(200, 300);
viewport.apply();
System.out.println(viewport.getWorldWidth());
// Just for testing in the resize method.
viewport.getCamera().translate(0, 0, 0);
viewport.getCamera().update();
// ShapeRenderer for testing
sr = new ShapeRenderer();
sr.setAutoShapeType(true);
}
#Override
public void render(float delta) {
Gdx.gl.glClearColor(.04f, .06f, .1f, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
// Draw circle at 0.0 in the world
sr.setProjectionMatrix(viewport.getCamera().combined);
sr.begin(ShapeRenderer.ShapeType.Line);
sr.circle(0, 0, 100);
sr.end();
}
#Override
public void resize(int width, int height) {
// Will center the camera in the world at half it's width and half it's height so left bottom is 0.0 as long as the camera did.
// By using true here you cannot move the camera since you order it to center on the screen and thus the circle we are drawing
// remains in the bottom left.
//viewport.update(width, height, true);
// This will just update the viewport, we moved the camera slightly to the left so the circle appears slight right from the middle.
viewport.update(width, height, false);
// So you want to start your camera centered on something but still want to move it you need to specify that center in the camera
// by either changing it's position or translating it like I did in the constructor. Unfortunately you only get to know the size
// of the world that is being displayed once this resize method did it's job so certain parts might get cut off or your world does
// not fill the screen.
}
}

Sprite image appears stretched and quality is poor, can't get in correct position GDXLib

I am quite new to programming so bear with me here...
I am making a 2d basic game just to practice programming in android studio and can't get my sprite to the correct position on the screen. Also when I draw the sprite it appears stretched and the quality is very poor. Any help is appreciated!
public class MyGdxGame extends ApplicationAdapter {
SpriteBatch batch;
Texture background;
Texture ball;
#Override
public void create () {
batch = new SpriteBatch();
background = new Texture("gamebackground.png");
ball = new Texture("ball2.png");
}
#Override
public void render () {
batch.begin();
batch.draw(background, 0,0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
batch.draw(ball, 0,0, Gdx.graphics.getWidth() / 2, Gdx.graphics.getHeight() / 2);
batch.end();
}
You need to keep the original width/height ratio:
instead of scaling it to half the screen size, define your scaling like that:
float scaleFactor = 2.0f;
batch.draw(ball, 0,0, ball.getWidth()*scaleFactor, ball.getHeight*scaleFactor);
If your image is "blurry", and you want the individual pixels to stay crisp, try loading the texture like that:
ball = new Texture("ball2.png");
ball.setFilter(TextureFilter.Nearest, TextureFilter.Nearest);
This prevents (default) linear interpolation when scaling the texture.

Libgdx: Different positions using BitmapFont

I encountered a problem within my (very) simple project. When I draw text, it is displayed in a different position (and appareantly with different size) between Android (Samsung Galaxy S Advance) and Desktop.
In my renderer class I have:
private OrthographicCamera cam;
public final int WIDTH = 320;
public final int HEIGHT = 480;
private ShapeRenderer debugRenderer = new ShapeRenderer();
private SpriteBatch batch = new SpriteBatch();
private BitmapFont font;
And in my constructor:
MyClass(){
this.cam = new OrthographicCamera(WIDTH, HEIGHT);
this.cam.position.set(WIDTH/2, HEIGHT/2, 0);
this.cam.update();
font = new BitmapFont(Gdx.files.internal("data/fonts/font.fnt"));
font.setColor(Color.RED);
}
Finally, the render function:
public void render() {
// For each block y use this code:
// debugRenderer.begin(ShapeType.Filled);
// Rectangle rect = block.getBounds();
// debugRenderer.rect(rect.x, rect.y, rect.width, rect.height);
// debugRenderer.end();
// And then I draw my text:
batch.begin();
font.draw(batch, "Score: " + world.getScore(), 50,50)
batch.end();
}
All block dimensions are in absolute numbers (not relative to any variable like stage.getWidth() or something like that).
The result in Desktop is the following:
While in Android I have:
As you may see, blocks have the same distribution along the screen (in terms of % of screen covered), while text doesn't. It doesn't start in the same place and doesn't have the same height.
Does anybody know what I could be doing wrong?
Just in case it helps, blocks bounds are:
XPos: 45 * i
YPos: 45 * j
SizeX: 32
SizeY: 32
EDIT: Another consideration, the font I'm using is extracted from: https://github.com/libgdx/libgdx/tree/master/demos/superjumper
After this.cam.update(); do you do -
batch.setProjectionMatrix(this.cam.combined);
debugRenderer.setProjectionMatrix(this.cam.combined);

Drawing font inside moving sprite libgdx

I have some problems with font drawing in the center of the sprite. The sprite is moving along the screen. I set my orthographic camera to:
w = Gdx.graphics.getWidth();
h = Gdx.graphics.getHeight();
camera = new OrthographicCamera(1, h/w);
and:
Sprite spr = new Sprite(starTx);
spr.setSize(0.3f, 0.3f);
spr.setOrigin(0.15f, 0.15f);
spr.setPosition(0.2f, 0.25f*(i+1));
Inside render I have the code:
batch.setProjectionMatrix(camera.combined);
batch.begin();
spr.draw(batch);
font.setScale(1, (w/h)*3);
font.draw(batchFont, mDate, valueX, valueY);
batch.end();
I must draw the font in the center of the sprite. Could anyone tell me how to calculate valueX and ValueY?
Not exactly what you posted, the screen width is 1, so multiplying for it is useless :p More general approach:
valueX = spr.getX()+spr.getWidth()/2F;
valueY = spr.getY()+spr.getHeight()/2F;

Categories

Resources