0

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.

Madz
  • 287
  • 3
  • 14
  • I'm not sure I understand, but if you want to convert an `x` and `y` pair back into `I` and `J`, `(2 * x + 4 * y) / (2 * cubeSize) = J`. Then it's simple to calculate `I` too. – Weak to Enuma Elish Nov 23 '15 at 21:49
  • I have my cursor coordinates(x,y), and i want to convert them in isometric coordinates(something like 0, 1 for example) if my click is on the map. Then, i want to convert (I,J) in the numero of my cube in the std::vector (named cs here) Sorry for broken english, it was hard for me to explain all of this. I tried your calculs, it didn't work, i don't get isometric coordinates. – Madz Nov 23 '15 at 21:51
  • Have you took a look at https://en.wikipedia.org/wiki/Isometric_graphics_in_video_games_and_pixel_art ? – Bob__ Nov 23 '15 at 21:54
  • I don't use tiles, but 4 points of vertex for the 6 sides of my cubes. – Madz Nov 23 '15 at 21:56
  • All I did was reverse the equation you had in the beginning. If that's not what you intended, I don't know what to calculate. – Weak to Enuma Elish Nov 23 '15 at 21:57
  • I don't want to reverse the equation, i want to calculate for an x and y, in wich cube they are(the I-J coordinates) – Madz Nov 23 '15 at 22:02
  • And how exactly do you take care of the z parts of your cubes coordinates? – Bob__ Nov 23 '15 at 22:10
  • When i display my cubes or i do getY() -> y - (z * (cubeSize/2)). Thanks for free downvote. I spent days on this problem... and the whole day to clarify my post. Sorry to no be english fluent.... – Madz Nov 23 '15 at 22:35
  • Once you have transformed the coordinate of the vertex of the cube into screen coordinates I and J, you can store them for each face and find out for every position of the mouse what face is. – Bob__ Nov 23 '15 at 22:49
  • Each cube is associated with the pair (x,y). Are these the *minimum* x and y of the cube face, or the *maximum*, or the *center*, or what? – Beta Nov 23 '15 at 22:55
  • It's same as a classic 2D isometric tile system. It's the start of the bouding box. (sorry if i'm not clear >_<). Then, i use the classic isometric formula to rotate all of this and calculate each points. So there is no I,J for the points. – Madz Nov 23 '15 at 22:57
  • I edited the whole post, i think i have now more clear ideas about what i want. I deleted all not needed informations. – Madz Nov 24 '15 at 02:18
  • So the question basically is “how to write a ray caster”? Such questions are a bit broad, and are generally considered off-topic for StackOverflow – roeland Nov 24 '15 at 02:45
  • A ray what ??? I'm sorry, i don't know why i'm off-topic. I just want to select a cube with a left click, and for that i need formulas wich i can't find by myself. – Madz Nov 24 '15 at 02:46
  • I took a look on wikipedia, it's like it's a 3D technique, i'm using 2D here. I just fake the 3D using vertex, but it's still 2D. – Madz Nov 24 '15 at 02:48
  • @Madz Depends. If a few cubes overlap, do you want to get the cube which is “on top” of the others? – roeland Nov 24 '15 at 02:50
  • @roeland I'll do something like this with all those formulas : `maps[currentMapID].layers()[currentLayerID].cubes()[getCubeByID(cursorX, cursorY)].setTexture(numTexture);` currentLayerID will always be the layer at the top of all layers. – Madz Nov 24 '15 at 03:03
  • So (1) you're absolutely sure that no 2 cubes in a layer will overlap in image space? Think about that for a while, what if you click in an area where 2 cubes overlap. And (2) just implement a simple for loop. There is no magic formula. – roeland Nov 24 '15 at 03:08
  • I'm sure, the formula to calcul coordinates give me the coordinates for the upper side of my cube, the upper sides don't overlap beetween each others. (I just fake 3D with vertex, but i still use 2D method). I'm sure if i got those 3 formulas it will work perfectly. PS: I can get the coordinates of the side of my cube i want. – Madz Nov 24 '15 at 03:12
  • I would add something : math are not magic, i know, but numbers in an algorythm are always related, so i know those formulas exist. – Madz Nov 24 '15 at 03:25
  • @roeland I did a wrong use of your reverse formula, this is working thanks. I'll edit my post. – Madz Nov 24 '15 at 05:52

1 Answers1

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
MvG
  • 57,380
  • 22
  • 148
  • 276
  • It's not so easy, he has an isometric like drawing of 3d cubes (he posted some pictures, many version of this question ago). Basically you have to click on hexagonal tiles. – Bob__ Nov 24 '15 at 12:11
  • @Bob__: It kind of would have helped to see that picture in the question… Thanks to your comment I found [this picture](http://i.stack.imgur.com/9Fv2u.png) from [revision 7](http://stackoverflow.com/revisions/33881027/7). Adapting my answer to that shouldn't be too hard. I guess though that I'll leave my answer as it stands now until the question gets edited in such a way that its requirements can be found without digging through the revision history. OP, feel free to comment if I miss that event. I'm currently seeing revision 19. – MvG Nov 24 '15 at 12:22
  • @MvG this is totally what i want, really thanks to you ! but the index (I + J * size;) don't work, what do you mean by map size ? in pixel ? in blocks ? It's only working for the tile 0, 0. – Madz Nov 24 '15 at 14:09
  • @Madz: The `mapSize` I wrote is the same you used as the upper limit in your inner loop, where you wrote `I < mapSize`. Incrementing `J`, the variable from the outer loop, by one is equivalent to taking `mapSize` steps in the inner loop, i.e. one full execution of the loop. – MvG Nov 24 '15 at 14:22
  • And for a reason i ignore, you resolved my other problem : http://stackoverflow.com/questions/33852194/sfml-c-sftexture-applied-in-a-wrong-way Maybe be math are really magic after all ha ha ha O_O – Madz Nov 24 '15 at 14:35