Java LibGDX触摸位置坐标错误

Java LibGDX触摸位置坐标错误,java,android,touch,libgdx,coordinate-transformation,Java,Android,Touch,Libgdx,Coordinate Transformation,所以,我使用LibGdx并尝试使用它们的矩形类作为触摸屏上按钮按下的边界。它以1:1的比例完美地工作,但当我把游戏放在手机上(屏幕更小)时,图像会被正确地缩放和绘制,但矩形不会。所以我试着保持矩形的正常比例,并“放大”触摸屏的XY坐标,但我想我做得不对,因为它不起作用 optionsMenu = new Vector<Rectangle>(); optionsMenu.add(new Rectangle(100 + (120 * 0), 100, 100, 100)); option

所以,我使用LibGdx并尝试使用它们的矩形类作为触摸屏上按钮按下的边界。它以1:1的比例完美地工作,但当我把游戏放在手机上(屏幕更小)时,图像会被正确地缩放和绘制,但矩形不会。所以我试着保持矩形的正常比例,并“放大”触摸屏的XY坐标,但我想我做得不对,因为它不起作用

optionsMenu = new Vector<Rectangle>();
optionsMenu.add(new Rectangle(100 + (120 * 0), 100, 100, 100));
optionsMenu.add(new Rectangle(100 + (120 * 1), 100, 100, 100));
optionsMenu.add(new Rectangle(100 + (120 * 2), 100, 100, 100));
optionsMenu.add(new Rectangle(100 + (120 * 3), 100, 100, 100));
以下是我绘制按钮的方式:

spriteBatch.draw(buttonImage, optionsMenu.get(2).bounds.getX(), 
optionsMenu.get(2).bounds.getY(), optionsMenu.get(2).bounds.getWidth(), 
optionsMenu.get(2).bounds.getHeight(), 0, 0, buttonImage.getWidth(), 
buttonImage.getHeight(), false, true);
这就是我做触摸屏逻辑的方式:

public boolean tap(float x, float y, int count, int button) {
    Vector3 temp = new Vector3(x, y, 0);
    camera.unproject(temp);

    float scalePos = (int) ((float) Gdx.graphics.getWidth()/800.0f);
    temp.x = temp.x * scalePos;
    temp.y = temp.y * scalePos;

    if(optionsMenu.get(0).bounds.contains(temp.x, temp.y)){
        //do sutff
    }

    else if(optionsMenu.get(1).bounds.contains(temp.x, temp.y)){
        //do other stuff

    }

  return false;
}

几天前我遇到了同样的问题。这就是我为解决这个问题所做的:

如果正在使用视口,则应将该数据添加到
摄影机.unproject
调用中,以确保考虑视口

例如:

camera.unproject(lastTouch,viewport.x,viewport.y,viewport.width,viewport.height);
为了调试矩形边界和触摸位置,我使用此方法将它们绘制到屏幕中:

private static ShapeRenderer debugShapeRenderer = new ShapeRenderer();

public static void showDebugBoundingBoxes(List<Rectangle> boundingBoxes) {
    debugShapeRenderer.begin(ShapeType.Line); // make sure to end the spritebatch before you call this line
    debugShapeRenderer.setColor(Color.BLUE);
    for (Rectangle rect : boundingBoxes) {
        debugShapeRenderer.rect(rect.x, rect.y, rect.width, rect.height);
    }
    debugShapeRenderer.end();
}
private static shaperender debugshaperender=new shaperender();
公共静态无效显示调试边界框(列出边界框){
debugShapeRenderer.begin(ShapeType.Line);//请确保在调用此行之前结束spritebatch
setColor(Color.BLUE);
用于(矩形矩形:边框){
debugshapenderer.rect(rect.x、rect.y、rect.width、rect.height);
}
debugShapeRenderer.end();
}

不确定这是否是完整的答案,但查看行“float scalePos=(int)((float)Gdx.graphics.getWidth()/800.0f);”,当getWidth()小于800时会发生什么?这将给出一个小于1.0f的数字,然后将其转换为int,并将其截断为零。对于按钮和UI,通常我建议您使用scene2d模块。为什么在取消投影后要缩放位置?您不应该这样做,因为unproject已经完成了转换。
private static ShapeRenderer debugShapeRenderer = new ShapeRenderer();

public static void showDebugBoundingBoxes(List<Rectangle> boundingBoxes) {
    debugShapeRenderer.begin(ShapeType.Line); // make sure to end the spritebatch before you call this line
    debugShapeRenderer.setColor(Color.BLUE);
    for (Rectangle rect : boundingBoxes) {
        debugShapeRenderer.rect(rect.x, rect.y, rect.width, rect.height);
    }
    debugShapeRenderer.end();
}