0
\$\begingroup\$

I know the touch postioning in libGdx uses OpenGL, making 0,0 the bottom left corner while the camera uses the top left corner as 0,0. I did some searching and found this SO question that specifies that I need to use the Camera.unoroject(Vector3) method. This is the relevant code:

package com.mycompany.mygame;

import com.badlogic.gdx.*;
import com.badlogic.gdx.graphics.*;
import com.badlogic.gdx.graphics.g2d.*;
import com.badlogic.gdx.math.*;

public class MyGdxGame implements ApplicationListener
{
    public float x;
    public float y;
    public float w = 50;
    public float h = 50;

    Texture texture;
    SpriteBatch batch;
    OrthographicCamera cam;

    @Override
    public void create()
    {
        texture = new Texture(Gdx.files.internal("android.jpg"));
        batch = new SpriteBatch();
        cam = new OrthographicCamera();

        //x = 50;
        //y = 50;
    }

    @Override
    public void render()
    {        
        Gdx.gl.glClearColor(1, 1, 1, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        batch.begin();

        if(Gdx.input.isTouched()){
            Vector3 touchPos = new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0);
            cam.unproject(touchPos);
            batch.draw(texture, touchPos.x, touchPos.y, w, h);
        }
        batch.end();
    }

    @Override
    public void dispose()
    {
    }

    @Override
    public void resize(int width, int height)
    {
    }

    @Override
    public void pause()
    {
    }

    @Override
    public void resume()
    {
    }
}

The problem is the game is not drawing correctly. It should draw the character at the touchPos.x and touchPos.y coordinates, but it is not. It is drawing in the bottom left corner. See the pictures below:

enter image description here

enter image description here

\$\endgroup\$
1
\$\begingroup\$

Try this:

package com.mycompany.mygame;

import com.badlogic.gdx.*;
import com.badlogic.gdx.graphics.*;
import com.badlogic.gdx.graphics.g2d.*;
import com.badlogic.gdx.math.*;

public class MyGdxGame implements ApplicationListener
{
    public float x;
    public float y;
    public float w = 50;
    public float h = 50;

    Texture texture;
    SpriteBatch batch;
    OrthographicCamera cam;

    @Override
    public void create()
    {
        texture = new Texture(Gdx.files.internal("android.jpg"));
        batch = new SpriteBatch();
        cam = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());

        //x = 50;
        //y = 50;
    }

    @Override
    public void render()
    {        
        Gdx.gl.glClearColor(1, 1, 1, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        batch.setProjectionMatrix(cam.combined);
        batch.begin();

        if(Gdx.input.isTouched()){
            Vector3 touchPos = new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0);
            cam.unproject(touchPos);
            batch.draw(texture, touchPos.x, touchPos.y, w, h);
        }
        batch.end();
    }

    @Override
    public void dispose()
    {
    }

    @Override
    public void resize(int width, int height)
    {
    }

    @Override
    public void pause()
    {
    }

    @Override
    public void resume()
    {
    }
}

If this doesn't work, try to print touch coordinates before and after cam.unproject(). That's the best way to understand bug and fix it.

\$\endgroup\$
  • \$\begingroup\$ When I try this code the texture doesnt appear on the screen at all. \$\endgroup\$ – JDSweetBeat Jun 17 '15 at 15:49
  • \$\begingroup\$ I am going to use virtual world units in my game, I am just trying to get a grasp on touch input with it. Already read through the linked wiki. Has little on handling input, just how to set up virtual world units. \$\endgroup\$ – JDSweetBeat Jun 17 '15 at 18:17
  • \$\begingroup\$ What if you change camera viewport size to Gdx.graphics.getWidth() Gdx.graphics.getHeight()? \$\endgroup\$ – nikoliazekter Jun 17 '15 at 19:06
  • \$\begingroup\$ Still nothing... \$\endgroup\$ – JDSweetBeat Jun 17 '15 at 19:14
  • \$\begingroup\$ As it turned out I forgot to update the camera with cam.update() method. Thanks for the support with ur answer. \$\endgroup\$ – JDSweetBeat Jun 18 '15 at 16:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.