I calculate my coordinates when I create a layer with a std::vector
, filled with cube
objects(which 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 want to 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.
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