2D isometric engine - Math problems - Cube selection - diamond shape map

十年热恋 提交于 2019-12-08 09:27:10

问题


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);
    }
}

I wanna do this : cs[getCubeByID(mouseX, mouseY)].setTexture(...);

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.

EDIT: 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.

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.


回答1:


So you have

x = (J - I) * (cubeSize/2);
y = (J + I) * (cubeSize/4);

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


来源:https://stackoverflow.com/questions/33881027/2d-isometric-engine-math-problems-cube-selection-diamond-shape-map

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!