地砖等距游戏的碰撞检测

时间:2011-11-23 18:31:45

标签: java collision-detection isometric

我正在编写一个基于等距瓷砖的游戏,其中角色可以在瓷砖之间自由行走,但无法跨越到具有碰撞标志的某些瓷砖。听起来很简单,只需使用Screen Coordinates to Tile方法检查玩家将要移动的位置,并使用我们返回的xy索引检查tiles数组,看看它是否可以碰撞。如果不是,则不要移动角色。我遇到的问题是我的Screen to Tile方法没有吐出正确的X,Y tile索引。这种方法可以完美地用鼠标选择瓷砖。 注意:我的X牌从左到右,我的Y牌从上到下。从网上的一些例子中反过来。 以下是相关代码:

    public Vector2 ScreentoTile(Vector2 screenPoint) {
            //Vector2 is just a object with x and y float properties
            //camOffsetX,Y are my camera values that I use to shift everything but the
                //current camera target when the target moves
            //tilescale = 128, screenheight = 480, the -46 offset is to center
                // vertically + 16 px for some extra gfx in my tile png
    Vector2 tileIndex = new Vector2(-1,-1);

    screenPoint.x -= camOffsetX;
    screenPoint.y = screenHeight - screenPoint.y - camOffsetY - 46;

    tileIndex.x = (screenPoint.x / tileScale) + (screenPoint.y / (tileScale / 2));
    tileIndex.y = (screenPoint.x / tileScale) - (screenPoint.y / (tileScale / 2));

    return tileIndex;
}

调用此代码的方法是:

    private void checkTileTouched () {

    if (Gdx.input.justTouched()) {
            if (last.x >= 0 && last.x < levelWidth && last.y >= 0 && last.y < levelHeight) {
                if (lastSelectedTile != null) lastSelectedTile.setColor(1, 1, 1, 1);
                Sprite sprite = levelTiles[(int) last.x][(int) last.y].sprite;
                sprite.setColor(0, 0.3f, 0, 1);
                lastSelectedTile = sprite;
            }
    }
    if (touchDown) {
        float moveX=0,moveY=0;
        Vector2 pos = new Vector2();

            if (player.direction == direction_left) {
                moveX =  -(player.moveSpeed);
                moveY =  -(player.moveSpeed / 2);
                Gdx.app.log("Movement", String.valueOf("left"));
            } else if (player.direction == direction_upleft) {
                moveX = -(player.moveSpeed);
                moveY = 0;
                Gdx.app.log("Movement", String.valueOf("upleft"));
            } else if (player.direction == direction_up) {
                moveX = -(player.moveSpeed);
                moveY = player.moveSpeed / 2;
                Gdx.app.log("Movement", String.valueOf("up"));
            } else if (player.direction == direction_upright) {
                moveX = 0;
                moveY = player.moveSpeed;
                Gdx.app.log("Movement", String.valueOf("upright"));
            } else if (player.direction == direction_right) {
                moveX = player.moveSpeed;
                moveY = player.moveSpeed / 2;
                Gdx.app.log("Movement", String.valueOf("right"));
            }  else if (player.direction == direction_downright) {
                moveX = player.moveSpeed;
                moveY = 0;
                Gdx.app.log("Movement", String.valueOf("downright"));
            }  else if (player.direction == direction_down) {
                moveX = player.moveSpeed;
                moveY = -(player.moveSpeed / 2);
                Gdx.app.log("Movement", String.valueOf("down"));
            }  else if (player.direction == direction_downleft) {
                moveX = 0;
                moveY = -(player.moveSpeed);
                Gdx.app.log("Movement", String.valueOf("downleft"));
            }
            //Player.moveSpeed is 1
                    //tileObjects.x is drawn in the center of the screen (400px,240px)
                    // the sprite width is 64, height is 128

            testX = moveX * 10;
            testY = moveY * 10;

            testX += tileObjects.get(player.zIndex).x + tileObjects.get(player.zIndex).sprite.getWidth() / 2;
            testY += tileObjects.get(player.zIndex).y + tileObjects.get(player.zIndex).sprite.getHeight() / 2;

            moveX += tileObjects.get(player.zIndex).x + tileObjects.get(player.zIndex).sprite.getWidth() / 2;
            moveY += tileObjects.get(player.zIndex).y + tileObjects.get(player.zIndex).sprite.getHeight() / 2;

            pos = ScreentoTile(new Vector2(moveX,moveY));
            Vector2 pos2 = ScreentoTile(new Vector2(testX,testY));


            if (!levelTiles[(int) pos2.x][(int) pos2.y].collides) {
                Vector2 newPlayerPos = ScreentoTile(new Vector2(moveX,moveY));
                CenterOnCoord(moveX,moveY);
                player.tileX = (int)newPlayerPos.x;
                player.tileY = (int)newPlayerPos.y;
            }
    }
}

当玩家向左移动时(从观看者的角度来看), 我的Pos2 X值按预期减少,但是pos2没有在x tile上检查,它正在检查 在Y牌上前进(好像我们正在向下移动,而不是向左移动),反之亦然,如果玩家向下移动,它将在X值上提前检查(好像我们正在向左移动,而不是向下移动)。 而不是Y值。

0 个答案:

没有答案