I have a camera:

//Map is a my object that wrapper of TiledMap class

In the create method:

this.renderer=new OrthogonalTiledMapRenderer(this.map.getMap();
this.camera=new OrthographicCamera();
this.camera.translate(map.getMapPixelWidth()/2, map.getMapPixelHeight()/2);
view=new StretchViewport(map.getMapPixelWidth(),map.getMapPixelHeight(), camera);
batch=new SpriteBatch();
In the update method:

this.renderer.setView(this.camera); this.renderer.render(); this.camera.update(); batch.setProjectionMatrix(camera.combined);

ArrayList<RectangleMapObject> layer=this.getObjectsFromLayer("collision");//find collision layers
//Mouse coordinates
int x=Gdx.input.getX();
int y=Gdx.input.getY();
Vector3 coordinateScreen=new Vector3(x,y,0);
Vector3 coordinateWorld=camera.unproject(coordinateScreen);
Rectangle rectMouse=new Rectangle(coordinateWorld.x,coordinateWorld.y,20,20);
for(RectangleMapObject rect: layer)
if(Intersector.intersectRectangles(rectMouse, rect.getRectangle(), rectMouse)){
                    System.out.println("COLLISION MOUSE-MAPOBJECT ");}

public ArrayList<RectangleMapObject> getObjectsFromLayer(String layerName){

        ArrayList<RectangleMapObject> array=new ArrayList<RectangleMapObject>();

        MapObjects og=this.map.getMap().getLayers().get(layerName).getObjects();

        for(MapObject object : og)
            if (object instanceof RectangleMapObject) 
                array.add(((RectangleMapObject) object));
        return array;

My problem is that the collision between mouse and mapobject never occurs Also if I click on the area where stands the MapObject. I think it is a problem of conversion of the coordinates of the mouse. Thank you for your time.

  • 2
    \$\begingroup\$ You must not create a new object in each frame ! Don't put things like Vector2 vec = new Vector2() or ArrayList<RectangleMapObject> in your update method. \$\endgroup\$
    – Ion Farima
    Jan 10, 2015 at 14:43
  • \$\begingroup\$ Using Scene2D for this would make it a lot easier. \$\endgroup\$ May 10, 2015 at 20:27

3 Answers 3


You need to unproject your coordinates using the camera.

worldCoordinates = camera.unproject(new Vector3(screenX, screenY, 0));

Just use your camera to unproject a Vector3. If screenX and screenY in this example is Gdx.input.getX and Gdx.input.getY, is the mouse input on your screen and when you unproject it you can use that to hit something that is where you clicked.


If i understand this correctly then you take some data and then as part of the rendering process you distort the postition of that data ...

this.camera=new OrthographicCamera();
this.camera.translate(map.getMapPixelWidth()/2, map.getMapPixelHeight()/2);
view=new StretchViewport(map.getMapPixelWidth(),map.getMapPixelHeight(), camera);

Then you want to be able to pick on it.

Essentially you need to figure out how to do that distortion in reverse or use a raycast to determine the vertex you hit.

Essentially the logic goes something like this ... Figure out a line that goes through the mouse cursor in 3d space in the direction that the camera is facing. Set a start and end point for that line. Raycast using the start and end points. the ray will tell you the first hit vertex.

I'm not familiar with the api you are using but this should at least point you in the right direction.

It looks like you have code for determining the mouse position in the 3d world you now need to create a near and far position for your line then give those to a ray to determine what you clicked on.

Something like this may help you ...


  • \$\begingroup\$ If I'm not wrong OrthographicCamera renders 2D spaces in libgdx \$\endgroup\$
    – Ion Farima
    Jan 10, 2015 at 14:44
  • \$\begingroup\$ Yeh I wasn't referring to the camera but the code that follows it ... by stretching the viewport is the gpu being asked to manipulate the screen position of the pixels its about to render? I would aim to do this without stretching the viewport thus saving the complex need for "unstretching" to determine what was clicked on when doing a raycast. \$\endgroup\$
    – War
    Jan 12, 2015 at 11:12

Tried to make it works with camera.unproject method, resulting in unsuccessful results, I decided to write a raw method myself, here we go :

public boolean mouseMoved(int mouseX, int mouseY) {
    OrthographicCamera camera = gameScreen.getCamera();

    float cameraX = camera.position.x - camera.viewportWidth / 2 * camera.zoom;

    float cameraWidth = camera.viewportWidth * camera.zoom;

    float mouseRelativeX = (float) mouseX / (float) Gdx.graphics.getWidth();

    float addRelativeX = cameraWidth * mouseRelativeX;

    float relativeX = cameraX + addRelativeX;

    float cameraY = camera.position.y - camera.viewportHeight / 2 * camera.zoom;

    float cameraHeight = camera.viewportHeight * camera.zoom;

    float mouseRelativeY = (float) -mouseY / (float) Gdx.graphics.getHeight();

    float addRelativeY = cameraHeight * mouseRelativeY + cameraHeight;

    float relativeY = cameraY + addRelativeY;

            .fireMouseMoved(new MouseMoveEvent(relativeX, relativeY, mouseX, Gdx.graphics.getHeight() - mouseY));
    return true;

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .