Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>One thing to try would be converting from isometric coordinates to a square grid coordinate set for all calculations. </p> <p>Say that 0,0 stays the root of the map. 0,1 stays the same, 1,2 becomes 0,2; 1,3 becomes 0,3; 2,3 becomes 1,4; 3,3 becomes 2,5; 0,2 becomes -1,1; etc. This puts you back into a square grid such that the coordinates and heuristics should work again.</p> <p>Y coordinate becomes Y+sourceX offset (3,3 is at x=2; so becomes 2,5); finding sourceX mathmatically is proving itself more difficult.</p> <p>[Stream of consciousness; ignore] Isometric coordinates at Y=0 are accurate for source X. So, to calculate source X you need to 'move left/up Y times' which should net a change of Y/2; rounded down, in the X coordinate.... roughly suggesting that the square coordinates would be:</p> <pre><code>sourceX = X - Y/2 sourceY = Y + sourceX </code></pre> <p>Where sourceX and sourceY are the coordinates in a normal square grid; and Y/2 is integer arithmetic/rounded down.</p> <p>So, in theory, this becomes:</p> <pre><code>double DistanceToEnd(Point at, Point end) { Point squareStart = squarify(at); Point squareEnd = squarify(end); int dx=squareStart.X-squareEnd.X; int dy=squareStart.Y-squareEnd.Y; return Math.Sqrt(dx*dx+dy*dy); } Point squarify(Point p1) { return new Point(p1.X-p1.Y/2, p1.Y+(p1.X-p1.Y/2)); } </code></pre> <p><strong>Update</strong> based on new bits of question:</p> <p>Assuming that you are trying to get distance(3,2; 3,3) &lt; (distance(2,3; 3,3) = distance(3,1; 3,3)); the following should work: (translated from C#; typos not guaranteed to be non present)</p> <pre><code>inline int Pathfinder::calculateDistanceEstimate(const CellCoord&amp; coord) const { int cx=coord.x - coord.y/2; int cy=coord.y + cx; int gx=goal-&gt;position.x - goal-&gt;position.y/2; int gy=goal-&gt;position.y + gx; int diagonal = std::min(abs(cx-gx), abs(cy-gy)); int straight = (abs(cx-gx) + abs(cy-gy)); return 14 * diagonal + 10 * (straight - 2 * diagonal); } </code></pre> <p><strong>EDIT:</strong> Fixed horrible typo.... again.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload