Java libgdx-校正多个坐标原点?

Java libgdx-校正多个坐标原点?,java,libgdx,coordinates,Java,Libgdx,Coordinates,我有一个碰撞矩形libgdx,而不是awt设置为球的坐标。我要做的是检查我是否点击了它。两个问题: A.我不知道矩形坐标的原点在哪里。 B我找不到纠正抽头位置的正确方法。 我能做什么 按请求编辑: public void create() { camera = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); ... camera.unproject(tmpPos); } public vo

我有一个碰撞矩形libgdx,而不是awt设置为球的坐标。我要做的是检查我是否点击了它。两个问题: A.我不知道矩形坐标的原点在哪里。 B我找不到纠正抽头位置的正确方法。 我能做什么

按请求编辑:

public void create() {
camera = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());

...

camera.unproject(tmpPos);
}

public void render() {
if (Gdx.input.isTouched()) {
            float x = Gdx.input.getX();
            float y = Gdx.input.getY();
            tmpPos.set(x, y, 0);
            camera.unproject(tmpPos);
            // now the world coordinates are tmpPos.x and tmpPos.y
            if (target.contains(tmpPos.x, tmpPos.y)) {
                System.out.println("Touched. ");
                score++;
            }

            System.out.println("Score: " + score + "..." + "X: " + (int) tmpPos.x + "...Y: " + (int) tmpPos.y);
        ...
        }

我假设你有一个正交相机。没有它,你真的不能做游戏

你不需要知道它的原点来检查是否被触摸,但是如果你想知道,原点是

float originX = rectangle.x + rectangle.width / 2f;
float originY = rectangle.y + rectangle.height / 2f;
无论如何,您将需要取消投影您的触摸坐标,这意味着您需要从屏幕位置转换到相机位置世界位置。 为此,您需要一个Vector3,最好在您使用的方法之外的某个地方声明它,因为不建议在每个帧中初始化对象

Vector3 tmpPos=new Vector3();
现在检查你的矩形是否被触碰,在你想要的任何地方。 你有两种方法可以做到这一点

在render/update方法中执行此操作,检查手指/光标的位置。 公共无效renderfloat delta{

   if (Gdx.input.isTouched() { // use .isJustTouched to check if the screen is touched down in this frame
            // so you will only check if the rectangle is touched just when you touch your screen.
            float x = Gdx.input.getX();
            float y = Gdx.input.getY();
            tmpPos.set(x, y, 0);
            camera.unproject(tmpPos);
            // now the world coordinates are tmpPos.x and tmpPos.y
            if (rectangle.contains(tmpPos.x, tmpPos.y)) {
                // your rectangle is touched
                System.out.println("YAAAY I'm TOUCHED");
            }

        }
    }
选项2,使用输入侦听器,因此您只能在触发触摸事件时进行检查。 在屏幕/游戏的显示/创建方法中添加输入侦听器

public void show() {

// .....
Gdx.input.setInputProcessor(new InputProcessor() {

    // and in the method that you want check if the rectangle is touched
    @override
    public void touchDown(int screenX, int screenY, int pointer, int button) {
        tmpPos.set(screenX, screenY, 0);
        camera.unproject(tmpPos);
        // now the world coordinates are tmpPos.x and tmpPos.y
        if (rectangle.contains(tmpPos.x, tmpPos.y)) {
            // your rectangle is touched
            System.out.println("YAAAY I'm TOUCHED");
        }
    }

    // and the rest of the methods
    // ....
    //

});
}

我会用第二种方法。
让我知道它是否对您有效。

我的最终解决方案是:


但是,通过此校正将向量设置为触摸坐标:Gdx.input.getY*-1+cy,其中cy是屏幕的高度。然后,我将该向量取消投影,并绘制它。这个圆不是我在没有校正的情况下得到的y轴反向位置,而是直接沿着我的手指下方,与世界上的其他物体完美地沟通。

1。setOrigin0,0;2.虽然从技术上讲,如果您使用相机而不是自己的Matrix3进行正交变换,那么我认为Camera.unproject可以为您提供水龙头的坐标。谢谢您的回复。我会看一看,并尝试这些当我可以。我有相机。unprojecttmpPos;在我的create方法中,以及在render中的第一个方法中。它似乎不起作用。它正在报告中心的坐标,而且是目前为止唯一一个这样做的东西。我的矩形是50,50是左下角,这是-300,-460。在原始问题中添加。这不是完整的代码,只是它的一部分。提供完整的类。粘贴并链接到这里整个类已经有数千行了。如果你的类有1000多行,那就太糟糕了。你不想创建这么大的类,使用更多的类是更好的主意。不管怎样,这段代码应该是有效的,1你怎么画你的矩形?2您编写的render方法是否被调用?