Spiral sampler #24

Open
opened 2026-03-07 18:39:17 +00:00 by nexustix · 2 comments
Owner

Code to turn a 2D coordinate into a 1D position, supporting negative numbers.

The coordinates on a square can be mapped to a discrete spiral that is spiraling out of the center of the square. That way you don't have to worry about the origin being the edge of the world (Like you would have with the usual way of converting 2D square coordinates to 1D). The goal is to be able to sample arbitrary 2D coordinates in a world, without having to generate or store all the space between the sampled location an origin - Even when it doesn't actually need to be generated the coordinate growth does not align well with player behavior.

(Writing this down looks like gibberish, so it will be easier to just implement)

Code to turn a 2D coordinate into a 1D position, supporting negative numbers. The coordinates on a square can be mapped to a discrete spiral that is spiraling out of the center of the square. That way you don't have to worry about the origin being the edge of the world (Like you would have with the usual way of converting 2D square coordinates to 1D). The goal is to be able to sample arbitrary 2D coordinates in a world, without having to generate or store all the space between the sampled location an origin - Even when it doesn't actually need to be generated the coordinate growth does not align well with player behavior. (Writing this down looks like gibberish, so it will be easier to just implement)
Author
Owner

The problem this is trying to solve is the fact that random generation like cellular automata based cave/island generation works on a grid, which is sampled by coordinates. To know the next state of a cell, the cell needs to know it's neighbors. Having a predefined 1D array that gets indexed as a 2D square using the usual x = index % width and y = index / width will eventually run up against the "edge" of the square. This is fine in many cases, but if you want to stack multiple of these automata recursively you easily run into a problem of allocating the correct space for your needs (coming from a C perspective). Bit even without manual memory allocation this turns into a mess quickly, and a recursive solution would be a lot easier if you just just sample arbitrary coordinates without having to pre-allocate space, or deal having to come up with a sane offset from origin (0, 0) ahead of time.

The problem this is trying to solve is the fact that random generation like cellular automata based cave/island generation works on a grid, which is sampled by coordinates. To know the next state of a cell, the cell needs to know it's neighbors. Having a predefined 1D array that gets indexed as a 2D square using the usual `x = index % width` and `y = index / width` will eventually run up against the "edge" of the square. This is fine in many cases, but if you want to stack multiple of these automata recursively you easily run into a problem of allocating the correct space for your needs (coming from a C perspective). Bit even without manual memory allocation this turns into a mess quickly, and a recursive solution would be a lot easier if you just just sample arbitrary coordinates without having to pre-allocate space, or deal having to come up with a sane offset from origin (0, 0) ahead of time.
Author
Owner

Sampling a spiral that is spiraling from origin solves these issues elegantly and allows a clean implementation of infinite noise, and as a bonus can easily use any 1D noise to generate random terrain (No need for perlin or open simplex), and can be easily implemented on retro hardware.

Sampling a spiral that is spiraling from origin solves these issues elegantly and allows a clean implementation of infinite noise, and as a bonus can easily use any 1D noise to generate random terrain (No need for perlin or open simplex), and can be easily implemented on retro hardware.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
nexustix/godot-nexustix-utilities#24
No description provided.