Secondly, itmayseemlike a horriblycomplicatedthing.
Convexpolygon, collisiondetection.
Butitisn't.
I wanttofocusonthealgorithmsratherthanthecodeforthisvideo.
Andsothecodingsectionsmaygo a bitquickerthanusual.
However, I encourageyoutodownloadthesourcefilefromthegethubandstudyitforyourselves.
Let's getsomeoftheverybasicsoutofthewayfirst, asmyregularviewerswillknowbynow, I draweverythingbyhand, sotechnicallynothingyou'llseeonthescreenis a polygo.
A polygonisdefinedbypointsconnectedbystraightedges, andasyoucansee, I'm nogoodatdrawingstraightthingsonmyscreen.
onwhat I havehereiscalled a concavepolygonandconcavepolygonpresentsallsortsofproblems I'm notgoingtodiscussinthevideosimplybecausetheyhavesomethinglikethis.
And a concavepolygoncanbeidentifiedbecauseithasatleastoneinternalanglethatisgreaterthan 180 degrees.
Andsoforthisvideo, we'renotinterestedinconqueFalagunswereinterestedinconvexpolygons, whichmeansalloftheinternalanglesarelessthan 100 and 80 degrees.
Now I knowthat a largepartofmyaudiencebuildsimpleto D gamesandsowemustn't forgetthatrectanglesandsquaresarealsoconvexpolygonsonthealgorithms.
Andthismeansgivenanaxisoftheworldinour X and Y directions, thesidesofthequadrilateral a paralleltothoseaxes, checkingtoseeiftwoaxisalignedQuadralateralsoverlapisverytrivialif I labelthisonepeeonthisone.
Q.
Wecanintuitivelyseethatinthe X axis.
Ifthissideof P islessthanthissideof Q, andthisisjustfor X axis, thenthereispotentialforoverlap.
Becauseifthishighlightedsideof P wasgreaterthantherighthandsideof Q, clearlytheywouldn't beoverlapping.
Wecanconfirmthiswithanothercheckthatifthissideoff P isgreaterthanthissideof Q, theninthe X axis, thesetwoQuadralateralsareoverlapping.
Andsoif I labelthisas X on, thispointis X pluswitforthatrectangle, andthesameappliesfor Q.
Weknowthatthere's overlapinthe X axisif P X islessthanque x plus w and p x plus W isgreaterthan Q X onbecauseourquadrilleactualsaxisaligned.
Whateverappliesinthe X axisalsoappliesinthe Y axis.
Sowecansimplyduplicatetheformula, replacingXson W's forwiseon H S.
Andthisisjust a littleprogramthatdemonstratestoalternativemethodsfordetectingoverlapbetweenconvexpolygon.
Sowe'llsee.
We'vegot a selectionofconvexpolygonsonthescreen.
I'vegot a Pentagon, which I canmovearoundThelinefromthemiddleofthePentagontotheoutsidejusthelpswiththedirection, so I knowhowtosteeritwiththearrowkeys.
I couldmovethePentagonaroundwiththe W S and D keys.
I canmovethetrianglearound.
I can't movethesquarearoundinthebottomcornerandwiththefunctionkeys, I canchoosebetweenfourdifferentoptionsofthealgorithms.
ThefirstiscalledseparatedAccessTheoryonAsyou'llseeas I movethePentagonintothesquare, it's changed.
A tallandthisisquiterightbecausethey'reat 90 degreestoeachother.
If, ontheotherhand, wehad a pointgoingto 11 weendupwithonetimesoneplusonetime, zeroclearly a oneonwecanseehereifweweretodrop a shadowdownfromthatparticularpoint.
Andthenforeachpointintheshapesmodel, I calculateitstransformedpositionon.
Thisisjust a combinedtwo D matrixtransformsowecanseeherewiththeCoastsignsandthesigns I'm handlingtherotationbasedontheangleon, I'm offsettingthefinalpositionbythecentercorner.
I'm alsogoingtotakethisopportunity, since I'm goingthroughalloftheshapestosettheoverlapflagstofalse.
Finally, I wanttodrawtheshapes.
Soagain I iteratedthroughalloftheshapes.
Using a littleautoforloopondhe, I draw a linebetweensuccessivepurseofpoints, and I canaccessthesepointsthroughtheindex I.
That's why I'm notusinganautoloopherebecause I knowthatsuccessivepointsare I andIIplusone.
I justneedtobecareful, though, because I wantmyshapetobeclosedon.
I don't wanttoaccesspointsthatdon't exist.
Sowhen I'm takingontheneighboringpoint, I usedthemoduleISSfunctionwithsizeofthenumberofpointsinthepointvector.
Thismeanswecanuse a verycommonlinesegmentintosectionalgorithmtotestthetwolines.
Theoutputof a linesegmentintosectionalgorithmisusually a parametricvaluethatrepresentsthedistancealongthatline, typicallynormalizedtothelocationwherethatintersectionoccurs, andthisisusuallydenotedby t tillhave t oneforthatlineand t too.
Forthatlineinthisinstance, T wouldbeginatzerohereandendatonehereandbegin a zerohereonendofonehere.
Sowiththisdiagonalsedgeintersectapproach, notonlydowegetoverlapinformation, wealsois a byproduct.
Getinformationonhowtostaticallyresolveit, and I feelit's also a littlebitmoreintuitivethandoingvectorprojections, thoughadmittedlynotasmathematicallyclever.
SoforourDagnall, I'm goingtotakethemidpointoftheshapeasbeingthestartingpointofourlinesegmenton a pointontheboundaryoftheshapeastheendpointofthelinesegment.
Inmuchthesameway, I'm goingtocreate a linesegmentthatrepresentstheedgeofthesecondshape.
Thistime I'm taking a pointfromtheedgeon I'm takingitsneighboringpoint, rememberingtowraparoundtoclosethepolygon.
Itbehavesin a verysimilarwaytotheseparatedaccesstheir, umandit's a littlebitbeyondmymathematicalabilities, but I'm fairlycertainyoucouldprovethatthisis a veryaccuratemethod.
Butlet's notforgetthatbecausewehavegenerated T oneand T twovalues.
We'vealsogottheinformationtoresolvethiscollision.
Statically.
Insteadofmodifyingthisfunction, I'm goingtocopyitandcreate a newone.
So I'm goingtoaccumulatedisplacementdependingonhowmanyintersectionsoccur.
Soherewecanseetheoneminus t onevalueand I'm goingtodisplacealongthediagonal, whichwecanrepresentis a vectorbysubtractingthetwopointsoftheendofthediagonalslinesegment.
I directlychangetheoriginalshapeslocationbecausewehavestaticallyresolvedthecollision, thereisnochanceofoverlapoccurring, sothisfunctionnowwillalwaysreturnfalseagain.