Square Grid With Trig

Because the disadvantages of the Octogrid make it nearly impractical to implement, I next went back to thinking how to make a square grid work realistically with 8 directions. Shortly after, I decided that perhaps simple formulas could be developed to allow full 360-degree simulation. Time to recall high school trigonometry.

After a lot of tinkering and calculations by spreadsheet, I've developed a template and generic formula for determining if a target square on a square grid is "within range" of a source square. I don't yet have a slick name for the system, beyond the descriptive Square Grid enhanced with trigonometry or simply Square Grid With Trig.

This chart shows which squares are reachable from the white "source square" for range values 1-10. Only angles 0-45 degrees are depicted; flipping and/or rotating the chart allows it to be applied to any case within 360 degrees.
sqgrid_extents_chart.pngFor ranges greater than 10 (or possibly fractional), a calculator needs to be used with the following formula. The following two diagrams are mathematically equivalent, designed to show how cell counting can provide the inputs needed for the formula. As with the chart above, flipping and/or rotating these depictions allow for any situation.



What's important to remember is that when counting the cells (squares) from the source to the target, the bigger number (i.e. the longer side of the right triangle that is formed) is assigned to $m$, and the smaller is assigned to $n$.

To determine which cell ($m^{\prime}, n^{\prime}$) along the ray in the direction of the target from the source (e.g. the red arrow in the diagrams above) is the farthest extent for a range value of $k$, use the following formulas:

\begin{align} m^{\prime} = k \left( \cos \left( \arctan \left( { n \over m} \right) \right) \right) \text{, rounded to a whole number; along the m axis} \end{align}
\begin{align} n^{\prime} = k \left( \sin \left( \arctan \left( { n \over m} \right) \right) \right) \text{, rounded to a whole number; along the n axis} \end{align}
\begin{align} \text{For m=n, } m^{\prime} = n^{\prime} = k { \sqrt{2} \over 2 } \text{, rounded to a whole number} \end{align}

If $m^{\prime} > m$ or $n^{\prime} > n$, then the target is out of range.
If $m^{\prime} \leq m$ and $n^{\prime} \leq n$, then the target is within range.




The next thing to figure is arbitrary line-of-sight formulas to determine if an occupied square is "in the way" between the source and the target.

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License