R&D · UE4 · Procedural Gen · 2022

Hex Grid Island Builder

Hex Grid Island Builder

A procedural hex-grid island builder in Unreal Engine. Perlin noise determines land vs water tile placement; offset rows give the classic flat-top hex layout. Players use an RTS camera to navigate, place tiles, and watch their island grow in real time.

Unreal Engine 4 C++ Perlin Noise Hex Grid Math RTS Camera
What I Built
  • Hex grid layout — 2-axis formula — offset-row coordinate system places tiles at x * HexWidth + (y%2 * HexWidth/2) and y * HexHeight * 0.75, derived analytically from 2 grid axes with no pre-baked data.
  • Procedural terrain — 1 threshold parameter — Perlin noise sampled at configurable scale; noise value compared against 1 configurable WaterThreshold — above → land tile, below → water tile.
  • Interactive placement — 1-click line trace1 line trace under cursor selects the target empty hex coord; spawns the chosen tile class at the correct world position with zero engineering dependency.
  • RTS camera — 3 behaviors — spring-arm zoom with scroll wheel, 4-directional edge-pan when cursor nears screen border, constrained within island bounds; 3 distinct behaviors in a single camera actor.
  • Tile variety — 5 tile types — land, water, forest, mountain, and village; 5 distinct actor classes each with their own collision profiles and visual representation.

Procedural Hex Island Generation

Every tile position is derived analytically from its grid coords. Perlin noise at the same XY determines biome. No pre-baked maps.

HexGridIslandBuilder.cpp — GenerateIsland()
void AHexGridIslandBuilder::GenerateIsland()
{
    for (int32 y = 0; y < GridHeight; ++y)
    for (int32 x = 0; x < GridWidth;  ++x)
    {
        FVector HexLoc = GetHexLocation(x, y);
        const float NoiseVal = PerlinNoise->GetNoise2D(x * NoiseScale, y * NoiseScale);

        // Above threshold → land, below → water
        UClass* TileClass = (NoiseVal > WaterThreshold) ? LandTile : WaterTile;
        SpawnTile(HexLoc, TileClass);
    }
}

FVector AHexGridIslandBuilder::GetHexLocation(int32 x, int32 y)
{
    // Flat-top hex layout: odd rows offset by half hex-width
    const float XOff = (y % 2 == 0) ? 0.f : HexWidth * 0.5f;
    return FVector(x * HexWidth + XOff,
                   y * HexHeight * 0.75f,
                   0.f);
}

void AHexGridIslandBuilder::SpawnTile(FVector Location, UClass* TileClass)
{
    FActorSpawnParameters Params;
    Params.SpawnCollisionHandlingOverride =
        ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
    GetWorld()->SpawnActor<AActor>(TileClass, Location, FRotator::ZeroRotator, Params);
}
Release March 10, 2022 Engine Unreal Engine 4 Language C++ Category Simulation · Strategy · R&D Source github.com/khaled71612000 ↗
Connect