Box2D.js 420 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866
  1. /*
  2. * Copyright (c) 2006-2007 Erin Catto http://www.gphysics.com
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty. In no event will the authors be held liable for any damages
  6. * arising from the use of this software.
  7. * Permission is granted to anyone to use this software for any purpose,
  8. * including commercial applications, and to alter it and redistribute it
  9. * freely, subject to the following restrictions:
  10. * 1. The origin of this software must not be misrepresented; you must not
  11. * claim that you wrote the original software. If you use this software
  12. * in a product, an acknowledgment in the product documentation would be
  13. * appreciated but is not required.
  14. * 2. Altered source versions must be plainly marked as such, and must not be
  15. * misrepresented as being the original software.
  16. * 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. var Box2D = {};
  19. (function (a2j, undefined) {
  20. if(!(Object.prototype.defineProperty instanceof Function)
  21. && Object.prototype.__defineGetter__ instanceof Function
  22. && Object.prototype.__defineSetter__ instanceof Function)
  23. {
  24. Object.defineProperty = function(obj, p, cfg) {
  25. if(cfg.get instanceof Function)
  26. obj.__defineGetter__(p, cfg.get);
  27. if(cfg.set instanceof Function)
  28. obj.__defineSetter__(p, cfg.set);
  29. }
  30. }
  31. function emptyFn() {};
  32. a2j.inherit = function(cls, base) {
  33. var tmpCtr = cls;
  34. emptyFn.prototype = base.prototype;
  35. cls.prototype = new emptyFn;
  36. cls.prototype.constructor = tmpCtr;
  37. };
  38. a2j.generateCallback = function generateCallback(context, cb) {
  39. return function () {
  40. cb.apply(context, arguments);
  41. };
  42. };
  43. a2j.NVector = function NVector(length) {
  44. if (length === undefined) length = 0;
  45. var tmp = new Array(length || 0);
  46. for (var i = 0; i < length; ++i)
  47. tmp[i] = 0;
  48. return tmp;
  49. };
  50. a2j.is = function is(o1, o2) {
  51. if (o1 === null) return false;
  52. if ((o2 instanceof Function) && (o1 instanceof o2)) return true;
  53. if ((o1.constructor.__implements != undefined) && (o1.constructor.__implements[o2])) return true;
  54. return false;
  55. };
  56. a2j.parseUInt = function(v) {
  57. return Math.abs(parseInt(v));
  58. }
  59. })(Box2D);
  60. //#TODO remove assignments from global namespace
  61. var Vector = Array;
  62. var Vector_a2j_Number = Box2D.NVector;
  63. //package structure
  64. if (typeof(Box2D) === "undefined") Box2D = {};
  65. if (typeof(Box2D.Collision) === "undefined") Box2D.Collision = {};
  66. if (typeof(Box2D.Collision.Shapes) === "undefined") Box2D.Collision.Shapes = {};
  67. if (typeof(Box2D.Common) === "undefined") Box2D.Common = {};
  68. if (typeof(Box2D.Common.Math) === "undefined") Box2D.Common.Math = {};
  69. if (typeof(Box2D.Dynamics) === "undefined") Box2D.Dynamics = {};
  70. if (typeof(Box2D.Dynamics.Contacts) === "undefined") Box2D.Dynamics.Contacts = {};
  71. if (typeof(Box2D.Dynamics.Controllers) === "undefined") Box2D.Dynamics.Controllers = {};
  72. if (typeof(Box2D.Dynamics.Joints) === "undefined") Box2D.Dynamics.Joints = {};
  73. //pre-definitions
  74. (function () {
  75. Box2D.Collision.IBroadPhase = 'Box2D.Collision.IBroadPhase';
  76. function b2AABB() {
  77. b2AABB.b2AABB.apply(this, arguments);
  78. };
  79. Box2D.Collision.b2AABB = b2AABB;
  80. function b2Bound() {
  81. b2Bound.b2Bound.apply(this, arguments);
  82. };
  83. Box2D.Collision.b2Bound = b2Bound;
  84. function b2BoundValues() {
  85. b2BoundValues.b2BoundValues.apply(this, arguments);
  86. if (this.constructor === b2BoundValues) this.b2BoundValues.apply(this, arguments);
  87. };
  88. Box2D.Collision.b2BoundValues = b2BoundValues;
  89. function b2Collision() {
  90. b2Collision.b2Collision.apply(this, arguments);
  91. };
  92. Box2D.Collision.b2Collision = b2Collision;
  93. function b2ContactID() {
  94. b2ContactID.b2ContactID.apply(this, arguments);
  95. if (this.constructor === b2ContactID) this.b2ContactID.apply(this, arguments);
  96. };
  97. Box2D.Collision.b2ContactID = b2ContactID;
  98. function b2ContactPoint() {
  99. b2ContactPoint.b2ContactPoint.apply(this, arguments);
  100. };
  101. Box2D.Collision.b2ContactPoint = b2ContactPoint;
  102. function b2Distance() {
  103. b2Distance.b2Distance.apply(this, arguments);
  104. };
  105. Box2D.Collision.b2Distance = b2Distance;
  106. function b2DistanceInput() {
  107. b2DistanceInput.b2DistanceInput.apply(this, arguments);
  108. };
  109. Box2D.Collision.b2DistanceInput = b2DistanceInput;
  110. function b2DistanceOutput() {
  111. b2DistanceOutput.b2DistanceOutput.apply(this, arguments);
  112. };
  113. Box2D.Collision.b2DistanceOutput = b2DistanceOutput;
  114. function b2DistanceProxy() {
  115. b2DistanceProxy.b2DistanceProxy.apply(this, arguments);
  116. };
  117. Box2D.Collision.b2DistanceProxy = b2DistanceProxy;
  118. function b2DynamicTree() {
  119. b2DynamicTree.b2DynamicTree.apply(this, arguments);
  120. if (this.constructor === b2DynamicTree) this.b2DynamicTree.apply(this, arguments);
  121. };
  122. Box2D.Collision.b2DynamicTree = b2DynamicTree;
  123. function b2DynamicTreeBroadPhase() {
  124. b2DynamicTreeBroadPhase.b2DynamicTreeBroadPhase.apply(this, arguments);
  125. };
  126. Box2D.Collision.b2DynamicTreeBroadPhase = b2DynamicTreeBroadPhase;
  127. function b2DynamicTreeNode() {
  128. b2DynamicTreeNode.b2DynamicTreeNode.apply(this, arguments);
  129. };
  130. Box2D.Collision.b2DynamicTreeNode = b2DynamicTreeNode;
  131. function b2DynamicTreePair() {
  132. b2DynamicTreePair.b2DynamicTreePair.apply(this, arguments);
  133. };
  134. Box2D.Collision.b2DynamicTreePair = b2DynamicTreePair;
  135. function b2Manifold() {
  136. b2Manifold.b2Manifold.apply(this, arguments);
  137. if (this.constructor === b2Manifold) this.b2Manifold.apply(this, arguments);
  138. };
  139. Box2D.Collision.b2Manifold = b2Manifold;
  140. function b2ManifoldPoint() {
  141. b2ManifoldPoint.b2ManifoldPoint.apply(this, arguments);
  142. if (this.constructor === b2ManifoldPoint) this.b2ManifoldPoint.apply(this, arguments);
  143. };
  144. Box2D.Collision.b2ManifoldPoint = b2ManifoldPoint;
  145. function b2Point() {
  146. b2Point.b2Point.apply(this, arguments);
  147. };
  148. Box2D.Collision.b2Point = b2Point;
  149. function b2RayCastInput() {
  150. b2RayCastInput.b2RayCastInput.apply(this, arguments);
  151. if (this.constructor === b2RayCastInput) this.b2RayCastInput.apply(this, arguments);
  152. };
  153. Box2D.Collision.b2RayCastInput = b2RayCastInput;
  154. function b2RayCastOutput() {
  155. b2RayCastOutput.b2RayCastOutput.apply(this, arguments);
  156. };
  157. Box2D.Collision.b2RayCastOutput = b2RayCastOutput;
  158. function b2Segment() {
  159. b2Segment.b2Segment.apply(this, arguments);
  160. };
  161. Box2D.Collision.b2Segment = b2Segment;
  162. function b2SeparationFunction() {
  163. b2SeparationFunction.b2SeparationFunction.apply(this, arguments);
  164. };
  165. Box2D.Collision.b2SeparationFunction = b2SeparationFunction;
  166. function b2Simplex() {
  167. b2Simplex.b2Simplex.apply(this, arguments);
  168. if (this.constructor === b2Simplex) this.b2Simplex.apply(this, arguments);
  169. };
  170. Box2D.Collision.b2Simplex = b2Simplex;
  171. function b2SimplexCache() {
  172. b2SimplexCache.b2SimplexCache.apply(this, arguments);
  173. };
  174. Box2D.Collision.b2SimplexCache = b2SimplexCache;
  175. function b2SimplexVertex() {
  176. b2SimplexVertex.b2SimplexVertex.apply(this, arguments);
  177. };
  178. Box2D.Collision.b2SimplexVertex = b2SimplexVertex;
  179. function b2TimeOfImpact() {
  180. b2TimeOfImpact.b2TimeOfImpact.apply(this, arguments);
  181. };
  182. Box2D.Collision.b2TimeOfImpact = b2TimeOfImpact;
  183. function b2TOIInput() {
  184. b2TOIInput.b2TOIInput.apply(this, arguments);
  185. };
  186. Box2D.Collision.b2TOIInput = b2TOIInput;
  187. function b2WorldManifold() {
  188. b2WorldManifold.b2WorldManifold.apply(this, arguments);
  189. if (this.constructor === b2WorldManifold) this.b2WorldManifold.apply(this, arguments);
  190. };
  191. Box2D.Collision.b2WorldManifold = b2WorldManifold;
  192. function ClipVertex() {
  193. ClipVertex.ClipVertex.apply(this, arguments);
  194. };
  195. Box2D.Collision.ClipVertex = ClipVertex;
  196. function Features() {
  197. Features.Features.apply(this, arguments);
  198. };
  199. Box2D.Collision.Features = Features;
  200. function b2CircleShape() {
  201. b2CircleShape.b2CircleShape.apply(this, arguments);
  202. if (this.constructor === b2CircleShape) this.b2CircleShape.apply(this, arguments);
  203. };
  204. Box2D.Collision.Shapes.b2CircleShape = b2CircleShape;
  205. function b2EdgeChainDef() {
  206. b2EdgeChainDef.b2EdgeChainDef.apply(this, arguments);
  207. if (this.constructor === b2EdgeChainDef) this.b2EdgeChainDef.apply(this, arguments);
  208. };
  209. Box2D.Collision.Shapes.b2EdgeChainDef = b2EdgeChainDef;
  210. function b2EdgeShape() {
  211. b2EdgeShape.b2EdgeShape.apply(this, arguments);
  212. if (this.constructor === b2EdgeShape) this.b2EdgeShape.apply(this, arguments);
  213. };
  214. Box2D.Collision.Shapes.b2EdgeShape = b2EdgeShape;
  215. function b2MassData() {
  216. b2MassData.b2MassData.apply(this, arguments);
  217. };
  218. Box2D.Collision.Shapes.b2MassData = b2MassData;
  219. function b2PolygonShape() {
  220. b2PolygonShape.b2PolygonShape.apply(this, arguments);
  221. if (this.constructor === b2PolygonShape) this.b2PolygonShape.apply(this, arguments);
  222. };
  223. Box2D.Collision.Shapes.b2PolygonShape = b2PolygonShape;
  224. function b2Shape() {
  225. b2Shape.b2Shape.apply(this, arguments);
  226. if (this.constructor === b2Shape) this.b2Shape.apply(this, arguments);
  227. };
  228. Box2D.Collision.Shapes.b2Shape = b2Shape;
  229. Box2D.Common.b2internal = 'Box2D.Common.b2internal';
  230. function b2Color() {
  231. b2Color.b2Color.apply(this, arguments);
  232. if (this.constructor === b2Color) this.b2Color.apply(this, arguments);
  233. };
  234. Box2D.Common.b2Color = b2Color;
  235. function b2Settings() {
  236. b2Settings.b2Settings.apply(this, arguments);
  237. };
  238. Box2D.Common.b2Settings = b2Settings;
  239. function b2Mat22() {
  240. b2Mat22.b2Mat22.apply(this, arguments);
  241. if (this.constructor === b2Mat22) this.b2Mat22.apply(this, arguments);
  242. };
  243. Box2D.Common.Math.b2Mat22 = b2Mat22;
  244. function b2Mat33() {
  245. b2Mat33.b2Mat33.apply(this, arguments);
  246. if (this.constructor === b2Mat33) this.b2Mat33.apply(this, arguments);
  247. };
  248. Box2D.Common.Math.b2Mat33 = b2Mat33;
  249. function b2Math() {
  250. b2Math.b2Math.apply(this, arguments);
  251. };
  252. Box2D.Common.Math.b2Math = b2Math;
  253. function b2Sweep() {
  254. b2Sweep.b2Sweep.apply(this, arguments);
  255. };
  256. Box2D.Common.Math.b2Sweep = b2Sweep;
  257. function b2Transform() {
  258. b2Transform.b2Transform.apply(this, arguments);
  259. if (this.constructor === b2Transform) this.b2Transform.apply(this, arguments);
  260. };
  261. Box2D.Common.Math.b2Transform = b2Transform;
  262. function b2Vec2() {
  263. b2Vec2.b2Vec2.apply(this, arguments);
  264. if (this.constructor === b2Vec2) this.b2Vec2.apply(this, arguments);
  265. };
  266. Box2D.Common.Math.b2Vec2 = b2Vec2;
  267. function b2Vec3() {
  268. b2Vec3.b2Vec3.apply(this, arguments);
  269. if (this.constructor === b2Vec3) this.b2Vec3.apply(this, arguments);
  270. };
  271. Box2D.Common.Math.b2Vec3 = b2Vec3;
  272. function b2Body() {
  273. b2Body.b2Body.apply(this, arguments);
  274. if (this.constructor === b2Body) this.b2Body.apply(this, arguments);
  275. };
  276. Box2D.Dynamics.b2Body = b2Body;
  277. function b2BodyDef() {
  278. b2BodyDef.b2BodyDef.apply(this, arguments);
  279. if (this.constructor === b2BodyDef) this.b2BodyDef.apply(this, arguments);
  280. };
  281. Box2D.Dynamics.b2BodyDef = b2BodyDef;
  282. function b2ContactFilter() {
  283. b2ContactFilter.b2ContactFilter.apply(this, arguments);
  284. };
  285. Box2D.Dynamics.b2ContactFilter = b2ContactFilter;
  286. function b2ContactImpulse() {
  287. b2ContactImpulse.b2ContactImpulse.apply(this, arguments);
  288. };
  289. Box2D.Dynamics.b2ContactImpulse = b2ContactImpulse;
  290. function b2ContactListener() {
  291. b2ContactListener.b2ContactListener.apply(this, arguments);
  292. };
  293. Box2D.Dynamics.b2ContactListener = b2ContactListener;
  294. function b2ContactManager() {
  295. b2ContactManager.b2ContactManager.apply(this, arguments);
  296. if (this.constructor === b2ContactManager) this.b2ContactManager.apply(this, arguments);
  297. };
  298. Box2D.Dynamics.b2ContactManager = b2ContactManager;
  299. function b2DebugDraw() {
  300. b2DebugDraw.b2DebugDraw.apply(this, arguments);
  301. if (this.constructor === b2DebugDraw) this.b2DebugDraw.apply(this, arguments);
  302. };
  303. Box2D.Dynamics.b2DebugDraw = b2DebugDraw;
  304. function b2DestructionListener() {
  305. b2DestructionListener.b2DestructionListener.apply(this, arguments);
  306. };
  307. Box2D.Dynamics.b2DestructionListener = b2DestructionListener;
  308. function b2FilterData() {
  309. b2FilterData.b2FilterData.apply(this, arguments);
  310. };
  311. Box2D.Dynamics.b2FilterData = b2FilterData;
  312. function b2Fixture() {
  313. b2Fixture.b2Fixture.apply(this, arguments);
  314. if (this.constructor === b2Fixture) this.b2Fixture.apply(this, arguments);
  315. };
  316. Box2D.Dynamics.b2Fixture = b2Fixture;
  317. function b2FixtureDef() {
  318. b2FixtureDef.b2FixtureDef.apply(this, arguments);
  319. if (this.constructor === b2FixtureDef) this.b2FixtureDef.apply(this, arguments);
  320. };
  321. Box2D.Dynamics.b2FixtureDef = b2FixtureDef;
  322. function b2Island() {
  323. b2Island.b2Island.apply(this, arguments);
  324. if (this.constructor === b2Island) this.b2Island.apply(this, arguments);
  325. };
  326. Box2D.Dynamics.b2Island = b2Island;
  327. function b2TimeStep() {
  328. b2TimeStep.b2TimeStep.apply(this, arguments);
  329. };
  330. Box2D.Dynamics.b2TimeStep = b2TimeStep;
  331. function b2World() {
  332. b2World.b2World.apply(this, arguments);
  333. if (this.constructor === b2World) this.b2World.apply(this, arguments);
  334. };
  335. Box2D.Dynamics.b2World = b2World;
  336. function b2CircleContact() {
  337. b2CircleContact.b2CircleContact.apply(this, arguments);
  338. };
  339. Box2D.Dynamics.Contacts.b2CircleContact = b2CircleContact;
  340. function b2Contact() {
  341. b2Contact.b2Contact.apply(this, arguments);
  342. if (this.constructor === b2Contact) this.b2Contact.apply(this, arguments);
  343. };
  344. Box2D.Dynamics.Contacts.b2Contact = b2Contact;
  345. function b2ContactConstraint() {
  346. b2ContactConstraint.b2ContactConstraint.apply(this, arguments);
  347. if (this.constructor === b2ContactConstraint) this.b2ContactConstraint.apply(this, arguments);
  348. };
  349. Box2D.Dynamics.Contacts.b2ContactConstraint = b2ContactConstraint;
  350. function b2ContactConstraintPoint() {
  351. b2ContactConstraintPoint.b2ContactConstraintPoint.apply(this, arguments);
  352. };
  353. Box2D.Dynamics.Contacts.b2ContactConstraintPoint = b2ContactConstraintPoint;
  354. function b2ContactEdge() {
  355. b2ContactEdge.b2ContactEdge.apply(this, arguments);
  356. };
  357. Box2D.Dynamics.Contacts.b2ContactEdge = b2ContactEdge;
  358. function b2ContactFactory() {
  359. b2ContactFactory.b2ContactFactory.apply(this, arguments);
  360. if (this.constructor === b2ContactFactory) this.b2ContactFactory.apply(this, arguments);
  361. };
  362. Box2D.Dynamics.Contacts.b2ContactFactory = b2ContactFactory;
  363. function b2ContactRegister() {
  364. b2ContactRegister.b2ContactRegister.apply(this, arguments);
  365. };
  366. Box2D.Dynamics.Contacts.b2ContactRegister = b2ContactRegister;
  367. function b2ContactResult() {
  368. b2ContactResult.b2ContactResult.apply(this, arguments);
  369. };
  370. Box2D.Dynamics.Contacts.b2ContactResult = b2ContactResult;
  371. function b2ContactSolver() {
  372. b2ContactSolver.b2ContactSolver.apply(this, arguments);
  373. if (this.constructor === b2ContactSolver) this.b2ContactSolver.apply(this, arguments);
  374. };
  375. Box2D.Dynamics.Contacts.b2ContactSolver = b2ContactSolver;
  376. function b2EdgeAndCircleContact() {
  377. b2EdgeAndCircleContact.b2EdgeAndCircleContact.apply(this, arguments);
  378. };
  379. Box2D.Dynamics.Contacts.b2EdgeAndCircleContact = b2EdgeAndCircleContact;
  380. function b2NullContact() {
  381. b2NullContact.b2NullContact.apply(this, arguments);
  382. if (this.constructor === b2NullContact) this.b2NullContact.apply(this, arguments);
  383. };
  384. Box2D.Dynamics.Contacts.b2NullContact = b2NullContact;
  385. function b2PolyAndCircleContact() {
  386. b2PolyAndCircleContact.b2PolyAndCircleContact.apply(this, arguments);
  387. };
  388. Box2D.Dynamics.Contacts.b2PolyAndCircleContact = b2PolyAndCircleContact;
  389. function b2PolyAndEdgeContact() {
  390. b2PolyAndEdgeContact.b2PolyAndEdgeContact.apply(this, arguments);
  391. };
  392. Box2D.Dynamics.Contacts.b2PolyAndEdgeContact = b2PolyAndEdgeContact;
  393. function b2PolygonContact() {
  394. b2PolygonContact.b2PolygonContact.apply(this, arguments);
  395. };
  396. Box2D.Dynamics.Contacts.b2PolygonContact = b2PolygonContact;
  397. function b2PositionSolverManifold() {
  398. b2PositionSolverManifold.b2PositionSolverManifold.apply(this, arguments);
  399. if (this.constructor === b2PositionSolverManifold) this.b2PositionSolverManifold.apply(this, arguments);
  400. };
  401. Box2D.Dynamics.Contacts.b2PositionSolverManifold = b2PositionSolverManifold;
  402. function b2BuoyancyController() {
  403. b2BuoyancyController.b2BuoyancyController.apply(this, arguments);
  404. };
  405. Box2D.Dynamics.Controllers.b2BuoyancyController = b2BuoyancyController;
  406. function b2ConstantAccelController() {
  407. b2ConstantAccelController.b2ConstantAccelController.apply(this, arguments);
  408. };
  409. Box2D.Dynamics.Controllers.b2ConstantAccelController = b2ConstantAccelController;
  410. function b2ConstantForceController() {
  411. b2ConstantForceController.b2ConstantForceController.apply(this, arguments);
  412. };
  413. Box2D.Dynamics.Controllers.b2ConstantForceController = b2ConstantForceController;
  414. function b2Controller() {
  415. b2Controller.b2Controller.apply(this, arguments);
  416. };
  417. Box2D.Dynamics.Controllers.b2Controller = b2Controller;
  418. function b2ControllerEdge() {
  419. b2ControllerEdge.b2ControllerEdge.apply(this, arguments);
  420. };
  421. Box2D.Dynamics.Controllers.b2ControllerEdge = b2ControllerEdge;
  422. function b2GravityController() {
  423. b2GravityController.b2GravityController.apply(this, arguments);
  424. };
  425. Box2D.Dynamics.Controllers.b2GravityController = b2GravityController;
  426. function b2TensorDampingController() {
  427. b2TensorDampingController.b2TensorDampingController.apply(this, arguments);
  428. };
  429. Box2D.Dynamics.Controllers.b2TensorDampingController = b2TensorDampingController;
  430. function b2DistanceJoint() {
  431. b2DistanceJoint.b2DistanceJoint.apply(this, arguments);
  432. if (this.constructor === b2DistanceJoint) this.b2DistanceJoint.apply(this, arguments);
  433. };
  434. Box2D.Dynamics.Joints.b2DistanceJoint = b2DistanceJoint;
  435. function b2DistanceJointDef() {
  436. b2DistanceJointDef.b2DistanceJointDef.apply(this, arguments);
  437. if (this.constructor === b2DistanceJointDef) this.b2DistanceJointDef.apply(this, arguments);
  438. };
  439. Box2D.Dynamics.Joints.b2DistanceJointDef = b2DistanceJointDef;
  440. function b2FrictionJoint() {
  441. b2FrictionJoint.b2FrictionJoint.apply(this, arguments);
  442. if (this.constructor === b2FrictionJoint) this.b2FrictionJoint.apply(this, arguments);
  443. };
  444. Box2D.Dynamics.Joints.b2FrictionJoint = b2FrictionJoint;
  445. function b2FrictionJointDef() {
  446. b2FrictionJointDef.b2FrictionJointDef.apply(this, arguments);
  447. if (this.constructor === b2FrictionJointDef) this.b2FrictionJointDef.apply(this, arguments);
  448. };
  449. Box2D.Dynamics.Joints.b2FrictionJointDef = b2FrictionJointDef;
  450. function b2GearJoint() {
  451. b2GearJoint.b2GearJoint.apply(this, arguments);
  452. if (this.constructor === b2GearJoint) this.b2GearJoint.apply(this, arguments);
  453. };
  454. Box2D.Dynamics.Joints.b2GearJoint = b2GearJoint;
  455. function b2GearJointDef() {
  456. b2GearJointDef.b2GearJointDef.apply(this, arguments);
  457. if (this.constructor === b2GearJointDef) this.b2GearJointDef.apply(this, arguments);
  458. };
  459. Box2D.Dynamics.Joints.b2GearJointDef = b2GearJointDef;
  460. function b2Jacobian() {
  461. b2Jacobian.b2Jacobian.apply(this, arguments);
  462. };
  463. Box2D.Dynamics.Joints.b2Jacobian = b2Jacobian;
  464. function b2Joint() {
  465. b2Joint.b2Joint.apply(this, arguments);
  466. if (this.constructor === b2Joint) this.b2Joint.apply(this, arguments);
  467. };
  468. Box2D.Dynamics.Joints.b2Joint = b2Joint;
  469. function b2JointDef() {
  470. b2JointDef.b2JointDef.apply(this, arguments);
  471. if (this.constructor === b2JointDef) this.b2JointDef.apply(this, arguments);
  472. };
  473. Box2D.Dynamics.Joints.b2JointDef = b2JointDef;
  474. function b2JointEdge() {
  475. b2JointEdge.b2JointEdge.apply(this, arguments);
  476. };
  477. Box2D.Dynamics.Joints.b2JointEdge = b2JointEdge;
  478. function b2LineJoint() {
  479. b2LineJoint.b2LineJoint.apply(this, arguments);
  480. if (this.constructor === b2LineJoint) this.b2LineJoint.apply(this, arguments);
  481. };
  482. Box2D.Dynamics.Joints.b2LineJoint = b2LineJoint;
  483. function b2LineJointDef() {
  484. b2LineJointDef.b2LineJointDef.apply(this, arguments);
  485. if (this.constructor === b2LineJointDef) this.b2LineJointDef.apply(this, arguments);
  486. };
  487. Box2D.Dynamics.Joints.b2LineJointDef = b2LineJointDef;
  488. function b2MouseJoint() {
  489. b2MouseJoint.b2MouseJoint.apply(this, arguments);
  490. if (this.constructor === b2MouseJoint) this.b2MouseJoint.apply(this, arguments);
  491. };
  492. Box2D.Dynamics.Joints.b2MouseJoint = b2MouseJoint;
  493. function b2MouseJointDef() {
  494. b2MouseJointDef.b2MouseJointDef.apply(this, arguments);
  495. if (this.constructor === b2MouseJointDef) this.b2MouseJointDef.apply(this, arguments);
  496. };
  497. Box2D.Dynamics.Joints.b2MouseJointDef = b2MouseJointDef;
  498. function b2PrismaticJoint() {
  499. b2PrismaticJoint.b2PrismaticJoint.apply(this, arguments);
  500. if (this.constructor === b2PrismaticJoint) this.b2PrismaticJoint.apply(this, arguments);
  501. };
  502. Box2D.Dynamics.Joints.b2PrismaticJoint = b2PrismaticJoint;
  503. function b2PrismaticJointDef() {
  504. b2PrismaticJointDef.b2PrismaticJointDef.apply(this, arguments);
  505. if (this.constructor === b2PrismaticJointDef) this.b2PrismaticJointDef.apply(this, arguments);
  506. };
  507. Box2D.Dynamics.Joints.b2PrismaticJointDef = b2PrismaticJointDef;
  508. function b2PulleyJoint() {
  509. b2PulleyJoint.b2PulleyJoint.apply(this, arguments);
  510. if (this.constructor === b2PulleyJoint) this.b2PulleyJoint.apply(this, arguments);
  511. };
  512. Box2D.Dynamics.Joints.b2PulleyJoint = b2PulleyJoint;
  513. function b2PulleyJointDef() {
  514. b2PulleyJointDef.b2PulleyJointDef.apply(this, arguments);
  515. if (this.constructor === b2PulleyJointDef) this.b2PulleyJointDef.apply(this, arguments);
  516. };
  517. Box2D.Dynamics.Joints.b2PulleyJointDef = b2PulleyJointDef;
  518. function b2RevoluteJoint() {
  519. b2RevoluteJoint.b2RevoluteJoint.apply(this, arguments);
  520. if (this.constructor === b2RevoluteJoint) this.b2RevoluteJoint.apply(this, arguments);
  521. };
  522. Box2D.Dynamics.Joints.b2RevoluteJoint = b2RevoluteJoint;
  523. function b2RevoluteJointDef() {
  524. b2RevoluteJointDef.b2RevoluteJointDef.apply(this, arguments);
  525. if (this.constructor === b2RevoluteJointDef) this.b2RevoluteJointDef.apply(this, arguments);
  526. };
  527. Box2D.Dynamics.Joints.b2RevoluteJointDef = b2RevoluteJointDef;
  528. function b2WeldJoint() {
  529. b2WeldJoint.b2WeldJoint.apply(this, arguments);
  530. if (this.constructor === b2WeldJoint) this.b2WeldJoint.apply(this, arguments);
  531. };
  532. Box2D.Dynamics.Joints.b2WeldJoint = b2WeldJoint;
  533. function b2WeldJointDef() {
  534. b2WeldJointDef.b2WeldJointDef.apply(this, arguments);
  535. if (this.constructor === b2WeldJointDef) this.b2WeldJointDef.apply(this, arguments);
  536. };
  537. Box2D.Dynamics.Joints.b2WeldJointDef = b2WeldJointDef;
  538. })(); //definitions
  539. Box2D.postDefs = [];
  540. (function () {
  541. var b2CircleShape = Box2D.Collision.Shapes.b2CircleShape,
  542. b2EdgeChainDef = Box2D.Collision.Shapes.b2EdgeChainDef,
  543. b2EdgeShape = Box2D.Collision.Shapes.b2EdgeShape,
  544. b2MassData = Box2D.Collision.Shapes.b2MassData,
  545. b2PolygonShape = Box2D.Collision.Shapes.b2PolygonShape,
  546. b2Shape = Box2D.Collision.Shapes.b2Shape,
  547. b2Color = Box2D.Common.b2Color,
  548. b2internal = Box2D.Common.b2internal,
  549. b2Settings = Box2D.Common.b2Settings,
  550. b2Mat22 = Box2D.Common.Math.b2Mat22,
  551. b2Mat33 = Box2D.Common.Math.b2Mat33,
  552. b2Math = Box2D.Common.Math.b2Math,
  553. b2Sweep = Box2D.Common.Math.b2Sweep,
  554. b2Transform = Box2D.Common.Math.b2Transform,
  555. b2Vec2 = Box2D.Common.Math.b2Vec2,
  556. b2Vec3 = Box2D.Common.Math.b2Vec3,
  557. b2AABB = Box2D.Collision.b2AABB,
  558. b2Bound = Box2D.Collision.b2Bound,
  559. b2BoundValues = Box2D.Collision.b2BoundValues,
  560. b2Collision = Box2D.Collision.b2Collision,
  561. b2ContactID = Box2D.Collision.b2ContactID,
  562. b2ContactPoint = Box2D.Collision.b2ContactPoint,
  563. b2Distance = Box2D.Collision.b2Distance,
  564. b2DistanceInput = Box2D.Collision.b2DistanceInput,
  565. b2DistanceOutput = Box2D.Collision.b2DistanceOutput,
  566. b2DistanceProxy = Box2D.Collision.b2DistanceProxy,
  567. b2DynamicTree = Box2D.Collision.b2DynamicTree,
  568. b2DynamicTreeBroadPhase = Box2D.Collision.b2DynamicTreeBroadPhase,
  569. b2DynamicTreeNode = Box2D.Collision.b2DynamicTreeNode,
  570. b2DynamicTreePair = Box2D.Collision.b2DynamicTreePair,
  571. b2Manifold = Box2D.Collision.b2Manifold,
  572. b2ManifoldPoint = Box2D.Collision.b2ManifoldPoint,
  573. b2Point = Box2D.Collision.b2Point,
  574. b2RayCastInput = Box2D.Collision.b2RayCastInput,
  575. b2RayCastOutput = Box2D.Collision.b2RayCastOutput,
  576. b2Segment = Box2D.Collision.b2Segment,
  577. b2SeparationFunction = Box2D.Collision.b2SeparationFunction,
  578. b2Simplex = Box2D.Collision.b2Simplex,
  579. b2SimplexCache = Box2D.Collision.b2SimplexCache,
  580. b2SimplexVertex = Box2D.Collision.b2SimplexVertex,
  581. b2TimeOfImpact = Box2D.Collision.b2TimeOfImpact,
  582. b2TOIInput = Box2D.Collision.b2TOIInput,
  583. b2WorldManifold = Box2D.Collision.b2WorldManifold,
  584. ClipVertex = Box2D.Collision.ClipVertex,
  585. Features = Box2D.Collision.Features,
  586. IBroadPhase = Box2D.Collision.IBroadPhase;
  587. b2AABB.b2AABB = function () {
  588. this.lowerBound = new b2Vec2();
  589. this.upperBound = new b2Vec2();
  590. };
  591. b2AABB.prototype.IsValid = function () {
  592. var dX = this.upperBound.x - this.lowerBound.x;
  593. var dY = this.upperBound.y - this.lowerBound.y;
  594. var valid = dX >= 0.0 && dY >= 0.0;
  595. valid = valid && this.lowerBound.IsValid() && this.upperBound.IsValid();
  596. return valid;
  597. }
  598. b2AABB.prototype.GetCenter = function () {
  599. return new b2Vec2((this.lowerBound.x + this.upperBound.x) / 2, (this.lowerBound.y + this.upperBound.y) / 2);
  600. }
  601. b2AABB.prototype.GetExtents = function () {
  602. return new b2Vec2((this.upperBound.x - this.lowerBound.x) / 2, (this.upperBound.y - this.lowerBound.y) / 2);
  603. }
  604. b2AABB.prototype.Contains = function (aabb) {
  605. var result = true;
  606. result = result && this.lowerBound.x <= aabb.lowerBound.x;
  607. result = result && this.lowerBound.y <= aabb.lowerBound.y;
  608. result = result && aabb.upperBound.x <= this.upperBound.x;
  609. result = result && aabb.upperBound.y <= this.upperBound.y;
  610. return result;
  611. }
  612. b2AABB.prototype.RayCast = function (output, input) {
  613. var tmin = (-Number.MAX_VALUE);
  614. var tmax = Number.MAX_VALUE;
  615. var pX = input.p1.x;
  616. var pY = input.p1.y;
  617. var dX = input.p2.x - input.p1.x;
  618. var dY = input.p2.y - input.p1.y;
  619. var absDX = Math.abs(dX);
  620. var absDY = Math.abs(dY);
  621. var normal = output.normal;
  622. var inv_d = 0;
  623. var t1 = 0;
  624. var t2 = 0;
  625. var t3 = 0;
  626. var s = 0; {
  627. if (absDX < Number.MIN_VALUE) {
  628. if (pX < this.lowerBound.x || this.upperBound.x < pX) return false;
  629. }
  630. else {
  631. inv_d = 1.0 / dX;
  632. t1 = (this.lowerBound.x - pX) * inv_d;
  633. t2 = (this.upperBound.x - pX) * inv_d;
  634. s = (-1.0);
  635. if (t1 > t2) {
  636. t3 = t1;
  637. t1 = t2;
  638. t2 = t3;
  639. s = 1.0;
  640. }
  641. if (t1 > tmin) {
  642. normal.x = s;
  643. normal.y = 0;
  644. tmin = t1;
  645. }
  646. tmax = Math.min(tmax, t2);
  647. if (tmin > tmax) return false;
  648. }
  649. } {
  650. if (absDY < Number.MIN_VALUE) {
  651. if (pY < this.lowerBound.y || this.upperBound.y < pY) return false;
  652. }
  653. else {
  654. inv_d = 1.0 / dY;
  655. t1 = (this.lowerBound.y - pY) * inv_d;
  656. t2 = (this.upperBound.y - pY) * inv_d;
  657. s = (-1.0);
  658. if (t1 > t2) {
  659. t3 = t1;
  660. t1 = t2;
  661. t2 = t3;
  662. s = 1.0;
  663. }
  664. if (t1 > tmin) {
  665. normal.y = s;
  666. normal.x = 0;
  667. tmin = t1;
  668. }
  669. tmax = Math.min(tmax, t2);
  670. if (tmin > tmax) return false;
  671. }
  672. }
  673. output.fraction = tmin;
  674. return true;
  675. }
  676. b2AABB.prototype.TestOverlap = function (other) {
  677. var d1X = other.lowerBound.x - this.upperBound.x;
  678. var d1Y = other.lowerBound.y - this.upperBound.y;
  679. var d2X = this.lowerBound.x - other.upperBound.x;
  680. var d2Y = this.lowerBound.y - other.upperBound.y;
  681. if (d1X > 0.0 || d1Y > 0.0) return false;
  682. if (d2X > 0.0 || d2Y > 0.0) return false;
  683. return true;
  684. }
  685. b2AABB.Combine = function (aabb1, aabb2) {
  686. var aabb = new b2AABB();
  687. aabb.Combine(aabb1, aabb2);
  688. return aabb;
  689. }
  690. b2AABB.prototype.Combine = function (aabb1, aabb2) {
  691. this.lowerBound.x = Math.min(aabb1.lowerBound.x, aabb2.lowerBound.x);
  692. this.lowerBound.y = Math.min(aabb1.lowerBound.y, aabb2.lowerBound.y);
  693. this.upperBound.x = Math.max(aabb1.upperBound.x, aabb2.upperBound.x);
  694. this.upperBound.y = Math.max(aabb1.upperBound.y, aabb2.upperBound.y);
  695. }
  696. b2Bound.b2Bound = function () {};
  697. b2Bound.prototype.IsLower = function () {
  698. return (this.value & 1) == 0;
  699. }
  700. b2Bound.prototype.IsUpper = function () {
  701. return (this.value & 1) == 1;
  702. }
  703. b2Bound.prototype.Swap = function (b) {
  704. var tempValue = this.value;
  705. var tempProxy = this.proxy;
  706. var tempStabbingCount = this.stabbingCount;
  707. this.value = b.value;
  708. this.proxy = b.proxy;
  709. this.stabbingCount = b.stabbingCount;
  710. b.value = tempValue;
  711. b.proxy = tempProxy;
  712. b.stabbingCount = tempStabbingCount;
  713. }
  714. b2BoundValues.b2BoundValues = function () {};
  715. b2BoundValues.prototype.b2BoundValues = function () {
  716. this.lowerValues = new Vector_a2j_Number();
  717. this.lowerValues[0] = 0.0;
  718. this.lowerValues[1] = 0.0;
  719. this.upperValues = new Vector_a2j_Number();
  720. this.upperValues[0] = 0.0;
  721. this.upperValues[1] = 0.0;
  722. }
  723. b2Collision.b2Collision = function () {};
  724. b2Collision.ClipSegmentToLine = function (vOut, vIn, normal, offset) {
  725. if (offset === undefined) offset = 0;
  726. var cv;
  727. var numOut = 0;
  728. cv = vIn[0];
  729. var vIn0 = cv.v;
  730. cv = vIn[1];
  731. var vIn1 = cv.v;
  732. var distance0 = normal.x * vIn0.x + normal.y * vIn0.y - offset;
  733. var distance1 = normal.x * vIn1.x + normal.y * vIn1.y - offset;
  734. if (distance0 <= 0.0) vOut[numOut++].Set(vIn[0]);
  735. if (distance1 <= 0.0) vOut[numOut++].Set(vIn[1]);
  736. if (distance0 * distance1 < 0.0) {
  737. var interp = distance0 / (distance0 - distance1);
  738. cv = vOut[numOut];
  739. var tVec = cv.v;
  740. tVec.x = vIn0.x + interp * (vIn1.x - vIn0.x);
  741. tVec.y = vIn0.y + interp * (vIn1.y - vIn0.y);
  742. cv = vOut[numOut];
  743. var cv2;
  744. if (distance0 > 0.0) {
  745. cv2 = vIn[0];
  746. cv.id = cv2.id;
  747. }
  748. else {
  749. cv2 = vIn[1];
  750. cv.id = cv2.id;
  751. }++numOut;
  752. }
  753. return numOut;
  754. }
  755. b2Collision.EdgeSeparation = function (poly1, xf1, edge1, poly2, xf2) {
  756. if (edge1 === undefined) edge1 = 0;
  757. var count1 = parseInt(poly1.m_vertexCount);
  758. var vertices1 = poly1.m_vertices;
  759. var normals1 = poly1.m_normals;
  760. var count2 = parseInt(poly2.m_vertexCount);
  761. var vertices2 = poly2.m_vertices;
  762. var tMat;
  763. var tVec;
  764. tMat = xf1.R;
  765. tVec = normals1[edge1];
  766. var normal1WorldX = (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
  767. var normal1WorldY = (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
  768. tMat = xf2.R;
  769. var normal1X = (tMat.col1.x * normal1WorldX + tMat.col1.y * normal1WorldY);
  770. var normal1Y = (tMat.col2.x * normal1WorldX + tMat.col2.y * normal1WorldY);
  771. var index = 0;
  772. var minDot = Number.MAX_VALUE;
  773. for (var i = 0; i < count2; ++i) {
  774. tVec = vertices2[i];
  775. var dot = tVec.x * normal1X + tVec.y * normal1Y;
  776. if (dot < minDot) {
  777. minDot = dot;
  778. index = i;
  779. }
  780. }
  781. tVec = vertices1[edge1];
  782. tMat = xf1.R;
  783. var v1X = xf1.position.x + (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
  784. var v1Y = xf1.position.y + (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
  785. tVec = vertices2[index];
  786. tMat = xf2.R;
  787. var v2X = xf2.position.x + (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
  788. var v2Y = xf2.position.y + (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
  789. v2X -= v1X;
  790. v2Y -= v1Y;
  791. var separation = v2X * normal1WorldX + v2Y * normal1WorldY;
  792. return separation;
  793. }
  794. b2Collision.FindMaxSeparation = function (edgeIndex, poly1, xf1, poly2, xf2) {
  795. var count1 = parseInt(poly1.m_vertexCount);
  796. var normals1 = poly1.m_normals;
  797. var tVec;
  798. var tMat;
  799. tMat = xf2.R;
  800. tVec = poly2.m_centroid;
  801. var dX = xf2.position.x + (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
  802. var dY = xf2.position.y + (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
  803. tMat = xf1.R;
  804. tVec = poly1.m_centroid;
  805. dX -= xf1.position.x + (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
  806. dY -= xf1.position.y + (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
  807. var dLocal1X = (dX * xf1.R.col1.x + dY * xf1.R.col1.y);
  808. var dLocal1Y = (dX * xf1.R.col2.x + dY * xf1.R.col2.y);
  809. var edge = 0;
  810. var maxDot = (-Number.MAX_VALUE);
  811. for (var i = 0; i < count1; ++i) {
  812. tVec = normals1[i];
  813. var dot = (tVec.x * dLocal1X + tVec.y * dLocal1Y);
  814. if (dot > maxDot) {
  815. maxDot = dot;
  816. edge = i;
  817. }
  818. }
  819. var s = b2Collision.EdgeSeparation(poly1, xf1, edge, poly2, xf2);
  820. var prevEdge = parseInt(edge - 1 >= 0 ? edge - 1 : count1 - 1);
  821. var sPrev = b2Collision.EdgeSeparation(poly1, xf1, prevEdge, poly2, xf2);
  822. var nextEdge = parseInt(edge + 1 < count1 ? edge + 1 : 0);
  823. var sNext = b2Collision.EdgeSeparation(poly1, xf1, nextEdge, poly2, xf2);
  824. var bestEdge = 0;
  825. var bestSeparation = 0;
  826. var increment = 0;
  827. if (sPrev > s && sPrev > sNext) {
  828. increment = (-1);
  829. bestEdge = prevEdge;
  830. bestSeparation = sPrev;
  831. }
  832. else if (sNext > s) {
  833. increment = 1;
  834. bestEdge = nextEdge;
  835. bestSeparation = sNext;
  836. }
  837. else {
  838. edgeIndex[0] = edge;
  839. return s;
  840. }
  841. while (true) {
  842. if (increment == (-1)) edge = bestEdge - 1 >= 0 ? bestEdge - 1 : count1 - 1;
  843. else edge = bestEdge + 1 < count1 ? bestEdge + 1 : 0;s = b2Collision.EdgeSeparation(poly1, xf1, edge, poly2, xf2);
  844. if (s > bestSeparation) {
  845. bestEdge = edge;
  846. bestSeparation = s;
  847. }
  848. else {
  849. break;
  850. }
  851. }
  852. edgeIndex[0] = bestEdge;
  853. return bestSeparation;
  854. }
  855. b2Collision.FindIncidentEdge = function (c, poly1, xf1, edge1, poly2, xf2) {
  856. if (edge1 === undefined) edge1 = 0;
  857. var count1 = parseInt(poly1.m_vertexCount);
  858. var normals1 = poly1.m_normals;
  859. var count2 = parseInt(poly2.m_vertexCount);
  860. var vertices2 = poly2.m_vertices;
  861. var normals2 = poly2.m_normals;
  862. var tMat;
  863. var tVec;
  864. tMat = xf1.R;
  865. tVec = normals1[edge1];
  866. var normal1X = (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
  867. var normal1Y = (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
  868. tMat = xf2.R;
  869. var tX = (tMat.col1.x * normal1X + tMat.col1.y * normal1Y);
  870. normal1Y = (tMat.col2.x * normal1X + tMat.col2.y * normal1Y);
  871. normal1X = tX;
  872. var index = 0;
  873. var minDot = Number.MAX_VALUE;
  874. for (var i = 0; i < count2; ++i) {
  875. tVec = normals2[i];
  876. var dot = (normal1X * tVec.x + normal1Y * tVec.y);
  877. if (dot < minDot) {
  878. minDot = dot;
  879. index = i;
  880. }
  881. }
  882. var tClip;
  883. var i1 = parseInt(index);
  884. var i2 = parseInt(i1 + 1 < count2 ? i1 + 1 : 0);
  885. tClip = c[0];
  886. tVec = vertices2[i1];
  887. tMat = xf2.R;
  888. tClip.v.x = xf2.position.x + (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
  889. tClip.v.y = xf2.position.y + (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
  890. tClip.id.features.referenceEdge = edge1;
  891. tClip.id.features.incidentEdge = i1;
  892. tClip.id.features.incidentVertex = 0;
  893. tClip = c[1];
  894. tVec = vertices2[i2];
  895. tMat = xf2.R;
  896. tClip.v.x = xf2.position.x + (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
  897. tClip.v.y = xf2.position.y + (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
  898. tClip.id.features.referenceEdge = edge1;
  899. tClip.id.features.incidentEdge = i2;
  900. tClip.id.features.incidentVertex = 1;
  901. }
  902. b2Collision.MakeClipPointVector = function () {
  903. var r = new Vector(2);
  904. r[0] = new ClipVertex();
  905. r[1] = new ClipVertex();
  906. return r;
  907. }
  908. b2Collision.CollidePolygons = function (manifold, polyA, xfA, polyB, xfB) {
  909. var cv;
  910. manifold.m_pointCount = 0;
  911. var totalRadius = polyA.m_radius + polyB.m_radius;
  912. var edgeA = 0;
  913. b2Collision.s_edgeAO[0] = edgeA;
  914. var separationA = b2Collision.FindMaxSeparation(b2Collision.s_edgeAO, polyA, xfA, polyB, xfB);
  915. edgeA = b2Collision.s_edgeAO[0];
  916. if (separationA > totalRadius) return;
  917. var edgeB = 0;
  918. b2Collision.s_edgeBO[0] = edgeB;
  919. var separationB = b2Collision.FindMaxSeparation(b2Collision.s_edgeBO, polyB, xfB, polyA, xfA);
  920. edgeB = b2Collision.s_edgeBO[0];
  921. if (separationB > totalRadius) return;
  922. var poly1;
  923. var poly2;
  924. var xf1;
  925. var xf2;
  926. var edge1 = 0;
  927. var flip = 0;
  928. var k_relativeTol = 0.98;
  929. var k_absoluteTol = 0.001;
  930. var tMat;
  931. if (separationB > k_relativeTol * separationA + k_absoluteTol) {
  932. poly1 = polyB;
  933. poly2 = polyA;
  934. xf1 = xfB;
  935. xf2 = xfA;
  936. edge1 = edgeB;
  937. manifold.m_type = b2Manifold.e_faceB;
  938. flip = 1;
  939. }
  940. else {
  941. poly1 = polyA;
  942. poly2 = polyB;
  943. xf1 = xfA;
  944. xf2 = xfB;
  945. edge1 = edgeA;
  946. manifold.m_type = b2Manifold.e_faceA;
  947. flip = 0;
  948. }
  949. var incidentEdge = b2Collision.s_incidentEdge;
  950. b2Collision.FindIncidentEdge(incidentEdge, poly1, xf1, edge1, poly2, xf2);
  951. var count1 = parseInt(poly1.m_vertexCount);
  952. var vertices1 = poly1.m_vertices;
  953. var local_v11 = vertices1[edge1];
  954. var local_v12;
  955. if (edge1 + 1 < count1) {
  956. local_v12 = vertices1[parseInt(edge1 + 1)];
  957. }
  958. else {
  959. local_v12 = vertices1[0];
  960. }
  961. var localTangent = b2Collision.s_localTangent;
  962. localTangent.Set(local_v12.x - local_v11.x, local_v12.y - local_v11.y);
  963. localTangent.Normalize();
  964. var localNormal = b2Collision.s_localNormal;
  965. localNormal.x = localTangent.y;
  966. localNormal.y = (-localTangent.x);
  967. var planePoint = b2Collision.s_planePoint;
  968. planePoint.Set(0.5 * (local_v11.x + local_v12.x), 0.5 * (local_v11.y + local_v12.y));
  969. var tangent = b2Collision.s_tangent;
  970. tMat = xf1.R;
  971. tangent.x = (tMat.col1.x * localTangent.x + tMat.col2.x * localTangent.y);
  972. tangent.y = (tMat.col1.y * localTangent.x + tMat.col2.y * localTangent.y);
  973. var tangent2 = b2Collision.s_tangent2;
  974. tangent2.x = (-tangent.x);
  975. tangent2.y = (-tangent.y);
  976. var normal = b2Collision.s_normal;
  977. normal.x = tangent.y;
  978. normal.y = (-tangent.x);
  979. var v11 = b2Collision.s_v11;
  980. var v12 = b2Collision.s_v12;
  981. v11.x = xf1.position.x + (tMat.col1.x * local_v11.x + tMat.col2.x * local_v11.y);
  982. v11.y = xf1.position.y + (tMat.col1.y * local_v11.x + tMat.col2.y * local_v11.y);
  983. v12.x = xf1.position.x + (tMat.col1.x * local_v12.x + tMat.col2.x * local_v12.y);
  984. v12.y = xf1.position.y + (tMat.col1.y * local_v12.x + tMat.col2.y * local_v12.y);
  985. var frontOffset = normal.x * v11.x + normal.y * v11.y;
  986. var sideOffset1 = (-tangent.x * v11.x) - tangent.y * v11.y + totalRadius;
  987. var sideOffset2 = tangent.x * v12.x + tangent.y * v12.y + totalRadius;
  988. var clipPoints1 = b2Collision.s_clipPoints1;
  989. var clipPoints2 = b2Collision.s_clipPoints2;
  990. var np = 0;
  991. np = b2Collision.ClipSegmentToLine(clipPoints1, incidentEdge, tangent2, sideOffset1);
  992. if (np < 2) return;
  993. np = b2Collision.ClipSegmentToLine(clipPoints2, clipPoints1, tangent, sideOffset2);
  994. if (np < 2) return;
  995. manifold.m_localPlaneNormal.SetV(localNormal);
  996. manifold.m_localPoint.SetV(planePoint);
  997. var pointCount = 0;
  998. for (var i = 0; i < b2Settings.b2_maxManifoldPoints; ++i) {
  999. cv = clipPoints2[i];
  1000. var separation = normal.x * cv.v.x + normal.y * cv.v.y - frontOffset;
  1001. if (separation <= totalRadius) {
  1002. var cp = manifold.m_points[pointCount];
  1003. tMat = xf2.R;
  1004. var tX = cv.v.x - xf2.position.x;
  1005. var tY = cv.v.y - xf2.position.y;
  1006. cp.m_localPoint.x = (tX * tMat.col1.x + tY * tMat.col1.y);
  1007. cp.m_localPoint.y = (tX * tMat.col2.x + tY * tMat.col2.y);
  1008. cp.m_id.Set(cv.id);
  1009. cp.m_id.features.flip = flip;
  1010. ++pointCount;
  1011. }
  1012. }
  1013. manifold.m_pointCount = pointCount;
  1014. }
  1015. b2Collision.CollideCircles = function (manifold, circle1, xf1, circle2, xf2) {
  1016. manifold.m_pointCount = 0;
  1017. var tMat;
  1018. var tVec;
  1019. tMat = xf1.R;
  1020. tVec = circle1.m_p;
  1021. var p1X = xf1.position.x + (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
  1022. var p1Y = xf1.position.y + (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
  1023. tMat = xf2.R;
  1024. tVec = circle2.m_p;
  1025. var p2X = xf2.position.x + (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
  1026. var p2Y = xf2.position.y + (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
  1027. var dX = p2X - p1X;
  1028. var dY = p2Y - p1Y;
  1029. var distSqr = dX * dX + dY * dY;
  1030. var radius = circle1.m_radius + circle2.m_radius;
  1031. if (distSqr > radius * radius) {
  1032. return;
  1033. }
  1034. manifold.m_type = b2Manifold.e_circles;
  1035. manifold.m_localPoint.SetV(circle1.m_p);
  1036. manifold.m_localPlaneNormal.SetZero();
  1037. manifold.m_pointCount = 1;
  1038. manifold.m_points[0].m_localPoint.SetV(circle2.m_p);
  1039. manifold.m_points[0].m_id.key = 0;
  1040. }
  1041. b2Collision.CollidePolygonAndCircle = function (manifold, polygon, xf1, circle, xf2) {
  1042. manifold.m_pointCount = 0;
  1043. var tPoint;
  1044. var dX = 0;
  1045. var dY = 0;
  1046. var positionX = 0;
  1047. var positionY = 0;
  1048. var tVec;
  1049. var tMat;
  1050. tMat = xf2.R;
  1051. tVec = circle.m_p;
  1052. var cX = xf2.position.x + (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
  1053. var cY = xf2.position.y + (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
  1054. dX = cX - xf1.position.x;
  1055. dY = cY - xf1.position.y;
  1056. tMat = xf1.R;
  1057. var cLocalX = (dX * tMat.col1.x + dY * tMat.col1.y);
  1058. var cLocalY = (dX * tMat.col2.x + dY * tMat.col2.y);
  1059. var dist = 0;
  1060. var normalIndex = 0;
  1061. var separation = (-Number.MAX_VALUE);
  1062. var radius = polygon.m_radius + circle.m_radius;
  1063. var vertexCount = parseInt(polygon.m_vertexCount);
  1064. var vertices = polygon.m_vertices;
  1065. var normals = polygon.m_normals;
  1066. for (var i = 0; i < vertexCount; ++i) {
  1067. tVec = vertices[i];
  1068. dX = cLocalX - tVec.x;
  1069. dY = cLocalY - tVec.y;
  1070. tVec = normals[i];
  1071. var s = tVec.x * dX + tVec.y * dY;
  1072. if (s > radius) {
  1073. return;
  1074. }
  1075. if (s > separation) {
  1076. separation = s;
  1077. normalIndex = i;
  1078. }
  1079. }
  1080. var vertIndex1 = parseInt(normalIndex);
  1081. var vertIndex2 = parseInt(vertIndex1 + 1 < vertexCount ? vertIndex1 + 1 : 0);
  1082. var v1 = vertices[vertIndex1];
  1083. var v2 = vertices[vertIndex2];
  1084. if (separation < Number.MIN_VALUE) {
  1085. manifold.m_pointCount = 1;
  1086. manifold.m_type = b2Manifold.e_faceA;
  1087. manifold.m_localPlaneNormal.SetV(normals[normalIndex]);
  1088. manifold.m_localPoint.x = 0.5 * (v1.x + v2.x);
  1089. manifold.m_localPoint.y = 0.5 * (v1.y + v2.y);
  1090. manifold.m_points[0].m_localPoint.SetV(circle.m_p);
  1091. manifold.m_points[0].m_id.key = 0;
  1092. return;
  1093. }
  1094. var u1 = (cLocalX - v1.x) * (v2.x - v1.x) + (cLocalY - v1.y) * (v2.y - v1.y);
  1095. var u2 = (cLocalX - v2.x) * (v1.x - v2.x) + (cLocalY - v2.y) * (v1.y - v2.y);
  1096. if (u1 <= 0.0) {
  1097. if ((cLocalX - v1.x) * (cLocalX - v1.x) + (cLocalY - v1.y) * (cLocalY - v1.y) > radius * radius) return;
  1098. manifold.m_pointCount = 1;
  1099. manifold.m_type = b2Manifold.e_faceA;
  1100. manifold.m_localPlaneNormal.x = cLocalX - v1.x;
  1101. manifold.m_localPlaneNormal.y = cLocalY - v1.y;
  1102. manifold.m_localPlaneNormal.Normalize();
  1103. manifold.m_localPoint.SetV(v1);
  1104. manifold.m_points[0].m_localPoint.SetV(circle.m_p);
  1105. manifold.m_points[0].m_id.key = 0;
  1106. }
  1107. else if (u2 <= 0) {
  1108. if ((cLocalX - v2.x) * (cLocalX - v2.x) + (cLocalY - v2.y) * (cLocalY - v2.y) > radius * radius) return;
  1109. manifold.m_pointCount = 1;
  1110. manifold.m_type = b2Manifold.e_faceA;
  1111. manifold.m_localPlaneNormal.x = cLocalX - v2.x;
  1112. manifold.m_localPlaneNormal.y = cLocalY - v2.y;
  1113. manifold.m_localPlaneNormal.Normalize();
  1114. manifold.m_localPoint.SetV(v2);
  1115. manifold.m_points[0].m_localPoint.SetV(circle.m_p);
  1116. manifold.m_points[0].m_id.key = 0;
  1117. }
  1118. else {
  1119. var faceCenterX = 0.5 * (v1.x + v2.x);
  1120. var faceCenterY = 0.5 * (v1.y + v2.y);
  1121. separation = (cLocalX - faceCenterX) * normals[vertIndex1].x + (cLocalY - faceCenterY) * normals[vertIndex1].y;
  1122. if (separation > radius) return;
  1123. manifold.m_pointCount = 1;
  1124. manifold.m_type = b2Manifold.e_faceA;
  1125. manifold.m_localPlaneNormal.x = normals[vertIndex1].x;
  1126. manifold.m_localPlaneNormal.y = normals[vertIndex1].y;
  1127. manifold.m_localPlaneNormal.Normalize();
  1128. manifold.m_localPoint.Set(faceCenterX, faceCenterY);
  1129. manifold.m_points[0].m_localPoint.SetV(circle.m_p);
  1130. manifold.m_points[0].m_id.key = 0;
  1131. }
  1132. }
  1133. b2Collision.TestOverlap = function (a, b) {
  1134. var t1 = b.lowerBound;
  1135. var t2 = a.upperBound;
  1136. var d1X = t1.x - t2.x;
  1137. var d1Y = t1.y - t2.y;
  1138. t1 = a.lowerBound;
  1139. t2 = b.upperBound;
  1140. var d2X = t1.x - t2.x;
  1141. var d2Y = t1.y - t2.y;
  1142. if (d1X > 0.0 || d1Y > 0.0) return false;
  1143. if (d2X > 0.0 || d2Y > 0.0) return false;
  1144. return true;
  1145. }
  1146. Box2D.postDefs.push(function () {
  1147. Box2D.Collision.b2Collision.s_incidentEdge = b2Collision.MakeClipPointVector();
  1148. Box2D.Collision.b2Collision.s_clipPoints1 = b2Collision.MakeClipPointVector();
  1149. Box2D.Collision.b2Collision.s_clipPoints2 = b2Collision.MakeClipPointVector();
  1150. Box2D.Collision.b2Collision.s_edgeAO = new Vector_a2j_Number(1);
  1151. Box2D.Collision.b2Collision.s_edgeBO = new Vector_a2j_Number(1);
  1152. Box2D.Collision.b2Collision.s_localTangent = new b2Vec2();
  1153. Box2D.Collision.b2Collision.s_localNormal = new b2Vec2();
  1154. Box2D.Collision.b2Collision.s_planePoint = new b2Vec2();
  1155. Box2D.Collision.b2Collision.s_normal = new b2Vec2();
  1156. Box2D.Collision.b2Collision.s_tangent = new b2Vec2();
  1157. Box2D.Collision.b2Collision.s_tangent2 = new b2Vec2();
  1158. Box2D.Collision.b2Collision.s_v11 = new b2Vec2();
  1159. Box2D.Collision.b2Collision.s_v12 = new b2Vec2();
  1160. Box2D.Collision.b2Collision.b2CollidePolyTempVec = new b2Vec2();
  1161. Box2D.Collision.b2Collision.b2_nullFeature = 0x000000ff;
  1162. });
  1163. b2ContactID.b2ContactID = function () {
  1164. this.features = new Features();
  1165. };
  1166. b2ContactID.prototype.b2ContactID = function () {
  1167. this.features._m_id = this;
  1168. }
  1169. b2ContactID.prototype.Set = function (id) {
  1170. this.key = id._key;
  1171. }
  1172. b2ContactID.prototype.Copy = function () {
  1173. var id = new b2ContactID();
  1174. id.key = this.key;
  1175. return id;
  1176. }
  1177. Object.defineProperty(b2ContactID.prototype, 'key', {
  1178. enumerable: false,
  1179. configurable: true,
  1180. get: function () {
  1181. return this._key;
  1182. }
  1183. });
  1184. Object.defineProperty(b2ContactID.prototype, 'key', {
  1185. enumerable: false,
  1186. configurable: true,
  1187. set: function (value) {
  1188. if (value === undefined) value = 0;
  1189. this._key = value;
  1190. this.features._referenceEdge = this._key & 0x000000ff;
  1191. this.features._incidentEdge = ((this._key & 0x0000ff00) >> 8) & 0x000000ff;
  1192. this.features._incidentVertex = ((this._key & 0x00ff0000) >> 16) & 0x000000ff;
  1193. this.features._flip = ((this._key & 0xff000000) >> 24) & 0x000000ff;
  1194. }
  1195. });
  1196. b2ContactPoint.b2ContactPoint = function () {
  1197. this.position = new b2Vec2();
  1198. this.velocity = new b2Vec2();
  1199. this.normal = new b2Vec2();
  1200. this.id = new b2ContactID();
  1201. };
  1202. b2Distance.b2Distance = function () {};
  1203. b2Distance.Distance = function (output, cache, input) {
  1204. ++b2Distance.b2_gjkCalls;
  1205. var proxyA = input.proxyA;
  1206. var proxyB = input.proxyB;
  1207. var transformA = input.transformA;
  1208. var transformB = input.transformB;
  1209. var simplex = b2Distance.s_simplex;
  1210. simplex.ReadCache(cache, proxyA, transformA, proxyB, transformB);
  1211. var vertices = simplex.m_vertices;
  1212. var k_maxIters = 20;
  1213. var saveA = b2Distance.s_saveA;
  1214. var saveB = b2Distance.s_saveB;
  1215. var saveCount = 0;
  1216. var closestPoint = simplex.GetClosestPoint();
  1217. var distanceSqr1 = closestPoint.LengthSquared();
  1218. var distanceSqr2 = distanceSqr1;
  1219. var i = 0;
  1220. var p;
  1221. var iter = 0;
  1222. while (iter < k_maxIters) {
  1223. saveCount = simplex.m_count;
  1224. for (i = 0;
  1225. i < saveCount; i++) {
  1226. saveA[i] = vertices[i].indexA;
  1227. saveB[i] = vertices[i].indexB;
  1228. }
  1229. switch (simplex.m_count) {
  1230. case 1:
  1231. break;
  1232. case 2:
  1233. simplex.Solve2();
  1234. break;
  1235. case 3:
  1236. simplex.Solve3();
  1237. break;
  1238. default:
  1239. b2Settings.b2Assert(false);
  1240. }
  1241. if (simplex.m_count == 3) {
  1242. break;
  1243. }
  1244. p = simplex.GetClosestPoint();
  1245. distanceSqr2 = p.LengthSquared();
  1246. if (distanceSqr2 > distanceSqr1) {}
  1247. distanceSqr1 = distanceSqr2;
  1248. var d = simplex.GetSearchDirection();
  1249. if (d.LengthSquared() < Number.MIN_VALUE * Number.MIN_VALUE) {
  1250. break;
  1251. }
  1252. var vertex = vertices[simplex.m_count];
  1253. vertex.indexA = proxyA.GetSupport(b2Math.MulTMV(transformA.R, d.GetNegative()));
  1254. vertex.wA = b2Math.MulX(transformA, proxyA.GetVertex(vertex.indexA));
  1255. vertex.indexB = proxyB.GetSupport(b2Math.MulTMV(transformB.R, d));
  1256. vertex.wB = b2Math.MulX(transformB, proxyB.GetVertex(vertex.indexB));
  1257. vertex.w = b2Math.SubtractVV(vertex.wB, vertex.wA);
  1258. ++iter;
  1259. ++b2Distance.b2_gjkIters;
  1260. var duplicate = false;
  1261. for (i = 0;
  1262. i < saveCount; i++) {
  1263. if (vertex.indexA == saveA[i] && vertex.indexB == saveB[i]) {
  1264. duplicate = true;
  1265. break;
  1266. }
  1267. }
  1268. if (duplicate) {
  1269. break;
  1270. }++simplex.m_count;
  1271. }
  1272. b2Distance.b2_gjkMaxIters = b2Math.Max(b2Distance.b2_gjkMaxIters, iter);
  1273. simplex.GetWitnessPoints(output.pointA, output.pointB);
  1274. output.distance = b2Math.SubtractVV(output.pointA, output.pointB).Length();
  1275. output.iterations = iter;
  1276. simplex.WriteCache(cache);
  1277. if (input.useRadii) {
  1278. var rA = proxyA.m_radius;
  1279. var rB = proxyB.m_radius;
  1280. if (output.distance > rA + rB && output.distance > Number.MIN_VALUE) {
  1281. output.distance -= rA + rB;
  1282. var normal = b2Math.SubtractVV(output.pointB, output.pointA);
  1283. normal.Normalize();
  1284. output.pointA.x += rA * normal.x;
  1285. output.pointA.y += rA * normal.y;
  1286. output.pointB.x -= rB * normal.x;
  1287. output.pointB.y -= rB * normal.y;
  1288. }
  1289. else {
  1290. p = new b2Vec2();
  1291. p.x = .5 * (output.pointA.x + output.pointB.x);
  1292. p.y = .5 * (output.pointA.y + output.pointB.y);
  1293. output.pointA.x = output.pointB.x = p.x;
  1294. output.pointA.y = output.pointB.y = p.y;
  1295. output.distance = 0.0;
  1296. }
  1297. }
  1298. }
  1299. Box2D.postDefs.push(function () {
  1300. Box2D.Collision.b2Distance.s_simplex = new b2Simplex();
  1301. Box2D.Collision.b2Distance.s_saveA = new Vector_a2j_Number(3);
  1302. Box2D.Collision.b2Distance.s_saveB = new Vector_a2j_Number(3);
  1303. });
  1304. b2DistanceInput.b2DistanceInput = function () {};
  1305. b2DistanceOutput.b2DistanceOutput = function () {
  1306. this.pointA = new b2Vec2();
  1307. this.pointB = new b2Vec2();
  1308. };
  1309. b2DistanceProxy.b2DistanceProxy = function () {};
  1310. b2DistanceProxy.prototype.Set = function (shape) {
  1311. switch (shape.GetType()) {
  1312. case b2Shape.e_circleShape:
  1313. {
  1314. var circle = (shape instanceof b2CircleShape ? shape : null);
  1315. this.m_vertices = new Vector(1, true);
  1316. this.m_vertices[0] = circle.m_p;
  1317. this.m_count = 1;
  1318. this.m_radius = circle.m_radius;
  1319. }
  1320. break;
  1321. case b2Shape.e_polygonShape:
  1322. {
  1323. var polygon = (shape instanceof b2PolygonShape ? shape : null);
  1324. this.m_vertices = polygon.m_vertices;
  1325. this.m_count = polygon.m_vertexCount;
  1326. this.m_radius = polygon.m_radius;
  1327. }
  1328. break;
  1329. default:
  1330. b2Settings.b2Assert(false);
  1331. }
  1332. }
  1333. b2DistanceProxy.prototype.GetSupport = function (d) {
  1334. var bestIndex = 0;
  1335. var bestValue = this.m_vertices[0].x * d.x + this.m_vertices[0].y * d.y;
  1336. for (var i = 1; i < this.m_count; ++i) {
  1337. var value = this.m_vertices[i].x * d.x + this.m_vertices[i].y * d.y;
  1338. if (value > bestValue) {
  1339. bestIndex = i;
  1340. bestValue = value;
  1341. }
  1342. }
  1343. return bestIndex;
  1344. }
  1345. b2DistanceProxy.prototype.GetSupportVertex = function (d) {
  1346. var bestIndex = 0;
  1347. var bestValue = this.m_vertices[0].x * d.x + this.m_vertices[0].y * d.y;
  1348. for (var i = 1; i < this.m_count; ++i) {
  1349. var value = this.m_vertices[i].x * d.x + this.m_vertices[i].y * d.y;
  1350. if (value > bestValue) {
  1351. bestIndex = i;
  1352. bestValue = value;
  1353. }
  1354. }
  1355. return this.m_vertices[bestIndex];
  1356. }
  1357. b2DistanceProxy.prototype.GetVertexCount = function () {
  1358. return this.m_count;
  1359. }
  1360. b2DistanceProxy.prototype.GetVertex = function (index) {
  1361. if (index === undefined) index = 0;
  1362. b2Settings.b2Assert(0 <= index && index < this.m_count);
  1363. return this.m_vertices[index];
  1364. }
  1365. b2DynamicTree.b2DynamicTree = function () {};
  1366. b2DynamicTree.prototype.b2DynamicTree = function () {
  1367. this.m_root = null;
  1368. this.m_freeList = null;
  1369. this.m_path = 0;
  1370. this.m_insertionCount = 0;
  1371. }
  1372. b2DynamicTree.prototype.CreateProxy = function (aabb, userData) {
  1373. var node = this.AllocateNode();
  1374. var extendX = b2Settings.b2_aabbExtension;
  1375. var extendY = b2Settings.b2_aabbExtension;
  1376. node.aabb.lowerBound.x = aabb.lowerBound.x - extendX;
  1377. node.aabb.lowerBound.y = aabb.lowerBound.y - extendY;
  1378. node.aabb.upperBound.x = aabb.upperBound.x + extendX;
  1379. node.aabb.upperBound.y = aabb.upperBound.y + extendY;
  1380. node.userData = userData;
  1381. this.InsertLeaf(node);
  1382. return node;
  1383. }
  1384. b2DynamicTree.prototype.DestroyProxy = function (proxy) {
  1385. this.RemoveLeaf(proxy);
  1386. this.FreeNode(proxy);
  1387. }
  1388. b2DynamicTree.prototype.MoveProxy = function (proxy, aabb, displacement) {
  1389. b2Settings.b2Assert(proxy.IsLeaf());
  1390. if (proxy.aabb.Contains(aabb)) {
  1391. return false;
  1392. }
  1393. this.RemoveLeaf(proxy);
  1394. var extendX = b2Settings.b2_aabbExtension + b2Settings.b2_aabbMultiplier * (displacement.x > 0 ? displacement.x : (-displacement.x));
  1395. var extendY = b2Settings.b2_aabbExtension + b2Settings.b2_aabbMultiplier * (displacement.y > 0 ? displacement.y : (-displacement.y));
  1396. proxy.aabb.lowerBound.x = aabb.lowerBound.x - extendX;
  1397. proxy.aabb.lowerBound.y = aabb.lowerBound.y - extendY;
  1398. proxy.aabb.upperBound.x = aabb.upperBound.x + extendX;
  1399. proxy.aabb.upperBound.y = aabb.upperBound.y + extendY;
  1400. this.InsertLeaf(proxy);
  1401. return true;
  1402. }
  1403. b2DynamicTree.prototype.Rebalance = function (iterations) {
  1404. if (iterations === undefined) iterations = 0;
  1405. if (this.m_root == null) return;
  1406. for (var i = 0; i < iterations; i++) {
  1407. var node = this.m_root;
  1408. var bit = 0;
  1409. while (node.IsLeaf() == false) {
  1410. node = (this.m_path >> bit) & 1 ? node.child2 : node.child1;
  1411. bit = (bit + 1) & 31;
  1412. }++this.m_path;
  1413. this.RemoveLeaf(node);
  1414. this.InsertLeaf(node);
  1415. }
  1416. }
  1417. b2DynamicTree.prototype.GetFatAABB = function (proxy) {
  1418. return proxy.aabb;
  1419. }
  1420. b2DynamicTree.prototype.GetUserData = function (proxy) {
  1421. return proxy.userData;
  1422. }
  1423. b2DynamicTree.prototype.Query = function (callback, aabb) {
  1424. if (this.m_root == null) return;
  1425. var stack = new Vector();
  1426. var count = 0;
  1427. stack[count++] = this.m_root;
  1428. while (count > 0) {
  1429. var node = stack[--count];
  1430. if (node.aabb.TestOverlap(aabb)) {
  1431. if (node.IsLeaf()) {
  1432. var proceed = callback(node);
  1433. if (!proceed) return;
  1434. }
  1435. else {
  1436. stack[count++] = node.child1;
  1437. stack[count++] = node.child2;
  1438. }
  1439. }
  1440. }
  1441. }
  1442. b2DynamicTree.prototype.RayCast = function (callback, input) {
  1443. if (this.m_root == null) return;
  1444. var p1 = input.p1;
  1445. var p2 = input.p2;
  1446. var r = b2Math.SubtractVV(p1, p2);
  1447. r.Normalize();
  1448. var v = b2Math.CrossFV(1.0, r);
  1449. var abs_v = b2Math.AbsV(v);
  1450. var maxFraction = input.maxFraction;
  1451. var segmentAABB = new b2AABB();
  1452. var tX = 0;
  1453. var tY = 0; {
  1454. tX = p1.x + maxFraction * (p2.x - p1.x);
  1455. tY = p1.y + maxFraction * (p2.y - p1.y);
  1456. segmentAABB.lowerBound.x = Math.min(p1.x, tX);
  1457. segmentAABB.lowerBound.y = Math.min(p1.y, tY);
  1458. segmentAABB.upperBound.x = Math.max(p1.x, tX);
  1459. segmentAABB.upperBound.y = Math.max(p1.y, tY);
  1460. }
  1461. var stack = new Vector();
  1462. var count = 0;
  1463. stack[count++] = this.m_root;
  1464. while (count > 0) {
  1465. var node = stack[--count];
  1466. if (node.aabb.TestOverlap(segmentAABB) == false) {
  1467. continue;
  1468. }
  1469. var c = node.aabb.GetCenter();
  1470. var h = node.aabb.GetExtents();
  1471. var separation = Math.abs(v.x * (p1.x - c.x) + v.y * (p1.y - c.y)) - abs_v.x * h.x - abs_v.y * h.y;
  1472. if (separation > 0.0) continue;
  1473. if (node.IsLeaf()) {
  1474. var subInput = new b2RayCastInput();
  1475. subInput.p1 = input.p1;
  1476. subInput.p2 = input.p2;
  1477. subInput.maxFraction = input.maxFraction;
  1478. maxFraction = callback(subInput, node);
  1479. if (maxFraction == 0.0) return;
  1480. if (maxFraction > 0.0) {
  1481. tX = p1.x + maxFraction * (p2.x - p1.x);
  1482. tY = p1.y + maxFraction * (p2.y - p1.y);
  1483. segmentAABB.lowerBound.x = Math.min(p1.x, tX);
  1484. segmentAABB.lowerBound.y = Math.min(p1.y, tY);
  1485. segmentAABB.upperBound.x = Math.max(p1.x, tX);
  1486. segmentAABB.upperBound.y = Math.max(p1.y, tY);
  1487. }
  1488. }
  1489. else {
  1490. stack[count++] = node.child1;
  1491. stack[count++] = node.child2;
  1492. }
  1493. }
  1494. }
  1495. b2DynamicTree.prototype.AllocateNode = function () {
  1496. if (this.m_freeList) {
  1497. var node = this.m_freeList;
  1498. this.m_freeList = node.parent;
  1499. node.parent = null;
  1500. node.child1 = null;
  1501. node.child2 = null;
  1502. return node;
  1503. }
  1504. return new b2DynamicTreeNode();
  1505. }
  1506. b2DynamicTree.prototype.FreeNode = function (node) {
  1507. node.parent = this.m_freeList;
  1508. this.m_freeList = node;
  1509. }
  1510. b2DynamicTree.prototype.InsertLeaf = function (leaf) {
  1511. ++this.m_insertionCount;
  1512. if (this.m_root == null) {
  1513. this.m_root = leaf;
  1514. this.m_root.parent = null;
  1515. return;
  1516. }
  1517. var center = leaf.aabb.GetCenter();
  1518. var sibling = this.m_root;
  1519. if (sibling.IsLeaf() == false) {
  1520. do {
  1521. var child1 = sibling.child1;
  1522. var child2 = sibling.child2;
  1523. var norm1 = Math.abs((child1.aabb.lowerBound.x + child1.aabb.upperBound.x) / 2 - center.x) + Math.abs((child1.aabb.lowerBound.y + child1.aabb.upperBound.y) / 2 - center.y);
  1524. var norm2 = Math.abs((child2.aabb.lowerBound.x + child2.aabb.upperBound.x) / 2 - center.x) + Math.abs((child2.aabb.lowerBound.y + child2.aabb.upperBound.y) / 2 - center.y);
  1525. if (norm1 < norm2) {
  1526. sibling = child1;
  1527. }
  1528. else {
  1529. sibling = child2;
  1530. }
  1531. }
  1532. while (sibling.IsLeaf() == false)
  1533. }
  1534. var node1 = sibling.parent;
  1535. var node2 = this.AllocateNode();
  1536. node2.parent = node1;
  1537. node2.userData = null;
  1538. node2.aabb.Combine(leaf.aabb, sibling.aabb);
  1539. if (node1) {
  1540. if (sibling.parent.child1 == sibling) {
  1541. node1.child1 = node2;
  1542. }
  1543. else {
  1544. node1.child2 = node2;
  1545. }
  1546. node2.child1 = sibling;
  1547. node2.child2 = leaf;
  1548. sibling.parent = node2;
  1549. leaf.parent = node2;
  1550. do {
  1551. if (node1.aabb.Contains(node2.aabb)) break;
  1552. node1.aabb.Combine(node1.child1.aabb, node1.child2.aabb);
  1553. node2 = node1;
  1554. node1 = node1.parent;
  1555. }
  1556. while (node1)
  1557. }
  1558. else {
  1559. node2.child1 = sibling;
  1560. node2.child2 = leaf;
  1561. sibling.parent = node2;
  1562. leaf.parent = node2;
  1563. this.m_root = node2;
  1564. }
  1565. }
  1566. b2DynamicTree.prototype.RemoveLeaf = function (leaf) {
  1567. if (leaf == this.m_root) {
  1568. this.m_root = null;
  1569. return;
  1570. }
  1571. var node2 = leaf.parent;
  1572. var node1 = node2.parent;
  1573. var sibling;
  1574. if (node2.child1 == leaf) {
  1575. sibling = node2.child2;
  1576. }
  1577. else {
  1578. sibling = node2.child1;
  1579. }
  1580. if (node1) {
  1581. if (node1.child1 == node2) {
  1582. node1.child1 = sibling;
  1583. }
  1584. else {
  1585. node1.child2 = sibling;
  1586. }
  1587. sibling.parent = node1;
  1588. this.FreeNode(node2);
  1589. while (node1) {
  1590. var oldAABB = node1.aabb;
  1591. node1.aabb = b2AABB.Combine(node1.child1.aabb, node1.child2.aabb);
  1592. if (oldAABB.Contains(node1.aabb)) break;
  1593. node1 = node1.parent;
  1594. }
  1595. }
  1596. else {
  1597. this.m_root = sibling;
  1598. sibling.parent = null;
  1599. this.FreeNode(node2);
  1600. }
  1601. }
  1602. b2DynamicTreeBroadPhase.b2DynamicTreeBroadPhase = function () {
  1603. this.m_tree = new b2DynamicTree();
  1604. this.m_moveBuffer = new Vector();
  1605. this.m_pairBuffer = new Vector();
  1606. this.m_pairCount = 0;
  1607. };
  1608. b2DynamicTreeBroadPhase.prototype.CreateProxy = function (aabb, userData) {
  1609. var proxy = this.m_tree.CreateProxy(aabb, userData);
  1610. ++this.m_proxyCount;
  1611. this.BufferMove(proxy);
  1612. return proxy;
  1613. }
  1614. b2DynamicTreeBroadPhase.prototype.DestroyProxy = function (proxy) {
  1615. this.UnBufferMove(proxy);
  1616. --this.m_proxyCount;
  1617. this.m_tree.DestroyProxy(proxy);
  1618. }
  1619. b2DynamicTreeBroadPhase.prototype.MoveProxy = function (proxy, aabb, displacement) {
  1620. var buffer = this.m_tree.MoveProxy(proxy, aabb, displacement);
  1621. if (buffer) {
  1622. this.BufferMove(proxy);
  1623. }
  1624. }
  1625. b2DynamicTreeBroadPhase.prototype.TestOverlap = function (proxyA, proxyB) {
  1626. var aabbA = this.m_tree.GetFatAABB(proxyA);
  1627. var aabbB = this.m_tree.GetFatAABB(proxyB);
  1628. return aabbA.TestOverlap(aabbB);
  1629. }
  1630. b2DynamicTreeBroadPhase.prototype.GetUserData = function (proxy) {
  1631. return this.m_tree.GetUserData(proxy);
  1632. }
  1633. b2DynamicTreeBroadPhase.prototype.GetFatAABB = function (proxy) {
  1634. return this.m_tree.GetFatAABB(proxy);
  1635. }
  1636. b2DynamicTreeBroadPhase.prototype.GetProxyCount = function () {
  1637. return this.m_proxyCount;
  1638. }
  1639. b2DynamicTreeBroadPhase.prototype.UpdatePairs = function (callback) {
  1640. var __this = this;
  1641. __this.m_pairCount = 0;
  1642. var i = 0,
  1643. queryProxy;
  1644. for (i = 0;
  1645. i < __this.m_moveBuffer.length; ++i) {
  1646. queryProxy = __this.m_moveBuffer[i];
  1647. function QueryCallback(proxy) {
  1648. if (proxy == queryProxy) return true;
  1649. if (__this.m_pairCount == __this.m_pairBuffer.length) {
  1650. __this.m_pairBuffer[__this.m_pairCount] = new b2DynamicTreePair();
  1651. }
  1652. var pair = __this.m_pairBuffer[__this.m_pairCount];
  1653. pair.proxyA = proxy < queryProxy ? proxy : queryProxy;
  1654. pair.proxyB = proxy >= queryProxy ? proxy : queryProxy;++__this.m_pairCount;
  1655. return true;
  1656. };
  1657. var fatAABB = __this.m_tree.GetFatAABB(queryProxy);
  1658. __this.m_tree.Query(QueryCallback, fatAABB);
  1659. }
  1660. __this.m_moveBuffer.length = 0;
  1661. for (var i = 0; i < __this.m_pairCount;) {
  1662. var primaryPair = __this.m_pairBuffer[i];
  1663. var userDataA = __this.m_tree.GetUserData(primaryPair.proxyA);
  1664. var userDataB = __this.m_tree.GetUserData(primaryPair.proxyB);
  1665. callback(userDataA, userDataB);
  1666. ++i;
  1667. while (i < __this.m_pairCount) {
  1668. var pair = __this.m_pairBuffer[i];
  1669. if (pair.proxyA != primaryPair.proxyA || pair.proxyB != primaryPair.proxyB) {
  1670. break;
  1671. }++i;
  1672. }
  1673. }
  1674. }
  1675. b2DynamicTreeBroadPhase.prototype.Query = function (callback, aabb) {
  1676. this.m_tree.Query(callback, aabb);
  1677. }
  1678. b2DynamicTreeBroadPhase.prototype.RayCast = function (callback, input) {
  1679. this.m_tree.RayCast(callback, input);
  1680. }
  1681. b2DynamicTreeBroadPhase.prototype.Validate = function () {}
  1682. b2DynamicTreeBroadPhase.prototype.Rebalance = function (iterations) {
  1683. if (iterations === undefined) iterations = 0;
  1684. this.m_tree.Rebalance(iterations);
  1685. }
  1686. b2DynamicTreeBroadPhase.prototype.BufferMove = function (proxy) {
  1687. this.m_moveBuffer[this.m_moveBuffer.length] = proxy;
  1688. }
  1689. b2DynamicTreeBroadPhase.prototype.UnBufferMove = function (proxy) {
  1690. var i = parseInt(this.m_moveBuffer.indexOf(proxy));
  1691. this.m_moveBuffer.splice(i, 1);
  1692. }
  1693. b2DynamicTreeBroadPhase.prototype.ComparePairs = function (pair1, pair2) {
  1694. return 0;
  1695. }
  1696. b2DynamicTreeBroadPhase.__implements = {};
  1697. b2DynamicTreeBroadPhase.__implements[IBroadPhase] = true;
  1698. b2DynamicTreeNode.b2DynamicTreeNode = function () {
  1699. this.aabb = new b2AABB();
  1700. };
  1701. b2DynamicTreeNode.prototype.IsLeaf = function () {
  1702. return this.child1 == null;
  1703. }
  1704. b2DynamicTreePair.b2DynamicTreePair = function () {};
  1705. b2Manifold.b2Manifold = function () {
  1706. this.m_pointCount = 0;
  1707. };
  1708. b2Manifold.prototype.b2Manifold = function () {
  1709. this.m_points = new Vector(b2Settings.b2_maxManifoldPoints);
  1710. for (var i = 0; i < b2Settings.b2_maxManifoldPoints; i++) {
  1711. this.m_points[i] = new b2ManifoldPoint();
  1712. }
  1713. this.m_localPlaneNormal = new b2Vec2();
  1714. this.m_localPoint = new b2Vec2();
  1715. }
  1716. b2Manifold.prototype.Reset = function () {
  1717. for (var i = 0; i < b2Settings.b2_maxManifoldPoints; i++) {
  1718. ((this.m_points[i] instanceof b2ManifoldPoint ? this.m_points[i] : null)).Reset();
  1719. }
  1720. this.m_localPlaneNormal.SetZero();
  1721. this.m_localPoint.SetZero();
  1722. this.m_type = 0;
  1723. this.m_pointCount = 0;
  1724. }
  1725. b2Manifold.prototype.Set = function (m) {
  1726. this.m_pointCount = m.m_pointCount;
  1727. for (var i = 0; i < b2Settings.b2_maxManifoldPoints; i++) {
  1728. ((this.m_points[i] instanceof b2ManifoldPoint ? this.m_points[i] : null)).Set(m.m_points[i]);
  1729. }
  1730. this.m_localPlaneNormal.SetV(m.m_localPlaneNormal);
  1731. this.m_localPoint.SetV(m.m_localPoint);
  1732. this.m_type = m.m_type;
  1733. }
  1734. b2Manifold.prototype.Copy = function () {
  1735. var copy = new b2Manifold();
  1736. copy.Set(this);
  1737. return copy;
  1738. }
  1739. Box2D.postDefs.push(function () {
  1740. Box2D.Collision.b2Manifold.e_circles = 0x0001;
  1741. Box2D.Collision.b2Manifold.e_faceA = 0x0002;
  1742. Box2D.Collision.b2Manifold.e_faceB = 0x0004;
  1743. });
  1744. b2ManifoldPoint.b2ManifoldPoint = function () {
  1745. this.m_localPoint = new b2Vec2();
  1746. this.m_id = new b2ContactID();
  1747. };
  1748. b2ManifoldPoint.prototype.b2ManifoldPoint = function () {
  1749. this.Reset();
  1750. }
  1751. b2ManifoldPoint.prototype.Reset = function () {
  1752. this.m_localPoint.SetZero();
  1753. this.m_normalImpulse = 0.0;
  1754. this.m_tangentImpulse = 0.0;
  1755. this.m_id.key = 0;
  1756. }
  1757. b2ManifoldPoint.prototype.Set = function (m) {
  1758. this.m_localPoint.SetV(m.m_localPoint);
  1759. this.m_normalImpulse = m.m_normalImpulse;
  1760. this.m_tangentImpulse = m.m_tangentImpulse;
  1761. this.m_id.Set(m.m_id);
  1762. }
  1763. b2Point.b2Point = function () {
  1764. this.p = new b2Vec2();
  1765. };
  1766. b2Point.prototype.Support = function (xf, vX, vY) {
  1767. if (vX === undefined) vX = 0;
  1768. if (vY === undefined) vY = 0;
  1769. return this.p;
  1770. }
  1771. b2Point.prototype.GetFirstVertex = function (xf) {
  1772. return this.p;
  1773. }
  1774. b2RayCastInput.b2RayCastInput = function () {
  1775. this.p1 = new b2Vec2();
  1776. this.p2 = new b2Vec2();
  1777. };
  1778. b2RayCastInput.prototype.b2RayCastInput = function (p1, p2, maxFraction) {
  1779. if (p1 === undefined) p1 = null;
  1780. if (p2 === undefined) p2 = null;
  1781. if (maxFraction === undefined) maxFraction = 1;
  1782. if (p1) this.p1.SetV(p1);
  1783. if (p2) this.p2.SetV(p2);
  1784. this.maxFraction = maxFraction;
  1785. }
  1786. b2RayCastOutput.b2RayCastOutput = function () {
  1787. this.normal = new b2Vec2();
  1788. };
  1789. b2Segment.b2Segment = function () {
  1790. this.p1 = new b2Vec2();
  1791. this.p2 = new b2Vec2();
  1792. };
  1793. b2Segment.prototype.TestSegment = function (lambda, normal, segment, maxLambda) {
  1794. if (maxLambda === undefined) maxLambda = 0;
  1795. var s = segment.p1;
  1796. var rX = segment.p2.x - s.x;
  1797. var rY = segment.p2.y - s.y;
  1798. var dX = this.p2.x - this.p1.x;
  1799. var dY = this.p2.y - this.p1.y;
  1800. var nX = dY;
  1801. var nY = (-dX);
  1802. var k_slop = 100.0 * Number.MIN_VALUE;
  1803. var denom = (-(rX * nX + rY * nY));
  1804. if (denom > k_slop) {
  1805. var bX = s.x - this.p1.x;
  1806. var bY = s.y - this.p1.y;
  1807. var a = (bX * nX + bY * nY);
  1808. if (0.0 <= a && a <= maxLambda * denom) {
  1809. var mu2 = (-rX * bY) + rY * bX;
  1810. if ((-k_slop * denom) <= mu2 && mu2 <= denom * (1.0 + k_slop)) {
  1811. a /= denom;
  1812. var nLen = Math.sqrt(nX * nX + nY * nY);
  1813. nX /= nLen;
  1814. nY /= nLen;
  1815. lambda[0] = a;
  1816. normal.Set(nX, nY);
  1817. return true;
  1818. }
  1819. }
  1820. }
  1821. return false;
  1822. }
  1823. b2Segment.prototype.Extend = function (aabb) {
  1824. this.ExtendForward(aabb);
  1825. this.ExtendBackward(aabb);
  1826. }
  1827. b2Segment.prototype.ExtendForward = function (aabb) {
  1828. var dX = this.p2.x - this.p1.x;
  1829. var dY = this.p2.y - this.p1.y;
  1830. var lambda = Math.min(dX > 0 ? (aabb.upperBound.x - this.p1.x) / dX : dX < 0 ? (aabb.lowerBound.x - this.p1.x) / dX : Number.POSITIVE_INFINITY,
  1831. dY > 0 ? (aabb.upperBound.y - this.p1.y) / dY : dY < 0 ? (aabb.lowerBound.y - this.p1.y) / dY : Number.POSITIVE_INFINITY);
  1832. this.p2.x = this.p1.x + dX * lambda;
  1833. this.p2.y = this.p1.y + dY * lambda;
  1834. }
  1835. b2Segment.prototype.ExtendBackward = function (aabb) {
  1836. var dX = (-this.p2.x) + this.p1.x;
  1837. var dY = (-this.p2.y) + this.p1.y;
  1838. var lambda = Math.min(dX > 0 ? (aabb.upperBound.x - this.p2.x) / dX : dX < 0 ? (aabb.lowerBound.x - this.p2.x) / dX : Number.POSITIVE_INFINITY,
  1839. dY > 0 ? (aabb.upperBound.y - this.p2.y) / dY : dY < 0 ? (aabb.lowerBound.y - this.p2.y) / dY : Number.POSITIVE_INFINITY);
  1840. this.p1.x = this.p2.x + dX * lambda;
  1841. this.p1.y = this.p2.y + dY * lambda;
  1842. }
  1843. b2SeparationFunction.b2SeparationFunction = function () {
  1844. this.m_localPoint = new b2Vec2();
  1845. this.m_axis = new b2Vec2();
  1846. };
  1847. b2SeparationFunction.prototype.Initialize = function (cache, proxyA, transformA, proxyB, transformB) {
  1848. this.m_proxyA = proxyA;
  1849. this.m_proxyB = proxyB;
  1850. var count = parseInt(cache.count);
  1851. b2Settings.b2Assert(0 < count && count < 3);
  1852. var localPointA;
  1853. var localPointA1;
  1854. var localPointA2;
  1855. var localPointB;
  1856. var localPointB1;
  1857. var localPointB2;
  1858. var pointAX = 0;
  1859. var pointAY = 0;
  1860. var pointBX = 0;
  1861. var pointBY = 0;
  1862. var normalX = 0;
  1863. var normalY = 0;
  1864. var tMat;
  1865. var tVec;
  1866. var s = 0;
  1867. var sgn = 0;
  1868. if (count == 1) {
  1869. this.m_type = b2SeparationFunction.e_points;
  1870. localPointA = this.m_proxyA.GetVertex(cache.indexA[0]);
  1871. localPointB = this.m_proxyB.GetVertex(cache.indexB[0]);
  1872. tVec = localPointA;
  1873. tMat = transformA.R;
  1874. pointAX = transformA.position.x + (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
  1875. pointAY = transformA.position.y + (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
  1876. tVec = localPointB;
  1877. tMat = transformB.R;
  1878. pointBX = transformB.position.x + (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
  1879. pointBY = transformB.position.y + (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
  1880. this.m_axis.x = pointBX - pointAX;
  1881. this.m_axis.y = pointBY - pointAY;
  1882. this.m_axis.Normalize();
  1883. }
  1884. else if (cache.indexB[0] == cache.indexB[1]) {
  1885. this.m_type = b2SeparationFunction.e_faceA;
  1886. localPointA1 = this.m_proxyA.GetVertex(cache.indexA[0]);
  1887. localPointA2 = this.m_proxyA.GetVertex(cache.indexA[1]);
  1888. localPointB = this.m_proxyB.GetVertex(cache.indexB[0]);
  1889. this.m_localPoint.x = 0.5 * (localPointA1.x + localPointA2.x);
  1890. this.m_localPoint.y = 0.5 * (localPointA1.y + localPointA2.y);
  1891. this.m_axis = b2Math.CrossVF(b2Math.SubtractVV(localPointA2, localPointA1), 1.0);
  1892. this.m_axis.Normalize();
  1893. tVec = this.m_axis;
  1894. tMat = transformA.R;
  1895. normalX = tMat.col1.x * tVec.x + tMat.col2.x * tVec.y;
  1896. normalY = tMat.col1.y * tVec.x + tMat.col2.y * tVec.y;
  1897. tVec = this.m_localPoint;
  1898. tMat = transformA.R;
  1899. pointAX = transformA.position.x + (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
  1900. pointAY = transformA.position.y + (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
  1901. tVec = localPointB;
  1902. tMat = transformB.R;
  1903. pointBX = transformB.position.x + (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
  1904. pointBY = transformB.position.y + (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
  1905. s = (pointBX - pointAX) * normalX + (pointBY - pointAY) * normalY;
  1906. if (s < 0.0) {
  1907. this.m_axis.NegativeSelf();
  1908. }
  1909. }
  1910. else if (cache.indexA[0] == cache.indexA[0]) {
  1911. this.m_type = b2SeparationFunction.e_faceB;
  1912. localPointB1 = this.m_proxyB.GetVertex(cache.indexB[0]);
  1913. localPointB2 = this.m_proxyB.GetVertex(cache.indexB[1]);
  1914. localPointA = this.m_proxyA.GetVertex(cache.indexA[0]);
  1915. this.m_localPoint.x = 0.5 * (localPointB1.x + localPointB2.x);
  1916. this.m_localPoint.y = 0.5 * (localPointB1.y + localPointB2.y);
  1917. this.m_axis = b2Math.CrossVF(b2Math.SubtractVV(localPointB2, localPointB1), 1.0);
  1918. this.m_axis.Normalize();
  1919. tVec = this.m_axis;
  1920. tMat = transformB.R;
  1921. normalX = tMat.col1.x * tVec.x + tMat.col2.x * tVec.y;
  1922. normalY = tMat.col1.y * tVec.x + tMat.col2.y * tVec.y;
  1923. tVec = this.m_localPoint;
  1924. tMat = transformB.R;
  1925. pointBX = transformB.position.x + (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
  1926. pointBY = transformB.position.y + (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
  1927. tVec = localPointA;
  1928. tMat = transformA.R;
  1929. pointAX = transformA.position.x + (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
  1930. pointAY = transformA.position.y + (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
  1931. s = (pointAX - pointBX) * normalX + (pointAY - pointBY) * normalY;
  1932. if (s < 0.0) {
  1933. this.m_axis.NegativeSelf();
  1934. }
  1935. }
  1936. else {
  1937. localPointA1 = this.m_proxyA.GetVertex(cache.indexA[0]);
  1938. localPointA2 = this.m_proxyA.GetVertex(cache.indexA[1]);
  1939. localPointB1 = this.m_proxyB.GetVertex(cache.indexB[0]);
  1940. localPointB2 = this.m_proxyB.GetVertex(cache.indexB[1]);
  1941. var pA = b2Math.MulX(transformA, localPointA);
  1942. var dA = b2Math.MulMV(transformA.R, b2Math.SubtractVV(localPointA2, localPointA1));
  1943. var pB = b2Math.MulX(transformB, localPointB);
  1944. var dB = b2Math.MulMV(transformB.R, b2Math.SubtractVV(localPointB2, localPointB1));
  1945. var a = dA.x * dA.x + dA.y * dA.y;
  1946. var e = dB.x * dB.x + dB.y * dB.y;
  1947. var r = b2Math.SubtractVV(dB, dA);
  1948. var c = dA.x * r.x + dA.y * r.y;
  1949. var f = dB.x * r.x + dB.y * r.y;
  1950. var b = dA.x * dB.x + dA.y * dB.y;
  1951. var denom = a * e - b * b;
  1952. s = 0.0;
  1953. if (denom != 0.0) {
  1954. s = b2Math.Clamp((b * f - c * e) / denom, 0.0, 1.0);
  1955. }
  1956. var t = (b * s + f) / e;
  1957. if (t < 0.0) {
  1958. t = 0.0;
  1959. s = b2Math.Clamp((b - c) / a, 0.0, 1.0);
  1960. }
  1961. localPointA = new b2Vec2();
  1962. localPointA.x = localPointA1.x + s * (localPointA2.x - localPointA1.x);
  1963. localPointA.y = localPointA1.y + s * (localPointA2.y - localPointA1.y);
  1964. localPointB = new b2Vec2();
  1965. localPointB.x = localPointB1.x + s * (localPointB2.x - localPointB1.x);
  1966. localPointB.y = localPointB1.y + s * (localPointB2.y - localPointB1.y);
  1967. if (s == 0.0 || s == 1.0) {
  1968. this.m_type = b2SeparationFunction.e_faceB;
  1969. this.m_axis = b2Math.CrossVF(b2Math.SubtractVV(localPointB2, localPointB1), 1.0);
  1970. this.m_axis.Normalize();
  1971. this.m_localPoint = localPointB;
  1972. tVec = this.m_axis;
  1973. tMat = transformB.R;
  1974. normalX = tMat.col1.x * tVec.x + tMat.col2.x * tVec.y;
  1975. normalY = tMat.col1.y * tVec.x + tMat.col2.y * tVec.y;
  1976. tVec = this.m_localPoint;
  1977. tMat = transformB.R;
  1978. pointBX = transformB.position.x + (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
  1979. pointBY = transformB.position.y + (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
  1980. tVec = localPointA;
  1981. tMat = transformA.R;
  1982. pointAX = transformA.position.x + (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
  1983. pointAY = transformA.position.y + (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
  1984. sgn = (pointAX - pointBX) * normalX + (pointAY - pointBY) * normalY;
  1985. if (s < 0.0) {
  1986. this.m_axis.NegativeSelf();
  1987. }
  1988. }
  1989. else {
  1990. this.m_type = b2SeparationFunction.e_faceA;
  1991. this.m_axis = b2Math.CrossVF(b2Math.SubtractVV(localPointA2, localPointA1), 1.0);
  1992. this.m_localPoint = localPointA;
  1993. tVec = this.m_axis;
  1994. tMat = transformA.R;
  1995. normalX = tMat.col1.x * tVec.x + tMat.col2.x * tVec.y;
  1996. normalY = tMat.col1.y * tVec.x + tMat.col2.y * tVec.y;
  1997. tVec = this.m_localPoint;
  1998. tMat = transformA.R;
  1999. pointAX = transformA.position.x + (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
  2000. pointAY = transformA.position.y + (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
  2001. tVec = localPointB;
  2002. tMat = transformB.R;
  2003. pointBX = transformB.position.x + (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
  2004. pointBY = transformB.position.y + (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
  2005. sgn = (pointBX - pointAX) * normalX + (pointBY - pointAY) * normalY;
  2006. if (s < 0.0) {
  2007. this.m_axis.NegativeSelf();
  2008. }
  2009. }
  2010. }
  2011. }
  2012. b2SeparationFunction.prototype.Evaluate = function (transformA, transformB) {
  2013. var axisA;
  2014. var axisB;
  2015. var localPointA;
  2016. var localPointB;
  2017. var pointA;
  2018. var pointB;
  2019. var seperation = 0;
  2020. var normal;
  2021. switch (this.m_type) {
  2022. case b2SeparationFunction.e_points:
  2023. {
  2024. axisA = b2Math.MulTMV(transformA.R, this.m_axis);
  2025. axisB = b2Math.MulTMV(transformB.R, this.m_axis.GetNegative());
  2026. localPointA = this.m_proxyA.GetSupportVertex(axisA);
  2027. localPointB = this.m_proxyB.GetSupportVertex(axisB);
  2028. pointA = b2Math.MulX(transformA, localPointA);
  2029. pointB = b2Math.MulX(transformB, localPointB);
  2030. seperation = (pointB.x - pointA.x) * this.m_axis.x + (pointB.y - pointA.y) * this.m_axis.y;
  2031. return seperation;
  2032. }
  2033. case b2SeparationFunction.e_faceA:
  2034. {
  2035. normal = b2Math.MulMV(transformA.R, this.m_axis);
  2036. pointA = b2Math.MulX(transformA, this.m_localPoint);
  2037. axisB = b2Math.MulTMV(transformB.R, normal.GetNegative());
  2038. localPointB = this.m_proxyB.GetSupportVertex(axisB);
  2039. pointB = b2Math.MulX(transformB, localPointB);
  2040. seperation = (pointB.x - pointA.x) * normal.x + (pointB.y - pointA.y) * normal.y;
  2041. return seperation;
  2042. }
  2043. case b2SeparationFunction.e_faceB:
  2044. {
  2045. normal = b2Math.MulMV(transformB.R, this.m_axis);
  2046. pointB = b2Math.MulX(transformB, this.m_localPoint);
  2047. axisA = b2Math.MulTMV(transformA.R, normal.GetNegative());
  2048. localPointA = this.m_proxyA.GetSupportVertex(axisA);
  2049. pointA = b2Math.MulX(transformA, localPointA);
  2050. seperation = (pointA.x - pointB.x) * normal.x + (pointA.y - pointB.y) * normal.y;
  2051. return seperation;
  2052. }
  2053. default:
  2054. b2Settings.b2Assert(false);
  2055. return 0.0;
  2056. }
  2057. }
  2058. Box2D.postDefs.push(function () {
  2059. Box2D.Collision.b2SeparationFunction.e_points = 0x01;
  2060. Box2D.Collision.b2SeparationFunction.e_faceA = 0x02;
  2061. Box2D.Collision.b2SeparationFunction.e_faceB = 0x04;
  2062. });
  2063. b2Simplex.b2Simplex = function () {
  2064. this.m_v1 = new b2SimplexVertex();
  2065. this.m_v2 = new b2SimplexVertex();
  2066. this.m_v3 = new b2SimplexVertex();
  2067. this.m_vertices = new Vector(3);
  2068. };
  2069. b2Simplex.prototype.b2Simplex = function () {
  2070. this.m_vertices[0] = this.m_v1;
  2071. this.m_vertices[1] = this.m_v2;
  2072. this.m_vertices[2] = this.m_v3;
  2073. }
  2074. b2Simplex.prototype.ReadCache = function (cache, proxyA, transformA, proxyB, transformB) {
  2075. b2Settings.b2Assert(0 <= cache.count && cache.count <= 3);
  2076. var wALocal;
  2077. var wBLocal;
  2078. this.m_count = cache.count;
  2079. var vertices = this.m_vertices;
  2080. for (var i = 0; i < this.m_count; i++) {
  2081. var v = vertices[i];
  2082. v.indexA = cache.indexA[i];
  2083. v.indexB = cache.indexB[i];
  2084. wALocal = proxyA.GetVertex(v.indexA);
  2085. wBLocal = proxyB.GetVertex(v.indexB);
  2086. v.wA = b2Math.MulX(transformA, wALocal);
  2087. v.wB = b2Math.MulX(transformB, wBLocal);
  2088. v.w = b2Math.SubtractVV(v.wB, v.wA);
  2089. v.a = 0;
  2090. }
  2091. if (this.m_count > 1) {
  2092. var metric1 = cache.metric;
  2093. var metric2 = this.GetMetric();
  2094. if (metric2 < .5 * metric1 || 2.0 * metric1 < metric2 || metric2 < Number.MIN_VALUE) {
  2095. this.m_count = 0;
  2096. }
  2097. }
  2098. if (this.m_count == 0) {
  2099. v = vertices[0];
  2100. v.indexA = 0;
  2101. v.indexB = 0;
  2102. wALocal = proxyA.GetVertex(0);
  2103. wBLocal = proxyB.GetVertex(0);
  2104. v.wA = b2Math.MulX(transformA, wALocal);
  2105. v.wB = b2Math.MulX(transformB, wBLocal);
  2106. v.w = b2Math.SubtractVV(v.wB, v.wA);
  2107. this.m_count = 1;
  2108. }
  2109. }
  2110. b2Simplex.prototype.WriteCache = function (cache) {
  2111. cache.metric = this.GetMetric();
  2112. cache.count = Box2D.parseUInt(this.m_count);
  2113. var vertices = this.m_vertices;
  2114. for (var i = 0; i < this.m_count; i++) {
  2115. cache.indexA[i] = Box2D.parseUInt(vertices[i].indexA);
  2116. cache.indexB[i] = Box2D.parseUInt(vertices[i].indexB);
  2117. }
  2118. }
  2119. b2Simplex.prototype.GetSearchDirection = function () {
  2120. switch (this.m_count) {
  2121. case 1:
  2122. return this.m_v1.w.GetNegative();
  2123. case 2:
  2124. {
  2125. var e12 = b2Math.SubtractVV(this.m_v2.w, this.m_v1.w);
  2126. var sgn = b2Math.CrossVV(e12, this.m_v1.w.GetNegative());
  2127. if (sgn > 0.0) {
  2128. return b2Math.CrossFV(1.0, e12);
  2129. }
  2130. else {
  2131. return b2Math.CrossVF(e12, 1.0);
  2132. }
  2133. }
  2134. default:
  2135. b2Settings.b2Assert(false);
  2136. return new b2Vec2();
  2137. }
  2138. }
  2139. b2Simplex.prototype.GetClosestPoint = function () {
  2140. switch (this.m_count) {
  2141. case 0:
  2142. b2Settings.b2Assert(false);
  2143. return new b2Vec2();
  2144. case 1:
  2145. return this.m_v1.w;
  2146. case 2:
  2147. return new b2Vec2(this.m_v1.a * this.m_v1.w.x + this.m_v2.a * this.m_v2.w.x, this.m_v1.a * this.m_v1.w.y + this.m_v2.a * this.m_v2.w.y);
  2148. default:
  2149. b2Settings.b2Assert(false);
  2150. return new b2Vec2();
  2151. }
  2152. }
  2153. b2Simplex.prototype.GetWitnessPoints = function (pA, pB) {
  2154. switch (this.m_count) {
  2155. case 0:
  2156. b2Settings.b2Assert(false);
  2157. break;
  2158. case 1:
  2159. pA.SetV(this.m_v1.wA);
  2160. pB.SetV(this.m_v1.wB);
  2161. break;
  2162. case 2:
  2163. pA.x = this.m_v1.a * this.m_v1.wA.x + this.m_v2.a * this.m_v2.wA.x;
  2164. pA.y = this.m_v1.a * this.m_v1.wA.y + this.m_v2.a * this.m_v2.wA.y;
  2165. pB.x = this.m_v1.a * this.m_v1.wB.x + this.m_v2.a * this.m_v2.wB.x;
  2166. pB.y = this.m_v1.a * this.m_v1.wB.y + this.m_v2.a * this.m_v2.wB.y;
  2167. break;
  2168. case 3:
  2169. pB.x = pA.x = this.m_v1.a * this.m_v1.wA.x + this.m_v2.a * this.m_v2.wA.x + this.m_v3.a * this.m_v3.wA.x;
  2170. pB.y = pA.y = this.m_v1.a * this.m_v1.wA.y + this.m_v2.a * this.m_v2.wA.y + this.m_v3.a * this.m_v3.wA.y;
  2171. break;
  2172. default:
  2173. b2Settings.b2Assert(false);
  2174. break;
  2175. }
  2176. }
  2177. b2Simplex.prototype.GetMetric = function () {
  2178. switch (this.m_count) {
  2179. case 0:
  2180. b2Settings.b2Assert(false);
  2181. return 0.0;
  2182. case 1:
  2183. return 0.0;
  2184. case 2:
  2185. return b2Math.SubtractVV(this.m_v1.w, this.m_v2.w).Length();
  2186. case 3:
  2187. return b2Math.CrossVV(b2Math.SubtractVV(this.m_v2.w, this.m_v1.w), b2Math.SubtractVV(this.m_v3.w, this.m_v1.w));
  2188. default:
  2189. b2Settings.b2Assert(false);
  2190. return 0.0;
  2191. }
  2192. }
  2193. b2Simplex.prototype.Solve2 = function () {
  2194. var w1 = this.m_v1.w;
  2195. var w2 = this.m_v2.w;
  2196. var e12 = b2Math.SubtractVV(w2, w1);
  2197. var d12_2 = (-(w1.x * e12.x + w1.y * e12.y));
  2198. if (d12_2 <= 0.0) {
  2199. this.m_v1.a = 1.0;
  2200. this.m_count = 1;
  2201. return;
  2202. }
  2203. var d12_1 = (w2.x * e12.x + w2.y * e12.y);
  2204. if (d12_1 <= 0.0) {
  2205. this.m_v2.a = 1.0;
  2206. this.m_count = 1;
  2207. this.m_v1.Set(this.m_v2);
  2208. return;
  2209. }
  2210. var inv_d12 = 1.0 / (d12_1 + d12_2);
  2211. this.m_v1.a = d12_1 * inv_d12;
  2212. this.m_v2.a = d12_2 * inv_d12;
  2213. this.m_count = 2;
  2214. }
  2215. b2Simplex.prototype.Solve3 = function () {
  2216. var w1 = this.m_v1.w;
  2217. var w2 = this.m_v2.w;
  2218. var w3 = this.m_v3.w;
  2219. var e12 = b2Math.SubtractVV(w2, w1);
  2220. var w1e12 = b2Math.Dot(w1, e12);
  2221. var w2e12 = b2Math.Dot(w2, e12);
  2222. var d12_1 = w2e12;
  2223. var d12_2 = (-w1e12);
  2224. var e13 = b2Math.SubtractVV(w3, w1);
  2225. var w1e13 = b2Math.Dot(w1, e13);
  2226. var w3e13 = b2Math.Dot(w3, e13);
  2227. var d13_1 = w3e13;
  2228. var d13_2 = (-w1e13);
  2229. var e23 = b2Math.SubtractVV(w3, w2);
  2230. var w2e23 = b2Math.Dot(w2, e23);
  2231. var w3e23 = b2Math.Dot(w3, e23);
  2232. var d23_1 = w3e23;
  2233. var d23_2 = (-w2e23);
  2234. var n123 = b2Math.CrossVV(e12, e13);
  2235. var d123_1 = n123 * b2Math.CrossVV(w2, w3);
  2236. var d123_2 = n123 * b2Math.CrossVV(w3, w1);
  2237. var d123_3 = n123 * b2Math.CrossVV(w1, w2);
  2238. if (d12_2 <= 0.0 && d13_2 <= 0.0) {
  2239. this.m_v1.a = 1.0;
  2240. this.m_count = 1;
  2241. return;
  2242. }
  2243. if (d12_1 > 0.0 && d12_2 > 0.0 && d123_3 <= 0.0) {
  2244. var inv_d12 = 1.0 / (d12_1 + d12_2);
  2245. this.m_v1.a = d12_1 * inv_d12;
  2246. this.m_v2.a = d12_2 * inv_d12;
  2247. this.m_count = 2;
  2248. return;
  2249. }
  2250. if (d13_1 > 0.0 && d13_2 > 0.0 && d123_2 <= 0.0) {
  2251. var inv_d13 = 1.0 / (d13_1 + d13_2);
  2252. this.m_v1.a = d13_1 * inv_d13;
  2253. this.m_v3.a = d13_2 * inv_d13;
  2254. this.m_count = 2;
  2255. this.m_v2.Set(this.m_v3);
  2256. return;
  2257. }
  2258. if (d12_1 <= 0.0 && d23_2 <= 0.0) {
  2259. this.m_v2.a = 1.0;
  2260. this.m_count = 1;
  2261. this.m_v1.Set(this.m_v2);
  2262. return;
  2263. }
  2264. if (d13_1 <= 0.0 && d23_1 <= 0.0) {
  2265. this.m_v3.a = 1.0;
  2266. this.m_count = 1;
  2267. this.m_v1.Set(this.m_v3);
  2268. return;
  2269. }
  2270. if (d23_1 > 0.0 && d23_2 > 0.0 && d123_1 <= 0.0) {
  2271. var inv_d23 = 1.0 / (d23_1 + d23_2);
  2272. this.m_v2.a = d23_1 * inv_d23;
  2273. this.m_v3.a = d23_2 * inv_d23;
  2274. this.m_count = 2;
  2275. this.m_v1.Set(this.m_v3);
  2276. return;
  2277. }
  2278. var inv_d123 = 1.0 / (d123_1 + d123_2 + d123_3);
  2279. this.m_v1.a = d123_1 * inv_d123;
  2280. this.m_v2.a = d123_2 * inv_d123;
  2281. this.m_v3.a = d123_3 * inv_d123;
  2282. this.m_count = 3;
  2283. }
  2284. b2SimplexCache.b2SimplexCache = function () {
  2285. this.indexA = new Vector_a2j_Number(3);
  2286. this.indexB = new Vector_a2j_Number(3);
  2287. };
  2288. b2SimplexVertex.b2SimplexVertex = function () {};
  2289. b2SimplexVertex.prototype.Set = function (other) {
  2290. this.wA.SetV(other.wA);
  2291. this.wB.SetV(other.wB);
  2292. this.w.SetV(other.w);
  2293. this.a = other.a;
  2294. this.indexA = other.indexA;
  2295. this.indexB = other.indexB;
  2296. }
  2297. b2TimeOfImpact.b2TimeOfImpact = function () {};
  2298. b2TimeOfImpact.TimeOfImpact = function (input) {
  2299. ++b2TimeOfImpact.b2_toiCalls;
  2300. var proxyA = input.proxyA;
  2301. var proxyB = input.proxyB;
  2302. var sweepA = input.sweepA;
  2303. var sweepB = input.sweepB;
  2304. b2Settings.b2Assert(sweepA.t0 == sweepB.t0);
  2305. b2Settings.b2Assert(1.0 - sweepA.t0 > Number.MIN_VALUE);
  2306. var radius = proxyA.m_radius + proxyB.m_radius;
  2307. var tolerance = input.tolerance;
  2308. var alpha = 0.0;
  2309. var k_maxIterations = 1000;
  2310. var iter = 0;
  2311. var target = 0.0;
  2312. b2TimeOfImpact.s_cache.count = 0;
  2313. b2TimeOfImpact.s_distanceInput.useRadii = false;
  2314. for (;;) {
  2315. sweepA.GetTransform(b2TimeOfImpact.s_xfA, alpha);
  2316. sweepB.GetTransform(b2TimeOfImpact.s_xfB, alpha);
  2317. b2TimeOfImpact.s_distanceInput.proxyA = proxyA;
  2318. b2TimeOfImpact.s_distanceInput.proxyB = proxyB;
  2319. b2TimeOfImpact.s_distanceInput.transformA = b2TimeOfImpact.s_xfA;
  2320. b2TimeOfImpact.s_distanceInput.transformB = b2TimeOfImpact.s_xfB;
  2321. b2Distance.Distance(b2TimeOfImpact.s_distanceOutput, b2TimeOfImpact.s_cache, b2TimeOfImpact.s_distanceInput);
  2322. if (b2TimeOfImpact.s_distanceOutput.distance <= 0.0) {
  2323. alpha = 1.0;
  2324. break;
  2325. }
  2326. b2TimeOfImpact.s_fcn.Initialize(b2TimeOfImpact.s_cache, proxyA, b2TimeOfImpact.s_xfA, proxyB, b2TimeOfImpact.s_xfB);
  2327. var separation = b2TimeOfImpact.s_fcn.Evaluate(b2TimeOfImpact.s_xfA, b2TimeOfImpact.s_xfB);
  2328. if (separation <= 0.0) {
  2329. alpha = 1.0;
  2330. break;
  2331. }
  2332. if (iter == 0) {
  2333. if (separation > radius) {
  2334. target = b2Math.Max(radius - tolerance, 0.75 * radius);
  2335. }
  2336. else {
  2337. target = b2Math.Max(separation - tolerance, 0.02 * radius);
  2338. }
  2339. }
  2340. if (separation - target < 0.5 * tolerance) {
  2341. if (iter == 0) {
  2342. alpha = 1.0;
  2343. break;
  2344. }
  2345. break;
  2346. }
  2347. var newAlpha = alpha; {
  2348. var x1 = alpha;
  2349. var x2 = 1.0;
  2350. var f1 = separation;
  2351. sweepA.GetTransform(b2TimeOfImpact.s_xfA, x2);
  2352. sweepB.GetTransform(b2TimeOfImpact.s_xfB, x2);
  2353. var f2 = b2TimeOfImpact.s_fcn.Evaluate(b2TimeOfImpact.s_xfA, b2TimeOfImpact.s_xfB);
  2354. if (f2 >= target) {
  2355. alpha = 1.0;
  2356. break;
  2357. }
  2358. var rootIterCount = 0;
  2359. for (;;) {
  2360. var x = 0;
  2361. if (rootIterCount & 1) {
  2362. x = x1 + (target - f1) * (x2 - x1) / (f2 - f1);
  2363. }
  2364. else {
  2365. x = 0.5 * (x1 + x2);
  2366. }
  2367. sweepA.GetTransform(b2TimeOfImpact.s_xfA, x);
  2368. sweepB.GetTransform(b2TimeOfImpact.s_xfB, x);
  2369. var f = b2TimeOfImpact.s_fcn.Evaluate(b2TimeOfImpact.s_xfA, b2TimeOfImpact.s_xfB);
  2370. if (b2Math.Abs(f - target) < 0.025 * tolerance) {
  2371. newAlpha = x;
  2372. break;
  2373. }
  2374. if (f > target) {
  2375. x1 = x;
  2376. f1 = f;
  2377. }
  2378. else {
  2379. x2 = x;
  2380. f2 = f;
  2381. }++rootIterCount;
  2382. ++b2TimeOfImpact.b2_toiRootIters;
  2383. if (rootIterCount == 50) {
  2384. break;
  2385. }
  2386. }
  2387. b2TimeOfImpact.b2_toiMaxRootIters = b2Math.Max(b2TimeOfImpact.b2_toiMaxRootIters, rootIterCount);
  2388. }
  2389. if (newAlpha < (1.0 + 100.0 * Number.MIN_VALUE) * alpha) {
  2390. break;
  2391. }
  2392. alpha = newAlpha;
  2393. iter++;
  2394. ++b2TimeOfImpact.b2_toiIters;
  2395. if (iter == k_maxIterations) {
  2396. break;
  2397. }
  2398. }
  2399. b2TimeOfImpact.b2_toiMaxIters = b2Math.Max(b2TimeOfImpact.b2_toiMaxIters, iter);
  2400. return alpha;
  2401. }
  2402. Box2D.postDefs.push(function () {
  2403. Box2D.Collision.b2TimeOfImpact.b2_toiCalls = 0;
  2404. Box2D.Collision.b2TimeOfImpact.b2_toiIters = 0;
  2405. Box2D.Collision.b2TimeOfImpact.b2_toiMaxIters = 0;
  2406. Box2D.Collision.b2TimeOfImpact.b2_toiRootIters = 0;
  2407. Box2D.Collision.b2TimeOfImpact.b2_toiMaxRootIters = 0;
  2408. Box2D.Collision.b2TimeOfImpact.s_cache = new b2SimplexCache();
  2409. Box2D.Collision.b2TimeOfImpact.s_distanceInput = new b2DistanceInput();
  2410. Box2D.Collision.b2TimeOfImpact.s_xfA = new b2Transform();
  2411. Box2D.Collision.b2TimeOfImpact.s_xfB = new b2Transform();
  2412. Box2D.Collision.b2TimeOfImpact.s_fcn = new b2SeparationFunction();
  2413. Box2D.Collision.b2TimeOfImpact.s_distanceOutput = new b2DistanceOutput();
  2414. });
  2415. b2TOIInput.b2TOIInput = function () {
  2416. this.proxyA = new b2DistanceProxy();
  2417. this.proxyB = new b2DistanceProxy();
  2418. this.sweepA = new b2Sweep();
  2419. this.sweepB = new b2Sweep();
  2420. };
  2421. b2WorldManifold.b2WorldManifold = function () {
  2422. this.m_normal = new b2Vec2();
  2423. };
  2424. b2WorldManifold.prototype.b2WorldManifold = function () {
  2425. this.m_points = new Vector(b2Settings.b2_maxManifoldPoints);
  2426. for (var i = 0; i < b2Settings.b2_maxManifoldPoints; i++) {
  2427. this.m_points[i] = new b2Vec2();
  2428. }
  2429. }
  2430. b2WorldManifold.prototype.Initialize = function (manifold, xfA, radiusA, xfB, radiusB) {
  2431. if (radiusA === undefined) radiusA = 0;
  2432. if (radiusB === undefined) radiusB = 0;
  2433. if (manifold.m_pointCount == 0) {
  2434. return;
  2435. }
  2436. var i = 0;
  2437. var tVec;
  2438. var tMat;
  2439. var normalX = 0;
  2440. var normalY = 0;
  2441. var planePointX = 0;
  2442. var planePointY = 0;
  2443. var clipPointX = 0;
  2444. var clipPointY = 0;
  2445. switch (manifold.m_type) {
  2446. case b2Manifold.e_circles:
  2447. {
  2448. tMat = xfA.R;
  2449. tVec = manifold.m_localPoint;
  2450. var pointAX = xfA.position.x + tMat.col1.x * tVec.x + tMat.col2.x * tVec.y;
  2451. var pointAY = xfA.position.y + tMat.col1.y * tVec.x + tMat.col2.y * tVec.y;
  2452. tMat = xfB.R;
  2453. tVec = manifold.m_points[0].m_localPoint;
  2454. var pointBX = xfB.position.x + tMat.col1.x * tVec.x + tMat.col2.x * tVec.y;
  2455. var pointBY = xfB.position.y + tMat.col1.y * tVec.x + tMat.col2.y * tVec.y;
  2456. var dX = pointBX - pointAX;
  2457. var dY = pointBY - pointAY;
  2458. var d2 = dX * dX + dY * dY;
  2459. if (d2 > Number.MIN_VALUE * Number.MIN_VALUE) {
  2460. var d = Math.sqrt(d2);
  2461. this.m_normal.x = dX / d;
  2462. this.m_normal.y = dY / d;
  2463. }
  2464. else {
  2465. this.m_normal.x = 1;
  2466. this.m_normal.y = 0;
  2467. }
  2468. var cAX = pointAX + radiusA * this.m_normal.x;
  2469. var cAY = pointAY + radiusA * this.m_normal.y;
  2470. var cBX = pointBX - radiusB * this.m_normal.x;
  2471. var cBY = pointBY - radiusB * this.m_normal.y;
  2472. this.m_points[0].x = 0.5 * (cAX + cBX);
  2473. this.m_points[0].y = 0.5 * (cAY + cBY);
  2474. }
  2475. break;
  2476. case b2Manifold.e_faceA:
  2477. {
  2478. tMat = xfA.R;
  2479. tVec = manifold.m_localPlaneNormal;
  2480. normalX = tMat.col1.x * tVec.x + tMat.col2.x * tVec.y;
  2481. normalY = tMat.col1.y * tVec.x + tMat.col2.y * tVec.y;
  2482. tMat = xfA.R;
  2483. tVec = manifold.m_localPoint;
  2484. planePointX = xfA.position.x + tMat.col1.x * tVec.x + tMat.col2.x * tVec.y;
  2485. planePointY = xfA.position.y + tMat.col1.y * tVec.x + tMat.col2.y * tVec.y;
  2486. this.m_normal.x = normalX;
  2487. this.m_normal.y = normalY;
  2488. for (i = 0;
  2489. i < manifold.m_pointCount; i++) {
  2490. tMat = xfB.R;
  2491. tVec = manifold.m_points[i].m_localPoint;
  2492. clipPointX = xfB.position.x + tMat.col1.x * tVec.x + tMat.col2.x * tVec.y;
  2493. clipPointY = xfB.position.y + tMat.col1.y * tVec.x + tMat.col2.y * tVec.y;
  2494. this.m_points[i].x = clipPointX + 0.5 * (radiusA - (clipPointX - planePointX) * normalX - (clipPointY - planePointY) * normalY - radiusB) * normalX;
  2495. this.m_points[i].y = clipPointY + 0.5 * (radiusA - (clipPointX - planePointX) * normalX - (clipPointY - planePointY) * normalY - radiusB) * normalY;
  2496. }
  2497. }
  2498. break;
  2499. case b2Manifold.e_faceB:
  2500. {
  2501. tMat = xfB.R;
  2502. tVec = manifold.m_localPlaneNormal;
  2503. normalX = tMat.col1.x * tVec.x + tMat.col2.x * tVec.y;
  2504. normalY = tMat.col1.y * tVec.x + tMat.col2.y * tVec.y;
  2505. tMat = xfB.R;
  2506. tVec = manifold.m_localPoint;
  2507. planePointX = xfB.position.x + tMat.col1.x * tVec.x + tMat.col2.x * tVec.y;
  2508. planePointY = xfB.position.y + tMat.col1.y * tVec.x + tMat.col2.y * tVec.y;
  2509. this.m_normal.x = (-normalX);
  2510. this.m_normal.y = (-normalY);
  2511. for (i = 0;
  2512. i < manifold.m_pointCount; i++) {
  2513. tMat = xfA.R;
  2514. tVec = manifold.m_points[i].m_localPoint;
  2515. clipPointX = xfA.position.x + tMat.col1.x * tVec.x + tMat.col2.x * tVec.y;
  2516. clipPointY = xfA.position.y + tMat.col1.y * tVec.x + tMat.col2.y * tVec.y;
  2517. this.m_points[i].x = clipPointX + 0.5 * (radiusB - (clipPointX - planePointX) * normalX - (clipPointY - planePointY) * normalY - radiusA) * normalX;
  2518. this.m_points[i].y = clipPointY + 0.5 * (radiusB - (clipPointX - planePointX) * normalX - (clipPointY - planePointY) * normalY - radiusA) * normalY;
  2519. }
  2520. }
  2521. break;
  2522. }
  2523. }
  2524. ClipVertex.ClipVertex = function () {
  2525. this.v = new b2Vec2();
  2526. this.id = new b2ContactID();
  2527. };
  2528. ClipVertex.prototype.Set = function (other) {
  2529. this.v.SetV(other.v);
  2530. this.id.Set(other.id);
  2531. }
  2532. Features.Features = function () {};
  2533. Object.defineProperty(Features.prototype, 'referenceEdge', {
  2534. enumerable: false,
  2535. configurable: true,
  2536. get: function () {
  2537. return this._referenceEdge;
  2538. }
  2539. });
  2540. Object.defineProperty(Features.prototype, 'referenceEdge', {
  2541. enumerable: false,
  2542. configurable: true,
  2543. set: function (value) {
  2544. if (value === undefined) value = 0;
  2545. this._referenceEdge = value;
  2546. this._m_id._key = (this._m_id._key & 0xffffff00) | (this._referenceEdge & 0x000000ff);
  2547. }
  2548. });
  2549. Object.defineProperty(Features.prototype, 'incidentEdge', {
  2550. enumerable: false,
  2551. configurable: true,
  2552. get: function () {
  2553. return this._incidentEdge;
  2554. }
  2555. });
  2556. Object.defineProperty(Features.prototype, 'incidentEdge', {
  2557. enumerable: false,
  2558. configurable: true,
  2559. set: function (value) {
  2560. if (value === undefined) value = 0;
  2561. this._incidentEdge = value;
  2562. this._m_id._key = (this._m_id._key & 0xffff00ff) | ((this._incidentEdge << 8) & 0x0000ff00);
  2563. }
  2564. });
  2565. Object.defineProperty(Features.prototype, 'incidentVertex', {
  2566. enumerable: false,
  2567. configurable: true,
  2568. get: function () {
  2569. return this._incidentVertex;
  2570. }
  2571. });
  2572. Object.defineProperty(Features.prototype, 'incidentVertex', {
  2573. enumerable: false,
  2574. configurable: true,
  2575. set: function (value) {
  2576. if (value === undefined) value = 0;
  2577. this._incidentVertex = value;
  2578. this._m_id._key = (this._m_id._key & 0xff00ffff) | ((this._incidentVertex << 16) & 0x00ff0000);
  2579. }
  2580. });
  2581. Object.defineProperty(Features.prototype, 'flip', {
  2582. enumerable: false,
  2583. configurable: true,
  2584. get: function () {
  2585. return this._flip;
  2586. }
  2587. });
  2588. Object.defineProperty(Features.prototype, 'flip', {
  2589. enumerable: false,
  2590. configurable: true,
  2591. set: function (value) {
  2592. if (value === undefined) value = 0;
  2593. this._flip = value;
  2594. this._m_id._key = (this._m_id._key & 0x00ffffff) | ((this._flip << 24) & 0xff000000);
  2595. }
  2596. });
  2597. })();
  2598. (function () {
  2599. var b2Color = Box2D.Common.b2Color,
  2600. b2internal = Box2D.Common.b2internal,
  2601. b2Settings = Box2D.Common.b2Settings,
  2602. b2CircleShape = Box2D.Collision.Shapes.b2CircleShape,
  2603. b2EdgeChainDef = Box2D.Collision.Shapes.b2EdgeChainDef,
  2604. b2EdgeShape = Box2D.Collision.Shapes.b2EdgeShape,
  2605. b2MassData = Box2D.Collision.Shapes.b2MassData,
  2606. b2PolygonShape = Box2D.Collision.Shapes.b2PolygonShape,
  2607. b2Shape = Box2D.Collision.Shapes.b2Shape,
  2608. b2Mat22 = Box2D.Common.Math.b2Mat22,
  2609. b2Mat33 = Box2D.Common.Math.b2Mat33,
  2610. b2Math = Box2D.Common.Math.b2Math,
  2611. b2Sweep = Box2D.Common.Math.b2Sweep,
  2612. b2Transform = Box2D.Common.Math.b2Transform,
  2613. b2Vec2 = Box2D.Common.Math.b2Vec2,
  2614. b2Vec3 = Box2D.Common.Math.b2Vec3,
  2615. b2Body = Box2D.Dynamics.b2Body,
  2616. b2BodyDef = Box2D.Dynamics.b2BodyDef,
  2617. b2ContactFilter = Box2D.Dynamics.b2ContactFilter,
  2618. b2ContactImpulse = Box2D.Dynamics.b2ContactImpulse,
  2619. b2ContactListener = Box2D.Dynamics.b2ContactListener,
  2620. b2ContactManager = Box2D.Dynamics.b2ContactManager,
  2621. b2DebugDraw = Box2D.Dynamics.b2DebugDraw,
  2622. b2DestructionListener = Box2D.Dynamics.b2DestructionListener,
  2623. b2FilterData = Box2D.Dynamics.b2FilterData,
  2624. b2Fixture = Box2D.Dynamics.b2Fixture,
  2625. b2FixtureDef = Box2D.Dynamics.b2FixtureDef,
  2626. b2Island = Box2D.Dynamics.b2Island,
  2627. b2TimeStep = Box2D.Dynamics.b2TimeStep,
  2628. b2World = Box2D.Dynamics.b2World,
  2629. b2AABB = Box2D.Collision.b2AABB,
  2630. b2Bound = Box2D.Collision.b2Bound,
  2631. b2BoundValues = Box2D.Collision.b2BoundValues,
  2632. b2Collision = Box2D.Collision.b2Collision,
  2633. b2ContactID = Box2D.Collision.b2ContactID,
  2634. b2ContactPoint = Box2D.Collision.b2ContactPoint,
  2635. b2Distance = Box2D.Collision.b2Distance,
  2636. b2DistanceInput = Box2D.Collision.b2DistanceInput,
  2637. b2DistanceOutput = Box2D.Collision.b2DistanceOutput,
  2638. b2DistanceProxy = Box2D.Collision.b2DistanceProxy,
  2639. b2DynamicTree = Box2D.Collision.b2DynamicTree,
  2640. b2DynamicTreeBroadPhase = Box2D.Collision.b2DynamicTreeBroadPhase,
  2641. b2DynamicTreeNode = Box2D.Collision.b2DynamicTreeNode,
  2642. b2DynamicTreePair = Box2D.Collision.b2DynamicTreePair,
  2643. b2Manifold = Box2D.Collision.b2Manifold,
  2644. b2ManifoldPoint = Box2D.Collision.b2ManifoldPoint,
  2645. b2Point = Box2D.Collision.b2Point,
  2646. b2RayCastInput = Box2D.Collision.b2RayCastInput,
  2647. b2RayCastOutput = Box2D.Collision.b2RayCastOutput,
  2648. b2Segment = Box2D.Collision.b2Segment,
  2649. b2SeparationFunction = Box2D.Collision.b2SeparationFunction,
  2650. b2Simplex = Box2D.Collision.b2Simplex,
  2651. b2SimplexCache = Box2D.Collision.b2SimplexCache,
  2652. b2SimplexVertex = Box2D.Collision.b2SimplexVertex,
  2653. b2TimeOfImpact = Box2D.Collision.b2TimeOfImpact,
  2654. b2TOIInput = Box2D.Collision.b2TOIInput,
  2655. b2WorldManifold = Box2D.Collision.b2WorldManifold,
  2656. ClipVertex = Box2D.Collision.ClipVertex,
  2657. Features = Box2D.Collision.Features,
  2658. IBroadPhase = Box2D.Collision.IBroadPhase;
  2659. Box2D.inherit(b2CircleShape, Box2D.Collision.Shapes.b2Shape);
  2660. b2CircleShape.prototype.__super = Box2D.Collision.Shapes.b2Shape.prototype;
  2661. b2CircleShape.b2CircleShape = function () {
  2662. Box2D.Collision.Shapes.b2Shape.b2Shape.apply(this, arguments);
  2663. this.m_p = new b2Vec2();
  2664. };
  2665. b2CircleShape.prototype.Copy = function () {
  2666. var s = new b2CircleShape();
  2667. s.Set(this);
  2668. return s;
  2669. }
  2670. b2CircleShape.prototype.Set = function (other) {
  2671. this.__super.Set.call(this, other);
  2672. if (Box2D.is(other, b2CircleShape)) {
  2673. var other2 = (other instanceof b2CircleShape ? other : null);
  2674. this.m_p.SetV(other2.m_p);
  2675. }
  2676. }
  2677. b2CircleShape.prototype.TestPoint = function (transform, p) {
  2678. var tMat = transform.R;
  2679. var dX = transform.position.x + (tMat.col1.x * this.m_p.x + tMat.col2.x * this.m_p.y);
  2680. var dY = transform.position.y + (tMat.col1.y * this.m_p.x + tMat.col2.y * this.m_p.y);
  2681. dX = p.x - dX;
  2682. dY = p.y - dY;
  2683. return (dX * dX + dY * dY) <= this.m_radius * this.m_radius;
  2684. }
  2685. b2CircleShape.prototype.RayCast = function (output, input, transform) {
  2686. var tMat = transform.R;
  2687. var positionX = transform.position.x + (tMat.col1.x * this.m_p.x + tMat.col2.x * this.m_p.y);
  2688. var positionY = transform.position.y + (tMat.col1.y * this.m_p.x + tMat.col2.y * this.m_p.y);
  2689. var sX = input.p1.x - positionX;
  2690. var sY = input.p1.y - positionY;
  2691. var b = (sX * sX + sY * sY) - this.m_radius * this.m_radius;
  2692. var rX = input.p2.x - input.p1.x;
  2693. var rY = input.p2.y - input.p1.y;
  2694. var c = (sX * rX + sY * rY);
  2695. var rr = (rX * rX + rY * rY);
  2696. var sigma = c * c - rr * b;
  2697. if (sigma < 0.0 || rr < Number.MIN_VALUE) {
  2698. return false;
  2699. }
  2700. var a = (-(c + Math.sqrt(sigma)));
  2701. if (0.0 <= a && a <= input.maxFraction * rr) {
  2702. a /= rr;
  2703. output.fraction = a;
  2704. output.normal.x = sX + a * rX;
  2705. output.normal.y = sY + a * rY;
  2706. output.normal.Normalize();
  2707. return true;
  2708. }
  2709. return false;
  2710. }
  2711. b2CircleShape.prototype.ComputeAABB = function (aabb, transform) {
  2712. var tMat = transform.R;
  2713. var pX = transform.position.x + (tMat.col1.x * this.m_p.x + tMat.col2.x * this.m_p.y);
  2714. var pY = transform.position.y + (tMat.col1.y * this.m_p.x + tMat.col2.y * this.m_p.y);
  2715. aabb.lowerBound.Set(pX - this.m_radius, pY - this.m_radius);
  2716. aabb.upperBound.Set(pX + this.m_radius, pY + this.m_radius);
  2717. }
  2718. b2CircleShape.prototype.ComputeMass = function (massData, density) {
  2719. if (density === undefined) density = 0;
  2720. massData.mass = density * b2Settings.b2_pi * this.m_radius * this.m_radius;
  2721. massData.center.SetV(this.m_p);
  2722. massData.I = massData.mass * (0.5 * this.m_radius * this.m_radius + (this.m_p.x * this.m_p.x + this.m_p.y * this.m_p.y));
  2723. }
  2724. b2CircleShape.prototype.ComputeSubmergedArea = function (normal, offset, xf, c) {
  2725. if (offset === undefined) offset = 0;
  2726. var p = b2Math.MulX(xf, this.m_p);
  2727. var l = (-(b2Math.Dot(normal, p) - offset));
  2728. if (l < (-this.m_radius) + Number.MIN_VALUE) {
  2729. return 0;
  2730. }
  2731. if (l > this.m_radius) {
  2732. c.SetV(p);
  2733. return Math.PI * this.m_radius * this.m_radius;
  2734. }
  2735. var r2 = this.m_radius * this.m_radius;
  2736. var l2 = l * l;
  2737. var area = r2 * (Math.asin(l / this.m_radius) + Math.PI / 2) + l * Math.sqrt(r2 - l2);
  2738. var com = (-2 / 3 * Math.pow(r2 - l2, 1.5) / area);
  2739. c.x = p.x + normal.x * com;
  2740. c.y = p.y + normal.y * com;
  2741. return area;
  2742. }
  2743. b2CircleShape.prototype.GetLocalPosition = function () {
  2744. return this.m_p;
  2745. }
  2746. b2CircleShape.prototype.SetLocalPosition = function (position) {
  2747. this.m_p.SetV(position);
  2748. }
  2749. b2CircleShape.prototype.GetRadius = function () {
  2750. return this.m_radius;
  2751. }
  2752. b2CircleShape.prototype.SetRadius = function (radius) {
  2753. if (radius === undefined) radius = 0;
  2754. this.m_radius = radius;
  2755. }
  2756. b2CircleShape.prototype.b2CircleShape = function (radius) {
  2757. if (radius === undefined) radius = 0;
  2758. this.__super.b2Shape.call(this);
  2759. this.m_type = b2Shape.e_circleShape;
  2760. this.m_radius = radius;
  2761. }
  2762. b2EdgeChainDef.b2EdgeChainDef = function () {};
  2763. b2EdgeChainDef.prototype.b2EdgeChainDef = function () {
  2764. this.vertexCount = 0;
  2765. this.isALoop = true;
  2766. this.vertices = [];
  2767. }
  2768. Box2D.inherit(b2EdgeShape, Box2D.Collision.Shapes.b2Shape);
  2769. b2EdgeShape.prototype.__super = Box2D.Collision.Shapes.b2Shape.prototype;
  2770. b2EdgeShape.b2EdgeShape = function () {
  2771. Box2D.Collision.Shapes.b2Shape.b2Shape.apply(this, arguments);
  2772. this.s_supportVec = new b2Vec2();
  2773. this.m_v1 = new b2Vec2();
  2774. this.m_v2 = new b2Vec2();
  2775. this.m_coreV1 = new b2Vec2();
  2776. this.m_coreV2 = new b2Vec2();
  2777. this.m_normal = new b2Vec2();
  2778. this.m_direction = new b2Vec2();
  2779. this.m_cornerDir1 = new b2Vec2();
  2780. this.m_cornerDir2 = new b2Vec2();
  2781. };
  2782. b2EdgeShape.prototype.TestPoint = function (transform, p) {
  2783. return false;
  2784. }
  2785. b2EdgeShape.prototype.RayCast = function (output, input, transform) {
  2786. var tMat;
  2787. var rX = input.p2.x - input.p1.x;
  2788. var rY = input.p2.y - input.p1.y;
  2789. tMat = transform.R;
  2790. var v1X = transform.position.x + (tMat.col1.x * this.m_v1.x + tMat.col2.x * this.m_v1.y);
  2791. var v1Y = transform.position.y + (tMat.col1.y * this.m_v1.x + tMat.col2.y * this.m_v1.y);
  2792. var nX = transform.position.y + (tMat.col1.y * this.m_v2.x + tMat.col2.y * this.m_v2.y) - v1Y;
  2793. var nY = (-(transform.position.x + (tMat.col1.x * this.m_v2.x + tMat.col2.x * this.m_v2.y) - v1X));
  2794. var k_slop = 100.0 * Number.MIN_VALUE;
  2795. var denom = (-(rX * nX + rY * nY));
  2796. if (denom > k_slop) {
  2797. var bX = input.p1.x - v1X;
  2798. var bY = input.p1.y - v1Y;
  2799. var a = (bX * nX + bY * nY);
  2800. if (0.0 <= a && a <= input.maxFraction * denom) {
  2801. var mu2 = (-rX * bY) + rY * bX;
  2802. if ((-k_slop * denom) <= mu2 && mu2 <= denom * (1.0 + k_slop)) {
  2803. a /= denom;
  2804. output.fraction = a;
  2805. var nLen = Math.sqrt(nX * nX + nY * nY);
  2806. output.normal.x = nX / nLen;
  2807. output.normal.y = nY / nLen;
  2808. return true;
  2809. }
  2810. }
  2811. }
  2812. return false;
  2813. }
  2814. b2EdgeShape.prototype.ComputeAABB = function (aabb, transform) {
  2815. var tMat = transform.R;
  2816. var v1X = transform.position.x + (tMat.col1.x * this.m_v1.x + tMat.col2.x * this.m_v1.y);
  2817. var v1Y = transform.position.y + (tMat.col1.y * this.m_v1.x + tMat.col2.y * this.m_v1.y);
  2818. var v2X = transform.position.x + (tMat.col1.x * this.m_v2.x + tMat.col2.x * this.m_v2.y);
  2819. var v2Y = transform.position.y + (tMat.col1.y * this.m_v2.x + tMat.col2.y * this.m_v2.y);
  2820. if (v1X < v2X) {
  2821. aabb.lowerBound.x = v1X;
  2822. aabb.upperBound.x = v2X;
  2823. }
  2824. else {
  2825. aabb.lowerBound.x = v2X;
  2826. aabb.upperBound.x = v1X;
  2827. }
  2828. if (v1Y < v2Y) {
  2829. aabb.lowerBound.y = v1Y;
  2830. aabb.upperBound.y = v2Y;
  2831. }
  2832. else {
  2833. aabb.lowerBound.y = v2Y;
  2834. aabb.upperBound.y = v1Y;
  2835. }
  2836. }
  2837. b2EdgeShape.prototype.ComputeMass = function (massData, density) {
  2838. if (density === undefined) density = 0;
  2839. massData.mass = 0;
  2840. massData.center.SetV(this.m_v1);
  2841. massData.I = 0;
  2842. }
  2843. b2EdgeShape.prototype.ComputeSubmergedArea = function (normal, offset, xf, c) {
  2844. if (offset === undefined) offset = 0;
  2845. var v0 = new b2Vec2(normal.x * offset, normal.y * offset);
  2846. var v1 = b2Math.MulX(xf, this.m_v1);
  2847. var v2 = b2Math.MulX(xf, this.m_v2);
  2848. var d1 = b2Math.Dot(normal, v1) - offset;
  2849. var d2 = b2Math.Dot(normal, v2) - offset;
  2850. if (d1 > 0) {
  2851. if (d2 > 0) {
  2852. return 0;
  2853. }
  2854. else {
  2855. v1.x = (-d2 / (d1 - d2) * v1.x) + d1 / (d1 - d2) * v2.x;
  2856. v1.y = (-d2 / (d1 - d2) * v1.y) + d1 / (d1 - d2) * v2.y;
  2857. }
  2858. }
  2859. else {
  2860. if (d2 > 0) {
  2861. v2.x = (-d2 / (d1 - d2) * v1.x) + d1 / (d1 - d2) * v2.x;
  2862. v2.y = (-d2 / (d1 - d2) * v1.y) + d1 / (d1 - d2) * v2.y;
  2863. }
  2864. else {}
  2865. }
  2866. c.x = (v0.x + v1.x + v2.x) / 3;
  2867. c.y = (v0.y + v1.y + v2.y) / 3;
  2868. return 0.5 * ((v1.x - v0.x) * (v2.y - v0.y) - (v1.y - v0.y) * (v2.x - v0.x));
  2869. }
  2870. b2EdgeShape.prototype.GetLength = function () {
  2871. return this.m_length;
  2872. }
  2873. b2EdgeShape.prototype.GetVertex1 = function () {
  2874. return this.m_v1;
  2875. }
  2876. b2EdgeShape.prototype.GetVertex2 = function () {
  2877. return this.m_v2;
  2878. }
  2879. b2EdgeShape.prototype.GetCoreVertex1 = function () {
  2880. return this.m_coreV1;
  2881. }
  2882. b2EdgeShape.prototype.GetCoreVertex2 = function () {
  2883. return this.m_coreV2;
  2884. }
  2885. b2EdgeShape.prototype.GetNormalVector = function () {
  2886. return this.m_normal;
  2887. }
  2888. b2EdgeShape.prototype.GetDirectionVector = function () {
  2889. return this.m_direction;
  2890. }
  2891. b2EdgeShape.prototype.GetCorner1Vector = function () {
  2892. return this.m_cornerDir1;
  2893. }
  2894. b2EdgeShape.prototype.GetCorner2Vector = function () {
  2895. return this.m_cornerDir2;
  2896. }
  2897. b2EdgeShape.prototype.Corner1IsConvex = function () {
  2898. return this.m_cornerConvex1;
  2899. }
  2900. b2EdgeShape.prototype.Corner2IsConvex = function () {
  2901. return this.m_cornerConvex2;
  2902. }
  2903. b2EdgeShape.prototype.GetFirstVertex = function (xf) {
  2904. var tMat = xf.R;
  2905. return new b2Vec2(xf.position.x + (tMat.col1.x * this.m_coreV1.x + tMat.col2.x * this.m_coreV1.y), xf.position.y + (tMat.col1.y * this.m_coreV1.x + tMat.col2.y * this.m_coreV1.y));
  2906. }
  2907. b2EdgeShape.prototype.GetNextEdge = function () {
  2908. return this.m_nextEdge;
  2909. }
  2910. b2EdgeShape.prototype.GetPrevEdge = function () {
  2911. return this.m_prevEdge;
  2912. }
  2913. b2EdgeShape.prototype.Support = function (xf, dX, dY) {
  2914. if (dX === undefined) dX = 0;
  2915. if (dY === undefined) dY = 0;
  2916. var tMat = xf.R;
  2917. var v1X = xf.position.x + (tMat.col1.x * this.m_coreV1.x + tMat.col2.x * this.m_coreV1.y);
  2918. var v1Y = xf.position.y + (tMat.col1.y * this.m_coreV1.x + tMat.col2.y * this.m_coreV1.y);
  2919. var v2X = xf.position.x + (tMat.col1.x * this.m_coreV2.x + tMat.col2.x * this.m_coreV2.y);
  2920. var v2Y = xf.position.y + (tMat.col1.y * this.m_coreV2.x + tMat.col2.y * this.m_coreV2.y);
  2921. if ((v1X * dX + v1Y * dY) > (v2X * dX + v2Y * dY)) {
  2922. this.s_supportVec.x = v1X;
  2923. this.s_supportVec.y = v1Y;
  2924. }
  2925. else {
  2926. this.s_supportVec.x = v2X;
  2927. this.s_supportVec.y = v2Y;
  2928. }
  2929. return this.s_supportVec;
  2930. }
  2931. b2EdgeShape.prototype.b2EdgeShape = function (v1, v2) {
  2932. this.__super.b2Shape.call(this);
  2933. this.m_type = b2Shape.e_edgeShape;
  2934. this.m_prevEdge = null;
  2935. this.m_nextEdge = null;
  2936. this.m_v1 = v1;
  2937. this.m_v2 = v2;
  2938. this.m_direction.Set(this.m_v2.x - this.m_v1.x, this.m_v2.y - this.m_v1.y);
  2939. this.m_length = this.m_direction.Normalize();
  2940. this.m_normal.Set(this.m_direction.y, (-this.m_direction.x));
  2941. this.m_coreV1.Set((-b2Settings.b2_toiSlop * (this.m_normal.x - this.m_direction.x)) + this.m_v1.x, (-b2Settings.b2_toiSlop * (this.m_normal.y - this.m_direction.y)) + this.m_v1.y);
  2942. this.m_coreV2.Set((-b2Settings.b2_toiSlop * (this.m_normal.x + this.m_direction.x)) + this.m_v2.x, (-b2Settings.b2_toiSlop * (this.m_normal.y + this.m_direction.y)) + this.m_v2.y);
  2943. this.m_cornerDir1 = this.m_normal;
  2944. this.m_cornerDir2.Set((-this.m_normal.x), (-this.m_normal.y));
  2945. }
  2946. b2EdgeShape.prototype.SetPrevEdge = function (edge, core, cornerDir, convex) {
  2947. this.m_prevEdge = edge;
  2948. this.m_coreV1 = core;
  2949. this.m_cornerDir1 = cornerDir;
  2950. this.m_cornerConvex1 = convex;
  2951. }
  2952. b2EdgeShape.prototype.SetNextEdge = function (edge, core, cornerDir, convex) {
  2953. this.m_nextEdge = edge;
  2954. this.m_coreV2 = core;
  2955. this.m_cornerDir2 = cornerDir;
  2956. this.m_cornerConvex2 = convex;
  2957. }
  2958. b2MassData.b2MassData = function () {
  2959. this.mass = 0.0;
  2960. this.center = new b2Vec2(0, 0);
  2961. this.I = 0.0;
  2962. };
  2963. Box2D.inherit(b2PolygonShape, Box2D.Collision.Shapes.b2Shape);
  2964. b2PolygonShape.prototype.__super = Box2D.Collision.Shapes.b2Shape.prototype;
  2965. b2PolygonShape.b2PolygonShape = function () {
  2966. Box2D.Collision.Shapes.b2Shape.b2Shape.apply(this, arguments);
  2967. };
  2968. b2PolygonShape.prototype.Copy = function () {
  2969. var s = new b2PolygonShape();
  2970. s.Set(this);
  2971. return s;
  2972. }
  2973. b2PolygonShape.prototype.Set = function (other) {
  2974. this.__super.Set.call(this, other);
  2975. if (Box2D.is(other, b2PolygonShape)) {
  2976. var other2 = (other instanceof b2PolygonShape ? other : null);
  2977. this.m_centroid.SetV(other2.m_centroid);
  2978. this.m_vertexCount = other2.m_vertexCount;
  2979. this.Reserve(this.m_vertexCount);
  2980. for (var i = 0; i < this.m_vertexCount; i++) {
  2981. this.m_vertices[i].SetV(other2.m_vertices[i]);
  2982. this.m_normals[i].SetV(other2.m_normals[i]);
  2983. }
  2984. }
  2985. }
  2986. b2PolygonShape.prototype.SetAsArray = function (vertices, vertexCount) {
  2987. if (vertexCount === undefined) vertexCount = 0;
  2988. var v = new Vector();
  2989. var i = 0,
  2990. tVec;
  2991. for (i = 0;
  2992. i < vertices.length; ++i) {
  2993. tVec = vertices[i];
  2994. v.push(tVec);
  2995. }
  2996. this.SetAsVector(v, vertexCount);
  2997. }
  2998. b2PolygonShape.AsArray = function (vertices, vertexCount) {
  2999. if (vertexCount === undefined) vertexCount = 0;
  3000. var polygonShape = new b2PolygonShape();
  3001. polygonShape.SetAsArray(vertices, vertexCount);
  3002. return polygonShape;
  3003. }
  3004. b2PolygonShape.prototype.SetAsVector = function (vertices, vertexCount) {
  3005. if (vertexCount === undefined) vertexCount = 0;
  3006. if (vertexCount == 0) vertexCount = vertices.length;
  3007. b2Settings.b2Assert(2 <= vertexCount);
  3008. this.m_vertexCount = vertexCount;
  3009. this.Reserve(vertexCount);
  3010. var i = 0;
  3011. for (i = 0;
  3012. i < this.m_vertexCount; i++) {
  3013. this.m_vertices[i].SetV(vertices[i]);
  3014. }
  3015. for (i = 0;
  3016. i < this.m_vertexCount; ++i) {
  3017. var i1 = parseInt(i);
  3018. var i2 = parseInt(i + 1 < this.m_vertexCount ? i + 1 : 0);
  3019. var edge = b2Math.SubtractVV(this.m_vertices[i2], this.m_vertices[i1]);
  3020. b2Settings.b2Assert(edge.LengthSquared() > Number.MIN_VALUE);
  3021. this.m_normals[i].SetV(b2Math.CrossVF(edge, 1.0));
  3022. this.m_normals[i].Normalize();
  3023. }
  3024. this.m_centroid = b2PolygonShape.ComputeCentroid(this.m_vertices, this.m_vertexCount);
  3025. }
  3026. b2PolygonShape.AsVector = function (vertices, vertexCount) {
  3027. if (vertexCount === undefined) vertexCount = 0;
  3028. var polygonShape = new b2PolygonShape();
  3029. polygonShape.SetAsVector(vertices, vertexCount);
  3030. return polygonShape;
  3031. }
  3032. b2PolygonShape.prototype.SetAsBox = function (hx, hy) {
  3033. if (hx === undefined) hx = 0;
  3034. if (hy === undefined) hy = 0;
  3035. this.m_vertexCount = 4;
  3036. this.Reserve(4);
  3037. this.m_vertices[0].Set((-hx), (-hy));
  3038. this.m_vertices[1].Set(hx, (-hy));
  3039. this.m_vertices[2].Set(hx, hy);
  3040. this.m_vertices[3].Set((-hx), hy);
  3041. this.m_normals[0].Set(0.0, (-1.0));
  3042. this.m_normals[1].Set(1.0, 0.0);
  3043. this.m_normals[2].Set(0.0, 1.0);
  3044. this.m_normals[3].Set((-1.0), 0.0);
  3045. this.m_centroid.SetZero();
  3046. }
  3047. b2PolygonShape.AsBox = function (hx, hy) {
  3048. if (hx === undefined) hx = 0;
  3049. if (hy === undefined) hy = 0;
  3050. var polygonShape = new b2PolygonShape();
  3051. polygonShape.SetAsBox(hx, hy);
  3052. return polygonShape;
  3053. }
  3054. b2PolygonShape.prototype.SetAsOrientedBox = function (hx, hy, center, angle) {
  3055. if (hx === undefined) hx = 0;
  3056. if (hy === undefined) hy = 0;
  3057. if (center === undefined) center = null;
  3058. if (angle === undefined) angle = 0.0;
  3059. this.m_vertexCount = 4;
  3060. this.Reserve(4);
  3061. this.m_vertices[0].Set((-hx), (-hy));
  3062. this.m_vertices[1].Set(hx, (-hy));
  3063. this.m_vertices[2].Set(hx, hy);
  3064. this.m_vertices[3].Set((-hx), hy);
  3065. this.m_normals[0].Set(0.0, (-1.0));
  3066. this.m_normals[1].Set(1.0, 0.0);
  3067. this.m_normals[2].Set(0.0, 1.0);
  3068. this.m_normals[3].Set((-1.0), 0.0);
  3069. this.m_centroid = center;
  3070. var xf = new b2Transform();
  3071. xf.position = center;
  3072. xf.R.Set(angle);
  3073. for (var i = 0; i < this.m_vertexCount; ++i) {
  3074. this.m_vertices[i] = b2Math.MulX(xf, this.m_vertices[i]);
  3075. this.m_normals[i] = b2Math.MulMV(xf.R, this.m_normals[i]);
  3076. }
  3077. }
  3078. b2PolygonShape.AsOrientedBox = function (hx, hy, center, angle) {
  3079. if (hx === undefined) hx = 0;
  3080. if (hy === undefined) hy = 0;
  3081. if (center === undefined) center = null;
  3082. if (angle === undefined) angle = 0.0;
  3083. var polygonShape = new b2PolygonShape();
  3084. polygonShape.SetAsOrientedBox(hx, hy, center, angle);
  3085. return polygonShape;
  3086. }
  3087. b2PolygonShape.prototype.SetAsEdge = function (v1, v2) {
  3088. this.m_vertexCount = 2;
  3089. this.Reserve(2);
  3090. this.m_vertices[0].SetV(v1);
  3091. this.m_vertices[1].SetV(v2);
  3092. this.m_centroid.x = 0.5 * (v1.x + v2.x);
  3093. this.m_centroid.y = 0.5 * (v1.y + v2.y);
  3094. this.m_normals[0] = b2Math.CrossVF(b2Math.SubtractVV(v2, v1), 1.0);
  3095. this.m_normals[0].Normalize();
  3096. this.m_normals[1].x = (-this.m_normals[0].x);
  3097. this.m_normals[1].y = (-this.m_normals[0].y);
  3098. }
  3099. b2PolygonShape.AsEdge = function (v1, v2) {
  3100. var polygonShape = new b2PolygonShape();
  3101. polygonShape.SetAsEdge(v1, v2);
  3102. return polygonShape;
  3103. }
  3104. b2PolygonShape.prototype.TestPoint = function (xf, p) {
  3105. var tVec;
  3106. var tMat = xf.R;
  3107. var tX = p.x - xf.position.x;
  3108. var tY = p.y - xf.position.y;
  3109. var pLocalX = (tX * tMat.col1.x + tY * tMat.col1.y);
  3110. var pLocalY = (tX * tMat.col2.x + tY * tMat.col2.y);
  3111. for (var i = 0; i < this.m_vertexCount; ++i) {
  3112. tVec = this.m_vertices[i];
  3113. tX = pLocalX - tVec.x;
  3114. tY = pLocalY - tVec.y;
  3115. tVec = this.m_normals[i];
  3116. var dot = (tVec.x * tX + tVec.y * tY);
  3117. if (dot > 0.0) {
  3118. return false;
  3119. }
  3120. }
  3121. return true;
  3122. }
  3123. b2PolygonShape.prototype.RayCast = function (output, input, transform) {
  3124. var lower = 0.0;
  3125. var upper = input.maxFraction;
  3126. var tX = 0;
  3127. var tY = 0;
  3128. var tMat;
  3129. var tVec;
  3130. tX = input.p1.x - transform.position.x;
  3131. tY = input.p1.y - transform.position.y;
  3132. tMat = transform.R;
  3133. var p1X = (tX * tMat.col1.x + tY * tMat.col1.y);
  3134. var p1Y = (tX * tMat.col2.x + tY * tMat.col2.y);
  3135. tX = input.p2.x - transform.position.x;
  3136. tY = input.p2.y - transform.position.y;
  3137. tMat = transform.R;
  3138. var p2X = (tX * tMat.col1.x + tY * tMat.col1.y);
  3139. var p2Y = (tX * tMat.col2.x + tY * tMat.col2.y);
  3140. var dX = p2X - p1X;
  3141. var dY = p2Y - p1Y;
  3142. var index = parseInt((-1));
  3143. for (var i = 0; i < this.m_vertexCount; ++i) {
  3144. tVec = this.m_vertices[i];
  3145. tX = tVec.x - p1X;
  3146. tY = tVec.y - p1Y;
  3147. tVec = this.m_normals[i];
  3148. var numerator = (tVec.x * tX + tVec.y * tY);
  3149. var denominator = (tVec.x * dX + tVec.y * dY);
  3150. if (denominator == 0.0) {
  3151. if (numerator < 0.0) {
  3152. return false;
  3153. }
  3154. }
  3155. else {
  3156. if (denominator < 0.0 && numerator < lower * denominator) {
  3157. lower = numerator / denominator;
  3158. index = i;
  3159. }
  3160. else if (denominator > 0.0 && numerator < upper * denominator) {
  3161. upper = numerator / denominator;
  3162. }
  3163. }
  3164. if (upper < lower - Number.MIN_VALUE) {
  3165. return false;
  3166. }
  3167. }
  3168. if (index >= 0) {
  3169. output.fraction = lower;
  3170. tMat = transform.R;
  3171. tVec = this.m_normals[index];
  3172. output.normal.x = (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
  3173. output.normal.y = (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
  3174. return true;
  3175. }
  3176. return false;
  3177. }
  3178. b2PolygonShape.prototype.ComputeAABB = function (aabb, xf) {
  3179. var tMat = xf.R;
  3180. var tVec = this.m_vertices[0];
  3181. var lowerX = xf.position.x + (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
  3182. var lowerY = xf.position.y + (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
  3183. var upperX = lowerX;
  3184. var upperY = lowerY;
  3185. for (var i = 1; i < this.m_vertexCount; ++i) {
  3186. tVec = this.m_vertices[i];
  3187. var vX = xf.position.x + (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
  3188. var vY = xf.position.y + (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
  3189. lowerX = lowerX < vX ? lowerX : vX;
  3190. lowerY = lowerY < vY ? lowerY : vY;
  3191. upperX = upperX > vX ? upperX : vX;
  3192. upperY = upperY > vY ? upperY : vY;
  3193. }
  3194. aabb.lowerBound.x = lowerX - this.m_radius;
  3195. aabb.lowerBound.y = lowerY - this.m_radius;
  3196. aabb.upperBound.x = upperX + this.m_radius;
  3197. aabb.upperBound.y = upperY + this.m_radius;
  3198. }
  3199. b2PolygonShape.prototype.ComputeMass = function (massData, density) {
  3200. if (density === undefined) density = 0;
  3201. if (this.m_vertexCount == 2) {
  3202. massData.center.x = 0.5 * (this.m_vertices[0].x + this.m_vertices[1].x);
  3203. massData.center.y = 0.5 * (this.m_vertices[0].y + this.m_vertices[1].y);
  3204. massData.mass = 0.0;
  3205. massData.I = 0.0;
  3206. return;
  3207. }
  3208. var centerX = 0.0;
  3209. var centerY = 0.0;
  3210. var area = 0.0;
  3211. var I = 0.0;
  3212. var p1X = 0.0;
  3213. var p1Y = 0.0;
  3214. var k_inv3 = 1.0 / 3.0;
  3215. for (var i = 0; i < this.m_vertexCount; ++i) {
  3216. var p2 = this.m_vertices[i];
  3217. var p3 = i + 1 < this.m_vertexCount ? this.m_vertices[parseInt(i + 1)] : this.m_vertices[0];
  3218. var e1X = p2.x - p1X;
  3219. var e1Y = p2.y - p1Y;
  3220. var e2X = p3.x - p1X;
  3221. var e2Y = p3.y - p1Y;
  3222. var D = e1X * e2Y - e1Y * e2X;
  3223. var triangleArea = 0.5 * D;area += triangleArea;
  3224. centerX += triangleArea * k_inv3 * (p1X + p2.x + p3.x);
  3225. centerY += triangleArea * k_inv3 * (p1Y + p2.y + p3.y);
  3226. var px = p1X;
  3227. var py = p1Y;
  3228. var ex1 = e1X;
  3229. var ey1 = e1Y;
  3230. var ex2 = e2X;
  3231. var ey2 = e2Y;
  3232. var intx2 = k_inv3 * (0.25 * (ex1 * ex1 + ex2 * ex1 + ex2 * ex2) + (px * ex1 + px * ex2)) + 0.5 * px * px;
  3233. var inty2 = k_inv3 * (0.25 * (ey1 * ey1 + ey2 * ey1 + ey2 * ey2) + (py * ey1 + py * ey2)) + 0.5 * py * py;I += D * (intx2 + inty2);
  3234. }
  3235. massData.mass = density * area;
  3236. centerX *= 1.0 / area;
  3237. centerY *= 1.0 / area;
  3238. massData.center.Set(centerX, centerY);
  3239. massData.I = density * I;
  3240. }
  3241. b2PolygonShape.prototype.ComputeSubmergedArea = function (normal, offset, xf, c) {
  3242. if (offset === undefined) offset = 0;
  3243. var normalL = b2Math.MulTMV(xf.R, normal);
  3244. var offsetL = offset - b2Math.Dot(normal, xf.position);
  3245. var depths = new Vector_a2j_Number();
  3246. var diveCount = 0;
  3247. var intoIndex = parseInt((-1));
  3248. var outoIndex = parseInt((-1));
  3249. var lastSubmerged = false;
  3250. var i = 0;
  3251. for (i = 0;
  3252. i < this.m_vertexCount; ++i) {
  3253. depths[i] = b2Math.Dot(normalL, this.m_vertices[i]) - offsetL;
  3254. var isSubmerged = depths[i] < (-Number.MIN_VALUE);
  3255. if (i > 0) {
  3256. if (isSubmerged) {
  3257. if (!lastSubmerged) {
  3258. intoIndex = i - 1;
  3259. diveCount++;
  3260. }
  3261. }
  3262. else {
  3263. if (lastSubmerged) {
  3264. outoIndex = i - 1;
  3265. diveCount++;
  3266. }
  3267. }
  3268. }
  3269. lastSubmerged = isSubmerged;
  3270. }
  3271. switch (diveCount) {
  3272. case 0:
  3273. if (lastSubmerged) {
  3274. var md = new b2MassData();
  3275. this.ComputeMass(md, 1);
  3276. c.SetV(b2Math.MulX(xf, md.center));
  3277. return md.mass;
  3278. }
  3279. else {
  3280. return 0;
  3281. }
  3282. break;
  3283. case 1:
  3284. if (intoIndex == (-1)) {
  3285. intoIndex = this.m_vertexCount - 1;
  3286. }
  3287. else {
  3288. outoIndex = this.m_vertexCount - 1;
  3289. }
  3290. break;
  3291. }
  3292. var intoIndex2 = parseInt((intoIndex + 1) % this.m_vertexCount);
  3293. var outoIndex2 = parseInt((outoIndex + 1) % this.m_vertexCount);
  3294. var intoLamdda = (0 - depths[intoIndex]) / (depths[intoIndex2] - depths[intoIndex]);
  3295. var outoLamdda = (0 - depths[outoIndex]) / (depths[outoIndex2] - depths[outoIndex]);
  3296. var intoVec = new b2Vec2(this.m_vertices[intoIndex].x * (1 - intoLamdda) + this.m_vertices[intoIndex2].x * intoLamdda, this.m_vertices[intoIndex].y * (1 - intoLamdda) + this.m_vertices[intoIndex2].y * intoLamdda);
  3297. var outoVec = new b2Vec2(this.m_vertices[outoIndex].x * (1 - outoLamdda) + this.m_vertices[outoIndex2].x * outoLamdda, this.m_vertices[outoIndex].y * (1 - outoLamdda) + this.m_vertices[outoIndex2].y * outoLamdda);
  3298. var area = 0;
  3299. var center = new b2Vec2();
  3300. var p2 = this.m_vertices[intoIndex2];
  3301. var p3;
  3302. i = intoIndex2;
  3303. while (i != outoIndex2) {
  3304. i = (i + 1) % this.m_vertexCount;
  3305. if (i == outoIndex2) p3 = outoVec;
  3306. else p3 = this.m_vertices[i];
  3307. var triangleArea = 0.5 * ((p2.x - intoVec.x) * (p3.y - intoVec.y) - (p2.y - intoVec.y) * (p3.x - intoVec.x));
  3308. area += triangleArea;
  3309. center.x += triangleArea * (intoVec.x + p2.x + p3.x) / 3;
  3310. center.y += triangleArea * (intoVec.y + p2.y + p3.y) / 3;
  3311. p2 = p3;
  3312. }
  3313. center.Multiply(1 / area);
  3314. c.SetV(b2Math.MulX(xf, center));
  3315. return area;
  3316. }
  3317. b2PolygonShape.prototype.GetVertexCount = function () {
  3318. return this.m_vertexCount;
  3319. }
  3320. b2PolygonShape.prototype.GetVertices = function () {
  3321. return this.m_vertices;
  3322. }
  3323. b2PolygonShape.prototype.GetNormals = function () {
  3324. return this.m_normals;
  3325. }
  3326. b2PolygonShape.prototype.GetSupport = function (d) {
  3327. var bestIndex = 0;
  3328. var bestValue = this.m_vertices[0].x * d.x + this.m_vertices[0].y * d.y;
  3329. for (var i = 1; i < this.m_vertexCount; ++i) {
  3330. var value = this.m_vertices[i].x * d.x + this.m_vertices[i].y * d.y;
  3331. if (value > bestValue) {
  3332. bestIndex = i;
  3333. bestValue = value;
  3334. }
  3335. }
  3336. return bestIndex;
  3337. }
  3338. b2PolygonShape.prototype.GetSupportVertex = function (d) {
  3339. var bestIndex = 0;
  3340. var bestValue = this.m_vertices[0].x * d.x + this.m_vertices[0].y * d.y;
  3341. for (var i = 1; i < this.m_vertexCount; ++i) {
  3342. var value = this.m_vertices[i].x * d.x + this.m_vertices[i].y * d.y;
  3343. if (value > bestValue) {
  3344. bestIndex = i;
  3345. bestValue = value;
  3346. }
  3347. }
  3348. return this.m_vertices[bestIndex];
  3349. }
  3350. b2PolygonShape.prototype.Validate = function () {
  3351. return false;
  3352. }
  3353. b2PolygonShape.prototype.b2PolygonShape = function () {
  3354. this.__super.b2Shape.call(this);
  3355. this.m_type = b2Shape.e_polygonShape;
  3356. this.m_centroid = new b2Vec2();
  3357. this.m_vertices = new Vector();
  3358. this.m_normals = new Vector();
  3359. }
  3360. b2PolygonShape.prototype.Reserve = function (count) {
  3361. if (count === undefined) count = 0;
  3362. for (var i = parseInt(this.m_vertices.length); i < count; i++) {
  3363. this.m_vertices[i] = new b2Vec2();
  3364. this.m_normals[i] = new b2Vec2();
  3365. }
  3366. }
  3367. b2PolygonShape.ComputeCentroid = function (vs, count) {
  3368. if (count === undefined) count = 0;
  3369. var c = new b2Vec2();
  3370. var area = 0.0;
  3371. var p1X = 0.0;
  3372. var p1Y = 0.0;
  3373. var inv3 = 1.0 / 3.0;
  3374. for (var i = 0; i < count; ++i) {
  3375. var p2 = vs[i];
  3376. var p3 = i + 1 < count ? vs[parseInt(i + 1)] : vs[0];
  3377. var e1X = p2.x - p1X;
  3378. var e1Y = p2.y - p1Y;
  3379. var e2X = p3.x - p1X;
  3380. var e2Y = p3.y - p1Y;
  3381. var D = (e1X * e2Y - e1Y * e2X);
  3382. var triangleArea = 0.5 * D;area += triangleArea;
  3383. c.x += triangleArea * inv3 * (p1X + p2.x + p3.x);
  3384. c.y += triangleArea * inv3 * (p1Y + p2.y + p3.y);
  3385. }
  3386. c.x *= 1.0 / area;
  3387. c.y *= 1.0 / area;
  3388. return c;
  3389. }
  3390. b2PolygonShape.ComputeOBB = function (obb, vs, count) {
  3391. if (count === undefined) count = 0;
  3392. var i = 0;
  3393. var p = new Vector(count + 1);
  3394. for (i = 0;
  3395. i < count; ++i) {
  3396. p[i] = vs[i];
  3397. }
  3398. p[count] = p[0];
  3399. var minArea = Number.MAX_VALUE;
  3400. for (i = 1;
  3401. i <= count; ++i) {
  3402. var root = p[parseInt(i - 1)];
  3403. var uxX = p[i].x - root.x;
  3404. var uxY = p[i].y - root.y;
  3405. var length = Math.sqrt(uxX * uxX + uxY * uxY);
  3406. uxX /= length;
  3407. uxY /= length;
  3408. var uyX = (-uxY);
  3409. var uyY = uxX;
  3410. var lowerX = Number.MAX_VALUE;
  3411. var lowerY = Number.MAX_VALUE;
  3412. var upperX = (-Number.MAX_VALUE);
  3413. var upperY = (-Number.MAX_VALUE);
  3414. for (var j = 0; j < count; ++j) {
  3415. var dX = p[j].x - root.x;
  3416. var dY = p[j].y - root.y;
  3417. var rX = (uxX * dX + uxY * dY);
  3418. var rY = (uyX * dX + uyY * dY);
  3419. if (rX < lowerX) lowerX = rX;
  3420. if (rY < lowerY) lowerY = rY;
  3421. if (rX > upperX) upperX = rX;
  3422. if (rY > upperY) upperY = rY;
  3423. }
  3424. var area = (upperX - lowerX) * (upperY - lowerY);
  3425. if (area < 0.95 * minArea) {
  3426. minArea = area;
  3427. obb.R.col1.x = uxX;
  3428. obb.R.col1.y = uxY;
  3429. obb.R.col2.x = uyX;
  3430. obb.R.col2.y = uyY;
  3431. var centerX = 0.5 * (lowerX + upperX);
  3432. var centerY = 0.5 * (lowerY + upperY);
  3433. var tMat = obb.R;
  3434. obb.center.x = root.x + (tMat.col1.x * centerX + tMat.col2.x * centerY);
  3435. obb.center.y = root.y + (tMat.col1.y * centerX + tMat.col2.y * centerY);
  3436. obb.extents.x = 0.5 * (upperX - lowerX);
  3437. obb.extents.y = 0.5 * (upperY - lowerY);
  3438. }
  3439. }
  3440. }
  3441. Box2D.postDefs.push(function () {
  3442. Box2D.Collision.Shapes.b2PolygonShape.s_mat = new b2Mat22();
  3443. });
  3444. b2Shape.b2Shape = function () {};
  3445. b2Shape.prototype.Copy = function () {
  3446. return null;
  3447. }
  3448. b2Shape.prototype.Set = function (other) {
  3449. this.m_radius = other.m_radius;
  3450. }
  3451. b2Shape.prototype.GetType = function () {
  3452. return this.m_type;
  3453. }
  3454. b2Shape.prototype.TestPoint = function (xf, p) {
  3455. return false;
  3456. }
  3457. b2Shape.prototype.RayCast = function (output, input, transform) {
  3458. return false;
  3459. }
  3460. b2Shape.prototype.ComputeAABB = function (aabb, xf) {}
  3461. b2Shape.prototype.ComputeMass = function (massData, density) {
  3462. if (density === undefined) density = 0;
  3463. }
  3464. b2Shape.prototype.ComputeSubmergedArea = function (normal, offset, xf, c) {
  3465. if (offset === undefined) offset = 0;
  3466. return 0;
  3467. }
  3468. b2Shape.TestOverlap = function (shape1, transform1, shape2, transform2) {
  3469. var input = new b2DistanceInput();
  3470. input.proxyA = new b2DistanceProxy();
  3471. input.proxyA.Set(shape1);
  3472. input.proxyB = new b2DistanceProxy();
  3473. input.proxyB.Set(shape2);
  3474. input.transformA = transform1;
  3475. input.transformB = transform2;
  3476. input.useRadii = true;
  3477. var simplexCache = new b2SimplexCache();
  3478. simplexCache.count = 0;
  3479. var output = new b2DistanceOutput();
  3480. b2Distance.Distance(output, simplexCache, input);
  3481. return output.distance < 10.0 * Number.MIN_VALUE;
  3482. }
  3483. b2Shape.prototype.b2Shape = function () {
  3484. this.m_type = b2Shape.e_unknownShape;
  3485. this.m_radius = b2Settings.b2_linearSlop;
  3486. }
  3487. Box2D.postDefs.push(function () {
  3488. Box2D.Collision.Shapes.b2Shape.e_unknownShape = parseInt((-1));
  3489. Box2D.Collision.Shapes.b2Shape.e_circleShape = 0;
  3490. Box2D.Collision.Shapes.b2Shape.e_polygonShape = 1;
  3491. Box2D.Collision.Shapes.b2Shape.e_edgeShape = 2;
  3492. Box2D.Collision.Shapes.b2Shape.e_shapeTypeCount = 3;
  3493. Box2D.Collision.Shapes.b2Shape.e_hitCollide = 1;
  3494. Box2D.Collision.Shapes.b2Shape.e_missCollide = 0;
  3495. Box2D.Collision.Shapes.b2Shape.e_startsInsideCollide = parseInt((-1));
  3496. });
  3497. })();
  3498. (function () {
  3499. var b2Color = Box2D.Common.b2Color,
  3500. b2internal = Box2D.Common.b2internal,
  3501. b2Settings = Box2D.Common.b2Settings,
  3502. b2Mat22 = Box2D.Common.Math.b2Mat22,
  3503. b2Mat33 = Box2D.Common.Math.b2Mat33,
  3504. b2Math = Box2D.Common.Math.b2Math,
  3505. b2Sweep = Box2D.Common.Math.b2Sweep,
  3506. b2Transform = Box2D.Common.Math.b2Transform,
  3507. b2Vec2 = Box2D.Common.Math.b2Vec2,
  3508. b2Vec3 = Box2D.Common.Math.b2Vec3;
  3509. b2Color.b2Color = function () {
  3510. this._r = 0;
  3511. this._g = 0;
  3512. this._b = 0;
  3513. };
  3514. b2Color.prototype.b2Color = function (rr, gg, bb) {
  3515. if (rr === undefined) rr = 0;
  3516. if (gg === undefined) gg = 0;
  3517. if (bb === undefined) bb = 0;
  3518. this._r = Box2D.parseUInt(255 * b2Math.Clamp(rr, 0.0, 1.0));
  3519. this._g = Box2D.parseUInt(255 * b2Math.Clamp(gg, 0.0, 1.0));
  3520. this._b = Box2D.parseUInt(255 * b2Math.Clamp(bb, 0.0, 1.0));
  3521. }
  3522. b2Color.prototype.Set = function (rr, gg, bb) {
  3523. if (rr === undefined) rr = 0;
  3524. if (gg === undefined) gg = 0;
  3525. if (bb === undefined) bb = 0;
  3526. this._r = Box2D.parseUInt(255 * b2Math.Clamp(rr, 0.0, 1.0));
  3527. this._g = Box2D.parseUInt(255 * b2Math.Clamp(gg, 0.0, 1.0));
  3528. this._b = Box2D.parseUInt(255 * b2Math.Clamp(bb, 0.0, 1.0));
  3529. }
  3530. Object.defineProperty(b2Color.prototype, 'r', {
  3531. enumerable: false,
  3532. configurable: true,
  3533. set: function (rr) {
  3534. if (rr === undefined) rr = 0;
  3535. this._r = Box2D.parseUInt(255 * b2Math.Clamp(rr, 0.0, 1.0));
  3536. }
  3537. });
  3538. Object.defineProperty(b2Color.prototype, 'g', {
  3539. enumerable: false,
  3540. configurable: true,
  3541. set: function (gg) {
  3542. if (gg === undefined) gg = 0;
  3543. this._g = Box2D.parseUInt(255 * b2Math.Clamp(gg, 0.0, 1.0));
  3544. }
  3545. });
  3546. Object.defineProperty(b2Color.prototype, 'b', {
  3547. enumerable: false,
  3548. configurable: true,
  3549. set: function (bb) {
  3550. if (bb === undefined) bb = 0;
  3551. this._b = Box2D.parseUInt(255 * b2Math.Clamp(bb, 0.0, 1.0));
  3552. }
  3553. });
  3554. Object.defineProperty(b2Color.prototype, 'color', {
  3555. enumerable: false,
  3556. configurable: true,
  3557. get: function () {
  3558. return (this._r << 16) | (this._g << 8) | (this._b);
  3559. }
  3560. });
  3561. b2Settings.b2Settings = function () {};
  3562. b2Settings.b2MixFriction = function (friction1, friction2) {
  3563. if (friction1 === undefined) friction1 = 0;
  3564. if (friction2 === undefined) friction2 = 0;
  3565. return Math.sqrt(friction1 * friction2);
  3566. }
  3567. b2Settings.b2MixRestitution = function (restitution1, restitution2) {
  3568. if (restitution1 === undefined) restitution1 = 0;
  3569. if (restitution2 === undefined) restitution2 = 0;
  3570. return restitution1 > restitution2 ? restitution1 : restitution2;
  3571. }
  3572. b2Settings.b2Assert = function (a) {
  3573. if (!a) {
  3574. throw "Assertion Failed";
  3575. }
  3576. }
  3577. Box2D.postDefs.push(function () {
  3578. Box2D.Common.b2Settings.VERSION = "2.1alpha";
  3579. Box2D.Common.b2Settings.USHRT_MAX = 0x0000ffff;
  3580. Box2D.Common.b2Settings.b2_pi = Math.PI;
  3581. Box2D.Common.b2Settings.b2_maxManifoldPoints = 2;
  3582. Box2D.Common.b2Settings.b2_aabbExtension = 0.1;
  3583. Box2D.Common.b2Settings.b2_aabbMultiplier = 2.0;
  3584. Box2D.Common.b2Settings.b2_polygonRadius = 2.0 * b2Settings.b2_linearSlop;
  3585. Box2D.Common.b2Settings.b2_linearSlop = 0.005;
  3586. Box2D.Common.b2Settings.b2_angularSlop = 2.0 / 180.0 * b2Settings.b2_pi;
  3587. Box2D.Common.b2Settings.b2_toiSlop = 8.0 * b2Settings.b2_linearSlop;
  3588. Box2D.Common.b2Settings.b2_maxTOIContactsPerIsland = 32;
  3589. Box2D.Common.b2Settings.b2_maxTOIJointsPerIsland = 32;
  3590. Box2D.Common.b2Settings.b2_velocityThreshold = 1.0;
  3591. Box2D.Common.b2Settings.b2_maxLinearCorrection = 0.2;
  3592. Box2D.Common.b2Settings.b2_maxAngularCorrection = 8.0 / 180.0 * b2Settings.b2_pi;
  3593. Box2D.Common.b2Settings.b2_maxTranslation = 2.0;
  3594. Box2D.Common.b2Settings.b2_maxTranslationSquared = b2Settings.b2_maxTranslation * b2Settings.b2_maxTranslation;
  3595. Box2D.Common.b2Settings.b2_maxRotation = 0.5 * b2Settings.b2_pi;
  3596. Box2D.Common.b2Settings.b2_maxRotationSquared = b2Settings.b2_maxRotation * b2Settings.b2_maxRotation;
  3597. Box2D.Common.b2Settings.b2_contactBaumgarte = 0.2;
  3598. Box2D.Common.b2Settings.b2_timeToSleep = 0.5;
  3599. Box2D.Common.b2Settings.b2_linearSleepTolerance = 0.01;
  3600. Box2D.Common.b2Settings.b2_angularSleepTolerance = 2.0 / 180.0 * b2Settings.b2_pi;
  3601. });
  3602. })();
  3603. (function () {
  3604. var b2AABB = Box2D.Collision.b2AABB,
  3605. b2Color = Box2D.Common.b2Color,
  3606. b2internal = Box2D.Common.b2internal,
  3607. b2Settings = Box2D.Common.b2Settings,
  3608. b2Mat22 = Box2D.Common.Math.b2Mat22,
  3609. b2Mat33 = Box2D.Common.Math.b2Mat33,
  3610. b2Math = Box2D.Common.Math.b2Math,
  3611. b2Sweep = Box2D.Common.Math.b2Sweep,
  3612. b2Transform = Box2D.Common.Math.b2Transform,
  3613. b2Vec2 = Box2D.Common.Math.b2Vec2,
  3614. b2Vec3 = Box2D.Common.Math.b2Vec3;
  3615. b2Mat22.b2Mat22 = function () {
  3616. this.col1 = new b2Vec2();
  3617. this.col2 = new b2Vec2();
  3618. };
  3619. b2Mat22.prototype.b2Mat22 = function () {
  3620. this.SetIdentity();
  3621. }
  3622. b2Mat22.FromAngle = function (angle) {
  3623. if (angle === undefined) angle = 0;
  3624. var mat = new b2Mat22();
  3625. mat.Set(angle);
  3626. return mat;
  3627. }
  3628. b2Mat22.FromVV = function (c1, c2) {
  3629. var mat = new b2Mat22();
  3630. mat.SetVV(c1, c2);
  3631. return mat;
  3632. }
  3633. b2Mat22.prototype.Set = function (angle) {
  3634. if (angle === undefined) angle = 0;
  3635. var c = Math.cos(angle);
  3636. var s = Math.sin(angle);
  3637. this.col1.x = c;
  3638. this.col2.x = (-s);
  3639. this.col1.y = s;
  3640. this.col2.y = c;
  3641. }
  3642. b2Mat22.prototype.SetVV = function (c1, c2) {
  3643. this.col1.SetV(c1);
  3644. this.col2.SetV(c2);
  3645. }
  3646. b2Mat22.prototype.Copy = function () {
  3647. var mat = new b2Mat22();
  3648. mat.SetM(this);
  3649. return mat;
  3650. }
  3651. b2Mat22.prototype.SetM = function (m) {
  3652. this.col1.SetV(m.col1);
  3653. this.col2.SetV(m.col2);
  3654. }
  3655. b2Mat22.prototype.AddM = function (m) {
  3656. this.col1.x += m.col1.x;
  3657. this.col1.y += m.col1.y;
  3658. this.col2.x += m.col2.x;
  3659. this.col2.y += m.col2.y;
  3660. }
  3661. b2Mat22.prototype.SetIdentity = function () {
  3662. this.col1.x = 1.0;
  3663. this.col2.x = 0.0;
  3664. this.col1.y = 0.0;
  3665. this.col2.y = 1.0;
  3666. }
  3667. b2Mat22.prototype.SetZero = function () {
  3668. this.col1.x = 0.0;
  3669. this.col2.x = 0.0;
  3670. this.col1.y = 0.0;
  3671. this.col2.y = 0.0;
  3672. }
  3673. b2Mat22.prototype.GetAngle = function () {
  3674. return Math.atan2(this.col1.y, this.col1.x);
  3675. }
  3676. b2Mat22.prototype.GetInverse = function (out) {
  3677. var a = this.col1.x;
  3678. var b = this.col2.x;
  3679. var c = this.col1.y;
  3680. var d = this.col2.y;
  3681. var det = a * d - b * c;
  3682. if (det != 0.0) {
  3683. det = 1.0 / det;
  3684. }
  3685. out.col1.x = det * d;
  3686. out.col2.x = (-det * b);
  3687. out.col1.y = (-det * c);
  3688. out.col2.y = det * a;
  3689. return out;
  3690. }
  3691. b2Mat22.prototype.Solve = function (out, bX, bY) {
  3692. if (bX === undefined) bX = 0;
  3693. if (bY === undefined) bY = 0;
  3694. var a11 = this.col1.x;
  3695. var a12 = this.col2.x;
  3696. var a21 = this.col1.y;
  3697. var a22 = this.col2.y;
  3698. var det = a11 * a22 - a12 * a21;
  3699. if (det != 0.0) {
  3700. det = 1.0 / det;
  3701. }
  3702. out.x = det * (a22 * bX - a12 * bY);
  3703. out.y = det * (a11 * bY - a21 * bX);
  3704. return out;
  3705. }
  3706. b2Mat22.prototype.Abs = function () {
  3707. this.col1.Abs();
  3708. this.col2.Abs();
  3709. }
  3710. b2Mat33.b2Mat33 = function () {
  3711. this.col1 = new b2Vec3();
  3712. this.col2 = new b2Vec3();
  3713. this.col3 = new b2Vec3();
  3714. };
  3715. b2Mat33.prototype.b2Mat33 = function (c1, c2, c3) {
  3716. if (c1 === undefined) c1 = null;
  3717. if (c2 === undefined) c2 = null;
  3718. if (c3 === undefined) c3 = null;
  3719. if (!c1 && !c2 && !c3) {
  3720. this.col1.SetZero();
  3721. this.col2.SetZero();
  3722. this.col3.SetZero();
  3723. }
  3724. else {
  3725. this.col1.SetV(c1);
  3726. this.col2.SetV(c2);
  3727. this.col3.SetV(c3);
  3728. }
  3729. }
  3730. b2Mat33.prototype.SetVVV = function (c1, c2, c3) {
  3731. this.col1.SetV(c1);
  3732. this.col2.SetV(c2);
  3733. this.col3.SetV(c3);
  3734. }
  3735. b2Mat33.prototype.Copy = function () {
  3736. return new b2Mat33(this.col1, this.col2, this.col3);
  3737. }
  3738. b2Mat33.prototype.SetM = function (m) {
  3739. this.col1.SetV(m.col1);
  3740. this.col2.SetV(m.col2);
  3741. this.col3.SetV(m.col3);
  3742. }
  3743. b2Mat33.prototype.AddM = function (m) {
  3744. this.col1.x += m.col1.x;
  3745. this.col1.y += m.col1.y;
  3746. this.col1.z += m.col1.z;
  3747. this.col2.x += m.col2.x;
  3748. this.col2.y += m.col2.y;
  3749. this.col2.z += m.col2.z;
  3750. this.col3.x += m.col3.x;
  3751. this.col3.y += m.col3.y;
  3752. this.col3.z += m.col3.z;
  3753. }
  3754. b2Mat33.prototype.SetIdentity = function () {
  3755. this.col1.x = 1.0;
  3756. this.col2.x = 0.0;
  3757. this.col3.x = 0.0;
  3758. this.col1.y = 0.0;
  3759. this.col2.y = 1.0;
  3760. this.col3.y = 0.0;
  3761. this.col1.z = 0.0;
  3762. this.col2.z = 0.0;
  3763. this.col3.z = 1.0;
  3764. }
  3765. b2Mat33.prototype.SetZero = function () {
  3766. this.col1.x = 0.0;
  3767. this.col2.x = 0.0;
  3768. this.col3.x = 0.0;
  3769. this.col1.y = 0.0;
  3770. this.col2.y = 0.0;
  3771. this.col3.y = 0.0;
  3772. this.col1.z = 0.0;
  3773. this.col2.z = 0.0;
  3774. this.col3.z = 0.0;
  3775. }
  3776. b2Mat33.prototype.Solve22 = function (out, bX, bY) {
  3777. if (bX === undefined) bX = 0;
  3778. if (bY === undefined) bY = 0;
  3779. var a11 = this.col1.x;
  3780. var a12 = this.col2.x;
  3781. var a21 = this.col1.y;
  3782. var a22 = this.col2.y;
  3783. var det = a11 * a22 - a12 * a21;
  3784. if (det != 0.0) {
  3785. det = 1.0 / det;
  3786. }
  3787. out.x = det * (a22 * bX - a12 * bY);
  3788. out.y = det * (a11 * bY - a21 * bX);
  3789. return out;
  3790. }
  3791. b2Mat33.prototype.Solve33 = function (out, bX, bY, bZ) {
  3792. if (bX === undefined) bX = 0;
  3793. if (bY === undefined) bY = 0;
  3794. if (bZ === undefined) bZ = 0;
  3795. var a11 = this.col1.x;
  3796. var a21 = this.col1.y;
  3797. var a31 = this.col1.z;
  3798. var a12 = this.col2.x;
  3799. var a22 = this.col2.y;
  3800. var a32 = this.col2.z;
  3801. var a13 = this.col3.x;
  3802. var a23 = this.col3.y;
  3803. var a33 = this.col3.z;
  3804. var det = a11 * (a22 * a33 - a32 * a23) + a21 * (a32 * a13 - a12 * a33) + a31 * (a12 * a23 - a22 * a13);
  3805. if (det != 0.0) {
  3806. det = 1.0 / det;
  3807. }
  3808. out.x = det * (bX * (a22 * a33 - a32 * a23) + bY * (a32 * a13 - a12 * a33) + bZ * (a12 * a23 - a22 * a13));
  3809. out.y = det * (a11 * (bY * a33 - bZ * a23) + a21 * (bZ * a13 - bX * a33) + a31 * (bX * a23 - bY * a13));
  3810. out.z = det * (a11 * (a22 * bZ - a32 * bY) + a21 * (a32 * bX - a12 * bZ) + a31 * (a12 * bY - a22 * bX));
  3811. return out;
  3812. }
  3813. b2Math.b2Math = function () {};
  3814. b2Math.IsValid = function (x) {
  3815. if (x === undefined) x = 0;
  3816. return isFinite(x);
  3817. }
  3818. b2Math.Dot = function (a, b) {
  3819. return a.x * b.x + a.y * b.y;
  3820. }
  3821. b2Math.CrossVV = function (a, b) {
  3822. return a.x * b.y - a.y * b.x;
  3823. }
  3824. b2Math.CrossVF = function (a, s) {
  3825. if (s === undefined) s = 0;
  3826. var v = new b2Vec2(s * a.y, (-s * a.x));
  3827. return v;
  3828. }
  3829. b2Math.CrossFV = function (s, a) {
  3830. if (s === undefined) s = 0;
  3831. var v = new b2Vec2((-s * a.y), s * a.x);
  3832. return v;
  3833. }
  3834. b2Math.MulMV = function (A, v) {
  3835. var u = new b2Vec2(A.col1.x * v.x + A.col2.x * v.y, A.col1.y * v.x + A.col2.y * v.y);
  3836. return u;
  3837. }
  3838. b2Math.MulTMV = function (A, v) {
  3839. var u = new b2Vec2(b2Math.Dot(v, A.col1), b2Math.Dot(v, A.col2));
  3840. return u;
  3841. }
  3842. b2Math.MulX = function (T, v) {
  3843. var a = b2Math.MulMV(T.R, v);
  3844. a.x += T.position.x;
  3845. a.y += T.position.y;
  3846. return a;
  3847. }
  3848. b2Math.MulXT = function (T, v) {
  3849. var a = b2Math.SubtractVV(v, T.position);
  3850. var tX = (a.x * T.R.col1.x + a.y * T.R.col1.y);
  3851. a.y = (a.x * T.R.col2.x + a.y * T.R.col2.y);
  3852. a.x = tX;
  3853. return a;
  3854. }
  3855. b2Math.AddVV = function (a, b) {
  3856. var v = new b2Vec2(a.x + b.x, a.y + b.y);
  3857. return v;
  3858. }
  3859. b2Math.SubtractVV = function (a, b) {
  3860. var v = new b2Vec2(a.x - b.x, a.y - b.y);
  3861. return v;
  3862. }
  3863. b2Math.Distance = function (a, b) {
  3864. var cX = a.x - b.x;
  3865. var cY = a.y - b.y;
  3866. return Math.sqrt(cX * cX + cY * cY);
  3867. }
  3868. b2Math.DistanceSquared = function (a, b) {
  3869. var cX = a.x - b.x;
  3870. var cY = a.y - b.y;
  3871. return (cX * cX + cY * cY);
  3872. }
  3873. b2Math.MulFV = function (s, a) {
  3874. if (s === undefined) s = 0;
  3875. var v = new b2Vec2(s * a.x, s * a.y);
  3876. return v;
  3877. }
  3878. b2Math.AddMM = function (A, B) {
  3879. var C = b2Mat22.FromVV(b2Math.AddVV(A.col1, B.col1), b2Math.AddVV(A.col2, B.col2));
  3880. return C;
  3881. }
  3882. b2Math.MulMM = function (A, B) {
  3883. var C = b2Mat22.FromVV(b2Math.MulMV(A, B.col1), b2Math.MulMV(A, B.col2));
  3884. return C;
  3885. }
  3886. b2Math.MulTMM = function (A, B) {
  3887. var c1 = new b2Vec2(b2Math.Dot(A.col1, B.col1), b2Math.Dot(A.col2, B.col1));
  3888. var c2 = new b2Vec2(b2Math.Dot(A.col1, B.col2), b2Math.Dot(A.col2, B.col2));
  3889. var C = b2Mat22.FromVV(c1, c2);
  3890. return C;
  3891. }
  3892. b2Math.Abs = function (a) {
  3893. if (a === undefined) a = 0;
  3894. return a > 0.0 ? a : (-a);
  3895. }
  3896. b2Math.AbsV = function (a) {
  3897. var b = new b2Vec2(b2Math.Abs(a.x), b2Math.Abs(a.y));
  3898. return b;
  3899. }
  3900. b2Math.AbsM = function (A) {
  3901. var B = b2Mat22.FromVV(b2Math.AbsV(A.col1), b2Math.AbsV(A.col2));
  3902. return B;
  3903. }
  3904. b2Math.Min = function (a, b) {
  3905. if (a === undefined) a = 0;
  3906. if (b === undefined) b = 0;
  3907. return a < b ? a : b;
  3908. }
  3909. b2Math.MinV = function (a, b) {
  3910. var c = new b2Vec2(b2Math.Min(a.x, b.x), b2Math.Min(a.y, b.y));
  3911. return c;
  3912. }
  3913. b2Math.Max = function (a, b) {
  3914. if (a === undefined) a = 0;
  3915. if (b === undefined) b = 0;
  3916. return a > b ? a : b;
  3917. }
  3918. b2Math.MaxV = function (a, b) {
  3919. var c = new b2Vec2(b2Math.Max(a.x, b.x), b2Math.Max(a.y, b.y));
  3920. return c;
  3921. }
  3922. b2Math.Clamp = function (a, low, high) {
  3923. if (a === undefined) a = 0;
  3924. if (low === undefined) low = 0;
  3925. if (high === undefined) high = 0;
  3926. return a < low ? low : a > high ? high : a;
  3927. }
  3928. b2Math.ClampV = function (a, low, high) {
  3929. return b2Math.MaxV(low, b2Math.MinV(a, high));
  3930. }
  3931. b2Math.Swap = function (a, b) {
  3932. var tmp = a[0];
  3933. a[0] = b[0];
  3934. b[0] = tmp;
  3935. }
  3936. b2Math.Random = function () {
  3937. return Math.random() * 2 - 1;
  3938. }
  3939. b2Math.RandomRange = function (lo, hi) {
  3940. if (lo === undefined) lo = 0;
  3941. if (hi === undefined) hi = 0;
  3942. var r = Math.random();
  3943. r = (hi - lo) * r + lo;
  3944. return r;
  3945. }
  3946. b2Math.NextPowerOfTwo = function (x) {
  3947. if (x === undefined) x = 0;
  3948. x |= (x >> 1) & 0x7FFFFFFF;
  3949. x |= (x >> 2) & 0x3FFFFFFF;
  3950. x |= (x >> 4) & 0x0FFFFFFF;
  3951. x |= (x >> 8) & 0x00FFFFFF;
  3952. x |= (x >> 16) & 0x0000FFFF;
  3953. return x + 1;
  3954. }
  3955. b2Math.IsPowerOfTwo = function (x) {
  3956. if (x === undefined) x = 0;
  3957. var result = x > 0 && (x & (x - 1)) == 0;
  3958. return result;
  3959. }
  3960. Box2D.postDefs.push(function () {
  3961. Box2D.Common.Math.b2Math.b2Vec2_zero = new b2Vec2(0.0, 0.0);
  3962. Box2D.Common.Math.b2Math.b2Mat22_identity = b2Mat22.FromVV(new b2Vec2(1.0, 0.0), new b2Vec2(0.0, 1.0));
  3963. Box2D.Common.Math.b2Math.b2Transform_identity = new b2Transform(b2Math.b2Vec2_zero, b2Math.b2Mat22_identity);
  3964. });
  3965. b2Sweep.b2Sweep = function () {
  3966. this.localCenter = new b2Vec2();
  3967. this.c0 = new b2Vec2;
  3968. this.c = new b2Vec2();
  3969. };
  3970. b2Sweep.prototype.Set = function (other) {
  3971. this.localCenter.SetV(other.localCenter);
  3972. this.c0.SetV(other.c0);
  3973. this.c.SetV(other.c);
  3974. this.a0 = other.a0;
  3975. this.a = other.a;
  3976. this.t0 = other.t0;
  3977. }
  3978. b2Sweep.prototype.Copy = function () {
  3979. var copy = new b2Sweep();
  3980. copy.localCenter.SetV(this.localCenter);
  3981. copy.c0.SetV(this.c0);
  3982. copy.c.SetV(this.c);
  3983. copy.a0 = this.a0;
  3984. copy.a = this.a;
  3985. copy.t0 = this.t0;
  3986. return copy;
  3987. }
  3988. b2Sweep.prototype.GetTransform = function (xf, alpha) {
  3989. if (alpha === undefined) alpha = 0;
  3990. xf.position.x = (1.0 - alpha) * this.c0.x + alpha * this.c.x;
  3991. xf.position.y = (1.0 - alpha) * this.c0.y + alpha * this.c.y;
  3992. var angle = (1.0 - alpha) * this.a0 + alpha * this.a;
  3993. xf.R.Set(angle);
  3994. var tMat = xf.R;
  3995. xf.position.x -= (tMat.col1.x * this.localCenter.x + tMat.col2.x * this.localCenter.y);
  3996. xf.position.y -= (tMat.col1.y * this.localCenter.x + tMat.col2.y * this.localCenter.y);
  3997. }
  3998. b2Sweep.prototype.Advance = function (t) {
  3999. if (t === undefined) t = 0;
  4000. if (this.t0 < t && 1.0 - this.t0 > Number.MIN_VALUE) {
  4001. var alpha = (t - this.t0) / (1.0 - this.t0);
  4002. this.c0.x = (1.0 - alpha) * this.c0.x + alpha * this.c.x;
  4003. this.c0.y = (1.0 - alpha) * this.c0.y + alpha * this.c.y;
  4004. this.a0 = (1.0 - alpha) * this.a0 + alpha * this.a;
  4005. this.t0 = t;
  4006. }
  4007. }
  4008. b2Transform.b2Transform = function () {
  4009. this.position = new b2Vec2;
  4010. this.R = new b2Mat22();
  4011. };
  4012. b2Transform.prototype.b2Transform = function (pos, r) {
  4013. if (pos === undefined) pos = null;
  4014. if (r === undefined) r = null;
  4015. if (pos) {
  4016. this.position.SetV(pos);
  4017. this.R.SetM(r);
  4018. }
  4019. }
  4020. b2Transform.prototype.Initialize = function (pos, r) {
  4021. this.position.SetV(pos);
  4022. this.R.SetM(r);
  4023. }
  4024. b2Transform.prototype.SetIdentity = function () {
  4025. this.position.SetZero();
  4026. this.R.SetIdentity();
  4027. }
  4028. b2Transform.prototype.Set = function (x) {
  4029. this.position.SetV(x.position);
  4030. this.R.SetM(x.R);
  4031. }
  4032. b2Transform.prototype.GetAngle = function () {
  4033. return Math.atan2(this.R.col1.y, this.R.col1.x);
  4034. }
  4035. b2Vec2.b2Vec2 = function () {};
  4036. b2Vec2.prototype.b2Vec2 = function (x_, y_) {
  4037. if (x_ === undefined) x_ = 0;
  4038. if (y_ === undefined) y_ = 0;
  4039. this.x = x_;
  4040. this.y = y_;
  4041. }
  4042. b2Vec2.prototype.SetZero = function () {
  4043. this.x = 0.0;
  4044. this.y = 0.0;
  4045. }
  4046. b2Vec2.prototype.Set = function (x_, y_) {
  4047. if (x_ === undefined) x_ = 0;
  4048. if (y_ === undefined) y_ = 0;
  4049. this.x = x_;
  4050. this.y = y_;
  4051. }
  4052. b2Vec2.prototype.SetV = function (v) {
  4053. this.x = v.x;
  4054. this.y = v.y;
  4055. }
  4056. b2Vec2.prototype.GetNegative = function () {
  4057. return new b2Vec2((-this.x), (-this.y));
  4058. }
  4059. b2Vec2.prototype.NegativeSelf = function () {
  4060. this.x = (-this.x);
  4061. this.y = (-this.y);
  4062. }
  4063. b2Vec2.Make = function (x_, y_) {
  4064. if (x_ === undefined) x_ = 0;
  4065. if (y_ === undefined) y_ = 0;
  4066. return new b2Vec2(x_, y_);
  4067. }
  4068. b2Vec2.prototype.Copy = function () {
  4069. return new b2Vec2(this.x, this.y);
  4070. }
  4071. b2Vec2.prototype.Add = function (v) {
  4072. this.x += v.x;
  4073. this.y += v.y;
  4074. }
  4075. b2Vec2.prototype.Subtract = function (v) {
  4076. this.x -= v.x;
  4077. this.y -= v.y;
  4078. }
  4079. b2Vec2.prototype.Multiply = function (a) {
  4080. if (a === undefined) a = 0;
  4081. this.x *= a;
  4082. this.y *= a;
  4083. }
  4084. b2Vec2.prototype.MulM = function (A) {
  4085. var tX = this.x;
  4086. this.x = A.col1.x * tX + A.col2.x * this.y;
  4087. this.y = A.col1.y * tX + A.col2.y * this.y;
  4088. }
  4089. b2Vec2.prototype.MulTM = function (A) {
  4090. var tX = b2Math.Dot(this, A.col1);
  4091. this.y = b2Math.Dot(this, A.col2);
  4092. this.x = tX;
  4093. }
  4094. b2Vec2.prototype.CrossVF = function (s) {
  4095. if (s === undefined) s = 0;
  4096. var tX = this.x;
  4097. this.x = s * this.y;
  4098. this.y = (-s * tX);
  4099. }
  4100. b2Vec2.prototype.CrossFV = function (s) {
  4101. if (s === undefined) s = 0;
  4102. var tX = this.x;
  4103. this.x = (-s * this.y);
  4104. this.y = s * tX;
  4105. }
  4106. b2Vec2.prototype.MinV = function (b) {
  4107. this.x = this.x < b.x ? this.x : b.x;
  4108. this.y = this.y < b.y ? this.y : b.y;
  4109. }
  4110. b2Vec2.prototype.MaxV = function (b) {
  4111. this.x = this.x > b.x ? this.x : b.x;
  4112. this.y = this.y > b.y ? this.y : b.y;
  4113. }
  4114. b2Vec2.prototype.Abs = function () {
  4115. if (this.x < 0) this.x = (-this.x);
  4116. if (this.y < 0) this.y = (-this.y);
  4117. }
  4118. b2Vec2.prototype.Length = function () {
  4119. return Math.sqrt(this.x * this.x + this.y * this.y);
  4120. }
  4121. b2Vec2.prototype.LengthSquared = function () {
  4122. return (this.x * this.x + this.y * this.y);
  4123. }
  4124. b2Vec2.prototype.Normalize = function () {
  4125. var length = Math.sqrt(this.x * this.x + this.y * this.y);
  4126. if (length < Number.MIN_VALUE) {
  4127. return 0.0;
  4128. }
  4129. var invLength = 1.0 / length;
  4130. this.x *= invLength;
  4131. this.y *= invLength;
  4132. return length;
  4133. }
  4134. b2Vec2.prototype.IsValid = function () {
  4135. return b2Math.IsValid(this.x) && b2Math.IsValid(this.y);
  4136. }
  4137. b2Vec3.b2Vec3 = function () {};
  4138. b2Vec3.prototype.b2Vec3 = function (x, y, z) {
  4139. if (x === undefined) x = 0;
  4140. if (y === undefined) y = 0;
  4141. if (z === undefined) z = 0;
  4142. this.x = x;
  4143. this.y = y;
  4144. this.z = z;
  4145. }
  4146. b2Vec3.prototype.SetZero = function () {
  4147. this.x = this.y = this.z = 0.0;
  4148. }
  4149. b2Vec3.prototype.Set = function (x, y, z) {
  4150. if (x === undefined) x = 0;
  4151. if (y === undefined) y = 0;
  4152. if (z === undefined) z = 0;
  4153. this.x = x;
  4154. this.y = y;
  4155. this.z = z;
  4156. }
  4157. b2Vec3.prototype.SetV = function (v) {
  4158. this.x = v.x;
  4159. this.y = v.y;
  4160. this.z = v.z;
  4161. }
  4162. b2Vec3.prototype.GetNegative = function () {
  4163. return new b2Vec3((-this.x), (-this.y), (-this.z));
  4164. }
  4165. b2Vec3.prototype.NegativeSelf = function () {
  4166. this.x = (-this.x);
  4167. this.y = (-this.y);
  4168. this.z = (-this.z);
  4169. }
  4170. b2Vec3.prototype.Copy = function () {
  4171. return new b2Vec3(this.x, this.y, this.z);
  4172. }
  4173. b2Vec3.prototype.Add = function (v) {
  4174. this.x += v.x;
  4175. this.y += v.y;
  4176. this.z += v.z;
  4177. }
  4178. b2Vec3.prototype.Subtract = function (v) {
  4179. this.x -= v.x;
  4180. this.y -= v.y;
  4181. this.z -= v.z;
  4182. }
  4183. b2Vec3.prototype.Multiply = function (a) {
  4184. if (a === undefined) a = 0;
  4185. this.x *= a;
  4186. this.y *= a;
  4187. this.z *= a;
  4188. }
  4189. })();
  4190. (function () {
  4191. var b2ControllerEdge = Box2D.Dynamics.Controllers.b2ControllerEdge,
  4192. b2Mat22 = Box2D.Common.Math.b2Mat22,
  4193. b2Mat33 = Box2D.Common.Math.b2Mat33,
  4194. b2Math = Box2D.Common.Math.b2Math,
  4195. b2Sweep = Box2D.Common.Math.b2Sweep,
  4196. b2Transform = Box2D.Common.Math.b2Transform,
  4197. b2Vec2 = Box2D.Common.Math.b2Vec2,
  4198. b2Vec3 = Box2D.Common.Math.b2Vec3,
  4199. b2Color = Box2D.Common.b2Color,
  4200. b2internal = Box2D.Common.b2internal,
  4201. b2Settings = Box2D.Common.b2Settings,
  4202. b2AABB = Box2D.Collision.b2AABB,
  4203. b2Bound = Box2D.Collision.b2Bound,
  4204. b2BoundValues = Box2D.Collision.b2BoundValues,
  4205. b2Collision = Box2D.Collision.b2Collision,
  4206. b2ContactID = Box2D.Collision.b2ContactID,
  4207. b2ContactPoint = Box2D.Collision.b2ContactPoint,
  4208. b2Distance = Box2D.Collision.b2Distance,
  4209. b2DistanceInput = Box2D.Collision.b2DistanceInput,
  4210. b2DistanceOutput = Box2D.Collision.b2DistanceOutput,
  4211. b2DistanceProxy = Box2D.Collision.b2DistanceProxy,
  4212. b2DynamicTree = Box2D.Collision.b2DynamicTree,
  4213. b2DynamicTreeBroadPhase = Box2D.Collision.b2DynamicTreeBroadPhase,
  4214. b2DynamicTreeNode = Box2D.Collision.b2DynamicTreeNode,
  4215. b2DynamicTreePair = Box2D.Collision.b2DynamicTreePair,
  4216. b2Manifold = Box2D.Collision.b2Manifold,
  4217. b2ManifoldPoint = Box2D.Collision.b2ManifoldPoint,
  4218. b2Point = Box2D.Collision.b2Point,
  4219. b2RayCastInput = Box2D.Collision.b2RayCastInput,
  4220. b2RayCastOutput = Box2D.Collision.b2RayCastOutput,
  4221. b2Segment = Box2D.Collision.b2Segment,
  4222. b2SeparationFunction = Box2D.Collision.b2SeparationFunction,
  4223. b2Simplex = Box2D.Collision.b2Simplex,
  4224. b2SimplexCache = Box2D.Collision.b2SimplexCache,
  4225. b2SimplexVertex = Box2D.Collision.b2SimplexVertex,
  4226. b2TimeOfImpact = Box2D.Collision.b2TimeOfImpact,
  4227. b2TOIInput = Box2D.Collision.b2TOIInput,
  4228. b2WorldManifold = Box2D.Collision.b2WorldManifold,
  4229. ClipVertex = Box2D.Collision.ClipVertex,
  4230. Features = Box2D.Collision.Features,
  4231. IBroadPhase = Box2D.Collision.IBroadPhase,
  4232. b2CircleShape = Box2D.Collision.Shapes.b2CircleShape,
  4233. b2EdgeChainDef = Box2D.Collision.Shapes.b2EdgeChainDef,
  4234. b2EdgeShape = Box2D.Collision.Shapes.b2EdgeShape,
  4235. b2MassData = Box2D.Collision.Shapes.b2MassData,
  4236. b2PolygonShape = Box2D.Collision.Shapes.b2PolygonShape,
  4237. b2Shape = Box2D.Collision.Shapes.b2Shape,
  4238. b2Body = Box2D.Dynamics.b2Body,
  4239. b2BodyDef = Box2D.Dynamics.b2BodyDef,
  4240. b2ContactFilter = Box2D.Dynamics.b2ContactFilter,
  4241. b2ContactImpulse = Box2D.Dynamics.b2ContactImpulse,
  4242. b2ContactListener = Box2D.Dynamics.b2ContactListener,
  4243. b2ContactManager = Box2D.Dynamics.b2ContactManager,
  4244. b2DebugDraw = Box2D.Dynamics.b2DebugDraw,
  4245. b2DestructionListener = Box2D.Dynamics.b2DestructionListener,
  4246. b2FilterData = Box2D.Dynamics.b2FilterData,
  4247. b2Fixture = Box2D.Dynamics.b2Fixture,
  4248. b2FixtureDef = Box2D.Dynamics.b2FixtureDef,
  4249. b2Island = Box2D.Dynamics.b2Island,
  4250. b2TimeStep = Box2D.Dynamics.b2TimeStep,
  4251. b2World = Box2D.Dynamics.b2World,
  4252. b2CircleContact = Box2D.Dynamics.Contacts.b2CircleContact,
  4253. b2Contact = Box2D.Dynamics.Contacts.b2Contact,
  4254. b2ContactConstraint = Box2D.Dynamics.Contacts.b2ContactConstraint,
  4255. b2ContactConstraintPoint = Box2D.Dynamics.Contacts.b2ContactConstraintPoint,
  4256. b2ContactEdge = Box2D.Dynamics.Contacts.b2ContactEdge,
  4257. b2ContactFactory = Box2D.Dynamics.Contacts.b2ContactFactory,
  4258. b2ContactRegister = Box2D.Dynamics.Contacts.b2ContactRegister,
  4259. b2ContactResult = Box2D.Dynamics.Contacts.b2ContactResult,
  4260. b2ContactSolver = Box2D.Dynamics.Contacts.b2ContactSolver,
  4261. b2EdgeAndCircleContact = Box2D.Dynamics.Contacts.b2EdgeAndCircleContact,
  4262. b2NullContact = Box2D.Dynamics.Contacts.b2NullContact,
  4263. b2PolyAndCircleContact = Box2D.Dynamics.Contacts.b2PolyAndCircleContact,
  4264. b2PolyAndEdgeContact = Box2D.Dynamics.Contacts.b2PolyAndEdgeContact,
  4265. b2PolygonContact = Box2D.Dynamics.Contacts.b2PolygonContact,
  4266. b2PositionSolverManifold = Box2D.Dynamics.Contacts.b2PositionSolverManifold,
  4267. b2Controller = Box2D.Dynamics.Controllers.b2Controller,
  4268. b2DistanceJoint = Box2D.Dynamics.Joints.b2DistanceJoint,
  4269. b2DistanceJointDef = Box2D.Dynamics.Joints.b2DistanceJointDef,
  4270. b2FrictionJoint = Box2D.Dynamics.Joints.b2FrictionJoint,
  4271. b2FrictionJointDef = Box2D.Dynamics.Joints.b2FrictionJointDef,
  4272. b2GearJoint = Box2D.Dynamics.Joints.b2GearJoint,
  4273. b2GearJointDef = Box2D.Dynamics.Joints.b2GearJointDef,
  4274. b2Jacobian = Box2D.Dynamics.Joints.b2Jacobian,
  4275. b2Joint = Box2D.Dynamics.Joints.b2Joint,
  4276. b2JointDef = Box2D.Dynamics.Joints.b2JointDef,
  4277. b2JointEdge = Box2D.Dynamics.Joints.b2JointEdge,
  4278. b2LineJoint = Box2D.Dynamics.Joints.b2LineJoint,
  4279. b2LineJointDef = Box2D.Dynamics.Joints.b2LineJointDef,
  4280. b2MouseJoint = Box2D.Dynamics.Joints.b2MouseJoint,
  4281. b2MouseJointDef = Box2D.Dynamics.Joints.b2MouseJointDef,
  4282. b2PrismaticJoint = Box2D.Dynamics.Joints.b2PrismaticJoint,
  4283. b2PrismaticJointDef = Box2D.Dynamics.Joints.b2PrismaticJointDef,
  4284. b2PulleyJoint = Box2D.Dynamics.Joints.b2PulleyJoint,
  4285. b2PulleyJointDef = Box2D.Dynamics.Joints.b2PulleyJointDef,
  4286. b2RevoluteJoint = Box2D.Dynamics.Joints.b2RevoluteJoint,
  4287. b2RevoluteJointDef = Box2D.Dynamics.Joints.b2RevoluteJointDef,
  4288. b2WeldJoint = Box2D.Dynamics.Joints.b2WeldJoint,
  4289. b2WeldJointDef = Box2D.Dynamics.Joints.b2WeldJointDef;
  4290. b2Body.b2Body = function () {
  4291. this.m_xf = new b2Transform();
  4292. this.m_sweep = new b2Sweep();
  4293. this.m_linearVelocity = new b2Vec2();
  4294. this.m_force = new b2Vec2();
  4295. };
  4296. b2Body.prototype.connectEdges = function (s1, s2, angle1) {
  4297. if (angle1 === undefined) angle1 = 0;
  4298. var angle2 = Math.atan2(s2.GetDirectionVector().y, s2.GetDirectionVector().x);
  4299. var coreOffset = Math.tan((angle2 - angle1) * 0.5);
  4300. var core = b2Math.MulFV(coreOffset, s2.GetDirectionVector());
  4301. core = b2Math.SubtractVV(core, s2.GetNormalVector());
  4302. core = b2Math.MulFV(b2Settings.b2_toiSlop, core);
  4303. core = b2Math.AddVV(core, s2.GetVertex1());
  4304. var cornerDir = b2Math.AddVV(s1.GetDirectionVector(), s2.GetDirectionVector());
  4305. cornerDir.Normalize();
  4306. var convex = b2Math.Dot(s1.GetDirectionVector(), s2.GetNormalVector()) > 0.0;
  4307. s1.SetNextEdge(s2, core, cornerDir, convex);
  4308. s2.SetPrevEdge(s1, core, cornerDir, convex);
  4309. return angle2;
  4310. }
  4311. b2Body.prototype.CreateFixture = function (def) {
  4312. if (this.m_world.IsLocked() == true) {
  4313. return null;
  4314. }
  4315. var fixture = new b2Fixture();
  4316. fixture.Create(this, this.m_xf, def);
  4317. if (this.m_flags & b2Body.e_activeFlag) {
  4318. var broadPhase = this.m_world.m_contactManager.m_broadPhase;
  4319. fixture.CreateProxy(broadPhase, this.m_xf);
  4320. }
  4321. fixture.m_next = this.m_fixtureList;
  4322. this.m_fixtureList = fixture;
  4323. ++this.m_fixtureCount;
  4324. fixture.m_body = this;
  4325. if (fixture.m_density > 0.0) {
  4326. this.ResetMassData();
  4327. }
  4328. this.m_world.m_flags |= b2World.e_newFixture;
  4329. return fixture;
  4330. }
  4331. b2Body.prototype.CreateFixture2 = function (shape, density) {
  4332. if (density === undefined) density = 0.0;
  4333. var def = new b2FixtureDef();
  4334. def.shape = shape;
  4335. def.density = density;
  4336. return this.CreateFixture(def);
  4337. }
  4338. b2Body.prototype.DestroyFixture = function (fixture) {
  4339. if (this.m_world.IsLocked() == true) {
  4340. return;
  4341. }
  4342. var node = this.m_fixtureList;
  4343. var ppF = null;
  4344. var found = false;
  4345. while (node != null) {
  4346. if (node == fixture) {
  4347. if (ppF) ppF.m_next = fixture.m_next;
  4348. else this.m_fixtureList = fixture.m_next;
  4349. found = true;
  4350. break;
  4351. }
  4352. ppF = node;
  4353. node = node.m_next;
  4354. }
  4355. var edge = this.m_contactList;
  4356. while (edge) {
  4357. var c = edge.contact;
  4358. edge = edge.next;
  4359. var fixtureA = c.GetFixtureA();
  4360. var fixtureB = c.GetFixtureB();
  4361. if (fixture == fixtureA || fixture == fixtureB) {
  4362. this.m_world.m_contactManager.Destroy(c);
  4363. }
  4364. }
  4365. if (this.m_flags & b2Body.e_activeFlag) {
  4366. var broadPhase = this.m_world.m_contactManager.m_broadPhase;
  4367. fixture.DestroyProxy(broadPhase);
  4368. }
  4369. else {}
  4370. fixture.Destroy();
  4371. fixture.m_body = null;
  4372. fixture.m_next = null;
  4373. --this.m_fixtureCount;
  4374. this.ResetMassData();
  4375. }
  4376. b2Body.prototype.SetPositionAndAngle = function (position, angle) {
  4377. if (angle === undefined) angle = 0;
  4378. var f;
  4379. if (this.m_world.IsLocked() == true) {
  4380. return;
  4381. }
  4382. this.m_xf.R.Set(angle);
  4383. this.m_xf.position.SetV(position);
  4384. var tMat = this.m_xf.R;
  4385. var tVec = this.m_sweep.localCenter;
  4386. this.m_sweep.c.x = (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
  4387. this.m_sweep.c.y = (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
  4388. this.m_sweep.c.x += this.m_xf.position.x;
  4389. this.m_sweep.c.y += this.m_xf.position.y;
  4390. this.m_sweep.c0.SetV(this.m_sweep.c);
  4391. this.m_sweep.a0 = this.m_sweep.a = angle;
  4392. var broadPhase = this.m_world.m_contactManager.m_broadPhase;
  4393. for (f = this.m_fixtureList;
  4394. f; f = f.m_next) {
  4395. f.Synchronize(broadPhase, this.m_xf, this.m_xf);
  4396. }
  4397. this.m_world.m_contactManager.FindNewContacts();
  4398. }
  4399. b2Body.prototype.SetTransform = function (xf) {
  4400. this.SetPositionAndAngle(xf.position, xf.GetAngle());
  4401. }
  4402. b2Body.prototype.GetTransform = function () {
  4403. return this.m_xf;
  4404. }
  4405. b2Body.prototype.GetPosition = function () {
  4406. return this.m_xf.position;
  4407. }
  4408. b2Body.prototype.SetPosition = function (position) {
  4409. this.SetPositionAndAngle(position, this.GetAngle());
  4410. }
  4411. b2Body.prototype.GetAngle = function () {
  4412. return this.m_sweep.a;
  4413. }
  4414. b2Body.prototype.SetAngle = function (angle) {
  4415. if (angle === undefined) angle = 0;
  4416. this.SetPositionAndAngle(this.GetPosition(), angle);
  4417. }
  4418. b2Body.prototype.GetWorldCenter = function () {
  4419. return this.m_sweep.c;
  4420. }
  4421. b2Body.prototype.GetLocalCenter = function () {
  4422. return this.m_sweep.localCenter;
  4423. }
  4424. b2Body.prototype.SetLinearVelocity = function (v) {
  4425. if (this.m_type == b2Body.b2_staticBody) {
  4426. return;
  4427. }
  4428. this.m_linearVelocity.SetV(v);
  4429. }
  4430. b2Body.prototype.GetLinearVelocity = function () {
  4431. return this.m_linearVelocity;
  4432. }
  4433. b2Body.prototype.SetAngularVelocity = function (omega) {
  4434. if (omega === undefined) omega = 0;
  4435. if (this.m_type == b2Body.b2_staticBody) {
  4436. return;
  4437. }
  4438. this.m_angularVelocity = omega;
  4439. }
  4440. b2Body.prototype.GetAngularVelocity = function () {
  4441. return this.m_angularVelocity;
  4442. }
  4443. b2Body.prototype.GetDefinition = function () {
  4444. var bd = new b2BodyDef();
  4445. bd.type = this.GetType();
  4446. bd.allowSleep = (this.m_flags & b2Body.e_allowSleepFlag) == b2Body.e_allowSleepFlag;
  4447. bd.angle = this.GetAngle();
  4448. bd.angularDamping = this.m_angularDamping;
  4449. bd.angularVelocity = this.m_angularVelocity;
  4450. bd.fixedRotation = (this.m_flags & b2Body.e_fixedRotationFlag) == b2Body.e_fixedRotationFlag;
  4451. bd.bullet = (this.m_flags & b2Body.e_bulletFlag) == b2Body.e_bulletFlag;
  4452. bd.awake = (this.m_flags & b2Body.e_awakeFlag) == b2Body.e_awakeFlag;
  4453. bd.linearDamping = this.m_linearDamping;
  4454. bd.linearVelocity.SetV(this.GetLinearVelocity());
  4455. bd.position = this.GetPosition();
  4456. bd.userData = this.GetUserData();
  4457. return bd;
  4458. }
  4459. b2Body.prototype.ApplyForce = function (force, point) {
  4460. if (this.m_type != b2Body.b2_dynamicBody) {
  4461. return;
  4462. }
  4463. if (this.IsAwake() == false) {
  4464. this.SetAwake(true);
  4465. }
  4466. this.m_force.x += force.x;
  4467. this.m_force.y += force.y;
  4468. this.m_torque += ((point.x - this.m_sweep.c.x) * force.y - (point.y - this.m_sweep.c.y) * force.x);
  4469. }
  4470. b2Body.prototype.ApplyTorque = function (torque) {
  4471. if (torque === undefined) torque = 0;
  4472. if (this.m_type != b2Body.b2_dynamicBody) {
  4473. return;
  4474. }
  4475. if (this.IsAwake() == false) {
  4476. this.SetAwake(true);
  4477. }
  4478. this.m_torque += torque;
  4479. }
  4480. b2Body.prototype.ApplyImpulse = function (impulse, point) {
  4481. if (this.m_type != b2Body.b2_dynamicBody) {
  4482. return;
  4483. }
  4484. if (this.IsAwake() == false) {
  4485. this.SetAwake(true);
  4486. }
  4487. this.m_linearVelocity.x += this.m_invMass * impulse.x;
  4488. this.m_linearVelocity.y += this.m_invMass * impulse.y;
  4489. this.m_angularVelocity += this.m_invI * ((point.x - this.m_sweep.c.x) * impulse.y - (point.y - this.m_sweep.c.y) * impulse.x);
  4490. }
  4491. b2Body.prototype.Split = function (callback) {
  4492. var linearVelocity = this.GetLinearVelocity().Copy();
  4493. var angularVelocity = this.GetAngularVelocity();
  4494. var center = this.GetWorldCenter();
  4495. var body1 = this;
  4496. var body2 = this.m_world.CreateBody(this.GetDefinition());
  4497. var prev;
  4498. for (var f = body1.m_fixtureList; f;) {
  4499. if (callback(f)) {
  4500. var next = f.m_next;
  4501. if (prev) {
  4502. prev.m_next = next;
  4503. }
  4504. else {
  4505. body1.m_fixtureList = next;
  4506. }
  4507. body1.m_fixtureCount--;
  4508. f.m_next = body2.m_fixtureList;
  4509. body2.m_fixtureList = f;
  4510. body2.m_fixtureCount++;
  4511. f.m_body = body2;
  4512. f = next;
  4513. }
  4514. else {
  4515. prev = f;
  4516. f = f.m_next;
  4517. }
  4518. }
  4519. body1.ResetMassData();
  4520. body2.ResetMassData();
  4521. var center1 = body1.GetWorldCenter();
  4522. var center2 = body2.GetWorldCenter();
  4523. var velocity1 = b2Math.AddVV(linearVelocity, b2Math.CrossFV(angularVelocity, b2Math.SubtractVV(center1, center)));
  4524. var velocity2 = b2Math.AddVV(linearVelocity, b2Math.CrossFV(angularVelocity, b2Math.SubtractVV(center2, center)));
  4525. body1.SetLinearVelocity(velocity1);
  4526. body2.SetLinearVelocity(velocity2);
  4527. body1.SetAngularVelocity(angularVelocity);
  4528. body2.SetAngularVelocity(angularVelocity);
  4529. body1.SynchronizeFixtures();
  4530. body2.SynchronizeFixtures();
  4531. return body2;
  4532. }
  4533. b2Body.prototype.Merge = function (other) {
  4534. var f;
  4535. for (f = other.m_fixtureList;
  4536. f;) {
  4537. var next = f.m_next;
  4538. other.m_fixtureCount--;
  4539. f.m_next = this.m_fixtureList;
  4540. this.m_fixtureList = f;
  4541. this.m_fixtureCount++;
  4542. f.m_body = body2;
  4543. f = next;
  4544. }
  4545. body1.m_fixtureCount = 0;
  4546. var body1 = this;
  4547. var body2 = other;
  4548. var center1 = body1.GetWorldCenter();
  4549. var center2 = body2.GetWorldCenter();
  4550. var velocity1 = body1.GetLinearVelocity().Copy();
  4551. var velocity2 = body2.GetLinearVelocity().Copy();
  4552. var angular1 = body1.GetAngularVelocity();
  4553. var angular = body2.GetAngularVelocity();
  4554. body1.ResetMassData();
  4555. this.SynchronizeFixtures();
  4556. }
  4557. b2Body.prototype.GetMass = function () {
  4558. return this.m_mass;
  4559. }
  4560. b2Body.prototype.GetInertia = function () {
  4561. return this.m_I;
  4562. }
  4563. b2Body.prototype.GetMassData = function (data) {
  4564. data.mass = this.m_mass;
  4565. data.I = this.m_I;
  4566. data.center.SetV(this.m_sweep.localCenter);
  4567. }
  4568. b2Body.prototype.SetMassData = function (massData) {
  4569. b2Settings.b2Assert(this.m_world.IsLocked() == false);
  4570. if (this.m_world.IsLocked() == true) {
  4571. return;
  4572. }
  4573. if (this.m_type != b2Body.b2_dynamicBody) {
  4574. return;
  4575. }
  4576. this.m_invMass = 0.0;
  4577. this.m_I = 0.0;
  4578. this.m_invI = 0.0;
  4579. this.m_mass = massData.mass;
  4580. if (this.m_mass <= 0.0) {
  4581. this.m_mass = 1.0;
  4582. }
  4583. this.m_invMass = 1.0 / this.m_mass;
  4584. if (massData.I > 0.0 && (this.m_flags & b2Body.e_fixedRotationFlag) == 0) {
  4585. this.m_I = massData.I - this.m_mass * (massData.center.x * massData.center.x + massData.center.y * massData.center.y);
  4586. this.m_invI = 1.0 / this.m_I;
  4587. }
  4588. var oldCenter = this.m_sweep.c.Copy();
  4589. this.m_sweep.localCenter.SetV(massData.center);
  4590. this.m_sweep.c0.SetV(b2Math.MulX(this.m_xf, this.m_sweep.localCenter));
  4591. this.m_sweep.c.SetV(this.m_sweep.c0);
  4592. this.m_linearVelocity.x += this.m_angularVelocity * (-(this.m_sweep.c.y - oldCenter.y));
  4593. this.m_linearVelocity.y += this.m_angularVelocity * (+(this.m_sweep.c.x - oldCenter.x));
  4594. }
  4595. b2Body.prototype.ResetMassData = function () {
  4596. this.m_mass = 0.0;
  4597. this.m_invMass = 0.0;
  4598. this.m_I = 0.0;
  4599. this.m_invI = 0.0;
  4600. this.m_sweep.localCenter.SetZero();
  4601. if (this.m_type == b2Body.b2_staticBody || this.m_type == b2Body.b2_kinematicBody) {
  4602. return;
  4603. }
  4604. var center = b2Vec2.Make(0, 0);
  4605. for (var f = this.m_fixtureList; f; f = f.m_next) {
  4606. if (f.m_density == 0.0) {
  4607. continue;
  4608. }
  4609. var massData = f.GetMassData();
  4610. this.m_mass += massData.mass;
  4611. center.x += massData.center.x * massData.mass;
  4612. center.y += massData.center.y * massData.mass;
  4613. this.m_I += massData.I;
  4614. }
  4615. if (this.m_mass > 0.0) {
  4616. this.m_invMass = 1.0 / this.m_mass;
  4617. center.x *= this.m_invMass;
  4618. center.y *= this.m_invMass;
  4619. }
  4620. else {
  4621. this.m_mass = 1.0;
  4622. this.m_invMass = 1.0;
  4623. }
  4624. if (this.m_I > 0.0 && (this.m_flags & b2Body.e_fixedRotationFlag) == 0) {
  4625. this.m_I -= this.m_mass * (center.x * center.x + center.y * center.y);
  4626. this.m_I *= this.m_inertiaScale;
  4627. b2Settings.b2Assert(this.m_I > 0);
  4628. this.m_invI = 1.0 / this.m_I;
  4629. }
  4630. else {
  4631. this.m_I = 0.0;
  4632. this.m_invI = 0.0;
  4633. }
  4634. var oldCenter = this.m_sweep.c.Copy();
  4635. this.m_sweep.localCenter.SetV(center);
  4636. this.m_sweep.c0.SetV(b2Math.MulX(this.m_xf, this.m_sweep.localCenter));
  4637. this.m_sweep.c.SetV(this.m_sweep.c0);
  4638. this.m_linearVelocity.x += this.m_angularVelocity * (-(this.m_sweep.c.y - oldCenter.y));
  4639. this.m_linearVelocity.y += this.m_angularVelocity * (+(this.m_sweep.c.x - oldCenter.x));
  4640. }
  4641. b2Body.prototype.GetWorldPoint = function (localPoint) {
  4642. var A = this.m_xf.R;
  4643. var u = new b2Vec2(A.col1.x * localPoint.x + A.col2.x * localPoint.y, A.col1.y * localPoint.x + A.col2.y * localPoint.y);
  4644. u.x += this.m_xf.position.x;
  4645. u.y += this.m_xf.position.y;
  4646. return u;
  4647. }
  4648. b2Body.prototype.GetWorldVector = function (localVector) {
  4649. return b2Math.MulMV(this.m_xf.R, localVector);
  4650. }
  4651. b2Body.prototype.GetLocalPoint = function (worldPoint) {
  4652. return b2Math.MulXT(this.m_xf, worldPoint);
  4653. }
  4654. b2Body.prototype.GetLocalVector = function (worldVector) {
  4655. return b2Math.MulTMV(this.m_xf.R, worldVector);
  4656. }
  4657. b2Body.prototype.GetLinearVelocityFromWorldPoint = function (worldPoint) {
  4658. return new b2Vec2(this.m_linearVelocity.x - this.m_angularVelocity * (worldPoint.y - this.m_sweep.c.y), this.m_linearVelocity.y + this.m_angularVelocity * (worldPoint.x - this.m_sweep.c.x));
  4659. }
  4660. b2Body.prototype.GetLinearVelocityFromLocalPoint = function (localPoint) {
  4661. var A = this.m_xf.R;
  4662. var worldPoint = new b2Vec2(A.col1.x * localPoint.x + A.col2.x * localPoint.y, A.col1.y * localPoint.x + A.col2.y * localPoint.y);
  4663. worldPoint.x += this.m_xf.position.x;
  4664. worldPoint.y += this.m_xf.position.y;
  4665. return new b2Vec2(this.m_linearVelocity.x - this.m_angularVelocity * (worldPoint.y - this.m_sweep.c.y), this.m_linearVelocity.y + this.m_angularVelocity * (worldPoint.x - this.m_sweep.c.x));
  4666. }
  4667. b2Body.prototype.GetLinearDamping = function () {
  4668. return this.m_linearDamping;
  4669. }
  4670. b2Body.prototype.SetLinearDamping = function (linearDamping) {
  4671. if (linearDamping === undefined) linearDamping = 0;
  4672. this.m_linearDamping = linearDamping;
  4673. }
  4674. b2Body.prototype.GetAngularDamping = function () {
  4675. return this.m_angularDamping;
  4676. }
  4677. b2Body.prototype.SetAngularDamping = function (angularDamping) {
  4678. if (angularDamping === undefined) angularDamping = 0;
  4679. this.m_angularDamping = angularDamping;
  4680. }
  4681. b2Body.prototype.SetType = function (type) {
  4682. if (type === undefined) type = 0;
  4683. if (this.m_type == type) {
  4684. return;
  4685. }
  4686. this.m_type = type;
  4687. this.ResetMassData();
  4688. if (this.m_type == b2Body.b2_staticBody) {
  4689. this.m_linearVelocity.SetZero();
  4690. this.m_angularVelocity = 0.0;
  4691. }
  4692. this.SetAwake(true);
  4693. this.m_force.SetZero();
  4694. this.m_torque = 0.0;
  4695. for (var ce = this.m_contactList; ce; ce = ce.next) {
  4696. ce.contact.FlagForFiltering();
  4697. }
  4698. }
  4699. b2Body.prototype.GetType = function () {
  4700. return this.m_type;
  4701. }
  4702. b2Body.prototype.SetBullet = function (flag) {
  4703. if (flag) {
  4704. this.m_flags |= b2Body.e_bulletFlag;
  4705. }
  4706. else {
  4707. this.m_flags &= ~b2Body.e_bulletFlag;
  4708. }
  4709. }
  4710. b2Body.prototype.IsBullet = function () {
  4711. return (this.m_flags & b2Body.e_bulletFlag) == b2Body.e_bulletFlag;
  4712. }
  4713. b2Body.prototype.SetSleepingAllowed = function (flag) {
  4714. if (flag) {
  4715. this.m_flags |= b2Body.e_allowSleepFlag;
  4716. }
  4717. else {
  4718. this.m_flags &= ~b2Body.e_allowSleepFlag;
  4719. this.SetAwake(true);
  4720. }
  4721. }
  4722. b2Body.prototype.SetAwake = function (flag) {
  4723. if (flag) {
  4724. this.m_flags |= b2Body.e_awakeFlag;
  4725. this.m_sleepTime = 0.0;
  4726. }
  4727. else {
  4728. this.m_flags &= ~b2Body.e_awakeFlag;
  4729. this.m_sleepTime = 0.0;
  4730. this.m_linearVelocity.SetZero();
  4731. this.m_angularVelocity = 0.0;
  4732. this.m_force.SetZero();
  4733. this.m_torque = 0.0;
  4734. }
  4735. }
  4736. b2Body.prototype.IsAwake = function () {
  4737. return (this.m_flags & b2Body.e_awakeFlag) == b2Body.e_awakeFlag;
  4738. }
  4739. b2Body.prototype.SetFixedRotation = function (fixed) {
  4740. if (fixed) {
  4741. this.m_flags |= b2Body.e_fixedRotationFlag;
  4742. }
  4743. else {
  4744. this.m_flags &= ~b2Body.e_fixedRotationFlag;
  4745. }
  4746. this.ResetMassData();
  4747. }
  4748. b2Body.prototype.IsFixedRotation = function () {
  4749. return (this.m_flags & b2Body.e_fixedRotationFlag) == b2Body.e_fixedRotationFlag;
  4750. }
  4751. b2Body.prototype.SetActive = function (flag) {
  4752. if (flag == this.IsActive()) {
  4753. return;
  4754. }
  4755. var broadPhase;
  4756. var f;
  4757. if (flag) {
  4758. this.m_flags |= b2Body.e_activeFlag;
  4759. broadPhase = this.m_world.m_contactManager.m_broadPhase;
  4760. for (f = this.m_fixtureList;
  4761. f; f = f.m_next) {
  4762. f.CreateProxy(broadPhase, this.m_xf);
  4763. }
  4764. }
  4765. else {
  4766. this.m_flags &= ~b2Body.e_activeFlag;
  4767. broadPhase = this.m_world.m_contactManager.m_broadPhase;
  4768. for (f = this.m_fixtureList;
  4769. f; f = f.m_next) {
  4770. f.DestroyProxy(broadPhase);
  4771. }
  4772. var ce = this.m_contactList;
  4773. while (ce) {
  4774. var ce0 = ce;
  4775. ce = ce.next;
  4776. this.m_world.m_contactManager.Destroy(ce0.contact);
  4777. }
  4778. this.m_contactList = null;
  4779. }
  4780. }
  4781. b2Body.prototype.IsActive = function () {
  4782. return (this.m_flags & b2Body.e_activeFlag) == b2Body.e_activeFlag;
  4783. }
  4784. b2Body.prototype.IsSleepingAllowed = function () {
  4785. return (this.m_flags & b2Body.e_allowSleepFlag) == b2Body.e_allowSleepFlag;
  4786. }
  4787. b2Body.prototype.GetFixtureList = function () {
  4788. return this.m_fixtureList;
  4789. }
  4790. b2Body.prototype.GetJointList = function () {
  4791. return this.m_jointList;
  4792. }
  4793. b2Body.prototype.GetControllerList = function () {
  4794. return this.m_controllerList;
  4795. }
  4796. b2Body.prototype.GetContactList = function () {
  4797. return this.m_contactList;
  4798. }
  4799. b2Body.prototype.GetNext = function () {
  4800. return this.m_next;
  4801. }
  4802. b2Body.prototype.GetUserData = function () {
  4803. return this.m_userData;
  4804. }
  4805. b2Body.prototype.SetUserData = function (data) {
  4806. this.m_userData = data;
  4807. }
  4808. b2Body.prototype.GetWorld = function () {
  4809. return this.m_world;
  4810. }
  4811. b2Body.prototype.b2Body = function (bd, world) {
  4812. this.m_flags = 0;
  4813. if (bd.bullet) {
  4814. this.m_flags |= b2Body.e_bulletFlag;
  4815. }
  4816. if (bd.fixedRotation) {
  4817. this.m_flags |= b2Body.e_fixedRotationFlag;
  4818. }
  4819. if (bd.allowSleep) {
  4820. this.m_flags |= b2Body.e_allowSleepFlag;
  4821. }
  4822. if (bd.awake) {
  4823. this.m_flags |= b2Body.e_awakeFlag;
  4824. }
  4825. if (bd.active) {
  4826. this.m_flags |= b2Body.e_activeFlag;
  4827. }
  4828. this.m_world = world;
  4829. this.m_xf.position.SetV(bd.position);
  4830. this.m_xf.R.Set(bd.angle);
  4831. this.m_sweep.localCenter.SetZero();
  4832. this.m_sweep.t0 = 1.0;
  4833. this.m_sweep.a0 = this.m_sweep.a = bd.angle;
  4834. var tMat = this.m_xf.R;
  4835. var tVec = this.m_sweep.localCenter;
  4836. this.m_sweep.c.x = (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
  4837. this.m_sweep.c.y = (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
  4838. this.m_sweep.c.x += this.m_xf.position.x;
  4839. this.m_sweep.c.y += this.m_xf.position.y;
  4840. this.m_sweep.c0.SetV(this.m_sweep.c);
  4841. this.m_jointList = null;
  4842. this.m_controllerList = null;
  4843. this.m_contactList = null;
  4844. this.m_controllerCount = 0;
  4845. this.m_prev = null;
  4846. this.m_next = null;
  4847. this.m_linearVelocity.SetV(bd.linearVelocity);
  4848. this.m_angularVelocity = bd.angularVelocity;
  4849. this.m_linearDamping = bd.linearDamping;
  4850. this.m_angularDamping = bd.angularDamping;
  4851. this.m_force.Set(0.0, 0.0);
  4852. this.m_torque = 0.0;
  4853. this.m_sleepTime = 0.0;
  4854. this.m_type = bd.type;
  4855. if (this.m_type == b2Body.b2_dynamicBody) {
  4856. this.m_mass = 1.0;
  4857. this.m_invMass = 1.0;
  4858. }
  4859. else {
  4860. this.m_mass = 0.0;
  4861. this.m_invMass = 0.0;
  4862. }
  4863. this.m_I = 0.0;
  4864. this.m_invI = 0.0;
  4865. this.m_inertiaScale = bd.inertiaScale;
  4866. this.m_userData = bd.userData;
  4867. this.m_fixtureList = null;
  4868. this.m_fixtureCount = 0;
  4869. }
  4870. b2Body.prototype.SynchronizeFixtures = function () {
  4871. var xf1 = b2Body.s_xf1;
  4872. xf1.R.Set(this.m_sweep.a0);
  4873. var tMat = xf1.R;
  4874. var tVec = this.m_sweep.localCenter;
  4875. xf1.position.x = this.m_sweep.c0.x - (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
  4876. xf1.position.y = this.m_sweep.c0.y - (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
  4877. var f;
  4878. var broadPhase = this.m_world.m_contactManager.m_broadPhase;
  4879. for (f = this.m_fixtureList;
  4880. f; f = f.m_next) {
  4881. f.Synchronize(broadPhase, xf1, this.m_xf);
  4882. }
  4883. }
  4884. b2Body.prototype.SynchronizeTransform = function () {
  4885. this.m_xf.R.Set(this.m_sweep.a);
  4886. var tMat = this.m_xf.R;
  4887. var tVec = this.m_sweep.localCenter;
  4888. this.m_xf.position.x = this.m_sweep.c.x - (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
  4889. this.m_xf.position.y = this.m_sweep.c.y - (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
  4890. }
  4891. b2Body.prototype.ShouldCollide = function (other) {
  4892. if (this.m_type != b2Body.b2_dynamicBody && other.m_type != b2Body.b2_dynamicBody) {
  4893. return false;
  4894. }
  4895. for (var jn = this.m_jointList; jn; jn = jn.next) {
  4896. if (jn.other == other) if (jn.joint.m_collideConnected == false) {
  4897. return false;
  4898. }
  4899. }
  4900. return true;
  4901. }
  4902. b2Body.prototype.Advance = function (t) {
  4903. if (t === undefined) t = 0;
  4904. this.m_sweep.Advance(t);
  4905. this.m_sweep.c.SetV(this.m_sweep.c0);
  4906. this.m_sweep.a = this.m_sweep.a0;
  4907. this.SynchronizeTransform();
  4908. }
  4909. Box2D.postDefs.push(function () {
  4910. Box2D.Dynamics.b2Body.s_xf1 = new b2Transform();
  4911. Box2D.Dynamics.b2Body.e_islandFlag = 0x0001;
  4912. Box2D.Dynamics.b2Body.e_awakeFlag = 0x0002;
  4913. Box2D.Dynamics.b2Body.e_allowSleepFlag = 0x0004;
  4914. Box2D.Dynamics.b2Body.e_bulletFlag = 0x0008;
  4915. Box2D.Dynamics.b2Body.e_fixedRotationFlag = 0x0010;
  4916. Box2D.Dynamics.b2Body.e_activeFlag = 0x0020;
  4917. Box2D.Dynamics.b2Body.b2_staticBody = 0;
  4918. Box2D.Dynamics.b2Body.b2_kinematicBody = 1;
  4919. Box2D.Dynamics.b2Body.b2_dynamicBody = 2;
  4920. });
  4921. b2BodyDef.b2BodyDef = function () {
  4922. this.position = new b2Vec2();
  4923. this.linearVelocity = new b2Vec2();
  4924. };
  4925. b2BodyDef.prototype.b2BodyDef = function () {
  4926. this.userData = null;
  4927. this.position.Set(0.0, 0.0);
  4928. this.angle = 0.0;
  4929. this.linearVelocity.Set(0, 0);
  4930. this.angularVelocity = 0.0;
  4931. this.linearDamping = 0.0;
  4932. this.angularDamping = 0.0;
  4933. this.allowSleep = true;
  4934. this.awake = true;
  4935. this.fixedRotation = false;
  4936. this.bullet = false;
  4937. this.type = b2Body.b2_staticBody;
  4938. this.active = true;
  4939. this.inertiaScale = 1.0;
  4940. }
  4941. b2ContactFilter.b2ContactFilter = function () {};
  4942. b2ContactFilter.prototype.ShouldCollide = function (fixtureA, fixtureB) {
  4943. var filter1 = fixtureA.GetFilterData();
  4944. var filter2 = fixtureB.GetFilterData();
  4945. if (filter1.groupIndex == filter2.groupIndex && filter1.groupIndex != 0) {
  4946. return filter1.groupIndex > 0;
  4947. }
  4948. var collide = (filter1.maskBits & filter2.categoryBits) != 0 && (filter1.categoryBits & filter2.maskBits) != 0;
  4949. return collide;
  4950. }
  4951. b2ContactFilter.prototype.RayCollide = function (userData, fixture) {
  4952. if (!userData) return true;
  4953. return this.ShouldCollide((userData instanceof b2Fixture ? userData : null), fixture);
  4954. }
  4955. Box2D.postDefs.push(function () {
  4956. Box2D.Dynamics.b2ContactFilter.b2_defaultFilter = new b2ContactFilter();
  4957. });
  4958. b2ContactImpulse.b2ContactImpulse = function () {
  4959. this.normalImpulses = new Vector_a2j_Number(b2Settings.b2_maxManifoldPoints);
  4960. this.tangentImpulses = new Vector_a2j_Number(b2Settings.b2_maxManifoldPoints);
  4961. };
  4962. b2ContactListener.b2ContactListener = function () {};
  4963. b2ContactListener.prototype.BeginContact = function (contact) {}
  4964. b2ContactListener.prototype.EndContact = function (contact) {}
  4965. b2ContactListener.prototype.PreSolve = function (contact, oldManifold) {}
  4966. b2ContactListener.prototype.PostSolve = function (contact, impulse) {}
  4967. Box2D.postDefs.push(function () {
  4968. Box2D.Dynamics.b2ContactListener.b2_defaultListener = new b2ContactListener();
  4969. });
  4970. b2ContactManager.b2ContactManager = function () {};
  4971. b2ContactManager.prototype.b2ContactManager = function () {
  4972. this.m_world = null;
  4973. this.m_contactCount = 0;
  4974. this.m_contactFilter = b2ContactFilter.b2_defaultFilter;
  4975. this.m_contactListener = b2ContactListener.b2_defaultListener;
  4976. this.m_contactFactory = new b2ContactFactory(this.m_allocator);
  4977. this.m_broadPhase = new b2DynamicTreeBroadPhase();
  4978. }
  4979. b2ContactManager.prototype.AddPair = function (proxyUserDataA, proxyUserDataB) {
  4980. var fixtureA = (proxyUserDataA instanceof b2Fixture ? proxyUserDataA : null);
  4981. var fixtureB = (proxyUserDataB instanceof b2Fixture ? proxyUserDataB : null);
  4982. var bodyA = fixtureA.GetBody();
  4983. var bodyB = fixtureB.GetBody();
  4984. if (bodyA == bodyB) return;
  4985. var edge = bodyB.GetContactList();
  4986. while (edge) {
  4987. if (edge.other == bodyA) {
  4988. var fA = edge.contact.GetFixtureA();
  4989. var fB = edge.contact.GetFixtureB();
  4990. if (fA == fixtureA && fB == fixtureB) return;
  4991. if (fA == fixtureB && fB == fixtureA) return;
  4992. }
  4993. edge = edge.next;
  4994. }
  4995. if (bodyB.ShouldCollide(bodyA) == false) {
  4996. return;
  4997. }
  4998. if (this.m_contactFilter.ShouldCollide(fixtureA, fixtureB) == false) {
  4999. return;
  5000. }
  5001. var c = this.m_contactFactory.Create(fixtureA, fixtureB);
  5002. fixtureA = c.GetFixtureA();
  5003. fixtureB = c.GetFixtureB();
  5004. bodyA = fixtureA.m_body;
  5005. bodyB = fixtureB.m_body;
  5006. c.m_prev = null;
  5007. c.m_next = this.m_world.m_contactList;
  5008. if (this.m_world.m_contactList != null) {
  5009. this.m_world.m_contactList.m_prev = c;
  5010. }
  5011. this.m_world.m_contactList = c;
  5012. c.m_nodeA.contact = c;
  5013. c.m_nodeA.other = bodyB;
  5014. c.m_nodeA.prev = null;
  5015. c.m_nodeA.next = bodyA.m_contactList;
  5016. if (bodyA.m_contactList != null) {
  5017. bodyA.m_contactList.prev = c.m_nodeA;
  5018. }
  5019. bodyA.m_contactList = c.m_nodeA;
  5020. c.m_nodeB.contact = c;
  5021. c.m_nodeB.other = bodyA;
  5022. c.m_nodeB.prev = null;
  5023. c.m_nodeB.next = bodyB.m_contactList;
  5024. if (bodyB.m_contactList != null) {
  5025. bodyB.m_contactList.prev = c.m_nodeB;
  5026. }
  5027. bodyB.m_contactList = c.m_nodeB;
  5028. ++this.m_world.m_contactCount;
  5029. return;
  5030. }
  5031. b2ContactManager.prototype.FindNewContacts = function () {
  5032. this.m_broadPhase.UpdatePairs(Box2D.generateCallback(this, this.AddPair));
  5033. }
  5034. b2ContactManager.prototype.Destroy = function (c) {
  5035. var fixtureA = c.GetFixtureA();
  5036. var fixtureB = c.GetFixtureB();
  5037. var bodyA = fixtureA.GetBody();
  5038. var bodyB = fixtureB.GetBody();
  5039. if (c.IsTouching()) {
  5040. this.m_contactListener.EndContact(c);
  5041. }
  5042. if (c.m_prev) {
  5043. c.m_prev.m_next = c.m_next;
  5044. }
  5045. if (c.m_next) {
  5046. c.m_next.m_prev = c.m_prev;
  5047. }
  5048. if (c == this.m_world.m_contactList) {
  5049. this.m_world.m_contactList = c.m_next;
  5050. }
  5051. if (c.m_nodeA.prev) {
  5052. c.m_nodeA.prev.next = c.m_nodeA.next;
  5053. }
  5054. if (c.m_nodeA.next) {
  5055. c.m_nodeA.next.prev = c.m_nodeA.prev;
  5056. }
  5057. if (c.m_nodeA == bodyA.m_contactList) {
  5058. bodyA.m_contactList = c.m_nodeA.next;
  5059. }
  5060. if (c.m_nodeB.prev) {
  5061. c.m_nodeB.prev.next = c.m_nodeB.next;
  5062. }
  5063. if (c.m_nodeB.next) {
  5064. c.m_nodeB.next.prev = c.m_nodeB.prev;
  5065. }
  5066. if (c.m_nodeB == bodyB.m_contactList) {
  5067. bodyB.m_contactList = c.m_nodeB.next;
  5068. }
  5069. this.m_contactFactory.Destroy(c);
  5070. --this.m_contactCount;
  5071. }
  5072. b2ContactManager.prototype.Collide = function () {
  5073. var c = this.m_world.m_contactList;
  5074. while (c) {
  5075. var fixtureA = c.GetFixtureA();
  5076. var fixtureB = c.GetFixtureB();
  5077. var bodyA = fixtureA.GetBody();
  5078. var bodyB = fixtureB.GetBody();
  5079. if (bodyA.IsAwake() == false && bodyB.IsAwake() == false) {
  5080. c = c.GetNext();
  5081. continue;
  5082. }
  5083. if (c.m_flags & b2Contact.e_filterFlag) {
  5084. if (bodyB.ShouldCollide(bodyA) == false) {
  5085. var cNuke = c;
  5086. c = cNuke.GetNext();
  5087. this.Destroy(cNuke);
  5088. continue;
  5089. }
  5090. if (this.m_contactFilter.ShouldCollide(fixtureA, fixtureB) == false) {
  5091. cNuke = c;
  5092. c = cNuke.GetNext();
  5093. this.Destroy(cNuke);
  5094. continue;
  5095. }
  5096. c.m_flags &= ~b2Contact.e_filterFlag;
  5097. }
  5098. var proxyA = fixtureA.m_proxy;
  5099. var proxyB = fixtureB.m_proxy;
  5100. var overlap = this.m_broadPhase.TestOverlap(proxyA, proxyB);
  5101. if (overlap == false) {
  5102. cNuke = c;
  5103. c = cNuke.GetNext();
  5104. this.Destroy(cNuke);
  5105. continue;
  5106. }
  5107. c.Update(this.m_contactListener);
  5108. c = c.GetNext();
  5109. }
  5110. }
  5111. Box2D.postDefs.push(function () {
  5112. Box2D.Dynamics.b2ContactManager.s_evalCP = new b2ContactPoint();
  5113. });
  5114. b2DebugDraw.b2DebugDraw = function () {};
  5115. b2DebugDraw.prototype.b2DebugDraw = function () {}
  5116. b2DebugDraw.prototype.SetFlags = function (flags) {
  5117. if (flags === undefined) flags = 0;
  5118. }
  5119. b2DebugDraw.prototype.GetFlags = function () {}
  5120. b2DebugDraw.prototype.AppendFlags = function (flags) {
  5121. if (flags === undefined) flags = 0;
  5122. }
  5123. b2DebugDraw.prototype.ClearFlags = function (flags) {
  5124. if (flags === undefined) flags = 0;
  5125. }
  5126. b2DebugDraw.prototype.SetSprite = function (sprite) {}
  5127. b2DebugDraw.prototype.GetSprite = function () {}
  5128. b2DebugDraw.prototype.SetDrawScale = function (drawScale) {
  5129. if (drawScale === undefined) drawScale = 0;
  5130. }
  5131. b2DebugDraw.prototype.GetDrawScale = function () {}
  5132. b2DebugDraw.prototype.SetLineThickness = function (lineThickness) {
  5133. if (lineThickness === undefined) lineThickness = 0;
  5134. }
  5135. b2DebugDraw.prototype.GetLineThickness = function () {}
  5136. b2DebugDraw.prototype.SetAlpha = function (alpha) {
  5137. if (alpha === undefined) alpha = 0;
  5138. }
  5139. b2DebugDraw.prototype.GetAlpha = function () {}
  5140. b2DebugDraw.prototype.SetFillAlpha = function (alpha) {
  5141. if (alpha === undefined) alpha = 0;
  5142. }
  5143. b2DebugDraw.prototype.GetFillAlpha = function () {}
  5144. b2DebugDraw.prototype.SetXFormScale = function (xformScale) {
  5145. if (xformScale === undefined) xformScale = 0;
  5146. }
  5147. b2DebugDraw.prototype.GetXFormScale = function () {}
  5148. b2DebugDraw.prototype.DrawPolygon = function (vertices, vertexCount, color) {
  5149. if (vertexCount === undefined) vertexCount = 0;
  5150. }
  5151. b2DebugDraw.prototype.DrawSolidPolygon = function (vertices, vertexCount, color) {
  5152. if (vertexCount === undefined) vertexCount = 0;
  5153. }
  5154. b2DebugDraw.prototype.DrawCircle = function (center, radius, color) {
  5155. if (radius === undefined) radius = 0;
  5156. }
  5157. b2DebugDraw.prototype.DrawSolidCircle = function (center, radius, axis, color) {
  5158. if (radius === undefined) radius = 0;
  5159. }
  5160. b2DebugDraw.prototype.DrawSegment = function (p1, p2, color) {}
  5161. b2DebugDraw.prototype.DrawTransform = function (xf) {}
  5162. Box2D.postDefs.push(function () {
  5163. Box2D.Dynamics.b2DebugDraw.e_shapeBit = 0x0001;
  5164. Box2D.Dynamics.b2DebugDraw.e_jointBit = 0x0002;
  5165. Box2D.Dynamics.b2DebugDraw.e_aabbBit = 0x0004;
  5166. Box2D.Dynamics.b2DebugDraw.e_pairBit = 0x0008;
  5167. Box2D.Dynamics.b2DebugDraw.e_centerOfMassBit = 0x0010;
  5168. Box2D.Dynamics.b2DebugDraw.e_controllerBit = 0x0020;
  5169. });
  5170. b2DestructionListener.b2DestructionListener = function () {};
  5171. b2DestructionListener.prototype.SayGoodbyeJoint = function (joint) {}
  5172. b2DestructionListener.prototype.SayGoodbyeFixture = function (fixture) {}
  5173. b2FilterData.b2FilterData = function () {
  5174. this.categoryBits = 0x0001;
  5175. this.maskBits = 0xFFFF;
  5176. this.groupIndex = 0;
  5177. };
  5178. b2FilterData.prototype.Copy = function () {
  5179. var copy = new b2FilterData();
  5180. copy.categoryBits = this.categoryBits;
  5181. copy.maskBits = this.maskBits;
  5182. copy.groupIndex = this.groupIndex;
  5183. return copy;
  5184. }
  5185. b2Fixture.b2Fixture = function () {
  5186. this.m_filter = new b2FilterData();
  5187. };
  5188. b2Fixture.prototype.GetType = function () {
  5189. return this.m_shape.GetType();
  5190. }
  5191. b2Fixture.prototype.GetShape = function () {
  5192. return this.m_shape;
  5193. }
  5194. b2Fixture.prototype.SetSensor = function (sensor) {
  5195. if (this.m_isSensor == sensor) return;
  5196. this.m_isSensor = sensor;
  5197. if (this.m_body == null) return;
  5198. var edge = this.m_body.GetContactList();
  5199. while (edge) {
  5200. var contact = edge.contact;
  5201. var fixtureA = contact.GetFixtureA();
  5202. var fixtureB = contact.GetFixtureB();
  5203. if (fixtureA == this || fixtureB == this) contact.SetSensor(fixtureA.IsSensor() || fixtureB.IsSensor());
  5204. edge = edge.next;
  5205. }
  5206. }
  5207. b2Fixture.prototype.IsSensor = function () {
  5208. return this.m_isSensor;
  5209. }
  5210. b2Fixture.prototype.SetFilterData = function (filter) {
  5211. this.m_filter = filter.Copy();
  5212. if (this.m_body) return;
  5213. var edge = this.m_body.GetContactList();
  5214. while (edge) {
  5215. var contact = edge.contact;
  5216. var fixtureA = contact.GetFixtureA();
  5217. var fixtureB = contact.GetFixtureB();
  5218. if (fixtureA == this || fixtureB == this) contact.FlagForFiltering();
  5219. edge = edge.next;
  5220. }
  5221. }
  5222. b2Fixture.prototype.GetFilterData = function () {
  5223. return this.m_filter.Copy();
  5224. }
  5225. b2Fixture.prototype.GetBody = function () {
  5226. return this.m_body;
  5227. }
  5228. b2Fixture.prototype.GetNext = function () {
  5229. return this.m_next;
  5230. }
  5231. b2Fixture.prototype.GetUserData = function () {
  5232. return this.m_userData;
  5233. }
  5234. b2Fixture.prototype.SetUserData = function (data) {
  5235. this.m_userData = data;
  5236. }
  5237. b2Fixture.prototype.TestPoint = function (p) {
  5238. return this.m_shape.TestPoint(this.m_body.GetTransform(), p);
  5239. }
  5240. b2Fixture.prototype.RayCast = function (output, input) {
  5241. return this.m_shape.RayCast(output, input, this.m_body.GetTransform());
  5242. }
  5243. b2Fixture.prototype.GetMassData = function (massData) {
  5244. if (massData === undefined) massData = null;
  5245. if (massData == null) {
  5246. massData = new b2MassData();
  5247. }
  5248. this.m_shape.ComputeMass(massData, this.m_density);
  5249. return massData;
  5250. }
  5251. b2Fixture.prototype.SetDensity = function (density) {
  5252. if (density === undefined) density = 0;
  5253. this.m_density = density;
  5254. }
  5255. b2Fixture.prototype.GetDensity = function () {
  5256. return this.m_density;
  5257. }
  5258. b2Fixture.prototype.GetFriction = function () {
  5259. return this.m_friction;
  5260. }
  5261. b2Fixture.prototype.SetFriction = function (friction) {
  5262. if (friction === undefined) friction = 0;
  5263. this.m_friction = friction;
  5264. }
  5265. b2Fixture.prototype.GetRestitution = function () {
  5266. return this.m_restitution;
  5267. }
  5268. b2Fixture.prototype.SetRestitution = function (restitution) {
  5269. if (restitution === undefined) restitution = 0;
  5270. this.m_restitution = restitution;
  5271. }
  5272. b2Fixture.prototype.GetAABB = function () {
  5273. return this.m_aabb;
  5274. }
  5275. b2Fixture.prototype.b2Fixture = function () {
  5276. this.m_aabb = new b2AABB();
  5277. this.m_userData = null;
  5278. this.m_body = null;
  5279. this.m_next = null;
  5280. this.m_shape = null;
  5281. this.m_density = 0.0;
  5282. this.m_friction = 0.0;
  5283. this.m_restitution = 0.0;
  5284. }
  5285. b2Fixture.prototype.Create = function (body, xf, def) {
  5286. this.m_userData = def.userData;
  5287. this.m_friction = def.friction;
  5288. this.m_restitution = def.restitution;
  5289. this.m_body = body;
  5290. this.m_next = null;
  5291. this.m_filter = def.filter.Copy();
  5292. this.m_isSensor = def.isSensor;
  5293. this.m_shape = def.shape.Copy();
  5294. this.m_density = def.density;
  5295. }
  5296. b2Fixture.prototype.Destroy = function () {
  5297. this.m_shape = null;
  5298. }
  5299. b2Fixture.prototype.CreateProxy = function (broadPhase, xf) {
  5300. this.m_shape.ComputeAABB(this.m_aabb, xf);
  5301. this.m_proxy = broadPhase.CreateProxy(this.m_aabb, this);
  5302. }
  5303. b2Fixture.prototype.DestroyProxy = function (broadPhase) {
  5304. if (this.m_proxy == null) {
  5305. return;
  5306. }
  5307. broadPhase.DestroyProxy(this.m_proxy);
  5308. this.m_proxy = null;
  5309. }
  5310. b2Fixture.prototype.Synchronize = function (broadPhase, transform1, transform2) {
  5311. if (!this.m_proxy) return;
  5312. var aabb1 = new b2AABB();
  5313. var aabb2 = new b2AABB();
  5314. this.m_shape.ComputeAABB(aabb1, transform1);
  5315. this.m_shape.ComputeAABB(aabb2, transform2);
  5316. this.m_aabb.Combine(aabb1, aabb2);
  5317. var displacement = b2Math.SubtractVV(transform2.position, transform1.position);
  5318. broadPhase.MoveProxy(this.m_proxy, this.m_aabb, displacement);
  5319. }
  5320. b2FixtureDef.b2FixtureDef = function () {
  5321. this.filter = new b2FilterData();
  5322. };
  5323. b2FixtureDef.prototype.b2FixtureDef = function () {
  5324. this.shape = null;
  5325. this.userData = null;
  5326. this.friction = 0.2;
  5327. this.restitution = 0.0;
  5328. this.density = 0.0;
  5329. this.filter.categoryBits = 0x0001;
  5330. this.filter.maskBits = 0xFFFF;
  5331. this.filter.groupIndex = 0;
  5332. this.isSensor = false;
  5333. }
  5334. b2Island.b2Island = function () {};
  5335. b2Island.prototype.b2Island = function () {
  5336. this.m_bodies = new Vector();
  5337. this.m_contacts = new Vector();
  5338. this.m_joints = new Vector();
  5339. }
  5340. b2Island.prototype.Initialize = function (bodyCapacity, contactCapacity, jointCapacity, allocator, listener, contactSolver) {
  5341. if (bodyCapacity === undefined) bodyCapacity = 0;
  5342. if (contactCapacity === undefined) contactCapacity = 0;
  5343. if (jointCapacity === undefined) jointCapacity = 0;
  5344. var i = 0;
  5345. this.m_bodyCapacity = bodyCapacity;
  5346. this.m_contactCapacity = contactCapacity;
  5347. this.m_jointCapacity = jointCapacity;
  5348. this.m_bodyCount = 0;
  5349. this.m_contactCount = 0;
  5350. this.m_jointCount = 0;
  5351. this.m_allocator = allocator;
  5352. this.m_listener = listener;
  5353. this.m_contactSolver = contactSolver;
  5354. for (i = this.m_bodies.length;
  5355. i < bodyCapacity; i++)
  5356. this.m_bodies[i] = null;
  5357. for (i = this.m_contacts.length;
  5358. i < contactCapacity; i++)
  5359. this.m_contacts[i] = null;
  5360. for (i = this.m_joints.length;
  5361. i < jointCapacity; i++)
  5362. this.m_joints[i] = null;
  5363. }
  5364. b2Island.prototype.Clear = function () {
  5365. this.m_bodyCount = 0;
  5366. this.m_contactCount = 0;
  5367. this.m_jointCount = 0;
  5368. }
  5369. b2Island.prototype.Solve = function (step, gravity, allowSleep) {
  5370. var i = 0;
  5371. var j = 0;
  5372. var b;
  5373. var joint;
  5374. for (i = 0;
  5375. i < this.m_bodyCount; ++i) {
  5376. b = this.m_bodies[i];
  5377. if (b.GetType() != b2Body.b2_dynamicBody) continue;
  5378. b.m_linearVelocity.x += step.dt * (gravity.x + b.m_invMass * b.m_force.x);
  5379. b.m_linearVelocity.y += step.dt * (gravity.y + b.m_invMass * b.m_force.y);
  5380. b.m_angularVelocity += step.dt * b.m_invI * b.m_torque;
  5381. b.m_linearVelocity.Multiply(b2Math.Clamp(1.0 - step.dt * b.m_linearDamping, 0.0, 1.0));
  5382. b.m_angularVelocity *= b2Math.Clamp(1.0 - step.dt * b.m_angularDamping, 0.0, 1.0);
  5383. }
  5384. this.m_contactSolver.Initialize(step, this.m_contacts, this.m_contactCount, this.m_allocator);
  5385. var contactSolver = this.m_contactSolver;
  5386. contactSolver.InitVelocityConstraints(step);
  5387. for (i = 0;
  5388. i < this.m_jointCount; ++i) {
  5389. joint = this.m_joints[i];
  5390. joint.InitVelocityConstraints(step);
  5391. }
  5392. for (i = 0;
  5393. i < step.velocityIterations; ++i) {
  5394. for (j = 0;
  5395. j < this.m_jointCount; ++j) {
  5396. joint = this.m_joints[j];
  5397. joint.SolveVelocityConstraints(step);
  5398. }
  5399. contactSolver.SolveVelocityConstraints();
  5400. }
  5401. for (i = 0;
  5402. i < this.m_jointCount; ++i) {
  5403. joint = this.m_joints[i];
  5404. joint.FinalizeVelocityConstraints();
  5405. }
  5406. contactSolver.FinalizeVelocityConstraints();
  5407. for (i = 0;
  5408. i < this.m_bodyCount; ++i) {
  5409. b = this.m_bodies[i];
  5410. if (b.GetType() == b2Body.b2_staticBody) continue;
  5411. var translationX = step.dt * b.m_linearVelocity.x;
  5412. var translationY = step.dt * b.m_linearVelocity.y;
  5413. if ((translationX * translationX + translationY * translationY) > b2Settings.b2_maxTranslationSquared) {
  5414. b.m_linearVelocity.Normalize();
  5415. b.m_linearVelocity.x *= b2Settings.b2_maxTranslation * step.inv_dt;
  5416. b.m_linearVelocity.y *= b2Settings.b2_maxTranslation * step.inv_dt;
  5417. }
  5418. var rotation = step.dt * b.m_angularVelocity;
  5419. if (rotation * rotation > b2Settings.b2_maxRotationSquared) {
  5420. if (b.m_angularVelocity < 0.0) {
  5421. b.m_angularVelocity = (-b2Settings.b2_maxRotation * step.inv_dt);
  5422. }
  5423. else {
  5424. b.m_angularVelocity = b2Settings.b2_maxRotation * step.inv_dt;
  5425. }
  5426. }
  5427. b.m_sweep.c0.SetV(b.m_sweep.c);
  5428. b.m_sweep.a0 = b.m_sweep.a;
  5429. b.m_sweep.c.x += step.dt * b.m_linearVelocity.x;
  5430. b.m_sweep.c.y += step.dt * b.m_linearVelocity.y;
  5431. b.m_sweep.a += step.dt * b.m_angularVelocity;
  5432. b.SynchronizeTransform();
  5433. }
  5434. for (i = 0;
  5435. i < step.positionIterations; ++i) {
  5436. var contactsOkay = contactSolver.SolvePositionConstraints(b2Settings.b2_contactBaumgarte);
  5437. var jointsOkay = true;
  5438. for (j = 0;
  5439. j < this.m_jointCount; ++j) {
  5440. joint = this.m_joints[j];
  5441. var jointOkay = joint.SolvePositionConstraints(b2Settings.b2_contactBaumgarte);
  5442. jointsOkay = jointsOkay && jointOkay;
  5443. }
  5444. if (contactsOkay && jointsOkay) {
  5445. break;
  5446. }
  5447. }
  5448. this.Report(contactSolver.m_constraints);
  5449. if (allowSleep) {
  5450. var minSleepTime = Number.MAX_VALUE;
  5451. var linTolSqr = b2Settings.b2_linearSleepTolerance * b2Settings.b2_linearSleepTolerance;
  5452. var angTolSqr = b2Settings.b2_angularSleepTolerance * b2Settings.b2_angularSleepTolerance;
  5453. for (i = 0;
  5454. i < this.m_bodyCount; ++i) {
  5455. b = this.m_bodies[i];
  5456. if (b.GetType() == b2Body.b2_staticBody) {
  5457. continue;
  5458. }
  5459. if ((b.m_flags & b2Body.e_allowSleepFlag) == 0) {
  5460. b.m_sleepTime = 0.0;
  5461. minSleepTime = 0.0;
  5462. }
  5463. if ((b.m_flags & b2Body.e_allowSleepFlag) == 0 || b.m_angularVelocity * b.m_angularVelocity > angTolSqr || b2Math.Dot(b.m_linearVelocity, b.m_linearVelocity) > linTolSqr) {
  5464. b.m_sleepTime = 0.0;
  5465. minSleepTime = 0.0;
  5466. }
  5467. else {
  5468. b.m_sleepTime += step.dt;
  5469. minSleepTime = b2Math.Min(minSleepTime, b.m_sleepTime);
  5470. }
  5471. }
  5472. if (minSleepTime >= b2Settings.b2_timeToSleep) {
  5473. for (i = 0;
  5474. i < this.m_bodyCount; ++i) {
  5475. b = this.m_bodies[i];
  5476. b.SetAwake(false);
  5477. }
  5478. }
  5479. }
  5480. }
  5481. b2Island.prototype.SolveTOI = function (subStep) {
  5482. var i = 0;
  5483. var j = 0;
  5484. this.m_contactSolver.Initialize(subStep, this.m_contacts, this.m_contactCount, this.m_allocator);
  5485. var contactSolver = this.m_contactSolver;
  5486. for (i = 0;
  5487. i < this.m_jointCount; ++i) {
  5488. this.m_joints[i].InitVelocityConstraints(subStep);
  5489. }
  5490. for (i = 0;
  5491. i < subStep.velocityIterations; ++i) {
  5492. contactSolver.SolveVelocityConstraints();
  5493. for (j = 0;
  5494. j < this.m_jointCount; ++j) {
  5495. this.m_joints[j].SolveVelocityConstraints(subStep);
  5496. }
  5497. }
  5498. for (i = 0;
  5499. i < this.m_bodyCount; ++i) {
  5500. var b = this.m_bodies[i];
  5501. if (b.GetType() == b2Body.b2_staticBody) continue;
  5502. var translationX = subStep.dt * b.m_linearVelocity.x;
  5503. var translationY = subStep.dt * b.m_linearVelocity.y;
  5504. if ((translationX * translationX + translationY * translationY) > b2Settings.b2_maxTranslationSquared) {
  5505. b.m_linearVelocity.Normalize();
  5506. b.m_linearVelocity.x *= b2Settings.b2_maxTranslation * subStep.inv_dt;
  5507. b.m_linearVelocity.y *= b2Settings.b2_maxTranslation * subStep.inv_dt;
  5508. }
  5509. var rotation = subStep.dt * b.m_angularVelocity;
  5510. if (rotation * rotation > b2Settings.b2_maxRotationSquared) {
  5511. if (b.m_angularVelocity < 0.0) {
  5512. b.m_angularVelocity = (-b2Settings.b2_maxRotation * subStep.inv_dt);
  5513. }
  5514. else {
  5515. b.m_angularVelocity = b2Settings.b2_maxRotation * subStep.inv_dt;
  5516. }
  5517. }
  5518. b.m_sweep.c0.SetV(b.m_sweep.c);
  5519. b.m_sweep.a0 = b.m_sweep.a;
  5520. b.m_sweep.c.x += subStep.dt * b.m_linearVelocity.x;
  5521. b.m_sweep.c.y += subStep.dt * b.m_linearVelocity.y;
  5522. b.m_sweep.a += subStep.dt * b.m_angularVelocity;
  5523. b.SynchronizeTransform();
  5524. }
  5525. var k_toiBaumgarte = 0.75;
  5526. for (i = 0;
  5527. i < subStep.positionIterations; ++i) {
  5528. var contactsOkay = contactSolver.SolvePositionConstraints(k_toiBaumgarte);
  5529. var jointsOkay = true;
  5530. for (j = 0;
  5531. j < this.m_jointCount; ++j) {
  5532. var jointOkay = this.m_joints[j].SolvePositionConstraints(b2Settings.b2_contactBaumgarte);
  5533. jointsOkay = jointsOkay && jointOkay;
  5534. }
  5535. if (contactsOkay && jointsOkay) {
  5536. break;
  5537. }
  5538. }
  5539. this.Report(contactSolver.m_constraints);
  5540. }
  5541. b2Island.prototype.Report = function (constraints) {
  5542. if (this.m_listener == null) {
  5543. return;
  5544. }
  5545. for (var i = 0; i < this.m_contactCount; ++i) {
  5546. var c = this.m_contacts[i];
  5547. var cc = constraints[i];
  5548. for (var j = 0; j < cc.pointCount; ++j) {
  5549. b2Island.s_impulse.normalImpulses[j] = cc.points[j].normalImpulse;
  5550. b2Island.s_impulse.tangentImpulses[j] = cc.points[j].tangentImpulse;
  5551. }
  5552. this.m_listener.PostSolve(c, b2Island.s_impulse);
  5553. }
  5554. }
  5555. b2Island.prototype.AddBody = function (body) {
  5556. body.m_islandIndex = this.m_bodyCount;
  5557. this.m_bodies[this.m_bodyCount++] = body;
  5558. }
  5559. b2Island.prototype.AddContact = function (contact) {
  5560. this.m_contacts[this.m_contactCount++] = contact;
  5561. }
  5562. b2Island.prototype.AddJoint = function (joint) {
  5563. this.m_joints[this.m_jointCount++] = joint;
  5564. }
  5565. Box2D.postDefs.push(function () {
  5566. Box2D.Dynamics.b2Island.s_impulse = new b2ContactImpulse();
  5567. });
  5568. b2TimeStep.b2TimeStep = function () {};
  5569. b2TimeStep.prototype.Set = function (step) {
  5570. this.dt = step.dt;
  5571. this.inv_dt = step.inv_dt;
  5572. this.positionIterations = step.positionIterations;
  5573. this.velocityIterations = step.velocityIterations;
  5574. this.warmStarting = step.warmStarting;
  5575. }
  5576. b2World.b2World = function () {
  5577. this.s_stack = new Vector();
  5578. this.m_contactManager = new b2ContactManager();
  5579. this.m_contactSolver = new b2ContactSolver();
  5580. this.m_island = new b2Island();
  5581. };
  5582. b2World.prototype.b2World = function (gravity, doSleep) {
  5583. this.m_destructionListener = null;
  5584. this.m_debugDraw = null;
  5585. this.m_bodyList = null;
  5586. this.m_contactList = null;
  5587. this.m_jointList = null;
  5588. this.m_controllerList = null;
  5589. this.m_bodyCount = 0;
  5590. this.m_contactCount = 0;
  5591. this.m_jointCount = 0;
  5592. this.m_controllerCount = 0;
  5593. b2World.m_warmStarting = true;
  5594. b2World.m_continuousPhysics = true;
  5595. this.m_allowSleep = doSleep;
  5596. this.m_gravity = gravity;
  5597. this.m_inv_dt0 = 0.0;
  5598. this.m_contactManager.m_world = this;
  5599. var bd = new b2BodyDef();
  5600. this.m_groundBody = this.CreateBody(bd);
  5601. }
  5602. b2World.prototype.SetDestructionListener = function (listener) {
  5603. this.m_destructionListener = listener;
  5604. }
  5605. b2World.prototype.SetContactFilter = function (filter) {
  5606. this.m_contactManager.m_contactFilter = filter;
  5607. }
  5608. b2World.prototype.SetContactListener = function (listener) {
  5609. this.m_contactManager.m_contactListener = listener;
  5610. }
  5611. b2World.prototype.SetDebugDraw = function (debugDraw) {
  5612. this.m_debugDraw = debugDraw;
  5613. }
  5614. b2World.prototype.SetBroadPhase = function (broadPhase) {
  5615. var oldBroadPhase = this.m_contactManager.m_broadPhase;
  5616. this.m_contactManager.m_broadPhase = broadPhase;
  5617. for (var b = this.m_bodyList; b; b = b.m_next) {
  5618. for (var f = b.m_fixtureList; f; f = f.m_next) {
  5619. f.m_proxy = broadPhase.CreateProxy(oldBroadPhase.GetFatAABB(f.m_proxy), f);
  5620. }
  5621. }
  5622. }
  5623. b2World.prototype.Validate = function () {
  5624. this.m_contactManager.m_broadPhase.Validate();
  5625. }
  5626. b2World.prototype.GetProxyCount = function () {
  5627. return this.m_contactManager.m_broadPhase.GetProxyCount();
  5628. }
  5629. b2World.prototype.CreateBody = function (def) {
  5630. if (this.IsLocked() == true) {
  5631. return null;
  5632. }
  5633. var b = new b2Body(def, this);
  5634. b.m_prev = null;
  5635. b.m_next = this.m_bodyList;
  5636. if (this.m_bodyList) {
  5637. this.m_bodyList.m_prev = b;
  5638. }
  5639. this.m_bodyList = b;
  5640. ++this.m_bodyCount;
  5641. return b;
  5642. }
  5643. b2World.prototype.DestroyBody = function (b) {
  5644. if (this.IsLocked() == true) {
  5645. return;
  5646. }
  5647. var jn = b.m_jointList;
  5648. while (jn) {
  5649. var jn0 = jn;
  5650. jn = jn.next;
  5651. if (this.m_destructionListener) {
  5652. this.m_destructionListener.SayGoodbyeJoint(jn0.joint);
  5653. }
  5654. this.DestroyJoint(jn0.joint);
  5655. }
  5656. var coe = b.m_controllerList;
  5657. while (coe) {
  5658. var coe0 = coe;
  5659. coe = coe.nextController;
  5660. coe0.controller.RemoveBody(b);
  5661. }
  5662. var ce = b.m_contactList;
  5663. while (ce) {
  5664. var ce0 = ce;
  5665. ce = ce.next;
  5666. this.m_contactManager.Destroy(ce0.contact);
  5667. }
  5668. b.m_contactList = null;
  5669. var f = b.m_fixtureList;
  5670. while (f) {
  5671. var f0 = f;
  5672. f = f.m_next;
  5673. if (this.m_destructionListener) {
  5674. this.m_destructionListener.SayGoodbyeFixture(f0);
  5675. }
  5676. f0.DestroyProxy(this.m_contactManager.m_broadPhase);
  5677. f0.Destroy();
  5678. }
  5679. b.m_fixtureList = null;
  5680. b.m_fixtureCount = 0;
  5681. if (b.m_prev) {
  5682. b.m_prev.m_next = b.m_next;
  5683. }
  5684. if (b.m_next) {
  5685. b.m_next.m_prev = b.m_prev;
  5686. }
  5687. if (b == this.m_bodyList) {
  5688. this.m_bodyList = b.m_next;
  5689. }--this.m_bodyCount;
  5690. }
  5691. b2World.prototype.CreateJoint = function (def) {
  5692. var j = b2Joint.Create(def, null);
  5693. j.m_prev = null;
  5694. j.m_next = this.m_jointList;
  5695. if (this.m_jointList) {
  5696. this.m_jointList.m_prev = j;
  5697. }
  5698. this.m_jointList = j;
  5699. ++this.m_jointCount;
  5700. j.m_edgeA.joint = j;
  5701. j.m_edgeA.other = j.m_bodyB;
  5702. j.m_edgeA.prev = null;
  5703. j.m_edgeA.next = j.m_bodyA.m_jointList;
  5704. if (j.m_bodyA.m_jointList) j.m_bodyA.m_jointList.prev = j.m_edgeA;
  5705. j.m_bodyA.m_jointList = j.m_edgeA;
  5706. j.m_edgeB.joint = j;
  5707. j.m_edgeB.other = j.m_bodyA;
  5708. j.m_edgeB.prev = null;
  5709. j.m_edgeB.next = j.m_bodyB.m_jointList;
  5710. if (j.m_bodyB.m_jointList) j.m_bodyB.m_jointList.prev = j.m_edgeB;
  5711. j.m_bodyB.m_jointList = j.m_edgeB;
  5712. var bodyA = def.bodyA;
  5713. var bodyB = def.bodyB;
  5714. if (def.collideConnected == false) {
  5715. var edge = bodyB.GetContactList();
  5716. while (edge) {
  5717. if (edge.other == bodyA) {
  5718. edge.contact.FlagForFiltering();
  5719. }
  5720. edge = edge.next;
  5721. }
  5722. }
  5723. return j;
  5724. }
  5725. b2World.prototype.DestroyJoint = function (j) {
  5726. var collideConnected = j.m_collideConnected;
  5727. if (j.m_prev) {
  5728. j.m_prev.m_next = j.m_next;
  5729. }
  5730. if (j.m_next) {
  5731. j.m_next.m_prev = j.m_prev;
  5732. }
  5733. if (j == this.m_jointList) {
  5734. this.m_jointList = j.m_next;
  5735. }
  5736. var bodyA = j.m_bodyA;
  5737. var bodyB = j.m_bodyB;
  5738. bodyA.SetAwake(true);
  5739. bodyB.SetAwake(true);
  5740. if (j.m_edgeA.prev) {
  5741. j.m_edgeA.prev.next = j.m_edgeA.next;
  5742. }
  5743. if (j.m_edgeA.next) {
  5744. j.m_edgeA.next.prev = j.m_edgeA.prev;
  5745. }
  5746. if (j.m_edgeA == bodyA.m_jointList) {
  5747. bodyA.m_jointList = j.m_edgeA.next;
  5748. }
  5749. j.m_edgeA.prev = null;
  5750. j.m_edgeA.next = null;
  5751. if (j.m_edgeB.prev) {
  5752. j.m_edgeB.prev.next = j.m_edgeB.next;
  5753. }
  5754. if (j.m_edgeB.next) {
  5755. j.m_edgeB.next.prev = j.m_edgeB.prev;
  5756. }
  5757. if (j.m_edgeB == bodyB.m_jointList) {
  5758. bodyB.m_jointList = j.m_edgeB.next;
  5759. }
  5760. j.m_edgeB.prev = null;
  5761. j.m_edgeB.next = null;
  5762. b2Joint.Destroy(j, null);
  5763. --this.m_jointCount;
  5764. if (collideConnected == false) {
  5765. var edge = bodyB.GetContactList();
  5766. while (edge) {
  5767. if (edge.other == bodyA) {
  5768. edge.contact.FlagForFiltering();
  5769. }
  5770. edge = edge.next;
  5771. }
  5772. }
  5773. }
  5774. b2World.prototype.AddController = function (c) {
  5775. c.m_next = this.m_controllerList;
  5776. c.m_prev = null;
  5777. this.m_controllerList = c;
  5778. c.m_world = this;
  5779. this.m_controllerCount++;
  5780. return c;
  5781. }
  5782. b2World.prototype.RemoveController = function (c) {
  5783. if (c.m_prev) c.m_prev.m_next = c.m_next;
  5784. if (c.m_next) c.m_next.m_prev = c.m_prev;
  5785. if (this.m_controllerList == c) this.m_controllerList = c.m_next;
  5786. this.m_controllerCount--;
  5787. }
  5788. b2World.prototype.CreateController = function (controller) {
  5789. if (controller.m_world != this) throw new Error("Controller can only be a member of one world");
  5790. controller.m_next = this.m_controllerList;
  5791. controller.m_prev = null;
  5792. if (this.m_controllerList) this.m_controllerList.m_prev = controller;
  5793. this.m_controllerList = controller;
  5794. ++this.m_controllerCount;
  5795. controller.m_world = this;
  5796. return controller;
  5797. }
  5798. b2World.prototype.DestroyController = function (controller) {
  5799. controller.Clear();
  5800. if (controller.m_next) controller.m_next.m_prev = controller.m_prev;
  5801. if (controller.m_prev) controller.m_prev.m_next = controller.m_next;
  5802. if (controller == this.m_controllerList) this.m_controllerList = controller.m_next;
  5803. --this.m_controllerCount;
  5804. }
  5805. b2World.prototype.SetWarmStarting = function (flag) {
  5806. b2World.m_warmStarting = flag;
  5807. }
  5808. b2World.prototype.SetContinuousPhysics = function (flag) {
  5809. b2World.m_continuousPhysics = flag;
  5810. }
  5811. b2World.prototype.GetBodyCount = function () {
  5812. return this.m_bodyCount;
  5813. }
  5814. b2World.prototype.GetJointCount = function () {
  5815. return this.m_jointCount;
  5816. }
  5817. b2World.prototype.GetContactCount = function () {
  5818. return this.m_contactCount;
  5819. }
  5820. b2World.prototype.SetGravity = function (gravity) {
  5821. this.m_gravity = gravity;
  5822. }
  5823. b2World.prototype.GetGravity = function () {
  5824. return this.m_gravity;
  5825. }
  5826. b2World.prototype.GetGroundBody = function () {
  5827. return this.m_groundBody;
  5828. }
  5829. b2World.prototype.Step = function (dt, velocityIterations, positionIterations) {
  5830. if (dt === undefined) dt = 0;
  5831. if (velocityIterations === undefined) velocityIterations = 0;
  5832. if (positionIterations === undefined) positionIterations = 0;
  5833. if (this.m_flags & b2World.e_newFixture) {
  5834. this.m_contactManager.FindNewContacts();
  5835. this.m_flags &= ~b2World.e_newFixture;
  5836. }
  5837. this.m_flags |= b2World.e_locked;
  5838. var step = b2World.s_timestep2;
  5839. step.dt = dt;
  5840. step.velocityIterations = velocityIterations;
  5841. step.positionIterations = positionIterations;
  5842. if (dt > 0.0) {
  5843. step.inv_dt = 1.0 / dt;
  5844. }
  5845. else {
  5846. step.inv_dt = 0.0;
  5847. }
  5848. step.dtRatio = this.m_inv_dt0 * dt;
  5849. step.warmStarting = b2World.m_warmStarting;
  5850. this.m_contactManager.Collide();
  5851. if (step.dt > 0.0) {
  5852. this.Solve(step);
  5853. }
  5854. if (b2World.m_continuousPhysics && step.dt > 0.0) {
  5855. this.SolveTOI(step);
  5856. }
  5857. if (step.dt > 0.0) {
  5858. this.m_inv_dt0 = step.inv_dt;
  5859. }
  5860. this.m_flags &= ~b2World.e_locked;
  5861. }
  5862. b2World.prototype.ClearForces = function () {
  5863. for (var body = this.m_bodyList; body; body = body.m_next) {
  5864. body.m_force.SetZero();
  5865. body.m_torque = 0.0;
  5866. }
  5867. }
  5868. b2World.prototype.DrawDebugData = function () {
  5869. if (this.m_debugDraw == null) {
  5870. return;
  5871. }
  5872. this.m_debugDraw.m_sprite.graphics.clear();
  5873. var flags = this.m_debugDraw.GetFlags();
  5874. var i = 0;
  5875. var b;
  5876. var f;
  5877. var s;
  5878. var j;
  5879. var bp;
  5880. var invQ = new b2Vec2;
  5881. var x1 = new b2Vec2;
  5882. var x2 = new b2Vec2;
  5883. var xf;
  5884. var b1 = new b2AABB();
  5885. var b2 = new b2AABB();
  5886. var vs = [new b2Vec2(), new b2Vec2(), new b2Vec2(), new b2Vec2()];
  5887. var color = new b2Color(0, 0, 0);
  5888. if (flags & b2DebugDraw.e_shapeBit) {
  5889. for (b = this.m_bodyList;
  5890. b; b = b.m_next) {
  5891. xf = b.m_xf;
  5892. for (f = b.GetFixtureList();
  5893. f; f = f.m_next) {
  5894. s = f.GetShape();
  5895. if (b.IsActive() == false) {
  5896. color.Set(0.5, 0.5, 0.3);
  5897. this.DrawShape(s, xf, color);
  5898. }
  5899. else if (b.GetType() == b2Body.b2_staticBody) {
  5900. color.Set(0.5, 0.9, 0.5);
  5901. this.DrawShape(s, xf, color);
  5902. }
  5903. else if (b.GetType() == b2Body.b2_kinematicBody) {
  5904. color.Set(0.5, 0.5, 0.9);
  5905. this.DrawShape(s, xf, color);
  5906. }
  5907. else if (b.IsAwake() == false) {
  5908. color.Set(0.6, 0.6, 0.6);
  5909. this.DrawShape(s, xf, color);
  5910. }
  5911. else {
  5912. color.Set(0.9, 0.7, 0.7);
  5913. this.DrawShape(s, xf, color);
  5914. }
  5915. }
  5916. }
  5917. }
  5918. if (flags & b2DebugDraw.e_jointBit) {
  5919. for (j = this.m_jointList;
  5920. j; j = j.m_next) {
  5921. this.DrawJoint(j);
  5922. }
  5923. }
  5924. if (flags & b2DebugDraw.e_controllerBit) {
  5925. for (var c = this.m_controllerList; c; c = c.m_next) {
  5926. c.Draw(this.m_debugDraw);
  5927. }
  5928. }
  5929. if (flags & b2DebugDraw.e_pairBit) {
  5930. color.Set(0.3, 0.9, 0.9);
  5931. for (var contact = this.m_contactManager.m_contactList; contact; contact = contact.GetNext()) {
  5932. var fixtureA = contact.GetFixtureA();
  5933. var fixtureB = contact.GetFixtureB();
  5934. var cA = fixtureA.GetAABB().GetCenter();
  5935. var cB = fixtureB.GetAABB().GetCenter();
  5936. this.m_debugDraw.DrawSegment(cA, cB, color);
  5937. }
  5938. }
  5939. if (flags & b2DebugDraw.e_aabbBit) {
  5940. bp = this.m_contactManager.m_broadPhase;
  5941. vs = [new b2Vec2(), new b2Vec2(), new b2Vec2(), new b2Vec2()];
  5942. for (b = this.m_bodyList;
  5943. b; b = b.GetNext()) {
  5944. if (b.IsActive() == false) {
  5945. continue;
  5946. }
  5947. for (f = b.GetFixtureList();
  5948. f; f = f.GetNext()) {
  5949. var aabb = bp.GetFatAABB(f.m_proxy);
  5950. vs[0].Set(aabb.lowerBound.x, aabb.lowerBound.y);
  5951. vs[1].Set(aabb.upperBound.x, aabb.lowerBound.y);
  5952. vs[2].Set(aabb.upperBound.x, aabb.upperBound.y);
  5953. vs[3].Set(aabb.lowerBound.x, aabb.upperBound.y);
  5954. this.m_debugDraw.DrawPolygon(vs, 4, color);
  5955. }
  5956. }
  5957. }
  5958. if (flags & b2DebugDraw.e_centerOfMassBit) {
  5959. for (b = this.m_bodyList;
  5960. b; b = b.m_next) {
  5961. xf = b2World.s_xf;
  5962. xf.R = b.m_xf.R;
  5963. xf.position = b.GetWorldCenter();
  5964. this.m_debugDraw.DrawTransform(xf);
  5965. }
  5966. }
  5967. }
  5968. b2World.prototype.QueryAABB = function (callback, aabb) {
  5969. var __this = this;
  5970. var broadPhase = __this.m_contactManager.m_broadPhase;
  5971. function WorldQueryWrapper(proxy) {
  5972. return callback(broadPhase.GetUserData(proxy));
  5973. };
  5974. broadPhase.Query(WorldQueryWrapper, aabb);
  5975. }
  5976. b2World.prototype.QueryShape = function (callback, shape, transform) {
  5977. var __this = this;
  5978. if (transform === undefined) transform = null;
  5979. if (transform == null) {
  5980. transform = new b2Transform();
  5981. transform.SetIdentity();
  5982. }
  5983. var broadPhase = __this.m_contactManager.m_broadPhase;
  5984. function WorldQueryWrapper(proxy) {
  5985. var fixture = (broadPhase.GetUserData(proxy) instanceof b2Fixture ? broadPhase.GetUserData(proxy) : null);
  5986. if (b2Shape.TestOverlap(shape, transform, fixture.GetShape(), fixture.GetBody().GetTransform())) return callback(fixture);
  5987. return true;
  5988. };
  5989. var aabb = new b2AABB();
  5990. shape.ComputeAABB(aabb, transform);
  5991. broadPhase.Query(WorldQueryWrapper, aabb);
  5992. }
  5993. b2World.prototype.QueryPoint = function (callback, p) {
  5994. var __this = this;
  5995. var broadPhase = __this.m_contactManager.m_broadPhase;
  5996. function WorldQueryWrapper(proxy) {
  5997. var fixture = (broadPhase.GetUserData(proxy) instanceof b2Fixture ? broadPhase.GetUserData(proxy) : null);
  5998. if (fixture.TestPoint(p)) return callback(fixture);
  5999. return true;
  6000. };
  6001. var aabb = new b2AABB();
  6002. aabb.lowerBound.Set(p.x - b2Settings.b2_linearSlop, p.y - b2Settings.b2_linearSlop);
  6003. aabb.upperBound.Set(p.x + b2Settings.b2_linearSlop, p.y + b2Settings.b2_linearSlop);
  6004. broadPhase.Query(WorldQueryWrapper, aabb);
  6005. }
  6006. b2World.prototype.RayCast = function (callback, point1, point2) {
  6007. var __this = this;
  6008. var broadPhase = __this.m_contactManager.m_broadPhase;
  6009. var output = new b2RayCastOutput;
  6010. function RayCastWrapper(input, proxy) {
  6011. var userData = broadPhase.GetUserData(proxy);
  6012. var fixture = (userData instanceof b2Fixture ? userData : null);
  6013. var hit = fixture.RayCast(output, input);
  6014. if (hit) {
  6015. var fraction = output.fraction;
  6016. var point = new b2Vec2((1.0 - fraction) * point1.x + fraction * point2.x, (1.0 - fraction) * point1.y + fraction * point2.y);
  6017. return callback(fixture, point, output.normal, fraction);
  6018. }
  6019. return input.maxFraction;
  6020. };
  6021. var input = new b2RayCastInput(point1, point2);
  6022. broadPhase.RayCast(RayCastWrapper, input);
  6023. }
  6024. b2World.prototype.RayCastOne = function (point1, point2) {
  6025. var __this = this;
  6026. var result;
  6027. function RayCastOneWrapper(fixture, point, normal, fraction) {
  6028. if (fraction === undefined) fraction = 0;
  6029. result = fixture;
  6030. return fraction;
  6031. };
  6032. __this.RayCast(RayCastOneWrapper, point1, point2);
  6033. return result;
  6034. }
  6035. b2World.prototype.RayCastAll = function (point1, point2) {
  6036. var __this = this;
  6037. var result = new Vector();
  6038. function RayCastAllWrapper(fixture, point, normal, fraction) {
  6039. if (fraction === undefined) fraction = 0;
  6040. result[result.length] = fixture;
  6041. return 1;
  6042. };
  6043. __this.RayCast(RayCastAllWrapper, point1, point2);
  6044. return result;
  6045. }
  6046. b2World.prototype.GetBodyList = function () {
  6047. return this.m_bodyList;
  6048. }
  6049. b2World.prototype.GetJointList = function () {
  6050. return this.m_jointList;
  6051. }
  6052. b2World.prototype.GetContactList = function () {
  6053. return this.m_contactList;
  6054. }
  6055. b2World.prototype.IsLocked = function () {
  6056. return (this.m_flags & b2World.e_locked) > 0;
  6057. }
  6058. b2World.prototype.Solve = function (step) {
  6059. var b;
  6060. for (var controller = this.m_controllerList; controller; controller = controller.m_next) {
  6061. controller.Step(step);
  6062. }
  6063. var island = this.m_island;
  6064. island.Initialize(this.m_bodyCount, this.m_contactCount, this.m_jointCount, null, this.m_contactManager.m_contactListener, this.m_contactSolver);
  6065. for (b = this.m_bodyList;
  6066. b; b = b.m_next) {
  6067. b.m_flags &= ~b2Body.e_islandFlag;
  6068. }
  6069. for (var c = this.m_contactList; c; c = c.m_next) {
  6070. c.m_flags &= ~b2Contact.e_islandFlag;
  6071. }
  6072. for (var j = this.m_jointList; j; j = j.m_next) {
  6073. j.m_islandFlag = false;
  6074. }
  6075. var stackSize = parseInt(this.m_bodyCount);
  6076. var stack = this.s_stack;
  6077. for (var seed = this.m_bodyList; seed; seed = seed.m_next) {
  6078. if (seed.m_flags & b2Body.e_islandFlag) {
  6079. continue;
  6080. }
  6081. if (seed.IsAwake() == false || seed.IsActive() == false) {
  6082. continue;
  6083. }
  6084. if (seed.GetType() == b2Body.b2_staticBody) {
  6085. continue;
  6086. }
  6087. island.Clear();
  6088. var stackCount = 0;
  6089. stack[stackCount++] = seed;
  6090. seed.m_flags |= b2Body.e_islandFlag;
  6091. while (stackCount > 0) {
  6092. b = stack[--stackCount];
  6093. island.AddBody(b);
  6094. if (b.IsAwake() == false) {
  6095. b.SetAwake(true);
  6096. }
  6097. if (b.GetType() == b2Body.b2_staticBody) {
  6098. continue;
  6099. }
  6100. var other;
  6101. for (var ce = b.m_contactList; ce; ce = ce.next) {
  6102. if (ce.contact.m_flags & b2Contact.e_islandFlag) {
  6103. continue;
  6104. }
  6105. if (ce.contact.IsSensor() == true || ce.contact.IsEnabled() == false || ce.contact.IsTouching() == false) {
  6106. continue;
  6107. }
  6108. island.AddContact(ce.contact);
  6109. ce.contact.m_flags |= b2Contact.e_islandFlag;
  6110. other = ce.other;
  6111. if (other.m_flags & b2Body.e_islandFlag) {
  6112. continue;
  6113. }
  6114. stack[stackCount++] = other;
  6115. other.m_flags |= b2Body.e_islandFlag;
  6116. }
  6117. for (var jn = b.m_jointList; jn; jn = jn.next) {
  6118. if (jn.joint.m_islandFlag == true) {
  6119. continue;
  6120. }
  6121. other = jn.other;
  6122. if (other.IsActive() == false) {
  6123. continue;
  6124. }
  6125. island.AddJoint(jn.joint);
  6126. jn.joint.m_islandFlag = true;
  6127. if (other.m_flags & b2Body.e_islandFlag) {
  6128. continue;
  6129. }
  6130. stack[stackCount++] = other;
  6131. other.m_flags |= b2Body.e_islandFlag;
  6132. }
  6133. }
  6134. island.Solve(step, this.m_gravity, this.m_allowSleep);
  6135. for (var i = 0; i < island.m_bodyCount; ++i) {
  6136. b = island.m_bodies[i];
  6137. if (b.GetType() == b2Body.b2_staticBody) {
  6138. b.m_flags &= ~b2Body.e_islandFlag;
  6139. }
  6140. }
  6141. }
  6142. for (i = 0;
  6143. i < stack.length; ++i) {
  6144. if (!stack[i]) break;
  6145. stack[i] = null;
  6146. }
  6147. for (b = this.m_bodyList;
  6148. b; b = b.m_next) {
  6149. if (b.IsAwake() == false || b.IsActive() == false) {
  6150. continue;
  6151. }
  6152. if (b.GetType() == b2Body.b2_staticBody) {
  6153. continue;
  6154. }
  6155. b.SynchronizeFixtures();
  6156. }
  6157. this.m_contactManager.FindNewContacts();
  6158. }
  6159. b2World.prototype.SolveTOI = function (step) {
  6160. var b;
  6161. var fA;
  6162. var fB;
  6163. var bA;
  6164. var bB;
  6165. var cEdge;
  6166. var j;
  6167. var island = this.m_island;
  6168. island.Initialize(this.m_bodyCount, b2Settings.b2_maxTOIContactsPerIsland, b2Settings.b2_maxTOIJointsPerIsland, null, this.m_contactManager.m_contactListener, this.m_contactSolver);
  6169. var queue = b2World.s_queue;
  6170. for (b = this.m_bodyList;
  6171. b; b = b.m_next) {
  6172. b.m_flags &= ~b2Body.e_islandFlag;
  6173. b.m_sweep.t0 = 0.0;
  6174. }
  6175. var c;
  6176. for (c = this.m_contactList;
  6177. c; c = c.m_next) {
  6178. c.m_flags &= ~ (b2Contact.e_toiFlag | b2Contact.e_islandFlag);
  6179. }
  6180. for (j = this.m_jointList;
  6181. j; j = j.m_next) {
  6182. j.m_islandFlag = false;
  6183. }
  6184. for (;;) {
  6185. var minContact = null;
  6186. var minTOI = 1.0;
  6187. for (c = this.m_contactList;
  6188. c; c = c.m_next) {
  6189. if (c.IsSensor() == true || c.IsEnabled() == false || c.IsContinuous() == false) {
  6190. continue;
  6191. }
  6192. var toi = 1.0;
  6193. if (c.m_flags & b2Contact.e_toiFlag) {
  6194. toi = c.m_toi;
  6195. }
  6196. else {
  6197. fA = c.m_fixtureA;
  6198. fB = c.m_fixtureB;
  6199. bA = fA.m_body;
  6200. bB = fB.m_body;
  6201. if ((bA.GetType() != b2Body.b2_dynamicBody || bA.IsAwake() == false) && (bB.GetType() != b2Body.b2_dynamicBody || bB.IsAwake() == false)) {
  6202. continue;
  6203. }
  6204. var t0 = bA.m_sweep.t0;
  6205. if (bA.m_sweep.t0 < bB.m_sweep.t0) {
  6206. t0 = bB.m_sweep.t0;
  6207. bA.m_sweep.Advance(t0);
  6208. }
  6209. else if (bB.m_sweep.t0 < bA.m_sweep.t0) {
  6210. t0 = bA.m_sweep.t0;
  6211. bB.m_sweep.Advance(t0);
  6212. }
  6213. toi = c.ComputeTOI(bA.m_sweep, bB.m_sweep);
  6214. b2Settings.b2Assert(0.0 <= toi && toi <= 1.0);
  6215. if (toi > 0.0 && toi < 1.0) {
  6216. toi = (1.0 - toi) * t0 + toi;
  6217. if (toi > 1) toi = 1;
  6218. }
  6219. c.m_toi = toi;
  6220. c.m_flags |= b2Contact.e_toiFlag;
  6221. }
  6222. if (Number.MIN_VALUE < toi && toi < minTOI) {
  6223. minContact = c;
  6224. minTOI = toi;
  6225. }
  6226. }
  6227. if (minContact == null || 1.0 - 100.0 * Number.MIN_VALUE < minTOI) {
  6228. break;
  6229. }
  6230. fA = minContact.m_fixtureA;
  6231. fB = minContact.m_fixtureB;
  6232. bA = fA.m_body;
  6233. bB = fB.m_body;
  6234. b2World.s_backupA.Set(bA.m_sweep);
  6235. b2World.s_backupB.Set(bB.m_sweep);
  6236. bA.Advance(minTOI);
  6237. bB.Advance(minTOI);
  6238. minContact.Update(this.m_contactManager.m_contactListener);
  6239. minContact.m_flags &= ~b2Contact.e_toiFlag;
  6240. if (minContact.IsSensor() == true || minContact.IsEnabled() == false) {
  6241. bA.m_sweep.Set(b2World.s_backupA);
  6242. bB.m_sweep.Set(b2World.s_backupB);
  6243. bA.SynchronizeTransform();
  6244. bB.SynchronizeTransform();
  6245. continue;
  6246. }
  6247. if (minContact.IsTouching() == false) {
  6248. continue;
  6249. }
  6250. var seed = bA;
  6251. if (seed.GetType() != b2Body.b2_dynamicBody) {
  6252. seed = bB;
  6253. }
  6254. island.Clear();
  6255. var queueStart = 0;
  6256. var queueSize = 0;
  6257. queue[queueStart + queueSize++] = seed;
  6258. seed.m_flags |= b2Body.e_islandFlag;
  6259. while (queueSize > 0) {
  6260. b = queue[queueStart++];
  6261. --queueSize;
  6262. island.AddBody(b);
  6263. if (b.IsAwake() == false) {
  6264. b.SetAwake(true);
  6265. }
  6266. if (b.GetType() != b2Body.b2_dynamicBody) {
  6267. continue;
  6268. }
  6269. for (cEdge = b.m_contactList;
  6270. cEdge; cEdge = cEdge.next) {
  6271. if (island.m_contactCount == island.m_contactCapacity) {
  6272. break;
  6273. }
  6274. if (cEdge.contact.m_flags & b2Contact.e_islandFlag) {
  6275. continue;
  6276. }
  6277. if (cEdge.contact.IsSensor() == true || cEdge.contact.IsEnabled() == false || cEdge.contact.IsTouching() == false) {
  6278. continue;
  6279. }
  6280. island.AddContact(cEdge.contact);
  6281. cEdge.contact.m_flags |= b2Contact.e_islandFlag;
  6282. var other = cEdge.other;
  6283. if (other.m_flags & b2Body.e_islandFlag) {
  6284. continue;
  6285. }
  6286. if (other.GetType() != b2Body.b2_staticBody) {
  6287. other.Advance(minTOI);
  6288. other.SetAwake(true);
  6289. }
  6290. queue[queueStart + queueSize] = other;
  6291. ++queueSize;
  6292. other.m_flags |= b2Body.e_islandFlag;
  6293. }
  6294. for (var jEdge = b.m_jointList; jEdge; jEdge = jEdge.next) {
  6295. if (island.m_jointCount == island.m_jointCapacity) continue;
  6296. if (jEdge.joint.m_islandFlag == true) continue;
  6297. other = jEdge.other;
  6298. if (other.IsActive() == false) {
  6299. continue;
  6300. }
  6301. island.AddJoint(jEdge.joint);
  6302. jEdge.joint.m_islandFlag = true;
  6303. if (other.m_flags & b2Body.e_islandFlag) continue;
  6304. if (other.GetType() != b2Body.b2_staticBody) {
  6305. other.Advance(minTOI);
  6306. other.SetAwake(true);
  6307. }
  6308. queue[queueStart + queueSize] = other;
  6309. ++queueSize;
  6310. other.m_flags |= b2Body.e_islandFlag;
  6311. }
  6312. }
  6313. var subStep = b2World.s_timestep;
  6314. subStep.warmStarting = false;
  6315. subStep.dt = (1.0 - minTOI) * step.dt;
  6316. subStep.inv_dt = 1.0 / subStep.dt;
  6317. subStep.dtRatio = 0.0;
  6318. subStep.velocityIterations = step.velocityIterations;
  6319. subStep.positionIterations = step.positionIterations;
  6320. island.SolveTOI(subStep);
  6321. var i = 0;
  6322. for (i = 0;
  6323. i < island.m_bodyCount; ++i) {
  6324. b = island.m_bodies[i];
  6325. b.m_flags &= ~b2Body.e_islandFlag;
  6326. if (b.IsAwake() == false) {
  6327. continue;
  6328. }
  6329. if (b.GetType() != b2Body.b2_dynamicBody) {
  6330. continue;
  6331. }
  6332. b.SynchronizeFixtures();
  6333. for (cEdge = b.m_contactList;
  6334. cEdge; cEdge = cEdge.next) {
  6335. cEdge.contact.m_flags &= ~b2Contact.e_toiFlag;
  6336. }
  6337. }
  6338. for (i = 0;
  6339. i < island.m_contactCount; ++i) {
  6340. c = island.m_contacts[i];
  6341. c.m_flags &= ~ (b2Contact.e_toiFlag | b2Contact.e_islandFlag);
  6342. }
  6343. for (i = 0;
  6344. i < island.m_jointCount; ++i) {
  6345. j = island.m_joints[i];
  6346. j.m_islandFlag = false;
  6347. }
  6348. this.m_contactManager.FindNewContacts();
  6349. }
  6350. }
  6351. b2World.prototype.DrawJoint = function (joint) {
  6352. var b1 = joint.GetBodyA();
  6353. var b2 = joint.GetBodyB();
  6354. var xf1 = b1.m_xf;
  6355. var xf2 = b2.m_xf;
  6356. var x1 = xf1.position;
  6357. var x2 = xf2.position;
  6358. var p1 = joint.GetAnchorA();
  6359. var p2 = joint.GetAnchorB();
  6360. var color = b2World.s_jointColor;
  6361. switch (joint.m_type) {
  6362. case b2Joint.e_distanceJoint:
  6363. this.m_debugDraw.DrawSegment(p1, p2, color);
  6364. break;
  6365. case b2Joint.e_pulleyJoint:
  6366. {
  6367. var pulley = ((joint instanceof b2PulleyJoint ? joint : null));
  6368. var s1 = pulley.GetGroundAnchorA();
  6369. var s2 = pulley.GetGroundAnchorB();
  6370. this.m_debugDraw.DrawSegment(s1, p1, color);
  6371. this.m_debugDraw.DrawSegment(s2, p2, color);
  6372. this.m_debugDraw.DrawSegment(s1, s2, color);
  6373. }
  6374. break;
  6375. case b2Joint.e_mouseJoint:
  6376. this.m_debugDraw.DrawSegment(p1, p2, color);
  6377. break;
  6378. default:
  6379. if (b1 != this.m_groundBody) this.m_debugDraw.DrawSegment(x1, p1, color);
  6380. this.m_debugDraw.DrawSegment(p1, p2, color);
  6381. if (b2 != this.m_groundBody) this.m_debugDraw.DrawSegment(x2, p2, color);
  6382. }
  6383. }
  6384. b2World.prototype.DrawShape = function (shape, xf, color) {
  6385. switch (shape.m_type) {
  6386. case b2Shape.e_circleShape:
  6387. {
  6388. var circle = ((shape instanceof b2CircleShape ? shape : null));
  6389. var center = b2Math.MulX(xf, circle.m_p);
  6390. var radius = circle.m_radius;
  6391. var axis = xf.R.col1;
  6392. this.m_debugDraw.DrawSolidCircle(center, radius, axis, color);
  6393. }
  6394. break;
  6395. case b2Shape.e_polygonShape:
  6396. {
  6397. var i = 0;
  6398. var poly = ((shape instanceof b2PolygonShape ? shape : null));
  6399. var vertexCount = parseInt(poly.GetVertexCount());
  6400. var localVertices = poly.GetVertices();
  6401. var vertices = new Vector(vertexCount);
  6402. for (i = 0;
  6403. i < vertexCount; ++i) {
  6404. vertices[i] = b2Math.MulX(xf, localVertices[i]);
  6405. }
  6406. this.m_debugDraw.DrawSolidPolygon(vertices, vertexCount, color);
  6407. }
  6408. break;
  6409. case b2Shape.e_edgeShape:
  6410. {
  6411. var edge = (shape instanceof b2EdgeShape ? shape : null);
  6412. this.m_debugDraw.DrawSegment(b2Math.MulX(xf, edge.GetVertex1()), b2Math.MulX(xf, edge.GetVertex2()), color);
  6413. }
  6414. break;
  6415. }
  6416. }
  6417. Box2D.postDefs.push(function () {
  6418. Box2D.Dynamics.b2World.s_timestep2 = new b2TimeStep();
  6419. Box2D.Dynamics.b2World.s_xf = new b2Transform();
  6420. Box2D.Dynamics.b2World.s_backupA = new b2Sweep();
  6421. Box2D.Dynamics.b2World.s_backupB = new b2Sweep();
  6422. Box2D.Dynamics.b2World.s_timestep = new b2TimeStep();
  6423. Box2D.Dynamics.b2World.s_queue = new Vector();
  6424. Box2D.Dynamics.b2World.s_jointColor = new b2Color(0.5, 0.8, 0.8);
  6425. Box2D.Dynamics.b2World.e_newFixture = 0x0001;
  6426. Box2D.Dynamics.b2World.e_locked = 0x0002;
  6427. });
  6428. })();
  6429. (function () {
  6430. var b2CircleShape = Box2D.Collision.Shapes.b2CircleShape,
  6431. b2EdgeChainDef = Box2D.Collision.Shapes.b2EdgeChainDef,
  6432. b2EdgeShape = Box2D.Collision.Shapes.b2EdgeShape,
  6433. b2MassData = Box2D.Collision.Shapes.b2MassData,
  6434. b2PolygonShape = Box2D.Collision.Shapes.b2PolygonShape,
  6435. b2Shape = Box2D.Collision.Shapes.b2Shape,
  6436. b2CircleContact = Box2D.Dynamics.Contacts.b2CircleContact,
  6437. b2Contact = Box2D.Dynamics.Contacts.b2Contact,
  6438. b2ContactConstraint = Box2D.Dynamics.Contacts.b2ContactConstraint,
  6439. b2ContactConstraintPoint = Box2D.Dynamics.Contacts.b2ContactConstraintPoint,
  6440. b2ContactEdge = Box2D.Dynamics.Contacts.b2ContactEdge,
  6441. b2ContactFactory = Box2D.Dynamics.Contacts.b2ContactFactory,
  6442. b2ContactRegister = Box2D.Dynamics.Contacts.b2ContactRegister,
  6443. b2ContactResult = Box2D.Dynamics.Contacts.b2ContactResult,
  6444. b2ContactSolver = Box2D.Dynamics.Contacts.b2ContactSolver,
  6445. b2EdgeAndCircleContact = Box2D.Dynamics.Contacts.b2EdgeAndCircleContact,
  6446. b2NullContact = Box2D.Dynamics.Contacts.b2NullContact,
  6447. b2PolyAndCircleContact = Box2D.Dynamics.Contacts.b2PolyAndCircleContact,
  6448. b2PolyAndEdgeContact = Box2D.Dynamics.Contacts.b2PolyAndEdgeContact,
  6449. b2PolygonContact = Box2D.Dynamics.Contacts.b2PolygonContact,
  6450. b2PositionSolverManifold = Box2D.Dynamics.Contacts.b2PositionSolverManifold,
  6451. b2Body = Box2D.Dynamics.b2Body,
  6452. b2BodyDef = Box2D.Dynamics.b2BodyDef,
  6453. b2ContactFilter = Box2D.Dynamics.b2ContactFilter,
  6454. b2ContactImpulse = Box2D.Dynamics.b2ContactImpulse,
  6455. b2ContactListener = Box2D.Dynamics.b2ContactListener,
  6456. b2ContactManager = Box2D.Dynamics.b2ContactManager,
  6457. b2DebugDraw = Box2D.Dynamics.b2DebugDraw,
  6458. b2DestructionListener = Box2D.Dynamics.b2DestructionListener,
  6459. b2FilterData = Box2D.Dynamics.b2FilterData,
  6460. b2Fixture = Box2D.Dynamics.b2Fixture,
  6461. b2FixtureDef = Box2D.Dynamics.b2FixtureDef,
  6462. b2Island = Box2D.Dynamics.b2Island,
  6463. b2TimeStep = Box2D.Dynamics.b2TimeStep,
  6464. b2World = Box2D.Dynamics.b2World,
  6465. b2Color = Box2D.Common.b2Color,
  6466. b2internal = Box2D.Common.b2internal,
  6467. b2Settings = Box2D.Common.b2Settings,
  6468. b2Mat22 = Box2D.Common.Math.b2Mat22,
  6469. b2Mat33 = Box2D.Common.Math.b2Mat33,
  6470. b2Math = Box2D.Common.Math.b2Math,
  6471. b2Sweep = Box2D.Common.Math.b2Sweep,
  6472. b2Transform = Box2D.Common.Math.b2Transform,
  6473. b2Vec2 = Box2D.Common.Math.b2Vec2,
  6474. b2Vec3 = Box2D.Common.Math.b2Vec3,
  6475. b2AABB = Box2D.Collision.b2AABB,
  6476. b2Bound = Box2D.Collision.b2Bound,
  6477. b2BoundValues = Box2D.Collision.b2BoundValues,
  6478. b2Collision = Box2D.Collision.b2Collision,
  6479. b2ContactID = Box2D.Collision.b2ContactID,
  6480. b2ContactPoint = Box2D.Collision.b2ContactPoint,
  6481. b2Distance = Box2D.Collision.b2Distance,
  6482. b2DistanceInput = Box2D.Collision.b2DistanceInput,
  6483. b2DistanceOutput = Box2D.Collision.b2DistanceOutput,
  6484. b2DistanceProxy = Box2D.Collision.b2DistanceProxy,
  6485. b2DynamicTree = Box2D.Collision.b2DynamicTree,
  6486. b2DynamicTreeBroadPhase = Box2D.Collision.b2DynamicTreeBroadPhase,
  6487. b2DynamicTreeNode = Box2D.Collision.b2DynamicTreeNode,
  6488. b2DynamicTreePair = Box2D.Collision.b2DynamicTreePair,
  6489. b2Manifold = Box2D.Collision.b2Manifold,
  6490. b2ManifoldPoint = Box2D.Collision.b2ManifoldPoint,
  6491. b2Point = Box2D.Collision.b2Point,
  6492. b2RayCastInput = Box2D.Collision.b2RayCastInput,
  6493. b2RayCastOutput = Box2D.Collision.b2RayCastOutput,
  6494. b2Segment = Box2D.Collision.b2Segment,
  6495. b2SeparationFunction = Box2D.Collision.b2SeparationFunction,
  6496. b2Simplex = Box2D.Collision.b2Simplex,
  6497. b2SimplexCache = Box2D.Collision.b2SimplexCache,
  6498. b2SimplexVertex = Box2D.Collision.b2SimplexVertex,
  6499. b2TimeOfImpact = Box2D.Collision.b2TimeOfImpact,
  6500. b2TOIInput = Box2D.Collision.b2TOIInput,
  6501. b2WorldManifold = Box2D.Collision.b2WorldManifold,
  6502. ClipVertex = Box2D.Collision.ClipVertex,
  6503. Features = Box2D.Collision.Features,
  6504. IBroadPhase = Box2D.Collision.IBroadPhase;
  6505. Box2D.inherit(b2CircleContact, Box2D.Dynamics.Contacts.b2Contact);
  6506. b2CircleContact.prototype.__super = Box2D.Dynamics.Contacts.b2Contact.prototype;
  6507. b2CircleContact.b2CircleContact = function () {
  6508. Box2D.Dynamics.Contacts.b2Contact.b2Contact.apply(this, arguments);
  6509. };
  6510. b2CircleContact.Create = function (allocator) {
  6511. return new b2CircleContact();
  6512. }
  6513. b2CircleContact.Destroy = function (contact, allocator) {}
  6514. b2CircleContact.prototype.Reset = function (fixtureA, fixtureB) {
  6515. this.__super.Reset.call(this, fixtureA, fixtureB);
  6516. }
  6517. b2CircleContact.prototype.Evaluate = function () {
  6518. var bA = this.m_fixtureA.GetBody();
  6519. var bB = this.m_fixtureB.GetBody();
  6520. b2Collision.CollideCircles(this.m_manifold, (this.m_fixtureA.GetShape() instanceof b2CircleShape ? this.m_fixtureA.GetShape() : null), bA.m_xf, (this.m_fixtureB.GetShape() instanceof b2CircleShape ? this.m_fixtureB.GetShape() : null), bB.m_xf);
  6521. }
  6522. b2Contact.b2Contact = function () {
  6523. this.m_nodeA = new b2ContactEdge();
  6524. this.m_nodeB = new b2ContactEdge();
  6525. this.m_manifold = new b2Manifold();
  6526. this.m_oldManifold = new b2Manifold();
  6527. };
  6528. b2Contact.prototype.GetManifold = function () {
  6529. return this.m_manifold;
  6530. }
  6531. b2Contact.prototype.GetWorldManifold = function (worldManifold) {
  6532. var bodyA = this.m_fixtureA.GetBody();
  6533. var bodyB = this.m_fixtureB.GetBody();
  6534. var shapeA = this.m_fixtureA.GetShape();
  6535. var shapeB = this.m_fixtureB.GetShape();
  6536. worldManifold.Initialize(this.m_manifold, bodyA.GetTransform(), shapeA.m_radius, bodyB.GetTransform(), shapeB.m_radius);
  6537. }
  6538. b2Contact.prototype.IsTouching = function () {
  6539. return (this.m_flags & b2Contact.e_touchingFlag) == b2Contact.e_touchingFlag;
  6540. }
  6541. b2Contact.prototype.IsContinuous = function () {
  6542. return (this.m_flags & b2Contact.e_continuousFlag) == b2Contact.e_continuousFlag;
  6543. }
  6544. b2Contact.prototype.SetSensor = function (sensor) {
  6545. if (sensor) {
  6546. this.m_flags |= b2Contact.e_sensorFlag;
  6547. }
  6548. else {
  6549. this.m_flags &= ~b2Contact.e_sensorFlag;
  6550. }
  6551. }
  6552. b2Contact.prototype.IsSensor = function () {
  6553. return (this.m_flags & b2Contact.e_sensorFlag) == b2Contact.e_sensorFlag;
  6554. }
  6555. b2Contact.prototype.SetEnabled = function (flag) {
  6556. if (flag) {
  6557. this.m_flags |= b2Contact.e_enabledFlag;
  6558. }
  6559. else {
  6560. this.m_flags &= ~b2Contact.e_enabledFlag;
  6561. }
  6562. }
  6563. b2Contact.prototype.IsEnabled = function () {
  6564. return (this.m_flags & b2Contact.e_enabledFlag) == b2Contact.e_enabledFlag;
  6565. }
  6566. b2Contact.prototype.GetNext = function () {
  6567. return this.m_next;
  6568. }
  6569. b2Contact.prototype.GetFixtureA = function () {
  6570. return this.m_fixtureA;
  6571. }
  6572. b2Contact.prototype.GetFixtureB = function () {
  6573. return this.m_fixtureB;
  6574. }
  6575. b2Contact.prototype.FlagForFiltering = function () {
  6576. this.m_flags |= b2Contact.e_filterFlag;
  6577. }
  6578. b2Contact.prototype.b2Contact = function () {}
  6579. b2Contact.prototype.Reset = function (fixtureA, fixtureB) {
  6580. if (fixtureA === undefined) fixtureA = null;
  6581. if (fixtureB === undefined) fixtureB = null;
  6582. this.m_flags = b2Contact.e_enabledFlag;
  6583. if (!fixtureA || !fixtureB) {
  6584. this.m_fixtureA = null;
  6585. this.m_fixtureB = null;
  6586. return;
  6587. }
  6588. if (fixtureA.IsSensor() || fixtureB.IsSensor()) {
  6589. this.m_flags |= b2Contact.e_sensorFlag;
  6590. }
  6591. var bodyA = fixtureA.GetBody();
  6592. var bodyB = fixtureB.GetBody();
  6593. if (bodyA.GetType() != b2Body.b2_dynamicBody || bodyA.IsBullet() || bodyB.GetType() != b2Body.b2_dynamicBody || bodyB.IsBullet()) {
  6594. this.m_flags |= b2Contact.e_continuousFlag;
  6595. }
  6596. this.m_fixtureA = fixtureA;
  6597. this.m_fixtureB = fixtureB;
  6598. this.m_manifold.m_pointCount = 0;
  6599. this.m_prev = null;
  6600. this.m_next = null;
  6601. this.m_nodeA.contact = null;
  6602. this.m_nodeA.prev = null;
  6603. this.m_nodeA.next = null;
  6604. this.m_nodeA.other = null;
  6605. this.m_nodeB.contact = null;
  6606. this.m_nodeB.prev = null;
  6607. this.m_nodeB.next = null;
  6608. this.m_nodeB.other = null;
  6609. }
  6610. b2Contact.prototype.Update = function (listener) {
  6611. var tManifold = this.m_oldManifold;
  6612. this.m_oldManifold = this.m_manifold;
  6613. this.m_manifold = tManifold;
  6614. this.m_flags |= b2Contact.e_enabledFlag;
  6615. var touching = false;
  6616. var wasTouching = (this.m_flags & b2Contact.e_touchingFlag) == b2Contact.e_touchingFlag;
  6617. var bodyA = this.m_fixtureA.m_body;
  6618. var bodyB = this.m_fixtureB.m_body;
  6619. var aabbOverlap = this.m_fixtureA.m_aabb.TestOverlap(this.m_fixtureB.m_aabb);
  6620. if (this.m_flags & b2Contact.e_sensorFlag) {
  6621. if (aabbOverlap) {
  6622. var shapeA = this.m_fixtureA.GetShape();
  6623. var shapeB = this.m_fixtureB.GetShape();
  6624. var xfA = bodyA.GetTransform();
  6625. var xfB = bodyB.GetTransform();
  6626. touching = b2Shape.TestOverlap(shapeA, xfA, shapeB, xfB);
  6627. }
  6628. this.m_manifold.m_pointCount = 0;
  6629. }
  6630. else {
  6631. if (bodyA.GetType() != b2Body.b2_dynamicBody || bodyA.IsBullet() || bodyB.GetType() != b2Body.b2_dynamicBody || bodyB.IsBullet()) {
  6632. this.m_flags |= b2Contact.e_continuousFlag;
  6633. }
  6634. else {
  6635. this.m_flags &= ~b2Contact.e_continuousFlag;
  6636. }
  6637. if (aabbOverlap) {
  6638. this.Evaluate();
  6639. touching = this.m_manifold.m_pointCount > 0;
  6640. for (var i = 0; i < this.m_manifold.m_pointCount; ++i) {
  6641. var mp2 = this.m_manifold.m_points[i];
  6642. mp2.m_normalImpulse = 0.0;
  6643. mp2.m_tangentImpulse = 0.0;
  6644. var id2 = mp2.m_id;
  6645. for (var j = 0; j < this.m_oldManifold.m_pointCount; ++j) {
  6646. var mp1 = this.m_oldManifold.m_points[j];
  6647. if (mp1.m_id.key == id2.key) {
  6648. mp2.m_normalImpulse = mp1.m_normalImpulse;
  6649. mp2.m_tangentImpulse = mp1.m_tangentImpulse;
  6650. break;
  6651. }
  6652. }
  6653. }
  6654. }
  6655. else {
  6656. this.m_manifold.m_pointCount = 0;
  6657. }
  6658. if (touching != wasTouching) {
  6659. bodyA.SetAwake(true);
  6660. bodyB.SetAwake(true);
  6661. }
  6662. }
  6663. if (touching) {
  6664. this.m_flags |= b2Contact.e_touchingFlag;
  6665. }
  6666. else {
  6667. this.m_flags &= ~b2Contact.e_touchingFlag;
  6668. }
  6669. if (wasTouching == false && touching == true) {
  6670. listener.BeginContact(this);
  6671. }
  6672. if (wasTouching == true && touching == false) {
  6673. listener.EndContact(this);
  6674. }
  6675. if ((this.m_flags & b2Contact.e_sensorFlag) == 0) {
  6676. listener.PreSolve(this, this.m_oldManifold);
  6677. }
  6678. }
  6679. b2Contact.prototype.Evaluate = function () {}
  6680. b2Contact.prototype.ComputeTOI = function (sweepA, sweepB) {
  6681. b2Contact.s_input.proxyA.Set(this.m_fixtureA.GetShape());
  6682. b2Contact.s_input.proxyB.Set(this.m_fixtureB.GetShape());
  6683. b2Contact.s_input.sweepA = sweepA;
  6684. b2Contact.s_input.sweepB = sweepB;
  6685. b2Contact.s_input.tolerance = b2Settings.b2_linearSlop;
  6686. return b2TimeOfImpact.TimeOfImpact(b2Contact.s_input);
  6687. }
  6688. Box2D.postDefs.push(function () {
  6689. Box2D.Dynamics.Contacts.b2Contact.e_sensorFlag = 0x0001;
  6690. Box2D.Dynamics.Contacts.b2Contact.e_continuousFlag = 0x0002;
  6691. Box2D.Dynamics.Contacts.b2Contact.e_islandFlag = 0x0004;
  6692. Box2D.Dynamics.Contacts.b2Contact.e_toiFlag = 0x0008;
  6693. Box2D.Dynamics.Contacts.b2Contact.e_touchingFlag = 0x0010;
  6694. Box2D.Dynamics.Contacts.b2Contact.e_enabledFlag = 0x0020;
  6695. Box2D.Dynamics.Contacts.b2Contact.e_filterFlag = 0x0040;
  6696. Box2D.Dynamics.Contacts.b2Contact.s_input = new b2TOIInput();
  6697. });
  6698. b2ContactConstraint.b2ContactConstraint = function () {
  6699. this.localPlaneNormal = new b2Vec2();
  6700. this.localPoint = new b2Vec2();
  6701. this.normal = new b2Vec2();
  6702. this.normalMass = new b2Mat22();
  6703. this.K = new b2Mat22();
  6704. };
  6705. b2ContactConstraint.prototype.b2ContactConstraint = function () {
  6706. this.points = new Vector(b2Settings.b2_maxManifoldPoints);
  6707. for (var i = 0; i < b2Settings.b2_maxManifoldPoints; i++) {
  6708. this.points[i] = new b2ContactConstraintPoint();
  6709. }
  6710. }
  6711. b2ContactConstraintPoint.b2ContactConstraintPoint = function () {
  6712. this.localPoint = new b2Vec2();
  6713. this.rA = new b2Vec2();
  6714. this.rB = new b2Vec2();
  6715. };
  6716. b2ContactEdge.b2ContactEdge = function () {};
  6717. b2ContactFactory.b2ContactFactory = function () {};
  6718. b2ContactFactory.prototype.b2ContactFactory = function (allocator) {
  6719. this.m_allocator = allocator;
  6720. this.InitializeRegisters();
  6721. }
  6722. b2ContactFactory.prototype.AddType = function (createFcn, destroyFcn, type1, type2) {
  6723. if (type1 === undefined) type1 = 0;
  6724. if (type2 === undefined) type2 = 0;
  6725. this.m_registers[type1][type2].createFcn = createFcn;
  6726. this.m_registers[type1][type2].destroyFcn = destroyFcn;
  6727. this.m_registers[type1][type2].primary = true;
  6728. if (type1 != type2) {
  6729. this.m_registers[type2][type1].createFcn = createFcn;
  6730. this.m_registers[type2][type1].destroyFcn = destroyFcn;
  6731. this.m_registers[type2][type1].primary = false;
  6732. }
  6733. }
  6734. b2ContactFactory.prototype.InitializeRegisters = function () {
  6735. this.m_registers = new Vector(b2Shape.e_shapeTypeCount);
  6736. for (var i = 0; i < b2Shape.e_shapeTypeCount; i++) {
  6737. this.m_registers[i] = new Vector(b2Shape.e_shapeTypeCount);
  6738. for (var j = 0; j < b2Shape.e_shapeTypeCount; j++) {
  6739. this.m_registers[i][j] = new b2ContactRegister();
  6740. }
  6741. }
  6742. this.AddType(b2CircleContact.Create, b2CircleContact.Destroy, b2Shape.e_circleShape, b2Shape.e_circleShape);
  6743. this.AddType(b2PolyAndCircleContact.Create, b2PolyAndCircleContact.Destroy, b2Shape.e_polygonShape, b2Shape.e_circleShape);
  6744. this.AddType(b2PolygonContact.Create, b2PolygonContact.Destroy, b2Shape.e_polygonShape, b2Shape.e_polygonShape);
  6745. this.AddType(b2EdgeAndCircleContact.Create, b2EdgeAndCircleContact.Destroy, b2Shape.e_edgeShape, b2Shape.e_circleShape);
  6746. this.AddType(b2PolyAndEdgeContact.Create, b2PolyAndEdgeContact.Destroy, b2Shape.e_polygonShape, b2Shape.e_edgeShape);
  6747. }
  6748. b2ContactFactory.prototype.Create = function (fixtureA, fixtureB) {
  6749. var type1 = parseInt(fixtureA.GetType());
  6750. var type2 = parseInt(fixtureB.GetType());
  6751. var reg = this.m_registers[type1][type2];
  6752. var c;
  6753. if (reg.pool) {
  6754. c = reg.pool;
  6755. reg.pool = c.m_next;
  6756. reg.poolCount--;
  6757. c.Reset(fixtureA, fixtureB);
  6758. return c;
  6759. }
  6760. var createFcn = reg.createFcn;
  6761. if (createFcn != null) {
  6762. if (reg.primary) {
  6763. c = createFcn(this.m_allocator);
  6764. c.Reset(fixtureA, fixtureB);
  6765. return c;
  6766. }
  6767. else {
  6768. c = createFcn(this.m_allocator);
  6769. c.Reset(fixtureB, fixtureA);
  6770. return c;
  6771. }
  6772. }
  6773. else {
  6774. return null;
  6775. }
  6776. }
  6777. b2ContactFactory.prototype.Destroy = function (contact) {
  6778. if (contact.m_manifold.m_pointCount > 0) {
  6779. contact.m_fixtureA.m_body.SetAwake(true);
  6780. contact.m_fixtureB.m_body.SetAwake(true);
  6781. }
  6782. var type1 = parseInt(contact.m_fixtureA.GetType());
  6783. var type2 = parseInt(contact.m_fixtureB.GetType());
  6784. var reg = this.m_registers[type1][type2];
  6785. if (true) {
  6786. reg.poolCount++;
  6787. contact.m_next = reg.pool;
  6788. reg.pool = contact;
  6789. }
  6790. var destroyFcn = reg.destroyFcn;
  6791. destroyFcn(contact, this.m_allocator);
  6792. }
  6793. b2ContactRegister.b2ContactRegister = function () {};
  6794. b2ContactResult.b2ContactResult = function () {
  6795. this.position = new b2Vec2();
  6796. this.normal = new b2Vec2();
  6797. this.id = new b2ContactID();
  6798. };
  6799. b2ContactSolver.b2ContactSolver = function () {
  6800. this.m_step = new b2TimeStep();
  6801. this.m_constraints = new Vector();
  6802. };
  6803. b2ContactSolver.prototype.b2ContactSolver = function () {}
  6804. b2ContactSolver.prototype.Initialize = function (step, contacts, contactCount, allocator) {
  6805. if (contactCount === undefined) contactCount = 0;
  6806. var contact;
  6807. this.m_step.Set(step);
  6808. this.m_allocator = allocator;
  6809. var i = 0;
  6810. var tVec;
  6811. var tMat;
  6812. this.m_constraintCount = contactCount;
  6813. while (this.m_constraints.length < this.m_constraintCount) {
  6814. this.m_constraints[this.m_constraints.length] = new b2ContactConstraint();
  6815. }
  6816. for (i = 0;
  6817. i < contactCount; ++i) {
  6818. contact = contacts[i];
  6819. var fixtureA = contact.m_fixtureA;
  6820. var fixtureB = contact.m_fixtureB;
  6821. var shapeA = fixtureA.m_shape;
  6822. var shapeB = fixtureB.m_shape;
  6823. var radiusA = shapeA.m_radius;
  6824. var radiusB = shapeB.m_radius;
  6825. var bodyA = fixtureA.m_body;
  6826. var bodyB = fixtureB.m_body;
  6827. var manifold = contact.GetManifold();
  6828. var friction = b2Settings.b2MixFriction(fixtureA.GetFriction(), fixtureB.GetFriction());
  6829. var restitution = b2Settings.b2MixRestitution(fixtureA.GetRestitution(), fixtureB.GetRestitution());
  6830. var vAX = bodyA.m_linearVelocity.x;
  6831. var vAY = bodyA.m_linearVelocity.y;
  6832. var vBX = bodyB.m_linearVelocity.x;
  6833. var vBY = bodyB.m_linearVelocity.y;
  6834. var wA = bodyA.m_angularVelocity;
  6835. var wB = bodyB.m_angularVelocity;
  6836. b2Settings.b2Assert(manifold.m_pointCount > 0);
  6837. b2ContactSolver.s_worldManifold.Initialize(manifold, bodyA.m_xf, radiusA, bodyB.m_xf, radiusB);
  6838. var normalX = b2ContactSolver.s_worldManifold.m_normal.x;
  6839. var normalY = b2ContactSolver.s_worldManifold.m_normal.y;
  6840. var cc = this.m_constraints[i];
  6841. cc.bodyA = bodyA;
  6842. cc.bodyB = bodyB;
  6843. cc.manifold = manifold;
  6844. cc.normal.x = normalX;
  6845. cc.normal.y = normalY;
  6846. cc.pointCount = manifold.m_pointCount;
  6847. cc.friction = friction;
  6848. cc.restitution = restitution;
  6849. cc.localPlaneNormal.x = manifold.m_localPlaneNormal.x;
  6850. cc.localPlaneNormal.y = manifold.m_localPlaneNormal.y;
  6851. cc.localPoint.x = manifold.m_localPoint.x;
  6852. cc.localPoint.y = manifold.m_localPoint.y;
  6853. cc.radius = radiusA + radiusB;
  6854. cc.type = manifold.m_type;
  6855. for (var k = 0; k < cc.pointCount; ++k) {
  6856. var cp = manifold.m_points[k];
  6857. var ccp = cc.points[k];
  6858. ccp.normalImpulse = cp.m_normalImpulse;
  6859. ccp.tangentImpulse = cp.m_tangentImpulse;
  6860. ccp.localPoint.SetV(cp.m_localPoint);
  6861. var rAX = ccp.rA.x = b2ContactSolver.s_worldManifold.m_points[k].x - bodyA.m_sweep.c.x;
  6862. var rAY = ccp.rA.y = b2ContactSolver.s_worldManifold.m_points[k].y - bodyA.m_sweep.c.y;
  6863. var rBX = ccp.rB.x = b2ContactSolver.s_worldManifold.m_points[k].x - bodyB.m_sweep.c.x;
  6864. var rBY = ccp.rB.y = b2ContactSolver.s_worldManifold.m_points[k].y - bodyB.m_sweep.c.y;
  6865. var rnA = rAX * normalY - rAY * normalX;
  6866. var rnB = rBX * normalY - rBY * normalX;
  6867. rnA *= rnA;
  6868. rnB *= rnB;
  6869. var kNormal = bodyA.m_invMass + bodyB.m_invMass + bodyA.m_invI * rnA + bodyB.m_invI * rnB;
  6870. ccp.normalMass = 1.0 / kNormal;
  6871. var kEqualized = bodyA.m_mass * bodyA.m_invMass + bodyB.m_mass * bodyB.m_invMass;
  6872. kEqualized += bodyA.m_mass * bodyA.m_invI * rnA + bodyB.m_mass * bodyB.m_invI * rnB;
  6873. ccp.equalizedMass = 1.0 / kEqualized;
  6874. var tangentX = normalY;
  6875. var tangentY = (-normalX);
  6876. var rtA = rAX * tangentY - rAY * tangentX;
  6877. var rtB = rBX * tangentY - rBY * tangentX;
  6878. rtA *= rtA;
  6879. rtB *= rtB;
  6880. var kTangent = bodyA.m_invMass + bodyB.m_invMass + bodyA.m_invI * rtA + bodyB.m_invI * rtB;
  6881. ccp.tangentMass = 1.0 / kTangent;
  6882. ccp.velocityBias = 0.0;
  6883. var tX = vBX + ((-wB * rBY)) - vAX - ((-wA * rAY));
  6884. var tY = vBY + (wB * rBX) - vAY - (wA * rAX);
  6885. var vRel = cc.normal.x * tX + cc.normal.y * tY;
  6886. if (vRel < (-b2Settings.b2_velocityThreshold)) {
  6887. ccp.velocityBias += (-cc.restitution * vRel);
  6888. }
  6889. }
  6890. if (cc.pointCount == 2) {
  6891. var ccp1 = cc.points[0];
  6892. var ccp2 = cc.points[1];
  6893. var invMassA = bodyA.m_invMass;
  6894. var invIA = bodyA.m_invI;
  6895. var invMassB = bodyB.m_invMass;
  6896. var invIB = bodyB.m_invI;
  6897. var rn1A = ccp1.rA.x * normalY - ccp1.rA.y * normalX;
  6898. var rn1B = ccp1.rB.x * normalY - ccp1.rB.y * normalX;
  6899. var rn2A = ccp2.rA.x * normalY - ccp2.rA.y * normalX;
  6900. var rn2B = ccp2.rB.x * normalY - ccp2.rB.y * normalX;
  6901. var k11 = invMassA + invMassB + invIA * rn1A * rn1A + invIB * rn1B * rn1B;
  6902. var k22 = invMassA + invMassB + invIA * rn2A * rn2A + invIB * rn2B * rn2B;
  6903. var k12 = invMassA + invMassB + invIA * rn1A * rn2A + invIB * rn1B * rn2B;
  6904. var k_maxConditionNumber = 100.0;
  6905. if (k11 * k11 < k_maxConditionNumber * (k11 * k22 - k12 * k12)) {
  6906. cc.K.col1.Set(k11, k12);
  6907. cc.K.col2.Set(k12, k22);
  6908. cc.K.GetInverse(cc.normalMass);
  6909. }
  6910. else {
  6911. cc.pointCount = 1;
  6912. }
  6913. }
  6914. }
  6915. }
  6916. b2ContactSolver.prototype.InitVelocityConstraints = function (step) {
  6917. var tVec;
  6918. var tVec2;
  6919. var tMat;
  6920. for (var i = 0; i < this.m_constraintCount; ++i) {
  6921. var c = this.m_constraints[i];
  6922. var bodyA = c.bodyA;
  6923. var bodyB = c.bodyB;
  6924. var invMassA = bodyA.m_invMass;
  6925. var invIA = bodyA.m_invI;
  6926. var invMassB = bodyB.m_invMass;
  6927. var invIB = bodyB.m_invI;
  6928. var normalX = c.normal.x;
  6929. var normalY = c.normal.y;
  6930. var tangentX = normalY;
  6931. var tangentY = (-normalX);
  6932. var tX = 0;
  6933. var j = 0;
  6934. var tCount = 0;
  6935. if (step.warmStarting) {
  6936. tCount = c.pointCount;
  6937. for (j = 0;
  6938. j < tCount; ++j) {
  6939. var ccp = c.points[j];
  6940. ccp.normalImpulse *= step.dtRatio;
  6941. ccp.tangentImpulse *= step.dtRatio;
  6942. var PX = ccp.normalImpulse * normalX + ccp.tangentImpulse * tangentX;
  6943. var PY = ccp.normalImpulse * normalY + ccp.tangentImpulse * tangentY;
  6944. bodyA.m_angularVelocity -= invIA * (ccp.rA.x * PY - ccp.rA.y * PX);
  6945. bodyA.m_linearVelocity.x -= invMassA * PX;
  6946. bodyA.m_linearVelocity.y -= invMassA * PY;
  6947. bodyB.m_angularVelocity += invIB * (ccp.rB.x * PY - ccp.rB.y * PX);
  6948. bodyB.m_linearVelocity.x += invMassB * PX;
  6949. bodyB.m_linearVelocity.y += invMassB * PY;
  6950. }
  6951. }
  6952. else {
  6953. tCount = c.pointCount;
  6954. for (j = 0;
  6955. j < tCount; ++j) {
  6956. var ccp2 = c.points[j];
  6957. ccp2.normalImpulse = 0.0;
  6958. ccp2.tangentImpulse = 0.0;
  6959. }
  6960. }
  6961. }
  6962. }
  6963. b2ContactSolver.prototype.SolveVelocityConstraints = function () {
  6964. var j = 0;
  6965. var ccp;
  6966. var rAX = 0;
  6967. var rAY = 0;
  6968. var rBX = 0;
  6969. var rBY = 0;
  6970. var dvX = 0;
  6971. var dvY = 0;
  6972. var vn = 0;
  6973. var vt = 0;
  6974. var lambda = 0;
  6975. var maxFriction = 0;
  6976. var newImpulse = 0;
  6977. var PX = 0;
  6978. var PY = 0;
  6979. var dX = 0;
  6980. var dY = 0;
  6981. var P1X = 0;
  6982. var P1Y = 0;
  6983. var P2X = 0;
  6984. var P2Y = 0;
  6985. var tMat;
  6986. var tVec;
  6987. for (var i = 0; i < this.m_constraintCount; ++i) {
  6988. var c = this.m_constraints[i];
  6989. var bodyA = c.bodyA;
  6990. var bodyB = c.bodyB;
  6991. var wA = bodyA.m_angularVelocity;
  6992. var wB = bodyB.m_angularVelocity;
  6993. var vA = bodyA.m_linearVelocity;
  6994. var vB = bodyB.m_linearVelocity;
  6995. var invMassA = bodyA.m_invMass;
  6996. var invIA = bodyA.m_invI;
  6997. var invMassB = bodyB.m_invMass;
  6998. var invIB = bodyB.m_invI;
  6999. var normalX = c.normal.x;
  7000. var normalY = c.normal.y;
  7001. var tangentX = normalY;
  7002. var tangentY = (-normalX);
  7003. var friction = c.friction;
  7004. var tX = 0;
  7005. for (j = 0;
  7006. j < c.pointCount; j++) {
  7007. ccp = c.points[j];
  7008. dvX = vB.x - wB * ccp.rB.y - vA.x + wA * ccp.rA.y;
  7009. dvY = vB.y + wB * ccp.rB.x - vA.y - wA * ccp.rA.x;
  7010. vt = dvX * tangentX + dvY * tangentY;
  7011. lambda = ccp.tangentMass * (-vt);
  7012. maxFriction = friction * ccp.normalImpulse;
  7013. newImpulse = b2Math.Clamp(ccp.tangentImpulse + lambda, (-maxFriction), maxFriction);
  7014. lambda = newImpulse - ccp.tangentImpulse;
  7015. PX = lambda * tangentX;
  7016. PY = lambda * tangentY;
  7017. vA.x -= invMassA * PX;
  7018. vA.y -= invMassA * PY;
  7019. wA -= invIA * (ccp.rA.x * PY - ccp.rA.y * PX);
  7020. vB.x += invMassB * PX;
  7021. vB.y += invMassB * PY;
  7022. wB += invIB * (ccp.rB.x * PY - ccp.rB.y * PX);
  7023. ccp.tangentImpulse = newImpulse;
  7024. }
  7025. var tCount = parseInt(c.pointCount);
  7026. if (c.pointCount == 1) {
  7027. ccp = c.points[0];
  7028. dvX = vB.x + ((-wB * ccp.rB.y)) - vA.x - ((-wA * ccp.rA.y));
  7029. dvY = vB.y + (wB * ccp.rB.x) - vA.y - (wA * ccp.rA.x);
  7030. vn = dvX * normalX + dvY * normalY;
  7031. lambda = (-ccp.normalMass * (vn - ccp.velocityBias));
  7032. newImpulse = ccp.normalImpulse + lambda;
  7033. newImpulse = newImpulse > 0 ? newImpulse : 0.0;
  7034. lambda = newImpulse - ccp.normalImpulse;
  7035. PX = lambda * normalX;
  7036. PY = lambda * normalY;
  7037. vA.x -= invMassA * PX;
  7038. vA.y -= invMassA * PY;
  7039. wA -= invIA * (ccp.rA.x * PY - ccp.rA.y * PX);
  7040. vB.x += invMassB * PX;
  7041. vB.y += invMassB * PY;
  7042. wB += invIB * (ccp.rB.x * PY - ccp.rB.y * PX);
  7043. ccp.normalImpulse = newImpulse;
  7044. }
  7045. else {
  7046. var cp1 = c.points[0];
  7047. var cp2 = c.points[1];
  7048. var aX = cp1.normalImpulse;
  7049. var aY = cp2.normalImpulse;
  7050. var dv1X = vB.x - wB * cp1.rB.y - vA.x + wA * cp1.rA.y;
  7051. var dv1Y = vB.y + wB * cp1.rB.x - vA.y - wA * cp1.rA.x;
  7052. var dv2X = vB.x - wB * cp2.rB.y - vA.x + wA * cp2.rA.y;
  7053. var dv2Y = vB.y + wB * cp2.rB.x - vA.y - wA * cp2.rA.x;
  7054. var vn1 = dv1X * normalX + dv1Y * normalY;
  7055. var vn2 = dv2X * normalX + dv2Y * normalY;
  7056. var bX = vn1 - cp1.velocityBias;
  7057. var bY = vn2 - cp2.velocityBias;
  7058. tMat = c.K;
  7059. bX -= tMat.col1.x * aX + tMat.col2.x * aY;
  7060. bY -= tMat.col1.y * aX + tMat.col2.y * aY;
  7061. var k_errorTol = 0.001;
  7062. for (;;) {
  7063. tMat = c.normalMass;
  7064. var xX = (-(tMat.col1.x * bX + tMat.col2.x * bY));
  7065. var xY = (-(tMat.col1.y * bX + tMat.col2.y * bY));
  7066. if (xX >= 0.0 && xY >= 0.0) {
  7067. dX = xX - aX;
  7068. dY = xY - aY;
  7069. P1X = dX * normalX;
  7070. P1Y = dX * normalY;
  7071. P2X = dY * normalX;
  7072. P2Y = dY * normalY;
  7073. vA.x -= invMassA * (P1X + P2X);
  7074. vA.y -= invMassA * (P1Y + P2Y);
  7075. wA -= invIA * (cp1.rA.x * P1Y - cp1.rA.y * P1X + cp2.rA.x * P2Y - cp2.rA.y * P2X);
  7076. vB.x += invMassB * (P1X + P2X);
  7077. vB.y += invMassB * (P1Y + P2Y);
  7078. wB += invIB * (cp1.rB.x * P1Y - cp1.rB.y * P1X + cp2.rB.x * P2Y - cp2.rB.y * P2X);
  7079. cp1.normalImpulse = xX;
  7080. cp2.normalImpulse = xY;
  7081. break;
  7082. }
  7083. xX = (-cp1.normalMass * bX);
  7084. xY = 0.0;
  7085. vn1 = 0.0;
  7086. vn2 = c.K.col1.y * xX + bY;
  7087. if (xX >= 0.0 && vn2 >= 0.0) {
  7088. dX = xX - aX;
  7089. dY = xY - aY;
  7090. P1X = dX * normalX;
  7091. P1Y = dX * normalY;
  7092. P2X = dY * normalX;
  7093. P2Y = dY * normalY;
  7094. vA.x -= invMassA * (P1X + P2X);
  7095. vA.y -= invMassA * (P1Y + P2Y);
  7096. wA -= invIA * (cp1.rA.x * P1Y - cp1.rA.y * P1X + cp2.rA.x * P2Y - cp2.rA.y * P2X);
  7097. vB.x += invMassB * (P1X + P2X);
  7098. vB.y += invMassB * (P1Y + P2Y);
  7099. wB += invIB * (cp1.rB.x * P1Y - cp1.rB.y * P1X + cp2.rB.x * P2Y - cp2.rB.y * P2X);
  7100. cp1.normalImpulse = xX;
  7101. cp2.normalImpulse = xY;
  7102. break;
  7103. }
  7104. xX = 0.0;
  7105. xY = (-cp2.normalMass * bY);
  7106. vn1 = c.K.col2.x * xY + bX;
  7107. vn2 = 0.0;
  7108. if (xY >= 0.0 && vn1 >= 0.0) {
  7109. dX = xX - aX;
  7110. dY = xY - aY;
  7111. P1X = dX * normalX;
  7112. P1Y = dX * normalY;
  7113. P2X = dY * normalX;
  7114. P2Y = dY * normalY;
  7115. vA.x -= invMassA * (P1X + P2X);
  7116. vA.y -= invMassA * (P1Y + P2Y);
  7117. wA -= invIA * (cp1.rA.x * P1Y - cp1.rA.y * P1X + cp2.rA.x * P2Y - cp2.rA.y * P2X);
  7118. vB.x += invMassB * (P1X + P2X);
  7119. vB.y += invMassB * (P1Y + P2Y);
  7120. wB += invIB * (cp1.rB.x * P1Y - cp1.rB.y * P1X + cp2.rB.x * P2Y - cp2.rB.y * P2X);
  7121. cp1.normalImpulse = xX;
  7122. cp2.normalImpulse = xY;
  7123. break;
  7124. }
  7125. xX = 0.0;
  7126. xY = 0.0;
  7127. vn1 = bX;
  7128. vn2 = bY;
  7129. if (vn1 >= 0.0 && vn2 >= 0.0) {
  7130. dX = xX - aX;
  7131. dY = xY - aY;
  7132. P1X = dX * normalX;
  7133. P1Y = dX * normalY;
  7134. P2X = dY * normalX;
  7135. P2Y = dY * normalY;
  7136. vA.x -= invMassA * (P1X + P2X);
  7137. vA.y -= invMassA * (P1Y + P2Y);
  7138. wA -= invIA * (cp1.rA.x * P1Y - cp1.rA.y * P1X + cp2.rA.x * P2Y - cp2.rA.y * P2X);
  7139. vB.x += invMassB * (P1X + P2X);
  7140. vB.y += invMassB * (P1Y + P2Y);
  7141. wB += invIB * (cp1.rB.x * P1Y - cp1.rB.y * P1X + cp2.rB.x * P2Y - cp2.rB.y * P2X);
  7142. cp1.normalImpulse = xX;
  7143. cp2.normalImpulse = xY;
  7144. break;
  7145. }
  7146. break;
  7147. }
  7148. }
  7149. bodyA.m_angularVelocity = wA;
  7150. bodyB.m_angularVelocity = wB;
  7151. }
  7152. }
  7153. b2ContactSolver.prototype.FinalizeVelocityConstraints = function () {
  7154. for (var i = 0; i < this.m_constraintCount; ++i) {
  7155. var c = this.m_constraints[i];
  7156. var m = c.manifold;
  7157. for (var j = 0; j < c.pointCount; ++j) {
  7158. var point1 = m.m_points[j];
  7159. var point2 = c.points[j];
  7160. point1.m_normalImpulse = point2.normalImpulse;
  7161. point1.m_tangentImpulse = point2.tangentImpulse;
  7162. }
  7163. }
  7164. }
  7165. b2ContactSolver.prototype.SolvePositionConstraints = function (baumgarte) {
  7166. if (baumgarte === undefined) baumgarte = 0;
  7167. var minSeparation = 0.0;
  7168. for (var i = 0; i < this.m_constraintCount; i++) {
  7169. var c = this.m_constraints[i];
  7170. var bodyA = c.bodyA;
  7171. var bodyB = c.bodyB;
  7172. var invMassA = bodyA.m_mass * bodyA.m_invMass;
  7173. var invIA = bodyA.m_mass * bodyA.m_invI;
  7174. var invMassB = bodyB.m_mass * bodyB.m_invMass;
  7175. var invIB = bodyB.m_mass * bodyB.m_invI;
  7176. b2ContactSolver.s_psm.Initialize(c);
  7177. var normal = b2ContactSolver.s_psm.m_normal;
  7178. for (var j = 0; j < c.pointCount; j++) {
  7179. var ccp = c.points[j];
  7180. var point = b2ContactSolver.s_psm.m_points[j];
  7181. var separation = b2ContactSolver.s_psm.m_separations[j];
  7182. var rAX = point.x - bodyA.m_sweep.c.x;
  7183. var rAY = point.y - bodyA.m_sweep.c.y;
  7184. var rBX = point.x - bodyB.m_sweep.c.x;
  7185. var rBY = point.y - bodyB.m_sweep.c.y;
  7186. minSeparation = minSeparation < separation ? minSeparation : separation;
  7187. var C = b2Math.Clamp(baumgarte * (separation + b2Settings.b2_linearSlop), (-b2Settings.b2_maxLinearCorrection), 0.0);
  7188. var impulse = (-ccp.equalizedMass * C);
  7189. var PX = impulse * normal.x;
  7190. var PY = impulse * normal.y;bodyA.m_sweep.c.x -= invMassA * PX;
  7191. bodyA.m_sweep.c.y -= invMassA * PY;
  7192. bodyA.m_sweep.a -= invIA * (rAX * PY - rAY * PX);
  7193. bodyA.SynchronizeTransform();
  7194. bodyB.m_sweep.c.x += invMassB * PX;
  7195. bodyB.m_sweep.c.y += invMassB * PY;
  7196. bodyB.m_sweep.a += invIB * (rBX * PY - rBY * PX);
  7197. bodyB.SynchronizeTransform();
  7198. }
  7199. }
  7200. return minSeparation > (-1.5 * b2Settings.b2_linearSlop);
  7201. }
  7202. Box2D.postDefs.push(function () {
  7203. Box2D.Dynamics.Contacts.b2ContactSolver.s_worldManifold = new b2WorldManifold();
  7204. Box2D.Dynamics.Contacts.b2ContactSolver.s_psm = new b2PositionSolverManifold();
  7205. });
  7206. Box2D.inherit(b2EdgeAndCircleContact, Box2D.Dynamics.Contacts.b2Contact);
  7207. b2EdgeAndCircleContact.prototype.__super = Box2D.Dynamics.Contacts.b2Contact.prototype;
  7208. b2EdgeAndCircleContact.b2EdgeAndCircleContact = function () {
  7209. Box2D.Dynamics.Contacts.b2Contact.b2Contact.apply(this, arguments);
  7210. };
  7211. b2EdgeAndCircleContact.Create = function (allocator) {
  7212. return new b2EdgeAndCircleContact();
  7213. }
  7214. b2EdgeAndCircleContact.Destroy = function (contact, allocator) {}
  7215. b2EdgeAndCircleContact.prototype.Reset = function (fixtureA, fixtureB) {
  7216. this.__super.Reset.call(this, fixtureA, fixtureB);
  7217. }
  7218. b2EdgeAndCircleContact.prototype.Evaluate = function () {
  7219. var bA = this.m_fixtureA.GetBody();
  7220. var bB = this.m_fixtureB.GetBody();
  7221. this.b2CollideEdgeAndCircle(this.m_manifold, (this.m_fixtureA.GetShape() instanceof b2EdgeShape ? this.m_fixtureA.GetShape() : null), bA.m_xf, (this.m_fixtureB.GetShape() instanceof b2CircleShape ? this.m_fixtureB.GetShape() : null), bB.m_xf);
  7222. }
  7223. b2EdgeAndCircleContact.prototype.b2CollideEdgeAndCircle = function (manifold, edge, xf1, circle, xf2) {}
  7224. Box2D.inherit(b2NullContact, Box2D.Dynamics.Contacts.b2Contact);
  7225. b2NullContact.prototype.__super = Box2D.Dynamics.Contacts.b2Contact.prototype;
  7226. b2NullContact.b2NullContact = function () {
  7227. Box2D.Dynamics.Contacts.b2Contact.b2Contact.apply(this, arguments);
  7228. };
  7229. b2NullContact.prototype.b2NullContact = function () {
  7230. this.__super.b2Contact.call(this);
  7231. }
  7232. b2NullContact.prototype.Evaluate = function () {}
  7233. Box2D.inherit(b2PolyAndCircleContact, Box2D.Dynamics.Contacts.b2Contact);
  7234. b2PolyAndCircleContact.prototype.__super = Box2D.Dynamics.Contacts.b2Contact.prototype;
  7235. b2PolyAndCircleContact.b2PolyAndCircleContact = function () {
  7236. Box2D.Dynamics.Contacts.b2Contact.b2Contact.apply(this, arguments);
  7237. };
  7238. b2PolyAndCircleContact.Create = function (allocator) {
  7239. return new b2PolyAndCircleContact();
  7240. }
  7241. b2PolyAndCircleContact.Destroy = function (contact, allocator) {}
  7242. b2PolyAndCircleContact.prototype.Reset = function (fixtureA, fixtureB) {
  7243. this.__super.Reset.call(this, fixtureA, fixtureB);
  7244. b2Settings.b2Assert(fixtureA.GetType() == b2Shape.e_polygonShape);
  7245. b2Settings.b2Assert(fixtureB.GetType() == b2Shape.e_circleShape);
  7246. }
  7247. b2PolyAndCircleContact.prototype.Evaluate = function () {
  7248. var bA = this.m_fixtureA.m_body;
  7249. var bB = this.m_fixtureB.m_body;
  7250. b2Collision.CollidePolygonAndCircle(this.m_manifold, (this.m_fixtureA.GetShape() instanceof b2PolygonShape ? this.m_fixtureA.GetShape() : null), bA.m_xf, (this.m_fixtureB.GetShape() instanceof b2CircleShape ? this.m_fixtureB.GetShape() : null), bB.m_xf);
  7251. }
  7252. Box2D.inherit(b2PolyAndEdgeContact, Box2D.Dynamics.Contacts.b2Contact);
  7253. b2PolyAndEdgeContact.prototype.__super = Box2D.Dynamics.Contacts.b2Contact.prototype;
  7254. b2PolyAndEdgeContact.b2PolyAndEdgeContact = function () {
  7255. Box2D.Dynamics.Contacts.b2Contact.b2Contact.apply(this, arguments);
  7256. };
  7257. b2PolyAndEdgeContact.Create = function (allocator) {
  7258. return new b2PolyAndEdgeContact();
  7259. }
  7260. b2PolyAndEdgeContact.Destroy = function (contact, allocator) {}
  7261. b2PolyAndEdgeContact.prototype.Reset = function (fixtureA, fixtureB) {
  7262. this.__super.Reset.call(this, fixtureA, fixtureB);
  7263. b2Settings.b2Assert(fixtureA.GetType() == b2Shape.e_polygonShape);
  7264. b2Settings.b2Assert(fixtureB.GetType() == b2Shape.e_edgeShape);
  7265. }
  7266. b2PolyAndEdgeContact.prototype.Evaluate = function () {
  7267. var bA = this.m_fixtureA.GetBody();
  7268. var bB = this.m_fixtureB.GetBody();
  7269. this.b2CollidePolyAndEdge(this.m_manifold, (this.m_fixtureA.GetShape() instanceof b2PolygonShape ? this.m_fixtureA.GetShape() : null), bA.m_xf, (this.m_fixtureB.GetShape() instanceof b2EdgeShape ? this.m_fixtureB.GetShape() : null), bB.m_xf);
  7270. }
  7271. b2PolyAndEdgeContact.prototype.b2CollidePolyAndEdge = function (manifold, polygon, xf1, edge, xf2) {}
  7272. Box2D.inherit(b2PolygonContact, Box2D.Dynamics.Contacts.b2Contact);
  7273. b2PolygonContact.prototype.__super = Box2D.Dynamics.Contacts.b2Contact.prototype;
  7274. b2PolygonContact.b2PolygonContact = function () {
  7275. Box2D.Dynamics.Contacts.b2Contact.b2Contact.apply(this, arguments);
  7276. };
  7277. b2PolygonContact.Create = function (allocator) {
  7278. return new b2PolygonContact();
  7279. }
  7280. b2PolygonContact.Destroy = function (contact, allocator) {}
  7281. b2PolygonContact.prototype.Reset = function (fixtureA, fixtureB) {
  7282. this.__super.Reset.call(this, fixtureA, fixtureB);
  7283. }
  7284. b2PolygonContact.prototype.Evaluate = function () {
  7285. var bA = this.m_fixtureA.GetBody();
  7286. var bB = this.m_fixtureB.GetBody();
  7287. b2Collision.CollidePolygons(this.m_manifold, (this.m_fixtureA.GetShape() instanceof b2PolygonShape ? this.m_fixtureA.GetShape() : null), bA.m_xf, (this.m_fixtureB.GetShape() instanceof b2PolygonShape ? this.m_fixtureB.GetShape() : null), bB.m_xf);
  7288. }
  7289. b2PositionSolverManifold.b2PositionSolverManifold = function () {};
  7290. b2PositionSolverManifold.prototype.b2PositionSolverManifold = function () {
  7291. this.m_normal = new b2Vec2();
  7292. this.m_separations = new Vector_a2j_Number(b2Settings.b2_maxManifoldPoints);
  7293. this.m_points = new Vector(b2Settings.b2_maxManifoldPoints);
  7294. for (var i = 0; i < b2Settings.b2_maxManifoldPoints; i++) {
  7295. this.m_points[i] = new b2Vec2();
  7296. }
  7297. }
  7298. b2PositionSolverManifold.prototype.Initialize = function (cc) {
  7299. b2Settings.b2Assert(cc.pointCount > 0);
  7300. var i = 0;
  7301. var clipPointX = 0;
  7302. var clipPointY = 0;
  7303. var tMat;
  7304. var tVec;
  7305. var planePointX = 0;
  7306. var planePointY = 0;
  7307. switch (cc.type) {
  7308. case b2Manifold.e_circles:
  7309. {
  7310. tMat = cc.bodyA.m_xf.R;
  7311. tVec = cc.localPoint;
  7312. var pointAX = cc.bodyA.m_xf.position.x + (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
  7313. var pointAY = cc.bodyA.m_xf.position.y + (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
  7314. tMat = cc.bodyB.m_xf.R;
  7315. tVec = cc.points[0].localPoint;
  7316. var pointBX = cc.bodyB.m_xf.position.x + (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
  7317. var pointBY = cc.bodyB.m_xf.position.y + (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
  7318. var dX = pointBX - pointAX;
  7319. var dY = pointBY - pointAY;
  7320. var d2 = dX * dX + dY * dY;
  7321. if (d2 > Number.MIN_VALUE * Number.MIN_VALUE) {
  7322. var d = Math.sqrt(d2);
  7323. this.m_normal.x = dX / d;
  7324. this.m_normal.y = dY / d;
  7325. }
  7326. else {
  7327. this.m_normal.x = 1.0;
  7328. this.m_normal.y = 0.0;
  7329. }
  7330. this.m_points[0].x = 0.5 * (pointAX + pointBX);
  7331. this.m_points[0].y = 0.5 * (pointAY + pointBY);
  7332. this.m_separations[0] = dX * this.m_normal.x + dY * this.m_normal.y - cc.radius;
  7333. }
  7334. break;
  7335. case b2Manifold.e_faceA:
  7336. {
  7337. tMat = cc.bodyA.m_xf.R;
  7338. tVec = cc.localPlaneNormal;
  7339. this.m_normal.x = tMat.col1.x * tVec.x + tMat.col2.x * tVec.y;
  7340. this.m_normal.y = tMat.col1.y * tVec.x + tMat.col2.y * tVec.y;
  7341. tMat = cc.bodyA.m_xf.R;
  7342. tVec = cc.localPoint;
  7343. planePointX = cc.bodyA.m_xf.position.x + (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
  7344. planePointY = cc.bodyA.m_xf.position.y + (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
  7345. tMat = cc.bodyB.m_xf.R;
  7346. for (i = 0;
  7347. i < cc.pointCount; ++i) {
  7348. tVec = cc.points[i].localPoint;
  7349. clipPointX = cc.bodyB.m_xf.position.x + (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
  7350. clipPointY = cc.bodyB.m_xf.position.y + (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
  7351. this.m_separations[i] = (clipPointX - planePointX) * this.m_normal.x + (clipPointY - planePointY) * this.m_normal.y - cc.radius;
  7352. this.m_points[i].x = clipPointX;
  7353. this.m_points[i].y = clipPointY;
  7354. }
  7355. }
  7356. break;
  7357. case b2Manifold.e_faceB:
  7358. {
  7359. tMat = cc.bodyB.m_xf.R;
  7360. tVec = cc.localPlaneNormal;
  7361. this.m_normal.x = tMat.col1.x * tVec.x + tMat.col2.x * tVec.y;
  7362. this.m_normal.y = tMat.col1.y * tVec.x + tMat.col2.y * tVec.y;
  7363. tMat = cc.bodyB.m_xf.R;
  7364. tVec = cc.localPoint;
  7365. planePointX = cc.bodyB.m_xf.position.x + (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
  7366. planePointY = cc.bodyB.m_xf.position.y + (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
  7367. tMat = cc.bodyA.m_xf.R;
  7368. for (i = 0;
  7369. i < cc.pointCount; ++i) {
  7370. tVec = cc.points[i].localPoint;
  7371. clipPointX = cc.bodyA.m_xf.position.x + (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
  7372. clipPointY = cc.bodyA.m_xf.position.y + (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
  7373. this.m_separations[i] = (clipPointX - planePointX) * this.m_normal.x + (clipPointY - planePointY) * this.m_normal.y - cc.radius;
  7374. this.m_points[i].Set(clipPointX, clipPointY);
  7375. }
  7376. this.m_normal.x *= (-1);
  7377. this.m_normal.y *= (-1);
  7378. }
  7379. break;
  7380. }
  7381. }
  7382. Box2D.postDefs.push(function () {
  7383. Box2D.Dynamics.Contacts.b2PositionSolverManifold.circlePointA = new b2Vec2();
  7384. Box2D.Dynamics.Contacts.b2PositionSolverManifold.circlePointB = new b2Vec2();
  7385. });
  7386. })();
  7387. (function () {
  7388. var b2Body = Box2D.Dynamics.b2Body,
  7389. b2BodyDef = Box2D.Dynamics.b2BodyDef,
  7390. b2ContactFilter = Box2D.Dynamics.b2ContactFilter,
  7391. b2ContactImpulse = Box2D.Dynamics.b2ContactImpulse,
  7392. b2ContactListener = Box2D.Dynamics.b2ContactListener,
  7393. b2ContactManager = Box2D.Dynamics.b2ContactManager,
  7394. b2DebugDraw = Box2D.Dynamics.b2DebugDraw,
  7395. b2DestructionListener = Box2D.Dynamics.b2DestructionListener,
  7396. b2FilterData = Box2D.Dynamics.b2FilterData,
  7397. b2Fixture = Box2D.Dynamics.b2Fixture,
  7398. b2FixtureDef = Box2D.Dynamics.b2FixtureDef,
  7399. b2Island = Box2D.Dynamics.b2Island,
  7400. b2TimeStep = Box2D.Dynamics.b2TimeStep,
  7401. b2World = Box2D.Dynamics.b2World,
  7402. b2Mat22 = Box2D.Common.Math.b2Mat22,
  7403. b2Mat33 = Box2D.Common.Math.b2Mat33,
  7404. b2Math = Box2D.Common.Math.b2Math,
  7405. b2Sweep = Box2D.Common.Math.b2Sweep,
  7406. b2Transform = Box2D.Common.Math.b2Transform,
  7407. b2Vec2 = Box2D.Common.Math.b2Vec2,
  7408. b2Vec3 = Box2D.Common.Math.b2Vec3,
  7409. b2Color = Box2D.Common.b2Color,
  7410. b2internal = Box2D.Common.b2internal,
  7411. b2Settings = Box2D.Common.b2Settings,
  7412. b2CircleShape = Box2D.Collision.Shapes.b2CircleShape,
  7413. b2EdgeChainDef = Box2D.Collision.Shapes.b2EdgeChainDef,
  7414. b2EdgeShape = Box2D.Collision.Shapes.b2EdgeShape,
  7415. b2MassData = Box2D.Collision.Shapes.b2MassData,
  7416. b2PolygonShape = Box2D.Collision.Shapes.b2PolygonShape,
  7417. b2Shape = Box2D.Collision.Shapes.b2Shape,
  7418. b2BuoyancyController = Box2D.Dynamics.Controllers.b2BuoyancyController,
  7419. b2ConstantAccelController = Box2D.Dynamics.Controllers.b2ConstantAccelController,
  7420. b2ConstantForceController = Box2D.Dynamics.Controllers.b2ConstantForceController,
  7421. b2Controller = Box2D.Dynamics.Controllers.b2Controller,
  7422. b2ControllerEdge = Box2D.Dynamics.Controllers.b2ControllerEdge,
  7423. b2GravityController = Box2D.Dynamics.Controllers.b2GravityController,
  7424. b2TensorDampingController = Box2D.Dynamics.Controllers.b2TensorDampingController;
  7425. Box2D.inherit(b2BuoyancyController, Box2D.Dynamics.Controllers.b2Controller);
  7426. b2BuoyancyController.prototype.__super = Box2D.Dynamics.Controllers.b2Controller.prototype;
  7427. b2BuoyancyController.b2BuoyancyController = function () {
  7428. Box2D.Dynamics.Controllers.b2Controller.b2Controller.apply(this, arguments);
  7429. this.normal = new b2Vec2(0, (-1));
  7430. this.offset = 0;
  7431. this.density = 0;
  7432. this.velocity = new b2Vec2(0, 0);
  7433. this.linearDrag = 2;
  7434. this.angularDrag = 1;
  7435. this.useDensity = false;
  7436. this.useWorldGravity = true;
  7437. this.gravity = null;
  7438. };
  7439. b2BuoyancyController.prototype.Step = function (step) {
  7440. if (!this.m_bodyList) return;
  7441. if (this.useWorldGravity) {
  7442. this.gravity = this.GetWorld().GetGravity().Copy();
  7443. }
  7444. for (var i = this.m_bodyList; i; i = i.nextBody) {
  7445. var body = i.body;
  7446. if (body.IsAwake() == false) {
  7447. continue;
  7448. }
  7449. var areac = new b2Vec2();
  7450. var massc = new b2Vec2();
  7451. var area = 0.0;
  7452. var mass = 0.0;
  7453. for (var fixture = body.GetFixtureList(); fixture; fixture = fixture.GetNext()) {
  7454. var sc = new b2Vec2();
  7455. var sarea = fixture.GetShape().ComputeSubmergedArea(this.normal, this.offset, body.GetTransform(), sc);
  7456. area += sarea;
  7457. areac.x += sarea * sc.x;
  7458. areac.y += sarea * sc.y;
  7459. var shapeDensity = 0;
  7460. if (this.useDensity) {
  7461. shapeDensity = 1;
  7462. }
  7463. else {
  7464. shapeDensity = 1;
  7465. }
  7466. mass += sarea * shapeDensity;
  7467. massc.x += sarea * sc.x * shapeDensity;
  7468. massc.y += sarea * sc.y * shapeDensity;
  7469. }
  7470. areac.x /= area;
  7471. areac.y /= area;
  7472. massc.x /= mass;
  7473. massc.y /= mass;
  7474. if (area < Number.MIN_VALUE) continue;
  7475. var buoyancyForce = this.gravity.GetNegative();
  7476. buoyancyForce.Multiply(this.density * area);
  7477. body.ApplyForce(buoyancyForce, massc);
  7478. var dragForce = body.GetLinearVelocityFromWorldPoint(areac);
  7479. dragForce.Subtract(this.velocity);
  7480. dragForce.Multiply((-this.linearDrag * area));
  7481. body.ApplyForce(dragForce, areac);
  7482. body.ApplyTorque((-body.GetInertia() / body.GetMass() * area * body.GetAngularVelocity() * this.angularDrag));
  7483. }
  7484. }
  7485. b2BuoyancyController.prototype.Draw = function (debugDraw) {
  7486. var r = 1000;
  7487. var p1 = new b2Vec2();
  7488. var p2 = new b2Vec2();
  7489. p1.x = this.normal.x * this.offset + this.normal.y * r;
  7490. p1.y = this.normal.y * this.offset - this.normal.x * r;
  7491. p2.x = this.normal.x * this.offset - this.normal.y * r;
  7492. p2.y = this.normal.y * this.offset + this.normal.x * r;
  7493. var color = new b2Color(0, 0, 1);
  7494. debugDraw.DrawSegment(p1, p2, color);
  7495. }
  7496. Box2D.inherit(b2ConstantAccelController, Box2D.Dynamics.Controllers.b2Controller);
  7497. b2ConstantAccelController.prototype.__super = Box2D.Dynamics.Controllers.b2Controller.prototype;
  7498. b2ConstantAccelController.b2ConstantAccelController = function () {
  7499. Box2D.Dynamics.Controllers.b2Controller.b2Controller.apply(this, arguments);
  7500. this.A = new b2Vec2(0, 0);
  7501. };
  7502. b2ConstantAccelController.prototype.Step = function (step) {
  7503. var smallA = new b2Vec2(this.A.x * step.dt, this.A.y * step.dt);
  7504. for (var i = this.m_bodyList; i; i = i.nextBody) {
  7505. var body = i.body;
  7506. if (!body.IsAwake()) continue;
  7507. body.SetLinearVelocity(new b2Vec2(body.GetLinearVelocity().x + smallA.x, body.GetLinearVelocity().y + smallA.y));
  7508. }
  7509. }
  7510. Box2D.inherit(b2ConstantForceController, Box2D.Dynamics.Controllers.b2Controller);
  7511. b2ConstantForceController.prototype.__super = Box2D.Dynamics.Controllers.b2Controller.prototype;
  7512. b2ConstantForceController.b2ConstantForceController = function () {
  7513. Box2D.Dynamics.Controllers.b2Controller.b2Controller.apply(this, arguments);
  7514. this.F = new b2Vec2(0, 0);
  7515. };
  7516. b2ConstantForceController.prototype.Step = function (step) {
  7517. for (var i = this.m_bodyList; i; i = i.nextBody) {
  7518. var body = i.body;
  7519. if (!body.IsAwake()) continue;
  7520. body.ApplyForce(this.F, body.GetWorldCenter());
  7521. }
  7522. }
  7523. b2Controller.b2Controller = function () {};
  7524. b2Controller.prototype.Step = function (step) {}
  7525. b2Controller.prototype.Draw = function (debugDraw) {}
  7526. b2Controller.prototype.AddBody = function (body) {
  7527. var edge = new b2ControllerEdge();
  7528. edge.controller = this;
  7529. edge.body = body;
  7530. edge.nextBody = this.m_bodyList;
  7531. edge.prevBody = null;
  7532. this.m_bodyList = edge;
  7533. if (edge.nextBody) edge.nextBody.prevBody = edge;
  7534. this.m_bodyCount++;
  7535. edge.nextController = body.m_controllerList;
  7536. edge.prevController = null;
  7537. body.m_controllerList = edge;
  7538. if (edge.nextController) edge.nextController.prevController = edge;
  7539. body.m_controllerCount++;
  7540. }
  7541. b2Controller.prototype.RemoveBody = function (body) {
  7542. var edge = body.m_controllerList;
  7543. while (edge && edge.controller != this)
  7544. edge = edge.nextController;
  7545. if (edge.prevBody) edge.prevBody.nextBody = edge.nextBody;
  7546. if (edge.nextBody) edge.nextBody.prevBody = edge.prevBody;
  7547. if (edge.nextController) edge.nextController.prevController = edge.prevController;
  7548. if (edge.prevController) edge.prevController.nextController = edge.nextController;
  7549. if (this.m_bodyList == edge) this.m_bodyList = edge.nextBody;
  7550. if (body.m_controllerList == edge) body.m_controllerList = edge.nextController;
  7551. body.m_controllerCount--;
  7552. this.m_bodyCount--;
  7553. }
  7554. b2Controller.prototype.Clear = function () {
  7555. while (this.m_bodyList)
  7556. this.RemoveBody(this.m_bodyList.body);
  7557. }
  7558. b2Controller.prototype.GetNext = function () {
  7559. return this.m_next;
  7560. }
  7561. b2Controller.prototype.GetWorld = function () {
  7562. return this.m_world;
  7563. }
  7564. b2Controller.prototype.GetBodyList = function () {
  7565. return this.m_bodyList;
  7566. }
  7567. b2ControllerEdge.b2ControllerEdge = function () {};
  7568. Box2D.inherit(b2GravityController, Box2D.Dynamics.Controllers.b2Controller);
  7569. b2GravityController.prototype.__super = Box2D.Dynamics.Controllers.b2Controller.prototype;
  7570. b2GravityController.b2GravityController = function () {
  7571. Box2D.Dynamics.Controllers.b2Controller.b2Controller.apply(this, arguments);
  7572. this.G = 1;
  7573. this.invSqr = true;
  7574. };
  7575. b2GravityController.prototype.Step = function (step) {
  7576. var i = null;
  7577. var body1 = null;
  7578. var p1 = null;
  7579. var mass1 = 0;
  7580. var j = null;
  7581. var body2 = null;
  7582. var p2 = null;
  7583. var dx = 0;
  7584. var dy = 0;
  7585. var r2 = 0;
  7586. var f = null;
  7587. if (this.invSqr) {
  7588. for (i = this.m_bodyList;
  7589. i; i = i.nextBody) {
  7590. body1 = i.body;
  7591. p1 = body1.GetWorldCenter();
  7592. mass1 = body1.GetMass();
  7593. for (j = this.m_bodyList;
  7594. j != i; j = j.nextBody) {
  7595. body2 = j.body;
  7596. p2 = body2.GetWorldCenter();
  7597. dx = p2.x - p1.x;
  7598. dy = p2.y - p1.y;
  7599. r2 = dx * dx + dy * dy;
  7600. if (r2 < Number.MIN_VALUE) continue;
  7601. f = new b2Vec2(dx, dy);
  7602. f.Multiply(this.G / r2 / Math.sqrt(r2) * mass1 * body2.GetMass());
  7603. if (body1.IsAwake()) body1.ApplyForce(f, p1);
  7604. f.Multiply((-1));
  7605. if (body2.IsAwake()) body2.ApplyForce(f, p2);
  7606. }
  7607. }
  7608. }
  7609. else {
  7610. for (i = this.m_bodyList;
  7611. i; i = i.nextBody) {
  7612. body1 = i.body;
  7613. p1 = body1.GetWorldCenter();
  7614. mass1 = body1.GetMass();
  7615. for (j = this.m_bodyList;
  7616. j != i; j = j.nextBody) {
  7617. body2 = j.body;
  7618. p2 = body2.GetWorldCenter();
  7619. dx = p2.x - p1.x;
  7620. dy = p2.y - p1.y;
  7621. r2 = dx * dx + dy * dy;
  7622. if (r2 < Number.MIN_VALUE) continue;
  7623. f = new b2Vec2(dx, dy);
  7624. f.Multiply(this.G / r2 * mass1 * body2.GetMass());
  7625. if (body1.IsAwake()) body1.ApplyForce(f, p1);
  7626. f.Multiply((-1));
  7627. if (body2.IsAwake()) body2.ApplyForce(f, p2);
  7628. }
  7629. }
  7630. }
  7631. }
  7632. Box2D.inherit(b2TensorDampingController, Box2D.Dynamics.Controllers.b2Controller);
  7633. b2TensorDampingController.prototype.__super = Box2D.Dynamics.Controllers.b2Controller.prototype;
  7634. b2TensorDampingController.b2TensorDampingController = function () {
  7635. Box2D.Dynamics.Controllers.b2Controller.b2Controller.apply(this, arguments);
  7636. this.T = new b2Mat22();
  7637. this.maxTimestep = 0;
  7638. };
  7639. b2TensorDampingController.prototype.SetAxisAligned = function (xDamping, yDamping) {
  7640. if (xDamping === undefined) xDamping = 0;
  7641. if (yDamping === undefined) yDamping = 0;
  7642. this.T.col1.x = (-xDamping);
  7643. this.T.col1.y = 0;
  7644. this.T.col2.x = 0;
  7645. this.T.col2.y = (-yDamping);
  7646. if (xDamping > 0 || yDamping > 0) {
  7647. this.maxTimestep = 1 / Math.max(xDamping, yDamping);
  7648. }
  7649. else {
  7650. this.maxTimestep = 0;
  7651. }
  7652. }
  7653. b2TensorDampingController.prototype.Step = function (step) {
  7654. var timestep = step.dt;
  7655. if (timestep <= Number.MIN_VALUE) return;
  7656. if (timestep > this.maxTimestep && this.maxTimestep > 0) timestep = this.maxTimestep;
  7657. for (var i = this.m_bodyList; i; i = i.nextBody) {
  7658. var body = i.body;
  7659. if (!body.IsAwake()) {
  7660. continue;
  7661. }
  7662. var damping = body.GetWorldVector(b2Math.MulMV(this.T, body.GetLocalVector(body.GetLinearVelocity())));
  7663. body.SetLinearVelocity(new b2Vec2(body.GetLinearVelocity().x + damping.x * timestep, body.GetLinearVelocity().y + damping.y * timestep));
  7664. }
  7665. }
  7666. })();
  7667. (function () {
  7668. var b2Color = Box2D.Common.b2Color,
  7669. b2internal = Box2D.Common.b2internal,
  7670. b2Settings = Box2D.Common.b2Settings,
  7671. b2Mat22 = Box2D.Common.Math.b2Mat22,
  7672. b2Mat33 = Box2D.Common.Math.b2Mat33,
  7673. b2Math = Box2D.Common.Math.b2Math,
  7674. b2Sweep = Box2D.Common.Math.b2Sweep,
  7675. b2Transform = Box2D.Common.Math.b2Transform,
  7676. b2Vec2 = Box2D.Common.Math.b2Vec2,
  7677. b2Vec3 = Box2D.Common.Math.b2Vec3,
  7678. b2DistanceJoint = Box2D.Dynamics.Joints.b2DistanceJoint,
  7679. b2DistanceJointDef = Box2D.Dynamics.Joints.b2DistanceJointDef,
  7680. b2FrictionJoint = Box2D.Dynamics.Joints.b2FrictionJoint,
  7681. b2FrictionJointDef = Box2D.Dynamics.Joints.b2FrictionJointDef,
  7682. b2GearJoint = Box2D.Dynamics.Joints.b2GearJoint,
  7683. b2GearJointDef = Box2D.Dynamics.Joints.b2GearJointDef,
  7684. b2Jacobian = Box2D.Dynamics.Joints.b2Jacobian,
  7685. b2Joint = Box2D.Dynamics.Joints.b2Joint,
  7686. b2JointDef = Box2D.Dynamics.Joints.b2JointDef,
  7687. b2JointEdge = Box2D.Dynamics.Joints.b2JointEdge,
  7688. b2LineJoint = Box2D.Dynamics.Joints.b2LineJoint,
  7689. b2LineJointDef = Box2D.Dynamics.Joints.b2LineJointDef,
  7690. b2MouseJoint = Box2D.Dynamics.Joints.b2MouseJoint,
  7691. b2MouseJointDef = Box2D.Dynamics.Joints.b2MouseJointDef,
  7692. b2PrismaticJoint = Box2D.Dynamics.Joints.b2PrismaticJoint,
  7693. b2PrismaticJointDef = Box2D.Dynamics.Joints.b2PrismaticJointDef,
  7694. b2PulleyJoint = Box2D.Dynamics.Joints.b2PulleyJoint,
  7695. b2PulleyJointDef = Box2D.Dynamics.Joints.b2PulleyJointDef,
  7696. b2RevoluteJoint = Box2D.Dynamics.Joints.b2RevoluteJoint,
  7697. b2RevoluteJointDef = Box2D.Dynamics.Joints.b2RevoluteJointDef,
  7698. b2WeldJoint = Box2D.Dynamics.Joints.b2WeldJoint,
  7699. b2WeldJointDef = Box2D.Dynamics.Joints.b2WeldJointDef,
  7700. b2Body = Box2D.Dynamics.b2Body,
  7701. b2BodyDef = Box2D.Dynamics.b2BodyDef,
  7702. b2ContactFilter = Box2D.Dynamics.b2ContactFilter,
  7703. b2ContactImpulse = Box2D.Dynamics.b2ContactImpulse,
  7704. b2ContactListener = Box2D.Dynamics.b2ContactListener,
  7705. b2ContactManager = Box2D.Dynamics.b2ContactManager,
  7706. b2DebugDraw = Box2D.Dynamics.b2DebugDraw,
  7707. b2DestructionListener = Box2D.Dynamics.b2DestructionListener,
  7708. b2FilterData = Box2D.Dynamics.b2FilterData,
  7709. b2Fixture = Box2D.Dynamics.b2Fixture,
  7710. b2FixtureDef = Box2D.Dynamics.b2FixtureDef,
  7711. b2Island = Box2D.Dynamics.b2Island,
  7712. b2TimeStep = Box2D.Dynamics.b2TimeStep,
  7713. b2World = Box2D.Dynamics.b2World;
  7714. Box2D.inherit(b2DistanceJoint, Box2D.Dynamics.Joints.b2Joint);
  7715. b2DistanceJoint.prototype.__super = Box2D.Dynamics.Joints.b2Joint.prototype;
  7716. b2DistanceJoint.b2DistanceJoint = function () {
  7717. Box2D.Dynamics.Joints.b2Joint.b2Joint.apply(this, arguments);
  7718. this.m_localAnchor1 = new b2Vec2();
  7719. this.m_localAnchor2 = new b2Vec2();
  7720. this.m_u = new b2Vec2();
  7721. };
  7722. b2DistanceJoint.prototype.GetAnchorA = function () {
  7723. return this.m_bodyA.GetWorldPoint(this.m_localAnchor1);
  7724. }
  7725. b2DistanceJoint.prototype.GetAnchorB = function () {
  7726. return this.m_bodyB.GetWorldPoint(this.m_localAnchor2);
  7727. }
  7728. b2DistanceJoint.prototype.GetReactionForce = function (inv_dt) {
  7729. if (inv_dt === undefined) inv_dt = 0;
  7730. return new b2Vec2(inv_dt * this.m_impulse * this.m_u.x, inv_dt * this.m_impulse * this.m_u.y);
  7731. }
  7732. b2DistanceJoint.prototype.GetReactionTorque = function (inv_dt) {
  7733. if (inv_dt === undefined) inv_dt = 0;
  7734. return 0.0;
  7735. }
  7736. b2DistanceJoint.prototype.GetLength = function () {
  7737. return this.m_length;
  7738. }
  7739. b2DistanceJoint.prototype.SetLength = function (length) {
  7740. if (length === undefined) length = 0;
  7741. this.m_length = length;
  7742. }
  7743. b2DistanceJoint.prototype.GetFrequency = function () {
  7744. return this.m_frequencyHz;
  7745. }
  7746. b2DistanceJoint.prototype.SetFrequency = function (hz) {
  7747. if (hz === undefined) hz = 0;
  7748. this.m_frequencyHz = hz;
  7749. }
  7750. b2DistanceJoint.prototype.GetDampingRatio = function () {
  7751. return this.m_dampingRatio;
  7752. }
  7753. b2DistanceJoint.prototype.SetDampingRatio = function (ratio) {
  7754. if (ratio === undefined) ratio = 0;
  7755. this.m_dampingRatio = ratio;
  7756. }
  7757. b2DistanceJoint.prototype.b2DistanceJoint = function (def) {
  7758. this.__super.b2Joint.call(this, def);
  7759. var tMat;
  7760. var tX = 0;
  7761. var tY = 0;
  7762. this.m_localAnchor1.SetV(def.localAnchorA);
  7763. this.m_localAnchor2.SetV(def.localAnchorB);
  7764. this.m_length = def.length;
  7765. this.m_frequencyHz = def.frequencyHz;
  7766. this.m_dampingRatio = def.dampingRatio;
  7767. this.m_impulse = 0.0;
  7768. this.m_gamma = 0.0;
  7769. this.m_bias = 0.0;
  7770. }
  7771. b2DistanceJoint.prototype.InitVelocityConstraints = function (step) {
  7772. var tMat;
  7773. var tX = 0;
  7774. var bA = this.m_bodyA;
  7775. var bB = this.m_bodyB;
  7776. tMat = bA.m_xf.R;
  7777. var r1X = this.m_localAnchor1.x - bA.m_sweep.localCenter.x;
  7778. var r1Y = this.m_localAnchor1.y - bA.m_sweep.localCenter.y;
  7779. tX = (tMat.col1.x * r1X + tMat.col2.x * r1Y);
  7780. r1Y = (tMat.col1.y * r1X + tMat.col2.y * r1Y);
  7781. r1X = tX;
  7782. tMat = bB.m_xf.R;
  7783. var r2X = this.m_localAnchor2.x - bB.m_sweep.localCenter.x;
  7784. var r2Y = this.m_localAnchor2.y - bB.m_sweep.localCenter.y;
  7785. tX = (tMat.col1.x * r2X + tMat.col2.x * r2Y);
  7786. r2Y = (tMat.col1.y * r2X + tMat.col2.y * r2Y);
  7787. r2X = tX;
  7788. this.m_u.x = bB.m_sweep.c.x + r2X - bA.m_sweep.c.x - r1X;
  7789. this.m_u.y = bB.m_sweep.c.y + r2Y - bA.m_sweep.c.y - r1Y;
  7790. var length = Math.sqrt(this.m_u.x * this.m_u.x + this.m_u.y * this.m_u.y);
  7791. if (length > b2Settings.b2_linearSlop) {
  7792. this.m_u.Multiply(1.0 / length);
  7793. }
  7794. else {
  7795. this.m_u.SetZero();
  7796. }
  7797. var cr1u = (r1X * this.m_u.y - r1Y * this.m_u.x);
  7798. var cr2u = (r2X * this.m_u.y - r2Y * this.m_u.x);
  7799. var invMass = bA.m_invMass + bA.m_invI * cr1u * cr1u + bB.m_invMass + bB.m_invI * cr2u * cr2u;
  7800. this.m_mass = invMass != 0.0 ? 1.0 / invMass : 0.0;
  7801. if (this.m_frequencyHz > 0.0) {
  7802. var C = length - this.m_length;
  7803. var omega = 2.0 * Math.PI * this.m_frequencyHz;
  7804. var d = 2.0 * this.m_mass * this.m_dampingRatio * omega;
  7805. var k = this.m_mass * omega * omega;
  7806. this.m_gamma = step.dt * (d + step.dt * k);
  7807. this.m_gamma = this.m_gamma != 0.0 ? 1 / this.m_gamma : 0.0;
  7808. this.m_bias = C * step.dt * k * this.m_gamma;
  7809. this.m_mass = invMass + this.m_gamma;
  7810. this.m_mass = this.m_mass != 0.0 ? 1.0 / this.m_mass : 0.0;
  7811. }
  7812. if (step.warmStarting) {
  7813. this.m_impulse *= step.dtRatio;
  7814. var PX = this.m_impulse * this.m_u.x;
  7815. var PY = this.m_impulse * this.m_u.y;
  7816. bA.m_linearVelocity.x -= bA.m_invMass * PX;
  7817. bA.m_linearVelocity.y -= bA.m_invMass * PY;
  7818. bA.m_angularVelocity -= bA.m_invI * (r1X * PY - r1Y * PX);
  7819. bB.m_linearVelocity.x += bB.m_invMass * PX;
  7820. bB.m_linearVelocity.y += bB.m_invMass * PY;
  7821. bB.m_angularVelocity += bB.m_invI * (r2X * PY - r2Y * PX);
  7822. }
  7823. else {
  7824. this.m_impulse = 0.0;
  7825. }
  7826. }
  7827. b2DistanceJoint.prototype.SolveVelocityConstraints = function (step) {
  7828. var tMat;
  7829. var bA = this.m_bodyA;
  7830. var bB = this.m_bodyB;
  7831. tMat = bA.m_xf.R;
  7832. var r1X = this.m_localAnchor1.x - bA.m_sweep.localCenter.x;
  7833. var r1Y = this.m_localAnchor1.y - bA.m_sweep.localCenter.y;
  7834. var tX = (tMat.col1.x * r1X + tMat.col2.x * r1Y);
  7835. r1Y = (tMat.col1.y * r1X + tMat.col2.y * r1Y);
  7836. r1X = tX;
  7837. tMat = bB.m_xf.R;
  7838. var r2X = this.m_localAnchor2.x - bB.m_sweep.localCenter.x;
  7839. var r2Y = this.m_localAnchor2.y - bB.m_sweep.localCenter.y;
  7840. tX = (tMat.col1.x * r2X + tMat.col2.x * r2Y);
  7841. r2Y = (tMat.col1.y * r2X + tMat.col2.y * r2Y);
  7842. r2X = tX;
  7843. var v1X = bA.m_linearVelocity.x + ((-bA.m_angularVelocity * r1Y));
  7844. var v1Y = bA.m_linearVelocity.y + (bA.m_angularVelocity * r1X);
  7845. var v2X = bB.m_linearVelocity.x + ((-bB.m_angularVelocity * r2Y));
  7846. var v2Y = bB.m_linearVelocity.y + (bB.m_angularVelocity * r2X);
  7847. var Cdot = (this.m_u.x * (v2X - v1X) + this.m_u.y * (v2Y - v1Y));
  7848. var impulse = (-this.m_mass * (Cdot + this.m_bias + this.m_gamma * this.m_impulse));
  7849. this.m_impulse += impulse;
  7850. var PX = impulse * this.m_u.x;
  7851. var PY = impulse * this.m_u.y;
  7852. bA.m_linearVelocity.x -= bA.m_invMass * PX;
  7853. bA.m_linearVelocity.y -= bA.m_invMass * PY;
  7854. bA.m_angularVelocity -= bA.m_invI * (r1X * PY - r1Y * PX);
  7855. bB.m_linearVelocity.x += bB.m_invMass * PX;
  7856. bB.m_linearVelocity.y += bB.m_invMass * PY;
  7857. bB.m_angularVelocity += bB.m_invI * (r2X * PY - r2Y * PX);
  7858. }
  7859. b2DistanceJoint.prototype.SolvePositionConstraints = function (baumgarte) {
  7860. if (baumgarte === undefined) baumgarte = 0;
  7861. var tMat;
  7862. if (this.m_frequencyHz > 0.0) {
  7863. return true;
  7864. }
  7865. var bA = this.m_bodyA;
  7866. var bB = this.m_bodyB;
  7867. tMat = bA.m_xf.R;
  7868. var r1X = this.m_localAnchor1.x - bA.m_sweep.localCenter.x;
  7869. var r1Y = this.m_localAnchor1.y - bA.m_sweep.localCenter.y;
  7870. var tX = (tMat.col1.x * r1X + tMat.col2.x * r1Y);
  7871. r1Y = (tMat.col1.y * r1X + tMat.col2.y * r1Y);
  7872. r1X = tX;
  7873. tMat = bB.m_xf.R;
  7874. var r2X = this.m_localAnchor2.x - bB.m_sweep.localCenter.x;
  7875. var r2Y = this.m_localAnchor2.y - bB.m_sweep.localCenter.y;
  7876. tX = (tMat.col1.x * r2X + tMat.col2.x * r2Y);
  7877. r2Y = (tMat.col1.y * r2X + tMat.col2.y * r2Y);
  7878. r2X = tX;
  7879. var dX = bB.m_sweep.c.x + r2X - bA.m_sweep.c.x - r1X;
  7880. var dY = bB.m_sweep.c.y + r2Y - bA.m_sweep.c.y - r1Y;
  7881. var length = Math.sqrt(dX * dX + dY * dY);
  7882. dX /= length;
  7883. dY /= length;
  7884. var C = length - this.m_length;
  7885. C = b2Math.Clamp(C, (-b2Settings.b2_maxLinearCorrection), b2Settings.b2_maxLinearCorrection);
  7886. var impulse = (-this.m_mass * C);
  7887. this.m_u.Set(dX, dY);
  7888. var PX = impulse * this.m_u.x;
  7889. var PY = impulse * this.m_u.y;
  7890. bA.m_sweep.c.x -= bA.m_invMass * PX;
  7891. bA.m_sweep.c.y -= bA.m_invMass * PY;
  7892. bA.m_sweep.a -= bA.m_invI * (r1X * PY - r1Y * PX);
  7893. bB.m_sweep.c.x += bB.m_invMass * PX;
  7894. bB.m_sweep.c.y += bB.m_invMass * PY;
  7895. bB.m_sweep.a += bB.m_invI * (r2X * PY - r2Y * PX);
  7896. bA.SynchronizeTransform();
  7897. bB.SynchronizeTransform();
  7898. return b2Math.Abs(C) < b2Settings.b2_linearSlop;
  7899. }
  7900. Box2D.inherit(b2DistanceJointDef, Box2D.Dynamics.Joints.b2JointDef);
  7901. b2DistanceJointDef.prototype.__super = Box2D.Dynamics.Joints.b2JointDef.prototype;
  7902. b2DistanceJointDef.b2DistanceJointDef = function () {
  7903. Box2D.Dynamics.Joints.b2JointDef.b2JointDef.apply(this, arguments);
  7904. this.localAnchorA = new b2Vec2();
  7905. this.localAnchorB = new b2Vec2();
  7906. };
  7907. b2DistanceJointDef.prototype.b2DistanceJointDef = function () {
  7908. this.__super.b2JointDef.call(this);
  7909. this.type = b2Joint.e_distanceJoint;
  7910. this.length = 1.0;
  7911. this.frequencyHz = 0.0;
  7912. this.dampingRatio = 0.0;
  7913. }
  7914. b2DistanceJointDef.prototype.Initialize = function (bA, bB, anchorA, anchorB) {
  7915. this.bodyA = bA;
  7916. this.bodyB = bB;
  7917. this.localAnchorA.SetV(this.bodyA.GetLocalPoint(anchorA));
  7918. this.localAnchorB.SetV(this.bodyB.GetLocalPoint(anchorB));
  7919. var dX = anchorB.x - anchorA.x;
  7920. var dY = anchorB.y - anchorA.y;
  7921. this.length = Math.sqrt(dX * dX + dY * dY);
  7922. this.frequencyHz = 0.0;
  7923. this.dampingRatio = 0.0;
  7924. }
  7925. Box2D.inherit(b2FrictionJoint, Box2D.Dynamics.Joints.b2Joint);
  7926. b2FrictionJoint.prototype.__super = Box2D.Dynamics.Joints.b2Joint.prototype;
  7927. b2FrictionJoint.b2FrictionJoint = function () {
  7928. Box2D.Dynamics.Joints.b2Joint.b2Joint.apply(this, arguments);
  7929. this.m_localAnchorA = new b2Vec2();
  7930. this.m_localAnchorB = new b2Vec2();
  7931. this.m_linearMass = new b2Mat22();
  7932. this.m_linearImpulse = new b2Vec2();
  7933. };
  7934. b2FrictionJoint.prototype.GetAnchorA = function () {
  7935. return this.m_bodyA.GetWorldPoint(this.m_localAnchorA);
  7936. }
  7937. b2FrictionJoint.prototype.GetAnchorB = function () {
  7938. return this.m_bodyB.GetWorldPoint(this.m_localAnchorB);
  7939. }
  7940. b2FrictionJoint.prototype.GetReactionForce = function (inv_dt) {
  7941. if (inv_dt === undefined) inv_dt = 0;
  7942. return new b2Vec2(inv_dt * this.m_linearImpulse.x, inv_dt * this.m_linearImpulse.y);
  7943. }
  7944. b2FrictionJoint.prototype.GetReactionTorque = function (inv_dt) {
  7945. if (inv_dt === undefined) inv_dt = 0;
  7946. return inv_dt * this.m_angularImpulse;
  7947. }
  7948. b2FrictionJoint.prototype.SetMaxForce = function (force) {
  7949. if (force === undefined) force = 0;
  7950. this.m_maxForce = force;
  7951. }
  7952. b2FrictionJoint.prototype.GetMaxForce = function () {
  7953. return this.m_maxForce;
  7954. }
  7955. b2FrictionJoint.prototype.SetMaxTorque = function (torque) {
  7956. if (torque === undefined) torque = 0;
  7957. this.m_maxTorque = torque;
  7958. }
  7959. b2FrictionJoint.prototype.GetMaxTorque = function () {
  7960. return this.m_maxTorque;
  7961. }
  7962. b2FrictionJoint.prototype.b2FrictionJoint = function (def) {
  7963. this.__super.b2Joint.call(this, def);
  7964. this.m_localAnchorA.SetV(def.localAnchorA);
  7965. this.m_localAnchorB.SetV(def.localAnchorB);
  7966. this.m_linearMass.SetZero();
  7967. this.m_angularMass = 0.0;
  7968. this.m_linearImpulse.SetZero();
  7969. this.m_angularImpulse = 0.0;
  7970. this.m_maxForce = def.maxForce;
  7971. this.m_maxTorque = def.maxTorque;
  7972. }
  7973. b2FrictionJoint.prototype.InitVelocityConstraints = function (step) {
  7974. var tMat;
  7975. var tX = 0;
  7976. var bA = this.m_bodyA;
  7977. var bB = this.m_bodyB;
  7978. tMat = bA.m_xf.R;
  7979. var rAX = this.m_localAnchorA.x - bA.m_sweep.localCenter.x;
  7980. var rAY = this.m_localAnchorA.y - bA.m_sweep.localCenter.y;
  7981. tX = (tMat.col1.x * rAX + tMat.col2.x * rAY);
  7982. rAY = (tMat.col1.y * rAX + tMat.col2.y * rAY);
  7983. rAX = tX;
  7984. tMat = bB.m_xf.R;
  7985. var rBX = this.m_localAnchorB.x - bB.m_sweep.localCenter.x;
  7986. var rBY = this.m_localAnchorB.y - bB.m_sweep.localCenter.y;
  7987. tX = (tMat.col1.x * rBX + tMat.col2.x * rBY);
  7988. rBY = (tMat.col1.y * rBX + tMat.col2.y * rBY);
  7989. rBX = tX;
  7990. var mA = bA.m_invMass;
  7991. var mB = bB.m_invMass;
  7992. var iA = bA.m_invI;
  7993. var iB = bB.m_invI;
  7994. var K = new b2Mat22();
  7995. K.col1.x = mA + mB;
  7996. K.col2.x = 0.0;
  7997. K.col1.y = 0.0;
  7998. K.col2.y = mA + mB;
  7999. K.col1.x += iA * rAY * rAY;
  8000. K.col2.x += (-iA * rAX * rAY);
  8001. K.col1.y += (-iA * rAX * rAY);
  8002. K.col2.y += iA * rAX * rAX;
  8003. K.col1.x += iB * rBY * rBY;
  8004. K.col2.x += (-iB * rBX * rBY);
  8005. K.col1.y += (-iB * rBX * rBY);
  8006. K.col2.y += iB * rBX * rBX;
  8007. K.GetInverse(this.m_linearMass);
  8008. this.m_angularMass = iA + iB;
  8009. if (this.m_angularMass > 0.0) {
  8010. this.m_angularMass = 1.0 / this.m_angularMass;
  8011. }
  8012. if (step.warmStarting) {
  8013. this.m_linearImpulse.x *= step.dtRatio;
  8014. this.m_linearImpulse.y *= step.dtRatio;
  8015. this.m_angularImpulse *= step.dtRatio;
  8016. var P = this.m_linearImpulse;
  8017. bA.m_linearVelocity.x -= mA * P.x;
  8018. bA.m_linearVelocity.y -= mA * P.y;
  8019. bA.m_angularVelocity -= iA * (rAX * P.y - rAY * P.x + this.m_angularImpulse);
  8020. bB.m_linearVelocity.x += mB * P.x;
  8021. bB.m_linearVelocity.y += mB * P.y;
  8022. bB.m_angularVelocity += iB * (rBX * P.y - rBY * P.x + this.m_angularImpulse);
  8023. }
  8024. else {
  8025. this.m_linearImpulse.SetZero();
  8026. this.m_angularImpulse = 0.0;
  8027. }
  8028. }
  8029. b2FrictionJoint.prototype.SolveVelocityConstraints = function (step) {
  8030. var tMat;
  8031. var tX = 0;
  8032. var bA = this.m_bodyA;
  8033. var bB = this.m_bodyB;
  8034. var vA = bA.m_linearVelocity;
  8035. var wA = bA.m_angularVelocity;
  8036. var vB = bB.m_linearVelocity;
  8037. var wB = bB.m_angularVelocity;
  8038. var mA = bA.m_invMass;
  8039. var mB = bB.m_invMass;
  8040. var iA = bA.m_invI;
  8041. var iB = bB.m_invI;
  8042. tMat = bA.m_xf.R;
  8043. var rAX = this.m_localAnchorA.x - bA.m_sweep.localCenter.x;
  8044. var rAY = this.m_localAnchorA.y - bA.m_sweep.localCenter.y;
  8045. tX = (tMat.col1.x * rAX + tMat.col2.x * rAY);
  8046. rAY = (tMat.col1.y * rAX + tMat.col2.y * rAY);
  8047. rAX = tX;
  8048. tMat = bB.m_xf.R;
  8049. var rBX = this.m_localAnchorB.x - bB.m_sweep.localCenter.x;
  8050. var rBY = this.m_localAnchorB.y - bB.m_sweep.localCenter.y;
  8051. tX = (tMat.col1.x * rBX + tMat.col2.x * rBY);
  8052. rBY = (tMat.col1.y * rBX + tMat.col2.y * rBY);
  8053. rBX = tX;
  8054. var maxImpulse = 0; {
  8055. var Cdot = wB - wA;
  8056. var impulse = (-this.m_angularMass * Cdot);
  8057. var oldImpulse = this.m_angularImpulse;
  8058. maxImpulse = step.dt * this.m_maxTorque;
  8059. this.m_angularImpulse = b2Math.Clamp(this.m_angularImpulse + impulse, (-maxImpulse), maxImpulse);
  8060. impulse = this.m_angularImpulse - oldImpulse;
  8061. wA -= iA * impulse;
  8062. wB += iB * impulse;
  8063. } {
  8064. var CdotX = vB.x - wB * rBY - vA.x + wA * rAY;
  8065. var CdotY = vB.y + wB * rBX - vA.y - wA * rAX;
  8066. var impulseV = b2Math.MulMV(this.m_linearMass, new b2Vec2((-CdotX), (-CdotY)));
  8067. var oldImpulseV = this.m_linearImpulse.Copy();
  8068. this.m_linearImpulse.Add(impulseV);
  8069. maxImpulse = step.dt * this.m_maxForce;
  8070. if (this.m_linearImpulse.LengthSquared() > maxImpulse * maxImpulse) {
  8071. this.m_linearImpulse.Normalize();
  8072. this.m_linearImpulse.Multiply(maxImpulse);
  8073. }
  8074. impulseV = b2Math.SubtractVV(this.m_linearImpulse, oldImpulseV);
  8075. vA.x -= mA * impulseV.x;
  8076. vA.y -= mA * impulseV.y;
  8077. wA -= iA * (rAX * impulseV.y - rAY * impulseV.x);
  8078. vB.x += mB * impulseV.x;
  8079. vB.y += mB * impulseV.y;
  8080. wB += iB * (rBX * impulseV.y - rBY * impulseV.x);
  8081. }
  8082. bA.m_angularVelocity = wA;
  8083. bB.m_angularVelocity = wB;
  8084. }
  8085. b2FrictionJoint.prototype.SolvePositionConstraints = function (baumgarte) {
  8086. if (baumgarte === undefined) baumgarte = 0;
  8087. return true;
  8088. }
  8089. Box2D.inherit(b2FrictionJointDef, Box2D.Dynamics.Joints.b2JointDef);
  8090. b2FrictionJointDef.prototype.__super = Box2D.Dynamics.Joints.b2JointDef.prototype;
  8091. b2FrictionJointDef.b2FrictionJointDef = function () {
  8092. Box2D.Dynamics.Joints.b2JointDef.b2JointDef.apply(this, arguments);
  8093. this.localAnchorA = new b2Vec2();
  8094. this.localAnchorB = new b2Vec2();
  8095. };
  8096. b2FrictionJointDef.prototype.b2FrictionJointDef = function () {
  8097. this.__super.b2JointDef.call(this);
  8098. this.type = b2Joint.e_frictionJoint;
  8099. this.maxForce = 0.0;
  8100. this.maxTorque = 0.0;
  8101. }
  8102. b2FrictionJointDef.prototype.Initialize = function (bA, bB, anchor) {
  8103. this.bodyA = bA;
  8104. this.bodyB = bB;
  8105. this.localAnchorA.SetV(this.bodyA.GetLocalPoint(anchor));
  8106. this.localAnchorB.SetV(this.bodyB.GetLocalPoint(anchor));
  8107. }
  8108. Box2D.inherit(b2GearJoint, Box2D.Dynamics.Joints.b2Joint);
  8109. b2GearJoint.prototype.__super = Box2D.Dynamics.Joints.b2Joint.prototype;
  8110. b2GearJoint.b2GearJoint = function () {
  8111. Box2D.Dynamics.Joints.b2Joint.b2Joint.apply(this, arguments);
  8112. this.m_groundAnchor1 = new b2Vec2();
  8113. this.m_groundAnchor2 = new b2Vec2();
  8114. this.m_localAnchor1 = new b2Vec2();
  8115. this.m_localAnchor2 = new b2Vec2();
  8116. this.m_J = new b2Jacobian();
  8117. };
  8118. b2GearJoint.prototype.GetAnchorA = function () {
  8119. return this.m_bodyA.GetWorldPoint(this.m_localAnchor1);
  8120. }
  8121. b2GearJoint.prototype.GetAnchorB = function () {
  8122. return this.m_bodyB.GetWorldPoint(this.m_localAnchor2);
  8123. }
  8124. b2GearJoint.prototype.GetReactionForce = function (inv_dt) {
  8125. if (inv_dt === undefined) inv_dt = 0;
  8126. return new b2Vec2(inv_dt * this.m_impulse * this.m_J.linearB.x, inv_dt * this.m_impulse * this.m_J.linearB.y);
  8127. }
  8128. b2GearJoint.prototype.GetReactionTorque = function (inv_dt) {
  8129. if (inv_dt === undefined) inv_dt = 0;
  8130. var tMat = this.m_bodyB.m_xf.R;
  8131. var rX = this.m_localAnchor1.x - this.m_bodyB.m_sweep.localCenter.x;
  8132. var rY = this.m_localAnchor1.y - this.m_bodyB.m_sweep.localCenter.y;
  8133. var tX = tMat.col1.x * rX + tMat.col2.x * rY;
  8134. rY = tMat.col1.y * rX + tMat.col2.y * rY;
  8135. rX = tX;
  8136. var PX = this.m_impulse * this.m_J.linearB.x;
  8137. var PY = this.m_impulse * this.m_J.linearB.y;
  8138. return inv_dt * (this.m_impulse * this.m_J.angularB - rX * PY + rY * PX);
  8139. }
  8140. b2GearJoint.prototype.GetRatio = function () {
  8141. return this.m_ratio;
  8142. }
  8143. b2GearJoint.prototype.SetRatio = function (ratio) {
  8144. if (ratio === undefined) ratio = 0;
  8145. this.m_ratio = ratio;
  8146. }
  8147. b2GearJoint.prototype.b2GearJoint = function (def) {
  8148. this.__super.b2Joint.call(this, def);
  8149. var type1 = parseInt(def.joint1.m_type);
  8150. var type2 = parseInt(def.joint2.m_type);
  8151. this.m_revolute1 = null;
  8152. this.m_prismatic1 = null;
  8153. this.m_revolute2 = null;
  8154. this.m_prismatic2 = null;
  8155. var coordinate1 = 0;
  8156. var coordinate2 = 0;
  8157. this.m_ground1 = def.joint1.GetBodyA();
  8158. this.m_bodyA = def.joint1.GetBodyB();
  8159. if (type1 == b2Joint.e_revoluteJoint) {
  8160. this.m_revolute1 = (def.joint1 instanceof b2RevoluteJoint ? def.joint1 : null);
  8161. this.m_groundAnchor1.SetV(this.m_revolute1.m_localAnchor1);
  8162. this.m_localAnchor1.SetV(this.m_revolute1.m_localAnchor2);
  8163. coordinate1 = this.m_revolute1.GetJointAngle();
  8164. }
  8165. else {
  8166. this.m_prismatic1 = (def.joint1 instanceof b2PrismaticJoint ? def.joint1 : null);
  8167. this.m_groundAnchor1.SetV(this.m_prismatic1.m_localAnchor1);
  8168. this.m_localAnchor1.SetV(this.m_prismatic1.m_localAnchor2);
  8169. coordinate1 = this.m_prismatic1.GetJointTranslation();
  8170. }
  8171. this.m_ground2 = def.joint2.GetBodyA();
  8172. this.m_bodyB = def.joint2.GetBodyB();
  8173. if (type2 == b2Joint.e_revoluteJoint) {
  8174. this.m_revolute2 = (def.joint2 instanceof b2RevoluteJoint ? def.joint2 : null);
  8175. this.m_groundAnchor2.SetV(this.m_revolute2.m_localAnchor1);
  8176. this.m_localAnchor2.SetV(this.m_revolute2.m_localAnchor2);
  8177. coordinate2 = this.m_revolute2.GetJointAngle();
  8178. }
  8179. else {
  8180. this.m_prismatic2 = (def.joint2 instanceof b2PrismaticJoint ? def.joint2 : null);
  8181. this.m_groundAnchor2.SetV(this.m_prismatic2.m_localAnchor1);
  8182. this.m_localAnchor2.SetV(this.m_prismatic2.m_localAnchor2);
  8183. coordinate2 = this.m_prismatic2.GetJointTranslation();
  8184. }
  8185. this.m_ratio = def.ratio;
  8186. this.m_constant = coordinate1 + this.m_ratio * coordinate2;
  8187. this.m_impulse = 0.0;
  8188. }
  8189. b2GearJoint.prototype.InitVelocityConstraints = function (step) {
  8190. var g1 = this.m_ground1;
  8191. var g2 = this.m_ground2;
  8192. var bA = this.m_bodyA;
  8193. var bB = this.m_bodyB;
  8194. var ugX = 0;
  8195. var ugY = 0;
  8196. var rX = 0;
  8197. var rY = 0;
  8198. var tMat;
  8199. var tVec;
  8200. var crug = 0;
  8201. var tX = 0;
  8202. var K = 0.0;
  8203. this.m_J.SetZero();
  8204. if (this.m_revolute1) {
  8205. this.m_J.angularA = (-1.0);
  8206. K += bA.m_invI;
  8207. }
  8208. else {
  8209. tMat = g1.m_xf.R;
  8210. tVec = this.m_prismatic1.m_localXAxis1;
  8211. ugX = tMat.col1.x * tVec.x + tMat.col2.x * tVec.y;
  8212. ugY = tMat.col1.y * tVec.x + tMat.col2.y * tVec.y;
  8213. tMat = bA.m_xf.R;
  8214. rX = this.m_localAnchor1.x - bA.m_sweep.localCenter.x;
  8215. rY = this.m_localAnchor1.y - bA.m_sweep.localCenter.y;
  8216. tX = tMat.col1.x * rX + tMat.col2.x * rY;
  8217. rY = tMat.col1.y * rX + tMat.col2.y * rY;
  8218. rX = tX;
  8219. crug = rX * ugY - rY * ugX;
  8220. this.m_J.linearA.Set((-ugX), (-ugY));
  8221. this.m_J.angularA = (-crug);
  8222. K += bA.m_invMass + bA.m_invI * crug * crug;
  8223. }
  8224. if (this.m_revolute2) {
  8225. this.m_J.angularB = (-this.m_ratio);
  8226. K += this.m_ratio * this.m_ratio * bB.m_invI;
  8227. }
  8228. else {
  8229. tMat = g2.m_xf.R;
  8230. tVec = this.m_prismatic2.m_localXAxis1;
  8231. ugX = tMat.col1.x * tVec.x + tMat.col2.x * tVec.y;
  8232. ugY = tMat.col1.y * tVec.x + tMat.col2.y * tVec.y;
  8233. tMat = bB.m_xf.R;
  8234. rX = this.m_localAnchor2.x - bB.m_sweep.localCenter.x;
  8235. rY = this.m_localAnchor2.y - bB.m_sweep.localCenter.y;
  8236. tX = tMat.col1.x * rX + tMat.col2.x * rY;
  8237. rY = tMat.col1.y * rX + tMat.col2.y * rY;
  8238. rX = tX;
  8239. crug = rX * ugY - rY * ugX;
  8240. this.m_J.linearB.Set((-this.m_ratio * ugX), (-this.m_ratio * ugY));
  8241. this.m_J.angularB = (-this.m_ratio * crug);
  8242. K += this.m_ratio * this.m_ratio * (bB.m_invMass + bB.m_invI * crug * crug);
  8243. }
  8244. this.m_mass = K > 0.0 ? 1.0 / K : 0.0;
  8245. if (step.warmStarting) {
  8246. bA.m_linearVelocity.x += bA.m_invMass * this.m_impulse * this.m_J.linearA.x;
  8247. bA.m_linearVelocity.y += bA.m_invMass * this.m_impulse * this.m_J.linearA.y;
  8248. bA.m_angularVelocity += bA.m_invI * this.m_impulse * this.m_J.angularA;
  8249. bB.m_linearVelocity.x += bB.m_invMass * this.m_impulse * this.m_J.linearB.x;
  8250. bB.m_linearVelocity.y += bB.m_invMass * this.m_impulse * this.m_J.linearB.y;
  8251. bB.m_angularVelocity += bB.m_invI * this.m_impulse * this.m_J.angularB;
  8252. }
  8253. else {
  8254. this.m_impulse = 0.0;
  8255. }
  8256. }
  8257. b2GearJoint.prototype.SolveVelocityConstraints = function (step) {
  8258. var bA = this.m_bodyA;
  8259. var bB = this.m_bodyB;
  8260. var Cdot = this.m_J.Compute(bA.m_linearVelocity, bA.m_angularVelocity, bB.m_linearVelocity, bB.m_angularVelocity);
  8261. var impulse = (-this.m_mass * Cdot);
  8262. this.m_impulse += impulse;
  8263. bA.m_linearVelocity.x += bA.m_invMass * impulse * this.m_J.linearA.x;
  8264. bA.m_linearVelocity.y += bA.m_invMass * impulse * this.m_J.linearA.y;
  8265. bA.m_angularVelocity += bA.m_invI * impulse * this.m_J.angularA;
  8266. bB.m_linearVelocity.x += bB.m_invMass * impulse * this.m_J.linearB.x;
  8267. bB.m_linearVelocity.y += bB.m_invMass * impulse * this.m_J.linearB.y;
  8268. bB.m_angularVelocity += bB.m_invI * impulse * this.m_J.angularB;
  8269. }
  8270. b2GearJoint.prototype.SolvePositionConstraints = function (baumgarte) {
  8271. if (baumgarte === undefined) baumgarte = 0;
  8272. var linearError = 0.0;
  8273. var bA = this.m_bodyA;
  8274. var bB = this.m_bodyB;
  8275. var coordinate1 = 0;
  8276. var coordinate2 = 0;
  8277. if (this.m_revolute1) {
  8278. coordinate1 = this.m_revolute1.GetJointAngle();
  8279. }
  8280. else {
  8281. coordinate1 = this.m_prismatic1.GetJointTranslation();
  8282. }
  8283. if (this.m_revolute2) {
  8284. coordinate2 = this.m_revolute2.GetJointAngle();
  8285. }
  8286. else {
  8287. coordinate2 = this.m_prismatic2.GetJointTranslation();
  8288. }
  8289. var C = this.m_constant - (coordinate1 + this.m_ratio * coordinate2);
  8290. var impulse = (-this.m_mass * C);
  8291. bA.m_sweep.c.x += bA.m_invMass * impulse * this.m_J.linearA.x;
  8292. bA.m_sweep.c.y += bA.m_invMass * impulse * this.m_J.linearA.y;
  8293. bA.m_sweep.a += bA.m_invI * impulse * this.m_J.angularA;
  8294. bB.m_sweep.c.x += bB.m_invMass * impulse * this.m_J.linearB.x;
  8295. bB.m_sweep.c.y += bB.m_invMass * impulse * this.m_J.linearB.y;
  8296. bB.m_sweep.a += bB.m_invI * impulse * this.m_J.angularB;
  8297. bA.SynchronizeTransform();
  8298. bB.SynchronizeTransform();
  8299. return linearError < b2Settings.b2_linearSlop;
  8300. }
  8301. Box2D.inherit(b2GearJointDef, Box2D.Dynamics.Joints.b2JointDef);
  8302. b2GearJointDef.prototype.__super = Box2D.Dynamics.Joints.b2JointDef.prototype;
  8303. b2GearJointDef.b2GearJointDef = function () {
  8304. Box2D.Dynamics.Joints.b2JointDef.b2JointDef.apply(this, arguments);
  8305. };
  8306. b2GearJointDef.prototype.b2GearJointDef = function () {
  8307. this.__super.b2JointDef.call(this);
  8308. this.type = b2Joint.e_gearJoint;
  8309. this.joint1 = null;
  8310. this.joint2 = null;
  8311. this.ratio = 1.0;
  8312. }
  8313. b2Jacobian.b2Jacobian = function () {
  8314. this.linearA = new b2Vec2();
  8315. this.linearB = new b2Vec2();
  8316. };
  8317. b2Jacobian.prototype.SetZero = function () {
  8318. this.linearA.SetZero();
  8319. this.angularA = 0.0;
  8320. this.linearB.SetZero();
  8321. this.angularB = 0.0;
  8322. }
  8323. b2Jacobian.prototype.Set = function (x1, a1, x2, a2) {
  8324. if (a1 === undefined) a1 = 0;
  8325. if (a2 === undefined) a2 = 0;
  8326. this.linearA.SetV(x1);
  8327. this.angularA = a1;
  8328. this.linearB.SetV(x2);
  8329. this.angularB = a2;
  8330. }
  8331. b2Jacobian.prototype.Compute = function (x1, a1, x2, a2) {
  8332. if (a1 === undefined) a1 = 0;
  8333. if (a2 === undefined) a2 = 0;
  8334. return (this.linearA.x * x1.x + this.linearA.y * x1.y) + this.angularA * a1 + (this.linearB.x * x2.x + this.linearB.y * x2.y) + this.angularB * a2;
  8335. }
  8336. b2Joint.b2Joint = function () {
  8337. this.m_edgeA = new b2JointEdge();
  8338. this.m_edgeB = new b2JointEdge();
  8339. this.m_localCenterA = new b2Vec2();
  8340. this.m_localCenterB = new b2Vec2();
  8341. };
  8342. b2Joint.prototype.GetType = function () {
  8343. return this.m_type;
  8344. }
  8345. b2Joint.prototype.GetAnchorA = function () {
  8346. return null;
  8347. }
  8348. b2Joint.prototype.GetAnchorB = function () {
  8349. return null;
  8350. }
  8351. b2Joint.prototype.GetReactionForce = function (inv_dt) {
  8352. if (inv_dt === undefined) inv_dt = 0;
  8353. return null;
  8354. }
  8355. b2Joint.prototype.GetReactionTorque = function (inv_dt) {
  8356. if (inv_dt === undefined) inv_dt = 0;
  8357. return 0.0;
  8358. }
  8359. b2Joint.prototype.GetBodyA = function () {
  8360. return this.m_bodyA;
  8361. }
  8362. b2Joint.prototype.GetBodyB = function () {
  8363. return this.m_bodyB;
  8364. }
  8365. b2Joint.prototype.GetNext = function () {
  8366. return this.m_next;
  8367. }
  8368. b2Joint.prototype.GetUserData = function () {
  8369. return this.m_userData;
  8370. }
  8371. b2Joint.prototype.SetUserData = function (data) {
  8372. this.m_userData = data;
  8373. }
  8374. b2Joint.prototype.IsActive = function () {
  8375. return this.m_bodyA.IsActive() && this.m_bodyB.IsActive();
  8376. }
  8377. b2Joint.Create = function (def, allocator) {
  8378. var joint = null;
  8379. switch (def.type) {
  8380. case b2Joint.e_distanceJoint:
  8381. {
  8382. joint = new b2DistanceJoint((def instanceof b2DistanceJointDef ? def : null));
  8383. }
  8384. break;
  8385. case b2Joint.e_mouseJoint:
  8386. {
  8387. joint = new b2MouseJoint((def instanceof b2MouseJointDef ? def : null));
  8388. }
  8389. break;
  8390. case b2Joint.e_prismaticJoint:
  8391. {
  8392. joint = new b2PrismaticJoint((def instanceof b2PrismaticJointDef ? def : null));
  8393. }
  8394. break;
  8395. case b2Joint.e_revoluteJoint:
  8396. {
  8397. joint = new b2RevoluteJoint((def instanceof b2RevoluteJointDef ? def : null));
  8398. }
  8399. break;
  8400. case b2Joint.e_pulleyJoint:
  8401. {
  8402. joint = new b2PulleyJoint((def instanceof b2PulleyJointDef ? def : null));
  8403. }
  8404. break;
  8405. case b2Joint.e_gearJoint:
  8406. {
  8407. joint = new b2GearJoint((def instanceof b2GearJointDef ? def : null));
  8408. }
  8409. break;
  8410. case b2Joint.e_lineJoint:
  8411. {
  8412. joint = new b2LineJoint((def instanceof b2LineJointDef ? def : null));
  8413. }
  8414. break;
  8415. case b2Joint.e_weldJoint:
  8416. {
  8417. joint = new b2WeldJoint((def instanceof b2WeldJointDef ? def : null));
  8418. }
  8419. break;
  8420. case b2Joint.e_frictionJoint:
  8421. {
  8422. joint = new b2FrictionJoint((def instanceof b2FrictionJointDef ? def : null));
  8423. }
  8424. break;
  8425. default:
  8426. break;
  8427. }
  8428. return joint;
  8429. }
  8430. b2Joint.Destroy = function (joint, allocator) {}
  8431. b2Joint.prototype.b2Joint = function (def) {
  8432. b2Settings.b2Assert(def.bodyA != def.bodyB);
  8433. this.m_type = def.type;
  8434. this.m_prev = null;
  8435. this.m_next = null;
  8436. this.m_bodyA = def.bodyA;
  8437. this.m_bodyB = def.bodyB;
  8438. this.m_collideConnected = def.collideConnected;
  8439. this.m_islandFlag = false;
  8440. this.m_userData = def.userData;
  8441. }
  8442. b2Joint.prototype.InitVelocityConstraints = function (step) {}
  8443. b2Joint.prototype.SolveVelocityConstraints = function (step) {}
  8444. b2Joint.prototype.FinalizeVelocityConstraints = function () {}
  8445. b2Joint.prototype.SolvePositionConstraints = function (baumgarte) {
  8446. if (baumgarte === undefined) baumgarte = 0;
  8447. return false;
  8448. }
  8449. Box2D.postDefs.push(function () {
  8450. Box2D.Dynamics.Joints.b2Joint.e_unknownJoint = 0;
  8451. Box2D.Dynamics.Joints.b2Joint.e_revoluteJoint = 1;
  8452. Box2D.Dynamics.Joints.b2Joint.e_prismaticJoint = 2;
  8453. Box2D.Dynamics.Joints.b2Joint.e_distanceJoint = 3;
  8454. Box2D.Dynamics.Joints.b2Joint.e_pulleyJoint = 4;
  8455. Box2D.Dynamics.Joints.b2Joint.e_mouseJoint = 5;
  8456. Box2D.Dynamics.Joints.b2Joint.e_gearJoint = 6;
  8457. Box2D.Dynamics.Joints.b2Joint.e_lineJoint = 7;
  8458. Box2D.Dynamics.Joints.b2Joint.e_weldJoint = 8;
  8459. Box2D.Dynamics.Joints.b2Joint.e_frictionJoint = 9;
  8460. Box2D.Dynamics.Joints.b2Joint.e_inactiveLimit = 0;
  8461. Box2D.Dynamics.Joints.b2Joint.e_atLowerLimit = 1;
  8462. Box2D.Dynamics.Joints.b2Joint.e_atUpperLimit = 2;
  8463. Box2D.Dynamics.Joints.b2Joint.e_equalLimits = 3;
  8464. });
  8465. b2JointDef.b2JointDef = function () {};
  8466. b2JointDef.prototype.b2JointDef = function () {
  8467. this.type = b2Joint.e_unknownJoint;
  8468. this.userData = null;
  8469. this.bodyA = null;
  8470. this.bodyB = null;
  8471. this.collideConnected = false;
  8472. }
  8473. b2JointEdge.b2JointEdge = function () {};
  8474. Box2D.inherit(b2LineJoint, Box2D.Dynamics.Joints.b2Joint);
  8475. b2LineJoint.prototype.__super = Box2D.Dynamics.Joints.b2Joint.prototype;
  8476. b2LineJoint.b2LineJoint = function () {
  8477. Box2D.Dynamics.Joints.b2Joint.b2Joint.apply(this, arguments);
  8478. this.m_localAnchor1 = new b2Vec2();
  8479. this.m_localAnchor2 = new b2Vec2();
  8480. this.m_localXAxis1 = new b2Vec2();
  8481. this.m_localYAxis1 = new b2Vec2();
  8482. this.m_axis = new b2Vec2();
  8483. this.m_perp = new b2Vec2();
  8484. this.m_K = new b2Mat22();
  8485. this.m_impulse = new b2Vec2();
  8486. };
  8487. b2LineJoint.prototype.GetAnchorA = function () {
  8488. return this.m_bodyA.GetWorldPoint(this.m_localAnchor1);
  8489. }
  8490. b2LineJoint.prototype.GetAnchorB = function () {
  8491. return this.m_bodyB.GetWorldPoint(this.m_localAnchor2);
  8492. }
  8493. b2LineJoint.prototype.GetReactionForce = function (inv_dt) {
  8494. if (inv_dt === undefined) inv_dt = 0;
  8495. return new b2Vec2(inv_dt * (this.m_impulse.x * this.m_perp.x + (this.m_motorImpulse + this.m_impulse.y) * this.m_axis.x), inv_dt * (this.m_impulse.x * this.m_perp.y + (this.m_motorImpulse + this.m_impulse.y) * this.m_axis.y));
  8496. }
  8497. b2LineJoint.prototype.GetReactionTorque = function (inv_dt) {
  8498. if (inv_dt === undefined) inv_dt = 0;
  8499. return inv_dt * this.m_impulse.y;
  8500. }
  8501. b2LineJoint.prototype.GetJointTranslation = function () {
  8502. var bA = this.m_bodyA;
  8503. var bB = this.m_bodyB;
  8504. var tMat;
  8505. var p1 = bA.GetWorldPoint(this.m_localAnchor1);
  8506. var p2 = bB.GetWorldPoint(this.m_localAnchor2);
  8507. var dX = p2.x - p1.x;
  8508. var dY = p2.y - p1.y;
  8509. var axis = bA.GetWorldVector(this.m_localXAxis1);
  8510. var translation = axis.x * dX + axis.y * dY;
  8511. return translation;
  8512. }
  8513. b2LineJoint.prototype.GetJointSpeed = function () {
  8514. var bA = this.m_bodyA;
  8515. var bB = this.m_bodyB;
  8516. var tMat;
  8517. tMat = bA.m_xf.R;
  8518. var r1X = this.m_localAnchor1.x - bA.m_sweep.localCenter.x;
  8519. var r1Y = this.m_localAnchor1.y - bA.m_sweep.localCenter.y;
  8520. var tX = (tMat.col1.x * r1X + tMat.col2.x * r1Y);
  8521. r1Y = (tMat.col1.y * r1X + tMat.col2.y * r1Y);
  8522. r1X = tX;
  8523. tMat = bB.m_xf.R;
  8524. var r2X = this.m_localAnchor2.x - bB.m_sweep.localCenter.x;
  8525. var r2Y = this.m_localAnchor2.y - bB.m_sweep.localCenter.y;
  8526. tX = (tMat.col1.x * r2X + tMat.col2.x * r2Y);
  8527. r2Y = (tMat.col1.y * r2X + tMat.col2.y * r2Y);
  8528. r2X = tX;
  8529. var p1X = bA.m_sweep.c.x + r1X;
  8530. var p1Y = bA.m_sweep.c.y + r1Y;
  8531. var p2X = bB.m_sweep.c.x + r2X;
  8532. var p2Y = bB.m_sweep.c.y + r2Y;
  8533. var dX = p2X - p1X;
  8534. var dY = p2Y - p1Y;
  8535. var axis = bA.GetWorldVector(this.m_localXAxis1);
  8536. var v1 = bA.m_linearVelocity;
  8537. var v2 = bB.m_linearVelocity;
  8538. var w1 = bA.m_angularVelocity;
  8539. var w2 = bB.m_angularVelocity;
  8540. var speed = (dX * ((-w1 * axis.y)) + dY * (w1 * axis.x)) + (axis.x * (((v2.x + ((-w2 * r2Y))) - v1.x) - ((-w1 * r1Y))) + axis.y * (((v2.y + (w2 * r2X)) - v1.y) - (w1 * r1X)));
  8541. return speed;
  8542. }
  8543. b2LineJoint.prototype.IsLimitEnabled = function () {
  8544. return this.m_enableLimit;
  8545. }
  8546. b2LineJoint.prototype.EnableLimit = function (flag) {
  8547. this.m_bodyA.SetAwake(true);
  8548. this.m_bodyB.SetAwake(true);
  8549. this.m_enableLimit = flag;
  8550. }
  8551. b2LineJoint.prototype.GetLowerLimit = function () {
  8552. return this.m_lowerTranslation;
  8553. }
  8554. b2LineJoint.prototype.GetUpperLimit = function () {
  8555. return this.m_upperTranslation;
  8556. }
  8557. b2LineJoint.prototype.SetLimits = function (lower, upper) {
  8558. if (lower === undefined) lower = 0;
  8559. if (upper === undefined) upper = 0;
  8560. this.m_bodyA.SetAwake(true);
  8561. this.m_bodyB.SetAwake(true);
  8562. this.m_lowerTranslation = lower;
  8563. this.m_upperTranslation = upper;
  8564. }
  8565. b2LineJoint.prototype.IsMotorEnabled = function () {
  8566. return this.m_enableMotor;
  8567. }
  8568. b2LineJoint.prototype.EnableMotor = function (flag) {
  8569. this.m_bodyA.SetAwake(true);
  8570. this.m_bodyB.SetAwake(true);
  8571. this.m_enableMotor = flag;
  8572. }
  8573. b2LineJoint.prototype.SetMotorSpeed = function (speed) {
  8574. if (speed === undefined) speed = 0;
  8575. this.m_bodyA.SetAwake(true);
  8576. this.m_bodyB.SetAwake(true);
  8577. this.m_motorSpeed = speed;
  8578. }
  8579. b2LineJoint.prototype.GetMotorSpeed = function () {
  8580. return this.m_motorSpeed;
  8581. }
  8582. b2LineJoint.prototype.SetMaxMotorForce = function (force) {
  8583. if (force === undefined) force = 0;
  8584. this.m_bodyA.SetAwake(true);
  8585. this.m_bodyB.SetAwake(true);
  8586. this.m_maxMotorForce = force;
  8587. }
  8588. b2LineJoint.prototype.GetMaxMotorForce = function () {
  8589. return this.m_maxMotorForce;
  8590. }
  8591. b2LineJoint.prototype.GetMotorForce = function () {
  8592. return this.m_motorImpulse;
  8593. }
  8594. b2LineJoint.prototype.b2LineJoint = function (def) {
  8595. this.__super.b2Joint.call(this, def);
  8596. var tMat;
  8597. var tX = 0;
  8598. var tY = 0;
  8599. this.m_localAnchor1.SetV(def.localAnchorA);
  8600. this.m_localAnchor2.SetV(def.localAnchorB);
  8601. this.m_localXAxis1.SetV(def.localAxisA);
  8602. this.m_localYAxis1.x = (-this.m_localXAxis1.y);
  8603. this.m_localYAxis1.y = this.m_localXAxis1.x;
  8604. this.m_impulse.SetZero();
  8605. this.m_motorMass = 0.0;
  8606. this.m_motorImpulse = 0.0;
  8607. this.m_lowerTranslation = def.lowerTranslation;
  8608. this.m_upperTranslation = def.upperTranslation;
  8609. this.m_maxMotorForce = def.maxMotorForce;
  8610. this.m_motorSpeed = def.motorSpeed;
  8611. this.m_enableLimit = def.enableLimit;
  8612. this.m_enableMotor = def.enableMotor;
  8613. this.m_limitState = b2Joint.e_inactiveLimit;
  8614. this.m_axis.SetZero();
  8615. this.m_perp.SetZero();
  8616. }
  8617. b2LineJoint.prototype.InitVelocityConstraints = function (step) {
  8618. var bA = this.m_bodyA;
  8619. var bB = this.m_bodyB;
  8620. var tMat;
  8621. var tX = 0;
  8622. this.m_localCenterA.SetV(bA.GetLocalCenter());
  8623. this.m_localCenterB.SetV(bB.GetLocalCenter());
  8624. var xf1 = bA.GetTransform();
  8625. var xf2 = bB.GetTransform();
  8626. tMat = bA.m_xf.R;
  8627. var r1X = this.m_localAnchor1.x - this.m_localCenterA.x;
  8628. var r1Y = this.m_localAnchor1.y - this.m_localCenterA.y;
  8629. tX = (tMat.col1.x * r1X + tMat.col2.x * r1Y);
  8630. r1Y = (tMat.col1.y * r1X + tMat.col2.y * r1Y);
  8631. r1X = tX;
  8632. tMat = bB.m_xf.R;
  8633. var r2X = this.m_localAnchor2.x - this.m_localCenterB.x;
  8634. var r2Y = this.m_localAnchor2.y - this.m_localCenterB.y;
  8635. tX = (tMat.col1.x * r2X + tMat.col2.x * r2Y);
  8636. r2Y = (tMat.col1.y * r2X + tMat.col2.y * r2Y);
  8637. r2X = tX;
  8638. var dX = bB.m_sweep.c.x + r2X - bA.m_sweep.c.x - r1X;
  8639. var dY = bB.m_sweep.c.y + r2Y - bA.m_sweep.c.y - r1Y;
  8640. this.m_invMassA = bA.m_invMass;
  8641. this.m_invMassB = bB.m_invMass;
  8642. this.m_invIA = bA.m_invI;
  8643. this.m_invIB = bB.m_invI; {
  8644. this.m_axis.SetV(b2Math.MulMV(xf1.R, this.m_localXAxis1));
  8645. this.m_a1 = (dX + r1X) * this.m_axis.y - (dY + r1Y) * this.m_axis.x;
  8646. this.m_a2 = r2X * this.m_axis.y - r2Y * this.m_axis.x;
  8647. this.m_motorMass = this.m_invMassA + this.m_invMassB + this.m_invIA * this.m_a1 * this.m_a1 + this.m_invIB * this.m_a2 * this.m_a2;
  8648. this.m_motorMass = this.m_motorMass > Number.MIN_VALUE ? 1.0 / this.m_motorMass : 0.0;
  8649. } {
  8650. this.m_perp.SetV(b2Math.MulMV(xf1.R, this.m_localYAxis1));
  8651. this.m_s1 = (dX + r1X) * this.m_perp.y - (dY + r1Y) * this.m_perp.x;
  8652. this.m_s2 = r2X * this.m_perp.y - r2Y * this.m_perp.x;
  8653. var m1 = this.m_invMassA;
  8654. var m2 = this.m_invMassB;
  8655. var i1 = this.m_invIA;
  8656. var i2 = this.m_invIB;
  8657. this.m_K.col1.x = m1 + m2 + i1 * this.m_s1 * this.m_s1 + i2 * this.m_s2 * this.m_s2;
  8658. this.m_K.col1.y = i1 * this.m_s1 * this.m_a1 + i2 * this.m_s2 * this.m_a2;
  8659. this.m_K.col2.x = this.m_K.col1.y;
  8660. this.m_K.col2.y = m1 + m2 + i1 * this.m_a1 * this.m_a1 + i2 * this.m_a2 * this.m_a2;
  8661. }
  8662. if (this.m_enableLimit) {
  8663. var jointTransition = this.m_axis.x * dX + this.m_axis.y * dY;
  8664. if (b2Math.Abs(this.m_upperTranslation - this.m_lowerTranslation) < 2.0 * b2Settings.b2_linearSlop) {
  8665. this.m_limitState = b2Joint.e_equalLimits;
  8666. }
  8667. else if (jointTransition <= this.m_lowerTranslation) {
  8668. if (this.m_limitState != b2Joint.e_atLowerLimit) {
  8669. this.m_limitState = b2Joint.e_atLowerLimit;
  8670. this.m_impulse.y = 0.0;
  8671. }
  8672. }
  8673. else if (jointTransition >= this.m_upperTranslation) {
  8674. if (this.m_limitState != b2Joint.e_atUpperLimit) {
  8675. this.m_limitState = b2Joint.e_atUpperLimit;
  8676. this.m_impulse.y = 0.0;
  8677. }
  8678. }
  8679. else {
  8680. this.m_limitState = b2Joint.e_inactiveLimit;
  8681. this.m_impulse.y = 0.0;
  8682. }
  8683. }
  8684. else {
  8685. this.m_limitState = b2Joint.e_inactiveLimit;
  8686. }
  8687. if (this.m_enableMotor == false) {
  8688. this.m_motorImpulse = 0.0;
  8689. }
  8690. if (step.warmStarting) {
  8691. this.m_impulse.x *= step.dtRatio;
  8692. this.m_impulse.y *= step.dtRatio;
  8693. this.m_motorImpulse *= step.dtRatio;
  8694. var PX = this.m_impulse.x * this.m_perp.x + (this.m_motorImpulse + this.m_impulse.y) * this.m_axis.x;
  8695. var PY = this.m_impulse.x * this.m_perp.y + (this.m_motorImpulse + this.m_impulse.y) * this.m_axis.y;
  8696. var L1 = this.m_impulse.x * this.m_s1 + (this.m_motorImpulse + this.m_impulse.y) * this.m_a1;
  8697. var L2 = this.m_impulse.x * this.m_s2 + (this.m_motorImpulse + this.m_impulse.y) * this.m_a2;
  8698. bA.m_linearVelocity.x -= this.m_invMassA * PX;
  8699. bA.m_linearVelocity.y -= this.m_invMassA * PY;
  8700. bA.m_angularVelocity -= this.m_invIA * L1;
  8701. bB.m_linearVelocity.x += this.m_invMassB * PX;
  8702. bB.m_linearVelocity.y += this.m_invMassB * PY;
  8703. bB.m_angularVelocity += this.m_invIB * L2;
  8704. }
  8705. else {
  8706. this.m_impulse.SetZero();
  8707. this.m_motorImpulse = 0.0;
  8708. }
  8709. }
  8710. b2LineJoint.prototype.SolveVelocityConstraints = function (step) {
  8711. var bA = this.m_bodyA;
  8712. var bB = this.m_bodyB;
  8713. var v1 = bA.m_linearVelocity;
  8714. var w1 = bA.m_angularVelocity;
  8715. var v2 = bB.m_linearVelocity;
  8716. var w2 = bB.m_angularVelocity;
  8717. var PX = 0;
  8718. var PY = 0;
  8719. var L1 = 0;
  8720. var L2 = 0;
  8721. if (this.m_enableMotor && this.m_limitState != b2Joint.e_equalLimits) {
  8722. var Cdot = this.m_axis.x * (v2.x - v1.x) + this.m_axis.y * (v2.y - v1.y) + this.m_a2 * w2 - this.m_a1 * w1;
  8723. var impulse = this.m_motorMass * (this.m_motorSpeed - Cdot);
  8724. var oldImpulse = this.m_motorImpulse;
  8725. var maxImpulse = step.dt * this.m_maxMotorForce;
  8726. this.m_motorImpulse = b2Math.Clamp(this.m_motorImpulse + impulse, (-maxImpulse), maxImpulse);
  8727. impulse = this.m_motorImpulse - oldImpulse;
  8728. PX = impulse * this.m_axis.x;
  8729. PY = impulse * this.m_axis.y;
  8730. L1 = impulse * this.m_a1;
  8731. L2 = impulse * this.m_a2;
  8732. v1.x -= this.m_invMassA * PX;
  8733. v1.y -= this.m_invMassA * PY;
  8734. w1 -= this.m_invIA * L1;
  8735. v2.x += this.m_invMassB * PX;
  8736. v2.y += this.m_invMassB * PY;
  8737. w2 += this.m_invIB * L2;
  8738. }
  8739. var Cdot1 = this.m_perp.x * (v2.x - v1.x) + this.m_perp.y * (v2.y - v1.y) + this.m_s2 * w2 - this.m_s1 * w1;
  8740. if (this.m_enableLimit && this.m_limitState != b2Joint.e_inactiveLimit) {
  8741. var Cdot2 = this.m_axis.x * (v2.x - v1.x) + this.m_axis.y * (v2.y - v1.y) + this.m_a2 * w2 - this.m_a1 * w1;
  8742. var f1 = this.m_impulse.Copy();
  8743. var df = this.m_K.Solve(new b2Vec2(), (-Cdot1), (-Cdot2));
  8744. this.m_impulse.Add(df);
  8745. if (this.m_limitState == b2Joint.e_atLowerLimit) {
  8746. this.m_impulse.y = b2Math.Max(this.m_impulse.y, 0.0);
  8747. }
  8748. else if (this.m_limitState == b2Joint.e_atUpperLimit) {
  8749. this.m_impulse.y = b2Math.Min(this.m_impulse.y, 0.0);
  8750. }
  8751. var b = (-Cdot1) - (this.m_impulse.y - f1.y) * this.m_K.col2.x;
  8752. var f2r = 0;
  8753. if (this.m_K.col1.x != 0.0) {
  8754. f2r = b / this.m_K.col1.x + f1.x;
  8755. }
  8756. else {
  8757. f2r = f1.x;
  8758. }
  8759. this.m_impulse.x = f2r;
  8760. df.x = this.m_impulse.x - f1.x;
  8761. df.y = this.m_impulse.y - f1.y;
  8762. PX = df.x * this.m_perp.x + df.y * this.m_axis.x;
  8763. PY = df.x * this.m_perp.y + df.y * this.m_axis.y;
  8764. L1 = df.x * this.m_s1 + df.y * this.m_a1;
  8765. L2 = df.x * this.m_s2 + df.y * this.m_a2;
  8766. v1.x -= this.m_invMassA * PX;
  8767. v1.y -= this.m_invMassA * PY;
  8768. w1 -= this.m_invIA * L1;
  8769. v2.x += this.m_invMassB * PX;
  8770. v2.y += this.m_invMassB * PY;
  8771. w2 += this.m_invIB * L2;
  8772. }
  8773. else {
  8774. var df2 = 0;
  8775. if (this.m_K.col1.x != 0.0) {
  8776. df2 = ((-Cdot1)) / this.m_K.col1.x;
  8777. }
  8778. else {
  8779. df2 = 0.0;
  8780. }
  8781. this.m_impulse.x += df2;
  8782. PX = df2 * this.m_perp.x;
  8783. PY = df2 * this.m_perp.y;
  8784. L1 = df2 * this.m_s1;
  8785. L2 = df2 * this.m_s2;
  8786. v1.x -= this.m_invMassA * PX;
  8787. v1.y -= this.m_invMassA * PY;
  8788. w1 -= this.m_invIA * L1;
  8789. v2.x += this.m_invMassB * PX;
  8790. v2.y += this.m_invMassB * PY;
  8791. w2 += this.m_invIB * L2;
  8792. }
  8793. bA.m_linearVelocity.SetV(v1);
  8794. bA.m_angularVelocity = w1;
  8795. bB.m_linearVelocity.SetV(v2);
  8796. bB.m_angularVelocity = w2;
  8797. }
  8798. b2LineJoint.prototype.SolvePositionConstraints = function (baumgarte) {
  8799. if (baumgarte === undefined) baumgarte = 0;
  8800. var limitC = 0;
  8801. var oldLimitImpulse = 0;
  8802. var bA = this.m_bodyA;
  8803. var bB = this.m_bodyB;
  8804. var c1 = bA.m_sweep.c;
  8805. var a1 = bA.m_sweep.a;
  8806. var c2 = bB.m_sweep.c;
  8807. var a2 = bB.m_sweep.a;
  8808. var tMat;
  8809. var tX = 0;
  8810. var m1 = 0;
  8811. var m2 = 0;
  8812. var i1 = 0;
  8813. var i2 = 0;
  8814. var linearError = 0.0;
  8815. var angularError = 0.0;
  8816. var active = false;
  8817. var C2 = 0.0;
  8818. var R1 = b2Mat22.FromAngle(a1);
  8819. var R2 = b2Mat22.FromAngle(a2);
  8820. tMat = R1;
  8821. var r1X = this.m_localAnchor1.x - this.m_localCenterA.x;
  8822. var r1Y = this.m_localAnchor1.y - this.m_localCenterA.y;
  8823. tX = (tMat.col1.x * r1X + tMat.col2.x * r1Y);
  8824. r1Y = (tMat.col1.y * r1X + tMat.col2.y * r1Y);
  8825. r1X = tX;
  8826. tMat = R2;
  8827. var r2X = this.m_localAnchor2.x - this.m_localCenterB.x;
  8828. var r2Y = this.m_localAnchor2.y - this.m_localCenterB.y;
  8829. tX = (tMat.col1.x * r2X + tMat.col2.x * r2Y);
  8830. r2Y = (tMat.col1.y * r2X + tMat.col2.y * r2Y);
  8831. r2X = tX;
  8832. var dX = c2.x + r2X - c1.x - r1X;
  8833. var dY = c2.y + r2Y - c1.y - r1Y;
  8834. if (this.m_enableLimit) {
  8835. this.m_axis = b2Math.MulMV(R1, this.m_localXAxis1);
  8836. this.m_a1 = (dX + r1X) * this.m_axis.y - (dY + r1Y) * this.m_axis.x;
  8837. this.m_a2 = r2X * this.m_axis.y - r2Y * this.m_axis.x;
  8838. var translation = this.m_axis.x * dX + this.m_axis.y * dY;
  8839. if (b2Math.Abs(this.m_upperTranslation - this.m_lowerTranslation) < 2.0 * b2Settings.b2_linearSlop) {
  8840. C2 = b2Math.Clamp(translation, (-b2Settings.b2_maxLinearCorrection), b2Settings.b2_maxLinearCorrection);
  8841. linearError = b2Math.Abs(translation);
  8842. active = true;
  8843. }
  8844. else if (translation <= this.m_lowerTranslation) {
  8845. C2 = b2Math.Clamp(translation - this.m_lowerTranslation + b2Settings.b2_linearSlop, (-b2Settings.b2_maxLinearCorrection), 0.0);
  8846. linearError = this.m_lowerTranslation - translation;
  8847. active = true;
  8848. }
  8849. else if (translation >= this.m_upperTranslation) {
  8850. C2 = b2Math.Clamp(translation - this.m_upperTranslation + b2Settings.b2_linearSlop, 0.0, b2Settings.b2_maxLinearCorrection);
  8851. linearError = translation - this.m_upperTranslation;
  8852. active = true;
  8853. }
  8854. }
  8855. this.m_perp = b2Math.MulMV(R1, this.m_localYAxis1);
  8856. this.m_s1 = (dX + r1X) * this.m_perp.y - (dY + r1Y) * this.m_perp.x;
  8857. this.m_s2 = r2X * this.m_perp.y - r2Y * this.m_perp.x;
  8858. var impulse = new b2Vec2();
  8859. var C1 = this.m_perp.x * dX + this.m_perp.y * dY;
  8860. linearError = b2Math.Max(linearError, b2Math.Abs(C1));
  8861. angularError = 0.0;
  8862. if (active) {
  8863. m1 = this.m_invMassA;
  8864. m2 = this.m_invMassB;
  8865. i1 = this.m_invIA;
  8866. i2 = this.m_invIB;
  8867. this.m_K.col1.x = m1 + m2 + i1 * this.m_s1 * this.m_s1 + i2 * this.m_s2 * this.m_s2;
  8868. this.m_K.col1.y = i1 * this.m_s1 * this.m_a1 + i2 * this.m_s2 * this.m_a2;
  8869. this.m_K.col2.x = this.m_K.col1.y;
  8870. this.m_K.col2.y = m1 + m2 + i1 * this.m_a1 * this.m_a1 + i2 * this.m_a2 * this.m_a2;
  8871. this.m_K.Solve(impulse, (-C1), (-C2));
  8872. }
  8873. else {
  8874. m1 = this.m_invMassA;
  8875. m2 = this.m_invMassB;
  8876. i1 = this.m_invIA;
  8877. i2 = this.m_invIB;
  8878. var k11 = m1 + m2 + i1 * this.m_s1 * this.m_s1 + i2 * this.m_s2 * this.m_s2;
  8879. var impulse1 = 0;
  8880. if (k11 != 0.0) {
  8881. impulse1 = ((-C1)) / k11;
  8882. }
  8883. else {
  8884. impulse1 = 0.0;
  8885. }
  8886. impulse.x = impulse1;
  8887. impulse.y = 0.0;
  8888. }
  8889. var PX = impulse.x * this.m_perp.x + impulse.y * this.m_axis.x;
  8890. var PY = impulse.x * this.m_perp.y + impulse.y * this.m_axis.y;
  8891. var L1 = impulse.x * this.m_s1 + impulse.y * this.m_a1;
  8892. var L2 = impulse.x * this.m_s2 + impulse.y * this.m_a2;
  8893. c1.x -= this.m_invMassA * PX;
  8894. c1.y -= this.m_invMassA * PY;
  8895. a1 -= this.m_invIA * L1;
  8896. c2.x += this.m_invMassB * PX;
  8897. c2.y += this.m_invMassB * PY;
  8898. a2 += this.m_invIB * L2;
  8899. bA.m_sweep.a = a1;
  8900. bB.m_sweep.a = a2;
  8901. bA.SynchronizeTransform();
  8902. bB.SynchronizeTransform();
  8903. return linearError <= b2Settings.b2_linearSlop && angularError <= b2Settings.b2_angularSlop;
  8904. }
  8905. Box2D.inherit(b2LineJointDef, Box2D.Dynamics.Joints.b2JointDef);
  8906. b2LineJointDef.prototype.__super = Box2D.Dynamics.Joints.b2JointDef.prototype;
  8907. b2LineJointDef.b2LineJointDef = function () {
  8908. Box2D.Dynamics.Joints.b2JointDef.b2JointDef.apply(this, arguments);
  8909. this.localAnchorA = new b2Vec2();
  8910. this.localAnchorB = new b2Vec2();
  8911. this.localAxisA = new b2Vec2();
  8912. };
  8913. b2LineJointDef.prototype.b2LineJointDef = function () {
  8914. this.__super.b2JointDef.call(this);
  8915. this.type = b2Joint.e_lineJoint;
  8916. this.localAxisA.Set(1.0, 0.0);
  8917. this.enableLimit = false;
  8918. this.lowerTranslation = 0.0;
  8919. this.upperTranslation = 0.0;
  8920. this.enableMotor = false;
  8921. this.maxMotorForce = 0.0;
  8922. this.motorSpeed = 0.0;
  8923. }
  8924. b2LineJointDef.prototype.Initialize = function (bA, bB, anchor, axis) {
  8925. this.bodyA = bA;
  8926. this.bodyB = bB;
  8927. this.localAnchorA = this.bodyA.GetLocalPoint(anchor);
  8928. this.localAnchorB = this.bodyB.GetLocalPoint(anchor);
  8929. this.localAxisA = this.bodyA.GetLocalVector(axis);
  8930. }
  8931. Box2D.inherit(b2MouseJoint, Box2D.Dynamics.Joints.b2Joint);
  8932. b2MouseJoint.prototype.__super = Box2D.Dynamics.Joints.b2Joint.prototype;
  8933. b2MouseJoint.b2MouseJoint = function () {
  8934. Box2D.Dynamics.Joints.b2Joint.b2Joint.apply(this, arguments);
  8935. this.K = new b2Mat22();
  8936. this.K1 = new b2Mat22();
  8937. this.K2 = new b2Mat22();
  8938. this.m_localAnchor = new b2Vec2();
  8939. this.m_target = new b2Vec2();
  8940. this.m_impulse = new b2Vec2();
  8941. this.m_mass = new b2Mat22();
  8942. this.m_C = new b2Vec2();
  8943. };
  8944. b2MouseJoint.prototype.GetAnchorA = function () {
  8945. return this.m_target;
  8946. }
  8947. b2MouseJoint.prototype.GetAnchorB = function () {
  8948. return this.m_bodyB.GetWorldPoint(this.m_localAnchor);
  8949. }
  8950. b2MouseJoint.prototype.GetReactionForce = function (inv_dt) {
  8951. if (inv_dt === undefined) inv_dt = 0;
  8952. return new b2Vec2(inv_dt * this.m_impulse.x, inv_dt * this.m_impulse.y);
  8953. }
  8954. b2MouseJoint.prototype.GetReactionTorque = function (inv_dt) {
  8955. if (inv_dt === undefined) inv_dt = 0;
  8956. return 0.0;
  8957. }
  8958. b2MouseJoint.prototype.GetTarget = function () {
  8959. return this.m_target;
  8960. }
  8961. b2MouseJoint.prototype.SetTarget = function (target) {
  8962. if (this.m_bodyB.IsAwake() == false) {
  8963. this.m_bodyB.SetAwake(true);
  8964. }
  8965. this.m_target = target;
  8966. }
  8967. b2MouseJoint.prototype.GetMaxForce = function () {
  8968. return this.m_maxForce;
  8969. }
  8970. b2MouseJoint.prototype.SetMaxForce = function (maxForce) {
  8971. if (maxForce === undefined) maxForce = 0;
  8972. this.m_maxForce = maxForce;
  8973. }
  8974. b2MouseJoint.prototype.GetFrequency = function () {
  8975. return this.m_frequencyHz;
  8976. }
  8977. b2MouseJoint.prototype.SetFrequency = function (hz) {
  8978. if (hz === undefined) hz = 0;
  8979. this.m_frequencyHz = hz;
  8980. }
  8981. b2MouseJoint.prototype.GetDampingRatio = function () {
  8982. return this.m_dampingRatio;
  8983. }
  8984. b2MouseJoint.prototype.SetDampingRatio = function (ratio) {
  8985. if (ratio === undefined) ratio = 0;
  8986. this.m_dampingRatio = ratio;
  8987. }
  8988. b2MouseJoint.prototype.b2MouseJoint = function (def) {
  8989. this.__super.b2Joint.call(this, def);
  8990. this.m_target.SetV(def.target);
  8991. var tX = this.m_target.x - this.m_bodyB.m_xf.position.x;
  8992. var tY = this.m_target.y - this.m_bodyB.m_xf.position.y;
  8993. var tMat = this.m_bodyB.m_xf.R;
  8994. this.m_localAnchor.x = (tX * tMat.col1.x + tY * tMat.col1.y);
  8995. this.m_localAnchor.y = (tX * tMat.col2.x + tY * tMat.col2.y);
  8996. this.m_maxForce = def.maxForce;
  8997. this.m_impulse.SetZero();
  8998. this.m_frequencyHz = def.frequencyHz;
  8999. this.m_dampingRatio = def.dampingRatio;
  9000. this.m_beta = 0.0;
  9001. this.m_gamma = 0.0;
  9002. }
  9003. b2MouseJoint.prototype.InitVelocityConstraints = function (step) {
  9004. var b = this.m_bodyB;
  9005. var mass = b.GetMass();
  9006. var omega = 2.0 * Math.PI * this.m_frequencyHz;
  9007. var d = 2.0 * mass * this.m_dampingRatio * omega;
  9008. var k = mass * omega * omega;
  9009. this.m_gamma = step.dt * (d + step.dt * k);
  9010. this.m_gamma = this.m_gamma != 0 ? 1 / this.m_gamma : 0.0;
  9011. this.m_beta = step.dt * k * this.m_gamma;
  9012. var tMat;tMat = b.m_xf.R;
  9013. var rX = this.m_localAnchor.x - b.m_sweep.localCenter.x;
  9014. var rY = this.m_localAnchor.y - b.m_sweep.localCenter.y;
  9015. var tX = (tMat.col1.x * rX + tMat.col2.x * rY);rY = (tMat.col1.y * rX + tMat.col2.y * rY);
  9016. rX = tX;
  9017. var invMass = b.m_invMass;
  9018. var invI = b.m_invI;this.K1.col1.x = invMass;
  9019. this.K1.col2.x = 0.0;
  9020. this.K1.col1.y = 0.0;
  9021. this.K1.col2.y = invMass;
  9022. this.K2.col1.x = invI * rY * rY;
  9023. this.K2.col2.x = (-invI * rX * rY);
  9024. this.K2.col1.y = (-invI * rX * rY);
  9025. this.K2.col2.y = invI * rX * rX;
  9026. this.K.SetM(this.K1);
  9027. this.K.AddM(this.K2);
  9028. this.K.col1.x += this.m_gamma;
  9029. this.K.col2.y += this.m_gamma;
  9030. this.K.GetInverse(this.m_mass);
  9031. this.m_C.x = b.m_sweep.c.x + rX - this.m_target.x;
  9032. this.m_C.y = b.m_sweep.c.y + rY - this.m_target.y;
  9033. b.m_angularVelocity *= 0.98;
  9034. this.m_impulse.x *= step.dtRatio;
  9035. this.m_impulse.y *= step.dtRatio;
  9036. b.m_linearVelocity.x += invMass * this.m_impulse.x;
  9037. b.m_linearVelocity.y += invMass * this.m_impulse.y;
  9038. b.m_angularVelocity += invI * (rX * this.m_impulse.y - rY * this.m_impulse.x);
  9039. }
  9040. b2MouseJoint.prototype.SolveVelocityConstraints = function (step) {
  9041. var b = this.m_bodyB;
  9042. var tMat;
  9043. var tX = 0;
  9044. var tY = 0;
  9045. tMat = b.m_xf.R;
  9046. var rX = this.m_localAnchor.x - b.m_sweep.localCenter.x;
  9047. var rY = this.m_localAnchor.y - b.m_sweep.localCenter.y;
  9048. tX = (tMat.col1.x * rX + tMat.col2.x * rY);
  9049. rY = (tMat.col1.y * rX + tMat.col2.y * rY);
  9050. rX = tX;
  9051. var CdotX = b.m_linearVelocity.x + ((-b.m_angularVelocity * rY));
  9052. var CdotY = b.m_linearVelocity.y + (b.m_angularVelocity * rX);
  9053. tMat = this.m_mass;
  9054. tX = CdotX + this.m_beta * this.m_C.x + this.m_gamma * this.m_impulse.x;
  9055. tY = CdotY + this.m_beta * this.m_C.y + this.m_gamma * this.m_impulse.y;
  9056. var impulseX = (-(tMat.col1.x * tX + tMat.col2.x * tY));
  9057. var impulseY = (-(tMat.col1.y * tX + tMat.col2.y * tY));
  9058. var oldImpulseX = this.m_impulse.x;
  9059. var oldImpulseY = this.m_impulse.y;
  9060. this.m_impulse.x += impulseX;
  9061. this.m_impulse.y += impulseY;
  9062. var maxImpulse = step.dt * this.m_maxForce;
  9063. if (this.m_impulse.LengthSquared() > maxImpulse * maxImpulse) {
  9064. this.m_impulse.Multiply(maxImpulse / this.m_impulse.Length());
  9065. }
  9066. impulseX = this.m_impulse.x - oldImpulseX;
  9067. impulseY = this.m_impulse.y - oldImpulseY;
  9068. b.m_linearVelocity.x += b.m_invMass * impulseX;
  9069. b.m_linearVelocity.y += b.m_invMass * impulseY;
  9070. b.m_angularVelocity += b.m_invI * (rX * impulseY - rY * impulseX);
  9071. }
  9072. b2MouseJoint.prototype.SolvePositionConstraints = function (baumgarte) {
  9073. if (baumgarte === undefined) baumgarte = 0;
  9074. return true;
  9075. }
  9076. Box2D.inherit(b2MouseJointDef, Box2D.Dynamics.Joints.b2JointDef);
  9077. b2MouseJointDef.prototype.__super = Box2D.Dynamics.Joints.b2JointDef.prototype;
  9078. b2MouseJointDef.b2MouseJointDef = function () {
  9079. Box2D.Dynamics.Joints.b2JointDef.b2JointDef.apply(this, arguments);
  9080. this.target = new b2Vec2();
  9081. };
  9082. b2MouseJointDef.prototype.b2MouseJointDef = function () {
  9083. this.__super.b2JointDef.call(this);
  9084. this.type = b2Joint.e_mouseJoint;
  9085. this.maxForce = 0.0;
  9086. this.frequencyHz = 5.0;
  9087. this.dampingRatio = 0.7;
  9088. }
  9089. Box2D.inherit(b2PrismaticJoint, Box2D.Dynamics.Joints.b2Joint);
  9090. b2PrismaticJoint.prototype.__super = Box2D.Dynamics.Joints.b2Joint.prototype;
  9091. b2PrismaticJoint.b2PrismaticJoint = function () {
  9092. Box2D.Dynamics.Joints.b2Joint.b2Joint.apply(this, arguments);
  9093. this.m_localAnchor1 = new b2Vec2();
  9094. this.m_localAnchor2 = new b2Vec2();
  9095. this.m_localXAxis1 = new b2Vec2();
  9096. this.m_localYAxis1 = new b2Vec2();
  9097. this.m_axis = new b2Vec2();
  9098. this.m_perp = new b2Vec2();
  9099. this.m_K = new b2Mat33();
  9100. this.m_impulse = new b2Vec3();
  9101. };
  9102. b2PrismaticJoint.prototype.GetAnchorA = function () {
  9103. return this.m_bodyA.GetWorldPoint(this.m_localAnchor1);
  9104. }
  9105. b2PrismaticJoint.prototype.GetAnchorB = function () {
  9106. return this.m_bodyB.GetWorldPoint(this.m_localAnchor2);
  9107. }
  9108. b2PrismaticJoint.prototype.GetReactionForce = function (inv_dt) {
  9109. if (inv_dt === undefined) inv_dt = 0;
  9110. return new b2Vec2(inv_dt * (this.m_impulse.x * this.m_perp.x + (this.m_motorImpulse + this.m_impulse.z) * this.m_axis.x), inv_dt * (this.m_impulse.x * this.m_perp.y + (this.m_motorImpulse + this.m_impulse.z) * this.m_axis.y));
  9111. }
  9112. b2PrismaticJoint.prototype.GetReactionTorque = function (inv_dt) {
  9113. if (inv_dt === undefined) inv_dt = 0;
  9114. return inv_dt * this.m_impulse.y;
  9115. }
  9116. b2PrismaticJoint.prototype.GetJointTranslation = function () {
  9117. var bA = this.m_bodyA;
  9118. var bB = this.m_bodyB;
  9119. var tMat;
  9120. var p1 = bA.GetWorldPoint(this.m_localAnchor1);
  9121. var p2 = bB.GetWorldPoint(this.m_localAnchor2);
  9122. var dX = p2.x - p1.x;
  9123. var dY = p2.y - p1.y;
  9124. var axis = bA.GetWorldVector(this.m_localXAxis1);
  9125. var translation = axis.x * dX + axis.y * dY;
  9126. return translation;
  9127. }
  9128. b2PrismaticJoint.prototype.GetJointSpeed = function () {
  9129. var bA = this.m_bodyA;
  9130. var bB = this.m_bodyB;
  9131. var tMat;
  9132. tMat = bA.m_xf.R;
  9133. var r1X = this.m_localAnchor1.x - bA.m_sweep.localCenter.x;
  9134. var r1Y = this.m_localAnchor1.y - bA.m_sweep.localCenter.y;
  9135. var tX = (tMat.col1.x * r1X + tMat.col2.x * r1Y);
  9136. r1Y = (tMat.col1.y * r1X + tMat.col2.y * r1Y);
  9137. r1X = tX;
  9138. tMat = bB.m_xf.R;
  9139. var r2X = this.m_localAnchor2.x - bB.m_sweep.localCenter.x;
  9140. var r2Y = this.m_localAnchor2.y - bB.m_sweep.localCenter.y;
  9141. tX = (tMat.col1.x * r2X + tMat.col2.x * r2Y);
  9142. r2Y = (tMat.col1.y * r2X + tMat.col2.y * r2Y);
  9143. r2X = tX;
  9144. var p1X = bA.m_sweep.c.x + r1X;
  9145. var p1Y = bA.m_sweep.c.y + r1Y;
  9146. var p2X = bB.m_sweep.c.x + r2X;
  9147. var p2Y = bB.m_sweep.c.y + r2Y;
  9148. var dX = p2X - p1X;
  9149. var dY = p2Y - p1Y;
  9150. var axis = bA.GetWorldVector(this.m_localXAxis1);
  9151. var v1 = bA.m_linearVelocity;
  9152. var v2 = bB.m_linearVelocity;
  9153. var w1 = bA.m_angularVelocity;
  9154. var w2 = bB.m_angularVelocity;
  9155. var speed = (dX * ((-w1 * axis.y)) + dY * (w1 * axis.x)) + (axis.x * (((v2.x + ((-w2 * r2Y))) - v1.x) - ((-w1 * r1Y))) + axis.y * (((v2.y + (w2 * r2X)) - v1.y) - (w1 * r1X)));
  9156. return speed;
  9157. }
  9158. b2PrismaticJoint.prototype.IsLimitEnabled = function () {
  9159. return this.m_enableLimit;
  9160. }
  9161. b2PrismaticJoint.prototype.EnableLimit = function (flag) {
  9162. this.m_bodyA.SetAwake(true);
  9163. this.m_bodyB.SetAwake(true);
  9164. this.m_enableLimit = flag;
  9165. }
  9166. b2PrismaticJoint.prototype.GetLowerLimit = function () {
  9167. return this.m_lowerTranslation;
  9168. }
  9169. b2PrismaticJoint.prototype.GetUpperLimit = function () {
  9170. return this.m_upperTranslation;
  9171. }
  9172. b2PrismaticJoint.prototype.SetLimits = function (lower, upper) {
  9173. if (lower === undefined) lower = 0;
  9174. if (upper === undefined) upper = 0;
  9175. this.m_bodyA.SetAwake(true);
  9176. this.m_bodyB.SetAwake(true);
  9177. this.m_lowerTranslation = lower;
  9178. this.m_upperTranslation = upper;
  9179. }
  9180. b2PrismaticJoint.prototype.IsMotorEnabled = function () {
  9181. return this.m_enableMotor;
  9182. }
  9183. b2PrismaticJoint.prototype.EnableMotor = function (flag) {
  9184. this.m_bodyA.SetAwake(true);
  9185. this.m_bodyB.SetAwake(true);
  9186. this.m_enableMotor = flag;
  9187. }
  9188. b2PrismaticJoint.prototype.SetMotorSpeed = function (speed) {
  9189. if (speed === undefined) speed = 0;
  9190. this.m_bodyA.SetAwake(true);
  9191. this.m_bodyB.SetAwake(true);
  9192. this.m_motorSpeed = speed;
  9193. }
  9194. b2PrismaticJoint.prototype.GetMotorSpeed = function () {
  9195. return this.m_motorSpeed;
  9196. }
  9197. b2PrismaticJoint.prototype.SetMaxMotorForce = function (force) {
  9198. if (force === undefined) force = 0;
  9199. this.m_bodyA.SetAwake(true);
  9200. this.m_bodyB.SetAwake(true);
  9201. this.m_maxMotorForce = force;
  9202. }
  9203. b2PrismaticJoint.prototype.GetMotorForce = function () {
  9204. return this.m_motorImpulse;
  9205. }
  9206. b2PrismaticJoint.prototype.b2PrismaticJoint = function (def) {
  9207. this.__super.b2Joint.call(this, def);
  9208. var tMat;
  9209. var tX = 0;
  9210. var tY = 0;
  9211. this.m_localAnchor1.SetV(def.localAnchorA);
  9212. this.m_localAnchor2.SetV(def.localAnchorB);
  9213. this.m_localXAxis1.SetV(def.localAxisA);
  9214. this.m_localYAxis1.x = (-this.m_localXAxis1.y);
  9215. this.m_localYAxis1.y = this.m_localXAxis1.x;
  9216. this.m_refAngle = def.referenceAngle;
  9217. this.m_impulse.SetZero();
  9218. this.m_motorMass = 0.0;
  9219. this.m_motorImpulse = 0.0;
  9220. this.m_lowerTranslation = def.lowerTranslation;
  9221. this.m_upperTranslation = def.upperTranslation;
  9222. this.m_maxMotorForce = def.maxMotorForce;
  9223. this.m_motorSpeed = def.motorSpeed;
  9224. this.m_enableLimit = def.enableLimit;
  9225. this.m_enableMotor = def.enableMotor;
  9226. this.m_limitState = b2Joint.e_inactiveLimit;
  9227. this.m_axis.SetZero();
  9228. this.m_perp.SetZero();
  9229. }
  9230. b2PrismaticJoint.prototype.InitVelocityConstraints = function (step) {
  9231. var bA = this.m_bodyA;
  9232. var bB = this.m_bodyB;
  9233. var tMat;
  9234. var tX = 0;
  9235. this.m_localCenterA.SetV(bA.GetLocalCenter());
  9236. this.m_localCenterB.SetV(bB.GetLocalCenter());
  9237. var xf1 = bA.GetTransform();
  9238. var xf2 = bB.GetTransform();
  9239. tMat = bA.m_xf.R;
  9240. var r1X = this.m_localAnchor1.x - this.m_localCenterA.x;
  9241. var r1Y = this.m_localAnchor1.y - this.m_localCenterA.y;
  9242. tX = (tMat.col1.x * r1X + tMat.col2.x * r1Y);
  9243. r1Y = (tMat.col1.y * r1X + tMat.col2.y * r1Y);
  9244. r1X = tX;
  9245. tMat = bB.m_xf.R;
  9246. var r2X = this.m_localAnchor2.x - this.m_localCenterB.x;
  9247. var r2Y = this.m_localAnchor2.y - this.m_localCenterB.y;
  9248. tX = (tMat.col1.x * r2X + tMat.col2.x * r2Y);
  9249. r2Y = (tMat.col1.y * r2X + tMat.col2.y * r2Y);
  9250. r2X = tX;
  9251. var dX = bB.m_sweep.c.x + r2X - bA.m_sweep.c.x - r1X;
  9252. var dY = bB.m_sweep.c.y + r2Y - bA.m_sweep.c.y - r1Y;
  9253. this.m_invMassA = bA.m_invMass;
  9254. this.m_invMassB = bB.m_invMass;
  9255. this.m_invIA = bA.m_invI;
  9256. this.m_invIB = bB.m_invI; {
  9257. this.m_axis.SetV(b2Math.MulMV(xf1.R, this.m_localXAxis1));
  9258. this.m_a1 = (dX + r1X) * this.m_axis.y - (dY + r1Y) * this.m_axis.x;
  9259. this.m_a2 = r2X * this.m_axis.y - r2Y * this.m_axis.x;
  9260. this.m_motorMass = this.m_invMassA + this.m_invMassB + this.m_invIA * this.m_a1 * this.m_a1 + this.m_invIB * this.m_a2 * this.m_a2;
  9261. if (this.m_motorMass > Number.MIN_VALUE) this.m_motorMass = 1.0 / this.m_motorMass;
  9262. } {
  9263. this.m_perp.SetV(b2Math.MulMV(xf1.R, this.m_localYAxis1));
  9264. this.m_s1 = (dX + r1X) * this.m_perp.y - (dY + r1Y) * this.m_perp.x;
  9265. this.m_s2 = r2X * this.m_perp.y - r2Y * this.m_perp.x;
  9266. var m1 = this.m_invMassA;
  9267. var m2 = this.m_invMassB;
  9268. var i1 = this.m_invIA;
  9269. var i2 = this.m_invIB;
  9270. this.m_K.col1.x = m1 + m2 + i1 * this.m_s1 * this.m_s1 + i2 * this.m_s2 * this.m_s2;
  9271. this.m_K.col1.y = i1 * this.m_s1 + i2 * this.m_s2;
  9272. this.m_K.col1.z = i1 * this.m_s1 * this.m_a1 + i2 * this.m_s2 * this.m_a2;
  9273. this.m_K.col2.x = this.m_K.col1.y;
  9274. this.m_K.col2.y = i1 + i2;
  9275. this.m_K.col2.z = i1 * this.m_a1 + i2 * this.m_a2;
  9276. this.m_K.col3.x = this.m_K.col1.z;
  9277. this.m_K.col3.y = this.m_K.col2.z;
  9278. this.m_K.col3.z = m1 + m2 + i1 * this.m_a1 * this.m_a1 + i2 * this.m_a2 * this.m_a2;
  9279. }
  9280. if (this.m_enableLimit) {
  9281. var jointTransition = this.m_axis.x * dX + this.m_axis.y * dY;
  9282. if (b2Math.Abs(this.m_upperTranslation - this.m_lowerTranslation) < 2.0 * b2Settings.b2_linearSlop) {
  9283. this.m_limitState = b2Joint.e_equalLimits;
  9284. }
  9285. else if (jointTransition <= this.m_lowerTranslation) {
  9286. if (this.m_limitState != b2Joint.e_atLowerLimit) {
  9287. this.m_limitState = b2Joint.e_atLowerLimit;
  9288. this.m_impulse.z = 0.0;
  9289. }
  9290. }
  9291. else if (jointTransition >= this.m_upperTranslation) {
  9292. if (this.m_limitState != b2Joint.e_atUpperLimit) {
  9293. this.m_limitState = b2Joint.e_atUpperLimit;
  9294. this.m_impulse.z = 0.0;
  9295. }
  9296. }
  9297. else {
  9298. this.m_limitState = b2Joint.e_inactiveLimit;
  9299. this.m_impulse.z = 0.0;
  9300. }
  9301. }
  9302. else {
  9303. this.m_limitState = b2Joint.e_inactiveLimit;
  9304. }
  9305. if (this.m_enableMotor == false) {
  9306. this.m_motorImpulse = 0.0;
  9307. }
  9308. if (step.warmStarting) {
  9309. this.m_impulse.x *= step.dtRatio;
  9310. this.m_impulse.y *= step.dtRatio;
  9311. this.m_motorImpulse *= step.dtRatio;
  9312. var PX = this.m_impulse.x * this.m_perp.x + (this.m_motorImpulse + this.m_impulse.z) * this.m_axis.x;
  9313. var PY = this.m_impulse.x * this.m_perp.y + (this.m_motorImpulse + this.m_impulse.z) * this.m_axis.y;
  9314. var L1 = this.m_impulse.x * this.m_s1 + this.m_impulse.y + (this.m_motorImpulse + this.m_impulse.z) * this.m_a1;
  9315. var L2 = this.m_impulse.x * this.m_s2 + this.m_impulse.y + (this.m_motorImpulse + this.m_impulse.z) * this.m_a2;
  9316. bA.m_linearVelocity.x -= this.m_invMassA * PX;
  9317. bA.m_linearVelocity.y -= this.m_invMassA * PY;
  9318. bA.m_angularVelocity -= this.m_invIA * L1;
  9319. bB.m_linearVelocity.x += this.m_invMassB * PX;
  9320. bB.m_linearVelocity.y += this.m_invMassB * PY;
  9321. bB.m_angularVelocity += this.m_invIB * L2;
  9322. }
  9323. else {
  9324. this.m_impulse.SetZero();
  9325. this.m_motorImpulse = 0.0;
  9326. }
  9327. }
  9328. b2PrismaticJoint.prototype.SolveVelocityConstraints = function (step) {
  9329. var bA = this.m_bodyA;
  9330. var bB = this.m_bodyB;
  9331. var v1 = bA.m_linearVelocity;
  9332. var w1 = bA.m_angularVelocity;
  9333. var v2 = bB.m_linearVelocity;
  9334. var w2 = bB.m_angularVelocity;
  9335. var PX = 0;
  9336. var PY = 0;
  9337. var L1 = 0;
  9338. var L2 = 0;
  9339. if (this.m_enableMotor && this.m_limitState != b2Joint.e_equalLimits) {
  9340. var Cdot = this.m_axis.x * (v2.x - v1.x) + this.m_axis.y * (v2.y - v1.y) + this.m_a2 * w2 - this.m_a1 * w1;
  9341. var impulse = this.m_motorMass * (this.m_motorSpeed - Cdot);
  9342. var oldImpulse = this.m_motorImpulse;
  9343. var maxImpulse = step.dt * this.m_maxMotorForce;
  9344. this.m_motorImpulse = b2Math.Clamp(this.m_motorImpulse + impulse, (-maxImpulse), maxImpulse);
  9345. impulse = this.m_motorImpulse - oldImpulse;
  9346. PX = impulse * this.m_axis.x;
  9347. PY = impulse * this.m_axis.y;
  9348. L1 = impulse * this.m_a1;
  9349. L2 = impulse * this.m_a2;
  9350. v1.x -= this.m_invMassA * PX;
  9351. v1.y -= this.m_invMassA * PY;
  9352. w1 -= this.m_invIA * L1;
  9353. v2.x += this.m_invMassB * PX;
  9354. v2.y += this.m_invMassB * PY;
  9355. w2 += this.m_invIB * L2;
  9356. }
  9357. var Cdot1X = this.m_perp.x * (v2.x - v1.x) + this.m_perp.y * (v2.y - v1.y) + this.m_s2 * w2 - this.m_s1 * w1;
  9358. var Cdot1Y = w2 - w1;
  9359. if (this.m_enableLimit && this.m_limitState != b2Joint.e_inactiveLimit) {
  9360. var Cdot2 = this.m_axis.x * (v2.x - v1.x) + this.m_axis.y * (v2.y - v1.y) + this.m_a2 * w2 - this.m_a1 * w1;
  9361. var f1 = this.m_impulse.Copy();
  9362. var df = this.m_K.Solve33(new b2Vec3(), (-Cdot1X), (-Cdot1Y), (-Cdot2));
  9363. this.m_impulse.Add(df);
  9364. if (this.m_limitState == b2Joint.e_atLowerLimit) {
  9365. this.m_impulse.z = b2Math.Max(this.m_impulse.z, 0.0);
  9366. }
  9367. else if (this.m_limitState == b2Joint.e_atUpperLimit) {
  9368. this.m_impulse.z = b2Math.Min(this.m_impulse.z, 0.0);
  9369. }
  9370. var bX = (-Cdot1X) - (this.m_impulse.z - f1.z) * this.m_K.col3.x;
  9371. var bY = (-Cdot1Y) - (this.m_impulse.z - f1.z) * this.m_K.col3.y;
  9372. var f2r = this.m_K.Solve22(new b2Vec2(), bX, bY);
  9373. f2r.x += f1.x;
  9374. f2r.y += f1.y;
  9375. this.m_impulse.x = f2r.x;
  9376. this.m_impulse.y = f2r.y;
  9377. df.x = this.m_impulse.x - f1.x;
  9378. df.y = this.m_impulse.y - f1.y;
  9379. df.z = this.m_impulse.z - f1.z;
  9380. PX = df.x * this.m_perp.x + df.z * this.m_axis.x;
  9381. PY = df.x * this.m_perp.y + df.z * this.m_axis.y;
  9382. L1 = df.x * this.m_s1 + df.y + df.z * this.m_a1;
  9383. L2 = df.x * this.m_s2 + df.y + df.z * this.m_a2;
  9384. v1.x -= this.m_invMassA * PX;
  9385. v1.y -= this.m_invMassA * PY;
  9386. w1 -= this.m_invIA * L1;
  9387. v2.x += this.m_invMassB * PX;
  9388. v2.y += this.m_invMassB * PY;
  9389. w2 += this.m_invIB * L2;
  9390. }
  9391. else {
  9392. var df2 = this.m_K.Solve22(new b2Vec2(), (-Cdot1X), (-Cdot1Y));
  9393. this.m_impulse.x += df2.x;
  9394. this.m_impulse.y += df2.y;
  9395. PX = df2.x * this.m_perp.x;
  9396. PY = df2.x * this.m_perp.y;
  9397. L1 = df2.x * this.m_s1 + df2.y;
  9398. L2 = df2.x * this.m_s2 + df2.y;
  9399. v1.x -= this.m_invMassA * PX;
  9400. v1.y -= this.m_invMassA * PY;
  9401. w1 -= this.m_invIA * L1;
  9402. v2.x += this.m_invMassB * PX;
  9403. v2.y += this.m_invMassB * PY;
  9404. w2 += this.m_invIB * L2;
  9405. }
  9406. bA.m_linearVelocity.SetV(v1);
  9407. bA.m_angularVelocity = w1;
  9408. bB.m_linearVelocity.SetV(v2);
  9409. bB.m_angularVelocity = w2;
  9410. }
  9411. b2PrismaticJoint.prototype.SolvePositionConstraints = function (baumgarte) {
  9412. if (baumgarte === undefined) baumgarte = 0;
  9413. var limitC = 0;
  9414. var oldLimitImpulse = 0;
  9415. var bA = this.m_bodyA;
  9416. var bB = this.m_bodyB;
  9417. var c1 = bA.m_sweep.c;
  9418. var a1 = bA.m_sweep.a;
  9419. var c2 = bB.m_sweep.c;
  9420. var a2 = bB.m_sweep.a;
  9421. var tMat;
  9422. var tX = 0;
  9423. var m1 = 0;
  9424. var m2 = 0;
  9425. var i1 = 0;
  9426. var i2 = 0;
  9427. var linearError = 0.0;
  9428. var angularError = 0.0;
  9429. var active = false;
  9430. var C2 = 0.0;
  9431. var R1 = b2Mat22.FromAngle(a1);
  9432. var R2 = b2Mat22.FromAngle(a2);
  9433. tMat = R1;
  9434. var r1X = this.m_localAnchor1.x - this.m_localCenterA.x;
  9435. var r1Y = this.m_localAnchor1.y - this.m_localCenterA.y;
  9436. tX = (tMat.col1.x * r1X + tMat.col2.x * r1Y);
  9437. r1Y = (tMat.col1.y * r1X + tMat.col2.y * r1Y);
  9438. r1X = tX;
  9439. tMat = R2;
  9440. var r2X = this.m_localAnchor2.x - this.m_localCenterB.x;
  9441. var r2Y = this.m_localAnchor2.y - this.m_localCenterB.y;
  9442. tX = (tMat.col1.x * r2X + tMat.col2.x * r2Y);
  9443. r2Y = (tMat.col1.y * r2X + tMat.col2.y * r2Y);
  9444. r2X = tX;
  9445. var dX = c2.x + r2X - c1.x - r1X;
  9446. var dY = c2.y + r2Y - c1.y - r1Y;
  9447. if (this.m_enableLimit) {
  9448. this.m_axis = b2Math.MulMV(R1, this.m_localXAxis1);
  9449. this.m_a1 = (dX + r1X) * this.m_axis.y - (dY + r1Y) * this.m_axis.x;
  9450. this.m_a2 = r2X * this.m_axis.y - r2Y * this.m_axis.x;
  9451. var translation = this.m_axis.x * dX + this.m_axis.y * dY;
  9452. if (b2Math.Abs(this.m_upperTranslation - this.m_lowerTranslation) < 2.0 * b2Settings.b2_linearSlop) {
  9453. C2 = b2Math.Clamp(translation, (-b2Settings.b2_maxLinearCorrection), b2Settings.b2_maxLinearCorrection);
  9454. linearError = b2Math.Abs(translation);
  9455. active = true;
  9456. }
  9457. else if (translation <= this.m_lowerTranslation) {
  9458. C2 = b2Math.Clamp(translation - this.m_lowerTranslation + b2Settings.b2_linearSlop, (-b2Settings.b2_maxLinearCorrection), 0.0);
  9459. linearError = this.m_lowerTranslation - translation;
  9460. active = true;
  9461. }
  9462. else if (translation >= this.m_upperTranslation) {
  9463. C2 = b2Math.Clamp(translation - this.m_upperTranslation + b2Settings.b2_linearSlop, 0.0, b2Settings.b2_maxLinearCorrection);
  9464. linearError = translation - this.m_upperTranslation;
  9465. active = true;
  9466. }
  9467. }
  9468. this.m_perp = b2Math.MulMV(R1, this.m_localYAxis1);
  9469. this.m_s1 = (dX + r1X) * this.m_perp.y - (dY + r1Y) * this.m_perp.x;
  9470. this.m_s2 = r2X * this.m_perp.y - r2Y * this.m_perp.x;
  9471. var impulse = new b2Vec3();
  9472. var C1X = this.m_perp.x * dX + this.m_perp.y * dY;
  9473. var C1Y = a2 - a1 - this.m_refAngle;
  9474. linearError = b2Math.Max(linearError, b2Math.Abs(C1X));
  9475. angularError = b2Math.Abs(C1Y);
  9476. if (active) {
  9477. m1 = this.m_invMassA;
  9478. m2 = this.m_invMassB;
  9479. i1 = this.m_invIA;
  9480. i2 = this.m_invIB;
  9481. this.m_K.col1.x = m1 + m2 + i1 * this.m_s1 * this.m_s1 + i2 * this.m_s2 * this.m_s2;
  9482. this.m_K.col1.y = i1 * this.m_s1 + i2 * this.m_s2;
  9483. this.m_K.col1.z = i1 * this.m_s1 * this.m_a1 + i2 * this.m_s2 * this.m_a2;
  9484. this.m_K.col2.x = this.m_K.col1.y;
  9485. this.m_K.col2.y = i1 + i2;
  9486. this.m_K.col2.z = i1 * this.m_a1 + i2 * this.m_a2;
  9487. this.m_K.col3.x = this.m_K.col1.z;
  9488. this.m_K.col3.y = this.m_K.col2.z;
  9489. this.m_K.col3.z = m1 + m2 + i1 * this.m_a1 * this.m_a1 + i2 * this.m_a2 * this.m_a2;
  9490. this.m_K.Solve33(impulse, (-C1X), (-C1Y), (-C2));
  9491. }
  9492. else {
  9493. m1 = this.m_invMassA;
  9494. m2 = this.m_invMassB;
  9495. i1 = this.m_invIA;
  9496. i2 = this.m_invIB;
  9497. var k11 = m1 + m2 + i1 * this.m_s1 * this.m_s1 + i2 * this.m_s2 * this.m_s2;
  9498. var k12 = i1 * this.m_s1 + i2 * this.m_s2;
  9499. var k22 = i1 + i2;
  9500. this.m_K.col1.Set(k11, k12, 0.0);
  9501. this.m_K.col2.Set(k12, k22, 0.0);
  9502. var impulse1 = this.m_K.Solve22(new b2Vec2(), (-C1X), (-C1Y));
  9503. impulse.x = impulse1.x;
  9504. impulse.y = impulse1.y;
  9505. impulse.z = 0.0;
  9506. }
  9507. var PX = impulse.x * this.m_perp.x + impulse.z * this.m_axis.x;
  9508. var PY = impulse.x * this.m_perp.y + impulse.z * this.m_axis.y;
  9509. var L1 = impulse.x * this.m_s1 + impulse.y + impulse.z * this.m_a1;
  9510. var L2 = impulse.x * this.m_s2 + impulse.y + impulse.z * this.m_a2;
  9511. c1.x -= this.m_invMassA * PX;
  9512. c1.y -= this.m_invMassA * PY;
  9513. a1 -= this.m_invIA * L1;
  9514. c2.x += this.m_invMassB * PX;
  9515. c2.y += this.m_invMassB * PY;
  9516. a2 += this.m_invIB * L2;
  9517. bA.m_sweep.a = a1;
  9518. bB.m_sweep.a = a2;
  9519. bA.SynchronizeTransform();
  9520. bB.SynchronizeTransform();
  9521. return linearError <= b2Settings.b2_linearSlop && angularError <= b2Settings.b2_angularSlop;
  9522. }
  9523. Box2D.inherit(b2PrismaticJointDef, Box2D.Dynamics.Joints.b2JointDef);
  9524. b2PrismaticJointDef.prototype.__super = Box2D.Dynamics.Joints.b2JointDef.prototype;
  9525. b2PrismaticJointDef.b2PrismaticJointDef = function () {
  9526. Box2D.Dynamics.Joints.b2JointDef.b2JointDef.apply(this, arguments);
  9527. this.localAnchorA = new b2Vec2();
  9528. this.localAnchorB = new b2Vec2();
  9529. this.localAxisA = new b2Vec2();
  9530. };
  9531. b2PrismaticJointDef.prototype.b2PrismaticJointDef = function () {
  9532. this.__super.b2JointDef.call(this);
  9533. this.type = b2Joint.e_prismaticJoint;
  9534. this.localAxisA.Set(1.0, 0.0);
  9535. this.referenceAngle = 0.0;
  9536. this.enableLimit = false;
  9537. this.lowerTranslation = 0.0;
  9538. this.upperTranslation = 0.0;
  9539. this.enableMotor = false;
  9540. this.maxMotorForce = 0.0;
  9541. this.motorSpeed = 0.0;
  9542. }
  9543. b2PrismaticJointDef.prototype.Initialize = function (bA, bB, anchor, axis) {
  9544. this.bodyA = bA;
  9545. this.bodyB = bB;
  9546. this.localAnchorA = this.bodyA.GetLocalPoint(anchor);
  9547. this.localAnchorB = this.bodyB.GetLocalPoint(anchor);
  9548. this.localAxisA = this.bodyA.GetLocalVector(axis);
  9549. this.referenceAngle = this.bodyB.GetAngle() - this.bodyA.GetAngle();
  9550. }
  9551. Box2D.inherit(b2PulleyJoint, Box2D.Dynamics.Joints.b2Joint);
  9552. b2PulleyJoint.prototype.__super = Box2D.Dynamics.Joints.b2Joint.prototype;
  9553. b2PulleyJoint.b2PulleyJoint = function () {
  9554. Box2D.Dynamics.Joints.b2Joint.b2Joint.apply(this, arguments);
  9555. this.m_groundAnchor1 = new b2Vec2();
  9556. this.m_groundAnchor2 = new b2Vec2();
  9557. this.m_localAnchor1 = new b2Vec2();
  9558. this.m_localAnchor2 = new b2Vec2();
  9559. this.m_u1 = new b2Vec2();
  9560. this.m_u2 = new b2Vec2();
  9561. };
  9562. b2PulleyJoint.prototype.GetAnchorA = function () {
  9563. return this.m_bodyA.GetWorldPoint(this.m_localAnchor1);
  9564. }
  9565. b2PulleyJoint.prototype.GetAnchorB = function () {
  9566. return this.m_bodyB.GetWorldPoint(this.m_localAnchor2);
  9567. }
  9568. b2PulleyJoint.prototype.GetReactionForce = function (inv_dt) {
  9569. if (inv_dt === undefined) inv_dt = 0;
  9570. return new b2Vec2(inv_dt * this.m_impulse * this.m_u2.x, inv_dt * this.m_impulse * this.m_u2.y);
  9571. }
  9572. b2PulleyJoint.prototype.GetReactionTorque = function (inv_dt) {
  9573. if (inv_dt === undefined) inv_dt = 0;
  9574. return 0.0;
  9575. }
  9576. b2PulleyJoint.prototype.GetGroundAnchorA = function () {
  9577. var a = this.m_ground.m_xf.position.Copy();
  9578. a.Add(this.m_groundAnchor1);
  9579. return a;
  9580. }
  9581. b2PulleyJoint.prototype.GetGroundAnchorB = function () {
  9582. var a = this.m_ground.m_xf.position.Copy();
  9583. a.Add(this.m_groundAnchor2);
  9584. return a;
  9585. }
  9586. b2PulleyJoint.prototype.GetLength1 = function () {
  9587. var p = this.m_bodyA.GetWorldPoint(this.m_localAnchor1);
  9588. var sX = this.m_ground.m_xf.position.x + this.m_groundAnchor1.x;
  9589. var sY = this.m_ground.m_xf.position.y + this.m_groundAnchor1.y;
  9590. var dX = p.x - sX;
  9591. var dY = p.y - sY;
  9592. return Math.sqrt(dX * dX + dY * dY);
  9593. }
  9594. b2PulleyJoint.prototype.GetLength2 = function () {
  9595. var p = this.m_bodyB.GetWorldPoint(this.m_localAnchor2);
  9596. var sX = this.m_ground.m_xf.position.x + this.m_groundAnchor2.x;
  9597. var sY = this.m_ground.m_xf.position.y + this.m_groundAnchor2.y;
  9598. var dX = p.x - sX;
  9599. var dY = p.y - sY;
  9600. return Math.sqrt(dX * dX + dY * dY);
  9601. }
  9602. b2PulleyJoint.prototype.GetRatio = function () {
  9603. return this.m_ratio;
  9604. }
  9605. b2PulleyJoint.prototype.b2PulleyJoint = function (def) {
  9606. this.__super.b2Joint.call(this, def);
  9607. var tMat;
  9608. var tX = 0;
  9609. var tY = 0;
  9610. this.m_ground = this.m_bodyA.m_world.m_groundBody;
  9611. this.m_groundAnchor1.x = def.groundAnchorA.x - this.m_ground.m_xf.position.x;
  9612. this.m_groundAnchor1.y = def.groundAnchorA.y - this.m_ground.m_xf.position.y;
  9613. this.m_groundAnchor2.x = def.groundAnchorB.x - this.m_ground.m_xf.position.x;
  9614. this.m_groundAnchor2.y = def.groundAnchorB.y - this.m_ground.m_xf.position.y;
  9615. this.m_localAnchor1.SetV(def.localAnchorA);
  9616. this.m_localAnchor2.SetV(def.localAnchorB);
  9617. this.m_ratio = def.ratio;
  9618. this.m_constant = def.lengthA + this.m_ratio * def.lengthB;
  9619. this.m_maxLength1 = b2Math.Min(def.maxLengthA, this.m_constant - this.m_ratio * b2PulleyJoint.b2_minPulleyLength);
  9620. this.m_maxLength2 = b2Math.Min(def.maxLengthB, (this.m_constant - b2PulleyJoint.b2_minPulleyLength) / this.m_ratio);
  9621. this.m_impulse = 0.0;
  9622. this.m_limitImpulse1 = 0.0;
  9623. this.m_limitImpulse2 = 0.0;
  9624. }
  9625. b2PulleyJoint.prototype.InitVelocityConstraints = function (step) {
  9626. var bA = this.m_bodyA;
  9627. var bB = this.m_bodyB;
  9628. var tMat;
  9629. tMat = bA.m_xf.R;
  9630. var r1X = this.m_localAnchor1.x - bA.m_sweep.localCenter.x;
  9631. var r1Y = this.m_localAnchor1.y - bA.m_sweep.localCenter.y;
  9632. var tX = (tMat.col1.x * r1X + tMat.col2.x * r1Y);
  9633. r1Y = (tMat.col1.y * r1X + tMat.col2.y * r1Y);
  9634. r1X = tX;
  9635. tMat = bB.m_xf.R;
  9636. var r2X = this.m_localAnchor2.x - bB.m_sweep.localCenter.x;
  9637. var r2Y = this.m_localAnchor2.y - bB.m_sweep.localCenter.y;
  9638. tX = (tMat.col1.x * r2X + tMat.col2.x * r2Y);
  9639. r2Y = (tMat.col1.y * r2X + tMat.col2.y * r2Y);
  9640. r2X = tX;
  9641. var p1X = bA.m_sweep.c.x + r1X;
  9642. var p1Y = bA.m_sweep.c.y + r1Y;
  9643. var p2X = bB.m_sweep.c.x + r2X;
  9644. var p2Y = bB.m_sweep.c.y + r2Y;
  9645. var s1X = this.m_ground.m_xf.position.x + this.m_groundAnchor1.x;
  9646. var s1Y = this.m_ground.m_xf.position.y + this.m_groundAnchor1.y;
  9647. var s2X = this.m_ground.m_xf.position.x + this.m_groundAnchor2.x;
  9648. var s2Y = this.m_ground.m_xf.position.y + this.m_groundAnchor2.y;
  9649. this.m_u1.Set(p1X - s1X, p1Y - s1Y);
  9650. this.m_u2.Set(p2X - s2X, p2Y - s2Y);
  9651. var length1 = this.m_u1.Length();
  9652. var length2 = this.m_u2.Length();
  9653. if (length1 > b2Settings.b2_linearSlop) {
  9654. this.m_u1.Multiply(1.0 / length1);
  9655. }
  9656. else {
  9657. this.m_u1.SetZero();
  9658. }
  9659. if (length2 > b2Settings.b2_linearSlop) {
  9660. this.m_u2.Multiply(1.0 / length2);
  9661. }
  9662. else {
  9663. this.m_u2.SetZero();
  9664. }
  9665. var C = this.m_constant - length1 - this.m_ratio * length2;
  9666. if (C > 0.0) {
  9667. this.m_state = b2Joint.e_inactiveLimit;
  9668. this.m_impulse = 0.0;
  9669. }
  9670. else {
  9671. this.m_state = b2Joint.e_atUpperLimit;
  9672. }
  9673. if (length1 < this.m_maxLength1) {
  9674. this.m_limitState1 = b2Joint.e_inactiveLimit;
  9675. this.m_limitImpulse1 = 0.0;
  9676. }
  9677. else {
  9678. this.m_limitState1 = b2Joint.e_atUpperLimit;
  9679. }
  9680. if (length2 < this.m_maxLength2) {
  9681. this.m_limitState2 = b2Joint.e_inactiveLimit;
  9682. this.m_limitImpulse2 = 0.0;
  9683. }
  9684. else {
  9685. this.m_limitState2 = b2Joint.e_atUpperLimit;
  9686. }
  9687. var cr1u1 = r1X * this.m_u1.y - r1Y * this.m_u1.x;
  9688. var cr2u2 = r2X * this.m_u2.y - r2Y * this.m_u2.x;
  9689. this.m_limitMass1 = bA.m_invMass + bA.m_invI * cr1u1 * cr1u1;
  9690. this.m_limitMass2 = bB.m_invMass + bB.m_invI * cr2u2 * cr2u2;
  9691. this.m_pulleyMass = this.m_limitMass1 + this.m_ratio * this.m_ratio * this.m_limitMass2;
  9692. this.m_limitMass1 = 1.0 / this.m_limitMass1;
  9693. this.m_limitMass2 = 1.0 / this.m_limitMass2;
  9694. this.m_pulleyMass = 1.0 / this.m_pulleyMass;
  9695. if (step.warmStarting) {
  9696. this.m_impulse *= step.dtRatio;
  9697. this.m_limitImpulse1 *= step.dtRatio;
  9698. this.m_limitImpulse2 *= step.dtRatio;
  9699. var P1X = ((-this.m_impulse) - this.m_limitImpulse1) * this.m_u1.x;
  9700. var P1Y = ((-this.m_impulse) - this.m_limitImpulse1) * this.m_u1.y;
  9701. var P2X = ((-this.m_ratio * this.m_impulse) - this.m_limitImpulse2) * this.m_u2.x;
  9702. var P2Y = ((-this.m_ratio * this.m_impulse) - this.m_limitImpulse2) * this.m_u2.y;
  9703. bA.m_linearVelocity.x += bA.m_invMass * P1X;
  9704. bA.m_linearVelocity.y += bA.m_invMass * P1Y;
  9705. bA.m_angularVelocity += bA.m_invI * (r1X * P1Y - r1Y * P1X);
  9706. bB.m_linearVelocity.x += bB.m_invMass * P2X;
  9707. bB.m_linearVelocity.y += bB.m_invMass * P2Y;
  9708. bB.m_angularVelocity += bB.m_invI * (r2X * P2Y - r2Y * P2X);
  9709. }
  9710. else {
  9711. this.m_impulse = 0.0;
  9712. this.m_limitImpulse1 = 0.0;
  9713. this.m_limitImpulse2 = 0.0;
  9714. }
  9715. }
  9716. b2PulleyJoint.prototype.SolveVelocityConstraints = function (step) {
  9717. var bA = this.m_bodyA;
  9718. var bB = this.m_bodyB;
  9719. var tMat;
  9720. tMat = bA.m_xf.R;
  9721. var r1X = this.m_localAnchor1.x - bA.m_sweep.localCenter.x;
  9722. var r1Y = this.m_localAnchor1.y - bA.m_sweep.localCenter.y;
  9723. var tX = (tMat.col1.x * r1X + tMat.col2.x * r1Y);
  9724. r1Y = (tMat.col1.y * r1X + tMat.col2.y * r1Y);
  9725. r1X = tX;
  9726. tMat = bB.m_xf.R;
  9727. var r2X = this.m_localAnchor2.x - bB.m_sweep.localCenter.x;
  9728. var r2Y = this.m_localAnchor2.y - bB.m_sweep.localCenter.y;
  9729. tX = (tMat.col1.x * r2X + tMat.col2.x * r2Y);
  9730. r2Y = (tMat.col1.y * r2X + tMat.col2.y * r2Y);
  9731. r2X = tX;
  9732. var v1X = 0;
  9733. var v1Y = 0;
  9734. var v2X = 0;
  9735. var v2Y = 0;
  9736. var P1X = 0;
  9737. var P1Y = 0;
  9738. var P2X = 0;
  9739. var P2Y = 0;
  9740. var Cdot = 0;
  9741. var impulse = 0;
  9742. var oldImpulse = 0;
  9743. if (this.m_state == b2Joint.e_atUpperLimit) {
  9744. v1X = bA.m_linearVelocity.x + ((-bA.m_angularVelocity * r1Y));
  9745. v1Y = bA.m_linearVelocity.y + (bA.m_angularVelocity * r1X);
  9746. v2X = bB.m_linearVelocity.x + ((-bB.m_angularVelocity * r2Y));
  9747. v2Y = bB.m_linearVelocity.y + (bB.m_angularVelocity * r2X);
  9748. Cdot = (-(this.m_u1.x * v1X + this.m_u1.y * v1Y)) - this.m_ratio * (this.m_u2.x * v2X + this.m_u2.y * v2Y);
  9749. impulse = this.m_pulleyMass * ((-Cdot));
  9750. oldImpulse = this.m_impulse;
  9751. this.m_impulse = b2Math.Max(0.0, this.m_impulse + impulse);
  9752. impulse = this.m_impulse - oldImpulse;
  9753. P1X = (-impulse * this.m_u1.x);
  9754. P1Y = (-impulse * this.m_u1.y);
  9755. P2X = (-this.m_ratio * impulse * this.m_u2.x);
  9756. P2Y = (-this.m_ratio * impulse * this.m_u2.y);
  9757. bA.m_linearVelocity.x += bA.m_invMass * P1X;
  9758. bA.m_linearVelocity.y += bA.m_invMass * P1Y;
  9759. bA.m_angularVelocity += bA.m_invI * (r1X * P1Y - r1Y * P1X);
  9760. bB.m_linearVelocity.x += bB.m_invMass * P2X;
  9761. bB.m_linearVelocity.y += bB.m_invMass * P2Y;
  9762. bB.m_angularVelocity += bB.m_invI * (r2X * P2Y - r2Y * P2X);
  9763. }
  9764. if (this.m_limitState1 == b2Joint.e_atUpperLimit) {
  9765. v1X = bA.m_linearVelocity.x + ((-bA.m_angularVelocity * r1Y));
  9766. v1Y = bA.m_linearVelocity.y + (bA.m_angularVelocity * r1X);
  9767. Cdot = (-(this.m_u1.x * v1X + this.m_u1.y * v1Y));
  9768. impulse = (-this.m_limitMass1 * Cdot);
  9769. oldImpulse = this.m_limitImpulse1;
  9770. this.m_limitImpulse1 = b2Math.Max(0.0, this.m_limitImpulse1 + impulse);
  9771. impulse = this.m_limitImpulse1 - oldImpulse;
  9772. P1X = (-impulse * this.m_u1.x);
  9773. P1Y = (-impulse * this.m_u1.y);
  9774. bA.m_linearVelocity.x += bA.m_invMass * P1X;
  9775. bA.m_linearVelocity.y += bA.m_invMass * P1Y;
  9776. bA.m_angularVelocity += bA.m_invI * (r1X * P1Y - r1Y * P1X);
  9777. }
  9778. if (this.m_limitState2 == b2Joint.e_atUpperLimit) {
  9779. v2X = bB.m_linearVelocity.x + ((-bB.m_angularVelocity * r2Y));
  9780. v2Y = bB.m_linearVelocity.y + (bB.m_angularVelocity * r2X);
  9781. Cdot = (-(this.m_u2.x * v2X + this.m_u2.y * v2Y));
  9782. impulse = (-this.m_limitMass2 * Cdot);
  9783. oldImpulse = this.m_limitImpulse2;
  9784. this.m_limitImpulse2 = b2Math.Max(0.0, this.m_limitImpulse2 + impulse);
  9785. impulse = this.m_limitImpulse2 - oldImpulse;
  9786. P2X = (-impulse * this.m_u2.x);
  9787. P2Y = (-impulse * this.m_u2.y);
  9788. bB.m_linearVelocity.x += bB.m_invMass * P2X;
  9789. bB.m_linearVelocity.y += bB.m_invMass * P2Y;
  9790. bB.m_angularVelocity += bB.m_invI * (r2X * P2Y - r2Y * P2X);
  9791. }
  9792. }
  9793. b2PulleyJoint.prototype.SolvePositionConstraints = function (baumgarte) {
  9794. if (baumgarte === undefined) baumgarte = 0;
  9795. var bA = this.m_bodyA;
  9796. var bB = this.m_bodyB;
  9797. var tMat;
  9798. var s1X = this.m_ground.m_xf.position.x + this.m_groundAnchor1.x;
  9799. var s1Y = this.m_ground.m_xf.position.y + this.m_groundAnchor1.y;
  9800. var s2X = this.m_ground.m_xf.position.x + this.m_groundAnchor2.x;
  9801. var s2Y = this.m_ground.m_xf.position.y + this.m_groundAnchor2.y;
  9802. var r1X = 0;
  9803. var r1Y = 0;
  9804. var r2X = 0;
  9805. var r2Y = 0;
  9806. var p1X = 0;
  9807. var p1Y = 0;
  9808. var p2X = 0;
  9809. var p2Y = 0;
  9810. var length1 = 0;
  9811. var length2 = 0;
  9812. var C = 0;
  9813. var impulse = 0;
  9814. var oldImpulse = 0;
  9815. var oldLimitPositionImpulse = 0;
  9816. var tX = 0;
  9817. var linearError = 0.0;
  9818. if (this.m_state == b2Joint.e_atUpperLimit) {
  9819. tMat = bA.m_xf.R;
  9820. r1X = this.m_localAnchor1.x - bA.m_sweep.localCenter.x;
  9821. r1Y = this.m_localAnchor1.y - bA.m_sweep.localCenter.y;
  9822. tX = (tMat.col1.x * r1X + tMat.col2.x * r1Y);
  9823. r1Y = (tMat.col1.y * r1X + tMat.col2.y * r1Y);
  9824. r1X = tX;
  9825. tMat = bB.m_xf.R;
  9826. r2X = this.m_localAnchor2.x - bB.m_sweep.localCenter.x;
  9827. r2Y = this.m_localAnchor2.y - bB.m_sweep.localCenter.y;
  9828. tX = (tMat.col1.x * r2X + tMat.col2.x * r2Y);
  9829. r2Y = (tMat.col1.y * r2X + tMat.col2.y * r2Y);
  9830. r2X = tX;
  9831. p1X = bA.m_sweep.c.x + r1X;
  9832. p1Y = bA.m_sweep.c.y + r1Y;
  9833. p2X = bB.m_sweep.c.x + r2X;
  9834. p2Y = bB.m_sweep.c.y + r2Y;
  9835. this.m_u1.Set(p1X - s1X, p1Y - s1Y);
  9836. this.m_u2.Set(p2X - s2X, p2Y - s2Y);
  9837. length1 = this.m_u1.Length();
  9838. length2 = this.m_u2.Length();
  9839. if (length1 > b2Settings.b2_linearSlop) {
  9840. this.m_u1.Multiply(1.0 / length1);
  9841. }
  9842. else {
  9843. this.m_u1.SetZero();
  9844. }
  9845. if (length2 > b2Settings.b2_linearSlop) {
  9846. this.m_u2.Multiply(1.0 / length2);
  9847. }
  9848. else {
  9849. this.m_u2.SetZero();
  9850. }
  9851. C = this.m_constant - length1 - this.m_ratio * length2;
  9852. linearError = b2Math.Max(linearError, (-C));
  9853. C = b2Math.Clamp(C + b2Settings.b2_linearSlop, (-b2Settings.b2_maxLinearCorrection), 0.0);
  9854. impulse = (-this.m_pulleyMass * C);
  9855. p1X = (-impulse * this.m_u1.x);
  9856. p1Y = (-impulse * this.m_u1.y);
  9857. p2X = (-this.m_ratio * impulse * this.m_u2.x);
  9858. p2Y = (-this.m_ratio * impulse * this.m_u2.y);
  9859. bA.m_sweep.c.x += bA.m_invMass * p1X;
  9860. bA.m_sweep.c.y += bA.m_invMass * p1Y;
  9861. bA.m_sweep.a += bA.m_invI * (r1X * p1Y - r1Y * p1X);
  9862. bB.m_sweep.c.x += bB.m_invMass * p2X;
  9863. bB.m_sweep.c.y += bB.m_invMass * p2Y;
  9864. bB.m_sweep.a += bB.m_invI * (r2X * p2Y - r2Y * p2X);
  9865. bA.SynchronizeTransform();
  9866. bB.SynchronizeTransform();
  9867. }
  9868. if (this.m_limitState1 == b2Joint.e_atUpperLimit) {
  9869. tMat = bA.m_xf.R;
  9870. r1X = this.m_localAnchor1.x - bA.m_sweep.localCenter.x;
  9871. r1Y = this.m_localAnchor1.y - bA.m_sweep.localCenter.y;
  9872. tX = (tMat.col1.x * r1X + tMat.col2.x * r1Y);
  9873. r1Y = (tMat.col1.y * r1X + tMat.col2.y * r1Y);
  9874. r1X = tX;
  9875. p1X = bA.m_sweep.c.x + r1X;
  9876. p1Y = bA.m_sweep.c.y + r1Y;
  9877. this.m_u1.Set(p1X - s1X, p1Y - s1Y);
  9878. length1 = this.m_u1.Length();
  9879. if (length1 > b2Settings.b2_linearSlop) {
  9880. this.m_u1.x *= 1.0 / length1;
  9881. this.m_u1.y *= 1.0 / length1;
  9882. }
  9883. else {
  9884. this.m_u1.SetZero();
  9885. }
  9886. C = this.m_maxLength1 - length1;
  9887. linearError = b2Math.Max(linearError, (-C));
  9888. C = b2Math.Clamp(C + b2Settings.b2_linearSlop, (-b2Settings.b2_maxLinearCorrection), 0.0);
  9889. impulse = (-this.m_limitMass1 * C);
  9890. p1X = (-impulse * this.m_u1.x);
  9891. p1Y = (-impulse * this.m_u1.y);
  9892. bA.m_sweep.c.x += bA.m_invMass * p1X;
  9893. bA.m_sweep.c.y += bA.m_invMass * p1Y;
  9894. bA.m_sweep.a += bA.m_invI * (r1X * p1Y - r1Y * p1X);
  9895. bA.SynchronizeTransform();
  9896. }
  9897. if (this.m_limitState2 == b2Joint.e_atUpperLimit) {
  9898. tMat = bB.m_xf.R;
  9899. r2X = this.m_localAnchor2.x - bB.m_sweep.localCenter.x;
  9900. r2Y = this.m_localAnchor2.y - bB.m_sweep.localCenter.y;
  9901. tX = (tMat.col1.x * r2X + tMat.col2.x * r2Y);
  9902. r2Y = (tMat.col1.y * r2X + tMat.col2.y * r2Y);
  9903. r2X = tX;
  9904. p2X = bB.m_sweep.c.x + r2X;
  9905. p2Y = bB.m_sweep.c.y + r2Y;
  9906. this.m_u2.Set(p2X - s2X, p2Y - s2Y);
  9907. length2 = this.m_u2.Length();
  9908. if (length2 > b2Settings.b2_linearSlop) {
  9909. this.m_u2.x *= 1.0 / length2;
  9910. this.m_u2.y *= 1.0 / length2;
  9911. }
  9912. else {
  9913. this.m_u2.SetZero();
  9914. }
  9915. C = this.m_maxLength2 - length2;
  9916. linearError = b2Math.Max(linearError, (-C));
  9917. C = b2Math.Clamp(C + b2Settings.b2_linearSlop, (-b2Settings.b2_maxLinearCorrection), 0.0);
  9918. impulse = (-this.m_limitMass2 * C);
  9919. p2X = (-impulse * this.m_u2.x);
  9920. p2Y = (-impulse * this.m_u2.y);
  9921. bB.m_sweep.c.x += bB.m_invMass * p2X;
  9922. bB.m_sweep.c.y += bB.m_invMass * p2Y;
  9923. bB.m_sweep.a += bB.m_invI * (r2X * p2Y - r2Y * p2X);
  9924. bB.SynchronizeTransform();
  9925. }
  9926. return linearError < b2Settings.b2_linearSlop;
  9927. }
  9928. Box2D.postDefs.push(function () {
  9929. Box2D.Dynamics.Joints.b2PulleyJoint.b2_minPulleyLength = 2.0;
  9930. });
  9931. Box2D.inherit(b2PulleyJointDef, Box2D.Dynamics.Joints.b2JointDef);
  9932. b2PulleyJointDef.prototype.__super = Box2D.Dynamics.Joints.b2JointDef.prototype;
  9933. b2PulleyJointDef.b2PulleyJointDef = function () {
  9934. Box2D.Dynamics.Joints.b2JointDef.b2JointDef.apply(this, arguments);
  9935. this.groundAnchorA = new b2Vec2();
  9936. this.groundAnchorB = new b2Vec2();
  9937. this.localAnchorA = new b2Vec2();
  9938. this.localAnchorB = new b2Vec2();
  9939. };
  9940. b2PulleyJointDef.prototype.b2PulleyJointDef = function () {
  9941. this.__super.b2JointDef.call(this);
  9942. this.type = b2Joint.e_pulleyJoint;
  9943. this.groundAnchorA.Set((-1.0), 1.0);
  9944. this.groundAnchorB.Set(1.0, 1.0);
  9945. this.localAnchorA.Set((-1.0), 0.0);
  9946. this.localAnchorB.Set(1.0, 0.0);
  9947. this.lengthA = 0.0;
  9948. this.maxLengthA = 0.0;
  9949. this.lengthB = 0.0;
  9950. this.maxLengthB = 0.0;
  9951. this.ratio = 1.0;
  9952. this.collideConnected = true;
  9953. }
  9954. b2PulleyJointDef.prototype.Initialize = function (bA, bB, gaA, gaB, anchorA, anchorB, r) {
  9955. if (r === undefined) r = 0;
  9956. this.bodyA = bA;
  9957. this.bodyB = bB;
  9958. this.groundAnchorA.SetV(gaA);
  9959. this.groundAnchorB.SetV(gaB);
  9960. this.localAnchorA = this.bodyA.GetLocalPoint(anchorA);
  9961. this.localAnchorB = this.bodyB.GetLocalPoint(anchorB);
  9962. var d1X = anchorA.x - gaA.x;
  9963. var d1Y = anchorA.y - gaA.y;
  9964. this.lengthA = Math.sqrt(d1X * d1X + d1Y * d1Y);
  9965. var d2X = anchorB.x - gaB.x;
  9966. var d2Y = anchorB.y - gaB.y;
  9967. this.lengthB = Math.sqrt(d2X * d2X + d2Y * d2Y);
  9968. this.ratio = r;
  9969. var C = this.lengthA + this.ratio * this.lengthB;
  9970. this.maxLengthA = C - this.ratio * b2PulleyJoint.b2_minPulleyLength;
  9971. this.maxLengthB = (C - b2PulleyJoint.b2_minPulleyLength) / this.ratio;
  9972. }
  9973. Box2D.inherit(b2RevoluteJoint, Box2D.Dynamics.Joints.b2Joint);
  9974. b2RevoluteJoint.prototype.__super = Box2D.Dynamics.Joints.b2Joint.prototype;
  9975. b2RevoluteJoint.b2RevoluteJoint = function () {
  9976. Box2D.Dynamics.Joints.b2Joint.b2Joint.apply(this, arguments);
  9977. this.K = new b2Mat22();
  9978. this.K1 = new b2Mat22();
  9979. this.K2 = new b2Mat22();
  9980. this.K3 = new b2Mat22();
  9981. this.impulse3 = new b2Vec3();
  9982. this.impulse2 = new b2Vec2();
  9983. this.reduced = new b2Vec2();
  9984. this.m_localAnchor1 = new b2Vec2();
  9985. this.m_localAnchor2 = new b2Vec2();
  9986. this.m_impulse = new b2Vec3();
  9987. this.m_mass = new b2Mat33();
  9988. };
  9989. b2RevoluteJoint.prototype.GetAnchorA = function () {
  9990. return this.m_bodyA.GetWorldPoint(this.m_localAnchor1);
  9991. }
  9992. b2RevoluteJoint.prototype.GetAnchorB = function () {
  9993. return this.m_bodyB.GetWorldPoint(this.m_localAnchor2);
  9994. }
  9995. b2RevoluteJoint.prototype.GetReactionForce = function (inv_dt) {
  9996. if (inv_dt === undefined) inv_dt = 0;
  9997. return new b2Vec2(inv_dt * this.m_impulse.x, inv_dt * this.m_impulse.y);
  9998. }
  9999. b2RevoluteJoint.prototype.GetReactionTorque = function (inv_dt) {
  10000. if (inv_dt === undefined) inv_dt = 0;
  10001. return inv_dt * this.m_impulse.z;
  10002. }
  10003. b2RevoluteJoint.prototype.GetJointAngle = function () {
  10004. return this.m_bodyB.m_sweep.a - this.m_bodyA.m_sweep.a - this.m_referenceAngle;
  10005. }
  10006. b2RevoluteJoint.prototype.GetJointSpeed = function () {
  10007. return this.m_bodyB.m_angularVelocity - this.m_bodyA.m_angularVelocity;
  10008. }
  10009. b2RevoluteJoint.prototype.IsLimitEnabled = function () {
  10010. return this.m_enableLimit;
  10011. }
  10012. b2RevoluteJoint.prototype.EnableLimit = function (flag) {
  10013. this.m_enableLimit = flag;
  10014. }
  10015. b2RevoluteJoint.prototype.GetLowerLimit = function () {
  10016. return this.m_lowerAngle;
  10017. }
  10018. b2RevoluteJoint.prototype.GetUpperLimit = function () {
  10019. return this.m_upperAngle;
  10020. }
  10021. b2RevoluteJoint.prototype.SetLimits = function (lower, upper) {
  10022. if (lower === undefined) lower = 0;
  10023. if (upper === undefined) upper = 0;
  10024. this.m_lowerAngle = lower;
  10025. this.m_upperAngle = upper;
  10026. }
  10027. b2RevoluteJoint.prototype.IsMotorEnabled = function () {
  10028. this.m_bodyA.SetAwake(true);
  10029. this.m_bodyB.SetAwake(true);
  10030. return this.m_enableMotor;
  10031. }
  10032. b2RevoluteJoint.prototype.EnableMotor = function (flag) {
  10033. this.m_enableMotor = flag;
  10034. }
  10035. b2RevoluteJoint.prototype.SetMotorSpeed = function (speed) {
  10036. if (speed === undefined) speed = 0;
  10037. this.m_bodyA.SetAwake(true);
  10038. this.m_bodyB.SetAwake(true);
  10039. this.m_motorSpeed = speed;
  10040. }
  10041. b2RevoluteJoint.prototype.GetMotorSpeed = function () {
  10042. return this.m_motorSpeed;
  10043. }
  10044. b2RevoluteJoint.prototype.SetMaxMotorTorque = function (torque) {
  10045. if (torque === undefined) torque = 0;
  10046. this.m_maxMotorTorque = torque;
  10047. }
  10048. b2RevoluteJoint.prototype.GetMotorTorque = function () {
  10049. return this.m_maxMotorTorque;
  10050. }
  10051. b2RevoluteJoint.prototype.b2RevoluteJoint = function (def) {
  10052. this.__super.b2Joint.call(this, def);
  10053. this.m_localAnchor1.SetV(def.localAnchorA);
  10054. this.m_localAnchor2.SetV(def.localAnchorB);
  10055. this.m_referenceAngle = def.referenceAngle;
  10056. this.m_impulse.SetZero();
  10057. this.m_motorImpulse = 0.0;
  10058. this.m_lowerAngle = def.lowerAngle;
  10059. this.m_upperAngle = def.upperAngle;
  10060. this.m_maxMotorTorque = def.maxMotorTorque;
  10061. this.m_motorSpeed = def.motorSpeed;
  10062. this.m_enableLimit = def.enableLimit;
  10063. this.m_enableMotor = def.enableMotor;
  10064. this.m_limitState = b2Joint.e_inactiveLimit;
  10065. }
  10066. b2RevoluteJoint.prototype.InitVelocityConstraints = function (step) {
  10067. var bA = this.m_bodyA;
  10068. var bB = this.m_bodyB;
  10069. var tMat;
  10070. var tX = 0;
  10071. if (this.m_enableMotor || this.m_enableLimit) {}
  10072. tMat = bA.m_xf.R;
  10073. var r1X = this.m_localAnchor1.x - bA.m_sweep.localCenter.x;
  10074. var r1Y = this.m_localAnchor1.y - bA.m_sweep.localCenter.y;
  10075. tX = (tMat.col1.x * r1X + tMat.col2.x * r1Y);
  10076. r1Y = (tMat.col1.y * r1X + tMat.col2.y * r1Y);
  10077. r1X = tX;
  10078. tMat = bB.m_xf.R;
  10079. var r2X = this.m_localAnchor2.x - bB.m_sweep.localCenter.x;
  10080. var r2Y = this.m_localAnchor2.y - bB.m_sweep.localCenter.y;
  10081. tX = (tMat.col1.x * r2X + tMat.col2.x * r2Y);
  10082. r2Y = (tMat.col1.y * r2X + tMat.col2.y * r2Y);
  10083. r2X = tX;
  10084. var m1 = bA.m_invMass;
  10085. var m2 = bB.m_invMass;
  10086. var i1 = bA.m_invI;
  10087. var i2 = bB.m_invI;
  10088. this.m_mass.col1.x = m1 + m2 + r1Y * r1Y * i1 + r2Y * r2Y * i2;
  10089. this.m_mass.col2.x = (-r1Y * r1X * i1) - r2Y * r2X * i2;
  10090. this.m_mass.col3.x = (-r1Y * i1) - r2Y * i2;
  10091. this.m_mass.col1.y = this.m_mass.col2.x;
  10092. this.m_mass.col2.y = m1 + m2 + r1X * r1X * i1 + r2X * r2X * i2;
  10093. this.m_mass.col3.y = r1X * i1 + r2X * i2;
  10094. this.m_mass.col1.z = this.m_mass.col3.x;
  10095. this.m_mass.col2.z = this.m_mass.col3.y;
  10096. this.m_mass.col3.z = i1 + i2;
  10097. this.m_motorMass = 1.0 / (i1 + i2);
  10098. if (this.m_enableMotor == false) {
  10099. this.m_motorImpulse = 0.0;
  10100. }
  10101. if (this.m_enableLimit) {
  10102. var jointAngle = bB.m_sweep.a - bA.m_sweep.a - this.m_referenceAngle;
  10103. if (b2Math.Abs(this.m_upperAngle - this.m_lowerAngle) < 2.0 * b2Settings.b2_angularSlop) {
  10104. this.m_limitState = b2Joint.e_equalLimits;
  10105. }
  10106. else if (jointAngle <= this.m_lowerAngle) {
  10107. if (this.m_limitState != b2Joint.e_atLowerLimit) {
  10108. this.m_impulse.z = 0.0;
  10109. }
  10110. this.m_limitState = b2Joint.e_atLowerLimit;
  10111. }
  10112. else if (jointAngle >= this.m_upperAngle) {
  10113. if (this.m_limitState != b2Joint.e_atUpperLimit) {
  10114. this.m_impulse.z = 0.0;
  10115. }
  10116. this.m_limitState = b2Joint.e_atUpperLimit;
  10117. }
  10118. else {
  10119. this.m_limitState = b2Joint.e_inactiveLimit;
  10120. this.m_impulse.z = 0.0;
  10121. }
  10122. }
  10123. else {
  10124. this.m_limitState = b2Joint.e_inactiveLimit;
  10125. }
  10126. if (step.warmStarting) {
  10127. this.m_impulse.x *= step.dtRatio;
  10128. this.m_impulse.y *= step.dtRatio;
  10129. this.m_motorImpulse *= step.dtRatio;
  10130. var PX = this.m_impulse.x;
  10131. var PY = this.m_impulse.y;
  10132. bA.m_linearVelocity.x -= m1 * PX;
  10133. bA.m_linearVelocity.y -= m1 * PY;
  10134. bA.m_angularVelocity -= i1 * ((r1X * PY - r1Y * PX) + this.m_motorImpulse + this.m_impulse.z);
  10135. bB.m_linearVelocity.x += m2 * PX;
  10136. bB.m_linearVelocity.y += m2 * PY;
  10137. bB.m_angularVelocity += i2 * ((r2X * PY - r2Y * PX) + this.m_motorImpulse + this.m_impulse.z);
  10138. }
  10139. else {
  10140. this.m_impulse.SetZero();
  10141. this.m_motorImpulse = 0.0;
  10142. }
  10143. }
  10144. b2RevoluteJoint.prototype.SolveVelocityConstraints = function (step) {
  10145. var bA = this.m_bodyA;
  10146. var bB = this.m_bodyB;
  10147. var tMat;
  10148. var tX = 0;
  10149. var newImpulse = 0;
  10150. var r1X = 0;
  10151. var r1Y = 0;
  10152. var r2X = 0;
  10153. var r2Y = 0;
  10154. var v1 = bA.m_linearVelocity;
  10155. var w1 = bA.m_angularVelocity;
  10156. var v2 = bB.m_linearVelocity;
  10157. var w2 = bB.m_angularVelocity;
  10158. var m1 = bA.m_invMass;
  10159. var m2 = bB.m_invMass;
  10160. var i1 = bA.m_invI;
  10161. var i2 = bB.m_invI;
  10162. if (this.m_enableMotor && this.m_limitState != b2Joint.e_equalLimits) {
  10163. var Cdot = w2 - w1 - this.m_motorSpeed;
  10164. var impulse = this.m_motorMass * ((-Cdot));
  10165. var oldImpulse = this.m_motorImpulse;
  10166. var maxImpulse = step.dt * this.m_maxMotorTorque;
  10167. this.m_motorImpulse = b2Math.Clamp(this.m_motorImpulse + impulse, (-maxImpulse), maxImpulse);
  10168. impulse = this.m_motorImpulse - oldImpulse;
  10169. w1 -= i1 * impulse;
  10170. w2 += i2 * impulse;
  10171. }
  10172. if (this.m_enableLimit && this.m_limitState != b2Joint.e_inactiveLimit) {
  10173. tMat = bA.m_xf.R;
  10174. r1X = this.m_localAnchor1.x - bA.m_sweep.localCenter.x;
  10175. r1Y = this.m_localAnchor1.y - bA.m_sweep.localCenter.y;
  10176. tX = (tMat.col1.x * r1X + tMat.col2.x * r1Y);
  10177. r1Y = (tMat.col1.y * r1X + tMat.col2.y * r1Y);
  10178. r1X = tX;
  10179. tMat = bB.m_xf.R;
  10180. r2X = this.m_localAnchor2.x - bB.m_sweep.localCenter.x;
  10181. r2Y = this.m_localAnchor2.y - bB.m_sweep.localCenter.y;
  10182. tX = (tMat.col1.x * r2X + tMat.col2.x * r2Y);
  10183. r2Y = (tMat.col1.y * r2X + tMat.col2.y * r2Y);
  10184. r2X = tX;
  10185. var Cdot1X = v2.x + ((-w2 * r2Y)) - v1.x - ((-w1 * r1Y));
  10186. var Cdot1Y = v2.y + (w2 * r2X) - v1.y - (w1 * r1X);
  10187. var Cdot2 = w2 - w1;
  10188. this.m_mass.Solve33(this.impulse3, (-Cdot1X), (-Cdot1Y), (-Cdot2));
  10189. if (this.m_limitState == b2Joint.e_equalLimits) {
  10190. this.m_impulse.Add(this.impulse3);
  10191. }
  10192. else if (this.m_limitState == b2Joint.e_atLowerLimit) {
  10193. newImpulse = this.m_impulse.z + this.impulse3.z;
  10194. if (newImpulse < 0.0) {
  10195. this.m_mass.Solve22(this.reduced, (-Cdot1X), (-Cdot1Y));
  10196. this.impulse3.x = this.reduced.x;
  10197. this.impulse3.y = this.reduced.y;
  10198. this.impulse3.z = (-this.m_impulse.z);
  10199. this.m_impulse.x += this.reduced.x;
  10200. this.m_impulse.y += this.reduced.y;
  10201. this.m_impulse.z = 0.0;
  10202. }
  10203. }
  10204. else if (this.m_limitState == b2Joint.e_atUpperLimit) {
  10205. newImpulse = this.m_impulse.z + this.impulse3.z;
  10206. if (newImpulse > 0.0) {
  10207. this.m_mass.Solve22(this.reduced, (-Cdot1X), (-Cdot1Y));
  10208. this.impulse3.x = this.reduced.x;
  10209. this.impulse3.y = this.reduced.y;
  10210. this.impulse3.z = (-this.m_impulse.z);
  10211. this.m_impulse.x += this.reduced.x;
  10212. this.m_impulse.y += this.reduced.y;
  10213. this.m_impulse.z = 0.0;
  10214. }
  10215. }
  10216. v1.x -= m1 * this.impulse3.x;
  10217. v1.y -= m1 * this.impulse3.y;
  10218. w1 -= i1 * (r1X * this.impulse3.y - r1Y * this.impulse3.x + this.impulse3.z);
  10219. v2.x += m2 * this.impulse3.x;
  10220. v2.y += m2 * this.impulse3.y;
  10221. w2 += i2 * (r2X * this.impulse3.y - r2Y * this.impulse3.x + this.impulse3.z);
  10222. }
  10223. else {
  10224. tMat = bA.m_xf.R;
  10225. r1X = this.m_localAnchor1.x - bA.m_sweep.localCenter.x;
  10226. r1Y = this.m_localAnchor1.y - bA.m_sweep.localCenter.y;
  10227. tX = (tMat.col1.x * r1X + tMat.col2.x * r1Y);
  10228. r1Y = (tMat.col1.y * r1X + tMat.col2.y * r1Y);
  10229. r1X = tX;
  10230. tMat = bB.m_xf.R;
  10231. r2X = this.m_localAnchor2.x - bB.m_sweep.localCenter.x;
  10232. r2Y = this.m_localAnchor2.y - bB.m_sweep.localCenter.y;
  10233. tX = (tMat.col1.x * r2X + tMat.col2.x * r2Y);
  10234. r2Y = (tMat.col1.y * r2X + tMat.col2.y * r2Y);
  10235. r2X = tX;
  10236. var CdotX = v2.x + ((-w2 * r2Y)) - v1.x - ((-w1 * r1Y));
  10237. var CdotY = v2.y + (w2 * r2X) - v1.y - (w1 * r1X);
  10238. this.m_mass.Solve22(this.impulse2, (-CdotX), (-CdotY));
  10239. this.m_impulse.x += this.impulse2.x;
  10240. this.m_impulse.y += this.impulse2.y;
  10241. v1.x -= m1 * this.impulse2.x;
  10242. v1.y -= m1 * this.impulse2.y;
  10243. w1 -= i1 * (r1X * this.impulse2.y - r1Y * this.impulse2.x);
  10244. v2.x += m2 * this.impulse2.x;
  10245. v2.y += m2 * this.impulse2.y;
  10246. w2 += i2 * (r2X * this.impulse2.y - r2Y * this.impulse2.x);
  10247. }
  10248. bA.m_linearVelocity.SetV(v1);
  10249. bA.m_angularVelocity = w1;
  10250. bB.m_linearVelocity.SetV(v2);
  10251. bB.m_angularVelocity = w2;
  10252. }
  10253. b2RevoluteJoint.prototype.SolvePositionConstraints = function (baumgarte) {
  10254. if (baumgarte === undefined) baumgarte = 0;
  10255. var oldLimitImpulse = 0;
  10256. var C = 0;
  10257. var tMat;
  10258. var bA = this.m_bodyA;
  10259. var bB = this.m_bodyB;
  10260. var angularError = 0.0;
  10261. var positionError = 0.0;
  10262. var tX = 0;
  10263. var impulseX = 0;
  10264. var impulseY = 0;
  10265. if (this.m_enableLimit && this.m_limitState != b2Joint.e_inactiveLimit) {
  10266. var angle = bB.m_sweep.a - bA.m_sweep.a - this.m_referenceAngle;
  10267. var limitImpulse = 0.0;
  10268. if (this.m_limitState == b2Joint.e_equalLimits) {
  10269. C = b2Math.Clamp(angle - this.m_lowerAngle, (-b2Settings.b2_maxAngularCorrection), b2Settings.b2_maxAngularCorrection);
  10270. limitImpulse = (-this.m_motorMass * C);
  10271. angularError = b2Math.Abs(C);
  10272. }
  10273. else if (this.m_limitState == b2Joint.e_atLowerLimit) {
  10274. C = angle - this.m_lowerAngle;
  10275. angularError = (-C);
  10276. C = b2Math.Clamp(C + b2Settings.b2_angularSlop, (-b2Settings.b2_maxAngularCorrection), 0.0);
  10277. limitImpulse = (-this.m_motorMass * C);
  10278. }
  10279. else if (this.m_limitState == b2Joint.e_atUpperLimit) {
  10280. C = angle - this.m_upperAngle;
  10281. angularError = C;
  10282. C = b2Math.Clamp(C - b2Settings.b2_angularSlop, 0.0, b2Settings.b2_maxAngularCorrection);
  10283. limitImpulse = (-this.m_motorMass * C);
  10284. }
  10285. bA.m_sweep.a -= bA.m_invI * limitImpulse;
  10286. bB.m_sweep.a += bB.m_invI * limitImpulse;
  10287. bA.SynchronizeTransform();
  10288. bB.SynchronizeTransform();
  10289. } {
  10290. tMat = bA.m_xf.R;
  10291. var r1X = this.m_localAnchor1.x - bA.m_sweep.localCenter.x;
  10292. var r1Y = this.m_localAnchor1.y - bA.m_sweep.localCenter.y;
  10293. tX = (tMat.col1.x * r1X + tMat.col2.x * r1Y);
  10294. r1Y = (tMat.col1.y * r1X + tMat.col2.y * r1Y);
  10295. r1X = tX;
  10296. tMat = bB.m_xf.R;
  10297. var r2X = this.m_localAnchor2.x - bB.m_sweep.localCenter.x;
  10298. var r2Y = this.m_localAnchor2.y - bB.m_sweep.localCenter.y;
  10299. tX = (tMat.col1.x * r2X + tMat.col2.x * r2Y);
  10300. r2Y = (tMat.col1.y * r2X + tMat.col2.y * r2Y);
  10301. r2X = tX;
  10302. var CX = bB.m_sweep.c.x + r2X - bA.m_sweep.c.x - r1X;
  10303. var CY = bB.m_sweep.c.y + r2Y - bA.m_sweep.c.y - r1Y;
  10304. var CLengthSquared = CX * CX + CY * CY;
  10305. var CLength = Math.sqrt(CLengthSquared);
  10306. positionError = CLength;
  10307. var invMass1 = bA.m_invMass;
  10308. var invMass2 = bB.m_invMass;
  10309. var invI1 = bA.m_invI;
  10310. var invI2 = bB.m_invI;
  10311. var k_allowedStretch = 10.0 * b2Settings.b2_linearSlop;
  10312. if (CLengthSquared > k_allowedStretch * k_allowedStretch) {
  10313. var uX = CX / CLength;
  10314. var uY = CY / CLength;
  10315. var k = invMass1 + invMass2;
  10316. var m = 1.0 / k;
  10317. impulseX = m * ((-CX));
  10318. impulseY = m * ((-CY));
  10319. var k_beta = 0.5;
  10320. bA.m_sweep.c.x -= k_beta * invMass1 * impulseX;
  10321. bA.m_sweep.c.y -= k_beta * invMass1 * impulseY;
  10322. bB.m_sweep.c.x += k_beta * invMass2 * impulseX;
  10323. bB.m_sweep.c.y += k_beta * invMass2 * impulseY;
  10324. CX = bB.m_sweep.c.x + r2X - bA.m_sweep.c.x - r1X;
  10325. CY = bB.m_sweep.c.y + r2Y - bA.m_sweep.c.y - r1Y;
  10326. }
  10327. this.K1.col1.x = invMass1 + invMass2;
  10328. this.K1.col2.x = 0.0;
  10329. this.K1.col1.y = 0.0;
  10330. this.K1.col2.y = invMass1 + invMass2;
  10331. this.K2.col1.x = invI1 * r1Y * r1Y;
  10332. this.K2.col2.x = (-invI1 * r1X * r1Y);
  10333. this.K2.col1.y = (-invI1 * r1X * r1Y);
  10334. this.K2.col2.y = invI1 * r1X * r1X;
  10335. this.K3.col1.x = invI2 * r2Y * r2Y;
  10336. this.K3.col2.x = (-invI2 * r2X * r2Y);
  10337. this.K3.col1.y = (-invI2 * r2X * r2Y);
  10338. this.K3.col2.y = invI2 * r2X * r2X;
  10339. this.K.SetM(this.K1);
  10340. this.K.AddM(this.K2);
  10341. this.K.AddM(this.K3);
  10342. this.K.Solve(b2RevoluteJoint.tImpulse, (-CX), (-CY));
  10343. impulseX = b2RevoluteJoint.tImpulse.x;
  10344. impulseY = b2RevoluteJoint.tImpulse.y;
  10345. bA.m_sweep.c.x -= bA.m_invMass * impulseX;
  10346. bA.m_sweep.c.y -= bA.m_invMass * impulseY;
  10347. bA.m_sweep.a -= bA.m_invI * (r1X * impulseY - r1Y * impulseX);
  10348. bB.m_sweep.c.x += bB.m_invMass * impulseX;
  10349. bB.m_sweep.c.y += bB.m_invMass * impulseY;
  10350. bB.m_sweep.a += bB.m_invI * (r2X * impulseY - r2Y * impulseX);
  10351. bA.SynchronizeTransform();
  10352. bB.SynchronizeTransform();
  10353. }
  10354. return positionError <= b2Settings.b2_linearSlop && angularError <= b2Settings.b2_angularSlop;
  10355. }
  10356. Box2D.postDefs.push(function () {
  10357. Box2D.Dynamics.Joints.b2RevoluteJoint.tImpulse = new b2Vec2();
  10358. });
  10359. Box2D.inherit(b2RevoluteJointDef, Box2D.Dynamics.Joints.b2JointDef);
  10360. b2RevoluteJointDef.prototype.__super = Box2D.Dynamics.Joints.b2JointDef.prototype;
  10361. b2RevoluteJointDef.b2RevoluteJointDef = function () {
  10362. Box2D.Dynamics.Joints.b2JointDef.b2JointDef.apply(this, arguments);
  10363. this.localAnchorA = new b2Vec2();
  10364. this.localAnchorB = new b2Vec2();
  10365. };
  10366. b2RevoluteJointDef.prototype.b2RevoluteJointDef = function () {
  10367. this.__super.b2JointDef.call(this);
  10368. this.type = b2Joint.e_revoluteJoint;
  10369. this.localAnchorA.Set(0.0, 0.0);
  10370. this.localAnchorB.Set(0.0, 0.0);
  10371. this.referenceAngle = 0.0;
  10372. this.lowerAngle = 0.0;
  10373. this.upperAngle = 0.0;
  10374. this.maxMotorTorque = 0.0;
  10375. this.motorSpeed = 0.0;
  10376. this.enableLimit = false;
  10377. this.enableMotor = false;
  10378. }
  10379. b2RevoluteJointDef.prototype.Initialize = function (bA, bB, anchor) {
  10380. this.bodyA = bA;
  10381. this.bodyB = bB;
  10382. this.localAnchorA = this.bodyA.GetLocalPoint(anchor);
  10383. this.localAnchorB = this.bodyB.GetLocalPoint(anchor);
  10384. this.referenceAngle = this.bodyB.GetAngle() - this.bodyA.GetAngle();
  10385. }
  10386. Box2D.inherit(b2WeldJoint, Box2D.Dynamics.Joints.b2Joint);
  10387. b2WeldJoint.prototype.__super = Box2D.Dynamics.Joints.b2Joint.prototype;
  10388. b2WeldJoint.b2WeldJoint = function () {
  10389. Box2D.Dynamics.Joints.b2Joint.b2Joint.apply(this, arguments);
  10390. this.m_localAnchorA = new b2Vec2();
  10391. this.m_localAnchorB = new b2Vec2();
  10392. this.m_impulse = new b2Vec3();
  10393. this.m_mass = new b2Mat33();
  10394. };
  10395. b2WeldJoint.prototype.GetAnchorA = function () {
  10396. return this.m_bodyA.GetWorldPoint(this.m_localAnchorA);
  10397. }
  10398. b2WeldJoint.prototype.GetAnchorB = function () {
  10399. return this.m_bodyB.GetWorldPoint(this.m_localAnchorB);
  10400. }
  10401. b2WeldJoint.prototype.GetReactionForce = function (inv_dt) {
  10402. if (inv_dt === undefined) inv_dt = 0;
  10403. return new b2Vec2(inv_dt * this.m_impulse.x, inv_dt * this.m_impulse.y);
  10404. }
  10405. b2WeldJoint.prototype.GetReactionTorque = function (inv_dt) {
  10406. if (inv_dt === undefined) inv_dt = 0;
  10407. return inv_dt * this.m_impulse.z;
  10408. }
  10409. b2WeldJoint.prototype.b2WeldJoint = function (def) {
  10410. this.__super.b2Joint.call(this, def);
  10411. this.m_localAnchorA.SetV(def.localAnchorA);
  10412. this.m_localAnchorB.SetV(def.localAnchorB);
  10413. this.m_referenceAngle = def.referenceAngle;
  10414. this.m_impulse.SetZero();
  10415. this.m_mass = new b2Mat33();
  10416. }
  10417. b2WeldJoint.prototype.InitVelocityConstraints = function (step) {
  10418. var tMat;
  10419. var tX = 0;
  10420. var bA = this.m_bodyA;
  10421. var bB = this.m_bodyB;
  10422. tMat = bA.m_xf.R;
  10423. var rAX = this.m_localAnchorA.x - bA.m_sweep.localCenter.x;
  10424. var rAY = this.m_localAnchorA.y - bA.m_sweep.localCenter.y;
  10425. tX = (tMat.col1.x * rAX + tMat.col2.x * rAY);
  10426. rAY = (tMat.col1.y * rAX + tMat.col2.y * rAY);
  10427. rAX = tX;
  10428. tMat = bB.m_xf.R;
  10429. var rBX = this.m_localAnchorB.x - bB.m_sweep.localCenter.x;
  10430. var rBY = this.m_localAnchorB.y - bB.m_sweep.localCenter.y;
  10431. tX = (tMat.col1.x * rBX + tMat.col2.x * rBY);
  10432. rBY = (tMat.col1.y * rBX + tMat.col2.y * rBY);
  10433. rBX = tX;
  10434. var mA = bA.m_invMass;
  10435. var mB = bB.m_invMass;
  10436. var iA = bA.m_invI;
  10437. var iB = bB.m_invI;
  10438. this.m_mass.col1.x = mA + mB + rAY * rAY * iA + rBY * rBY * iB;
  10439. this.m_mass.col2.x = (-rAY * rAX * iA) - rBY * rBX * iB;
  10440. this.m_mass.col3.x = (-rAY * iA) - rBY * iB;
  10441. this.m_mass.col1.y = this.m_mass.col2.x;
  10442. this.m_mass.col2.y = mA + mB + rAX * rAX * iA + rBX * rBX * iB;
  10443. this.m_mass.col3.y = rAX * iA + rBX * iB;
  10444. this.m_mass.col1.z = this.m_mass.col3.x;
  10445. this.m_mass.col2.z = this.m_mass.col3.y;
  10446. this.m_mass.col3.z = iA + iB;
  10447. if (step.warmStarting) {
  10448. this.m_impulse.x *= step.dtRatio;
  10449. this.m_impulse.y *= step.dtRatio;
  10450. this.m_impulse.z *= step.dtRatio;
  10451. bA.m_linearVelocity.x -= mA * this.m_impulse.x;
  10452. bA.m_linearVelocity.y -= mA * this.m_impulse.y;
  10453. bA.m_angularVelocity -= iA * (rAX * this.m_impulse.y - rAY * this.m_impulse.x + this.m_impulse.z);
  10454. bB.m_linearVelocity.x += mB * this.m_impulse.x;
  10455. bB.m_linearVelocity.y += mB * this.m_impulse.y;
  10456. bB.m_angularVelocity += iB * (rBX * this.m_impulse.y - rBY * this.m_impulse.x + this.m_impulse.z);
  10457. }
  10458. else {
  10459. this.m_impulse.SetZero();
  10460. }
  10461. }
  10462. b2WeldJoint.prototype.SolveVelocityConstraints = function (step) {
  10463. var tMat;
  10464. var tX = 0;
  10465. var bA = this.m_bodyA;
  10466. var bB = this.m_bodyB;
  10467. var vA = bA.m_linearVelocity;
  10468. var wA = bA.m_angularVelocity;
  10469. var vB = bB.m_linearVelocity;
  10470. var wB = bB.m_angularVelocity;
  10471. var mA = bA.m_invMass;
  10472. var mB = bB.m_invMass;
  10473. var iA = bA.m_invI;
  10474. var iB = bB.m_invI;
  10475. tMat = bA.m_xf.R;
  10476. var rAX = this.m_localAnchorA.x - bA.m_sweep.localCenter.x;
  10477. var rAY = this.m_localAnchorA.y - bA.m_sweep.localCenter.y;
  10478. tX = (tMat.col1.x * rAX + tMat.col2.x * rAY);
  10479. rAY = (tMat.col1.y * rAX + tMat.col2.y * rAY);
  10480. rAX = tX;
  10481. tMat = bB.m_xf.R;
  10482. var rBX = this.m_localAnchorB.x - bB.m_sweep.localCenter.x;
  10483. var rBY = this.m_localAnchorB.y - bB.m_sweep.localCenter.y;
  10484. tX = (tMat.col1.x * rBX + tMat.col2.x * rBY);
  10485. rBY = (tMat.col1.y * rBX + tMat.col2.y * rBY);
  10486. rBX = tX;
  10487. var Cdot1X = vB.x - wB * rBY - vA.x + wA * rAY;
  10488. var Cdot1Y = vB.y + wB * rBX - vA.y - wA * rAX;
  10489. var Cdot2 = wB - wA;
  10490. var impulse = new b2Vec3();
  10491. this.m_mass.Solve33(impulse, (-Cdot1X), (-Cdot1Y), (-Cdot2));
  10492. this.m_impulse.Add(impulse);
  10493. vA.x -= mA * impulse.x;
  10494. vA.y -= mA * impulse.y;
  10495. wA -= iA * (rAX * impulse.y - rAY * impulse.x + impulse.z);
  10496. vB.x += mB * impulse.x;
  10497. vB.y += mB * impulse.y;
  10498. wB += iB * (rBX * impulse.y - rBY * impulse.x + impulse.z);
  10499. bA.m_angularVelocity = wA;
  10500. bB.m_angularVelocity = wB;
  10501. }
  10502. b2WeldJoint.prototype.SolvePositionConstraints = function (baumgarte) {
  10503. if (baumgarte === undefined) baumgarte = 0;
  10504. var tMat;
  10505. var tX = 0;
  10506. var bA = this.m_bodyA;
  10507. var bB = this.m_bodyB;
  10508. tMat = bA.m_xf.R;
  10509. var rAX = this.m_localAnchorA.x - bA.m_sweep.localCenter.x;
  10510. var rAY = this.m_localAnchorA.y - bA.m_sweep.localCenter.y;
  10511. tX = (tMat.col1.x * rAX + tMat.col2.x * rAY);
  10512. rAY = (tMat.col1.y * rAX + tMat.col2.y * rAY);
  10513. rAX = tX;
  10514. tMat = bB.m_xf.R;
  10515. var rBX = this.m_localAnchorB.x - bB.m_sweep.localCenter.x;
  10516. var rBY = this.m_localAnchorB.y - bB.m_sweep.localCenter.y;
  10517. tX = (tMat.col1.x * rBX + tMat.col2.x * rBY);
  10518. rBY = (tMat.col1.y * rBX + tMat.col2.y * rBY);
  10519. rBX = tX;
  10520. var mA = bA.m_invMass;
  10521. var mB = bB.m_invMass;
  10522. var iA = bA.m_invI;
  10523. var iB = bB.m_invI;
  10524. var C1X = bB.m_sweep.c.x + rBX - bA.m_sweep.c.x - rAX;
  10525. var C1Y = bB.m_sweep.c.y + rBY - bA.m_sweep.c.y - rAY;
  10526. var C2 = bB.m_sweep.a - bA.m_sweep.a - this.m_referenceAngle;
  10527. var k_allowedStretch = 10.0 * b2Settings.b2_linearSlop;
  10528. var positionError = Math.sqrt(C1X * C1X + C1Y * C1Y);
  10529. var angularError = b2Math.Abs(C2);
  10530. if (positionError > k_allowedStretch) {
  10531. iA *= 1.0;
  10532. iB *= 1.0;
  10533. }
  10534. this.m_mass.col1.x = mA + mB + rAY * rAY * iA + rBY * rBY * iB;
  10535. this.m_mass.col2.x = (-rAY * rAX * iA) - rBY * rBX * iB;
  10536. this.m_mass.col3.x = (-rAY * iA) - rBY * iB;
  10537. this.m_mass.col1.y = this.m_mass.col2.x;
  10538. this.m_mass.col2.y = mA + mB + rAX * rAX * iA + rBX * rBX * iB;
  10539. this.m_mass.col3.y = rAX * iA + rBX * iB;
  10540. this.m_mass.col1.z = this.m_mass.col3.x;
  10541. this.m_mass.col2.z = this.m_mass.col3.y;
  10542. this.m_mass.col3.z = iA + iB;
  10543. var impulse = new b2Vec3();
  10544. this.m_mass.Solve33(impulse, (-C1X), (-C1Y), (-C2));
  10545. bA.m_sweep.c.x -= mA * impulse.x;
  10546. bA.m_sweep.c.y -= mA * impulse.y;
  10547. bA.m_sweep.a -= iA * (rAX * impulse.y - rAY * impulse.x + impulse.z);
  10548. bB.m_sweep.c.x += mB * impulse.x;
  10549. bB.m_sweep.c.y += mB * impulse.y;
  10550. bB.m_sweep.a += iB * (rBX * impulse.y - rBY * impulse.x + impulse.z);
  10551. bA.SynchronizeTransform();
  10552. bB.SynchronizeTransform();
  10553. return positionError <= b2Settings.b2_linearSlop && angularError <= b2Settings.b2_angularSlop;
  10554. }
  10555. Box2D.inherit(b2WeldJointDef, Box2D.Dynamics.Joints.b2JointDef);
  10556. b2WeldJointDef.prototype.__super = Box2D.Dynamics.Joints.b2JointDef.prototype;
  10557. b2WeldJointDef.b2WeldJointDef = function () {
  10558. Box2D.Dynamics.Joints.b2JointDef.b2JointDef.apply(this, arguments);
  10559. this.localAnchorA = new b2Vec2();
  10560. this.localAnchorB = new b2Vec2();
  10561. };
  10562. b2WeldJointDef.prototype.b2WeldJointDef = function () {
  10563. this.__super.b2JointDef.call(this);
  10564. this.type = b2Joint.e_weldJoint;
  10565. this.referenceAngle = 0.0;
  10566. }
  10567. b2WeldJointDef.prototype.Initialize = function (bA, bB, anchor) {
  10568. this.bodyA = bA;
  10569. this.bodyB = bB;
  10570. this.localAnchorA.SetV(this.bodyA.GetLocalPoint(anchor));
  10571. this.localAnchorB.SetV(this.bodyB.GetLocalPoint(anchor));
  10572. this.referenceAngle = this.bodyB.GetAngle() - this.bodyA.GetAngle();
  10573. }
  10574. })();
  10575. (function () {
  10576. var b2DebugDraw = Box2D.Dynamics.b2DebugDraw;
  10577. b2DebugDraw.b2DebugDraw = function () {
  10578. this.m_drawScale = 1.0;
  10579. this.m_lineThickness = 1.0;
  10580. this.m_alpha = 1.0;
  10581. this.m_fillAlpha = 1.0;
  10582. this.m_xformScale = 1.0;
  10583. var __this = this;
  10584. //#WORKAROUND
  10585. this.m_sprite = {
  10586. graphics: {
  10587. clear: function () {
  10588. __this.m_ctx.clearRect(0, 0, __this.m_ctx.canvas.width, __this.m_ctx.canvas.height)
  10589. }
  10590. }
  10591. };
  10592. };
  10593. b2DebugDraw.prototype._color = function (color, alpha) {
  10594. return "rgba(" + ((color & 0xFF0000) >> 16) + "," + ((color & 0xFF00) >> 8) + "," + (color & 0xFF) + "," + alpha + ")";
  10595. };
  10596. b2DebugDraw.prototype.b2DebugDraw = function () {
  10597. this.m_drawFlags = 0;
  10598. };
  10599. b2DebugDraw.prototype.SetFlags = function (flags) {
  10600. if (flags === undefined) flags = 0;
  10601. this.m_drawFlags = flags;
  10602. };
  10603. b2DebugDraw.prototype.GetFlags = function () {
  10604. return this.m_drawFlags;
  10605. };
  10606. b2DebugDraw.prototype.AppendFlags = function (flags) {
  10607. if (flags === undefined) flags = 0;
  10608. this.m_drawFlags |= flags;
  10609. };
  10610. b2DebugDraw.prototype.ClearFlags = function (flags) {
  10611. if (flags === undefined) flags = 0;
  10612. this.m_drawFlags &= ~flags;
  10613. };
  10614. b2DebugDraw.prototype.SetSprite = function (sprite) {
  10615. this.m_ctx = sprite;
  10616. };
  10617. b2DebugDraw.prototype.GetSprite = function () {
  10618. return this.m_ctx;
  10619. };
  10620. b2DebugDraw.prototype.SetDrawScale = function (drawScale) {
  10621. if (drawScale === undefined) drawScale = 0;
  10622. this.m_drawScale = drawScale;
  10623. };
  10624. b2DebugDraw.prototype.GetDrawScale = function () {
  10625. return this.m_drawScale;
  10626. };
  10627. b2DebugDraw.prototype.SetLineThickness = function (lineThickness) {
  10628. if (lineThickness === undefined) lineThickness = 0;
  10629. this.m_lineThickness = lineThickness;
  10630. this.m_ctx.strokeWidth = lineThickness;
  10631. };
  10632. b2DebugDraw.prototype.GetLineThickness = function () {
  10633. return this.m_lineThickness;
  10634. };
  10635. b2DebugDraw.prototype.SetAlpha = function (alpha) {
  10636. if (alpha === undefined) alpha = 0;
  10637. this.m_alpha = alpha;
  10638. };
  10639. b2DebugDraw.prototype.GetAlpha = function () {
  10640. return this.m_alpha;
  10641. };
  10642. b2DebugDraw.prototype.SetFillAlpha = function (alpha) {
  10643. if (alpha === undefined) alpha = 0;
  10644. this.m_fillAlpha = alpha;
  10645. };
  10646. b2DebugDraw.prototype.GetFillAlpha = function () {
  10647. return this.m_fillAlpha;
  10648. };
  10649. b2DebugDraw.prototype.SetXFormScale = function (xformScale) {
  10650. if (xformScale === undefined) xformScale = 0;
  10651. this.m_xformScale = xformScale;
  10652. };
  10653. b2DebugDraw.prototype.GetXFormScale = function () {
  10654. return this.m_xformScale;
  10655. };
  10656. b2DebugDraw.prototype.DrawPolygon = function (vertices, vertexCount, color) {
  10657. if (!vertexCount) return;
  10658. var s = this.m_ctx;
  10659. var drawScale = this.m_drawScale;
  10660. s.beginPath();
  10661. s.strokeStyle = this._color(color.color, this.m_alpha);
  10662. s.moveTo(vertices[0].x * drawScale, vertices[0].y * drawScale);
  10663. for (var i = 1; i < vertexCount; i++) {
  10664. s.lineTo(vertices[i].x * drawScale, vertices[i].y * drawScale);
  10665. }
  10666. s.lineTo(vertices[0].x * drawScale, vertices[0].y * drawScale);
  10667. s.closePath();
  10668. s.stroke();
  10669. };
  10670. b2DebugDraw.prototype.DrawSolidPolygon = function (vertices, vertexCount, color) {
  10671. if (!vertexCount) return;
  10672. var s = this.m_ctx;
  10673. var drawScale = this.m_drawScale;
  10674. s.beginPath();
  10675. s.strokeStyle = this._color(color.color, this.m_alpha);
  10676. s.fillStyle = this._color(color.color, this.m_fillAlpha);
  10677. s.moveTo(vertices[0].x * drawScale, vertices[0].y * drawScale);
  10678. for (var i = 1; i < vertexCount; i++) {
  10679. s.lineTo(vertices[i].x * drawScale, vertices[i].y * drawScale);
  10680. }
  10681. s.lineTo(vertices[0].x * drawScale, vertices[0].y * drawScale);
  10682. s.closePath();
  10683. s.fill();
  10684. s.stroke();
  10685. };
  10686. b2DebugDraw.prototype.DrawCircle = function (center, radius, color) {
  10687. if (!radius) return;
  10688. var s = this.m_ctx;
  10689. var drawScale = this.m_drawScale;
  10690. s.beginPath();
  10691. s.strokeStyle = this._color(color.color, this.m_alpha);
  10692. s.arc(center.x * drawScale, center.y * drawScale, radius * drawScale, 0, Math.PI * 2, true);
  10693. s.closePath();
  10694. s.stroke();
  10695. };
  10696. b2DebugDraw.prototype.DrawSolidCircle = function (center, radius, axis, color) {
  10697. if (!radius) return;
  10698. var s = this.m_ctx,
  10699. drawScale = this.m_drawScale,
  10700. cx = center.x * drawScale,
  10701. cy = center.y * drawScale;
  10702. s.moveTo(0, 0);
  10703. s.beginPath();
  10704. s.strokeStyle = this._color(color.color, this.m_alpha);
  10705. s.fillStyle = this._color(color.color, this.m_fillAlpha);
  10706. s.arc(cx, cy, radius * drawScale, 0, Math.PI * 2, true);
  10707. s.moveTo(cx, cy);
  10708. s.lineTo((center.x + axis.x * radius) * drawScale, (center.y + axis.y * radius) * drawScale);
  10709. s.closePath();
  10710. s.fill();
  10711. s.stroke();
  10712. };
  10713. b2DebugDraw.prototype.DrawSegment = function (p1, p2, color) {
  10714. var s = this.m_ctx,
  10715. drawScale = this.m_drawScale;
  10716. s.strokeStyle = this._color(color.color, this.m_alpha);
  10717. s.beginPath();
  10718. s.moveTo(p1.x * drawScale, p1.y * drawScale);
  10719. s.lineTo(p2.x * drawScale, p2.y * drawScale);
  10720. s.closePath();
  10721. s.stroke();
  10722. };
  10723. b2DebugDraw.prototype.DrawTransform = function (xf) {
  10724. var s = this.m_ctx,
  10725. drawScale = this.m_drawScale;
  10726. s.beginPath();
  10727. s.strokeStyle = this._color(0xff0000, this.m_alpha);
  10728. s.moveTo(xf.position.x * drawScale, xf.position.y * drawScale);
  10729. s.lineTo((xf.position.x + this.m_xformScale * xf.R.col1.x) * drawScale, (xf.position.y + this.m_xformScale * xf.R.col1.y) * drawScale);
  10730. s.strokeStyle = this._color(0xff00, this.m_alpha);
  10731. s.moveTo(xf.position.x * drawScale, xf.position.y * drawScale);
  10732. s.lineTo((xf.position.x + this.m_xformScale * xf.R.col2.x) * drawScale, (xf.position.y + this.m_xformScale * xf.R.col2.y) * drawScale);
  10733. s.closePath();
  10734. s.stroke();
  10735. };
  10736. })(); //post-definitions
  10737. var i;
  10738. for (i = 0; i < Box2D.postDefs.length; ++i) Box2D.postDefs[i]();
  10739. delete Box2D.postDefs;