Now, Tothoseofyouthatfrequentthediscordserver, youwillknowthat I'm a bigfanoftheeliteSiri's ofgamesgoingwaybacktotheearlyeighties, wheretheelitegamespresentedanentireuniverseon a verysmallmachine.
I ammachinewithnotverymuchmemory, Andi.
OvertheChristmasperiod, I'vebeenplayingelite, dangerousandlovingeveryminuteofit, andithas a vastuniverse.
Infact, ithas a universesolargeitcan't allpossiblybestoredonthecomputer.
Andso I'vedecidedtocreate a smallapplicationhere, whichtriestoemulatethateffectonAsyoucansee, I'm panningaroundwell, a verysimplisticlookinguniverseofdifferentstarsystems.
Weneedtocontrolthatrandomnessinsuch a waythatmakesourapplicationssensible.
Inthisvideo, I hopetoshowusthat.
Solet's getstuffasusual.
I'm goingtostartwith a skeletonpixelgameengineproject, soit's gotnothingintheonusercreatedonuserupdatefunctionson I'm ConstructingthePixelgameenginetobe 5 12 by 4 80 ondereachgamepixelistomytoscreenpixels.
Now.
I'vealsomovedovertoVisualStudio 2019 and I'm stilllearningwhatit's goingtobedoingwithtooltipsandthingspoppingupalloverthevideo.
So I apologizeiftheygetintheway.
Fundamentally, ProceduralGenerationisaboutcontrollingrandomness, andso I thinkit's worthspending a fewminutesjustexploringtheavailableoptionsoffpseudorandomnumbergenerationthat's availabletouswith c++ language.
Sobymakingitssensitivetothespacebarbeingreleased, I shouldhopefullybeabletoretaintheintegrityoftheimage, whichisgoingtobenecessarytounderstandwhether a randomnumbergeneratorisgoodforusornot.
Thetwoqualitiesoftherandomnumbergenerator I wanttolookatthefirstishowgoodistherandomnessonthesecondishowfastcanitgeneratetherandomnumber?
So I'm puttingintoolittletimepointsso I canmeasurethedurationoffthegeneration.
Myintentionhereistogothrougheverypixelonthescreenandforeachpixelwe'regoingtodraw a randomnumber, anddependinguponvalueofthatrandomnumber, I'm goingtoset a flag B s startotrueorfalse, and I'lljustsimplyfornowuse a singlepixelisthestar.
Giventhatwe'rechoosingtoplot a pixelaswhiteorblack, basedonthefirstrandomnumbergeneratedaftersetting a seed, thespatternssuggestthattherandomnumberishighlycorrelatedtotheseed.
Therefore, it's burly, randomatall.
Infact, it's utterlypredictable.
Inthisinstance, it's aroundhisrubbish.
We'regoingtoneedsomethingbetterthanround.
Modern C++ actuallyprovides a goodrandomlibraryaspartofitsstandardlibrary, generatinghighqualityandrobustrandomnumbersiswellbeyondthescopeofthisvideo.
It's a completecomputerscienceandmathematicsfieldinitsownright.
There's a lotofresearchanddedicatedeffortgoingintogeneratinghighqualityrandomnumberssimplybecausetheyunderpinallofourdigitalcommunicationsandcryptography.
Service's as I justdemonstrated, eventhoughwe'veusedroundplentyoftimesinpreviousvideos, it's greatforlittlebitsofrandomnessingames.
Ifanything, I wouldsayit's a littlemoreevenlydistributedthanitwasusingRandNotseeingquite a czmanyclustersorlineartifacts, youmightjustbemyeyesplayingtricksonme.
However, ithasgenerated a verynicerandomuniverse, so I couldplacemymousecursoranywhereinthissceneonconvertingitscoordinatestouseas a seedfortherandomnumbergeneratorwilltellmewhetherthestarexistsornotisthepixelwhiteorblack.
Andsothequalityoftherandomnumberisbrillianthereinthiscase, buttheperformanceofitssucksweneedsomethingthat's a goodinbetween.
So C++ randomlibrarymiseentwister, you'reout.
Ittakesfarsmarterpeopleonmorededicatedpeoplethanmetogenerate a highqualityrandomnumbergenerator.
Behindthisismostlybeyondme, but I'm ontheunderstandingthatthesearesometypeofspecialistprimenumberandbuys a roaringwiththeseprimenumbersandshiftingtheregister.
Wecanchangethestateofal M a variable.
Inmanyways, thisisverysimilarto a linearfeedbackshiftregister, solet's justgetstuckinanduseitstraightawayinsteadoflookingatthewholescreen.
Let's justtake a verysimple, onedimensionalexampletoassume I'vegot a planeofland, and I wantedtopopulatethislandwithtreesintheuniverse.
Example.
We'reusingthelocationinspaceastheseedfor a randomnumbergenerator, So I'm goingtodothesamesortofthinghereon I'm breakingspaceupintodiscretesons.
Foreachzone, I'm effectivelyrolling a dyeandmaking a decisionbasedontheoutputso I canassumewithmyDiexamplethat's a treeexistsIfmyrandomnumberislessthantwo.
Inthiscase, there's a 33% chanceof a treeexisting, sowemightseeonehereandonehere, onehere, etcetera, etcetera.
If I madethisthresholdlarger, let's sayifit's lessthanorequaltofive, sothere's only a oneinsixchanceofitnotbeing a tree.
Wewouldcertainlyexpecttosee a lotmoretreesineachsectiontothepoint, whetheralluniformlydistributedacrossthelandinordertobiasproceduralgenerationtogiveusplausibleresults.
Wemayseethatifwedidhavetheprobabilityofitbeing a treesettoohigh, allofourtreeswouldbeuniformlyspaced.
Itwouldn't lookparticularlyrealistic.
Perhapswewantclumpsoftreestoexistinstead.
Well, thisisjust a caseofbiasing.
Ourthresholdvaluesonsomeoftheparameters.
Perhapsthespecialvaluecouldbebasedonlocation.
Soasthelocationnumberincreasesisgoingacrossthescreen, theprobabilityofitbeing a treealsoincreases, atwhichpointit's unlikelywouldsee a treehereatthestart.
We'llstarttoseethemwithmorefrequencysoyoucouldusethisapproachtoehavesortof a boundarygoinginto a forest, forexample, wecanalsouselotsofotherinterestingfunctionstoaccessthisthreshold.
Perhapswewanted a clumpoftreestoappearsomewhereinthemiddleofthisland.
Well, ifweused a thresholdvaluethatwasbiasedbysomeofthefunction, forexample, Gaussiancurve, theninthisregionit's farmorelikelywe'llseetreesthanintheseextremeregions.
Between 10 and 40 thesunitsaremeaningless, andtheywillneedtobechosentosuittheapplicationthatyou'retryingtoprocedurallygenerate.
Then I'm goingtodrawanotherrandomnumberforthecolorofthestar, and I'vegoteightpossiblecolorstochoosefrom, andthen I wanttogeneratehowmanyplanetssurroundthestarandthenforeachplanetinorder.
I wanttodeterminesomepropertiesofftheplanet.
Whatisitssize?
Whatisitstemperature?
Whatisitsmakeup?
A.
Whatpercentageisperhapscoveredinfoliage?
Whatpercentageiscoveredinwater?
Howmuchofitisminerals?
Youcanmakeupallsortsofdifferentparameters.
Whatisitspopulation?
Eachtimewestarttocreate a statistic, wealwaysdrawsomerandomnumberfromourpseudorandomnumbersequence.
Thefirstthingthatstarsystemneedstodoisgeneratedseedbasedonitslocation, which, aswe'vejustseenbefore, ismergingtogetherthe X and Y coordinates.
Now I'm using a 32 bitsystemhere, but I'm onlylookingattheleastsignificant 16 bitsoffthe X and Y cornerthatwill, ofcourse, putsomeboundariesontheuniverse.
A Boolean, whetheritexists, somethingthatrepresentsthesizeofthisstaronthestarscolor.
And I'lldrawthestarscolorfromanarrayofcolors, which I'vedefinedas a constantexpressionatthestartoftheprogram.
SotheseareAlfablue, green, redinhex.
I don't alwayswanttodraw a randomnumbersofthetyped, unsignedinterview 32.
InsteadofratherworkwithdoublesandregularEMTs, that's a bitmoreconvenient, andalso I wanttoworkwithnumbersthatarewithinboundariesthatissensibleforthethings I'm tryingtodefine.
So I wanttocreatesomeutilityfunctionswhere I supply a minimumandmaximumeachtime.
Sothisonewillgenerate a randominterviewbetweenthisminimumonthatmaximumonthisfunctionwillreturn a randomdoublewithsameconstraints.
Assumingthestartdoesexist, then I'm goingtogeneratesomethingthatrepresentsitsdiameterandchooseitscolorfromthearrayabove.
Insteadofworkingwithscreenin a perpixelbasis, I'm goingtobreakthescreenintosectorswhichiseasilydonebytakinganylocationonthescreenoninterview, dividingitbythedimensionsofoursector, which I'm goingtosetto 16 by 16 pixels.
Unlessyou'llseewiththebestofthisvideo, I'm reallyjustgoingtohackinthegraphicsappropriately, Sohere I'm goingtodraw a circleinthemiddleofoursector, and I'vegotsomehardcodednumbersinhere.
I know I shouldn't, butthepointofthisvideoisnotaboutthevisualization.
It's aboutthegeneration.
SothiswilljustdrawPhilCirclebaseduponthesizeofthestar, whichalreadywenowknowbecausewehave a starsystemobjectontothecolor.
Solet's justtake a quicklook.
That's verynice.
Wecanseewe'vegot a varietyofdifferentsizestars.
Noteverysinglesectorispopulatedwith a snaponthestarsofdifferentcolors, butrightnowouruniverseisvery, verysmallbecausewecan't movearoundinit.
If I movethiswayandwecanseethestarsareconsistentgoingback, itisactually a bitof a boldstatementformetosayWecanseethestars a consistentbecausenoneofushavememoriesthatarethatgood.
So I'm goingtoadd a Booleanas a simpleexampleappearofgeneratefullsystemwhenwe'reintheGalaxymapview, whichwe'vejustbeenschoolingaround.
I don't carewhetherthestarshaveplanetsornot.
All I careaboutiswhetherthestarexists.
It's onlywhen I selectoneofthestars.
Do I thenneedtogoon, generate a starwithalloftheplanet.
Andsoif I don't careaboutgeneratingthefullsystem, I'm justgoingtoreturnimmediately.
All I caredaboutwasgeneratingwhatdoesthestarlooklike?
Butnow I docarewhattheplanetslooklike, andwe'llgetthroughthisprettyquicklybecause I thinkwe'regettingtothepointwhere I'm justrepeatingthesamepointoverandover.
Buttogenerate a planet, I'm goingtochoose a randomdistancefromthestarbecausetheydon't allexistoftheuniformeddistancesfromstarson, I'm goingtogeneratehowmanyplanetsthatmightbe.
Ourcreates a fourlooptothengothroughalloftheseplanetsonebyone, andforeachplanetarecreatesmyplanetstructure, and I'llstarttopopulateitwithsomeoftheinformation.
Becausewe'veusedproceduralgenerationwith a highqualityrandomnumbergenerator, that's veryfast.
We'reguaranteedthatwecanalwayscomebackto a knownpositionintheuniverse, anditwillbegeneratedexactlythesameway.
Andsotherewehave a persistentandprocedurallygenerateduniverse, allstoredwithinabout 200 linesofcodeonsomefancyrandomnumbergeneration.
Thishasbeen a verysimpledemonstrationofthisconcept, but I'd liketogobackto a point I madeofthatstartthatit's notnecessarilythesameas, forexample, theMinecraftproceduralgenerationroutines.