Quantcast
Channel: Question and Answer » libgdx
Viewing all articles
Browse latest Browse all 434

Mouse coordinate conversion Libgdx

$
0
0

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);
this.camera.update();
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.


Viewing all articles
Browse latest Browse all 434

Trending Articles