Jump to content

  • Log In with Google      Sign In   
  • Create Account


Map Coordinates


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 BuckyOHare486   Members   -  Reputation: 129

Like
0Likes
Like

Posted 22 August 2012 - 06:20 AM

Hi,

I've been puzzling over how to convert X,Y coordinates of a 2D map into an ID and back again. I want to be able to do this so that I can determine which cell on a map grid has been clicked but also display the coordinates to the user as they'd expect them. I intend to use a 10 x 10 grid map to begin with and I know that X is "how many from left" and Y is "how many from top" but I just can't figure out the formula for converting them. Have any of you done this before? Also, what is the technical name for this site of grid-related mathematics?

Sponsor:

#2 Juliean   GDNet+   -  Reputation: 2453

Like
1Likes
Like

Posted 22 August 2012 - 06:34 AM

//ID to x+y
x = ID/width
y = ID%width
//x+y to ID
ID = x+y*width

This is all you need. width is the width of your map, in that case 10.

Edited by The King2, 22 August 2012 - 06:35 AM.


#3 BuckyOHare486   Members   -  Reputation: 129

Like
0Likes
Like

Posted 22 August 2012 - 08:18 AM

[code=auto:0]
//ID to x+y
x = ID/width


Leaves a remainder so I am flooring it but for ID '1' which should be coordinates 0,0 comes out as 0,1 while ID '34' is coming out as 3,4 but should be 3,3. I know I'm doing something wrong but can't see what. I'm testing this in Excel with the following formulas:

X = FLOOR([@ID]/10, 1)
Y = MOD([@ID], 10)

The grid is a 10 x 10 area and I have labelled both axis with 0 through to 10.

#4 Juliean   GDNet+   -  Reputation: 2453

Like
1Likes
Like

Posted 22 August 2012 - 09:31 AM

Ok, so if your ID starts at 1, you need to say @ID-1 before applying the formula, and when reversing, you afterwards need to add 1. I'd suggest having "0" as the first ID anyway..

Edited by The King2, 22 August 2012 - 09:31 AM.


#5 Exessuz   Members   -  Reputation: 141

Like
2Likes
Like

Posted 23 August 2012 - 01:04 PM

if you want to make a 10 x 10 map , lets say that each tile is 32 x 32 px

i used to make a linear array holding all the tile ids

int width = 32;

short map[100]; -> 10x10

supposing that you are drawing Y first

for(int x =0 ; x < 10; x++){
for(int y =0 ; y < 10; y++){

RENDER()
}
}

int x = click.x / 32;
int y = click.y / 32;

then the tile is

int id = x*width + y;

map[id].selected;

#6 Bacterius   Crossbones+   -  Reputation: 8658

Like
0Likes
Like

Posted 23 August 2012 - 10:28 PM


[code=auto:0]
//ID to x+y
x = ID/width


Leaves a remainder so I am flooring it but for ID '1' which should be coordinates 0,0 comes out as 0,1 while ID '34' is coming out as 3,4 but should be 3,3. I know I'm doing something wrong but can't see what. I'm testing this in Excel with the following formulas:

X = FLOOR([@ID]/10, 1)
Y = MOD([@ID], 10)

The grid is a 10 x 10 area and I have labelled both axis with 0 through to 10.

You have to use integer math here, e.g. integer division (always rounds down, so flooring is correct barring floating-point inaccuracies) and integer remainder (= modulo operation). This assumes your array starts at 0 and (0, 0) respectively, so offset as needed.

Edited by Bacterius, 23 August 2012 - 10:30 PM.

The slowsort algorithm is a perfect illustration of the multiply and surrender paradigm, which is perhaps the single most important paradigm in the development of reluctant algorithms. The basic multiply and surrender strategy consists in replacing the problem at hand by two or more subproblems, each slightly simpler than the original, and continue multiplying subproblems and subsubproblems recursively in this fashion as long as possible. At some point the subproblems will all become so simple that their solution can no longer be postponed, and we will have to surrender. Experience shows that, in most cases, by the time this point is reached the total work will be substantially higher than what could have been wasted by a more direct approach.

 

- Pessimal Algorithms and Simplexity Analysis





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS