Camera.unproject(Vector3) method is not working in libGdx

| | August 5, 2015

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

One Response to “Camera.unproject(Vector3) method is not working in libGdx”

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

Leave a Reply