Skip to main content
GameDev.net gamedev.net
🔒 Locked

X,Y map Question

Started by Stormfury Oct 10, 2000 at 12:49 PM 2 replies 250+ views
Original Post
Stormfury
Stormfury
Hello, I am in the process of developing a strategy game which uses a map array and 2D tiles as the playing board. I have an array as follows. dim array(x,y,z) where X is the X and Y is the Y. Z is the data I want to store for each cell. For a map that is x=5 and y=5. 0,0 would be the top left corner and 4,4 would be the bottom right. I know there is most likely a math formula to calculate this but I have not figure it out yet: How do you calculate the # of cells from 1 cell to another with the following assumptions. #1 Any cell can be travelled over. #2 Any direction is ok. So an army going from 0,0 to 4,4 would travel over 1,1 --> 2,2 --> 3,3 --> 4,4 This should come out as 4 Cells travelled. Thanks, Stormfury
goir
goir
This is just from the top of my head, but here it goes:

dist = ( abs(x1-x2) + abs(y1-y2) + abs( abs(x1-x2) - abs(y1-y2) ) ) / 2 + 1

The +1 at the end is something I don''t understand because for my moving 1,1 --> 2,2 --> 3,3 --> 4,4 would be 3 moves, but if you want it to be the number of squares you travled over, just add 1.


this forumla would give (with the 1,1 --> 4,4 move):

( abs(1-4) + abs(1-4) + abs( (1-4) - (1-4) )) / 2 + 1 =
(abs(-3) + abs(-3) + abs( (-3) - (-3) )) / 2 + 1 =
(3 + 3 + 0) / 2 + 1 = 4


I havn''t checked it too much if I say so
Stormfury
Stormfury
I plugged your formula in and it worked like a champ! I did not need the +1. My example included 0,0 as a hex which you may not have seen. That would explain the 4 when you thought there should be 3.

At any rate, I plugged it into my app and it is working.

Thanks for your help.

Storm
goir
goir
wow, I didn''t expect it to work I only wrote down something that could be reasonable and tested it two times

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.