鼠标坐标转换Libgdx

时间:2014-09-02 10:04:10

标签: java libgdx

我有一台相机:

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

在create方法中:

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();

在更新方法中:

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;
}

我的问题是鼠标和mapobject之间的碰撞永远不会发生。如果我点击MapObject的区域。 我认为这是转换鼠标坐标的问题。 谢谢你的时间。

0 个答案:

没有答案