简体   繁体   English

从 x,y 屏幕空间坐标中查找 2D 等距网格上的列、行(将方程转换为函数)

[英]Find column, row on 2D isometric grid from x,y screen space coords (Convert equation to function)

在此处输入图像描述

I'm trying to find the row, column in a 2d isometric grid of a screen space point (x, y)我正在尝试在屏幕空间点 (x, y) 的 2d 等距网格中找到行、列

Now I pretty much know what I need to do which is find the length of the vectors in red in the pictures above and then compare it to the length of the vector that represent the bounds of the grid (which is represented by the black vectors)现在我几乎知道我需要做的是在上面的图片中找到红色向量的长度,然后将其与表示网格边界的向量的长度(由黑色向量表示)进行比较

Now I asked for help over at mathematics stack exchange to get the equation for figuring out what the parallel vectors are of a point x,y compared to the black boundary vectors.现在我在数学堆栈交换中寻求帮助,以获得计算点 x,y 与黑色边界向量相比的平行向量的方程。 Link here Length of Perpendicular/Parallel Vectors在这里链接垂直/平行向量的长度

but im having trouble converting this to a function但我无法将其转换为 function

Ideally i need enough of a function to get the length of both red vectors from three sets of points, the x,y of the end of the 2 black vectors and the point at the end of the red vectors.理想情况下,我需要足够的 function 来从三组点中获取两个红色向量的长度,即 2 个黑色向量末端的 x、y 和红色向量末端的点。

Any language is fine but ideally javascript任何语言都可以,但最好是 javascript

What you need is a base transformation:您需要的是基础转换:

Suppose the coordinates of the first black vector are (x1, x2) and the coordinates of the second vector are (y1, y2).假设第一个黑色向量的坐标是(x1,x2),第二个向量的坐标是(y1,y2)。

Therefore, finding the red vectors that get at a point (z1, z2) is equivalent to solving the following linear system:因此,找到到达点 (z1, z2) 的红色向量等价于求解以下线性系统:

x1*r1 + y1*r2 = z1
x2*r1 + y2*r2 = z2

or in matrix form:或以矩阵形式:

   A      x  =  b

/x1 y1\ |r1| = |z1|
\x2 y2/ |r2|   |z2|

          x = inverse(A)*b

For example, lets have the black vector be (2, 1) and (2, -1).例如,让黑色向量为 (2, 1) 和 (2, -1)。 The corresponding matrix A will be对应的矩阵 A 将是

2 2
1 -1

and its inverse will be它的倒数将是

1/4 1/2
1/4 -1/2

So a point (x, y) in the original coordinates will be able to be represened in the alternate base, bia the following formula:所以原坐标中的点 (x, y) 将能够在备用基础中表示,偏差如下公式:

(x, y) = (1/4 * x + 1/2 * y)*(2,1)  + (1/4 * x -1/2 * y)*(2, -1)

What exactly is the point of doing it like this?这样做到底有什么意义? Any isometric grid you display usually contains cells of equal size, so you can skip all the vector math and simply do something like:您显示的任何等距网格通常包含相同大小的单元格,因此您可以跳过所有矢量数学并简单地执行以下操作:

var xStep = 50,
    yStep = 30, // roughly matches your image

   pointX = 2*xStep,
   pointY = 0;

Basically the points on any isometric grid fall onto the intersections of a non-isometric grid.基本上,任何等距网格上的点都落在非等距网格的交点上。 Isometric grid controller:等距网格 controller:

screenPositionToIsoXY : function(o, w, h){
    var sX   = ((((o.x - this.canvas.xPosition) - this.screenOffsetX) / this.unitWidth ) * 2) >> 0,
        sY   = ((((o.y - this.canvas.yPosition) - this.screenOffsetY) / this.unitHeight) * 2) >> 0,
        isoX = ((sX + sY - this.cols) / 2) >> 0,
        isoY = (((-1 + this.cols) - (sX - sY)) / 2) >> 0;

    // isoX = ((sX + sY) / isoGrid.width) - 1
    // isoY = ((-2 + isoGrid.width) - sX - sY) / 2

    return $.extend(o, {
        isoX : Math.constrain(isoX, 0, this.cols - (w||0)),
        isoY : Math.constrain(isoY, 0, this.rows - (h||0))
    });
},

// ...

isoToUnitGrid : function(isoX, isoY){
    var offset = this.grid.offset(),
        isoX   = $.uD(isoX) ? this.isoX : isoX,
        isoY   = $.uD(isoY) ? this.isoY : isoY;

    return {
        x : (offset.x + (this.grid.unitWidth  / 2) * (this.grid.rows - this.isoWidth + isoX - isoY)) >> 0,
        y : (offset.y + (this.grid.unitHeight / 2) * (isoX + isoY)) >> 0
    };
},

Okay so with the help of other answers (sorry guys neither quite provided the answer i was after)好的,在其他答案的帮助下(对不起,伙计们都没有提供我想要的答案)

I present my function for finding the grid position on an iso 2d grid using a world x,y coordinate where the world x,y is an offset screen space coord.我展示了我的 function,用于使用世界 x,y 坐标在 iso 2d 网格上查找网格 position,其中世界 x,y 是偏移屏幕空间坐标。

WorldPosToGridPos: function(iPosX, iPosY){
    var d = (this.mcBoundaryVectors.upper.x * this.mcBoundaryVectors.lower.y) - (this.mcBoundaryVectors.upper.y * this.mcBoundaryVectors.lower.x);

    var a = ((iPosX * this.mcBoundaryVectors.lower.y) - (this.mcBoundaryVectors.lower.x * iPosY)) / d;
    var b = ((this.mcBoundaryVectors.upper.x * iPosY) - (iPosX * this.mcBoundaryVectors.upper.y)) / d;

    var cParaUpperVec = new Vector2(a * this.mcBoundaryVectors.upper.x, a * this.mcBoundaryVectors.upper.y);
    var cParaLowerVec = new Vector2(b * this.mcBoundaryVectors.lower.x, b * this.mcBoundaryVectors.lower.y);

    var iGridWidth = 40;
    var iGridHeight = 40;

    var iGridX = Math.floor((cParaLowerVec.length() / this.mcBoundaryVectors.lower.length()) * iGridWidth);
    var iGridY = Math.floor((cParaUpperVec.length() / this.mcBoundaryVectors.upper.length()) * iGridHeight);

    return {gridX: iGridX, gridY: iGridY};
},

The first line is best done once in an init function or similar to save doing the same calculation over and over, I just included it for completeness.第一行最好在 init function 或类似文件中完成一次,以保存一遍又一遍地执行相同的计算,我只是为了完整性而将其包括在内。

The mcBoundaryVectors are two vectors defining the outer limits of the x and y axis of the isometric grid (The black vectors shown in the picture above). mcBoundaryVectors 是定义等距网格的 x 和 y 轴的外部边界的两个向量(上图中显示的黑色向量)。

Hope this helps anyone else in the future希望这对将来的其他人有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM