【问题标题】:Collision detection with user's tap [duplicate]使用用户点击进行碰撞检测[重复]
【发布时间】:2014-07-29 23:13:16
【问题描述】:

我很快就开始使用 libGDX,我正在制作一个游戏,水滴从屏幕顶部落下,用户应该在它们触碰屏幕底部之前点击它们。我无法知道用户是否确实点击了水滴来做某事。

这是我的渲染方法:

Gdx.gl.glClearColor(0/255.0f, 0/255.0f, 100/255.0f, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

cam.update();

if (TimeUtils.millis() - lastDrop > 333) 
    spawnRainDrop();

Iterator<Rectangle> iter = raindrops.iterator();
while (iter.hasNext()) {
    Rectangle raindrop = iter.next();
    raindrop.y -= 300 * Gdx.graphics.getDeltaTime();
    if (raindrop.y + 64 < 0) {
        iter.remove();
    }
    if (Gdx.input.isTouched()) {
        Vector3 touchPos = new Vector3();
        cam.unproject(touchPos);
        if (Gdx.input.getX() == raindrop.x + 64 / 2 &&  Gdx.input.getY() == raindrop.y + 64 / 2) {
            System.out.println("Tapped");
        }
    }   
}

点击代码似乎不起作用。

如果有人解释了他们的答案,我将不胜感激。 谢谢

【问题讨论】:

标签: java android libgdx game-engine


【解决方案1】:

您正在测试单个点的触摸,而不是在对象周围的边界框中。使用以下方法测试边界框内的触摸:

if(Gdx.input.isTouched()) {
   Vector3 touchPos = new Vector3();
   cam.unproject(touchPos);
   float halfWidth = 64 / 2.0f;  
   float halfHeight = 64 / 2.0f; 
   if( Gdx.input.getX() >= raindrop.x - halfWidth &&
       Gdx.input.getX() <= raindrop.x + halfWidth &&
       Gdx.input.getY() <= raindrop.y + halfHeight && 
       Gdx.input.getY() >= raindrop.y - halfHeight ) {
      System.out.println("Tapped");
   }
}

我假设是底部/左侧原点,所以如果您使用不同的符号,只需相应地更改符号即可。

【讨论】:

  • 我正在尝试测试边界矩形周围的触摸,但问题是我的矩形是一个数组,我不知道如何告诉他在按下特定矩形时点击打印(下降)。
【解决方案2】:

在二维中,我使用以下方法来检测玩家点击的位置:https://stackoverflow.com/a/24511980/2413303

但在你的情况下,我认为问题只是你使用了Gdx.input.getX()Gdx.input.getY() 而不是你从cam.unproject(touchPos) 获得的坐标,根据同一问题的另一个答案:https://stackoverflow.com/a/24503526/2413303 .

【讨论】:

    猜你喜欢
    • 2010-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多