Whatwe'llseeinthisvideoisthat's actuallynot a bigleap.
Infact, we'regoingtocomeupwith a wayoftreatingedgesasiftherewerecircles, and I'llalsobelookingatchangingthetimingmethodofthesimulationtomakeitmoreaccurate.
Sojustincaseyouhaven't seenpartone, and I reallyrecommendthatyoudo, because I'm notgoingtocoverallofthiscodeagain, weendedupwith a programthatlookedlikethison.
Wecouldpickup a ballandwecanseeitdealswithwhatarecalledstaticcollisions.
But I'm surethere's somecreativetypesouttherewhocancomeupwithallkindsofsimulationswherethismightbeuseful.
So I wantedtostartbytalkingaboutmakingthetimingofthesimulation a littlemoreaccurateoninthepreviousvideo, wehad a ballwhichhadanoriginpointonitsvelocity, moveditto a newpositiononthiswasover a fixedtimestep T That's allverywellandgood, becauseweknowthatspeedequalsdistanceovertimeandthereforedistanceequalsspeedtimestime.
Soifwemomentarilytreatsourvelocityvectoras a timeline, weknowthatactuallythecollisionhappenedabouthere, whichisnot a completeiPAQoftime, andthismeanswecansplittheparkintotwodifferentsections.
Idealshave a physicsenginewhereobjectscanpassthrougheachother.
Nowthere's aneasywayon a hardwaytodealwiththisontheeasywayisstillanapproximation, anditsimplysaysthatwebreakuptheparkintosmallersimulationsteps, andwechoose a simulationstepsizewhichweknowissuitabletotherestofthegameenginethatwe'reimplementing.
Theharderonyetmoreaccuratewayistocast a rayalongthevelocityvectoroftheobject.
Nowyouwedon't cutitshortsocontinuingdirectlyfromthecodelasttime, Let's getstartedcouldcreate a variableandinterviewcalled M simulationupdates, whichishowmanytimesyouwanttosubdividethepark.
And I'm goingtostartwiththevalueforSothiseffectivelymeansthatforeachframeupdate, we'regoingtorunthephysicssimulationfourtimesnow.
Ofcourse, wedon't wanttouse F elapsedtimeanymoreasthetimestepforoursimulation.
Wewanttousesomesmallerversionoffit, so I'm goingtocreate F Simelapsedtime, whichisgoingtobeequalto F elapsedtimedividedbythenumberofsimulationupdates.
Andsoeachofthephysicsupdatesinthiscaseisgoingtotake 1/4 offthe F elapsedtime, anditreallyis a caseofjusttakingallofthephysicscodeandstickingitinside a loop.
Theonlybitthatweneedtochangeinsideiswe'renolongerusing F elapsedtime.
We'regoingtobeusing F simulationelapsedtime, soweneedtochangethesevariablesandwe'lljusthave a quickruntoseewhathappens.
Andhopefullyabsolutelynothinghaschanged.
Everythingisasitwasbefore.
Ourepochisnodefinedas F simelapsedtime, butweneedtobreakthisupevenfurtherintothetimebeforethecollisiononafterthecollisionandinfact, inone e pocketspossibletoehavemultiplecollisions, allofwhichtaketheirownduration.
So I'm goingtoaddtotheballstructure a floatingpointvariable f simtimeremainingonBeforewedoanyphysicsupdates, I'm goingtosetthe F sametimeremainingforeveryball.
Soforthisframe, we'regoingtosimulatefour e poxonbeachballisgoingtokeeptrackofhowmuchtimeit's gotleftinthatepochasit's bouncingaroundnow.
Currently, inanypark, weupdatetheball's position.
Wecheckforstaticcollisionsonweresolvethedynamiccollisions, andthat's it.
Now I'm goingtodefinemylinesegmentassimplybeingtocoordinatesstart X andwhyandanend X and Y on a radius, inwhichthesamewaythatwemanipulatethebullstructure I'm goingtocreate a linesegmentstructure.
We'regoingtoconsistofcoordinatesforthestartingpointSXand S y coordinatesfortheend e x n e y Ondaradius.
Infact, I'm goingtodothingsverysimilar.
Well, we'vegot a vectortostoretheballobjects.
I'm goingtocreate a vectortostallthelineobjects.
And I know I'm alsogoingtowanttoselectandmanipulatethelinesonthescreenatsomepoint.
So I'm goingtosteellistfromhere, changethisto a linesegmentandinsteadofselectedball, willhaveselectedlightinourownuser.
CreatefunctionWeweredefiningthewifeframemodelof a circleonaddingsomeballstothesimulationwilladd a linetothesimulation, too.
So I'm justgoingtocreate a variable, whichisthelineRadiusCouldwillprobablywanttoaddseverallinesatsomepoint.
I'm goingtodraw a circleatthestartinglocationwiththelinesradius.
I'm askingit.
Insteadofshadingitinsolidcolors, it's goingtoshadeitinhalf, soit'llbe a slightlydarkgraycolorin a similarway.
I'm goingtodrawtheendofthelineusingthe E X and a y components.
Now I wanttodrawtwolinesthatrepresentthesisIEDsoffthecapsule.
Solet's justconsidermylinesegmentfor a moment.
I know I'vejustdrawntocircles, butthestartandend, What I wanttodonowisdrawanotherlinefromheretohereon a secondlinefromheretohere, that's a draw.
Lines.
I needthecoordinatesofthesepoints.
Andtogetthesepoints, what I'm goingtodoiscalculatethenormaloffmylineandmakesurethat I scalethenormaltobe a unitvectorandwantedtoUnitVector.
I cansimplymultiplyitbytheRadiustogivemetheoffsetsfromthestartandendlocations.
Sofirst I'm justgoingtocreate a normalvector, andthen I'm goingtocalculatethelengthofiton.
Once I'vegotthelengthofit, I cannormalizethevectorcomponents.
Soit's now a unitvectorandtheninoneline.
Andapologiesisgonnabe a nice, longlineofcode.
I'm goingtocallthedrilllinefunction, but I'm specifyingan X coordinate, whichisthisstartingpointplusthe X componentofthenormalmultipliedbytheradiusonagain, thestartingpointinthewhitecomponentofthenormaltimesbytheradiustotheendoftheline, too.
Well, ifwecreate a secondvectorfromthemidpointoftheballtothestartingpointofourlinesegmentinthisareahereisdeterminedasbeingthedotproductbetweenvectors P and S s two E.
Ifwedividethatbythelengthof S A, thatgivesusour T valueandthere's a finalimportantstep.
Onceweknowtheclosestpointonthelinesegmenttotheball, let's justfor a momentpretendthatthereis a ballerthelengthoftheradiuswe'vealreadysolvedknowingwhethertwocircleshaveoverlappedinthepreviousvideo, sotocheckforstaticcollisions, weneedtocreatetwovectors, thevectorthatrepresentsthelinesegmenton a vector.
Onthis, ofcourse, clampsistoouronevaluebecauseanydotproductthatyields a valuegreaterthanEJlengthdoesn't matter.
It's excellentthat's returnedbythemainfunction.
Wethenlookforthemaximumwherebetweenzeroonwhateverisreturnedhereonthisclampsisatzero, causeanythinggreaterthanzerowillbereturnedandthenwenormalizethewholethingbydividingitbytheedgelengththatgivesour T valuebetweenzeroandone, whichrepresentstheclosestpointonthelinesegmentonactuallycalculatingtheclosestpointnowisquitetrivial.
So I'm goingtoreplacethedrawerwireframecodewithsimply a PhilCirclecode.
Sinceweknowthatalloftheobjectsinthisparticularcodeup, Paul's goingtoadd a bunchmorelinesforustoplaywith, I'lljustoffsetthemverticallydownthescreentobeginwith 50 70 90.
Dothatagainforthe Y coordinatesbecausehavingmorelinestoplaywithallowsustotrysomethingsoutandseehowthedynamicsbehaveTohearofmyfaultlineson, I'm goingtocreatesomesortofbook.
It's tryandcapturethingsin.
I wanttoseewhathappenswhenwesqueezeobjectstogether.