开发者

Generating 3x3x3 "block" big structure randomly

开发者 https://www.devze.com 2023-04-10 23:53 出处:网络
I am in progress of making开发者_JS百科 my first 3D game, but I stuck into one part. I have never been good with algortihms or even math, so I am kinda having hard time with this :(

I am in progress of making开发者_JS百科 my first 3D game, but I stuck into one part. I have never been good with algortihms or even math, so I am kinda having hard time with this :(

Anyways, I want to generate 3x3x3 ( of course if algorithm would on any size it would be great ! ) "structure" or whatever it should be called. 1 unit is one block/cube. I don't want it to be full of blocks, but generate shapes randomly, so that some parts would have block and some would be empty. All the blocks should be connected to atleast one other block ( not diagonally, but "straight" ).

I hope that you understand what I am after :)

I quickly made a small picture with paint if it helps at all. However I would like it to be a lot emptier and it'd be great if upper part would be more frequently emptier than lower part.

Generating 3x3x3 "block" big structure randomly


Why don't you just create a few structures and then use random numbers to determine one of those. If you make like 7 different ones the users/players will hardly notice any form of repetition. Btw: There shouldn't be so many different structures matching your criteria if you ignore all structures that are rotational symmetric.


As an extension to @FlyOn's comment, I would suggest you think about the problem as a system of rules. Write/diagram out the rules. Ask yourself questions like this:

  • When generating an adjacent position, what are the valid 3-space movements to get to that position?

    (Each coordinate block in your solid has 6 face-adjacent coordinate blocks, 8 point-adjacent coordinate blocks and 12 edge-adjacent coordinate blocks. 6+8+12+1=27=3^3)

  • How can you restrict your random generation to, itself, only generate face-adjacent coordinates so that you don't have to apply that rule after the random?

  • If you are at position (0, 0, 0), and the random adjacent block chosen is (0, 0, -1), what are the tests that are required to determine if that is valid?

Write up the logic and write some unit-test-style methods that call the logic methods with tests. See if they work as you expect as you test them with different inputs.

Logic puzzles in 3-space are terribly entertaining :).


An example algorithm you could implement:
- pick a random position in the 3x3x3
- pick a random direction out of the possible straight options (remove options that would take you outside the cube, )
- go to that position (so set it to '1' in your 3x3x3 array or something like that)
- start over

optional:
* also remove options where you've already been
* first generate a random number for the amount of spots you want filled, then stop the algorithm once you have that many.
* allow all directions, and simply enter the 'other side' of the cube (this may cause parts to be not connected to other parts)

0

精彩评论

暂无评论...
验证码 换一张
取 消