【问题标题】:Collision detection (LibGDX)碰撞检测 (LibGDX)
【发布时间】:2016-06-20 10:23:00
【问题描述】:

我的程序检测到图像与屏幕边缘之间的碰撞,但碰撞后我无法拖动图像。我要进行哪些更改,以便我仍然可以拖动图像并且不让图像超出屏幕边缘?

我制作的 Gif 示例:https://s31.postimg.org/f60hh3z2z/Animation.gif

内部渲染

batch.begin();
batch.draw(texture, spritePosition.x, spritePosition.y);
batch.end();

if (Gdx.input.isTouched() && isNotColliding()) {

        camera.unproject(spritePosition.set(Gdx.input.getX() - texture.getWidth() / 2, Gdx.input.getY() + texture.getHeight() / 2, 0));

}


textureBounds.set(spritePosition.x, spritePosition.y, texture.getWidth(), texture.getHeight());

.

private boolean isNotColliding(){
    return screenBounds.contains(textureBounds);
}

【问题讨论】:

    标签: android libgdx collision-detection


    【解决方案1】:

    精灵的位置是左下角。这个位置是你用来检查精灵是否与边缘碰撞的位置。

    因为您可以在一帧中将精灵移动超过 1 个像素,所以您可以将精灵 x 位置设置为负数。如果 spritePosition.x == -1,您将能够很好地看到精灵,但第一行像素将在屏幕之外。

    你不能再移动精灵的原因是因为你的 if

    (Gdx.input.isTouched() && isNotColliding()) {
    

    当 spritePosition.x

    不让精灵移动,让它留在屏幕内。

    if(spritePosition.x < 0){
        spritePosition.x = 0;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-04
      • 1970-01-01
      • 2012-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多