Skip to main content
gamedev.net
🔒 Locked

range based algorithms

Started by Grey_Havoc 2 replies 1.1k views
Original Post
Grey_Havoc
Grey_Havoc
Hey, I'm making a tile-based game that has units on x,y coords. Some units have range based attacks. Is there an easy way of determining whether a unit is in range of another unit based on their x,y coords? Thanks!
http://www.brandonbarnes.us
ferr
ferr
Manhattan distance

The taxicab distance between two points in a Euclidean space with fixed Cartesian coordinate system is the sum of the lengths of the projections of the line segment between the points onto the coordinate axes. For example, in the plane, the taxicab distance between the point P1 with coordinates (x1, y1) and the point P2 at (x2, y2) is |x1 - x2| + |y1 - y2|.

so, say you've got one person at [2,3] and another at [6,10], the distance between the two would be |2 - 6| + |3 - 10| = 11. If 11 > rangeMax then OutOfRange.

this formula can also be useful for path finding.

[Edited by - ferr on July 4, 2007 12:32:31 AM]
Nypyren
Nypyren
Squared Distance works well for less-than and greater-than checks, but not for printing out the range (since the distance stays squared). The valid "in-range" zone will be more circular than the Manhattan Distance (which is more like a diamond).

If you want to convert squared distance to "real" distance, then just take the square root of (x*x+y*y)

int diffX = x1 - x2;int diffY = y1 - y2;if (diffX*diffX + diffY*diffY < range*range){  // it's within range}
Grey_Havoc
Grey_Havoc
Wow! great responses! Thanks a bunch!
http://www.brandonbarnes.us

Topic Locked

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

Sign in to reply to this topic.