Need some help with an algorithm

This topic is 2141 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Hi, i have a 2D vector with the tiles of a map

vector<vector <int> > tiles;

2nd dimension is like this

{ coord_x, coord_y, graphic_id }

Now I want to change some of the base tiles according where user clicks on the map.

Problem is tile coords are all 32 multiple, and clicks can happen anywhere ofc.

My idea is to find the nearest 32 multiple and then iterate over the vector and find which tile match.

What do you guys think? Is there a more efficient way to code this?

Share on other sites

The description of the question is hard to understand but I am going to take a guess and assume you mean:

You have a 2D grid of tiles on a board, each tile being 32×32, and you can click anywhere on the board.  You want to know which tile is being clicked.

std::vector is not helping you.  Use a 2D array.

int tiles[96][64]; // Board is 64 tiles wide and 96 tiles tall.  You can make it any size you want.

Click.Pos = 459×761.

Clicked tile X = 459 / 32 = 14.

Clicked tile Y = 761 / 32 = 23.

Clicked tile = tiles[23][14].

L. Spiro

Share on other sites

Problem is tile coords are all 32 multiple, and clicks can happen anywhere ofc.

My idea is to find the nearest 32 multiple and then iterate over the vector and find which tile match.

Sounds like you want some kind of snapping. Why not use integer division?

Share on other sites

Sorry, forgot to mention...

Map is isometric, I'm rendering it as a diamond.

Share on other sites
First-off the red numbers are wrong.
[0,0] = 0
[0,1] = 1
[0,2] = 2
etc.

And not only are they wrong, they are meaningless. The black numbers are what you actually store (in tiles[5][5]).

The only difference between this and what I already gave you is that you need to convert the mouse coordinates into rectangular coordinates first, which is simple.

The origin is at the top of square [0,0]. That is X = 0, Y = 0.
The diamond increases 2 pixels along the X (as you go from the origin to the right of the screen, every 2 pixels right you go the diamond goes down the screen 1 pixel).

So start by getting the Y.
Let’s say the origin is at screen coordinate [300,25] and we click at [338,70] (with screen coordinates [0,0] being the upper-left corner).

Get the Y first.
The X coordinate is 38 pixels to the right of the origin, and for every 2 horizontal pixels it moves vertically down by 1, so 38 ÷ 2 = 19.
The adjusted Y coordinate is 29.

Now get the X.
The Y is 45 units below the board origin, and for every pixel down, 2 pixels are going to the right. So 45 × 2 = 90, which is the adjusted X.

int tiles[5][5]; // Board is 5 tiles wide and 5 tiles tall. You can make it any size you want.

Click.Pos = 19×90.

Clicked tile X = 19 / 32 = 0.
Clicked tile Y = 90 / 32 = 2.

Clicked tile = tiles[2][0].

L. Spiro Edited by L. Spiro

Share on other sites

Thanks, will try it.

The image is one I just found through google,

Share on other sites

I have the y part but can't get the x.

Let’s say the origin is at screen coordinate [300,25]

...300.25 is the top corner of the diamond, right?

If I understand rightly your approach will not work for me, since I'm rendering the tiles in the order as shown in the image (the red numbers).

Edited by Kevin Chuca

Share on other sites

If I understand rightly your approach will not work for me, since I'm rendering the tiles in the order as shown in the image (the red numbers).

That sounds like a self-imposed limit. Unless you have a good reason for that (and I doubt one exists), it should be a 2-dimensional array via the black numbers.

300.25 is the top corner of the diamond, right?

It is.

L. Spiro

• Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 16
• 9
• 15
• 9
• 11