Solet's takeVictor a here, and I'llinitializethatto 1.0 ondhe 2.5, forexample, onDhe, really, I workwithjustonevector.
Wemayhave a bunchofthem, so I'llcreate A B and C.
Andwhen I wanttooperateonthesedirectors, I woulddoiton a componentbycomponentbasis.
So a dot X equals b dot x plusseedattacksonwhateveryoudofor X, youtypicallywanttodoforwhite.
Sointhiscase, I'm addingthetwofactorstogether.
I couldalsodoitusinginitializethelists.
I couldsay a equals b dot x plus c dot x commabedotwarIIplus C Don't why, andthatwouldbeequallyvalid.
Andsofar, myprogramshavebeensimplyenoughthatthisis a completelyfinewaytodothings.
However, withworkingonthetopdowncitybasedcarcrimegame, I'm doing a lotofthingsusingphysicsandtodevectors, andafter a whileitgets a littlecumbersometokeepsplittingofthecodeintotwolines.
I alsomentionedinmycoatyourselffeedtheengineSiri's thatoneday I'lldo a videoaboutoperatoroverloadingtosimplifythenatureofthebacktotype.
Andsohereitis.
Andwhatdo I meanbysimplifying?
Well, surelyitwouldbemuchmoreconveniencetojustwrite a equals.
B Plus C onthesystemknowshowtogoonAdam.
Youcanseeitgivesyou a littleredwigglyherebecauseitdoesn't knowhowtooutthesetwotypesofthemoment.
Solet's make a basictwo D vectortypethat, toallintentsandpurposes, canbetreatedlikeanyotherbasicprimitiveintheprogram.
Firstly, theonlytwomembers I wantmytypetocontainarethe X and Y variables, and I wanteverythingwithinthistypetoremainpublic.
So I'm justgoingtokeepitas a structureletsout a simpleconstructortostartwith, and I'm goingtousememberinitializationsyntax.
I don't reallyneed a bodyformyconstructor, but I'vegottoincludethebracketsanyway.
Andsimplybecause I wanttoshowvariousinterestingthingsinthisvideo, I'm alsogoingtoadd a copyconstructor, whichtakes a referencetoanobjectofthesametypeandconstructswithitagain, anemptybody.
Sincewe'recreating a vectortype, itmayalsobeusefultoincludesomeutilityfunctionsnormallyassociatedwithvectors, forexample, gettingthemagnitudeorthelengthofthevector, whichissimplythehighPotternews.
I neverknowwhyvideostudiodoesn't putspacesthereanothercommonthingtodoifactorsistonormalizethem.
Socreate a functioncallednorm, whichreturnsanew.
Vector, whichisthenormalizedformofthisvector.
Itbecomestheunitvector.
Sofirstwewanttocreate a temporalvariablecoldOh, whichistheinverseofthemagnitude.
And I'm goingtoconstruct a newvectorwhere I multiplythecomponentsbyourandthatwillgivemethenormalthethirdyearsolefunction, maybetogettheperpendicularyousomethingat 90 degreestothisfactor.
Well, that's easyenough.
I'm justgoingtocreate a newvectorandswizzlethecomponentsaround.
Sofar, wegotnothingoutoftheordinary.
We'vejustaddedutilityfunctionsto a stroke, Buthowdowemakeitbehavelike a primitive?
It's timetostartoverloadingsomeoperators.
I thinkthefirstoperator I'm goingtoaddisgoingtobesimplyadditionAunt.
Now I findtothesyntaxofoperatoroverloadinglittlebitclumsy, butitsayitdoesmakesenseonceyougetusedtoit.
Andtheintentionofthisaddition, ifwewilltakewhat's ontherighthandsideandaddittothelefthandsideandwillassumethatthisparticularstrokedisonthelefthandsideandsowillpassintothisoperatoroverloadtherighthandsideoratleast a referencetoit.
Sothisobjects X valuePlusequalstherighthandsidedot x valueonthesameforwhy, but I wanttoreturn a referencetothisobject, justincaseit's usedbyotherthings.
I don't wanttoreturn a pointertothisobject, so I'm goingtograbthevalueoffit.
Nowwecanseethecompilerisquitehappywiththat.
Syntax, multiplicationanddivisionaresimilar, butagain, wecan't multiplytwovectorssensibly, so I'm goingtochangeitto a scaleofvalue.
Nooperatordividematchesthestwooperas, andit's basicallysaying I can't divideonevectorbyanotherbecauseitdoesn't makeanysensebecause A is a vectorontheresultofthisresults.
In a vector, itsflagging.
Thatisanerror.
Solet's justturnthisbottomoneintosomethingcanworkwith, whichwouldbe a scalervalue.
Let's takethemagnitudeofit.
Thecompilerisquitehappy, sowe'venowgotquite a sophisticatedoperationon a typewe'vedefinedourselves, butwecanhandleitashumanslikenormalmathematics.
I canchangethesefloatstwodoublesandagain, Justushappy, butfundamentallytheunderlyingdatatypeofour X and a Y onallofthecalculationsthatareperformeduponthemhappeningusing a differentprimitivetype.
Also, thisisnow a bitof a mouthfuleverytimewewanttocreateaninstanceofourvectortype.
So I'm goingtocreatesomeconvenientshorthandtypesusingtypedeathoneforfloat, whichwillgobacktoouroriginalname, VFtwo D andonefordouble.
We'llcallup V Dytwo d.
It's allofthesenow.
I couldgobacktohowtheywere, andsotheyhaveit, a customvectortypethatbehaveslike a primitivebecausewe'veusedoperatoroverloadingtomakeitmoreconvenienttouseon.
Wecanalsouseitwithdifferentfundamentaltypes.
Itisbothusefulandpretty, verymuchunlikeme.
Infact, I likethissomuch I'm goingtoincludeitdirectlyintothepixelgameEngineHeadoffile.