hex-math

Compute hex grid coordinates, distances, paths, and line-of-sight.

1|Updated Dec 26, 2025
One-click install
npx skills add https://github.com/chaoming/ww2-flutter-game --skill hex-math
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: hex-math
Source: https://github.com/chaoming/ww2-flutter-game/tree/main/.claude/skills/hex-math
Command: npx skills add https://github.com/chaoming/ww2-flutter-game --skill hex-math

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This Skill provides a concise reference for hexagonal grid mathematics, empowering developers and game designers to implement movement, range calculations, and line-of-sight on hex grids without hunting through external docs.

Core Features & Use Cases

  • Coordinate Systems: Understand cube and offset coordinate representations for hex grids.
  • Key Algorithms: Distance, neighbors, hex-to-pixel conversions, and line drawing for line-of-sight.
  • Use Case: Implement a hex-based pathfinding module for a strategy game.

Quick Start

Use this skill to implement core hex-grid math: distance, neighbors, and line-of-sight calculations for your hex grid.

Frequently Asked Questions about hex-math

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I calculate distance between two hexagons on a grid?

Hex grid distance is calculated using cube coordinates, where the distance equals the maximum of the absolute differences across all three axes divided by two. This works for any hex orientation and is more efficient than pixel-based distance for pathfinding and range queries.

What coordinate systems should I use for hex grids?

Cube coordinates and offset coordinates are the two main systems. Cube coordinates simplify distance and neighbor calculations; offset coordinates (odd-q variant) map efficiently to 2D arrays. Choose based on whether you prioritize math elegance or storage efficiency.

How do I find all hexagons within a certain range on a hex grid?

Range queries iterate through cube coordinates within a distance threshold, filtering by the max-of-absolute-differences rule. This produces all hexagons reachable in N moves, essential for movement UI, ability targeting, and fog-of-war in strategy games.

How do I implement line-of-sight between two hexagons?

Line-of-sight uses line-drawing algorithms on cube coordinates to trace a path between two hexes, checking for obstacles along the way. This determines visibility for AI perception, fog reveal, and attack targeting in hex-based games.

Can I convert between cube and offset hex coordinates?

Yes. Cube-to-offset and offset-to-cube conversions are bidirectional transformations, with odd-q offset being the standard variant. Use these to switch between coordinate systems depending on whether your algorithm needs math simplicity or array storage.

What's the best way to find neighboring hexagons?

Neighbor generation in cube coordinates adds fixed direction vectors to a hex's position, producing six adjacent hexes. This is faster and more elegant than distance-based lookup and forms the foundation for pathfinding and movement validation.