Roll20 uses cookies to improve your experience on our site. Cookies enable you to enjoy certain features, social sharing functionality, and tailor message and display ads to your interests on our site and others. They also help us understand how our site is being used. By continuing to use our site, you consent to our use of cookies. Update your cookie preferences .
×
Create a free account

The Rangefinder tool

1410145183

Edited 1410145218
Is there API access to, or functions duplicating the output of, the built-in Rangefinder? I ask because I am using a vertical hex grid and I need to be able to calculate the distance from attacker to target to check for melee weapon reach and ranged weapon ranged penalties. Due to the funk-a-delic way the hex grid is handled, the actual distance-in-pixels can vary widely, and I am having trouble converting it cleanly to "units" and then to "feet". I am sure I can't be the first person to need to do this on a hex grid...
1410152251
The Aaron
Roll20 Production Team
API Scripter
Is suggest just taking the Math.floor() of the calculated distance. That should give a close approximation of the actual distance.
The distance formula is more technically accurate than the range finder, then... perhaps I should tell my Players not to rely on the RangeFinder? Hmm... that sux, that looked very convenient, for calculating ranges and movement. I wish I could change the Settings on that RangeFinder tool to use that method instead... «puzzled»
1410152979
The Aaron
Roll20 Production Team
API Scripter
I'm sure the range finder is just counting hexes and multiplying by unit size. I'll have to play with that tomorrow, but I bet you can come up with a formula to get the same measurement. How does the range finder do with measuring the distance to the hex with the C in it?
Here is a better illustration of the problem. You're right - the rangefinder is counting hexes (recursion?) and coming up with 25 feet to orc B. (Five hexes passed through.) But, using the algebraic distance formula, every orc in this example is at a distance of 20 feet...
1410165578

Edited 1410165924
No need for recursion. Just measure the distance at a 60 degree angle up or down to the point where the y coordinates match. If that point is outside of where the target is (i.e. if the target is within the 60 degree arc above or below the origin), then you're done. Otherwise add on the distance right or left from there to the point where the x coordinates match. Divide the total distance (in pixels) by the hex size (the small way, so on the order of 75-76px in your example; I'll leave it as an exercise to the reader to get a more exact number by measuring the size of a large number of hexes, and to add in appropriate rounding to make sure things work out over even larger distances than that). In Javascript-esque pseudocode: function hexDist(x1, y1, x2, y2){ assert(x1 <= x2) // assume p1 left of p2; could swap if not vertical_distance = abs(y2 - y1) // distance up or down from p1 to p2 -- the long leg of the 30-60-90 right triangle total_distance = vertical_distance * 2 / sqrt(3) // divide long leg by sqrt(3)/2 to get hypotenuse x_mid = x1 + (total_distance / 2) // divide hypotenuse by 2 to get short leg: horizontal distance if (x2 > x_mid){ // if x2 <= x_mid, then the number of hexes from p1 to p2 is the same as if p2 were at the end of the diagonal total_distance += x2 - x_mid // if x2 > x_mid, then p2 is to the right of the diagonal, so add the extra hexes } return round(total_distance / 75) }
OK, thanks for the code! But, uh, I think I need to use the algebraic distance formula instead (the "real" distance, not hex-based), because it's more accurate and that is how the auras work, my spells already work, etc. I will just indicate to my Players that they are not to rely on the built-in rangefinder, and give them a Macro button to use instead. Thanks!