2D等距引擎-数学问题-多维数据集选择-菱形形状图 [英] 2D isometric engine - Math problems - Cube selection - diamond shape map
问题描述
当我创建一个包含cube
对象(其中是我的一类)的std::vector
层时,我会计算坐标:
I calculate my coordinates when i create a layer with a std::vector
, filled with cube
objects(wich is a class of mine):
for(int J = 0; J < mapSize; J++)
{
for(int I = 0; I < mapSize; I++)
{
x = (J - I) * (cubeSize/2);
y = (J + I) * (cubeSize/4);
c = new cube(cubeSize, x, y, z, I, J);
cs.push_back(*c);
}
}
我想这样做:cs[getCubeByID(mouseX, mouseY)].setTexture(...);
使用示例:I-J [0, 0]
中的多维数据集在cubes数组中具有number 0
.如果我click on 0,0
我知道这个电话号码.
Example of use: The cube in I-J [0, 0]
have the number 0
in the cubes array. if i click on 0,0
i got this number.
我们给了我公式,让J或I在注释中带有一对x,y,非常感谢. 我只需要像我给的例子一样将这对I-J转换为数组的条目号即可.
We gave me the formula to get a J or a I with a pair of x,y in the comments, thanks a lot. I only need to convert this pair of I-J to the entry number of my array like the example i gave.
我尝试过:int entry = (J - 1) * size + (I - 1);
并且所选的多维数据集与我想要的多维数据集相距不远,但仍不是正确的公式.模块化算术可以解决我的问题,但是我不知道它是如何工作的.
I tried : int entry = (J - 1) * size + (I - 1);
and the selected cube is not so far from the one i want but still not the right formula. Modular arithmetic can fix my problem but i don't understand how it's working.
推荐答案
所以你有
x = (J - I) * (cubeSize/2);
y = (J + I) * (cubeSize/4);
,并且您想据此计算I
和J
(以及因此索引为I + J*mapSize
),对吗?这是一个由两个方程组成的线性系统.
and you want to compute I
and J
(and therefore the index which is I + J*mapSize
) from that, right? It's a linear system of two equations.
J - I = x * 2 / cubeSize
J + I = y * 4 / cubeSize
I = (y * 2 - x) / cubeSize
J = (y * 2 + x) / cubeSize
这篇关于2D等距引擎-数学问题-多维数据集选择-菱形形状图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!