I have started learning android and libgdx now, and trying to figure out how does the screen, and the touch input work in android, now I am testing touch positions by drawing a rectangle upon touch.
The rectangle will be drawing on the touched X, Y.
But when I did it, when I touch on the left, it will draw a rectangle on the right, same if I do the opposite, it will draw on left.
if I touch the center, somewhat it will draw it in the center, but really not same coords as my finger's touch.
What is the accurate way to detect positions?
public class myApp implements ApplicationListener {
private OrthographicCamera camera;
private SpriteBatch batch;
private Texture texture;
private Sprite sprite;
private ShapeRenderer shapeRender;
private Player player;
private ArrayList<Player> players;
@Override
public void create() {
float w = Gdx.graphics.getWidth();
float h = Gdx.graphics.getHeight();
camera = new OrthographicCamera(w, h);
shapeRender = new ShapeRenderer();
batch = new SpriteBatch();
texture = new Texture(Gdx.files.internal("data/libgdx.png"));
texture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
TextureRegion region = new TextureRegion(texture, 0, 0, 512, 275);
sprite = new Sprite(region);
sprite.setSize(0.9f, 0.9f * sprite.getHeight() / sprite.getWidth());
sprite.setOrigin(sprite.getWidth()/2, sprite.getHeight()/2);
sprite.setPosition(-sprite.getWidth()/2, -sprite.getHeight()/2);
this.players = new ArrayList<Player>();
}
@Override
public void dispose() {
batch.dispose();
texture.dispose();
}
@Override
public void render() {
update(Gdx.graphics.getDeltaTime());
renderGame();
}
private void update(double delta) {
Input i = Gdx.input;
if (i.isTouched()) {
this.players.add(new Player(i.getX(), i.getY()));
}
}
private void renderGame() {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
//shapeRender.setProjectionMatrix(camera.combined);
if (players.size() > 0) {
shapeRender.begin(ShapeType.Filled);
shapeRender.setColor(Color.RED);
for (Player p : this.players) {
shapeRender.rect(p.getX(), p.getY(), 100, 100);
}
shapeRender.end();
}
}
@Override
public void resize(int width, int height) {
}
@Override
public void pause() {
}
@Override
public void resume() {
}
}