Discuss Scratch

griffithsrock1
Scratcher
37 posts

Ideas for Random Terrain Generation (2D)

I have been working on a new random terrain generator, and I have a slight issue.

It's not a bug per say, more like a limitation. First of all, a quick breakdown of how it works.

It's totally random for one. No seeds or anything. Only the scratch RNG.

It does a few random things to determine the height of the ground at a given x position, then stores that value in a list, and draws it.

It also scrolls, infinitely in fact. The issue is that the starting position is x=0.
It can generate terrain in real time to the right of x=0, but not to the left. This is because scratch lists can't have negative indexes.
How I'm trying to fix that now, is with another list to represent negative x values, but it's not going to be totally clean.

I forgot to mention how it's rendered. It renders the screen left to right, starting from i_offset to i_offset + (screen width/pixel size)

Any ideas on how to do this better?

Project: https://scratch.mit.edu/projects/977894216/

Last edited by griffithsrock1 (March 11, 2024 13:29:28)

Modz31
Scratcher
72 posts

Ideas for Random Terrain Generation (2D)

-1*itemlist numberofpositions
AHypnoman
Scratcher
1000+ posts

Ideas for Random Terrain Generation (2D)

Could you share the project to make this easier to help with?

Until then, here's what I'd do (this may not work as without seeing the project I can't really give a solution that fits your code):

Generate terrain (say, 480px of it to cover the screen), render it from -240x then when the user moves right simply add terrain to the right, and if they move left insert terrain at the start and offset the origin for everything else - no negative numbers involved.

Essentially you'd be faking negative coordinates.
griffithsrock1
Scratcher
37 posts

Ideas for Random Terrain Generation (2D)

AHypnoman wrote:

Could you share the project to make this easier to help with?

Until then, here's what I'd do (this may not work as without seeing the project I can't really give a solution that fits your code):

Generate terrain (say, 480px of it to cover the screen), render it from -240x then when the user moves right simply add terrain to the right, and if they move left insert terrain at the start and offset the origin for everything else - no negative numbers involved.

Essentially you'd be faking negative coordinates.

The project is shared, maybe I will add it's link to my og message
MrKingofScratch
Scratcher
100+ posts

Ideas for Random Terrain Generation (2D)

If you have a set size for your world, you can add an offset to the “tiles.” for example, if it's 200 lines wide, you can make the pen script draw 100 pixels to the left. then, everything above 100 pixels will be “positive” and everything below will be “negative”

gotox:xposofline-100y:lookupyinthelist
griffithsrock1
Scratcher
37 posts

Ideas for Random Terrain Generation (2D)

Turns out the solution was easy, though imperfect.
Like AHypnoman said, but resetting the world origin to the leftmost terrain, it kinda does what I want. The only issue, is that the left terrain and the right terrain don't match up. But pre-generating terrain fixes that problem.

Last edited by griffithsrock1 (March 11, 2024 15:09:49)

Powered by DjangoBB