等距瓷砖采摘给予负值

时间:2014-10-07 02:50:27

标签: java game-engine isometric

我需要在点击时找到等距瓷砖的坐标。我已经把它弄清楚了。 Y值正确返回,但显然我的X公式不正确,我收到了负值。

图表A:

灰色瓷砖应该是0,0但是由于某种原因我得到了-5 ... enter image description here

这是我的代码。

这里我们制作地图(可能与问题无关,但这里只是为了说明我是如何做事的)。 ObjectID只是一个存储磁贴类型的枚举。

ObjectID tile_map[][] = new ObjectID[][] {
                {ObjectID.Ground_Concrete, ObjectID.Ground_Grass, ObjectID.Ground_Grass, ObjectID.Ground_Grass},
                {ObjectID.Ground_Grass, ObjectID.Ground_Grass, ObjectID.Ground_Grass, ObjectID.Ground_Grass},
                {ObjectID.Ground_Grass, ObjectID.Ground_Grass, ObjectID.Ground_Grass, ObjectID.Ground_Grass},
                {ObjectID.Ground_Grass, ObjectID.Ground_Grass, ObjectID.Ground_Grass, ObjectID.Ground_Grass},
                {ObjectID.Ground_Grass, ObjectID.Ground_Grass, ObjectID.Ground_Grass, ObjectID.Ground_Grass},
        };

我们加载地图循环遍历上面的数组并将它们添加到LinkedList(通过handler.addObject()),存储它们的x和y值以及tile类型,以及它们的宽度和高度。

private void loadMap(ObjectID map[][]) {
    int x = offsetX, y = offsetY;
    //int tile_width = 128, tile_height = 128;
    for(int i = 0; i < map.length; i++) {
        for(int j = map[i].length-1; j >= 0; j--) {

            handler.addObejct(new objGround(x + (j * map[i][j].getWidth() / 2) + (i * map[i][j].getWidth() / 2), y + (i * map[i][j].getBottomGroundPlane() / 2) - (j * map[i][j].getBottomGroundPlane() / 2), map[i][j]));
        }
    }
}

我们的MouseListener

        private class MouseListener extends MouseInputAdapter {
            public void mousePressed(MouseEvent e) {
                if(!dragging) {
                    getTileByXY(e.getX(), e.getY());
                }
            }

public void mouseDragged(MouseEvent e) {
            //System.out.println("mosue pressed");

            if(SwingUtilities.isRightMouseButton(e)) {
                if(e.getX() != mX || e.getY() != mY) {
                    dragging = true;
                    if(e.getX() < mX) {
                        offsetX -= (mX - e.getX());
                    } else {
                        offsetX += (e.getX() - mX);
                    }
                    if(e.getY() < mY) {
                        offsetY -= (mY - e.getY());
                    } else {
                        offsetY += (e.getY() - mY);
                    }

                    mX = e.getX();
                    mY = e.getY();

                    //System.out.println("ox: " + offsetX + " oy: " + offsetY);
                }
            }
        }
    ...
    }

最后我们的Tile Getter主要问题在于htx:

private void getTileByXY(int x, int y) {
        int tileW = 127;
        int tileH = 63;

        x += (tileW * 2);
        y += (tileH * 2);

        int htx = (int)(((double)(offsetX - x) / (double)tileW) + ((double)(offsetY - y) / (double)tileH) - 0.5);
        int hty = (int)(((double)(offsetY - y) / (double)tileH) - ((double)(offsetX - x) / (double)tileW) + 0.5);

        System.out.println("htx: " + htx + " hty: " + hty);
    }

2 个答案:

答案 0 :(得分:0)

尝试使用Photoshop和Excel并假设您的原点位于左上角,您的offsetX是最左侧单元格的左侧,而您的offsetY是最左侧单元格的底部:

int htx = (int)(((double)(x - offsetX) / (double)tileW) - ((double)(offsetY - y) / (double)tileH) + 0.5);
int hty = (int)(((double)(offsetY - y) / (double)tileH) + ((double)(x - offsetX) / (double)tileW) - 0.5);

答案 1 :(得分:0)

问题是我正在将tileW * 2和tileH * 2添加到x和y。这是我最初发现的例子。评论它们会修复它,现在瓷砖会返回正确的值!

//x += (tileW * 2);
//y += (tileH * 2);

int htx = (int)(((double)(x - offsetX) / (double)tileW) + ((double)(y - offsetY) / (double)tileH) - 0.5);
int hty = (int)(((double)(offsetY - y) / (double)tileH) - ((double)(offsetX - x) / (double)tileW) + 0.5);