【问题标题】:libgdx - correcting for multiple coordinate origins?libgdx - 校正多个坐标原点?
【发布时间】:2014-09-17 06:41:08
【问题描述】:

我有一个碰撞矩形(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);
        ...
        }

【问题讨论】:

  • 1.) setOrigin(0, 0); 2.) stackoverflow.com/questions/24501268/…
  • 虽然从技术上讲,如果您使用Camera 而不是您自己的Matrix3 进行正交变换,那么我认为camera.unproject() 可以为您提供水龙头的坐标。
  • 感谢您的回复。我会看看,并尽可能尝试这些。

标签: java libgdx coordinates


【解决方案1】:

我假设您有一个正交相机。 (没有它你真的无法制作游戏)

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

float originX = rectangle.x + rectangle.width / 2f;
float originY = rectangle.y + rectangle.height / 2f;

无论如何,您需要取消投影您的触摸坐标,这意味着您需要从屏幕位置转换到相机位置(世界位置)。 为此,您需要一个 Vector3,最好在您正在使用的方法之外的某个地方声明它,因为不建议在每一帧中初始化一个对象。

Vector3 tmpPos=new Vector3();

现在检查你的矩形是否被触摸到,你想要的任何地方。 您有两种方法可以做到这一点。

  1. 在渲染/更新方法中执行此操作,检查手指/光标的位置。

公共无效渲染(浮动增量){

   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,使用输入侦听器,因此您只检查何时触发触摸事件。 在你的屏幕/游戏的 show / create 方法中添加你的输入监听器

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
    // ....
    //

});

}

我会使用第二种方式。 让我知道它是否对你有用。

【讨论】:

  • 我在 create() 方法中有 camera.unproject(tmpPos);,在 render() 中有你的第一个方法。它似乎不起作用。它正在报告中心的坐标,也是迄今为止唯一这样做的事情。我的矩形位于左下角50, 50,这就是-300, -460
  • 在原始问题中添加。
  • 这不是完整的代码,只是其中的一部分。提供完整的课程。将其粘贴并链接到此处
  • 整个课程已经有数千行了。这是唯一甚至遥不可及的部分。
  • 如果你的课程是 1000 多行,那很糟糕..你不想做这么大的课程,使用更多的课程是一个更好的主意。无论如何,该代码应该可以工作,1)你如何绘制你的矩形? 2) 你写的render方法叫吗?
【解决方案2】:

我的最终解决方案是这样的:

但是,将矢量设置为触摸坐标,并进行以下更正:(Gdx.input.getY() * -1) + cy 其中cy 是屏幕的高度。然后我取消投影该向量并绘制它。与我未经校正得到的逆 y 位置不同,圆圈直接跟随在我的手指下方,并与世界上的其他物体完美交流。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-09-09
    • 1970-01-01
    • 2014-08-08
    • 2013-02-15
    • 1970-01-01
    • 1970-01-01
    • 2017-05-30
    • 1970-01-01
    相关资源
    最近更新 更多