Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411423 Posts in 69363 Topics- by 58416 Members - Latest Member: JamesAGreen

April 18, 2024, 07:54:40 PM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)2D Procedural Generation
Pages: [1]
Print
Author Topic: 2D Procedural Generation  (Read 3468 times)
danlthemanl
Level 0
***

The only thing I'm sure of, is that I don't know..


View Profile
« on: December 20, 2010, 12:29:31 AM »

This is my first time trying out anything Procedural, I'm using Flashpunk with Flash and I can't seem to figure out how to make all the block below the highest block to be filled in.
Help?
Code:
public function randomize():void 
{
var visableBlockWidth:int = worldWidth / 16;
var visableBlockHeight:int = worldHeight / 16;

var totalBlocks:int = visableBlockHeight + visableBlockWidth;

for (var i:int = 0; i < worldWidth; i++)
{
for (var ii:int = 0; ii < visableBlockHeight; ii++)
{
var randomizer:int = Math.ceil(Math.random() * (1 + 10 - 4)) + 4;
if (ii > randomizer)
{
add(new block(16 * i, 16 * ii));

}
}
}
}

It usually comes out looking like this:
Logged

SunnyKatt
Level 1
*


Just a guy writing code


View Profile WWW
« Reply #1 on: December 20, 2010, 02:52:27 AM »

Right now all you are doing is a noise pass, which is more like random generation than procedural generation.

Your algorithm is going to have to be a lot more complicated - Do you want it to just make hills or are you looking to make cave systems?

To fill blocks below each block, have it quickly loop downward and check if there already is a block or not below each one that you place, and place them if there aren't. This will make each block you place with the engine the new max. This is not the fastest way to do it if you are going for speed; I would go horizontally and have it pick a height value for each x position on the grid, and then place blocks while counting up to that height on each one.

If you want to do cave systems, though, then neither of those methods would work.

Hope this helped,
-Sk
Logged

. . .
danlthemanl
Level 0
***

The only thing I'm sure of, is that I don't know..


View Profile
« Reply #2 on: December 20, 2010, 11:31:34 AM »

Okay cool, so what's the difference between Random generation and procedural?
Logged

John Nesky
Level 10
*****


aka shaktool


View Profile WWW
« Reply #3 on: December 20, 2010, 11:45:35 AM »

Random generation is a subset of procedural generation. Procedural generation is generating data with code, and randomness is one way to do that. It's usually more interesting if randomness is combined with other techniques.
Logged
Rob Lach
Level 10
*****



View Profile WWW
« Reply #4 on: December 20, 2010, 12:26:26 PM »

You're doing things computationally in an overly complex manner.

if you just want a random stack of blocks from the bottom filled in, do that.

traveling along your x and randomly define a height, fill from the bottom to top of the height.

Logged

Will Vale
Level 4
****



View Profile WWW
« Reply #5 on: December 20, 2010, 02:03:35 PM »

This thread in the tutorials section has a lot of good stuff on procedural generation:

http://forums.tigsource.com/index.php?topic=5174.0

Will
Logged
chris_b
Level 1
*


View Profile WWW
« Reply #6 on: December 21, 2010, 01:19:43 AM »

@danlthemanl:

The main problem with your function is that you are picking a new random value  in each step of your inner loop where you are filling the column of blocks - so if (ii > randomizer) will sometimes skip a block because randomizer keeps changing.

You need to pick a random value in the outer loop, then you can just use:

for (var ii:int = randomizer; ii < visableBlockHeight; ii++)

for your inner loop - no need to check if (ii > randomizer) each time you add a block.



Also, your outer loop should be for (var i:int = 0; i < visableBlockWidth; i++) - you used worldWidth by mistake, so you're adding 16 times to many blocks.
Logged
danlthemanl
Level 0
***

The only thing I'm sure of, is that I don't know..


View Profile
« Reply #7 on: December 22, 2010, 10:39:32 AM »

Oh wow!! Thanks so much! Works perfectly!
Logged

Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic