Distance calculations

Started by
4 comments, last by mengo76 22 years, 2 months ago
hi all... what are the most common methods of measuring distances between two tiles in isometric maps? first of all, would you count the number of "moves" it takes to go from a to b? or would you use the pythagorean theorem, and would the base and height of the triangle be the horizontal/vertical or the two diagonals? then, what would the mathematical/programming algorithim be to apply these methods to the even-row-odd-row coordinate system that is used to implement isometric maps? thanks for the help ~Mengo.
~Mengo.
Advertisement
it depends on what you are doing... if the game is turn based, and the player is restricted to moving his "pieces" one tile at a time, use the number of tiles needed to move there... but if he can move freely (i.e. real-time) across the tiles, use the pythagorean theorem...

--- krez (krezisback@aol.com)
--- krez ([email="krez_AT_optonline_DOT_net"]krez_AT_optonline_DOT_net[/email])
let me explain a little more my situation

by the way this is turn based, but that shouldn't matter, my question refers to a static state

let me explain my question using a regular cartesian plane (vertical/horizontal grid) which is what i am comfortable width

lets say you have three units in the following squares:
A: (0,0)
B: (6,0)
C: (5,5)

which unit is closest to A: B or C?

if you count the number of moves, A to B is 6, and A to C is 5, so C wins.

if you use pyth, A to B is still 6, but A to C is sqrt(25+25)=7, so B wins.

so my question is, what are the pluses and minuses of using each method? in an isometric grid of course...


~Mengo.

EDIT: stupid ": (" got interpreted as ""

Edited by - mengo76 on January 15, 2002 4:22:39 PM
~Mengo.
again, it depends on what ou want this information for.
with your example, if you want one of your units to travel to A, then use the tile-based one (as far as turn-based movement goes). if, however, you want to find out a real distance (i.e. either B or C should shoot an arrow at A, and the closer they are the better the shot) then use the real distance (pythagorean). you might have to implement both methods in your game, because both are useful for different things.
if you are using diamond-shaped tiles, the math should be the same because it is a cartesian grid rotated 45 degrees (they look shorter up-down than they do left-right, but that is for perspective; the distances should still be equal).
if you are using hex-shaped tiles, i have no clue, sorry

--- krez (krezisback@aol.com)
--- krez ([email="krez_AT_optonline_DOT_net"]krez_AT_optonline_DOT_net[/email])
quote:Original post by krez
again, it depends on what you want this information for.
you might have to implement both methods in your game, because both are useful for different things.

seems true enuf
quote:
if you are using hex-shaped tiles, i have no clue, sorry

the thing is, you can also calculate the actual distance, using pixels/points, rather than tiles. this would depend on whether you move in whole tiles or more freely.
so if we''re talking about the latter,
when using diamond tiles (tiles, not maps), you would use the point which is the center of the tile, right? that is, the pythagorean distance would be between the centers of 2 tiles, right? (top left corner + tile_width\height /2)
why not use the same with hexagonal tiles?
or maybe use the point which is where your character is standing, whatever you need to do..

if using whole tiles, perform a tile-walking between the 2 tiles just as you would - you know..for instance eastward movement is (x+1,y-1) on a diamond map.
so every increase/decrease in X or Y is basically moving to another tile, right? so count how many tiles you walk before getting from 1 place to the other. just like you did, to know which is shorter between (0,0)->(6,0) and (0,0)->(5,5).
BUT, this is still up to you to decide. what do i mean?
if i''m not mistaken, x-com:apocalypse charged you more "TUs" when you made a north/south/east/west movement, than when you made a northeast/northwest/southeast/southwest movement, because like i said above every addition to x/y is another tile to walk, even if it didn''t show on the screen.
if you go by that, then moving to tile (5,5) would cost double the movement to tile (5,0) or (0,5).
if moving south costs the same as moving southeast then moving to (3,3) is the same as moving to (3,2) - the shortest distance to both is still 3 steps (2 south and 1 southeast). if you count south as 2 (southeast + southwest), then moving to (3,2) will cost less, because ultimately it''s 5 steps whereas (3,3) is 6. keep that in mind..

in hexagonal tiles, referring again only to a diamond map, once again for example east movement is (x+1,y-1). the coordinate system remains the same, thus the calculation should also be the same.

conclusion? the only thing that changes is how you draw it :D
count in your own way how many tiles you walk to each target and compare which is closest (or what %to-hit you get, or whatever it is you''re doing with this distance calculation in the first place. don''t forget the %hit thing is very much affected by the whole-tile or free movement thing. like in fallout:tactics where every milimeter can increase your %hit.)

quote: Original quote by kelly bundy
It''s as easy as 1-2-C...


//Demiurge
Make something idiot proof, and someone will make a better idiot..
//DemiurgeMake something idiot proof, and someone will make a better idiot..
If you can only move north, south, east, and est, then use manhattan distance.

Dm = |x2-x1| + |y2-y1|

This topic is closed to new replies.

Advertisement