Spiral sampler #24
Labels
No labels
bug
improvement
needs testing
new feature
pondering
priority
high
priority
low
priority
normal
priority
shelved
wontfix
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
nexustix/godot-nexustix-utilities#24
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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)
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 % widthandy = index / widthwill 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.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.