helloThisweek, we'regoingtostarttaking a lookatspinesandhowyouis a codercommutesthemtoenrichyourapplicationsandgamesinshorts, blindsor a wayofalgorithmicLeerepresenting a curve.
Andifweweretryingtorepresent a longerpathusingmorepointsinthisfashion, wehavetoprettymuchguesswherethosecontrolpointsneedtobaywillalsofaceissuesaboutwhathappenswhenyoutrytojointoSPlines.
Ifwehave a lookattheWikipediaarticle, crisplineswillseethatit's actuallyquitemathematical, andthiscouldmakeitinaccessiblefortheonelonecodeisouttherewhojustwanttogetsomecurvatureintotheirapplication.
Soeventhoughwetraditionallywouldhave X andwhywecouldhavemoredimensionsafterwardson, themathematicsdoesn't changejustscalesup.
So I'm goingtotryandstartexplaininghowsplittingworkswithoutgettinginvolvedwiththemathematicsjustyet.
And I finditusefultothinkaboutthepointsasbeingthecenterofinfluentialcirclesthatguideourpoint t sosimplyput, asweapproach P, too, we'remoreinfluencedby P, too.
Andwebecomelessinfluencedby P onebecausewe'refurtheraway.
So, ofcourse, ifthepointliesdirectlyinthemiddleof P oneand p twowereequallyinfluencedbyboththepointsandifthepointonthecurvewereinterestedinhappenstolieononeofourcontrolpointswere 100% influencedbythatcontrolpoint, andnothingcomesfromtheothercontrolpoint.
Sowhat's appoints P zero p 34 then?
Well, ifwejusthadthepoints p oneMPtoinfluencingthecurve, wewouldget a straightlinewhichyoudon't needmetotellyouisn't a curve.
Soin a similarwaytothecirclesofinfluencefrom P oneand p.
Two p zeroonPCPalsoareinfluencing a pointonthecurve, butin a muchmoremildway.
Sowhereaswesaw P oneand P twoattractourtargetpoints, p zero p firepelthetargetpoint, andwecanseeinthisexamplethatthecurvepushesupwardsusingtheDesmond's dotcomGraphingcalculator.
Wecanhave a lookatthemathsbehindthespine.
Considerthe X axisalongthebottomtorepresentourverbalteanormalizespacesoitgoesfromzerotoeoneandrememberforourpointthatwasblue, whichis a P one.
Wewanttohave a fullinfluencewhenwe'reonthepointon.
Sointwo D space, we'd havetocalculateallofthesefor X onalloftheseforwhytogiveusthe X Y pointonthecurvesegment.
I thinkit's timetostartputtingtheseintopractice.
So I'vecreated a newconsoleproject, and I'm goingtouseouroldfriendtheoilseeconsolegameenginesowecanvisualizethespinesasmostofmyviewerswillknowbynowyou'llseecounselgameEnginejustsimplywrapsuptheconsolein a waythatitrepeatedlydisplaysinformationtothescreen, anditalsoacceptsomeuserinputon.
Weneedtooverridetwofunctionsinthisclass.
Theunusedcreateononuserupdatefunctions.
I'm goingtocreate a councilapplicationthat's 160 characterswideby 80 characters.
Highoneachcharacterintheconsoleisgoingtobe 10 by 10 pixels.
Let's startbycreating a structurethatrepresents a to D Point.
So I'm justgoingtohaveanex.
AndwhyCoordinates?
I'm goingtocreateanotherstructurenowtorepresentthespinefromthespineisgoingtoconsistof a vectorofpoints.
Thiswillgiveme a threebythreeblocktorepresent a controlpoint, andinthemiddleofthatthreebythreeblock, I'm goingtodrawtheindexoftheircontrolpointsowecanseewhichone's which.
Let's take a lookperfect.
I wanttomovethecontrolpointsaroundthescreen, so I'm goingtocreate a variableTherepresents, a pointthat's selectedformovement.
We'llinitializeit.
Zero.
Solet's handle a littlebitofinputfromtheuser.
Iftheuserpressestheexkey, I wanttoincrementmyselectedpointsvariable, but I'm alsogoingtomakesurethatitdoesn't gooutofbounds, soifitdoes, itrotates.
I thinkit's importantthatwehighlightthecurrentlyselectedcontrolpoint, so I'm goingtouseexactlythesamecodeas I'vedonehere, exceptinsteadofcoloringitinred, I'm goingtocoloritinyellowondhe.
Let's take a looksowecanseethezerocontrolpointiscurrentlyhighlighted, and I canusethearrowkeystomoveitaround.
Andif I usedisthatthenextkeys, I couldmoveanycontrolpointthat I want.
Sohowdowedrawthecurve?
I'm goingtocreate a functioncalledgetsplittingpoint, whichtakesin a valueteethandreturnsin X Y coordinates.
Fornow, I'm justgonnahardcodethatis 00 Drawingspineis a littletricky, and I wanttomakesure I'm drawingitbehindthecontrolpoints, remember, Wereusingthecurvetogeneratean X Y positionbasedon a normalicecoordinate.
Sowe'regoingtowishyou a zeroto a one, anditwillreturnin X y forthatpointonthecurve.
Solet's create a littlefourloop, whichitsratesfrom 0 to 1, and I'm goingtonibbleatitinverysmallstepsandwe'llseethatthisisactuallyoneofthedownsidesofusingspinesforeachvalueoftea.
I'm goingtocalloursupplyingpointfunction, whichshouldreturnthe X y coordinate.
And I'm justgoingtodraw a pixelatthat X y coordinate.
Let's justtake a minutetolookataninterestingpropertyofthishere.
I'velaidoutthepointsinwhatappearstobe a loop, andinfact, if I takethetwoendpoints, whichinthiscasewillbeoneandeightandoverlapthem, thatwouldactuallymake a loop.
Butit's a loopwith a bitof a kinkinitaroundthe 0.8, but I cangetridofthatbyusingthetwooriginalendpointsandoverlappingthosewiththetwoneighboringpoints, whichgivesus a nice, perfectlycurvedloop.
I'm goingtodraw a linewhich I'm hopingisgoingtobeorthogonaltothepathatallpoints.
Solet's have a lookatthis.
Thefirstthing I doiscalculatetheangleofthegraydidn't returnedbythegetsbyingredientfunction, sothiswillreturnan X y vector.
But I'm goingtoconvertittoradiance, and I'm goingtousethedrawerlinefunctionwhichisprovidedbythegameenginetocalculatetwopointsthatarerotatedaround P one, whichiswhereouragentisonthepathbytheanglerepresentedbytheGradyint.
I'm goingtooffsetthesebyfivepixels.
So I'm goingfromonepointhere, which, youcanseeisAh, plusfiveto a minusfive.
Soif I usedthe N s keys, wecanseetheagentfollowsthepathnowbecause I'm incriminatingitspositionalongthecurveanditwrapsaroundverynice.
Sothisvideoshowingyouhowtocreate a pathandsplitbetweenallthecontrolpoints, and I thinkit's quiteusefulforthingslikenonplayercharacter A I oranimations.
Butthereisstill a fewthingstoworkout, Soas I movemyagentaround, wecanseeitsspeedvaries.
Zerotoeonewillcomeupwith a solutionforthisinparttwoofthespinesvideo.
AllofthecodeforthisvideoisavailableonGetup.
Ifyou'veenjoyedit, pleasegiveme a bigthumbsup.
Everthinkaboutsubscribingand I'lldefinitelyseeyounexttimeand I'lldefinitelyseeif a parttwoofthisvideosotakecare.
helloThisweek, we'regoingtostarttaking a lookatspinesandhowyouis a codercommutesthemtoenrichyourapplicationsandgamesinshorts, blindsor a wayofalgorithmicLeerepresenting a curve.