logoalt Hacker News

Building a Procedural Hex Map with Wave Function Collapse

309 pointsby imadrtoday at 5:02 PM43 commentsview on HN

Comments

porphyratoday at 6:43 PM

The post glosses over the "backtracking" and says they just limit it to 500 steps but actually constraint programming is an extremely interesting and complicated field with lots of cool algorithms and tricks. In this case we could solve it with Knuth's Algorithm X [1] with dancing links, which is a special kind of backtracking. Algorithm X should, in theory, be able to solve the border region described in the article's "Layer 2" with a higher success rate as opposed to 86%.

Furthermore, various heuristics can speed up the backtracking a lot compared to a brute force approach. As anyone who has implemented a Sudoku solver can attest, a brute force backtracking is easy to implement but will immediately get bogged down with slowness.

[1] https://en.wikipedia.org/wiki/Knuth%27s_Algorithm_X

show 1 reply
rhdunntoday at 6:24 PM

Oskar Stålberg used wave function collapse for various games, including Townscaper. He talks about it here: https://www.youtube.com/watch?v=Uxeo9c-PX-w&pp=ygUhdG93bnNjY... (SGC21- Oskar Stålberg - Beyond Townscapers).

profer602today at 10:27 PM

Interesting exploration of WFC on a non-square grid. The constraint propagation challenges must be significantly more complex than the 'canonical' example. Makes me wonder if a different constraint solver (e.g., constraint logic programming) might offer advantages in terms of expressiveness and performance for these more complex topologies.

jcalxtoday at 6:31 PM

Reminds me of Jasper Flick's Unity tutorial on hex terrain [0] which is similarly wonderfully detailed. Interesting contrast: this project uses premade tiles and constraint solving to match tile boundaries, while that one dynamically generates tile boundaries (geometries, blending, etc.) on the fly. Both enjoyable reads!

[0] https://catlikecoding.com/unity/tutorials/hex-map/

jesse__today at 5:58 PM

Love this.

As an aside, if the author reads this, did you consider using bitfields for the superposition state (ie, what options are available for a tile)? I did a wfc implementation a while back and moved to bitfields after a while.. the speedup was incredible. It became faster to just recompute a chunk from scratch than backtrack because the inner loop was nearly completely branchless. I think my chunks were 100 tiles cubed or something.

OscarCunninghamtoday at 7:26 PM

It seems like a lot of the difficulty is in finding arrangements that satisfy constraints. I wonder if an alternative approach would be to use a SAT solver. I suppose the problem with that approach would be that the solver might always find an 'easy' solution that doesn't look random. I know that some SAT solvers let you randomly assign the initial assignments of the variables, but that doesn't mean you get a random solution. Has anyone tried a similar approach?

show 1 reply
xiphotoday at 5:48 PM

Inspirational stuff, with lots of great references to the OGs at the bottom, and source available. Now can it be merged with the look/feel of https://heredragonsabound.blogspot.com/. ;)

footatoday at 9:08 PM

I realize this comes up every so often, but I was just looking at this the other day :) A related idea is wang tiles, which are a way to construct a tileset such that you can place them without ever running into a contradiction.

schemathingstoday at 6:15 PM

OP is probably familiar but this site has a lot of good examples of hex math with code examples - https://www.redblobgames.com/grids/hexagons/

show 1 reply
tomtomistakentoday at 6:12 PM

Reminds me of Dorfromantik[0].

[0] https://store.steampowered.com/app/1455840/Dorfromantik/

show 1 reply
z3t4today at 8:41 PM

"WebGPU is not available on your device or browser.". Other 3d demos works fine though. Tried Firefox and Opera on mobile.

MattRixtoday at 9:14 PM

Fun fact: because WFC is graph-based, you can do stuff like creating a graph where it uses time as a dimension, so you can create animations that “wrap” in time.

In this rabbit example I made 8 years ago, the WFC solver ensures that the animation must loop, which means you will always end up with an equal number of births and deaths.

https://xcancel.com/MattRix/status/979020989181890560

ionwaketoday at 7:07 PM

This is absolutely beautiful, I could even tell I was going to like it from the title. Good job.

btbuildemtoday at 7:18 PM

I really like the part where you can "reroll" sub-areas of each tile. Consider exposing some of the weight knobs (eg, I'd like to tweak it to favour mountainous terrain)!

jcultoday at 6:33 PM

That "Carcassonne" game sounds really fun. I'd never heard of it before.

show 1 reply
jbmsftoday at 8:31 PM

Years and years ago (pre-smart phone), I built a mobile map and navigation product. Labeling streets was one of the more interesting side quests and the solution I found took a similar approach of generating a large number of candidates, picking one solution, and iterating. It worked quite well in practice.

llm_nerdtoday at 10:06 PM

This is fun and neat, and looks fantastic, but the generated maps are basically nonsensical, aren't they? Landmasses and waterways and roads and buildings and forests and so on that don't make any logical sense for their placement.

I say this having had a couple of fun "hex-based strategy game hobby projects" over the years (sidenote -- trying to cover a sphere in hexes is actually a non-trivial matter). Invariably I ended up with "to make a map from scratch, first you create the universe" where I'd go through all of the ages, compute waterflows and precipitation, and on and on. Maybe I made the requirements too unreasonable and that's precisely why I never yielded a working game from it.

show 1 reply
contextfreetoday at 5:44 PM

"Stop playing your AI garbage and get to bed!" "Mooooom! It's not AI garbage, it's classical procedurally generated content!"

nickandbrotoday at 5:55 PM

This looks amazing man, seriously good job with this.

kevinsynctoday at 6:14 PM

Super awesome, love the tilt-shift camera effect!

I was also wishing I could zoom in to human size and run around HAHAHA

show 1 reply
behnam_amiritoday at 6:16 PM

This is cool. Curious if you plan on keep it as a map generator or turn it into something more interactive too.

verdvermtoday at 5:48 PM

Related (?) has anyone else been following the Hytale Worldgen v2? They've built a visual node editor so anyone can create biomes, structures, or complete worlds. I believe there is a competition going on right now.

They are essentially making the entire game based on similar concepts and then using them to develop their core content. Simon is an inspiration and has said they won't be taking investor money so they can stay true to the users and creators.

bobektoday at 5:59 PM

Made me smile. Thank you!

MattDamonSpacetoday at 5:21 PM

Gorgeous

ArcaneMoosetoday at 6:04 PM

Beautiful work!

westurnertoday at 6:46 PM

Model synthesis: https://en.wikipedia.org/wiki/Model_synthesis :

> Model synthesis (also wave function collapse or 'wfc') is a family of constraint-solving algorithms commonly used in procedural generation, especially in the video game industry.

> [...] One of the differences between Merrell & Gumin's implementation and 'wave function collapse' lies in the decision of which cell to 'collapse' next. Merrell's implementation uses a scanline approach, whereas Gumin's always selects as next cell the one with the lowest number of possible outcomes

And then `## Developments` mentions:

"Hierarchical semantic wave function collapse" (2023) Alaska, Bidarra: .. citations of: https://scholar.google.com/scholar?cites=1671019743611687613...

gedytoday at 5:41 PM

Real engineering skills, I love it.

moi2388today at 6:01 PM

This entire article reads like it was fully written by AI unfortunately

show 1 reply