Instead, I'm goingtoshowyouwhatwe'regoingtoachievetoday, whichistheadditionofunitswhichyoucanseehavecollisiondetectionandresponsewiththeterrainwillalsoaddarbitraryexplosionsso I canclicksomewhereanditcausesanexplosiononalsodamagestheterrainwithwith a crateronder.
Ifyoucauseanexplosionnear a unit, itblaststhemaroundscreen.
Verynice.
Onthefinalpartwillbeaddingissequencesanevent.
Soherewe'recreating a weaponobject, whichonecollides, sortofcombinesallthreethingstogether.
Now, eventhough I'm splittingthisprojectacrossseveralvideos, there's stillquite a lottogetthrough, so I'm goingtobegoingthrough a slightlyquickerpacethannormal.
Afterall, a lotofthetechniquesweseeinthisvideoarebasedupontechniques I'vealreadytalkedaboutinpreviousvideos.
Just a quickintroductionto, say, a bigthankyoutoallthepeoplethathavejoinedtheDiscordserver.
There's almost 100 loancodeisouttherenow, andifyou'reinterestedinjoininginandgettinginvolvedinthechapters, quite a lotoftalentonthatchatserver.
Then I suggestyoulookattheleaksbelowforaninvitetothediscord.
Now I maybeboring, butitisveryimportantthatwithanybigprojectyouform a planbeforestartingonwormsisnodifferent.
I knowthat I'vegotrandomlygeneratedterrainszeonthesearoundthemwegeneratedtothepixellevel.
Andgiventhatwormsis a gameaboutaccuracy, I thinkit's importantthatwehavepixellevelcollisions.
Sothat's thefirstrequirement.
Wormsisagainallabouttrajectories, soassumingyou'vegot a littleunithereandhecanfirehisweaponacrosstheterrain, theterraingetsalteredandthencausesmultiplemoretrajectoriesfordebrisandpossiblyalltheunits.
Butwhendebrislandsonthesurface, itdoesn't justimmediatelycometo a stop.
Itcarriesonbouncing a littlebit, soweknowwe'regoingtoneed a verysimplephysicsengine.
Well, bothofthesearehard.
Let's dowhatwecantosimplifythem, yetkeepthemworkingin a waywhichissuitableforgameplay.
But I'm goingtoletobjectorientedprogrammingandpullhimofficer, helpusout.
Everythingthatcanmoveinmygameisgoingtobederivedfrom a classwhichrepresents a physicsobjectonthisclasswillbeabstract, butit'llcontainusefulinformationsuchasposition, velocity, accelerationand a flagthatrepresentswhetheritismovingornot a stabilityflag.
I'm alsogoingtodefine a purevirtualmethod, whichmakesthisobjectabstractdraw, whichwillbeusedbysubclassestodrawthemselvesonthescreen, themissileanddebrissubclasses.
I'm goingtoseparateoutthemapgenerationto a createmapfunctionbecause I stillwanttheabilitytoscrollthroughmapsandinterrogatethemandfindonesthat I like.
Now.
I alsoknowthatmymapsaregoingtorequirePerlinnoise, so I'm justgoingtocookandpasteabsolutelycharacterforcharacterfromthepearlynoisevideo, Thepearlynoiseone D function.
It's just a remindertakesaccountoffthenumberofelementsinthearray a cedar, a fullofnoise.
Andwhen I dothemapscrollsinthatparticulardirectionatthisparticularspeedonforthisproject, you'llseemeusinglotsofhardcodednumbers, which I'vealreadydecidedthatthebestvalueafterall, itwouldbequite a frustratingandboringvideo.
Butthisleadsto a ratherdangeroussituationthatthecameracouldexistoutsideofthemapspace, whichmeanswe'd bedrawingrandomelementsofmemoryandallsortsofchaoswouldensue.
So I'm goingtomaintaincontroloverthecamerabyclampingitstotheedgesoffthemap.
Sonowwhenwedraw a mapinthedrawlandscaperoutine, I'm goingtooffsetitbyourcamerapositions, goingtoconvertthemtointerview, Ofcourse.
Andwe'reusingthenowveryestablishedtechniqueoff.
Whymultipliedbywithplus X toeworkoutwhereweareinthetwo D array?
I thinkwe'rereadytotake a looknow.
Sothecouncilwasstartedopen.
We'reinthesky, soif I movethecursordown, yep, that's good.
Ofcourse, I couldfixitto, say 0.25 anditwouldbehigher.
Opal, lowerdown.
We'venowcreated a twodimensionalpixellevelmapthatrepresentsthatserene, infact, represents a verylargeterrain, andwecouldmakeitlargerbyplayingwithnumbers.
Let's getstuckintothefunstuffnowandcreateourphysicsenginegoingtostartbydefining a baseclasscalled C physicsobject, And I'm gonnahavesomedirectlyaccessiblepropertiesjusttoannoypeoplethatrepresenttheposition, thevelocityontheaccelerationthey'reallgoingtobeinfloatingpoint.
Infact, thewholephysicsengineisgoingtobe a floatingpointphysicsengine.
I'm alsogoingtoadd a defaultconstructor, whichallowsustosettheposition.
Thereweretwoothervariables.
We'reinterestedin a radius, whichrepresentsthecollisionboundaryoftheobjectontheflagthatrepresentswhethertheobjectisstable.
Stabilityisquiteanimportantthingtoknowin a physicsengine.
Tohelpdebuggedthephysicsengine, I'm goingtocreate a dummyobjectwhichinheritsfromtheseaphysicsobjects.
Infact, I'm goingtocallitseedummyonlytocreate a constructiveformydummyobjects.
I wasjustpassesthroughthe X and Y positioninworldspacebecausemybasedclassesabstract.
I needtoimplementthedrawmethodsomewhere.
Now I'm goingtoaddsomethingwhich I'm sureisgoingtocausemenoendofbotherintheYouTubecommentsandonthediscordserver, I'm goingtohave a privatestaticvariableto a vectorofpersoffloats.
Infact, thisishowwe'regoingtostart a wireframemodelon.
I'm makingitstaticbecause I mightcreate 100 ofthesedummyobjects.
But I don't wanttorekeeprecreatingandallocatingresourcestostorethesamemodelforeachobject.
So I wantonemodeltobesharedacrossallobjectsofthesameclass, andit's goingtoget a littlemorecomplicatedbecause I needtoinitializethisvariable, and I'm goingtouse a factoryfunctiontodoit.
SowhentheprogramcomesaroundtodefiningexactlywhatBackmodelisfortheseadummyclass, itcostthedefinedummyfunctiononthedefinedummyfunctionproduces a listofpointsin a circle.
Infact, it's a unitcirclebecauseweknowthedrawerwifeframedfunctionswillscaleforus.
Butthisapproachisnicebecause I onlyneedtodothisonce, regardlessofhowmanydummyobjectsaregoingtobefloatingaroundinmyworld.
Andsotodrawtheobject, I'm justgoingtocallthedrawerwireframemodel, whichispartoftheoneloneKodaCouncilgameengineclass, andthat's whywe'vepasseditinis a pointer, andsotheperimetersaredrawthemodeldata, whichwe'vealreadycreateditspartofthatstaticvariablethe X and y locations, which, ofcourse, thelocal X and Y inworldspaceoftheobject.
Butoffsetbythecamera, whichwe'vealsopassedintothisfunction, I alsoneedtochoosetheangleatwhichwerotatetheobjectbecauseit's wifeframeon.
Todothis, I canusetheAtento a functionoffthevelocitycomponents, and I alsowanttoscaletheobjectsizeon.
Let's take a look, moveitdownsowecanfindsometerrain.
Andif I clickonthescreenwiththemiddlemousebutton, I canplaceanobjectanditscrudelycircularWhatyou'llseeit.
I'veactuallyalsoadded a linefromthemiddleofthecircletotheRadius, andthiswasdefinedaspartoftheobjectinthatstaticdefinitionon, wecanusethislinetosaywhatangleofrotationhasbeenappliedtothewirefameobject.
Inthiscase, wehaven't evenlooked a velocityyet, sothey'reallpointinginthesamedirection, whichisjustsomedefaultstartupdirection.
Andofcourse, we'vegotnocollisionso I canputthesethingsalloverthescreen.
If I movethecamera, theymoverelativetotheterrain.
Wecanuse a similarit's orationtoupdateallofthephysics.
Andintheasteroidsandtheflappybirdvideo, wecanlookathowwedothephysicsverysimplyfortwo D objects.
But a wholebunchofobjectsinandtheyallbehaveindependently.
Verynice, although a bitslowphysicsenginesandgraphicsenginesreallycoexistatthesamespeedonproperphysics.
Engineisnotlikethisonereallydon't likehavingchangesintimeStepNow I knowthatwiththeonelocotocounselgameengine, drawingtothescreenisactuallytheslowestpartandthatthephysicsatthislevelofworkoutlater.
Butanyway, we'llreallytakeupnoCPS a tall.
Soforeveryframeofgraphicsupdate, I'm goingtodo 10 framesoffPhysicsofDeath, whichissimply a caseofwrappinguptheupdatephysicsloopin a secondloop.
Andsoif I runitnow, fundthat's rainagainstcoolitdown.
Youseetheobjectsfallof a slightlyfasterpacebecausethere's 10 timesmoreupdateshappening.
I knowthat I couldjustsimplychoose a theatervaluesufficientlysmallenoughthatitwillbebecausenoneofmyradiusis a verylarge, but a sensibleapproachwouldbetochoose a theatervaluebasedonbasedontheradius.
Notcollisionbetweenobjectsonthefirstitem I'm goingtoneedistheactualangleinradiancederivedfromthevelocityvector, aswesawfortherotationofthewirefameobjectsbefore, I'm alsogoingtocreatean X and Y componentoffmyresponsefactor.
Once I knowthisangle, I'm intoogreat a fourloop, whichitsratesfromonesideofthesemicircletotheotheratinthiscaseaffectstepsizebuthasjustbeendescribing.
Youcouldprobablydosomethingmorecleverheretoensurethatallofyourpointsarewithin a unitdistancefromeachother.
Usingsignandcosign, I cantaketheradiusofthecurrentobject, plustheangleofthepointbeingtestedonthepotentialnewlocationtoeworkoutwearingtheworldmap.
Onlyitspotentiallocationforcollisionactuallyresultsin a collision.
But I don't wanttheobjectstooverlapwiththeterrain.
I'm goingtousemyresponsefactorasthenormaltothetangentofftheterrainandusethisin a reflectioncalculationso I canget a reasonablyaccuratebounceoftheobject.
Atsomepoint, I justneedtosimplysay, Look, there's notenoughmovementtoactuallycountersmovement, so I'm goingtodothatdeliberatelybylookingatthemagnitudeofftheobjectsforlost e vectorandseeingifthat's lessthan a smallenoughamount, andifitis, I'm forcingittotruth.
Payoffbecauseall I needtodoisadd, say, 20 newdebrisobjectstothelist, andtheposition I'm goingtogivethemisthemousecoordinatesinscreenspacetranslatedintoworldspace.
I don't needtomakeanyotheralterations, buttheobjectswilldrawthemselves, andthephysicsenginewilllookafterthem.
Solet's findtheterrainagain.
Clickthemouse.
Youcanseeitspawns a wholebunchofobjects, whichjustbouncearounduntiltheoldoneoutofenergykeepclickingandclickingandclickingloadsofobjectsallbeinghandledbythephysicsengine.
Variablesfrombeforewillsaythat's a littleexplosionas a radiusoff 10.
Solet's take a lookandseeifitinterfereswithotherobjects.
Let's festivalplacesomeunits.
Yeah, Andi, I'm clicking.
It's causinganexplosion.
That's good.
Andif I clickinthisclusterofobjects, wecanseetheygetknockedabout.
Andif I tryandlaunchanexplosionfarawayfromtheobjects, hegetsaffectedverylittle.
Butif I launchitinthemiddleoftheobjects, hegetsaffected a greatdealproportionaltothesizeoftheexplosion.
Butwecanseewe'renotdoinganydamagetotheterrain.
Giventhatourterrainmapissimply a bitmappedimage, I'm goingtousethecircledrawingroutinetoerasepartsofit.
I amgoingtodrawskyontopoftheterrainin a filledincircle, and I'm goingtouseBresnanCirclealgorithmtodothis.
I'vejustliftedthisdirectfromWikipedia.
They'veevengotitinsee, but I havemodifieditslightly.
Insteadofusingputpixeltodraw a circleoutline, I'm usingthesecoordinatestocreate a linethatspentessentially a scanlinehorizontallyacrossthescreentodrawfilledincircle.
Andjustbecause I likeusingtheminmyvideos.
Here's a lambdafunction, whichcreatesthefilledinBreslincircle.
Itjusttakesan X coordinate, a Y coordinateand a radiusallin.
Introducedonthisisinworldspecsisthatthisisaffectingthebitmapimagethatrepresentstheterrain, anditisliterally a couldn't pastefromWikipediawith a smallmodificationfordrawinglines.
Sonow I cancreate a crater.
It's difficulttosaybydrawing a circleofskylocatedaroundtheworldexofworldwidecoordinatesthatcameinaspartofourboomfunctiononspecifyingtheblastradius.
Solet's take a looknowthatworksverynicelyindeed.
I mean, I cansortofcreateexplosionswithintheterrain, whichprobablywouldn't benormal, but I canetchawayattheterrain.
I'm notspecificallycheckingwhatanyobjectisasanytime I don't wantmymissilestobounce.
Infact, I wantthemtocalltheboomfunctionandexplodewhentheycontacttheterrain.
So I knowthey'vecontactedtheterrainbecauseofmycollisionroutine.
Andif I saidthebouncebeforedeathnumbertowone, thatwillsetthedeadflagonthefirstcontactso I coulddosomething.
Undertheseconditions, I'm goingtocreate a functioncalledBounceDeathAction, whichis a functionofmy C physicsobject, which I'm goingtousetogetinformationaboutwhattodowhentheobjectdies.
And I needtoaddthistomybasephysicsobjectas a purevirtualmethod, whichalsomeans I needtoupdateitinallofmysubclasses.