Oimo.js 365 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317
  1. (function (global, factory) {
  2. typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
  3. typeof define === 'function' && define.amd ? define(['exports'], factory) :
  4. (factory((global.OIMO = global.OIMO || {})));
  5. }(this, (function (exports) { 'use strict';
  6. // Polyfills
  7. if ( Number.EPSILON === undefined ) {
  8. Number.EPSILON = Math.pow( 2, - 52 );
  9. }
  10. //
  11. if ( Math.sign === undefined ) {
  12. // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/sign
  13. Math.sign = function ( x ) {
  14. return ( x < 0 ) ? - 1 : ( x > 0 ) ? 1 : + x;
  15. };
  16. }
  17. if ( Function.prototype.name === undefined ) {
  18. // Missing in IE9-11.
  19. // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/name
  20. Object.defineProperty( Function.prototype, 'name', {
  21. get: function () {
  22. return this.toString().match( /^\s*function\s*([^\(\s]*)/ )[ 1 ];
  23. }
  24. } );
  25. }
  26. if ( Object.assign === undefined ) {
  27. // Missing in IE.
  28. // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign
  29. ( function () {
  30. Object.assign = function ( target ) {
  31. 'use strict';
  32. if ( target === undefined || target === null ) {
  33. throw new TypeError( 'Cannot convert undefined or null to object' );
  34. }
  35. var output = Object( target );
  36. for ( var index = 1; index < arguments.length; index ++ ) {
  37. var source = arguments[ index ];
  38. if ( source !== undefined && source !== null ) {
  39. for ( var nextKey in source ) {
  40. if ( Object.prototype.hasOwnProperty.call( source, nextKey ) ) {
  41. output[ nextKey ] = source[ nextKey ];
  42. }
  43. }
  44. }
  45. }
  46. return output;
  47. };
  48. } )();
  49. }
  50. /*
  51. * A list of constants built-in for
  52. * the physics engine.
  53. */
  54. var REVISION = '1.0.9';
  55. // BroadPhase
  56. var BR_NULL = 0;
  57. var BR_BRUTE_FORCE = 1;
  58. var BR_SWEEP_AND_PRUNE = 2;
  59. var BR_BOUNDING_VOLUME_TREE = 3;
  60. // Body type
  61. var BODY_NULL = 0;
  62. var BODY_DYNAMIC = 1;
  63. var BODY_STATIC = 2;
  64. var BODY_KINEMATIC = 3;
  65. var BODY_GHOST = 4;
  66. // Shape type
  67. var SHAPE_NULL = 0;
  68. var SHAPE_SPHERE = 1;
  69. var SHAPE_BOX = 2;
  70. var SHAPE_CYLINDER = 3;
  71. var SHAPE_PLANE = 4;
  72. var SHAPE_PARTICLE = 5;
  73. var SHAPE_TETRA = 6;
  74. // Joint type
  75. var JOINT_NULL = 0;
  76. var JOINT_DISTANCE = 1;
  77. var JOINT_BALL_AND_SOCKET = 2;
  78. var JOINT_HINGE = 3;
  79. var JOINT_WHEEL = 4;
  80. var JOINT_SLIDER = 5;
  81. var JOINT_PRISMATIC = 6;
  82. // AABB aproximation
  83. var AABB_PROX = 0.005;
  84. var _Math = {
  85. sqrt : Math.sqrt,
  86. abs : Math.abs,
  87. floor : Math.floor,
  88. cos : Math.cos,
  89. sin : Math.sin,
  90. acos : Math.acos,
  91. asin : Math.asin,
  92. atan2 : Math.atan2,
  93. round : Math.round,
  94. pow : Math.pow,
  95. max : Math.max,
  96. min : Math.min,
  97. random : Math.random,
  98. degtorad : 0.0174532925199432957,
  99. radtodeg : 57.295779513082320876,
  100. PI : 3.141592653589793,
  101. TwoPI : 6.283185307179586,
  102. PI90 : 1.570796326794896,
  103. PI270 : 4.712388980384689,
  104. INF : Infinity,
  105. EPZ : 0.00001,
  106. EPZ2 : 0.000001,
  107. lerp: function ( x, y, t ) {
  108. return ( 1 - t ) * x + t * y;
  109. },
  110. randInt: function ( low, high ) {
  111. return low + _Math.floor( _Math.random() * ( high - low + 1 ) );
  112. },
  113. rand: function ( low, high ) {
  114. return low + _Math.random() * ( high - low );
  115. },
  116. generateUUID: function () {
  117. // http://www.broofa.com/Tools/Math.uuid.htm
  118. var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split( '' );
  119. var uuid = new Array( 36 );
  120. var rnd = 0, r;
  121. return function generateUUID() {
  122. for ( var i = 0; i < 36; i ++ ) {
  123. if ( i === 8 || i === 13 || i === 18 || i === 23 ) {
  124. uuid[ i ] = '-';
  125. } else if ( i === 14 ) {
  126. uuid[ i ] = '4';
  127. } else {
  128. if ( rnd <= 0x02 ) rnd = 0x2000000 + ( Math.random() * 0x1000000 ) | 0;
  129. r = rnd & 0xf;
  130. rnd = rnd >> 4;
  131. uuid[ i ] = chars[ ( i === 19 ) ? ( r & 0x3 ) | 0x8 : r ];
  132. }
  133. }
  134. return uuid.join( '' );
  135. };
  136. }(),
  137. int: function( x ) {
  138. return _Math.floor(x);
  139. },
  140. fix: function( x, n ) {
  141. return x.toFixed(n || 3, 10);
  142. },
  143. clamp: function ( value, min, max ) {
  144. return _Math.max( min, _Math.min( max, value ) );
  145. },
  146. //clamp: function ( x, a, b ) { return ( x < a ) ? a : ( ( x > b ) ? b : x ); },
  147. distance: function( p1, p2 ){
  148. var xd = p2[0]-p1[0];
  149. var yd = p2[1]-p1[1];
  150. var zd = p2[2]-p1[2];
  151. return _Math.sqrt(xd*xd + yd*yd + zd*zd);
  152. },
  153. /*unwrapDegrees: function ( r ) {
  154. r = r % 360;
  155. if (r > 180) r -= 360;
  156. if (r < -180) r += 360;
  157. return r;
  158. },
  159. unwrapRadian: function( r ){
  160. r = r % _Math.TwoPI;
  161. if (r > _Math.PI) r -= _Math.TwoPI;
  162. if (r < -_Math.PI) r += _Math.TwoPI;
  163. return r;
  164. },*/
  165. acosClamp: function ( cos ) {
  166. if(cos>1)return 0;
  167. else if(cos<-1)return _Math.PI;
  168. else return _Math.acos(cos);
  169. },
  170. distanceVector: function( v1, v2 ){
  171. var xd = v1.x - v2.x;
  172. var yd = v1.y - v2.y;
  173. var zd = v1.z - v2.z;
  174. return xd * xd + yd * yd + zd * zd;
  175. },
  176. dotVectors: function ( a, b ) {
  177. return a.x * b.x + a.y * b.y + a.z * b.z;
  178. },
  179. };
  180. function printError( clazz, msg ){
  181. console.error("[OIMO] " + clazz + ": " + msg);
  182. }
  183. // A performance evaluator
  184. function InfoDisplay(world){
  185. this.parent = world;
  186. this.infos = new Float32Array( 13 );
  187. this.f = [0,0,0];
  188. this.times = [0,0,0,0];
  189. this.broadPhase = this.parent.broadPhaseType;
  190. this.version = REVISION;
  191. this.fps = 0;
  192. this.tt = 0;
  193. this.broadPhaseTime = 0;
  194. this.narrowPhaseTime = 0;
  195. this.solvingTime = 0;
  196. this.totalTime = 0;
  197. this.updateTime = 0;
  198. this.MaxBroadPhaseTime = 0;
  199. this.MaxNarrowPhaseTime = 0;
  200. this.MaxSolvingTime = 0;
  201. this.MaxTotalTime = 0;
  202. this.MaxUpdateTime = 0;
  203. }
  204. Object.assign( InfoDisplay.prototype, {
  205. setTime: function(n){
  206. this.times[ n || 0 ] = performance.now();
  207. },
  208. resetMax: function(){
  209. this.MaxBroadPhaseTime = 0;
  210. this.MaxNarrowPhaseTime = 0;
  211. this.MaxSolvingTime = 0;
  212. this.MaxTotalTime = 0;
  213. this.MaxUpdateTime = 0;
  214. },
  215. calcBroadPhase: function () {
  216. this.setTime( 2 );
  217. this.broadPhaseTime = this.times[ 2 ] - this.times[ 1 ];
  218. },
  219. calcNarrowPhase: function () {
  220. this.setTime( 3 );
  221. this.narrowPhaseTime = this.times[ 3 ] - this.times[ 2 ];
  222. },
  223. calcEnd: function () {
  224. this.setTime( 2 );
  225. this.solvingTime = this.times[ 2 ] - this.times[ 1 ];
  226. this.totalTime = this.times[ 2 ] - this.times[ 0 ];
  227. this.updateTime = this.totalTime - ( this.broadPhaseTime + this.narrowPhaseTime + this.solvingTime );
  228. if( this.tt === 100 ) this.resetMax();
  229. if( this.tt > 100 ){
  230. if( this.broadPhaseTime > this.MaxBroadPhaseTime ) this.MaxBroadPhaseTime = this.broadPhaseTime;
  231. if( this.narrowPhaseTime > this.MaxNarrowPhaseTime ) this.MaxNarrowPhaseTime = this.narrowPhaseTime;
  232. if( this.solvingTime > this.MaxSolvingTime ) this.MaxSolvingTime = this.solvingTime;
  233. if( this.totalTime > this.MaxTotalTime ) this.MaxTotalTime = this.totalTime;
  234. if( this.updateTime > this.MaxUpdateTime ) this.MaxUpdateTime = this.updateTime;
  235. }
  236. this.upfps();
  237. this.tt ++;
  238. if(this.tt > 500) this.tt = 0;
  239. },
  240. upfps : function(){
  241. this.f[1] = Date.now();
  242. if (this.f[1]-1000>this.f[0]){ this.f[0] = this.f[1]; this.fps = this.f[2]; this.f[2] = 0; } this.f[2]++;
  243. },
  244. show: function(){
  245. var info =[
  246. "Oimo.js "+this.version+"<br>",
  247. this.broadPhase + "<br><br>",
  248. "FPS: " + this.fps +" fps<br><br>",
  249. "rigidbody "+this.parent.numRigidBodies+"<br>",
  250. "contact &nbsp;&nbsp;"+this.parent.numContacts+"<br>",
  251. "ct-point &nbsp;"+this.parent.numContactPoints+"<br>",
  252. "paircheck "+this.parent.broadPhase.numPairChecks+"<br>",
  253. "island &nbsp;&nbsp;&nbsp;"+this.parent.numIslands +"<br><br>",
  254. "Time in milliseconds<br><br>",
  255. "broadphase &nbsp;"+ _Math.fix(this.broadPhaseTime) + " | " + _Math.fix(this.MaxBroadPhaseTime) +"<br>",
  256. "narrowphase "+ _Math.fix(this.narrowPhaseTime) + " | " + _Math.fix(this.MaxNarrowPhaseTime) + "<br>",
  257. "solving &nbsp;&nbsp;&nbsp;&nbsp;"+ _Math.fix(this.solvingTime)+ " | " + _Math.fix(this.MaxSolvingTime) + "<br>",
  258. "total &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"+ _Math.fix(this.totalTime) + " | " + _Math.fix(this.MaxTotalTime) + "<br>",
  259. "updating &nbsp;&nbsp;&nbsp;"+ _Math.fix(this.updateTime) + " | " + _Math.fix(this.MaxUpdateTime) + "<br>"
  260. ].join("\n");
  261. return info;
  262. },
  263. toArray: function(){
  264. this.infos[0] = this.parent.broadPhase.types;
  265. this.infos[1] = this.parent.numRigidBodies;
  266. this.infos[2] = this.parent.numContacts;
  267. this.infos[3] = this.parent.broadPhase.numPairChecks;
  268. this.infos[4] = this.parent.numContactPoints;
  269. this.infos[5] = this.parent.numIslands;
  270. this.infos[6] = this.broadPhaseTime;
  271. this.infos[7] = this.narrowPhaseTime;
  272. this.infos[8] = this.solvingTime;
  273. this.infos[9] = this.updateTime;
  274. this.infos[10] = this.totalTime;
  275. this.infos[11] = this.fps;
  276. return this.infos;
  277. }
  278. });
  279. function Vec3 ( x, y, z ) {
  280. this.x = x || 0;
  281. this.y = y || 0;
  282. this.z = z || 0;
  283. }
  284. Object.assign( Vec3.prototype, {
  285. Vec3: true,
  286. set: function( x, y, z ){
  287. this.x = x;
  288. this.y = y;
  289. this.z = z;
  290. return this;
  291. },
  292. add: function ( a, b ) {
  293. if ( b !== undefined ) return this.addVectors( a, b );
  294. this.x += a.x;
  295. this.y += a.y;
  296. this.z += a.z;
  297. return this;
  298. },
  299. addVectors: function ( a, b ) {
  300. this.x = a.x + b.x;
  301. this.y = a.y + b.y;
  302. this.z = a.z + b.z;
  303. return this;
  304. },
  305. addEqual: function ( v ) {
  306. this.x += v.x;
  307. this.y += v.y;
  308. this.z += v.z;
  309. return this;
  310. },
  311. sub: function ( a, b ) {
  312. if ( b !== undefined ) return this.subVectors( a, b );
  313. this.x -= a.x;
  314. this.y -= a.y;
  315. this.z -= a.z;
  316. return this;
  317. },
  318. subVectors: function ( a, b ) {
  319. this.x = a.x - b.x;
  320. this.y = a.y - b.y;
  321. this.z = a.z - b.z;
  322. return this;
  323. },
  324. subEqual: function ( v ) {
  325. this.x -= v.x;
  326. this.y -= v.y;
  327. this.z -= v.z;
  328. return this;
  329. },
  330. scale: function ( v, s ) {
  331. this.x = v.x * s;
  332. this.y = v.y * s;
  333. this.z = v.z * s;
  334. return this;
  335. },
  336. scaleEqual: function( s ){
  337. this.x *= s;
  338. this.y *= s;
  339. this.z *= s;
  340. return this;
  341. },
  342. multiply: function( v ){
  343. this.x *= v.x;
  344. this.y *= v.y;
  345. this.z *= v.z;
  346. return this;
  347. },
  348. multiplyScalar: function( s ){
  349. this.x *= s;
  350. this.y *= s;
  351. this.z *= s;
  352. return this;
  353. },
  354. /*scaleV: function( v ){
  355. this.x *= v.x;
  356. this.y *= v.y;
  357. this.z *= v.z;
  358. return this;
  359. },
  360. scaleVectorEqual: function( v ){
  361. this.x *= v.x;
  362. this.y *= v.y;
  363. this.z *= v.z;
  364. return this;
  365. },*/
  366. addScaledVector: function ( v, s ) {
  367. this.x += v.x * s;
  368. this.y += v.y * s;
  369. this.z += v.z * s;
  370. return this;
  371. },
  372. subScaledVector: function ( v, s ) {
  373. this.x -= v.x * s;
  374. this.y -= v.y * s;
  375. this.z -= v.z * s;
  376. return this;
  377. },
  378. /*addTime: function ( v, t ) {
  379. this.x += v.x * t;
  380. this.y += v.y * t;
  381. this.z += v.z * t;
  382. return this;
  383. },
  384. addScale: function ( v, s ) {
  385. this.x += v.x * s;
  386. this.y += v.y * s;
  387. this.z += v.z * s;
  388. return this;
  389. },
  390. subScale: function ( v, s ) {
  391. this.x -= v.x * s;
  392. this.y -= v.y * s;
  393. this.z -= v.z * s;
  394. return this;
  395. },*/
  396. cross: function( a, b ) {
  397. if ( b !== undefined ) return this.crossVectors( a, b );
  398. var x = this.x, y = this.y, z = this.z;
  399. this.x = y * a.z - z * a.y;
  400. this.y = z * a.x - x * a.z;
  401. this.z = x * a.y - y * a.x;
  402. return this;
  403. },
  404. crossVectors: function ( a, b ) {
  405. var ax = a.x, ay = a.y, az = a.z;
  406. var bx = b.x, by = b.y, bz = b.z;
  407. this.x = ay * bz - az * by;
  408. this.y = az * bx - ax * bz;
  409. this.z = ax * by - ay * bx;
  410. return this;
  411. },
  412. tangent: function ( a ) {
  413. var ax = a.x, ay = a.y, az = a.z;
  414. this.x = ay * ax - az * az;
  415. this.y = - az * ay - ax * ax;
  416. this.z = ax * az + ay * ay;
  417. return this;
  418. },
  419. invert: function ( v ) {
  420. this.x=-v.x;
  421. this.y=-v.y;
  422. this.z=-v.z;
  423. return this;
  424. },
  425. negate: function () {
  426. this.x = - this.x;
  427. this.y = - this.y;
  428. this.z = - this.z;
  429. return this;
  430. },
  431. dot: function ( v ) {
  432. return this.x * v.x + this.y * v.y + this.z * v.z;
  433. },
  434. addition: function () {
  435. return this.x + this.y + this.z;
  436. },
  437. lengthSq: function () {
  438. return this.x * this.x + this.y * this.y + this.z * this.z;
  439. },
  440. length: function () {
  441. return _Math.sqrt( this.x * this.x + this.y * this.y + this.z * this.z );
  442. },
  443. copy: function( v ){
  444. this.x = v.x;
  445. this.y = v.y;
  446. this.z = v.z;
  447. return this;
  448. },
  449. /*mul: function( b, a, m ){
  450. return this.mulMat( m, a ).add( b );
  451. },
  452. mulMat: function( m, a ){
  453. var e = m.elements;
  454. var x = a.x, y = a.y, z = a.z;
  455. this.x = e[ 0 ] * x + e[ 1 ] * y + e[ 2 ] * z;
  456. this.y = e[ 3 ] * x + e[ 4 ] * y + e[ 5 ] * z;
  457. this.z = e[ 6 ] * x + e[ 7 ] * y + e[ 8 ] * z;
  458. return this;
  459. },*/
  460. applyMatrix3: function ( m, transpose ) {
  461. //if( transpose ) m = m.clone().transpose();
  462. var x = this.x, y = this.y, z = this.z;
  463. var e = m.elements;
  464. if( transpose ){
  465. this.x = e[ 0 ] * x + e[ 1 ] * y + e[ 2 ] * z;
  466. this.y = e[ 3 ] * x + e[ 4 ] * y + e[ 5 ] * z;
  467. this.z = e[ 6 ] * x + e[ 7 ] * y + e[ 8 ] * z;
  468. } else {
  469. this.x = e[ 0 ] * x + e[ 3 ] * y + e[ 6 ] * z;
  470. this.y = e[ 1 ] * x + e[ 4 ] * y + e[ 7 ] * z;
  471. this.z = e[ 2 ] * x + e[ 5 ] * y + e[ 8 ] * z;
  472. }
  473. return this;
  474. },
  475. applyQuaternion: function ( q ) {
  476. var x = this.x;
  477. var y = this.y;
  478. var z = this.z;
  479. var qx = q.x;
  480. var qy = q.y;
  481. var qz = q.z;
  482. var qw = q.w;
  483. // calculate quat * vector
  484. var ix = qw * x + qy * z - qz * y;
  485. var iy = qw * y + qz * x - qx * z;
  486. var iz = qw * z + qx * y - qy * x;
  487. var iw = - qx * x - qy * y - qz * z;
  488. // calculate result * inverse quat
  489. this.x = ix * qw + iw * - qx + iy * - qz - iz * - qy;
  490. this.y = iy * qw + iw * - qy + iz * - qx - ix * - qz;
  491. this.z = iz * qw + iw * - qz + ix * - qy - iy * - qx;
  492. return this;
  493. },
  494. testZero: function () {
  495. if(this.x!==0 || this.y!==0 || this.z!==0) return true;
  496. else return false;
  497. },
  498. testDiff: function( v ){
  499. return this.equals( v ) ? false : true;
  500. },
  501. equals: function ( v ) {
  502. return v.x === this.x && v.y === this.y && v.z === this.z;
  503. },
  504. clone: function () {
  505. return new this.constructor( this.x, this.y, this.z );
  506. },
  507. toString: function(){
  508. return"Vec3["+this.x.toFixed(4)+", "+this.y.toFixed(4)+", "+this.z.toFixed(4)+"]";
  509. },
  510. multiplyScalar: function ( scalar ) {
  511. if ( isFinite( scalar ) ) {
  512. this.x *= scalar;
  513. this.y *= scalar;
  514. this.z *= scalar;
  515. } else {
  516. this.x = 0;
  517. this.y = 0;
  518. this.z = 0;
  519. }
  520. return this;
  521. },
  522. divideScalar: function ( scalar ) {
  523. return this.multiplyScalar( 1 / scalar );
  524. },
  525. normalize: function () {
  526. return this.divideScalar( this.length() );
  527. },
  528. toArray: function ( array, offset ) {
  529. if ( offset === undefined ) offset = 0;
  530. array[ offset ] = this.x;
  531. array[ offset + 1 ] = this.y;
  532. array[ offset + 2 ] = this.z;
  533. },
  534. fromArray: function( array, offset ){
  535. if ( offset === undefined ) offset = 0;
  536. this.x = array[ offset ];
  537. this.y = array[ offset + 1 ];
  538. this.z = array[ offset + 2 ];
  539. return this;
  540. },
  541. } );
  542. function Quat ( x, y, z, w ){
  543. this.x = x || 0;
  544. this.y = y || 0;
  545. this.z = z || 0;
  546. this.w = ( w !== undefined ) ? w : 1;
  547. }
  548. Object.assign( Quat.prototype, {
  549. Quat: true,
  550. set: function ( x, y, z, w ) {
  551. this.x = x;
  552. this.y = y;
  553. this.z = z;
  554. this.w = w;
  555. return this;
  556. },
  557. addTime: function( v, t ){
  558. var ax = v.x, ay = v.y, az = v.z;
  559. var qw = this.w, qx = this.x, qy = this.y, qz = this.z;
  560. t *= 0.5;
  561. this.x += t * ( ax*qw + ay*qz - az*qy );
  562. this.y += t * ( ay*qw + az*qx - ax*qz );
  563. this.z += t * ( az*qw + ax*qy - ay*qx );
  564. this.w += t * ( -ax*qx - ay*qy - az*qz );
  565. this.normalize();
  566. return this;
  567. },
  568. /*mul: function( q1, q2 ){
  569. var ax = q1.x, ay = q1.y, az = q1.z, as = q1.w,
  570. bx = q2.x, by = q2.y, bz = q2.z, bs = q2.w;
  571. this.x = ax * bs + as * bx + ay * bz - az * by;
  572. this.y = ay * bs + as * by + az * bx - ax * bz;
  573. this.z = az * bs + as * bz + ax * by - ay * bx;
  574. this.w = as * bs - ax * bx - ay * by - az * bz;
  575. return this;
  576. },*/
  577. multiply: function ( q, p ) {
  578. if ( p !== undefined ) return this.multiplyQuaternions( q, p );
  579. return this.multiplyQuaternions( this, q );
  580. },
  581. multiplyQuaternions: function ( a, b ) {
  582. var qax = a.x, qay = a.y, qaz = a.z, qaw = a.w;
  583. var qbx = b.x, qby = b.y, qbz = b.z, qbw = b.w;
  584. this.x = qax * qbw + qaw * qbx + qay * qbz - qaz * qby;
  585. this.y = qay * qbw + qaw * qby + qaz * qbx - qax * qbz;
  586. this.z = qaz * qbw + qaw * qbz + qax * qby - qay * qbx;
  587. this.w = qaw * qbw - qax * qbx - qay * qby - qaz * qbz;
  588. return this;
  589. },
  590. setFromUnitVectors: function( v1, v2 ) {
  591. var vx = new Vec3();
  592. var r = v1.dot( v2 ) + 1;
  593. if ( r < _Math.EPS2 ) {
  594. r = 0;
  595. if ( _Math.abs( v1.x ) > _Math.abs( v1.z ) ) vx.set( - v1.y, v1.x, 0 );
  596. else vx.set( 0, - v1.z, v1.y );
  597. } else {
  598. vx.crossVectors( v1, v2 );
  599. }
  600. this._x = vx.x;
  601. this._y = vx.y;
  602. this._z = vx.z;
  603. this._w = r;
  604. return this.normalize();
  605. },
  606. arc: function( v1, v2 ){
  607. var x1 = v1.x;
  608. var y1 = v1.y;
  609. var z1 = v1.z;
  610. var x2 = v2.x;
  611. var y2 = v2.y;
  612. var z2 = v2.z;
  613. var d = x1*x2 + y1*y2 + z1*z2;
  614. if( d==-1 ){
  615. x2 = y1*x1 - z1*z1;
  616. y2 = -z1*y1 - x1*x1;
  617. z2 = x1*z1 + y1*y1;
  618. d = 1 / _Math.sqrt( x2*x2 + y2*y2 + z2*z2 );
  619. this.w = 0;
  620. this.x = x2*d;
  621. this.y = y2*d;
  622. this.z = z2*d;
  623. return this;
  624. }
  625. var cx = y1*z2 - z1*y2;
  626. var cy = z1*x2 - x1*z2;
  627. var cz = x1*y2 - y1*x2;
  628. this.w = _Math.sqrt( ( 1 + d) * 0.5 );
  629. d = 0.5 / this.w;
  630. this.x = cx * d;
  631. this.y = cy * d;
  632. this.z = cz * d;
  633. return this;
  634. },
  635. normalize: function(){
  636. var l = this.length();
  637. if ( l === 0 ) {
  638. this.set( 0, 0, 0, 1 );
  639. } else {
  640. l = 1 / l;
  641. this.x = this.x * l;
  642. this.y = this.y * l;
  643. this.z = this.z * l;
  644. this.w = this.w * l;
  645. }
  646. return this;
  647. },
  648. inverse: function () {
  649. return this.conjugate().normalize();
  650. },
  651. invert: function ( q ) {
  652. this.x = q.x;
  653. this.y = q.y;
  654. this.z = q.z;
  655. this.w = q.w;
  656. this.conjugate().normalize();
  657. return this;
  658. },
  659. conjugate: function () {
  660. this.x *= - 1;
  661. this.y *= - 1;
  662. this.z *= - 1;
  663. return this;
  664. },
  665. length: function(){
  666. return _Math.sqrt( this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w );
  667. },
  668. lengthSq: function () {
  669. return this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w;
  670. },
  671. copy: function( q ){
  672. this.x = q.x;
  673. this.y = q.y;
  674. this.z = q.z;
  675. this.w = q.w;
  676. return this;
  677. },
  678. clone: function( q ){
  679. return new Quat( this.x, this.y, this.z, this.w );
  680. },
  681. testDiff: function ( q ) {
  682. return this.equals( q ) ? false : true;
  683. },
  684. equals: function ( q ) {
  685. return this.x === q.x && this.y === q.y && this.z === q.z && this.w === q.w;
  686. },
  687. toString: function(){
  688. return"Quat["+this.x.toFixed(4)+", ("+this.y.toFixed(4)+", "+this.z.toFixed(4)+", "+this.w.toFixed(4)+")]";
  689. },
  690. setFromEuler: function ( x, y, z ){
  691. var c1 = Math.cos( x * 0.5 );
  692. var c2 = Math.cos( y * 0.5 );
  693. var c3 = Math.cos( z * 0.5 );
  694. var s1 = Math.sin( x * 0.5 );
  695. var s2 = Math.sin( y * 0.5 );
  696. var s3 = Math.sin( z * 0.5 );
  697. // XYZ
  698. this.x = s1 * c2 * c3 + c1 * s2 * s3;
  699. this.y = c1 * s2 * c3 - s1 * c2 * s3;
  700. this.z = c1 * c2 * s3 + s1 * s2 * c3;
  701. this.w = c1 * c2 * c3 - s1 * s2 * s3;
  702. return this;
  703. },
  704. setFromAxis: function ( axis, rad ) {
  705. axis.normalize();
  706. rad = rad * 0.5;
  707. var s = _Math.sin( rad );
  708. this.x = s * axis.x;
  709. this.y = s * axis.y;
  710. this.z = s * axis.z;
  711. this.w = _Math.cos( rad );
  712. return this;
  713. },
  714. setFromMat33: function ( m ) {
  715. var trace = m[0] + m[4] + m[8];
  716. var s;
  717. if ( trace > 0 ) {
  718. s = _Math.sqrt( trace + 1.0 );
  719. this.w = 0.5 / s;
  720. s = 0.5 / s;
  721. this.x = ( m[5] - m[7] ) * s;
  722. this.y = ( m[6] - m[2] ) * s;
  723. this.z = ( m[1] - m[3] ) * s;
  724. } else {
  725. var out = [];
  726. var i = 0;
  727. if ( m[4] > m[0] ) i = 1;
  728. if ( m[8] > m[i*3+i] ) i = 2;
  729. var j = (i+1)%3;
  730. var k = (i+2)%3;
  731. s = _Math.sqrt( m[i*3+i] - m[j*3+j] - m[k*3+k] + 1.0 );
  732. out[i] = 0.5 * fRoot;
  733. s = 0.5 / fRoot;
  734. this.w = ( m[j*3+k] - m[k*3+j] ) * s;
  735. out[j] = ( m[j*3+i] + m[i*3+j] ) * s;
  736. out[k] = ( m[k*3+i] + m[i*3+k] ) * s;
  737. this.x = out[1];
  738. this.y = out[2];
  739. this.z = out[3];
  740. }
  741. return this;
  742. },
  743. toArray: function ( array, offset ) {
  744. offset = offset || 0;
  745. array[ offset ] = this.x;
  746. array[ offset + 1 ] = this.y;
  747. array[ offset + 2 ] = this.z;
  748. array[ offset + 3 ] = this.w;
  749. },
  750. fromArray: function( array, offset ){
  751. offset = offset || 0;
  752. this.set( array[ offset ], array[ offset + 1 ], array[ offset + 2 ], array[ offset + 3 ] );
  753. return this;
  754. }
  755. });
  756. function Mat33 ( e00, e01, e02, e10, e11, e12, e20, e21, e22 ){
  757. this.elements = [
  758. 1, 0, 0,
  759. 0, 1, 0,
  760. 0, 0, 1
  761. ];
  762. if ( arguments.length > 0 ) {
  763. console.error( 'OIMO.Mat33: the constructor no longer reads arguments. use .set() instead.' );
  764. }
  765. }
  766. Object.assign( Mat33.prototype, {
  767. Mat33: true,
  768. set: function ( e00, e01, e02, e10, e11, e12, e20, e21, e22 ){
  769. var te = this.elements;
  770. te[0] = e00; te[1] = e01; te[2] = e02;
  771. te[3] = e10; te[4] = e11; te[5] = e12;
  772. te[6] = e20; te[7] = e21; te[8] = e22;
  773. return this;
  774. },
  775. add: function ( a, b ) {
  776. if( b !== undefined ) return this.addMatrixs( a, b );
  777. var e = this.elements, te = a.elements;
  778. e[0] += te[0]; e[1] += te[1]; e[2] += te[2];
  779. e[3] += te[3]; e[4] += te[4]; e[5] += te[5];
  780. e[6] += te[6]; e[7] += te[7]; e[8] += te[8];
  781. return this;
  782. },
  783. addMatrixs: function ( a, b ) {
  784. var te = this.elements, tem1 = a.elements, tem2 = b.elements;
  785. te[0] = tem1[0] + tem2[0]; te[1] = tem1[1] + tem2[1]; te[2] = tem1[2] + tem2[2];
  786. te[3] = tem1[3] + tem2[3]; te[4] = tem1[4] + tem2[4]; te[5] = tem1[5] + tem2[5];
  787. te[6] = tem1[6] + tem2[6]; te[7] = tem1[7] + tem2[7]; te[8] = tem1[8] + tem2[8];
  788. return this;
  789. },
  790. addEqual: function( m ){
  791. var te = this.elements, tem = m.elements;
  792. te[0] += tem[0]; te[1] += tem[1]; te[2] += tem[2];
  793. te[3] += tem[3]; te[4] += tem[4]; te[5] += tem[5];
  794. te[6] += tem[6]; te[7] += tem[7]; te[8] += tem[8];
  795. return this;
  796. },
  797. sub: function ( a, b ) {
  798. if( b !== undefined ) return this.subMatrixs( a, b );
  799. var e = this.elements, te = a.elements;
  800. e[0] -= te[0]; e[1] -= te[1]; e[2] -= te[2];
  801. e[3] -= te[3]; e[4] -= te[4]; e[5] -= te[5];
  802. e[6] -= te[6]; e[7] -= te[7]; e[8] -= te[8];
  803. return this;
  804. },
  805. subMatrixs: function ( a, b ) {
  806. var te = this.elements, tem1 = a.elements, tem2 = b.elements;
  807. te[0] = tem1[0] - tem2[0]; te[1] = tem1[1] - tem2[1]; te[2] = tem1[2] - tem2[2];
  808. te[3] = tem1[3] - tem2[3]; te[4] = tem1[4] - tem2[4]; te[5] = tem1[5] - tem2[5];
  809. te[6] = tem1[6] - tem2[6]; te[7] = tem1[7] - tem2[7]; te[8] = tem1[8] - tem2[8];
  810. return this;
  811. },
  812. subEqual: function ( m ) {
  813. var te = this.elements, tem = m.elements;
  814. te[0] -= tem[0]; te[1] -= tem[1]; te[2] -= tem[2];
  815. te[3] -= tem[3]; te[4] -= tem[4]; te[5] -= tem[5];
  816. te[6] -= tem[6]; te[7] -= tem[7]; te[8] -= tem[8];
  817. return this;
  818. },
  819. scale: function ( m, s ) {
  820. var te = this.elements, tm = m.elements;
  821. te[0] = tm[0] * s; te[1] = tm[1] * s; te[2] = tm[2] * s;
  822. te[3] = tm[3] * s; te[4] = tm[4] * s; te[5] = tm[5] * s;
  823. te[6] = tm[6] * s; te[7] = tm[7] * s; te[8] = tm[8] * s;
  824. return this;
  825. },
  826. scaleEqual: function ( s ){// multiplyScalar
  827. var te = this.elements;
  828. te[0] *= s; te[1] *= s; te[2] *= s;
  829. te[3] *= s; te[4] *= s; te[5] *= s;
  830. te[6] *= s; te[7] *= s; te[8] *= s;
  831. return this;
  832. },
  833. multiplyMatrices: function ( m1, m2, transpose ) {
  834. if( transpose ) m2 = m2.clone().transpose();
  835. var te = this.elements;
  836. var tm1 = m1.elements;
  837. var tm2 = m2.elements;
  838. var a0 = tm1[0], a3 = tm1[3], a6 = tm1[6];
  839. var a1 = tm1[1], a4 = tm1[4], a7 = tm1[7];
  840. var a2 = tm1[2], a5 = tm1[5], a8 = tm1[8];
  841. var b0 = tm2[0], b3 = tm2[3], b6 = tm2[6];
  842. var b1 = tm2[1], b4 = tm2[4], b7 = tm2[7];
  843. var b2 = tm2[2], b5 = tm2[5], b8 = tm2[8];
  844. te[0] = a0*b0 + a1*b3 + a2*b6;
  845. te[1] = a0*b1 + a1*b4 + a2*b7;
  846. te[2] = a0*b2 + a1*b5 + a2*b8;
  847. te[3] = a3*b0 + a4*b3 + a5*b6;
  848. te[4] = a3*b1 + a4*b4 + a5*b7;
  849. te[5] = a3*b2 + a4*b5 + a5*b8;
  850. te[6] = a6*b0 + a7*b3 + a8*b6;
  851. te[7] = a6*b1 + a7*b4 + a8*b7;
  852. te[8] = a6*b2 + a7*b5 + a8*b8;
  853. return this;
  854. },
  855. /*mul: function ( m1, m2, transpose ) {
  856. if( transpose ) m2 = m2.clone().transpose();
  857. var te = this.elements;
  858. var tm1 = m1.elements;
  859. var tm2 = m2.elements;
  860. //var tmp;
  861. var a0 = tm1[0], a3 = tm1[3], a6 = tm1[6];
  862. var a1 = tm1[1], a4 = tm1[4], a7 = tm1[7];
  863. var a2 = tm1[2], a5 = tm1[5], a8 = tm1[8];
  864. var b0 = tm2[0], b3 = tm2[3], b6 = tm2[6];
  865. var b1 = tm2[1], b4 = tm2[4], b7 = tm2[7];
  866. var b2 = tm2[2], b5 = tm2[5], b8 = tm2[8];
  867. /*if( transpose ){
  868. tmp = b1; b1 = b3; b3 = tmp;
  869. tmp = b2; b2 = b6; b6 = tmp;
  870. tmp = b5; b5 = b7; b7 = tmp;
  871. }
  872. te[0] = a0*b0 + a1*b3 + a2*b6;
  873. te[1] = a0*b1 + a1*b4 + a2*b7;
  874. te[2] = a0*b2 + a1*b5 + a2*b8;
  875. te[3] = a3*b0 + a4*b3 + a5*b6;
  876. te[4] = a3*b1 + a4*b4 + a5*b7;
  877. te[5] = a3*b2 + a4*b5 + a5*b8;
  878. te[6] = a6*b0 + a7*b3 + a8*b6;
  879. te[7] = a6*b1 + a7*b4 + a8*b7;
  880. te[8] = a6*b2 + a7*b5 + a8*b8;
  881. return this;
  882. },*/
  883. transpose: function ( m ) {
  884. if( m !== undefined ){
  885. var a = m.elements;
  886. this.set( a[0], a[3], a[6], a[1], a[4], a[7], a[2], a[5], a[8] );
  887. return this;
  888. }
  889. var te = this.elements;
  890. var a01 = te[1], a02 = te[2], a12 = te[5];
  891. te[1] = te[3];
  892. te[2] = te[6];
  893. te[3] = a01;
  894. te[5] = te[7];
  895. te[6] = a02;
  896. te[7] = a12;
  897. return this;
  898. },
  899. /*mulScale: function ( m, sx, sy, sz, Prepend ) {
  900. var prepend = Prepend || false;
  901. var te = this.elements, tm = m.elements;
  902. if(prepend){
  903. te[0] = sx*tm[0]; te[1] = sx*tm[1]; te[2] = sx*tm[2];
  904. te[3] = sy*tm[3]; te[4] = sy*tm[4]; te[5] = sy*tm[5];
  905. te[6] = sz*tm[6]; te[7] = sz*tm[7]; te[8] = sz*tm[8];
  906. }else{
  907. te[0] = tm[0]*sx; te[1] = tm[1]*sy; te[2] = tm[2]*sz;
  908. te[3] = tm[3]*sx; te[4] = tm[4]*sy; te[5] = tm[5]*sz;
  909. te[6] = tm[6]*sx; te[7] = tm[7]*sy; te[8] = tm[8]*sz;
  910. }
  911. return this;
  912. },
  913. transpose: function ( m ) {
  914. var te = this.elements, tm = m.elements;
  915. te[0] = tm[0]; te[1] = tm[3]; te[2] = tm[6];
  916. te[3] = tm[1]; te[4] = tm[4]; te[5] = tm[7];
  917. te[6] = tm[2]; te[7] = tm[5]; te[8] = tm[8];
  918. return this;
  919. },*/
  920. setQuat: function ( q ) {
  921. var te = this.elements;
  922. var x = q.x, y = q.y, z = q.z, w = q.w;
  923. var x2 = x + x, y2 = y + y, z2 = z + z;
  924. var xx = x * x2, xy = x * y2, xz = x * z2;
  925. var yy = y * y2, yz = y * z2, zz = z * z2;
  926. var wx = w * x2, wy = w * y2, wz = w * z2;
  927. te[0] = 1 - ( yy + zz );
  928. te[1] = xy - wz;
  929. te[2] = xz + wy;
  930. te[3] = xy + wz;
  931. te[4] = 1 - ( xx + zz );
  932. te[5] = yz - wx;
  933. te[6] = xz - wy;
  934. te[7] = yz + wx;
  935. te[8] = 1 - ( xx + yy );
  936. return this;
  937. },
  938. invert: function( m ) {
  939. var te = this.elements, tm = m.elements,
  940. a00 = tm[0], a10 = tm[3], a20 = tm[6],
  941. a01 = tm[1], a11 = tm[4], a21 = tm[7],
  942. a02 = tm[2], a12 = tm[5], a22 = tm[8],
  943. b01 = a22 * a11 - a12 * a21,
  944. b11 = -a22 * a10 + a12 * a20,
  945. b21 = a21 * a10 - a11 * a20,
  946. det = a00 * b01 + a01 * b11 + a02 * b21;
  947. if ( det === 0 ) {
  948. console.log( "can't invert matrix, determinant is 0");
  949. return this.identity();
  950. }
  951. det = 1.0 / det;
  952. te[0] = b01 * det;
  953. te[1] = (-a22 * a01 + a02 * a21) * det;
  954. te[2] = (a12 * a01 - a02 * a11) * det;
  955. te[3] = b11 * det;
  956. te[4] = (a22 * a00 - a02 * a20) * det;
  957. te[5] = (-a12 * a00 + a02 * a10) * det;
  958. te[6] = b21 * det;
  959. te[7] = (-a21 * a00 + a01 * a20) * det;
  960. te[8] = (a11 * a00 - a01 * a10) * det;
  961. return this;
  962. },
  963. addOffset: function ( m, v ) {
  964. var relX = v.x;
  965. var relY = v.y;
  966. var relZ = v.z;
  967. var te = this.elements;
  968. te[0] += m * ( relY * relY + relZ * relZ );
  969. te[4] += m * ( relX * relX + relZ * relZ );
  970. te[8] += m * ( relX * relX + relY * relY );
  971. var xy = m * relX * relY;
  972. var yz = m * relY * relZ;
  973. var zx = m * relZ * relX;
  974. te[1] -= xy;
  975. te[3] -= xy;
  976. te[2] -= yz;
  977. te[6] -= yz;
  978. te[5] -= zx;
  979. te[7] -= zx;
  980. return this;
  981. },
  982. subOffset: function ( m, v ) {
  983. var relX = v.x;
  984. var relY = v.y;
  985. var relZ = v.z;
  986. var te = this.elements;
  987. te[0] -= m * ( relY * relY + relZ * relZ );
  988. te[4] -= m * ( relX * relX + relZ * relZ );
  989. te[8] -= m * ( relX * relX + relY * relY );
  990. var xy = m * relX * relY;
  991. var yz = m * relY * relZ;
  992. var zx = m * relZ * relX;
  993. te[1] += xy;
  994. te[3] += xy;
  995. te[2] += yz;
  996. te[6] += yz;
  997. te[5] += zx;
  998. te[7] += zx;
  999. return this;
  1000. },
  1001. // OK
  1002. multiplyScalar: function ( s ) {
  1003. var te = this.elements;
  1004. te[ 0 ] *= s; te[ 3 ] *= s; te[ 6 ] *= s;
  1005. te[ 1 ] *= s; te[ 4 ] *= s; te[ 7 ] *= s;
  1006. te[ 2 ] *= s; te[ 5 ] *= s; te[ 8 ] *= s;
  1007. return this;
  1008. },
  1009. identity: function () {
  1010. this.set( 1, 0, 0, 0, 1, 0, 0, 0, 1 );
  1011. return this;
  1012. },
  1013. clone: function () {
  1014. return new Mat33().fromArray( this.elements );
  1015. },
  1016. copy: function ( m ) {
  1017. for ( var i = 0; i < 9; i ++ ) this.elements[ i ] = m.elements[ i ];
  1018. return this;
  1019. },
  1020. determinant: function () {
  1021. var te = this.elements;
  1022. var a = te[ 0 ], b = te[ 1 ], c = te[ 2 ],
  1023. d = te[ 3 ], e = te[ 4 ], f = te[ 5 ],
  1024. g = te[ 6 ], h = te[ 7 ], i = te[ 8 ];
  1025. return a * e * i - a * f * h - b * d * i + b * f * g + c * d * h - c * e * g;
  1026. },
  1027. fromArray: function ( array, offset ) {
  1028. if ( offset === undefined ) offset = 0;
  1029. for( var i = 0; i < 9; i ++ ) {
  1030. this.elements[ i ] = array[ i + offset ];
  1031. }
  1032. return this;
  1033. },
  1034. toArray: function ( array, offset ) {
  1035. if ( array === undefined ) array = [];
  1036. if ( offset === undefined ) offset = 0;
  1037. var te = this.elements;
  1038. array[ offset ] = te[ 0 ];
  1039. array[ offset + 1 ] = te[ 1 ];
  1040. array[ offset + 2 ] = te[ 2 ];
  1041. array[ offset + 3 ] = te[ 3 ];
  1042. array[ offset + 4 ] = te[ 4 ];
  1043. array[ offset + 5 ] = te[ 5 ];
  1044. array[ offset + 6 ] = te[ 6 ];
  1045. array[ offset + 7 ] = te[ 7 ];
  1046. array[ offset + 8 ] = te[ 8 ];
  1047. return array;
  1048. }
  1049. } );
  1050. /**
  1051. * An axis-aligned bounding box.
  1052. *
  1053. * @author saharan
  1054. * @author lo-th
  1055. */
  1056. function AABB( minX, maxX, minY, maxY, minZ, maxZ ){
  1057. this.elements = new Float32Array( 6 );
  1058. var te = this.elements;
  1059. te[0] = minX || 0; te[1] = minY || 0; te[2] = minZ || 0;
  1060. te[3] = maxX || 0; te[4] = maxY || 0; te[5] = maxZ || 0;
  1061. }
  1062. Object.assign( AABB.prototype, {
  1063. AABB: true,
  1064. set: function(minX, maxX, minY, maxY, minZ, maxZ){
  1065. var te = this.elements;
  1066. te[0] = minX;
  1067. te[3] = maxX;
  1068. te[1] = minY;
  1069. te[4] = maxY;
  1070. te[2] = minZ;
  1071. te[5] = maxZ;
  1072. return this;
  1073. },
  1074. intersectTest: function ( aabb ) {
  1075. var te = this.elements;
  1076. var ue = aabb.elements;
  1077. return te[0] > ue[3] || te[1] > ue[4] || te[2] > ue[5] || te[3] < ue[0] || te[4] < ue[1] || te[5] < ue[2] ? true : false;
  1078. },
  1079. intersectTestTwo: function ( aabb ) {
  1080. var te = this.elements;
  1081. var ue = aabb.elements;
  1082. return te[0] < ue[0] || te[1] < ue[1] || te[2] < ue[2] || te[3] > ue[3] || te[4] > ue[4] || te[5] > ue[5] ? true : false;
  1083. },
  1084. clone: function () {
  1085. return new this.constructor().fromArray( this.elements );
  1086. },
  1087. copy: function ( aabb, margin ) {
  1088. var m = margin || 0;
  1089. var me = aabb.elements;
  1090. this.set( me[ 0 ]-m, me[ 3 ]+m, me[ 1 ]-m, me[ 4 ]+m, me[ 2 ]-m, me[ 5 ]+m );
  1091. return this;
  1092. },
  1093. fromArray: function ( array ) {
  1094. this.elements.set( array );
  1095. return this;
  1096. },
  1097. // Set this AABB to the combined AABB of aabb1 and aabb2.
  1098. combine: function( aabb1, aabb2 ) {
  1099. var a = aabb1.elements;
  1100. var b = aabb2.elements;
  1101. var te = this.elements;
  1102. te[0] = a[0] < b[0] ? a[0] : b[0];
  1103. te[1] = a[1] < b[1] ? a[1] : b[1];
  1104. te[2] = a[2] < b[2] ? a[2] : b[2];
  1105. te[3] = a[3] > b[3] ? a[3] : b[3];
  1106. te[4] = a[4] > b[4] ? a[4] : b[4];
  1107. te[5] = a[5] > b[5] ? a[5] : b[5];
  1108. return this;
  1109. },
  1110. // Get the surface area.
  1111. surfaceArea: function () {
  1112. var te = this.elements;
  1113. var a = te[3] - te[0];
  1114. var h = te[4] - te[1];
  1115. var d = te[5] - te[2];
  1116. return 2 * (a * (h + d) + h * d );
  1117. },
  1118. // Get whether the AABB intersects with the point or not.
  1119. intersectsWithPoint:function(x,y,z){
  1120. var te = this.elements;
  1121. return x>=te[0] && x<=te[3] && y>=te[1] && y<=te[4] && z>=te[2] && z<=te[5];
  1122. },
  1123. /**
  1124. * Set the AABB from an array
  1125. * of vertices. From THREE.
  1126. * @author WestLangley
  1127. * @author xprogram
  1128. */
  1129. setFromPoints: function(arr){
  1130. this.makeEmpty();
  1131. for(var i = 0; i < arr.length; i++){
  1132. this.expandByPoint(arr[i]);
  1133. }
  1134. },
  1135. makeEmpty: function(){
  1136. this.set(-Infinity, -Infinity, -Infinity, Infinity, Infinity, Infinity);
  1137. },
  1138. expandByPoint: function(pt){
  1139. var te = this.elements;
  1140. this.set(
  1141. _Math.min(te[ 0 ], pt.x), _Math.min(te[ 1 ], pt.y), _Math.min(te[ 2 ], pt.z),
  1142. _Math.max(te[ 3 ], pt.x), _Math.max(te[ 4 ], pt.y), _Math.max(te[ 5 ], pt.z)
  1143. );
  1144. },
  1145. expandByScalar: function(s){
  1146. var te = this.elements;
  1147. te[0] += -s;
  1148. te[1] += -s;
  1149. te[2] += -s;
  1150. te[3] += s;
  1151. te[4] += s;
  1152. te[5] += s;
  1153. }
  1154. });
  1155. var count = 0;
  1156. function ShapeIdCount() { return count++; }
  1157. /**
  1158. * A shape is used to detect collisions of rigid bodies.
  1159. *
  1160. * @author saharan
  1161. * @author lo-th
  1162. */
  1163. function Shape ( config ) {
  1164. this.type = SHAPE_NULL;
  1165. // global identification of the shape should be unique to the shape.
  1166. this.id = ShapeIdCount();
  1167. // previous shape in parent rigid body. Used for fast interations.
  1168. this.prev = null;
  1169. // next shape in parent rigid body. Used for fast interations.
  1170. this.next = null;
  1171. // proxy of the shape used for broad-phase collision detection.
  1172. this.proxy = null;
  1173. // parent rigid body of the shape.
  1174. this.parent = null;
  1175. // linked list of the contacts with the shape.
  1176. this.contactLink = null;
  1177. // number of the contacts with the shape.
  1178. this.numContacts = 0;
  1179. // center of gravity of the shape in world coordinate system.
  1180. this.position = new Vec3();
  1181. // rotation matrix of the shape in world coordinate system.
  1182. this.rotation = new Mat33();
  1183. // position of the shape in parent's coordinate system.
  1184. this.relativePosition = new Vec3().copy( config.relativePosition );
  1185. // rotation matrix of the shape in parent's coordinate system.
  1186. this.relativeRotation = new Mat33().copy( config.relativeRotation );
  1187. // axis-aligned bounding box of the shape.
  1188. this.aabb = new AABB();
  1189. // density of the shape.
  1190. this.density = config.density;
  1191. // coefficient of friction of the shape.
  1192. this.friction = config.friction;
  1193. // coefficient of restitution of the shape.
  1194. this.restitution = config.restitution;
  1195. // bits of the collision groups to which the shape belongs.
  1196. this.belongsTo = config.belongsTo;
  1197. // bits of the collision groups with which the shape collides.
  1198. this.collidesWith = config.collidesWith;
  1199. }
  1200. Object.assign( Shape.prototype, {
  1201. Shape: true,
  1202. // Calculate the mass information of the shape.
  1203. calculateMassInfo: function( out ){
  1204. printError("Shape", "Inheritance error.");
  1205. },
  1206. // Update the proxy of the shape.
  1207. updateProxy: function(){
  1208. printError("Shape", "Inheritance error.");
  1209. }
  1210. });
  1211. /**
  1212. * Box shape.
  1213. * @author saharan
  1214. * @author lo-th
  1215. */
  1216. function Box ( config, Width, Height, Depth ) {
  1217. Shape.call( this, config );
  1218. this.type = SHAPE_BOX;
  1219. this.width = Width;
  1220. this.height = Height;
  1221. this.depth = Depth;
  1222. this.halfWidth = Width * 0.5;
  1223. this.halfHeight = Height * 0.5;
  1224. this.halfDepth = Depth * 0.5;
  1225. this.dimentions = new Float32Array( 18 );
  1226. this.elements = new Float32Array( 24 );
  1227. }
  1228. Box.prototype = Object.assign( Object.create( Shape.prototype ), {
  1229. constructor: Box,
  1230. calculateMassInfo: function ( out ) {
  1231. var mass = this.width * this.height * this.depth * this.density;
  1232. var divid = 1/12;
  1233. out.mass = mass;
  1234. out.inertia.set(
  1235. mass * ( this.height * this.height + this.depth * this.depth ) * divid, 0, 0,
  1236. 0, mass * ( this.width * this.width + this.depth * this.depth ) * divid, 0,
  1237. 0, 0, mass * ( this.width * this.width + this.height * this.height ) * divid
  1238. );
  1239. },
  1240. updateProxy: function () {
  1241. var te = this.rotation.elements;
  1242. var di = this.dimentions;
  1243. // Width
  1244. di[0] = te[0];
  1245. di[1] = te[3];
  1246. di[2] = te[6];
  1247. // Height
  1248. di[3] = te[1];
  1249. di[4] = te[4];
  1250. di[5] = te[7];
  1251. // Depth
  1252. di[6] = te[2];
  1253. di[7] = te[5];
  1254. di[8] = te[8];
  1255. // half Width
  1256. di[9] = te[0] * this.halfWidth;
  1257. di[10] = te[3] * this.halfWidth;
  1258. di[11] = te[6] * this.halfWidth;
  1259. // half Height
  1260. di[12] = te[1] * this.halfHeight;
  1261. di[13] = te[4] * this.halfHeight;
  1262. di[14] = te[7] * this.halfHeight;
  1263. // half Depth
  1264. di[15] = te[2] * this.halfDepth;
  1265. di[16] = te[5] * this.halfDepth;
  1266. di[17] = te[8] * this.halfDepth;
  1267. var wx = di[9];
  1268. var wy = di[10];
  1269. var wz = di[11];
  1270. var hx = di[12];
  1271. var hy = di[13];
  1272. var hz = di[14];
  1273. var dx = di[15];
  1274. var dy = di[16];
  1275. var dz = di[17];
  1276. var x = this.position.x;
  1277. var y = this.position.y;
  1278. var z = this.position.z;
  1279. var v = this.elements;
  1280. //v1
  1281. v[0] = x + wx + hx + dx;
  1282. v[1] = y + wy + hy + dy;
  1283. v[2] = z + wz + hz + dz;
  1284. //v2
  1285. v[3] = x + wx + hx - dx;
  1286. v[4] = y + wy + hy - dy;
  1287. v[5] = z + wz + hz - dz;
  1288. //v3
  1289. v[6] = x + wx - hx + dx;
  1290. v[7] = y + wy - hy + dy;
  1291. v[8] = z + wz - hz + dz;
  1292. //v4
  1293. v[9] = x + wx - hx - dx;
  1294. v[10] = y + wy - hy - dy;
  1295. v[11] = z + wz - hz - dz;
  1296. //v5
  1297. v[12] = x - wx + hx + dx;
  1298. v[13] = y - wy + hy + dy;
  1299. v[14] = z - wz + hz + dz;
  1300. //v6
  1301. v[15] = x - wx + hx - dx;
  1302. v[16] = y - wy + hy - dy;
  1303. v[17] = z - wz + hz - dz;
  1304. //v7
  1305. v[18] = x - wx - hx + dx;
  1306. v[19] = y - wy - hy + dy;
  1307. v[20] = z - wz - hz + dz;
  1308. //v8
  1309. v[21] = x - wx - hx - dx;
  1310. v[22] = y - wy - hy - dy;
  1311. v[23] = z - wz - hz - dz;
  1312. var w = di[9] < 0 ? -di[9] : di[9];
  1313. var h = di[10] < 0 ? -di[10] : di[10];
  1314. var d = di[11] < 0 ? -di[11] : di[11];
  1315. w = di[12] < 0 ? w - di[12] : w + di[12];
  1316. h = di[13] < 0 ? h - di[13] : h + di[13];
  1317. d = di[14] < 0 ? d - di[14] : d + di[14];
  1318. w = di[15] < 0 ? w - di[15] : w + di[15];
  1319. h = di[16] < 0 ? h - di[16] : h + di[16];
  1320. d = di[17] < 0 ? d - di[17] : d + di[17];
  1321. var p = AABB_PROX;
  1322. this.aabb.set(
  1323. this.position.x - w - p, this.position.x + w + p,
  1324. this.position.y - h - p, this.position.y + h + p,
  1325. this.position.z - d - p, this.position.z + d + p
  1326. );
  1327. if ( this.proxy != null ) this.proxy.update();
  1328. }
  1329. });
  1330. /**
  1331. * Sphere shape
  1332. * @author saharan
  1333. * @author lo-th
  1334. */
  1335. function Sphere( config, radius ) {
  1336. Shape.call( this, config );
  1337. this.type = SHAPE_SPHERE;
  1338. // radius of the shape.
  1339. this.radius = radius;
  1340. }
  1341. Sphere.prototype = Object.assign( Object.create( Shape.prototype ), {
  1342. constructor: Sphere,
  1343. volume: function () {
  1344. return _Math.PI * this.radius * 1.333333;
  1345. },
  1346. calculateMassInfo: function ( out ) {
  1347. var mass = this.volume() * this.radius * this.radius * this.density; //1.333 * _Math.PI * this.radius * this.radius * this.radius * this.density;
  1348. out.mass = mass;
  1349. var inertia = mass * this.radius * this.radius * 0.4;
  1350. out.inertia.set( inertia, 0, 0, 0, inertia, 0, 0, 0, inertia );
  1351. },
  1352. updateProxy: function () {
  1353. var p = AABB_PROX;
  1354. this.aabb.set(
  1355. this.position.x - this.radius - p, this.position.x + this.radius + p,
  1356. this.position.y - this.radius - p, this.position.y + this.radius + p,
  1357. this.position.z - this.radius - p, this.position.z + this.radius + p
  1358. );
  1359. if ( this.proxy != null ) this.proxy.update();
  1360. }
  1361. });
  1362. /**
  1363. * Cylinder shape
  1364. * @author saharan
  1365. * @author lo-th
  1366. */
  1367. function Cylinder ( config, radius, height ) {
  1368. Shape.call( this, config );
  1369. this.type = SHAPE_CYLINDER;
  1370. this.radius = radius;
  1371. this.height = height;
  1372. this.halfHeight = height * 0.5;
  1373. this.normalDirection = new Vec3();
  1374. this.halfDirection = new Vec3();
  1375. }
  1376. Cylinder.prototype = Object.assign( Object.create( Shape.prototype ), {
  1377. constructor: Cylinder,
  1378. calculateMassInfo: function ( out ) {
  1379. var rsq = this.radius * this.radius;
  1380. var mass = _Math.PI * rsq * this.height * this.density;
  1381. var inertiaXZ = ( ( 0.25 * rsq ) + ( 0.0833 * this.height * this.height ) ) * mass;
  1382. var inertiaY = 0.5 * rsq;
  1383. out.mass = mass;
  1384. out.inertia.set( inertiaXZ, 0, 0, 0, inertiaY, 0, 0, 0, inertiaXZ );
  1385. },
  1386. updateProxy: function () {
  1387. var te = this.rotation.elements;
  1388. var len, wx, hy, dz, xx, yy, zz, w, h, d, p;
  1389. xx = te[1] * te[1];
  1390. yy = te[4] * te[4];
  1391. zz = te[7] * te[7];
  1392. this.normalDirection.set( te[1], te[4], te[7] );
  1393. this.halfDirection.scale( this.normalDirection, this.halfHeight );
  1394. wx = 1 - xx;
  1395. len = _Math.sqrt(wx*wx + xx*yy + xx*zz);
  1396. if(len>0) len = this.radius/len;
  1397. wx *= len;
  1398. hy = 1 - yy;
  1399. len = _Math.sqrt(yy*xx + hy*hy + yy*zz);
  1400. if(len>0) len = this.radius/len;
  1401. hy *= len;
  1402. dz = 1 - zz;
  1403. len = _Math.sqrt(zz*xx + zz*yy + dz*dz);
  1404. if(len>0) len = this.radius/len;
  1405. dz *= len;
  1406. w = this.halfDirection.x < 0 ? -this.halfDirection.x : this.halfDirection.x;
  1407. h = this.halfDirection.y < 0 ? -this.halfDirection.y : this.halfDirection.y;
  1408. d = this.halfDirection.z < 0 ? -this.halfDirection.z : this.halfDirection.z;
  1409. w = wx < 0 ? w - wx : w + wx;
  1410. h = hy < 0 ? h - hy : h + hy;
  1411. d = dz < 0 ? d - dz : d + dz;
  1412. p = AABB_PROX;
  1413. this.aabb.set(
  1414. this.position.x - w - p, this.position.x + w + p,
  1415. this.position.y - h - p, this.position.y + h + p,
  1416. this.position.z - d - p, this.position.z + d + p
  1417. );
  1418. if ( this.proxy != null ) this.proxy.update();
  1419. }
  1420. });
  1421. /**
  1422. * Plane shape.
  1423. * @author lo-th
  1424. */
  1425. function Plane( config, normal ) {
  1426. Shape.call( this, config );
  1427. this.type = SHAPE_PLANE;
  1428. // radius of the shape.
  1429. this.normal = new Vec3( 0, 1, 0 );
  1430. }
  1431. Plane.prototype = Object.assign( Object.create( Shape.prototype ), {
  1432. constructor: Plane,
  1433. volume: function () {
  1434. return Number.MAX_VALUE;
  1435. },
  1436. calculateMassInfo: function ( out ) {
  1437. out.mass = this.density;//0.0001;
  1438. var inertia = 1;
  1439. out.inertia.set( inertia, 0, 0, 0, inertia, 0, 0, 0, inertia );
  1440. },
  1441. updateProxy: function () {
  1442. var p = AABB_PROX;
  1443. var min = -_Math.INF;
  1444. var max = _Math.INF;
  1445. var n = this.normal;
  1446. // The plane AABB is infinite, except if the normal is pointing along any axis
  1447. this.aabb.set(
  1448. n.x === -1 ? this.position.x - p : min, n.x === 1 ? this.position.x + p : max,
  1449. n.y === -1 ? this.position.y - p : min, n.y === 1 ? this.position.y + p : max,
  1450. n.z === -1 ? this.position.z - p : min, n.z === 1 ? this.position.z + p : max
  1451. );
  1452. if ( this.proxy != null ) this.proxy.update();
  1453. }
  1454. });
  1455. /**
  1456. * A Particule shape
  1457. * @author lo-th
  1458. */
  1459. function Particle( config, normal ) {
  1460. Shape.call( this, config );
  1461. this.type = SHAPE_PARTICLE;
  1462. }
  1463. Particle.prototype = Object.assign( Object.create( Shape.prototype ), {
  1464. constructor: Particle,
  1465. volume: function () {
  1466. return Number.MAX_VALUE;
  1467. },
  1468. calculateMassInfo: function ( out ) {
  1469. var inertia = 0;
  1470. out.inertia.set( inertia, 0, 0, 0, inertia, 0, 0, 0, inertia );
  1471. },
  1472. updateProxy: function () {
  1473. var p = 0;//AABB_PROX;
  1474. this.aabb.set(
  1475. this.position.x - p, this.position.x + p,
  1476. this.position.y - p, this.position.y + p,
  1477. this.position.z - p, this.position.z + p
  1478. );
  1479. if ( this.proxy != null ) this.proxy.update();
  1480. }
  1481. });
  1482. /**
  1483. * A shape configuration holds common configuration data for constructing a shape.
  1484. * These configurations can be reused safely.
  1485. *
  1486. * @author saharan
  1487. * @author lo-th
  1488. */
  1489. function ShapeConfig(){
  1490. // position of the shape in parent's coordinate system.
  1491. this.relativePosition = new Vec3();
  1492. // rotation matrix of the shape in parent's coordinate system.
  1493. this.relativeRotation = new Mat33();
  1494. // coefficient of friction of the shape.
  1495. this.friction = 0.2; // 0.4
  1496. // coefficient of restitution of the shape.
  1497. this.restitution = 0.2;
  1498. // density of the shape.
  1499. this.density = 1;
  1500. // bits of the collision groups to which the shape belongs.
  1501. this.belongsTo = 1;
  1502. // bits of the collision groups with which the shape collides.
  1503. this.collidesWith = 0xffffffff;
  1504. }
  1505. /**
  1506. * An information of limit and motor.
  1507. *
  1508. * @author saharan
  1509. */
  1510. function LimitMotor ( axis, fixed ) {
  1511. fixed = fixed || false;
  1512. // The axis of the constraint.
  1513. this.axis = axis;
  1514. // The current angle for rotational constraints.
  1515. this.angle = 0;
  1516. // The lower limit. Set lower > upper to disable
  1517. this.lowerLimit = fixed ? 0 : 1;
  1518. // The upper limit. Set lower > upper to disable.
  1519. this.upperLimit = 0;
  1520. // The target motor speed.
  1521. this.motorSpeed = 0;
  1522. // The maximum motor force or torque. Set 0 to disable.
  1523. this.maxMotorForce = 0;
  1524. // The frequency of the spring. Set 0 to disable.
  1525. this.frequency = 0;
  1526. // The damping ratio of the spring. Set 0 for no damping, 1 for critical damping.
  1527. this.dampingRatio = 0;
  1528. }
  1529. Object.assign( LimitMotor.prototype, {
  1530. LimitMotor: true,
  1531. // Set limit data into this constraint.
  1532. setLimit:function ( lowerLimit, upperLimit ) {
  1533. this.lowerLimit = lowerLimit;
  1534. this.upperLimit = upperLimit;
  1535. },
  1536. // Set motor data into this constraint.
  1537. setMotor:function ( motorSpeed, maxMotorForce ) {
  1538. this.motorSpeed = motorSpeed;
  1539. this.maxMotorForce = maxMotorForce;
  1540. },
  1541. // Set spring data into this constraint.
  1542. setSpring:function ( frequency, dampingRatio ) {
  1543. this.frequency = frequency;
  1544. this.dampingRatio = dampingRatio;
  1545. }
  1546. });
  1547. /**
  1548. * The base class of all type of the constraints.
  1549. *
  1550. * @author saharan
  1551. * @author lo-th
  1552. */
  1553. function Constraint(){
  1554. // parent world of the constraint.
  1555. this.parent = null;
  1556. // first body of the constraint.
  1557. this.body1 = null;
  1558. // second body of the constraint.
  1559. this.body2 = null;
  1560. // Internal
  1561. this.addedToIsland = false;
  1562. }
  1563. Object.assign( Constraint.prototype, {
  1564. Constraint: true,
  1565. // Prepare for solving the constraint
  1566. preSolve: function( timeStep, invTimeStep ){
  1567. printError("Constraint", "Inheritance error.");
  1568. },
  1569. // Solve the constraint. This is usually called iteratively.
  1570. solve: function(){
  1571. printError("Constraint", "Inheritance error.");
  1572. },
  1573. // Do the post-processing.
  1574. postSolve: function(){
  1575. printError("Constraint", "Inheritance error.");
  1576. }
  1577. });
  1578. function JointLink ( joint ){
  1579. // The previous joint link.
  1580. this.prev = null;
  1581. // The next joint link.
  1582. this.next = null;
  1583. // The other rigid body connected to the joint.
  1584. this.body = null;
  1585. // The joint of the link.
  1586. this.joint = joint;
  1587. }
  1588. /**
  1589. * Joints are used to constrain the motion between two rigid bodies.
  1590. *
  1591. * @author saharan
  1592. * @author lo-th
  1593. */
  1594. function Joint ( config ){
  1595. Constraint.call( this );
  1596. this.scale = 1;
  1597. this.invScale = 1;
  1598. // joint name
  1599. this.name = "";
  1600. this.id = NaN;
  1601. // The type of the joint.
  1602. this.type = JOINT_NULL;
  1603. // The previous joint in the world.
  1604. this.prev = null;
  1605. // The next joint in the world.
  1606. this.next = null;
  1607. this.body1 = config.body1;
  1608. this.body2 = config.body2;
  1609. // anchor point on the first rigid body in local coordinate system.
  1610. this.localAnchorPoint1 = new Vec3().copy( config.localAnchorPoint1 );
  1611. // anchor point on the second rigid body in local coordinate system.
  1612. this.localAnchorPoint2 = new Vec3().copy( config.localAnchorPoint2 );
  1613. // anchor point on the first rigid body in world coordinate system relative to the body's origin.
  1614. this.relativeAnchorPoint1 = new Vec3();
  1615. // anchor point on the second rigid body in world coordinate system relative to the body's origin.
  1616. this.relativeAnchorPoint2 = new Vec3();
  1617. // anchor point on the first rigid body in world coordinate system.
  1618. this.anchorPoint1 = new Vec3();
  1619. // anchor point on the second rigid body in world coordinate system.
  1620. this.anchorPoint2 = new Vec3();
  1621. // Whether allow collision between connected rigid bodies or not.
  1622. this.allowCollision = config.allowCollision;
  1623. this.b1Link = new JointLink( this );
  1624. this.b2Link = new JointLink( this );
  1625. }
  1626. Joint.prototype = Object.assign( Object.create( Constraint.prototype ), {
  1627. constructor: Joint,
  1628. setId: function ( n ) {
  1629. this.id = i;
  1630. },
  1631. setParent: function ( world ) {
  1632. this.parent = world;
  1633. this.scale = this.parent.scale;
  1634. this.invScale = this.parent.invScale;
  1635. this.id = this.parent.numJoints;
  1636. if( !this.name ) this.name = 'J' + this.id;
  1637. },
  1638. // Update all the anchor points.
  1639. updateAnchorPoints: function () {
  1640. this.relativeAnchorPoint1.copy( this.localAnchorPoint1 ).applyMatrix3( this.body1.rotation, true );
  1641. this.relativeAnchorPoint2.copy( this.localAnchorPoint2 ).applyMatrix3( this.body2.rotation, true );
  1642. this.anchorPoint1.add( this.relativeAnchorPoint1, this.body1.position );
  1643. this.anchorPoint2.add( this.relativeAnchorPoint2, this.body2.position );
  1644. },
  1645. // Attach the joint from the bodies.
  1646. attach: function ( isX ) {
  1647. this.b1Link.body = this.body2;
  1648. this.b2Link.body = this.body1;
  1649. if(isX){
  1650. this.body1.jointLink.push( this.b1Link );
  1651. this.body2.jointLink.push( this.b2Link );
  1652. } else {
  1653. if(this.body1.jointLink != null) (this.b1Link.next=this.body1.jointLink).prev = this.b1Link;
  1654. else this.b1Link.next = null;
  1655. this.body1.jointLink = this.b1Link;
  1656. this.body1.numJoints++;
  1657. if(this.body2.jointLink != null) (this.b2Link.next=this.body2.jointLink).prev = this.b2Link;
  1658. else this.b2Link.next = null;
  1659. this.body2.jointLink = this.b2Link;
  1660. this.body2.numJoints++;
  1661. }
  1662. },
  1663. // Detach the joint from the bodies.
  1664. detach: function ( isX ) {
  1665. if( isX ){
  1666. this.body1.jointLink.splice( this.body1.jointLink.indexOf( this.b1Link ), 1 );
  1667. this.body2.jointLink.splice( this.body2.jointLink.indexOf( this.b2Link ), 1 );
  1668. } else {
  1669. var prev = this.b1Link.prev;
  1670. var next = this.b1Link.next;
  1671. if(prev != null) prev.next = next;
  1672. if(next != null) next.prev = prev;
  1673. if(this.body1.jointLink == this.b1Link) this.body1.jointLink = next;
  1674. this.b1Link.prev = null;
  1675. this.b1Link.next = null;
  1676. this.b1Link.body = null;
  1677. this.body1.numJoints--;
  1678. prev = this.b2Link.prev;
  1679. next = this.b2Link.next;
  1680. if(prev != null) prev.next = next;
  1681. if(next != null) next.prev = prev;
  1682. if(this.body2.jointLink==this.b2Link) this.body2.jointLink = next;
  1683. this.b2Link.prev = null;
  1684. this.b2Link.next = null;
  1685. this.b2Link.body = null;
  1686. this.body2.numJoints--;
  1687. }
  1688. this.b1Link.body = null;
  1689. this.b2Link.body = null;
  1690. },
  1691. // Awake the bodies.
  1692. awake: function () {
  1693. this.body1.awake();
  1694. this.body2.awake();
  1695. },
  1696. // calculation function
  1697. preSolve: function ( timeStep, invTimeStep ) {
  1698. },
  1699. solve: function () {
  1700. },
  1701. postSolve: function () {
  1702. },
  1703. // Delete process
  1704. remove: function () {
  1705. this.dispose();
  1706. },
  1707. dispose: function () {
  1708. this.parent.removeJoint( this );
  1709. },
  1710. // Three js add
  1711. getPosition: function () {
  1712. var p1 = new Vec3().scale( this.anchorPoint1, this.scale );
  1713. var p2 = new Vec3().scale( this.anchorPoint2, this.scale );
  1714. return [ p1, p2 ];
  1715. }
  1716. });
  1717. /**
  1718. * A linear constraint for all axes for various joints.
  1719. * @author saharan
  1720. */
  1721. function LinearConstraint ( joint ){
  1722. this.m1=NaN;
  1723. this.m2=NaN;
  1724. this.ii1 = null;
  1725. this.ii2 = null;
  1726. this.dd = null;
  1727. this.r1x=NaN;
  1728. this.r1y=NaN;
  1729. this.r1z=NaN;
  1730. this.r2x=NaN;
  1731. this.r2y=NaN;
  1732. this.r2z=NaN;
  1733. this.ax1x=NaN;
  1734. this.ax1y=NaN;
  1735. this.ax1z=NaN;
  1736. this.ay1x=NaN;
  1737. this.ay1y=NaN;
  1738. this.ay1z=NaN;
  1739. this.az1x=NaN;
  1740. this.az1y=NaN;
  1741. this.az1z=NaN;
  1742. this.ax2x=NaN;
  1743. this.ax2y=NaN;
  1744. this.ax2z=NaN;
  1745. this.ay2x=NaN;
  1746. this.ay2y=NaN;
  1747. this.ay2z=NaN;
  1748. this.az2x=NaN;
  1749. this.az2y=NaN;
  1750. this.az2z=NaN;
  1751. this.vel=NaN;
  1752. this.velx=NaN;
  1753. this.vely=NaN;
  1754. this.velz=NaN;
  1755. this.joint = joint;
  1756. this.r1 = joint.relativeAnchorPoint1;
  1757. this.r2 = joint.relativeAnchorPoint2;
  1758. this.p1 = joint.anchorPoint1;
  1759. this.p2 = joint.anchorPoint2;
  1760. this.b1 = joint.body1;
  1761. this.b2 = joint.body2;
  1762. this.l1 = this.b1.linearVelocity;
  1763. this.l2 = this.b2.linearVelocity;
  1764. this.a1 = this.b1.angularVelocity;
  1765. this.a2 = this.b2.angularVelocity;
  1766. this.i1 = this.b1.inverseInertia;
  1767. this.i2 = this.b2.inverseInertia;
  1768. this.impx = 0;
  1769. this.impy = 0;
  1770. this.impz = 0;
  1771. }
  1772. Object.assign( LinearConstraint.prototype, {
  1773. LinearConstraint: true,
  1774. preSolve: function ( timeStep, invTimeStep ) {
  1775. this.r1x = this.r1.x;
  1776. this.r1y = this.r1.y;
  1777. this.r1z = this.r1.z;
  1778. this.r2x = this.r2.x;
  1779. this.r2y = this.r2.y;
  1780. this.r2z = this.r2.z;
  1781. this.m1 = this.b1.inverseMass;
  1782. this.m2 = this.b2.inverseMass;
  1783. this.ii1 = this.i1.clone();
  1784. this.ii2 = this.i2.clone();
  1785. var ii1 = this.ii1.elements;
  1786. var ii2 = this.ii2.elements;
  1787. this.ax1x = this.r1z*ii1[1]+-this.r1y*ii1[2];
  1788. this.ax1y = this.r1z*ii1[4]+-this.r1y*ii1[5];
  1789. this.ax1z = this.r1z*ii1[7]+-this.r1y*ii1[8];
  1790. this.ay1x = -this.r1z*ii1[0]+this.r1x*ii1[2];
  1791. this.ay1y = -this.r1z*ii1[3]+this.r1x*ii1[5];
  1792. this.ay1z = -this.r1z*ii1[6]+this.r1x*ii1[8];
  1793. this.az1x = this.r1y*ii1[0]+-this.r1x*ii1[1];
  1794. this.az1y = this.r1y*ii1[3]+-this.r1x*ii1[4];
  1795. this.az1z = this.r1y*ii1[6]+-this.r1x*ii1[7];
  1796. this.ax2x = this.r2z*ii2[1]+-this.r2y*ii2[2];
  1797. this.ax2y = this.r2z*ii2[4]+-this.r2y*ii2[5];
  1798. this.ax2z = this.r2z*ii2[7]+-this.r2y*ii2[8];
  1799. this.ay2x = -this.r2z*ii2[0]+this.r2x*ii2[2];
  1800. this.ay2y = -this.r2z*ii2[3]+this.r2x*ii2[5];
  1801. this.ay2z = -this.r2z*ii2[6]+this.r2x*ii2[8];
  1802. this.az2x = this.r2y*ii2[0]+-this.r2x*ii2[1];
  1803. this.az2y = this.r2y*ii2[3]+-this.r2x*ii2[4];
  1804. this.az2z = this.r2y*ii2[6]+-this.r2x*ii2[7];
  1805. // calculate point-to-point mass matrix
  1806. // from impulse equation
  1807. //
  1808. // M = ([/m] - [r^][/I][r^]) ^ -1
  1809. //
  1810. // where
  1811. //
  1812. // [/m] = |1/m, 0, 0|
  1813. // |0, 1/m, 0|
  1814. // |0, 0, 1/m|
  1815. //
  1816. // [r^] = |0, -rz, ry|
  1817. // |rz, 0, -rx|
  1818. // |-ry, rx, 0|
  1819. //
  1820. // [/I] = Inverted moment inertia
  1821. var rxx = this.m1+this.m2;
  1822. var kk = new Mat33().set( rxx, 0, 0, 0, rxx, 0, 0, 0, rxx );
  1823. var k = kk.elements;
  1824. k[0] += ii1[4]*this.r1z*this.r1z-(ii1[7]+ii1[5])*this.r1y*this.r1z+ii1[8]*this.r1y*this.r1y;
  1825. k[1] += (ii1[6]*this.r1y+ii1[5]*this.r1x)*this.r1z-ii1[3]*this.r1z*this.r1z-ii1[8]*this.r1x*this.r1y;
  1826. k[2] += (ii1[3]*this.r1y-ii1[4]*this.r1x)*this.r1z-ii1[6]*this.r1y*this.r1y+ii1[7]*this.r1x*this.r1y;
  1827. k[3] += (ii1[2]*this.r1y+ii1[7]*this.r1x)*this.r1z-ii1[1]*this.r1z*this.r1z-ii1[8]*this.r1x*this.r1y;
  1828. k[4] += ii1[0]*this.r1z*this.r1z-(ii1[6]+ii1[2])*this.r1x*this.r1z+ii1[8]*this.r1x*this.r1x;
  1829. k[5] += (ii1[1]*this.r1x-ii1[0]*this.r1y)*this.r1z-ii1[7]*this.r1x*this.r1x+ii1[6]*this.r1x*this.r1y;
  1830. k[6] += (ii1[1]*this.r1y-ii1[4]*this.r1x)*this.r1z-ii1[2]*this.r1y*this.r1y+ii1[5]*this.r1x*this.r1y;
  1831. k[7] += (ii1[3]*this.r1x-ii1[0]*this.r1y)*this.r1z-ii1[5]*this.r1x*this.r1x+ii1[2]*this.r1x*this.r1y;
  1832. k[8] += ii1[0]*this.r1y*this.r1y-(ii1[3]+ii1[1])*this.r1x*this.r1y+ii1[4]*this.r1x*this.r1x;
  1833. k[0] += ii2[4]*this.r2z*this.r2z-(ii2[7]+ii2[5])*this.r2y*this.r2z+ii2[8]*this.r2y*this.r2y;
  1834. k[1] += (ii2[6]*this.r2y+ii2[5]*this.r2x)*this.r2z-ii2[3]*this.r2z*this.r2z-ii2[8]*this.r2x*this.r2y;
  1835. k[2] += (ii2[3]*this.r2y-ii2[4]*this.r2x)*this.r2z-ii2[6]*this.r2y*this.r2y+ii2[7]*this.r2x*this.r2y;
  1836. k[3] += (ii2[2]*this.r2y+ii2[7]*this.r2x)*this.r2z-ii2[1]*this.r2z*this.r2z-ii2[8]*this.r2x*this.r2y;
  1837. k[4] += ii2[0]*this.r2z*this.r2z-(ii2[6]+ii2[2])*this.r2x*this.r2z+ii2[8]*this.r2x*this.r2x;
  1838. k[5] += (ii2[1]*this.r2x-ii2[0]*this.r2y)*this.r2z-ii2[7]*this.r2x*this.r2x+ii2[6]*this.r2x*this.r2y;
  1839. k[6] += (ii2[1]*this.r2y-ii2[4]*this.r2x)*this.r2z-ii2[2]*this.r2y*this.r2y+ii2[5]*this.r2x*this.r2y;
  1840. k[7] += (ii2[3]*this.r2x-ii2[0]*this.r2y)*this.r2z-ii2[5]*this.r2x*this.r2x+ii2[2]*this.r2x*this.r2y;
  1841. k[8] += ii2[0]*this.r2y*this.r2y-(ii2[3]+ii2[1])*this.r2x*this.r2y+ii2[4]*this.r2x*this.r2x;
  1842. var inv=1/( k[0]*(k[4]*k[8]-k[7]*k[5]) + k[3]*(k[7]*k[2]-k[1]*k[8]) + k[6]*(k[1]*k[5]-k[4]*k[2]) );
  1843. this.dd = new Mat33().set(
  1844. k[4]*k[8]-k[5]*k[7], k[2]*k[7]-k[1]*k[8], k[1]*k[5]-k[2]*k[4],
  1845. k[5]*k[6]-k[3]*k[8], k[0]*k[8]-k[2]*k[6], k[2]*k[3]-k[0]*k[5],
  1846. k[3]*k[7]-k[4]*k[6], k[1]*k[6]-k[0]*k[7], k[0]*k[4]-k[1]*k[3]
  1847. ).scaleEqual( inv );
  1848. this.velx = this.p2.x-this.p1.x;
  1849. this.vely = this.p2.y-this.p1.y;
  1850. this.velz = this.p2.z-this.p1.z;
  1851. var len = _Math.sqrt(this.velx*this.velx+this.vely*this.vely+this.velz*this.velz);
  1852. if(len>0.005){
  1853. len = (0.005-len)/len*invTimeStep*0.05;
  1854. this.velx *= len;
  1855. this.vely *= len;
  1856. this.velz *= len;
  1857. }else{
  1858. this.velx = 0;
  1859. this.vely = 0;
  1860. this.velz = 0;
  1861. }
  1862. this.impx *= 0.95;
  1863. this.impy *= 0.95;
  1864. this.impz *= 0.95;
  1865. this.l1.x += this.impx*this.m1;
  1866. this.l1.y += this.impy*this.m1;
  1867. this.l1.z += this.impz*this.m1;
  1868. this.a1.x += this.impx*this.ax1x+this.impy*this.ay1x+this.impz*this.az1x;
  1869. this.a1.y += this.impx*this.ax1y+this.impy*this.ay1y+this.impz*this.az1y;
  1870. this.a1.z += this.impx*this.ax1z+this.impy*this.ay1z+this.impz*this.az1z;
  1871. this.l2.x -= this.impx*this.m2;
  1872. this.l2.y -= this.impy*this.m2;
  1873. this.l2.z -= this.impz*this.m2;
  1874. this.a2.x -= this.impx*this.ax2x+this.impy*this.ay2x+this.impz*this.az2x;
  1875. this.a2.y -= this.impx*this.ax2y+this.impy*this.ay2y+this.impz*this.az2y;
  1876. this.a2.z -= this.impx*this.ax2z+this.impy*this.ay2z+this.impz*this.az2z;
  1877. },
  1878. solve: function () {
  1879. var d = this.dd.elements;
  1880. var rvx = this.l2.x-this.l1.x+this.a2.y*this.r2z-this.a2.z*this.r2y-this.a1.y*this.r1z+this.a1.z*this.r1y-this.velx;
  1881. var rvy = this.l2.y-this.l1.y+this.a2.z*this.r2x-this.a2.x*this.r2z-this.a1.z*this.r1x+this.a1.x*this.r1z-this.vely;
  1882. var rvz = this.l2.z-this.l1.z+this.a2.x*this.r2y-this.a2.y*this.r2x-this.a1.x*this.r1y+this.a1.y*this.r1x-this.velz;
  1883. var nimpx = rvx*d[0]+rvy*d[1]+rvz*d[2];
  1884. var nimpy = rvx*d[3]+rvy*d[4]+rvz*d[5];
  1885. var nimpz = rvx*d[6]+rvy*d[7]+rvz*d[8];
  1886. this.impx += nimpx;
  1887. this.impy += nimpy;
  1888. this.impz += nimpz;
  1889. this.l1.x += nimpx*this.m1;
  1890. this.l1.y += nimpy*this.m1;
  1891. this.l1.z += nimpz*this.m1;
  1892. this.a1.x += nimpx*this.ax1x+nimpy*this.ay1x+nimpz*this.az1x;
  1893. this.a1.y += nimpx*this.ax1y+nimpy*this.ay1y+nimpz*this.az1y;
  1894. this.a1.z += nimpx*this.ax1z+nimpy*this.ay1z+nimpz*this.az1z;
  1895. this.l2.x -= nimpx*this.m2;
  1896. this.l2.y -= nimpy*this.m2;
  1897. this.l2.z -= nimpz*this.m2;
  1898. this.a2.x -= nimpx*this.ax2x+nimpy*this.ay2x+nimpz*this.az2x;
  1899. this.a2.y -= nimpx*this.ax2y+nimpy*this.ay2y+nimpz*this.az2y;
  1900. this.a2.z -= nimpx*this.ax2z+nimpy*this.ay2z+nimpz*this.az2z;
  1901. }
  1902. } );
  1903. /**
  1904. * A three-axis rotational constraint for various joints.
  1905. * @author saharan
  1906. */
  1907. function Rotational3Constraint ( joint, limitMotor1, limitMotor2, limitMotor3 ) {
  1908. this.cfm1=NaN;
  1909. this.cfm2=NaN;
  1910. this.cfm3=NaN;
  1911. this.i1e00=NaN;
  1912. this.i1e01=NaN;
  1913. this.i1e02=NaN;
  1914. this.i1e10=NaN;
  1915. this.i1e11=NaN;
  1916. this.i1e12=NaN;
  1917. this.i1e20=NaN;
  1918. this.i1e21=NaN;
  1919. this.i1e22=NaN;
  1920. this.i2e00=NaN;
  1921. this.i2e01=NaN;
  1922. this.i2e02=NaN;
  1923. this.i2e10=NaN;
  1924. this.i2e11=NaN;
  1925. this.i2e12=NaN;
  1926. this.i2e20=NaN;
  1927. this.i2e21=NaN;
  1928. this.i2e22=NaN;
  1929. this.ax1=NaN;
  1930. this.ay1=NaN;
  1931. this.az1=NaN;
  1932. this.ax2=NaN;
  1933. this.ay2=NaN;
  1934. this.az2=NaN;
  1935. this.ax3=NaN;
  1936. this.ay3=NaN;
  1937. this.az3=NaN;
  1938. this.a1x1=NaN; // jacoians
  1939. this.a1y1=NaN;
  1940. this.a1z1=NaN;
  1941. this.a2x1=NaN;
  1942. this.a2y1=NaN;
  1943. this.a2z1=NaN;
  1944. this.a1x2=NaN;
  1945. this.a1y2=NaN;
  1946. this.a1z2=NaN;
  1947. this.a2x2=NaN;
  1948. this.a2y2=NaN;
  1949. this.a2z2=NaN;
  1950. this.a1x3=NaN;
  1951. this.a1y3=NaN;
  1952. this.a1z3=NaN;
  1953. this.a2x3=NaN;
  1954. this.a2y3=NaN;
  1955. this.a2z3=NaN;
  1956. this.lowerLimit1=NaN;
  1957. this.upperLimit1=NaN;
  1958. this.limitVelocity1=NaN;
  1959. this.limitState1=0; // -1: at lower, 0: locked, 1: at upper, 2: free
  1960. this.enableMotor1=false;
  1961. this.motorSpeed1=NaN;
  1962. this.maxMotorForce1=NaN;
  1963. this.maxMotorImpulse1=NaN;
  1964. this.lowerLimit2=NaN;
  1965. this.upperLimit2=NaN;
  1966. this.limitVelocity2=NaN;
  1967. this.limitState2=0; // -1: at lower, 0: locked, 1: at upper, 2: free
  1968. this.enableMotor2=false;
  1969. this.motorSpeed2=NaN;
  1970. this.maxMotorForce2=NaN;
  1971. this.maxMotorImpulse2=NaN;
  1972. this.lowerLimit3=NaN;
  1973. this.upperLimit3=NaN;
  1974. this.limitVelocity3=NaN;
  1975. this.limitState3=0; // -1: at lower, 0: locked, 1: at upper, 2: free
  1976. this.enableMotor3=false;
  1977. this.motorSpeed3=NaN;
  1978. this.maxMotorForce3=NaN;
  1979. this.maxMotorImpulse3=NaN;
  1980. this.k00=NaN; // K = J*M*JT
  1981. this.k01=NaN;
  1982. this.k02=NaN;
  1983. this.k10=NaN;
  1984. this.k11=NaN;
  1985. this.k12=NaN;
  1986. this.k20=NaN;
  1987. this.k21=NaN;
  1988. this.k22=NaN;
  1989. this.kv00=NaN; // diagonals without CFMs
  1990. this.kv11=NaN;
  1991. this.kv22=NaN;
  1992. this.dv00=NaN; // ...inverted
  1993. this.dv11=NaN;
  1994. this.dv22=NaN;
  1995. this.d00=NaN; // K^-1
  1996. this.d01=NaN;
  1997. this.d02=NaN;
  1998. this.d10=NaN;
  1999. this.d11=NaN;
  2000. this.d12=NaN;
  2001. this.d20=NaN;
  2002. this.d21=NaN;
  2003. this.d22=NaN;
  2004. this.limitMotor1=limitMotor1;
  2005. this.limitMotor2=limitMotor2;
  2006. this.limitMotor3=limitMotor3;
  2007. this.b1=joint.body1;
  2008. this.b2=joint.body2;
  2009. this.a1=this.b1.angularVelocity;
  2010. this.a2=this.b2.angularVelocity;
  2011. this.i1=this.b1.inverseInertia;
  2012. this.i2=this.b2.inverseInertia;
  2013. this.limitImpulse1=0;
  2014. this.motorImpulse1=0;
  2015. this.limitImpulse2=0;
  2016. this.motorImpulse2=0;
  2017. this.limitImpulse3=0;
  2018. this.motorImpulse3=0;
  2019. }
  2020. Object.assign( Rotational3Constraint.prototype, {
  2021. Rotational3Constraint: true,
  2022. preSolve: function( timeStep, invTimeStep ){
  2023. this.ax1=this.limitMotor1.axis.x;
  2024. this.ay1=this.limitMotor1.axis.y;
  2025. this.az1=this.limitMotor1.axis.z;
  2026. this.ax2=this.limitMotor2.axis.x;
  2027. this.ay2=this.limitMotor2.axis.y;
  2028. this.az2=this.limitMotor2.axis.z;
  2029. this.ax3=this.limitMotor3.axis.x;
  2030. this.ay3=this.limitMotor3.axis.y;
  2031. this.az3=this.limitMotor3.axis.z;
  2032. this.lowerLimit1=this.limitMotor1.lowerLimit;
  2033. this.upperLimit1=this.limitMotor1.upperLimit;
  2034. this.motorSpeed1=this.limitMotor1.motorSpeed;
  2035. this.maxMotorForce1=this.limitMotor1.maxMotorForce;
  2036. this.enableMotor1=this.maxMotorForce1>0;
  2037. this.lowerLimit2=this.limitMotor2.lowerLimit;
  2038. this.upperLimit2=this.limitMotor2.upperLimit;
  2039. this.motorSpeed2=this.limitMotor2.motorSpeed;
  2040. this.maxMotorForce2=this.limitMotor2.maxMotorForce;
  2041. this.enableMotor2=this.maxMotorForce2>0;
  2042. this.lowerLimit3=this.limitMotor3.lowerLimit;
  2043. this.upperLimit3=this.limitMotor3.upperLimit;
  2044. this.motorSpeed3=this.limitMotor3.motorSpeed;
  2045. this.maxMotorForce3=this.limitMotor3.maxMotorForce;
  2046. this.enableMotor3=this.maxMotorForce3>0;
  2047. var ti1 = this.i1.elements;
  2048. var ti2 = this.i2.elements;
  2049. this.i1e00=ti1[0];
  2050. this.i1e01=ti1[1];
  2051. this.i1e02=ti1[2];
  2052. this.i1e10=ti1[3];
  2053. this.i1e11=ti1[4];
  2054. this.i1e12=ti1[5];
  2055. this.i1e20=ti1[6];
  2056. this.i1e21=ti1[7];
  2057. this.i1e22=ti1[8];
  2058. this.i2e00=ti2[0];
  2059. this.i2e01=ti2[1];
  2060. this.i2e02=ti2[2];
  2061. this.i2e10=ti2[3];
  2062. this.i2e11=ti2[4];
  2063. this.i2e12=ti2[5];
  2064. this.i2e20=ti2[6];
  2065. this.i2e21=ti2[7];
  2066. this.i2e22=ti2[8];
  2067. var frequency1=this.limitMotor1.frequency;
  2068. var frequency2=this.limitMotor2.frequency;
  2069. var frequency3=this.limitMotor3.frequency;
  2070. var enableSpring1=frequency1>0;
  2071. var enableSpring2=frequency2>0;
  2072. var enableSpring3=frequency3>0;
  2073. var enableLimit1=this.lowerLimit1<=this.upperLimit1;
  2074. var enableLimit2=this.lowerLimit2<=this.upperLimit2;
  2075. var enableLimit3=this.lowerLimit3<=this.upperLimit3;
  2076. var angle1=this.limitMotor1.angle;
  2077. if(enableLimit1){
  2078. if(this.lowerLimit1==this.upperLimit1){
  2079. if(this.limitState1!=0){
  2080. this.limitState1=0;
  2081. this.limitImpulse1=0;
  2082. }
  2083. this.limitVelocity1=this.lowerLimit1-angle1;
  2084. }else if(angle1<this.lowerLimit1){
  2085. if(this.limitState1!=-1){
  2086. this.limitState1=-1;
  2087. this.limitImpulse1=0;
  2088. }
  2089. this.limitVelocity1=this.lowerLimit1-angle1;
  2090. }else if(angle1>this.upperLimit1){
  2091. if(this.limitState1!=1){
  2092. this.limitState1=1;
  2093. this.limitImpulse1=0;
  2094. }
  2095. this.limitVelocity1=this.upperLimit1-angle1;
  2096. }else{
  2097. this.limitState1=2;
  2098. this.limitImpulse1=0;
  2099. this.limitVelocity1=0;
  2100. }
  2101. if(!enableSpring1){
  2102. if(this.limitVelocity1>0.02)this.limitVelocity1-=0.02;
  2103. else if(this.limitVelocity1<-0.02)this.limitVelocity1+=0.02;
  2104. else this.limitVelocity1=0;
  2105. }
  2106. }else{
  2107. this.limitState1=2;
  2108. this.limitImpulse1=0;
  2109. }
  2110. var angle2=this.limitMotor2.angle;
  2111. if(enableLimit2){
  2112. if(this.lowerLimit2==this.upperLimit2){
  2113. if(this.limitState2!=0){
  2114. this.limitState2=0;
  2115. this.limitImpulse2=0;
  2116. }
  2117. this.limitVelocity2=this.lowerLimit2-angle2;
  2118. }else if(angle2<this.lowerLimit2){
  2119. if(this.limitState2!=-1){
  2120. this.limitState2=-1;
  2121. this.limitImpulse2=0;
  2122. }
  2123. this.limitVelocity2=this.lowerLimit2-angle2;
  2124. }else if(angle2>this.upperLimit2){
  2125. if(this.limitState2!=1){
  2126. this.limitState2=1;
  2127. this.limitImpulse2=0;
  2128. }
  2129. this.limitVelocity2=this.upperLimit2-angle2;
  2130. }else{
  2131. this.limitState2=2;
  2132. this.limitImpulse2=0;
  2133. this.limitVelocity2=0;
  2134. }
  2135. if(!enableSpring2){
  2136. if(this.limitVelocity2>0.02)this.limitVelocity2-=0.02;
  2137. else if(this.limitVelocity2<-0.02)this.limitVelocity2+=0.02;
  2138. else this.limitVelocity2=0;
  2139. }
  2140. }else{
  2141. this.limitState2=2;
  2142. this.limitImpulse2=0;
  2143. }
  2144. var angle3=this.limitMotor3.angle;
  2145. if(enableLimit3){
  2146. if(this.lowerLimit3==this.upperLimit3){
  2147. if(this.limitState3!=0){
  2148. this.limitState3=0;
  2149. this.limitImpulse3=0;
  2150. }
  2151. this.limitVelocity3=this.lowerLimit3-angle3;
  2152. }else if(angle3<this.lowerLimit3){
  2153. if(this.limitState3!=-1){
  2154. this.limitState3=-1;
  2155. this.limitImpulse3=0;
  2156. }
  2157. this.limitVelocity3=this.lowerLimit3-angle3;
  2158. }else if(angle3>this.upperLimit3){
  2159. if(this.limitState3!=1){
  2160. this.limitState3=1;
  2161. this.limitImpulse3=0;
  2162. }
  2163. this.limitVelocity3=this.upperLimit3-angle3;
  2164. }else{
  2165. this.limitState3=2;
  2166. this.limitImpulse3=0;
  2167. this.limitVelocity3=0;
  2168. }
  2169. if(!enableSpring3){
  2170. if(this.limitVelocity3>0.02)this.limitVelocity3-=0.02;
  2171. else if(this.limitVelocity3<-0.02)this.limitVelocity3+=0.02;
  2172. else this.limitVelocity3=0;
  2173. }
  2174. }else{
  2175. this.limitState3=2;
  2176. this.limitImpulse3=0;
  2177. }
  2178. if(this.enableMotor1&&(this.limitState1!=0||enableSpring1)){
  2179. this.maxMotorImpulse1=this.maxMotorForce1*timeStep;
  2180. }else{
  2181. this.motorImpulse1=0;
  2182. this.maxMotorImpulse1=0;
  2183. }
  2184. if(this.enableMotor2&&(this.limitState2!=0||enableSpring2)){
  2185. this.maxMotorImpulse2=this.maxMotorForce2*timeStep;
  2186. }else{
  2187. this.motorImpulse2=0;
  2188. this.maxMotorImpulse2=0;
  2189. }
  2190. if(this.enableMotor3&&(this.limitState3!=0||enableSpring3)){
  2191. this.maxMotorImpulse3=this.maxMotorForce3*timeStep;
  2192. }else{
  2193. this.motorImpulse3=0;
  2194. this.maxMotorImpulse3=0;
  2195. }
  2196. // build jacobians
  2197. this.a1x1=this.ax1*this.i1e00+this.ay1*this.i1e01+this.az1*this.i1e02;
  2198. this.a1y1=this.ax1*this.i1e10+this.ay1*this.i1e11+this.az1*this.i1e12;
  2199. this.a1z1=this.ax1*this.i1e20+this.ay1*this.i1e21+this.az1*this.i1e22;
  2200. this.a2x1=this.ax1*this.i2e00+this.ay1*this.i2e01+this.az1*this.i2e02;
  2201. this.a2y1=this.ax1*this.i2e10+this.ay1*this.i2e11+this.az1*this.i2e12;
  2202. this.a2z1=this.ax1*this.i2e20+this.ay1*this.i2e21+this.az1*this.i2e22;
  2203. this.a1x2=this.ax2*this.i1e00+this.ay2*this.i1e01+this.az2*this.i1e02;
  2204. this.a1y2=this.ax2*this.i1e10+this.ay2*this.i1e11+this.az2*this.i1e12;
  2205. this.a1z2=this.ax2*this.i1e20+this.ay2*this.i1e21+this.az2*this.i1e22;
  2206. this.a2x2=this.ax2*this.i2e00+this.ay2*this.i2e01+this.az2*this.i2e02;
  2207. this.a2y2=this.ax2*this.i2e10+this.ay2*this.i2e11+this.az2*this.i2e12;
  2208. this.a2z2=this.ax2*this.i2e20+this.ay2*this.i2e21+this.az2*this.i2e22;
  2209. this.a1x3=this.ax3*this.i1e00+this.ay3*this.i1e01+this.az3*this.i1e02;
  2210. this.a1y3=this.ax3*this.i1e10+this.ay3*this.i1e11+this.az3*this.i1e12;
  2211. this.a1z3=this.ax3*this.i1e20+this.ay3*this.i1e21+this.az3*this.i1e22;
  2212. this.a2x3=this.ax3*this.i2e00+this.ay3*this.i2e01+this.az3*this.i2e02;
  2213. this.a2y3=this.ax3*this.i2e10+this.ay3*this.i2e11+this.az3*this.i2e12;
  2214. this.a2z3=this.ax3*this.i2e20+this.ay3*this.i2e21+this.az3*this.i2e22;
  2215. // build an impulse matrix
  2216. this.k00=this.ax1*(this.a1x1+this.a2x1)+this.ay1*(this.a1y1+this.a2y1)+this.az1*(this.a1z1+this.a2z1);
  2217. this.k01=this.ax1*(this.a1x2+this.a2x2)+this.ay1*(this.a1y2+this.a2y2)+this.az1*(this.a1z2+this.a2z2);
  2218. this.k02=this.ax1*(this.a1x3+this.a2x3)+this.ay1*(this.a1y3+this.a2y3)+this.az1*(this.a1z3+this.a2z3);
  2219. this.k10=this.ax2*(this.a1x1+this.a2x1)+this.ay2*(this.a1y1+this.a2y1)+this.az2*(this.a1z1+this.a2z1);
  2220. this.k11=this.ax2*(this.a1x2+this.a2x2)+this.ay2*(this.a1y2+this.a2y2)+this.az2*(this.a1z2+this.a2z2);
  2221. this.k12=this.ax2*(this.a1x3+this.a2x3)+this.ay2*(this.a1y3+this.a2y3)+this.az2*(this.a1z3+this.a2z3);
  2222. this.k20=this.ax3*(this.a1x1+this.a2x1)+this.ay3*(this.a1y1+this.a2y1)+this.az3*(this.a1z1+this.a2z1);
  2223. this.k21=this.ax3*(this.a1x2+this.a2x2)+this.ay3*(this.a1y2+this.a2y2)+this.az3*(this.a1z2+this.a2z2);
  2224. this.k22=this.ax3*(this.a1x3+this.a2x3)+this.ay3*(this.a1y3+this.a2y3)+this.az3*(this.a1z3+this.a2z3);
  2225. this.kv00=this.k00;
  2226. this.kv11=this.k11;
  2227. this.kv22=this.k22;
  2228. this.dv00=1/this.kv00;
  2229. this.dv11=1/this.kv11;
  2230. this.dv22=1/this.kv22;
  2231. if(enableSpring1&&this.limitState1!=2){
  2232. var omega=6.2831853*frequency1;
  2233. var k=omega*omega*timeStep;
  2234. var dmp=invTimeStep/(k+2*this.limitMotor1.dampingRatio*omega);
  2235. this.cfm1=this.kv00*dmp;
  2236. this.limitVelocity1*=k*dmp;
  2237. }else{
  2238. this.cfm1=0;
  2239. this.limitVelocity1*=invTimeStep*0.05;
  2240. }
  2241. if(enableSpring2&&this.limitState2!=2){
  2242. omega=6.2831853*frequency2;
  2243. k=omega*omega*timeStep;
  2244. dmp=invTimeStep/(k+2*this.limitMotor2.dampingRatio*omega);
  2245. this.cfm2=this.kv11*dmp;
  2246. this.limitVelocity2*=k*dmp;
  2247. }else{
  2248. this.cfm2=0;
  2249. this.limitVelocity2*=invTimeStep*0.05;
  2250. }
  2251. if(enableSpring3&&this.limitState3!=2){
  2252. omega=6.2831853*frequency3;
  2253. k=omega*omega*timeStep;
  2254. dmp=invTimeStep/(k+2*this.limitMotor3.dampingRatio*omega);
  2255. this.cfm3=this.kv22*dmp;
  2256. this.limitVelocity3*=k*dmp;
  2257. }else{
  2258. this.cfm3=0;
  2259. this.limitVelocity3*=invTimeStep*0.05;
  2260. }
  2261. this.k00+=this.cfm1;
  2262. this.k11+=this.cfm2;
  2263. this.k22+=this.cfm3;
  2264. var inv=1/(
  2265. this.k00*(this.k11*this.k22-this.k21*this.k12)+
  2266. this.k10*(this.k21*this.k02-this.k01*this.k22)+
  2267. this.k20*(this.k01*this.k12-this.k11*this.k02)
  2268. );
  2269. this.d00=(this.k11*this.k22-this.k12*this.k21)*inv;
  2270. this.d01=(this.k02*this.k21-this.k01*this.k22)*inv;
  2271. this.d02=(this.k01*this.k12-this.k02*this.k11)*inv;
  2272. this.d10=(this.k12*this.k20-this.k10*this.k22)*inv;
  2273. this.d11=(this.k00*this.k22-this.k02*this.k20)*inv;
  2274. this.d12=(this.k02*this.k10-this.k00*this.k12)*inv;
  2275. this.d20=(this.k10*this.k21-this.k11*this.k20)*inv;
  2276. this.d21=(this.k01*this.k20-this.k00*this.k21)*inv;
  2277. this.d22=(this.k00*this.k11-this.k01*this.k10)*inv;
  2278. this.limitImpulse1*=0.95;
  2279. this.motorImpulse1*=0.95;
  2280. this.limitImpulse2*=0.95;
  2281. this.motorImpulse2*=0.95;
  2282. this.limitImpulse3*=0.95;
  2283. this.motorImpulse3*=0.95;
  2284. var totalImpulse1=this.limitImpulse1+this.motorImpulse1;
  2285. var totalImpulse2=this.limitImpulse2+this.motorImpulse2;
  2286. var totalImpulse3=this.limitImpulse3+this.motorImpulse3;
  2287. this.a1.x+=totalImpulse1*this.a1x1+totalImpulse2*this.a1x2+totalImpulse3*this.a1x3;
  2288. this.a1.y+=totalImpulse1*this.a1y1+totalImpulse2*this.a1y2+totalImpulse3*this.a1y3;
  2289. this.a1.z+=totalImpulse1*this.a1z1+totalImpulse2*this.a1z2+totalImpulse3*this.a1z3;
  2290. this.a2.x-=totalImpulse1*this.a2x1+totalImpulse2*this.a2x2+totalImpulse3*this.a2x3;
  2291. this.a2.y-=totalImpulse1*this.a2y1+totalImpulse2*this.a2y2+totalImpulse3*this.a2y3;
  2292. this.a2.z-=totalImpulse1*this.a2z1+totalImpulse2*this.a2z2+totalImpulse3*this.a2z3;
  2293. },
  2294. solve_:function(){
  2295. var rvx=this.a2.x-this.a1.x;
  2296. var rvy=this.a2.y-this.a1.y;
  2297. var rvz=this.a2.z-this.a1.z;
  2298. this.limitVelocity3=30;
  2299. var rvn1=rvx*this.ax1+rvy*this.ay1+rvz*this.az1-this.limitVelocity1;
  2300. var rvn2=rvx*this.ax2+rvy*this.ay2+rvz*this.az2-this.limitVelocity2;
  2301. var rvn3=rvx*this.ax3+rvy*this.ay3+rvz*this.az3-this.limitVelocity3;
  2302. var dLimitImpulse1=rvn1*this.d00+rvn2*this.d01+rvn3*this.d02;
  2303. var dLimitImpulse2=rvn1*this.d10+rvn2*this.d11+rvn3*this.d12;
  2304. var dLimitImpulse3=rvn1*this.d20+rvn2*this.d21+rvn3*this.d22;
  2305. this.limitImpulse1+=dLimitImpulse1;
  2306. this.limitImpulse2+=dLimitImpulse2;
  2307. this.limitImpulse3+=dLimitImpulse3;
  2308. this.a1.x+=dLimitImpulse1*this.a1x1+dLimitImpulse2*this.a1x2+dLimitImpulse3*this.a1x3;
  2309. this.a1.y+=dLimitImpulse1*this.a1y1+dLimitImpulse2*this.a1y2+dLimitImpulse3*this.a1y3;
  2310. this.a1.z+=dLimitImpulse1*this.a1z1+dLimitImpulse2*this.a1z2+dLimitImpulse3*this.a1z3;
  2311. this.a2.x-=dLimitImpulse1*this.a2x1+dLimitImpulse2*this.a2x2+dLimitImpulse3*this.a2x3;
  2312. this.a2.y-=dLimitImpulse1*this.a2y1+dLimitImpulse2*this.a2y2+dLimitImpulse3*this.a2y3;
  2313. this.a2.z-=dLimitImpulse1*this.a2z1+dLimitImpulse2*this.a2z2+dLimitImpulse3*this.a2z3;
  2314. },
  2315. solve:function(){
  2316. var rvx=this.a2.x-this.a1.x;
  2317. var rvy=this.a2.y-this.a1.y;
  2318. var rvz=this.a2.z-this.a1.z;
  2319. var rvn1=rvx*this.ax1+rvy*this.ay1+rvz*this.az1;
  2320. var rvn2=rvx*this.ax2+rvy*this.ay2+rvz*this.az2;
  2321. var rvn3=rvx*this.ax3+rvy*this.ay3+rvz*this.az3;
  2322. var oldMotorImpulse1=this.motorImpulse1;
  2323. var oldMotorImpulse2=this.motorImpulse2;
  2324. var oldMotorImpulse3=this.motorImpulse3;
  2325. var dMotorImpulse1=0;
  2326. var dMotorImpulse2=0;
  2327. var dMotorImpulse3=0;
  2328. if(this.enableMotor1){
  2329. dMotorImpulse1=(rvn1-this.motorSpeed1)*this.dv00;
  2330. this.motorImpulse1+=dMotorImpulse1;
  2331. if(this.motorImpulse1>this.maxMotorImpulse1){ // clamp motor impulse
  2332. this.motorImpulse1=this.maxMotorImpulse1;
  2333. }else if(this.motorImpulse1<-this.maxMotorImpulse1){
  2334. this.motorImpulse1=-this.maxMotorImpulse1;
  2335. }
  2336. dMotorImpulse1=this.motorImpulse1-oldMotorImpulse1;
  2337. }
  2338. if(this.enableMotor2){
  2339. dMotorImpulse2=(rvn2-this.motorSpeed2)*this.dv11;
  2340. this.motorImpulse2+=dMotorImpulse2;
  2341. if(this.motorImpulse2>this.maxMotorImpulse2){ // clamp motor impulse
  2342. this.motorImpulse2=this.maxMotorImpulse2;
  2343. }else if(this.motorImpulse2<-this.maxMotorImpulse2){
  2344. this.motorImpulse2=-this.maxMotorImpulse2;
  2345. }
  2346. dMotorImpulse2=this.motorImpulse2-oldMotorImpulse2;
  2347. }
  2348. if(this.enableMotor3){
  2349. dMotorImpulse3=(rvn3-this.motorSpeed3)*this.dv22;
  2350. this.motorImpulse3+=dMotorImpulse3;
  2351. if(this.motorImpulse3>this.maxMotorImpulse3){ // clamp motor impulse
  2352. this.motorImpulse3=this.maxMotorImpulse3;
  2353. }else if(this.motorImpulse3<-this.maxMotorImpulse3){
  2354. this.motorImpulse3=-this.maxMotorImpulse3;
  2355. }
  2356. dMotorImpulse3=this.motorImpulse3-oldMotorImpulse3;
  2357. }
  2358. // apply motor impulse to relative velocity
  2359. rvn1+=dMotorImpulse1*this.kv00+dMotorImpulse2*this.k01+dMotorImpulse3*this.k02;
  2360. rvn2+=dMotorImpulse1*this.k10+dMotorImpulse2*this.kv11+dMotorImpulse3*this.k12;
  2361. rvn3+=dMotorImpulse1*this.k20+dMotorImpulse2*this.k21+dMotorImpulse3*this.kv22;
  2362. // subtract target velocity and applied impulse
  2363. rvn1-=this.limitVelocity1+this.limitImpulse1*this.cfm1;
  2364. rvn2-=this.limitVelocity2+this.limitImpulse2*this.cfm2;
  2365. rvn3-=this.limitVelocity3+this.limitImpulse3*this.cfm3;
  2366. var oldLimitImpulse1=this.limitImpulse1;
  2367. var oldLimitImpulse2=this.limitImpulse2;
  2368. var oldLimitImpulse3=this.limitImpulse3;
  2369. var dLimitImpulse1=rvn1*this.d00+rvn2*this.d01+rvn3*this.d02;
  2370. var dLimitImpulse2=rvn1*this.d10+rvn2*this.d11+rvn3*this.d12;
  2371. var dLimitImpulse3=rvn1*this.d20+rvn2*this.d21+rvn3*this.d22;
  2372. this.limitImpulse1+=dLimitImpulse1;
  2373. this.limitImpulse2+=dLimitImpulse2;
  2374. this.limitImpulse3+=dLimitImpulse3;
  2375. // clamp
  2376. var clampState=0;
  2377. if(this.limitState1==2||this.limitImpulse1*this.limitState1<0){
  2378. dLimitImpulse1=-oldLimitImpulse1;
  2379. rvn2+=dLimitImpulse1*this.k10;
  2380. rvn3+=dLimitImpulse1*this.k20;
  2381. clampState|=1;
  2382. }
  2383. if(this.limitState2==2||this.limitImpulse2*this.limitState2<0){
  2384. dLimitImpulse2=-oldLimitImpulse2;
  2385. rvn1+=dLimitImpulse2*this.k01;
  2386. rvn3+=dLimitImpulse2*this.k21;
  2387. clampState|=2;
  2388. }
  2389. if(this.limitState3==2||this.limitImpulse3*this.limitState3<0){
  2390. dLimitImpulse3=-oldLimitImpulse3;
  2391. rvn1+=dLimitImpulse3*this.k02;
  2392. rvn2+=dLimitImpulse3*this.k12;
  2393. clampState|=4;
  2394. }
  2395. // update un-clamped impulse
  2396. // TODO: isolate division
  2397. var det;
  2398. switch(clampState){
  2399. case 1: // update 2 3
  2400. det=1/(this.k11*this.k22-this.k12*this.k21);
  2401. dLimitImpulse2=(this.k22*rvn2+-this.k12*rvn3)*det;
  2402. dLimitImpulse3=(-this.k21*rvn2+this.k11*rvn3)*det;
  2403. break;
  2404. case 2: // update 1 3
  2405. det=1/(this.k00*this.k22-this.k02*this.k20);
  2406. dLimitImpulse1=(this.k22*rvn1+-this.k02*rvn3)*det;
  2407. dLimitImpulse3=(-this.k20*rvn1+this.k00*rvn3)*det;
  2408. break;
  2409. case 3: // update 3
  2410. dLimitImpulse3=rvn3/this.k22;
  2411. break;
  2412. case 4: // update 1 2
  2413. det=1/(this.k00*this.k11-this.k01*this.k10);
  2414. dLimitImpulse1=(this.k11*rvn1+-this.k01*rvn2)*det;
  2415. dLimitImpulse2=(-this.k10*rvn1+this.k00*rvn2)*det;
  2416. break;
  2417. case 5: // update 2
  2418. dLimitImpulse2=rvn2/this.k11;
  2419. break;
  2420. case 6: // update 1
  2421. dLimitImpulse1=rvn1/this.k00;
  2422. break;
  2423. }
  2424. this.limitImpulse1=dLimitImpulse1+oldLimitImpulse1;
  2425. this.limitImpulse2=dLimitImpulse2+oldLimitImpulse2;
  2426. this.limitImpulse3=dLimitImpulse3+oldLimitImpulse3;
  2427. var dImpulse1=dMotorImpulse1+dLimitImpulse1;
  2428. var dImpulse2=dMotorImpulse2+dLimitImpulse2;
  2429. var dImpulse3=dMotorImpulse3+dLimitImpulse3;
  2430. // apply impulse
  2431. this.a1.x+=dImpulse1*this.a1x1+dImpulse2*this.a1x2+dImpulse3*this.a1x3;
  2432. this.a1.y+=dImpulse1*this.a1y1+dImpulse2*this.a1y2+dImpulse3*this.a1y3;
  2433. this.a1.z+=dImpulse1*this.a1z1+dImpulse2*this.a1z2+dImpulse3*this.a1z3;
  2434. this.a2.x-=dImpulse1*this.a2x1+dImpulse2*this.a2x2+dImpulse3*this.a2x3;
  2435. this.a2.y-=dImpulse1*this.a2y1+dImpulse2*this.a2y2+dImpulse3*this.a2y3;
  2436. this.a2.z-=dImpulse1*this.a2z1+dImpulse2*this.a2z2+dImpulse3*this.a2z3;
  2437. rvx=this.a2.x-this.a1.x;
  2438. rvy=this.a2.y-this.a1.y;
  2439. rvz=this.a2.z-this.a1.z;
  2440. rvn2=rvx*this.ax2+rvy*this.ay2+rvz*this.az2;
  2441. }
  2442. } );
  2443. /**
  2444. * A hinge joint allows only for relative rotation of rigid bodies along the axis.
  2445. *
  2446. * @author saharan
  2447. * @author lo-th
  2448. */
  2449. function HingeJoint ( config, lowerAngleLimit, upperAngleLimit ) {
  2450. Joint.call( this, config );
  2451. this.type = JOINT_HINGE;
  2452. // The axis in the first body's coordinate system.
  2453. this.localAxis1 = config.localAxis1.clone().normalize();
  2454. // The axis in the second body's coordinate system.
  2455. this.localAxis2 = config.localAxis2.clone().normalize();
  2456. // make angle axis
  2457. var arc = new Mat33().setQuat( new Quat().setFromUnitVectors( this.localAxis1, this.localAxis2 ) );
  2458. this.localAngle1 = new Vec3().tangent( this.localAxis1 ).normalize();
  2459. this.localAngle2 = this.localAngle1.clone().applyMatrix3( arc, true );
  2460. this.ax1 = new Vec3();
  2461. this.ax2 = new Vec3();
  2462. this.an1 = new Vec3();
  2463. this.an2 = new Vec3();
  2464. this.tmp = new Vec3();
  2465. this.nor = new Vec3();
  2466. this.tan = new Vec3();
  2467. this.bin = new Vec3();
  2468. // The rotational limit and motor information of the joint.
  2469. this.limitMotor = new LimitMotor( this.nor, false );
  2470. this.limitMotor.lowerLimit = lowerAngleLimit;
  2471. this.limitMotor.upperLimit = upperAngleLimit;
  2472. this.lc = new LinearConstraint( this );
  2473. this.r3 = new Rotational3Constraint( this, this.limitMotor, new LimitMotor( this.tan, true ), new LimitMotor( this.bin, true ) );
  2474. }
  2475. HingeJoint.prototype = Object.assign( Object.create( Joint.prototype ), {
  2476. constructor: HingeJoint,
  2477. preSolve: function ( timeStep, invTimeStep ) {
  2478. this.updateAnchorPoints();
  2479. this.ax1.copy( this.localAxis1 ).applyMatrix3( this.body1.rotation, true );
  2480. this.ax2.copy( this.localAxis2 ).applyMatrix3( this.body2.rotation, true );
  2481. this.an1.copy( this.localAngle1 ).applyMatrix3( this.body1.rotation, true );
  2482. this.an2.copy( this.localAngle2 ).applyMatrix3( this.body2.rotation, true );
  2483. // normal tangent binormal
  2484. this.nor.set(
  2485. this.ax1.x*this.body2.inverseMass + this.ax2.x*this.body1.inverseMass,
  2486. this.ax1.y*this.body2.inverseMass + this.ax2.y*this.body1.inverseMass,
  2487. this.ax1.z*this.body2.inverseMass + this.ax2.z*this.body1.inverseMass
  2488. ).normalize();
  2489. this.tan.tangent( this.nor ).normalize();
  2490. this.bin.crossVectors( this.nor, this.tan );
  2491. // calculate hinge angle
  2492. var limite = _Math.acosClamp( _Math.dotVectors( this.an1, this.an2 ) );
  2493. this.tmp.crossVectors( this.an1, this.an2 );
  2494. if( _Math.dotVectors( this.nor, this.tmp ) < 0 ) this.limitMotor.angle = -limite;
  2495. else this.limitMotor.angle = limite;
  2496. this.tmp.crossVectors( this.ax1, this.ax2 );
  2497. this.r3.limitMotor2.angle = _Math.dotVectors( this.tan, this.tmp );
  2498. this.r3.limitMotor3.angle = _Math.dotVectors( this.bin, this.tmp );
  2499. // preSolve
  2500. this.r3.preSolve( timeStep, invTimeStep );
  2501. this.lc.preSolve( timeStep, invTimeStep );
  2502. },
  2503. solve: function () {
  2504. this.r3.solve();
  2505. this.lc.solve();
  2506. },
  2507. postSolve: function () {
  2508. }
  2509. });
  2510. /**
  2511. * A ball-and-socket joint limits relative translation on two anchor points on rigid bodies.
  2512. *
  2513. * @author saharan
  2514. * @author lo-th
  2515. */
  2516. function BallAndSocketJoint ( config ){
  2517. Joint.call( this, config );
  2518. this.type = JOINT_BALL_AND_SOCKET;
  2519. this.lc = new LinearConstraint( this );
  2520. }
  2521. BallAndSocketJoint.prototype = Object.assign( Object.create( Joint.prototype ), {
  2522. constructor: BallAndSocketJoint,
  2523. preSolve: function ( timeStep, invTimeStep ) {
  2524. this.updateAnchorPoints();
  2525. // preSolve
  2526. this.lc.preSolve( timeStep, invTimeStep );
  2527. },
  2528. solve: function () {
  2529. this.lc.solve();
  2530. },
  2531. postSolve: function () {
  2532. }
  2533. });
  2534. /**
  2535. * A translational constraint for various joints.
  2536. * @author saharan
  2537. */
  2538. function TranslationalConstraint ( joint, limitMotor ){
  2539. this.cfm=NaN;
  2540. this.m1=NaN;
  2541. this.m2=NaN;
  2542. this.i1e00=NaN;
  2543. this.i1e01=NaN;
  2544. this.i1e02=NaN;
  2545. this.i1e10=NaN;
  2546. this.i1e11=NaN;
  2547. this.i1e12=NaN;
  2548. this.i1e20=NaN;
  2549. this.i1e21=NaN;
  2550. this.i1e22=NaN;
  2551. this.i2e00=NaN;
  2552. this.i2e01=NaN;
  2553. this.i2e02=NaN;
  2554. this.i2e10=NaN;
  2555. this.i2e11=NaN;
  2556. this.i2e12=NaN;
  2557. this.i2e20=NaN;
  2558. this.i2e21=NaN;
  2559. this.i2e22=NaN;
  2560. this.motorDenom=NaN;
  2561. this.invMotorDenom=NaN;
  2562. this.invDenom=NaN;
  2563. this.ax=NaN;
  2564. this.ay=NaN;
  2565. this.az=NaN;
  2566. this.r1x=NaN;
  2567. this.r1y=NaN;
  2568. this.r1z=NaN;
  2569. this.r2x=NaN;
  2570. this.r2y=NaN;
  2571. this.r2z=NaN;
  2572. this.t1x=NaN;
  2573. this.t1y=NaN;
  2574. this.t1z=NaN;
  2575. this.t2x=NaN;
  2576. this.t2y=NaN;
  2577. this.t2z=NaN;
  2578. this.l1x=NaN;
  2579. this.l1y=NaN;
  2580. this.l1z=NaN;
  2581. this.l2x=NaN;
  2582. this.l2y=NaN;
  2583. this.l2z=NaN;
  2584. this.a1x=NaN;
  2585. this.a1y=NaN;
  2586. this.a1z=NaN;
  2587. this.a2x=NaN;
  2588. this.a2y=NaN;
  2589. this.a2z=NaN;
  2590. this.lowerLimit=NaN;
  2591. this.upperLimit=NaN;
  2592. this.limitVelocity=NaN;
  2593. this.limitState=0; // -1: at lower, 0: locked, 1: at upper, 2: free
  2594. this.enableMotor=false;
  2595. this.motorSpeed=NaN;
  2596. this.maxMotorForce=NaN;
  2597. this.maxMotorImpulse=NaN;
  2598. this.limitMotor=limitMotor;
  2599. this.b1=joint.body1;
  2600. this.b2=joint.body2;
  2601. this.p1=joint.anchorPoint1;
  2602. this.p2=joint.anchorPoint2;
  2603. this.r1=joint.relativeAnchorPoint1;
  2604. this.r2=joint.relativeAnchorPoint2;
  2605. this.l1=this.b1.linearVelocity;
  2606. this.l2=this.b2.linearVelocity;
  2607. this.a1=this.b1.angularVelocity;
  2608. this.a2=this.b2.angularVelocity;
  2609. this.i1=this.b1.inverseInertia;
  2610. this.i2=this.b2.inverseInertia;
  2611. this.limitImpulse=0;
  2612. this.motorImpulse=0;
  2613. }
  2614. Object.assign( TranslationalConstraint.prototype, {
  2615. TranslationalConstraint: true,
  2616. preSolve:function(timeStep,invTimeStep){
  2617. this.ax=this.limitMotor.axis.x;
  2618. this.ay=this.limitMotor.axis.y;
  2619. this.az=this.limitMotor.axis.z;
  2620. this.lowerLimit=this.limitMotor.lowerLimit;
  2621. this.upperLimit=this.limitMotor.upperLimit;
  2622. this.motorSpeed=this.limitMotor.motorSpeed;
  2623. this.maxMotorForce=this.limitMotor.maxMotorForce;
  2624. this.enableMotor=this.maxMotorForce>0;
  2625. this.m1=this.b1.inverseMass;
  2626. this.m2=this.b2.inverseMass;
  2627. var ti1 = this.i1.elements;
  2628. var ti2 = this.i2.elements;
  2629. this.i1e00=ti1[0];
  2630. this.i1e01=ti1[1];
  2631. this.i1e02=ti1[2];
  2632. this.i1e10=ti1[3];
  2633. this.i1e11=ti1[4];
  2634. this.i1e12=ti1[5];
  2635. this.i1e20=ti1[6];
  2636. this.i1e21=ti1[7];
  2637. this.i1e22=ti1[8];
  2638. this.i2e00=ti2[0];
  2639. this.i2e01=ti2[1];
  2640. this.i2e02=ti2[2];
  2641. this.i2e10=ti2[3];
  2642. this.i2e11=ti2[4];
  2643. this.i2e12=ti2[5];
  2644. this.i2e20=ti2[6];
  2645. this.i2e21=ti2[7];
  2646. this.i2e22=ti2[8];
  2647. var dx=this.p2.x-this.p1.x;
  2648. var dy=this.p2.y-this.p1.y;
  2649. var dz=this.p2.z-this.p1.z;
  2650. var d=dx*this.ax+dy*this.ay+dz*this.az;
  2651. var frequency=this.limitMotor.frequency;
  2652. var enableSpring=frequency>0;
  2653. var enableLimit=this.lowerLimit<=this.upperLimit;
  2654. if(enableSpring&&d>20||d<-20){
  2655. enableSpring=false;
  2656. }
  2657. if(enableLimit){
  2658. if(this.lowerLimit==this.upperLimit){
  2659. if(this.limitState!=0){
  2660. this.limitState=0;
  2661. this.limitImpulse=0;
  2662. }
  2663. this.limitVelocity=this.lowerLimit-d;
  2664. if(!enableSpring)d=this.lowerLimit;
  2665. }else if(d<this.lowerLimit){
  2666. if(this.limitState!=-1){
  2667. this.limitState=-1;
  2668. this.limitImpulse=0;
  2669. }
  2670. this.limitVelocity=this.lowerLimit-d;
  2671. if(!enableSpring)d=this.lowerLimit;
  2672. }else if(d>this.upperLimit){
  2673. if(this.limitState!=1){
  2674. this.limitState=1;
  2675. this.limitImpulse=0;
  2676. }
  2677. this.limitVelocity=this.upperLimit-d;
  2678. if(!enableSpring)d=this.upperLimit;
  2679. }else{
  2680. this.limitState=2;
  2681. this.limitImpulse=0;
  2682. this.limitVelocity=0;
  2683. }
  2684. if(!enableSpring){
  2685. if(this.limitVelocity>0.005)this.limitVelocity-=0.005;
  2686. else if(this.limitVelocity<-0.005)this.limitVelocity+=0.005;
  2687. else this.limitVelocity=0;
  2688. }
  2689. }else{
  2690. this.limitState=2;
  2691. this.limitImpulse=0;
  2692. }
  2693. if(this.enableMotor&&(this.limitState!=0||enableSpring)){
  2694. this.maxMotorImpulse=this.maxMotorForce*timeStep;
  2695. }else{
  2696. this.motorImpulse=0;
  2697. this.maxMotorImpulse=0;
  2698. }
  2699. var rdx=d*this.ax;
  2700. var rdy=d*this.ay;
  2701. var rdz=d*this.az;
  2702. var w1=this.m1/(this.m1+this.m2);
  2703. var w2=1-w1;
  2704. this.r1x=this.r1.x+rdx*w1;
  2705. this.r1y=this.r1.y+rdy*w1;
  2706. this.r1z=this.r1.z+rdz*w1;
  2707. this.r2x=this.r2.x-rdx*w2;
  2708. this.r2y=this.r2.y-rdy*w2;
  2709. this.r2z=this.r2.z-rdz*w2;
  2710. this.t1x=this.r1y*this.az-this.r1z*this.ay;
  2711. this.t1y=this.r1z*this.ax-this.r1x*this.az;
  2712. this.t1z=this.r1x*this.ay-this.r1y*this.ax;
  2713. this.t2x=this.r2y*this.az-this.r2z*this.ay;
  2714. this.t2y=this.r2z*this.ax-this.r2x*this.az;
  2715. this.t2z=this.r2x*this.ay-this.r2y*this.ax;
  2716. this.l1x=this.ax*this.m1;
  2717. this.l1y=this.ay*this.m1;
  2718. this.l1z=this.az*this.m1;
  2719. this.l2x=this.ax*this.m2;
  2720. this.l2y=this.ay*this.m2;
  2721. this.l2z=this.az*this.m2;
  2722. this.a1x=this.t1x*this.i1e00+this.t1y*this.i1e01+this.t1z*this.i1e02;
  2723. this.a1y=this.t1x*this.i1e10+this.t1y*this.i1e11+this.t1z*this.i1e12;
  2724. this.a1z=this.t1x*this.i1e20+this.t1y*this.i1e21+this.t1z*this.i1e22;
  2725. this.a2x=this.t2x*this.i2e00+this.t2y*this.i2e01+this.t2z*this.i2e02;
  2726. this.a2y=this.t2x*this.i2e10+this.t2y*this.i2e11+this.t2z*this.i2e12;
  2727. this.a2z=this.t2x*this.i2e20+this.t2y*this.i2e21+this.t2z*this.i2e22;
  2728. this.motorDenom=
  2729. this.m1+this.m2+
  2730. this.ax*(this.a1y*this.r1z-this.a1z*this.r1y+this.a2y*this.r2z-this.a2z*this.r2y)+
  2731. this.ay*(this.a1z*this.r1x-this.a1x*this.r1z+this.a2z*this.r2x-this.a2x*this.r2z)+
  2732. this.az*(this.a1x*this.r1y-this.a1y*this.r1x+this.a2x*this.r2y-this.a2y*this.r2x);
  2733. this.invMotorDenom=1/this.motorDenom;
  2734. if(enableSpring&&this.limitState!=2){
  2735. var omega=6.2831853*frequency;
  2736. var k=omega*omega*timeStep;
  2737. var dmp=invTimeStep/(k+2*this.limitMotor.dampingRatio*omega);
  2738. this.cfm=this.motorDenom*dmp;
  2739. this.limitVelocity*=k*dmp;
  2740. }else{
  2741. this.cfm=0;
  2742. this.limitVelocity*=invTimeStep*0.05;
  2743. }
  2744. this.invDenom=1/(this.motorDenom+this.cfm);
  2745. var totalImpulse=this.limitImpulse+this.motorImpulse;
  2746. this.l1.x+=totalImpulse*this.l1x;
  2747. this.l1.y+=totalImpulse*this.l1y;
  2748. this.l1.z+=totalImpulse*this.l1z;
  2749. this.a1.x+=totalImpulse*this.a1x;
  2750. this.a1.y+=totalImpulse*this.a1y;
  2751. this.a1.z+=totalImpulse*this.a1z;
  2752. this.l2.x-=totalImpulse*this.l2x;
  2753. this.l2.y-=totalImpulse*this.l2y;
  2754. this.l2.z-=totalImpulse*this.l2z;
  2755. this.a2.x-=totalImpulse*this.a2x;
  2756. this.a2.y-=totalImpulse*this.a2y;
  2757. this.a2.z-=totalImpulse*this.a2z;
  2758. },
  2759. solve:function(){
  2760. var rvn=
  2761. this.ax*(this.l2.x-this.l1.x)+this.ay*(this.l2.y-this.l1.y)+this.az*(this.l2.z-this.l1.z)+
  2762. this.t2x*this.a2.x-this.t1x*this.a1.x+this.t2y*this.a2.y-this.t1y*this.a1.y+this.t2z*this.a2.z-this.t1z*this.a1.z;
  2763. // motor part
  2764. var newMotorImpulse;
  2765. if(this.enableMotor){
  2766. newMotorImpulse=(rvn-this.motorSpeed)*this.invMotorDenom;
  2767. var oldMotorImpulse=this.motorImpulse;
  2768. this.motorImpulse+=newMotorImpulse;
  2769. if(this.motorImpulse>this.maxMotorImpulse)this.motorImpulse=this.maxMotorImpulse;
  2770. else if(this.motorImpulse<-this.maxMotorImpulse)this.motorImpulse=-this.maxMotorImpulse;
  2771. newMotorImpulse=this.motorImpulse-oldMotorImpulse;
  2772. rvn-=newMotorImpulse*this.motorDenom;
  2773. }else newMotorImpulse=0;
  2774. // limit part
  2775. var newLimitImpulse;
  2776. if(this.limitState!=2){
  2777. newLimitImpulse=(rvn-this.limitVelocity-this.limitImpulse*this.cfm)*this.invDenom;
  2778. var oldLimitImpulse=this.limitImpulse;
  2779. this.limitImpulse+=newLimitImpulse;
  2780. if(this.limitImpulse*this.limitState<0)this.limitImpulse=0;
  2781. newLimitImpulse=this.limitImpulse-oldLimitImpulse;
  2782. }else newLimitImpulse=0;
  2783. var totalImpulse=newLimitImpulse+newMotorImpulse;
  2784. this.l1.x+=totalImpulse*this.l1x;
  2785. this.l1.y+=totalImpulse*this.l1y;
  2786. this.l1.z+=totalImpulse*this.l1z;
  2787. this.a1.x+=totalImpulse*this.a1x;
  2788. this.a1.y+=totalImpulse*this.a1y;
  2789. this.a1.z+=totalImpulse*this.a1z;
  2790. this.l2.x-=totalImpulse*this.l2x;
  2791. this.l2.y-=totalImpulse*this.l2y;
  2792. this.l2.z-=totalImpulse*this.l2z;
  2793. this.a2.x-=totalImpulse*this.a2x;
  2794. this.a2.y-=totalImpulse*this.a2y;
  2795. this.a2.z-=totalImpulse*this.a2z;
  2796. }
  2797. } );
  2798. /**
  2799. * A distance joint limits the distance between two anchor points on rigid bodies.
  2800. *
  2801. * @author saharan
  2802. * @author lo-th
  2803. */
  2804. function DistanceJoint ( config, minDistance, maxDistance ){
  2805. Joint.call( this, config );
  2806. this.type = JOINT_DISTANCE;
  2807. this.nor = new Vec3();
  2808. // The limit and motor information of the joint.
  2809. this.limitMotor = new LimitMotor( this.nor, true );
  2810. this.limitMotor.lowerLimit = minDistance;
  2811. this.limitMotor.upperLimit = maxDistance;
  2812. this.t = new TranslationalConstraint( this, this.limitMotor );
  2813. }
  2814. DistanceJoint.prototype = Object.assign( Object.create( Joint.prototype ), {
  2815. constructor: DistanceJoint,
  2816. preSolve: function ( timeStep, invTimeStep ) {
  2817. this.updateAnchorPoints();
  2818. this.nor.sub( this.anchorPoint2, this.anchorPoint1 ).normalize();
  2819. // preSolve
  2820. this.t.preSolve( timeStep, invTimeStep );
  2821. },
  2822. solve: function () {
  2823. this.t.solve();
  2824. },
  2825. postSolve: function () {
  2826. }
  2827. });
  2828. /**
  2829. * An angular constraint for all axes for various joints.
  2830. * @author saharan
  2831. */
  2832. function AngularConstraint( joint, targetOrientation ) {
  2833. this.joint = joint;
  2834. this.targetOrientation = new Quat().invert( targetOrientation );
  2835. this.relativeOrientation = new Quat();
  2836. this.ii1 = null;
  2837. this.ii2 = null;
  2838. this.dd = null;
  2839. this.vel = new Vec3();
  2840. this.imp = new Vec3();
  2841. this.rn0 = new Vec3();
  2842. this.rn1 = new Vec3();
  2843. this.rn2 = new Vec3();
  2844. this.b1 = joint.body1;
  2845. this.b2 = joint.body2;
  2846. this.a1 = this.b1.angularVelocity;
  2847. this.a2 = this.b2.angularVelocity;
  2848. this.i1 = this.b1.inverseInertia;
  2849. this.i2 = this.b2.inverseInertia;
  2850. }
  2851. Object.assign( AngularConstraint.prototype, {
  2852. AngularConstraint: true,
  2853. preSolve: function ( timeStep, invTimeStep ) {
  2854. var inv, len, v;
  2855. this.ii1 = this.i1.clone();
  2856. this.ii2 = this.i2.clone();
  2857. v = new Mat33().add(this.ii1, this.ii2).elements;
  2858. inv = 1/( v[0]*(v[4]*v[8]-v[7]*v[5]) + v[3]*(v[7]*v[2]-v[1]*v[8]) + v[6]*(v[1]*v[5]-v[4]*v[2]) );
  2859. this.dd = new Mat33().set(
  2860. v[4]*v[8]-v[5]*v[7], v[2]*v[7]-v[1]*v[8], v[1]*v[5]-v[2]*v[4],
  2861. v[5]*v[6]-v[3]*v[8], v[0]*v[8]-v[2]*v[6], v[2]*v[3]-v[0]*v[5],
  2862. v[3]*v[7]-v[4]*v[6], v[1]*v[6]-v[0]*v[7], v[0]*v[4]-v[1]*v[3]
  2863. ).multiplyScalar( inv );
  2864. this.relativeOrientation.invert( this.b1.orientation ).multiply( this.targetOrientation ).multiply( this.b2.orientation );
  2865. inv = this.relativeOrientation.w*2;
  2866. this.vel.copy( this.relativeOrientation ).multiplyScalar( inv );
  2867. len = this.vel.length();
  2868. if( len > 0.02 ) {
  2869. len = (0.02-len)/len*invTimeStep*0.05;
  2870. this.vel.multiplyScalar( len );
  2871. }else{
  2872. this.vel.set(0,0,0);
  2873. }
  2874. this.rn1.copy( this.imp ).applyMatrix3( this.ii1, true );
  2875. this.rn2.copy( this.imp ).applyMatrix3( this.ii2, true );
  2876. this.a1.add( this.rn1 );
  2877. this.a2.sub( this.rn2 );
  2878. },
  2879. solve: function () {
  2880. var r = this.a2.clone().sub( this.a1 ).sub( this.vel );
  2881. this.rn0.copy( r ).applyMatrix3( this.dd, true );
  2882. this.rn1.copy( this.rn0 ).applyMatrix3( this.ii1, true );
  2883. this.rn2.copy( this.rn0 ).applyMatrix3( this.ii2, true );
  2884. this.imp.add( this.rn0 );
  2885. this.a1.add( this.rn1 );
  2886. this.a2.sub( this.rn2 );
  2887. }
  2888. } );
  2889. /**
  2890. * A three-axis translational constraint for various joints.
  2891. * @author saharan
  2892. */
  2893. function Translational3Constraint (joint,limitMotor1,limitMotor2,limitMotor3){
  2894. this.m1=NaN;
  2895. this.m2=NaN;
  2896. this.i1e00=NaN;
  2897. this.i1e01=NaN;
  2898. this.i1e02=NaN;
  2899. this.i1e10=NaN;
  2900. this.i1e11=NaN;
  2901. this.i1e12=NaN;
  2902. this.i1e20=NaN;
  2903. this.i1e21=NaN;
  2904. this.i1e22=NaN;
  2905. this.i2e00=NaN;
  2906. this.i2e01=NaN;
  2907. this.i2e02=NaN;
  2908. this.i2e10=NaN;
  2909. this.i2e11=NaN;
  2910. this.i2e12=NaN;
  2911. this.i2e20=NaN;
  2912. this.i2e21=NaN;
  2913. this.i2e22=NaN;
  2914. this.ax1=NaN;
  2915. this.ay1=NaN;
  2916. this.az1=NaN;
  2917. this.ax2=NaN;
  2918. this.ay2=NaN;
  2919. this.az2=NaN;
  2920. this.ax3=NaN;
  2921. this.ay3=NaN;
  2922. this.az3=NaN;
  2923. this.r1x=NaN;
  2924. this.r1y=NaN;
  2925. this.r1z=NaN;
  2926. this.r2x=NaN;
  2927. this.r2y=NaN;
  2928. this.r2z=NaN;
  2929. this.t1x1=NaN;// jacobians
  2930. this.t1y1=NaN;
  2931. this.t1z1=NaN;
  2932. this.t2x1=NaN;
  2933. this.t2y1=NaN;
  2934. this.t2z1=NaN;
  2935. this.l1x1=NaN;
  2936. this.l1y1=NaN;
  2937. this.l1z1=NaN;
  2938. this.l2x1=NaN;
  2939. this.l2y1=NaN;
  2940. this.l2z1=NaN;
  2941. this.a1x1=NaN;
  2942. this.a1y1=NaN;
  2943. this.a1z1=NaN;
  2944. this.a2x1=NaN;
  2945. this.a2y1=NaN;
  2946. this.a2z1=NaN;
  2947. this.t1x2=NaN;
  2948. this.t1y2=NaN;
  2949. this.t1z2=NaN;
  2950. this.t2x2=NaN;
  2951. this.t2y2=NaN;
  2952. this.t2z2=NaN;
  2953. this.l1x2=NaN;
  2954. this.l1y2=NaN;
  2955. this.l1z2=NaN;
  2956. this.l2x2=NaN;
  2957. this.l2y2=NaN;
  2958. this.l2z2=NaN;
  2959. this.a1x2=NaN;
  2960. this.a1y2=NaN;
  2961. this.a1z2=NaN;
  2962. this.a2x2=NaN;
  2963. this.a2y2=NaN;
  2964. this.a2z2=NaN;
  2965. this.t1x3=NaN;
  2966. this.t1y3=NaN;
  2967. this.t1z3=NaN;
  2968. this.t2x3=NaN;
  2969. this.t2y3=NaN;
  2970. this.t2z3=NaN;
  2971. this.l1x3=NaN;
  2972. this.l1y3=NaN;
  2973. this.l1z3=NaN;
  2974. this.l2x3=NaN;
  2975. this.l2y3=NaN;
  2976. this.l2z3=NaN;
  2977. this.a1x3=NaN;
  2978. this.a1y3=NaN;
  2979. this.a1z3=NaN;
  2980. this.a2x3=NaN;
  2981. this.a2y3=NaN;
  2982. this.a2z3=NaN;
  2983. this.lowerLimit1=NaN;
  2984. this.upperLimit1=NaN;
  2985. this.limitVelocity1=NaN;
  2986. this.limitState1=0; // -1: at lower, 0: locked, 1: at upper, 2: unlimited
  2987. this.enableMotor1=false;
  2988. this.motorSpeed1=NaN;
  2989. this.maxMotorForce1=NaN;
  2990. this.maxMotorImpulse1=NaN;
  2991. this.lowerLimit2=NaN;
  2992. this.upperLimit2=NaN;
  2993. this.limitVelocity2=NaN;
  2994. this.limitState2=0; // -1: at lower, 0: locked, 1: at upper, 2: unlimited
  2995. this.enableMotor2=false;
  2996. this.motorSpeed2=NaN;
  2997. this.maxMotorForce2=NaN;
  2998. this.maxMotorImpulse2=NaN;
  2999. this.lowerLimit3=NaN;
  3000. this.upperLimit3=NaN;
  3001. this.limitVelocity3=NaN;
  3002. this.limitState3=0; // -1: at lower, 0: locked, 1: at upper, 2: unlimited
  3003. this.enableMotor3=false;
  3004. this.motorSpeed3=NaN;
  3005. this.maxMotorForce3=NaN;
  3006. this.maxMotorImpulse3=NaN;
  3007. this.k00=NaN; // K = J*M*JT
  3008. this.k01=NaN;
  3009. this.k02=NaN;
  3010. this.k10=NaN;
  3011. this.k11=NaN;
  3012. this.k12=NaN;
  3013. this.k20=NaN;
  3014. this.k21=NaN;
  3015. this.k22=NaN;
  3016. this.kv00=NaN; // diagonals without CFMs
  3017. this.kv11=NaN;
  3018. this.kv22=NaN;
  3019. this.dv00=NaN; // ...inverted
  3020. this.dv11=NaN;
  3021. this.dv22=NaN;
  3022. this.d00=NaN; // K^-1
  3023. this.d01=NaN;
  3024. this.d02=NaN;
  3025. this.d10=NaN;
  3026. this.d11=NaN;
  3027. this.d12=NaN;
  3028. this.d20=NaN;
  3029. this.d21=NaN;
  3030. this.d22=NaN;
  3031. this.limitMotor1=limitMotor1;
  3032. this.limitMotor2=limitMotor2;
  3033. this.limitMotor3=limitMotor3;
  3034. this.b1=joint.body1;
  3035. this.b2=joint.body2;
  3036. this.p1=joint.anchorPoint1;
  3037. this.p2=joint.anchorPoint2;
  3038. this.r1=joint.relativeAnchorPoint1;
  3039. this.r2=joint.relativeAnchorPoint2;
  3040. this.l1=this.b1.linearVelocity;
  3041. this.l2=this.b2.linearVelocity;
  3042. this.a1=this.b1.angularVelocity;
  3043. this.a2=this.b2.angularVelocity;
  3044. this.i1=this.b1.inverseInertia;
  3045. this.i2=this.b2.inverseInertia;
  3046. this.limitImpulse1=0;
  3047. this.motorImpulse1=0;
  3048. this.limitImpulse2=0;
  3049. this.motorImpulse2=0;
  3050. this.limitImpulse3=0;
  3051. this.motorImpulse3=0;
  3052. this.cfm1=0;// Constraint Force Mixing
  3053. this.cfm2=0;
  3054. this.cfm3=0;
  3055. this.weight=-1;
  3056. }
  3057. Object.assign( Translational3Constraint.prototype, {
  3058. Translational3Constraint: true,
  3059. preSolve:function(timeStep,invTimeStep){
  3060. this.ax1=this.limitMotor1.axis.x;
  3061. this.ay1=this.limitMotor1.axis.y;
  3062. this.az1=this.limitMotor1.axis.z;
  3063. this.ax2=this.limitMotor2.axis.x;
  3064. this.ay2=this.limitMotor2.axis.y;
  3065. this.az2=this.limitMotor2.axis.z;
  3066. this.ax3=this.limitMotor3.axis.x;
  3067. this.ay3=this.limitMotor3.axis.y;
  3068. this.az3=this.limitMotor3.axis.z;
  3069. this.lowerLimit1=this.limitMotor1.lowerLimit;
  3070. this.upperLimit1=this.limitMotor1.upperLimit;
  3071. this.motorSpeed1=this.limitMotor1.motorSpeed;
  3072. this.maxMotorForce1=this.limitMotor1.maxMotorForce;
  3073. this.enableMotor1=this.maxMotorForce1>0;
  3074. this.lowerLimit2=this.limitMotor2.lowerLimit;
  3075. this.upperLimit2=this.limitMotor2.upperLimit;
  3076. this.motorSpeed2=this.limitMotor2.motorSpeed;
  3077. this.maxMotorForce2=this.limitMotor2.maxMotorForce;
  3078. this.enableMotor2=this.maxMotorForce2>0;
  3079. this.lowerLimit3=this.limitMotor3.lowerLimit;
  3080. this.upperLimit3=this.limitMotor3.upperLimit;
  3081. this.motorSpeed3=this.limitMotor3.motorSpeed;
  3082. this.maxMotorForce3=this.limitMotor3.maxMotorForce;
  3083. this.enableMotor3=this.maxMotorForce3>0;
  3084. this.m1=this.b1.inverseMass;
  3085. this.m2=this.b2.inverseMass;
  3086. var ti1 = this.i1.elements;
  3087. var ti2 = this.i2.elements;
  3088. this.i1e00=ti1[0];
  3089. this.i1e01=ti1[1];
  3090. this.i1e02=ti1[2];
  3091. this.i1e10=ti1[3];
  3092. this.i1e11=ti1[4];
  3093. this.i1e12=ti1[5];
  3094. this.i1e20=ti1[6];
  3095. this.i1e21=ti1[7];
  3096. this.i1e22=ti1[8];
  3097. this.i2e00=ti2[0];
  3098. this.i2e01=ti2[1];
  3099. this.i2e02=ti2[2];
  3100. this.i2e10=ti2[3];
  3101. this.i2e11=ti2[4];
  3102. this.i2e12=ti2[5];
  3103. this.i2e20=ti2[6];
  3104. this.i2e21=ti2[7];
  3105. this.i2e22=ti2[8];
  3106. var dx=this.p2.x-this.p1.x;
  3107. var dy=this.p2.y-this.p1.y;
  3108. var dz=this.p2.z-this.p1.z;
  3109. var d1=dx*this.ax1+dy*this.ay1+dz*this.az1;
  3110. var d2=dx*this.ax2+dy*this.ay2+dz*this.az2;
  3111. var d3=dx*this.ax3+dy*this.ay3+dz*this.az3;
  3112. var frequency1=this.limitMotor1.frequency;
  3113. var frequency2=this.limitMotor2.frequency;
  3114. var frequency3=this.limitMotor3.frequency;
  3115. var enableSpring1=frequency1>0;
  3116. var enableSpring2=frequency2>0;
  3117. var enableSpring3=frequency3>0;
  3118. var enableLimit1=this.lowerLimit1<=this.upperLimit1;
  3119. var enableLimit2=this.lowerLimit2<=this.upperLimit2;
  3120. var enableLimit3=this.lowerLimit3<=this.upperLimit3;
  3121. // for stability
  3122. if(enableSpring1&&d1>20||d1<-20){
  3123. enableSpring1=false;
  3124. }
  3125. if(enableSpring2&&d2>20||d2<-20){
  3126. enableSpring2=false;
  3127. }
  3128. if(enableSpring3&&d3>20||d3<-20){
  3129. enableSpring3=false;
  3130. }
  3131. if(enableLimit1){
  3132. if(this.lowerLimit1==this.upperLimit1){
  3133. if(this.limitState1!=0){
  3134. this.limitState1=0;
  3135. this.limitImpulse1=0;
  3136. }
  3137. this.limitVelocity1=this.lowerLimit1-d1;
  3138. if(!enableSpring1)d1=this.lowerLimit1;
  3139. }else if(d1<this.lowerLimit1){
  3140. if(this.limitState1!=-1){
  3141. this.limitState1=-1;
  3142. this.limitImpulse1=0;
  3143. }
  3144. this.limitVelocity1=this.lowerLimit1-d1;
  3145. if(!enableSpring1)d1=this.lowerLimit1;
  3146. }else if(d1>this.upperLimit1){
  3147. if(this.limitState1!=1){
  3148. this.limitState1=1;
  3149. this.limitImpulse1=0;
  3150. }
  3151. this.limitVelocity1=this.upperLimit1-d1;
  3152. if(!enableSpring1)d1=this.upperLimit1;
  3153. }else{
  3154. this.limitState1=2;
  3155. this.limitImpulse1=0;
  3156. this.limitVelocity1=0;
  3157. }
  3158. if(!enableSpring1){
  3159. if(this.limitVelocity1>0.005)this.limitVelocity1-=0.005;
  3160. else if(this.limitVelocity1<-0.005)this.limitVelocity1+=0.005;
  3161. else this.limitVelocity1=0;
  3162. }
  3163. }else{
  3164. this.limitState1=2;
  3165. this.limitImpulse1=0;
  3166. }
  3167. if(enableLimit2){
  3168. if(this.lowerLimit2==this.upperLimit2){
  3169. if(this.limitState2!=0){
  3170. this.limitState2=0;
  3171. this.limitImpulse2=0;
  3172. }
  3173. this.limitVelocity2=this.lowerLimit2-d2;
  3174. if(!enableSpring2)d2=this.lowerLimit2;
  3175. }else if(d2<this.lowerLimit2){
  3176. if(this.limitState2!=-1){
  3177. this.limitState2=-1;
  3178. this.limitImpulse2=0;
  3179. }
  3180. this.limitVelocity2=this.lowerLimit2-d2;
  3181. if(!enableSpring2)d2=this.lowerLimit2;
  3182. }else if(d2>this.upperLimit2){
  3183. if(this.limitState2!=1){
  3184. this.limitState2=1;
  3185. this.limitImpulse2=0;
  3186. }
  3187. this.limitVelocity2=this.upperLimit2-d2;
  3188. if(!enableSpring2)d2=this.upperLimit2;
  3189. }else{
  3190. this.limitState2=2;
  3191. this.limitImpulse2=0;
  3192. this.limitVelocity2=0;
  3193. }
  3194. if(!enableSpring2){
  3195. if(this.limitVelocity2>0.005)this.limitVelocity2-=0.005;
  3196. else if(this.limitVelocity2<-0.005)this.limitVelocity2+=0.005;
  3197. else this.limitVelocity2=0;
  3198. }
  3199. }else{
  3200. this.limitState2=2;
  3201. this.limitImpulse2=0;
  3202. }
  3203. if(enableLimit3){
  3204. if(this.lowerLimit3==this.upperLimit3){
  3205. if(this.limitState3!=0){
  3206. this.limitState3=0;
  3207. this.limitImpulse3=0;
  3208. }
  3209. this.limitVelocity3=this.lowerLimit3-d3;
  3210. if(!enableSpring3)d3=this.lowerLimit3;
  3211. }else if(d3<this.lowerLimit3){
  3212. if(this.limitState3!=-1){
  3213. this.limitState3=-1;
  3214. this.limitImpulse3=0;
  3215. }
  3216. this.limitVelocity3=this.lowerLimit3-d3;
  3217. if(!enableSpring3)d3=this.lowerLimit3;
  3218. }else if(d3>this.upperLimit3){
  3219. if(this.limitState3!=1){
  3220. this.limitState3=1;
  3221. this.limitImpulse3=0;
  3222. }
  3223. this.limitVelocity3=this.upperLimit3-d3;
  3224. if(!enableSpring3)d3=this.upperLimit3;
  3225. }else{
  3226. this.limitState3=2;
  3227. this.limitImpulse3=0;
  3228. this.limitVelocity3=0;
  3229. }
  3230. if(!enableSpring3){
  3231. if(this.limitVelocity3>0.005)this.limitVelocity3-=0.005;
  3232. else if(this.limitVelocity3<-0.005)this.limitVelocity3+=0.005;
  3233. else this.limitVelocity3=0;
  3234. }
  3235. }else{
  3236. this.limitState3=2;
  3237. this.limitImpulse3=0;
  3238. }
  3239. if(this.enableMotor1&&(this.limitState1!=0||enableSpring1)){
  3240. this.maxMotorImpulse1=this.maxMotorForce1*timeStep;
  3241. }else{
  3242. this.motorImpulse1=0;
  3243. this.maxMotorImpulse1=0;
  3244. }
  3245. if(this.enableMotor2&&(this.limitState2!=0||enableSpring2)){
  3246. this.maxMotorImpulse2=this.maxMotorForce2*timeStep;
  3247. }else{
  3248. this.motorImpulse2=0;
  3249. this.maxMotorImpulse2=0;
  3250. }
  3251. if(this.enableMotor3&&(this.limitState3!=0||enableSpring3)){
  3252. this.maxMotorImpulse3=this.maxMotorForce3*timeStep;
  3253. }else{
  3254. this.motorImpulse3=0;
  3255. this.maxMotorImpulse3=0;
  3256. }
  3257. var rdx=d1*this.ax1+d2*this.ax2+d3*this.ax2;
  3258. var rdy=d1*this.ay1+d2*this.ay2+d3*this.ay2;
  3259. var rdz=d1*this.az1+d2*this.az2+d3*this.az2;
  3260. var w1=this.m2/(this.m1+this.m2);
  3261. if(this.weight>=0)w1=this.weight; // use given weight
  3262. var w2=1-w1;
  3263. this.r1x=this.r1.x+rdx*w1;
  3264. this.r1y=this.r1.y+rdy*w1;
  3265. this.r1z=this.r1.z+rdz*w1;
  3266. this.r2x=this.r2.x-rdx*w2;
  3267. this.r2y=this.r2.y-rdy*w2;
  3268. this.r2z=this.r2.z-rdz*w2;
  3269. // build jacobians
  3270. this.t1x1=this.r1y*this.az1-this.r1z*this.ay1;
  3271. this.t1y1=this.r1z*this.ax1-this.r1x*this.az1;
  3272. this.t1z1=this.r1x*this.ay1-this.r1y*this.ax1;
  3273. this.t2x1=this.r2y*this.az1-this.r2z*this.ay1;
  3274. this.t2y1=this.r2z*this.ax1-this.r2x*this.az1;
  3275. this.t2z1=this.r2x*this.ay1-this.r2y*this.ax1;
  3276. this.l1x1=this.ax1*this.m1;
  3277. this.l1y1=this.ay1*this.m1;
  3278. this.l1z1=this.az1*this.m1;
  3279. this.l2x1=this.ax1*this.m2;
  3280. this.l2y1=this.ay1*this.m2;
  3281. this.l2z1=this.az1*this.m2;
  3282. this.a1x1=this.t1x1*this.i1e00+this.t1y1*this.i1e01+this.t1z1*this.i1e02;
  3283. this.a1y1=this.t1x1*this.i1e10+this.t1y1*this.i1e11+this.t1z1*this.i1e12;
  3284. this.a1z1=this.t1x1*this.i1e20+this.t1y1*this.i1e21+this.t1z1*this.i1e22;
  3285. this.a2x1=this.t2x1*this.i2e00+this.t2y1*this.i2e01+this.t2z1*this.i2e02;
  3286. this.a2y1=this.t2x1*this.i2e10+this.t2y1*this.i2e11+this.t2z1*this.i2e12;
  3287. this.a2z1=this.t2x1*this.i2e20+this.t2y1*this.i2e21+this.t2z1*this.i2e22;
  3288. this.t1x2=this.r1y*this.az2-this.r1z*this.ay2;
  3289. this.t1y2=this.r1z*this.ax2-this.r1x*this.az2;
  3290. this.t1z2=this.r1x*this.ay2-this.r1y*this.ax2;
  3291. this.t2x2=this.r2y*this.az2-this.r2z*this.ay2;
  3292. this.t2y2=this.r2z*this.ax2-this.r2x*this.az2;
  3293. this.t2z2=this.r2x*this.ay2-this.r2y*this.ax2;
  3294. this.l1x2=this.ax2*this.m1;
  3295. this.l1y2=this.ay2*this.m1;
  3296. this.l1z2=this.az2*this.m1;
  3297. this.l2x2=this.ax2*this.m2;
  3298. this.l2y2=this.ay2*this.m2;
  3299. this.l2z2=this.az2*this.m2;
  3300. this.a1x2=this.t1x2*this.i1e00+this.t1y2*this.i1e01+this.t1z2*this.i1e02;
  3301. this.a1y2=this.t1x2*this.i1e10+this.t1y2*this.i1e11+this.t1z2*this.i1e12;
  3302. this.a1z2=this.t1x2*this.i1e20+this.t1y2*this.i1e21+this.t1z2*this.i1e22;
  3303. this.a2x2=this.t2x2*this.i2e00+this.t2y2*this.i2e01+this.t2z2*this.i2e02;
  3304. this.a2y2=this.t2x2*this.i2e10+this.t2y2*this.i2e11+this.t2z2*this.i2e12;
  3305. this.a2z2=this.t2x2*this.i2e20+this.t2y2*this.i2e21+this.t2z2*this.i2e22;
  3306. this.t1x3=this.r1y*this.az3-this.r1z*this.ay3;
  3307. this.t1y3=this.r1z*this.ax3-this.r1x*this.az3;
  3308. this.t1z3=this.r1x*this.ay3-this.r1y*this.ax3;
  3309. this.t2x3=this.r2y*this.az3-this.r2z*this.ay3;
  3310. this.t2y3=this.r2z*this.ax3-this.r2x*this.az3;
  3311. this.t2z3=this.r2x*this.ay3-this.r2y*this.ax3;
  3312. this.l1x3=this.ax3*this.m1;
  3313. this.l1y3=this.ay3*this.m1;
  3314. this.l1z3=this.az3*this.m1;
  3315. this.l2x3=this.ax3*this.m2;
  3316. this.l2y3=this.ay3*this.m2;
  3317. this.l2z3=this.az3*this.m2;
  3318. this.a1x3=this.t1x3*this.i1e00+this.t1y3*this.i1e01+this.t1z3*this.i1e02;
  3319. this.a1y3=this.t1x3*this.i1e10+this.t1y3*this.i1e11+this.t1z3*this.i1e12;
  3320. this.a1z3=this.t1x3*this.i1e20+this.t1y3*this.i1e21+this.t1z3*this.i1e22;
  3321. this.a2x3=this.t2x3*this.i2e00+this.t2y3*this.i2e01+this.t2z3*this.i2e02;
  3322. this.a2y3=this.t2x3*this.i2e10+this.t2y3*this.i2e11+this.t2z3*this.i2e12;
  3323. this.a2z3=this.t2x3*this.i2e20+this.t2y3*this.i2e21+this.t2z3*this.i2e22;
  3324. // build an impulse matrix
  3325. var m12=this.m1+this.m2;
  3326. this.k00=(this.ax1*this.ax1+this.ay1*this.ay1+this.az1*this.az1)*m12;
  3327. this.k01=(this.ax1*this.ax2+this.ay1*this.ay2+this.az1*this.az2)*m12;
  3328. this.k02=(this.ax1*this.ax3+this.ay1*this.ay3+this.az1*this.az3)*m12;
  3329. this.k10=(this.ax2*this.ax1+this.ay2*this.ay1+this.az2*this.az1)*m12;
  3330. this.k11=(this.ax2*this.ax2+this.ay2*this.ay2+this.az2*this.az2)*m12;
  3331. this.k12=(this.ax2*this.ax3+this.ay2*this.ay3+this.az2*this.az3)*m12;
  3332. this.k20=(this.ax3*this.ax1+this.ay3*this.ay1+this.az3*this.az1)*m12;
  3333. this.k21=(this.ax3*this.ax2+this.ay3*this.ay2+this.az3*this.az2)*m12;
  3334. this.k22=(this.ax3*this.ax3+this.ay3*this.ay3+this.az3*this.az3)*m12;
  3335. this.k00+=this.t1x1*this.a1x1+this.t1y1*this.a1y1+this.t1z1*this.a1z1;
  3336. this.k01+=this.t1x1*this.a1x2+this.t1y1*this.a1y2+this.t1z1*this.a1z2;
  3337. this.k02+=this.t1x1*this.a1x3+this.t1y1*this.a1y3+this.t1z1*this.a1z3;
  3338. this.k10+=this.t1x2*this.a1x1+this.t1y2*this.a1y1+this.t1z2*this.a1z1;
  3339. this.k11+=this.t1x2*this.a1x2+this.t1y2*this.a1y2+this.t1z2*this.a1z2;
  3340. this.k12+=this.t1x2*this.a1x3+this.t1y2*this.a1y3+this.t1z2*this.a1z3;
  3341. this.k20+=this.t1x3*this.a1x1+this.t1y3*this.a1y1+this.t1z3*this.a1z1;
  3342. this.k21+=this.t1x3*this.a1x2+this.t1y3*this.a1y2+this.t1z3*this.a1z2;
  3343. this.k22+=this.t1x3*this.a1x3+this.t1y3*this.a1y3+this.t1z3*this.a1z3;
  3344. this.k00+=this.t2x1*this.a2x1+this.t2y1*this.a2y1+this.t2z1*this.a2z1;
  3345. this.k01+=this.t2x1*this.a2x2+this.t2y1*this.a2y2+this.t2z1*this.a2z2;
  3346. this.k02+=this.t2x1*this.a2x3+this.t2y1*this.a2y3+this.t2z1*this.a2z3;
  3347. this.k10+=this.t2x2*this.a2x1+this.t2y2*this.a2y1+this.t2z2*this.a2z1;
  3348. this.k11+=this.t2x2*this.a2x2+this.t2y2*this.a2y2+this.t2z2*this.a2z2;
  3349. this.k12+=this.t2x2*this.a2x3+this.t2y2*this.a2y3+this.t2z2*this.a2z3;
  3350. this.k20+=this.t2x3*this.a2x1+this.t2y3*this.a2y1+this.t2z3*this.a2z1;
  3351. this.k21+=this.t2x3*this.a2x2+this.t2y3*this.a2y2+this.t2z3*this.a2z2;
  3352. this.k22+=this.t2x3*this.a2x3+this.t2y3*this.a2y3+this.t2z3*this.a2z3;
  3353. this.kv00=this.k00;
  3354. this.kv11=this.k11;
  3355. this.kv22=this.k22;
  3356. this.dv00=1/this.kv00;
  3357. this.dv11=1/this.kv11;
  3358. this.dv22=1/this.kv22;
  3359. if(enableSpring1&&this.limitState1!=2){
  3360. var omega=6.2831853*frequency1;
  3361. var k=omega*omega*timeStep;
  3362. var dmp=invTimeStep/(k+2*this.limitMotor1.dampingRatio*omega);
  3363. this.cfm1=this.kv00*dmp;
  3364. this.limitVelocity1*=k*dmp;
  3365. }else{
  3366. this.cfm1=0;
  3367. this.limitVelocity1*=invTimeStep*0.05;
  3368. }
  3369. if(enableSpring2&&this.limitState2!=2){
  3370. omega=6.2831853*frequency2;
  3371. k=omega*omega*timeStep;
  3372. dmp=invTimeStep/(k+2*this.limitMotor2.dampingRatio*omega);
  3373. this.cfm2=this.kv11*dmp;
  3374. this.limitVelocity2*=k*dmp;
  3375. }else{
  3376. this.cfm2=0;
  3377. this.limitVelocity2*=invTimeStep*0.05;
  3378. }
  3379. if(enableSpring3&&this.limitState3!=2){
  3380. omega=6.2831853*frequency3;
  3381. k=omega*omega*timeStep;
  3382. dmp=invTimeStep/(k+2*this.limitMotor3.dampingRatio*omega);
  3383. this.cfm3=this.kv22*dmp;
  3384. this.limitVelocity3*=k*dmp;
  3385. }else{
  3386. this.cfm3=0;
  3387. this.limitVelocity3*=invTimeStep*0.05;
  3388. }
  3389. this.k00+=this.cfm1;
  3390. this.k11+=this.cfm2;
  3391. this.k22+=this.cfm3;
  3392. var inv=1/(
  3393. this.k00*(this.k11*this.k22-this.k21*this.k12)+
  3394. this.k10*(this.k21*this.k02-this.k01*this.k22)+
  3395. this.k20*(this.k01*this.k12-this.k11*this.k02)
  3396. );
  3397. this.d00=(this.k11*this.k22-this.k12*this.k21)*inv;
  3398. this.d01=(this.k02*this.k21-this.k01*this.k22)*inv;
  3399. this.d02=(this.k01*this.k12-this.k02*this.k11)*inv;
  3400. this.d10=(this.k12*this.k20-this.k10*this.k22)*inv;
  3401. this.d11=(this.k00*this.k22-this.k02*this.k20)*inv;
  3402. this.d12=(this.k02*this.k10-this.k00*this.k12)*inv;
  3403. this.d20=(this.k10*this.k21-this.k11*this.k20)*inv;
  3404. this.d21=(this.k01*this.k20-this.k00*this.k21)*inv;
  3405. this.d22=(this.k00*this.k11-this.k01*this.k10)*inv;
  3406. // warm starting
  3407. var totalImpulse1=this.limitImpulse1+this.motorImpulse1;
  3408. var totalImpulse2=this.limitImpulse2+this.motorImpulse2;
  3409. var totalImpulse3=this.limitImpulse3+this.motorImpulse3;
  3410. this.l1.x+=totalImpulse1*this.l1x1+totalImpulse2*this.l1x2+totalImpulse3*this.l1x3;
  3411. this.l1.y+=totalImpulse1*this.l1y1+totalImpulse2*this.l1y2+totalImpulse3*this.l1y3;
  3412. this.l1.z+=totalImpulse1*this.l1z1+totalImpulse2*this.l1z2+totalImpulse3*this.l1z3;
  3413. this.a1.x+=totalImpulse1*this.a1x1+totalImpulse2*this.a1x2+totalImpulse3*this.a1x3;
  3414. this.a1.y+=totalImpulse1*this.a1y1+totalImpulse2*this.a1y2+totalImpulse3*this.a1y3;
  3415. this.a1.z+=totalImpulse1*this.a1z1+totalImpulse2*this.a1z2+totalImpulse3*this.a1z3;
  3416. this.l2.x-=totalImpulse1*this.l2x1+totalImpulse2*this.l2x2+totalImpulse3*this.l2x3;
  3417. this.l2.y-=totalImpulse1*this.l2y1+totalImpulse2*this.l2y2+totalImpulse3*this.l2y3;
  3418. this.l2.z-=totalImpulse1*this.l2z1+totalImpulse2*this.l2z2+totalImpulse3*this.l2z3;
  3419. this.a2.x-=totalImpulse1*this.a2x1+totalImpulse2*this.a2x2+totalImpulse3*this.a2x3;
  3420. this.a2.y-=totalImpulse1*this.a2y1+totalImpulse2*this.a2y2+totalImpulse3*this.a2y3;
  3421. this.a2.z-=totalImpulse1*this.a2z1+totalImpulse2*this.a2z2+totalImpulse3*this.a2z3;
  3422. },
  3423. solve:function(){
  3424. var rvx=this.l2.x-this.l1.x+this.a2.y*this.r2z-this.a2.z*this.r2y-this.a1.y*this.r1z+this.a1.z*this.r1y;
  3425. var rvy=this.l2.y-this.l1.y+this.a2.z*this.r2x-this.a2.x*this.r2z-this.a1.z*this.r1x+this.a1.x*this.r1z;
  3426. var rvz=this.l2.z-this.l1.z+this.a2.x*this.r2y-this.a2.y*this.r2x-this.a1.x*this.r1y+this.a1.y*this.r1x;
  3427. var rvn1=rvx*this.ax1+rvy*this.ay1+rvz*this.az1;
  3428. var rvn2=rvx*this.ax2+rvy*this.ay2+rvz*this.az2;
  3429. var rvn3=rvx*this.ax3+rvy*this.ay3+rvz*this.az3;
  3430. var oldMotorImpulse1=this.motorImpulse1;
  3431. var oldMotorImpulse2=this.motorImpulse2;
  3432. var oldMotorImpulse3=this.motorImpulse3;
  3433. var dMotorImpulse1=0;
  3434. var dMotorImpulse2=0;
  3435. var dMotorImpulse3=0;
  3436. if(this.enableMotor1){
  3437. dMotorImpulse1=(rvn1-this.motorSpeed1)*this.dv00;
  3438. this.motorImpulse1+=dMotorImpulse1;
  3439. if(this.motorImpulse1>this.maxMotorImpulse1){ // clamp motor impulse
  3440. this.motorImpulse1=this.maxMotorImpulse1;
  3441. }else if(this.motorImpulse1<-this.maxMotorImpulse1){
  3442. this.motorImpulse1=-this.maxMotorImpulse1;
  3443. }
  3444. dMotorImpulse1=this.motorImpulse1-oldMotorImpulse1;
  3445. }
  3446. if(this.enableMotor2){
  3447. dMotorImpulse2=(rvn2-this.motorSpeed2)*this.dv11;
  3448. this.motorImpulse2+=dMotorImpulse2;
  3449. if(this.motorImpulse2>this.maxMotorImpulse2){ // clamp motor impulse
  3450. this.motorImpulse2=this.maxMotorImpulse2;
  3451. }else if(this.motorImpulse2<-this.maxMotorImpulse2){
  3452. this.motorImpulse2=-this.maxMotorImpulse2;
  3453. }
  3454. dMotorImpulse2=this.motorImpulse2-oldMotorImpulse2;
  3455. }
  3456. if(this.enableMotor3){
  3457. dMotorImpulse3=(rvn3-this.motorSpeed3)*this.dv22;
  3458. this.motorImpulse3+=dMotorImpulse3;
  3459. if(this.motorImpulse3>this.maxMotorImpulse3){ // clamp motor impulse
  3460. this.motorImpulse3=this.maxMotorImpulse3;
  3461. }else if(this.motorImpulse3<-this.maxMotorImpulse3){
  3462. this.motorImpulse3=-this.maxMotorImpulse3;
  3463. }
  3464. dMotorImpulse3=this.motorImpulse3-oldMotorImpulse3;
  3465. }
  3466. // apply motor impulse to relative velocity
  3467. rvn1+=dMotorImpulse1*this.kv00+dMotorImpulse2*this.k01+dMotorImpulse3*this.k02;
  3468. rvn2+=dMotorImpulse1*this.k10+dMotorImpulse2*this.kv11+dMotorImpulse3*this.k12;
  3469. rvn3+=dMotorImpulse1*this.k20+dMotorImpulse2*this.k21+dMotorImpulse3*this.kv22;
  3470. // subtract target velocity and applied impulse
  3471. rvn1-=this.limitVelocity1+this.limitImpulse1*this.cfm1;
  3472. rvn2-=this.limitVelocity2+this.limitImpulse2*this.cfm2;
  3473. rvn3-=this.limitVelocity3+this.limitImpulse3*this.cfm3;
  3474. var oldLimitImpulse1=this.limitImpulse1;
  3475. var oldLimitImpulse2=this.limitImpulse2;
  3476. var oldLimitImpulse3=this.limitImpulse3;
  3477. var dLimitImpulse1=rvn1*this.d00+rvn2*this.d01+rvn3*this.d02;
  3478. var dLimitImpulse2=rvn1*this.d10+rvn2*this.d11+rvn3*this.d12;
  3479. var dLimitImpulse3=rvn1*this.d20+rvn2*this.d21+rvn3*this.d22;
  3480. this.limitImpulse1+=dLimitImpulse1;
  3481. this.limitImpulse2+=dLimitImpulse2;
  3482. this.limitImpulse3+=dLimitImpulse3;
  3483. // clamp
  3484. var clampState=0;
  3485. if(this.limitState1==2||this.limitImpulse1*this.limitState1<0){
  3486. dLimitImpulse1=-oldLimitImpulse1;
  3487. rvn2+=dLimitImpulse1*this.k10;
  3488. rvn3+=dLimitImpulse1*this.k20;
  3489. clampState|=1;
  3490. }
  3491. if(this.limitState2==2||this.limitImpulse2*this.limitState2<0){
  3492. dLimitImpulse2=-oldLimitImpulse2;
  3493. rvn1+=dLimitImpulse2*this.k01;
  3494. rvn3+=dLimitImpulse2*this.k21;
  3495. clampState|=2;
  3496. }
  3497. if(this.limitState3==2||this.limitImpulse3*this.limitState3<0){
  3498. dLimitImpulse3=-oldLimitImpulse3;
  3499. rvn1+=dLimitImpulse3*this.k02;
  3500. rvn2+=dLimitImpulse3*this.k12;
  3501. clampState|=4;
  3502. }
  3503. // update un-clamped impulse
  3504. // TODO: isolate division
  3505. var det;
  3506. switch(clampState){
  3507. case 1:// update 2 3
  3508. det=1/(this.k11*this.k22-this.k12*this.k21);
  3509. dLimitImpulse2=(this.k22*rvn2+-this.k12*rvn3)*det;
  3510. dLimitImpulse3=(-this.k21*rvn2+this.k11*rvn3)*det;
  3511. break;
  3512. case 2:// update 1 3
  3513. det=1/(this.k00*this.k22-this.k02*this.k20);
  3514. dLimitImpulse1=(this.k22*rvn1+-this.k02*rvn3)*det;
  3515. dLimitImpulse3=(-this.k20*rvn1+this.k00*rvn3)*det;
  3516. break;
  3517. case 3:// update 3
  3518. dLimitImpulse3=rvn3/this.k22;
  3519. break;
  3520. case 4:// update 1 2
  3521. det=1/(this.k00*this.k11-this.k01*this.k10);
  3522. dLimitImpulse1=(this.k11*rvn1+-this.k01*rvn2)*det;
  3523. dLimitImpulse2=(-this.k10*rvn1+this.k00*rvn2)*det;
  3524. break;
  3525. case 5:// update 2
  3526. dLimitImpulse2=rvn2/this.k11;
  3527. break;
  3528. case 6:// update 1
  3529. dLimitImpulse1=rvn1/this.k00;
  3530. break;
  3531. }
  3532. this.limitImpulse1=oldLimitImpulse1+dLimitImpulse1;
  3533. this.limitImpulse2=oldLimitImpulse2+dLimitImpulse2;
  3534. this.limitImpulse3=oldLimitImpulse3+dLimitImpulse3;
  3535. var dImpulse1=dMotorImpulse1+dLimitImpulse1;
  3536. var dImpulse2=dMotorImpulse2+dLimitImpulse2;
  3537. var dImpulse3=dMotorImpulse3+dLimitImpulse3;
  3538. // apply impulse
  3539. this.l1.x+=dImpulse1*this.l1x1+dImpulse2*this.l1x2+dImpulse3*this.l1x3;
  3540. this.l1.y+=dImpulse1*this.l1y1+dImpulse2*this.l1y2+dImpulse3*this.l1y3;
  3541. this.l1.z+=dImpulse1*this.l1z1+dImpulse2*this.l1z2+dImpulse3*this.l1z3;
  3542. this.a1.x+=dImpulse1*this.a1x1+dImpulse2*this.a1x2+dImpulse3*this.a1x3;
  3543. this.a1.y+=dImpulse1*this.a1y1+dImpulse2*this.a1y2+dImpulse3*this.a1y3;
  3544. this.a1.z+=dImpulse1*this.a1z1+dImpulse2*this.a1z2+dImpulse3*this.a1z3;
  3545. this.l2.x-=dImpulse1*this.l2x1+dImpulse2*this.l2x2+dImpulse3*this.l2x3;
  3546. this.l2.y-=dImpulse1*this.l2y1+dImpulse2*this.l2y2+dImpulse3*this.l2y3;
  3547. this.l2.z-=dImpulse1*this.l2z1+dImpulse2*this.l2z2+dImpulse3*this.l2z3;
  3548. this.a2.x-=dImpulse1*this.a2x1+dImpulse2*this.a2x2+dImpulse3*this.a2x3;
  3549. this.a2.y-=dImpulse1*this.a2y1+dImpulse2*this.a2y2+dImpulse3*this.a2y3;
  3550. this.a2.z-=dImpulse1*this.a2z1+dImpulse2*this.a2z2+dImpulse3*this.a2z3;
  3551. }
  3552. } );
  3553. /**
  3554. * A prismatic joint allows only for relative translation of rigid bodies along the axis.
  3555. *
  3556. * @author saharan
  3557. * @author lo-th
  3558. */
  3559. function PrismaticJoint( config, lowerTranslation, upperTranslation ){
  3560. Joint.call( this, config );
  3561. this.type = JOINT_PRISMATIC;
  3562. // The axis in the first body's coordinate system.
  3563. this.localAxis1 = config.localAxis1.clone().normalize();
  3564. // The axis in the second body's coordinate system.
  3565. this.localAxis2 = config.localAxis2.clone().normalize();
  3566. this.ax1 = new Vec3();
  3567. this.ax2 = new Vec3();
  3568. this.nor = new Vec3();
  3569. this.tan = new Vec3();
  3570. this.bin = new Vec3();
  3571. this.ac = new AngularConstraint( this, new Quat().setFromUnitVectors( this.localAxis1, this.localAxis2 ) );
  3572. // The translational limit and motor information of the joint.
  3573. this.limitMotor = new LimitMotor( this.nor, true );
  3574. this.limitMotor.lowerLimit = lowerTranslation;
  3575. this.limitMotor.upperLimit = upperTranslation;
  3576. this.t3 = new Translational3Constraint( this, this.limitMotor, new LimitMotor( this.tan, true ), new LimitMotor( this.bin, true ) );
  3577. }
  3578. PrismaticJoint.prototype = Object.assign( Object.create( Joint.prototype ), {
  3579. constructor: PrismaticJoint,
  3580. preSolve: function ( timeStep, invTimeStep ) {
  3581. this.updateAnchorPoints();
  3582. this.ax1.copy( this.localAxis1 ).applyMatrix3( this.body1.rotation, true );
  3583. this.ax2.copy( this.localAxis2 ).applyMatrix3( this.body2.rotation, true );
  3584. // normal tangent binormal
  3585. this.nor.set(
  3586. this.ax1.x*this.body2.inverseMass + this.ax2.x*this.body1.inverseMass,
  3587. this.ax1.y*this.body2.inverseMass + this.ax2.y*this.body1.inverseMass,
  3588. this.ax1.z*this.body2.inverseMass + this.ax2.z*this.body1.inverseMass
  3589. ).normalize();
  3590. this.tan.tangent( this.nor ).normalize();
  3591. this.bin.crossVectors( this.nor, this.tan );
  3592. // preSolve
  3593. this.ac.preSolve( timeStep, invTimeStep );
  3594. this.t3.preSolve( timeStep, invTimeStep );
  3595. },
  3596. solve: function () {
  3597. this.ac.solve();
  3598. this.t3.solve();
  3599. },
  3600. postSolve: function () {
  3601. }
  3602. });
  3603. /**
  3604. * A slider joint allows for relative translation and relative rotation between two rigid bodies along the axis.
  3605. *
  3606. * @author saharan
  3607. * @author lo-th
  3608. */
  3609. function SliderJoint( config, lowerTranslation, upperTranslation ){
  3610. Joint.call( this, config );
  3611. this.type = JOINT_SLIDER;
  3612. // The axis in the first body's coordinate system.
  3613. this.localAxis1 = config.localAxis1.clone().normalize();
  3614. // The axis in the second body's coordinate system.
  3615. this.localAxis2 = config.localAxis2.clone().normalize();
  3616. // make angle axis
  3617. var arc = new Mat33().setQuat( new Quat().setFromUnitVectors( this.localAxis1, this.localAxis2 ) );
  3618. this.localAngle1 = new Vec3().tangent( this.localAxis1 ).normalize();
  3619. this.localAngle2 = this.localAngle1.clone().applyMatrix3( arc, true );
  3620. this.ax1 = new Vec3();
  3621. this.ax2 = new Vec3();
  3622. this.an1 = new Vec3();
  3623. this.an2 = new Vec3();
  3624. this.tmp = new Vec3();
  3625. this.nor = new Vec3();
  3626. this.tan = new Vec3();
  3627. this.bin = new Vec3();
  3628. // The limit and motor for the rotation
  3629. this.rotationalLimitMotor = new LimitMotor( this.nor, false );
  3630. this.r3 = new Rotational3Constraint( this, this.rotationalLimitMotor, new LimitMotor( this.tan, true ), new LimitMotor( this.bin, true ) );
  3631. // The limit and motor for the translation.
  3632. this.translationalLimitMotor = new LimitMotor( this.nor, true );
  3633. this.translationalLimitMotor.lowerLimit = lowerTranslation;
  3634. this.translationalLimitMotor.upperLimit = upperTranslation;
  3635. this.t3 = new Translational3Constraint( this, this.translationalLimitMotor, new LimitMotor( this.tan, true ), new LimitMotor( this.bin, true ) );
  3636. }
  3637. SliderJoint.prototype = Object.assign( Object.create( Joint.prototype ), {
  3638. constructor: SliderJoint,
  3639. preSolve: function ( timeStep, invTimeStep ) {
  3640. this.updateAnchorPoints();
  3641. this.ax1.copy( this.localAxis1 ).applyMatrix3( this.body1.rotation, true );
  3642. this.an1.copy( this.localAngle1 ).applyMatrix3( this.body1.rotation, true );
  3643. this.ax2.copy( this.localAxis2 ).applyMatrix3( this.body2.rotation, true );
  3644. this.an2.copy( this.localAngle2 ).applyMatrix3( this.body2.rotation, true );
  3645. // normal tangent binormal
  3646. this.nor.set(
  3647. this.ax1.x*this.body2.inverseMass + this.ax2.x*this.body1.inverseMass,
  3648. this.ax1.y*this.body2.inverseMass + this.ax2.y*this.body1.inverseMass,
  3649. this.ax1.z*this.body2.inverseMass + this.ax2.z*this.body1.inverseMass
  3650. ).normalize();
  3651. this.tan.tangent( this.nor ).normalize();
  3652. this.bin.crossVectors( this.nor, this.tan );
  3653. // calculate hinge angle
  3654. this.tmp.crossVectors( this.an1, this.an2 );
  3655. var limite = _Math.acosClamp( _Math.dotVectors( this.an1, this.an2 ) );
  3656. if( _Math.dotVectors( this.nor, this.tmp ) < 0 ) this.rotationalLimitMotor.angle = -limite;
  3657. else this.rotationalLimitMotor.angle = limite;
  3658. // angular error
  3659. this.tmp.crossVectors( this.ax1, this.ax2 );
  3660. this.r3.limitMotor2.angle = _Math.dotVectors( this.tan, this.tmp );
  3661. this.r3.limitMotor3.angle = _Math.dotVectors( this.bin, this.tmp );
  3662. // preSolve
  3663. this.r3.preSolve( timeStep, invTimeStep );
  3664. this.t3.preSolve( timeStep, invTimeStep );
  3665. },
  3666. solve: function () {
  3667. this.r3.solve();
  3668. this.t3.solve();
  3669. },
  3670. postSolve: function () {
  3671. }
  3672. });
  3673. /**
  3674. * A wheel joint allows for relative rotation between two rigid bodies along two axes.
  3675. * The wheel joint also allows for relative translation for the suspension.
  3676. *
  3677. * @author saharan
  3678. * @author lo-th
  3679. */
  3680. function WheelJoint ( config ){
  3681. Joint.call( this, config );
  3682. this.type = JOINT_WHEEL;
  3683. // The axis in the first body's coordinate system.
  3684. this.localAxis1 = config.localAxis1.clone().normalize();
  3685. // The axis in the second body's coordinate system.
  3686. this.localAxis2 = config.localAxis2.clone().normalize();
  3687. this.localAngle1 = new Vec3();
  3688. this.localAngle2 = new Vec3();
  3689. var dot = _Math.dotVectors( this.localAxis1, this.localAxis2 );
  3690. if( dot > -1 && dot < 1 ){
  3691. this.localAngle1.set(
  3692. this.localAxis2.x - dot*this.localAxis1.x,
  3693. this.localAxis2.y - dot*this.localAxis1.y,
  3694. this.localAxis2.z - dot*this.localAxis1.z
  3695. ).normalize();
  3696. this.localAngle2.set(
  3697. this.localAxis1.x - dot*this.localAxis2.x,
  3698. this.localAxis1.y - dot*this.localAxis2.y,
  3699. this.localAxis1.z - dot*this.localAxis2.z
  3700. ).normalize();
  3701. } else {
  3702. var arc = new Mat33().setQuat( new Quat().setFromUnitVectors( this.localAxis1, this.localAxis2 ) );
  3703. this.localAngle1.tangent( this.localAxis1 ).normalize();
  3704. this.localAngle2 = this.localAngle1.clone().applyMatrix3( arc, true );
  3705. }
  3706. this.ax1 = new Vec3();
  3707. this.ax2 = new Vec3();
  3708. this.an1 = new Vec3();
  3709. this.an2 = new Vec3();
  3710. this.tmp = new Vec3();
  3711. this.nor = new Vec3();
  3712. this.tan = new Vec3();
  3713. this.bin = new Vec3();
  3714. // The translational limit and motor information of the joint.
  3715. this.translationalLimitMotor = new LimitMotor( this.tan,true );
  3716. this.translationalLimitMotor.frequency = 8;
  3717. this.translationalLimitMotor.dampingRatio = 1;
  3718. // The first rotational limit and motor information of the joint.
  3719. this.rotationalLimitMotor1 = new LimitMotor( this.tan, false );
  3720. // The second rotational limit and motor information of the joint.
  3721. this.rotationalLimitMotor2 = new LimitMotor( this.bin, false );
  3722. this.t3 = new Translational3Constraint( this, new LimitMotor( this.nor, true ),this.translationalLimitMotor,new LimitMotor( this.bin, true ));
  3723. this.t3.weight = 1;
  3724. this.r3 = new Rotational3Constraint(this,new LimitMotor( this.nor, true ),this.rotationalLimitMotor1,this.rotationalLimitMotor2);
  3725. }
  3726. WheelJoint.prototype = Object.assign( Object.create( Joint.prototype ), {
  3727. constructor: WheelJoint,
  3728. preSolve: function ( timeStep, invTimeStep ) {
  3729. this.updateAnchorPoints();
  3730. this.ax1.copy( this.localAxis1 ).applyMatrix3( this.body1.rotation, true );
  3731. this.an1.copy( this.localAngle1 ).applyMatrix3( this.body1.rotation, true );
  3732. this.ax2.copy( this.localAxis2 ).applyMatrix3( this.body2.rotation, true );
  3733. this.an2.copy( this.localAngle2 ).applyMatrix3( this.body2.rotation, true );
  3734. this.r3.limitMotor1.angle = _Math.dotVectors( this.ax1, this.ax2 );
  3735. var limite = _Math.dotVectors( this.an1, this.ax2 );
  3736. if( _Math.dotVectors( this.ax1, this.tmp.crossVectors( this.an1, this.ax2 ) ) < 0 ) this.rotationalLimitMotor1.angle = -limite;
  3737. else this.rotationalLimitMotor1.angle = limite;
  3738. limite = _Math.dotVectors( this.an2, this.ax1 );
  3739. if( _Math.dotVectors( this.ax2, this.tmp.crossVectors( this.an2, this.ax1 ) ) < 0 ) this.rotationalLimitMotor2.angle = -limite;
  3740. else this.rotationalLimitMotor2.angle = limite;
  3741. this.nor.crossVectors( this.ax1, this.ax2 ).normalize();
  3742. this.tan.crossVectors( this.nor, this.ax2 ).normalize();
  3743. this.bin.crossVectors( this.nor, this.ax1 ).normalize();
  3744. this.r3.preSolve(timeStep,invTimeStep);
  3745. this.t3.preSolve(timeStep,invTimeStep);
  3746. },
  3747. solve: function () {
  3748. this.r3.solve();
  3749. this.t3.solve();
  3750. },
  3751. postSolve: function () {
  3752. }
  3753. });
  3754. function JointConfig(){
  3755. this.scale = 1;
  3756. this.invScale = 1;
  3757. // The first rigid body of the joint.
  3758. this.body1 = null;
  3759. // The second rigid body of the joint.
  3760. this.body2 = null;
  3761. // The anchor point on the first rigid body in local coordinate system.
  3762. this.localAnchorPoint1 = new Vec3();
  3763. // The anchor point on the second rigid body in local coordinate system.
  3764. this.localAnchorPoint2 = new Vec3();
  3765. // The axis in the first body's coordinate system.
  3766. // his property is available in some joints.
  3767. this.localAxis1 = new Vec3();
  3768. // The axis in the second body's coordinate system.
  3769. // This property is available in some joints.
  3770. this.localAxis2 = new Vec3();
  3771. // Whether allow collision between connected rigid bodies or not.
  3772. this.allowCollision = false;
  3773. }
  3774. /**
  3775. * This class holds mass information of a shape.
  3776. * @author lo-th
  3777. * @author saharan
  3778. */
  3779. function MassInfo (){
  3780. // Mass of the shape.
  3781. this.mass = 0;
  3782. // The moment inertia of the shape.
  3783. this.inertia = new Mat33();
  3784. }
  3785. /**
  3786. * A link list of contacts.
  3787. * @author saharan
  3788. */
  3789. function ContactLink ( contact ){
  3790. // The previous contact link.
  3791. this.prev = null;
  3792. // The next contact link.
  3793. this.next = null;
  3794. // The shape of the contact.
  3795. this.shape = null;
  3796. // The other rigid body.
  3797. this.body = null;
  3798. // The contact of the link.
  3799. this.contact = contact;
  3800. }
  3801. function ImpulseDataBuffer (){
  3802. this.lp1X = NaN;
  3803. this.lp1Y = NaN;
  3804. this.lp1Z = NaN;
  3805. this.lp2X = NaN;
  3806. this.lp2Y = NaN;
  3807. this.lp2Z = NaN;
  3808. this.impulse = NaN;
  3809. }
  3810. /**
  3811. * The class holds details of the contact point.
  3812. * @author saharan
  3813. */
  3814. function ManifoldPoint(){
  3815. // Whether this manifold point is persisting or not.
  3816. this.warmStarted = false;
  3817. // The position of this manifold point.
  3818. this.position = new Vec3();
  3819. // The position in the first shape's coordinate.
  3820. this.localPoint1 = new Vec3();
  3821. // The position in the second shape's coordinate.
  3822. this.localPoint2 = new Vec3();
  3823. // The normal vector of this manifold point.
  3824. this.normal = new Vec3();
  3825. // The tangent vector of this manifold point.
  3826. this.tangent = new Vec3();
  3827. // The binormal vector of this manifold point.
  3828. this.binormal = new Vec3();
  3829. // The impulse in normal direction.
  3830. this.normalImpulse = 0;
  3831. // The impulse in tangent direction.
  3832. this.tangentImpulse = 0;
  3833. // The impulse in binormal direction.
  3834. this.binormalImpulse = 0;
  3835. // The denominator in normal direction.
  3836. this.normalDenominator = 0;
  3837. // The denominator in tangent direction.
  3838. this.tangentDenominator = 0;
  3839. // The denominator in binormal direction.
  3840. this.binormalDenominator = 0;
  3841. // The depth of penetration.
  3842. this.penetration = 0;
  3843. }
  3844. /**
  3845. * A contact manifold between two shapes.
  3846. * @author saharan
  3847. * @author lo-th
  3848. */
  3849. function ContactManifold () {
  3850. // The first rigid body.
  3851. this.body1 = null;
  3852. // The second rigid body.
  3853. this.body2 = null;
  3854. // The number of manifold points.
  3855. this.numPoints = 0;
  3856. // The manifold points.
  3857. this.points = [
  3858. new ManifoldPoint(),
  3859. new ManifoldPoint(),
  3860. new ManifoldPoint(),
  3861. new ManifoldPoint()
  3862. ];
  3863. }
  3864. ContactManifold.prototype = {
  3865. constructor: ContactManifold,
  3866. //Reset the manifold.
  3867. reset:function( shape1, shape2 ){
  3868. this.body1 = shape1.parent;
  3869. this.body2 = shape2.parent;
  3870. this.numPoints = 0;
  3871. },
  3872. // Add a point into this manifold.
  3873. addPointVec: function ( pos, norm, penetration, flip ) {
  3874. var p = this.points[ this.numPoints++ ];
  3875. p.position.copy( pos );
  3876. p.localPoint1.sub( pos, this.body1.position ).applyMatrix3( this.body1.rotation );
  3877. p.localPoint2.sub( pos, this.body2.position ).applyMatrix3( this.body2.rotation );
  3878. p.normal.copy( norm );
  3879. if( flip ) p.normal.negate();
  3880. p.normalImpulse = 0;
  3881. p.penetration = penetration;
  3882. p.warmStarted = false;
  3883. },
  3884. // Add a point into this manifold.
  3885. addPoint: function ( x, y, z, nx, ny, nz, penetration, flip ) {
  3886. var p = this.points[ this.numPoints++ ];
  3887. p.position.set( x, y, z );
  3888. p.localPoint1.sub( p.position, this.body1.position ).applyMatrix3( this.body1.rotation );
  3889. p.localPoint2.sub( p.position, this.body2.position ).applyMatrix3( this.body2.rotation );
  3890. p.normalImpulse = 0;
  3891. p.normal.set( nx, ny, nz );
  3892. if( flip ) p.normal.negate();
  3893. p.penetration = penetration;
  3894. p.warmStarted = false;
  3895. }
  3896. };
  3897. function ContactPointDataBuffer (){
  3898. this.nor = new Vec3();
  3899. this.tan = new Vec3();
  3900. this.bin = new Vec3();
  3901. this.norU1 = new Vec3();
  3902. this.tanU1 = new Vec3();
  3903. this.binU1 = new Vec3();
  3904. this.norU2 = new Vec3();
  3905. this.tanU2 = new Vec3();
  3906. this.binU2 = new Vec3();
  3907. this.norT1 = new Vec3();
  3908. this.tanT1 = new Vec3();
  3909. this.binT1 = new Vec3();
  3910. this.norT2 = new Vec3();
  3911. this.tanT2 = new Vec3();
  3912. this.binT2 = new Vec3();
  3913. this.norTU1 = new Vec3();
  3914. this.tanTU1 = new Vec3();
  3915. this.binTU1 = new Vec3();
  3916. this.norTU2 = new Vec3();
  3917. this.tanTU2 = new Vec3();
  3918. this.binTU2 = new Vec3();
  3919. this.norImp = 0;
  3920. this.tanImp = 0;
  3921. this.binImp = 0;
  3922. this.norDen = 0;
  3923. this.tanDen = 0;
  3924. this.binDen = 0;
  3925. this.norTar = 0;
  3926. this.next = null;
  3927. this.last = false;
  3928. }
  3929. /**
  3930. * ...
  3931. * @author saharan
  3932. */
  3933. function ContactConstraint ( manifold ){
  3934. Constraint.call( this );
  3935. // The contact manifold of the constraint.
  3936. this.manifold = manifold;
  3937. // The coefficient of restitution of the constraint.
  3938. this.restitution=NaN;
  3939. // The coefficient of friction of the constraint.
  3940. this.friction=NaN;
  3941. this.p1=null;
  3942. this.p2=null;
  3943. this.lv1=null;
  3944. this.lv2=null;
  3945. this.av1=null;
  3946. this.av2=null;
  3947. this.i1=null;
  3948. this.i2=null;
  3949. //this.ii1 = null;
  3950. //this.ii2 = null;
  3951. this.tmp = new Vec3();
  3952. this.tmpC1 = new Vec3();
  3953. this.tmpC2 = new Vec3();
  3954. this.tmpP1 = new Vec3();
  3955. this.tmpP2 = new Vec3();
  3956. this.tmplv1 = new Vec3();
  3957. this.tmplv2 = new Vec3();
  3958. this.tmpav1 = new Vec3();
  3959. this.tmpav2 = new Vec3();
  3960. this.m1=NaN;
  3961. this.m2=NaN;
  3962. this.num=0;
  3963. this.ps = manifold.points;
  3964. this.cs = new ContactPointDataBuffer();
  3965. this.cs.next = new ContactPointDataBuffer();
  3966. this.cs.next.next = new ContactPointDataBuffer();
  3967. this.cs.next.next.next = new ContactPointDataBuffer();
  3968. }
  3969. ContactConstraint.prototype = Object.assign( Object.create( Constraint.prototype ), {
  3970. constructor: ContactConstraint,
  3971. // Attach the constraint to the bodies.
  3972. attach: function(){
  3973. this.p1=this.body1.position;
  3974. this.p2=this.body2.position;
  3975. this.lv1=this.body1.linearVelocity;
  3976. this.av1=this.body1.angularVelocity;
  3977. this.lv2=this.body2.linearVelocity;
  3978. this.av2=this.body2.angularVelocity;
  3979. this.i1=this.body1.inverseInertia;
  3980. this.i2=this.body2.inverseInertia;
  3981. },
  3982. // Detach the constraint from the bodies.
  3983. detach: function(){
  3984. this.p1=null;
  3985. this.p2=null;
  3986. this.lv1=null;
  3987. this.lv2=null;
  3988. this.av1=null;
  3989. this.av2=null;
  3990. this.i1=null;
  3991. this.i2=null;
  3992. },
  3993. preSolve: function( timeStep, invTimeStep ){
  3994. this.m1 = this.body1.inverseMass;
  3995. this.m2 = this.body2.inverseMass;
  3996. var m1m2 = this.m1 + this.m2;
  3997. this.num = this.manifold.numPoints;
  3998. var c = this.cs;
  3999. var p, rvn, len, norImp, norTar, sepV, i1, i2;
  4000. for( var i=0; i < this.num; i++ ){
  4001. p = this.ps[i];
  4002. this.tmpP1.sub( p.position, this.p1 );
  4003. this.tmpP2.sub( p.position, this.p2 );
  4004. this.tmpC1.crossVectors( this.av1, this.tmpP1 );
  4005. this.tmpC2.crossVectors( this.av2, this.tmpP2 );
  4006. c.norImp = p.normalImpulse;
  4007. c.tanImp = p.tangentImpulse;
  4008. c.binImp = p.binormalImpulse;
  4009. c.nor.copy( p.normal );
  4010. this.tmp.set(
  4011. ( this.lv2.x + this.tmpC2.x ) - ( this.lv1.x + this.tmpC1.x ),
  4012. ( this.lv2.y + this.tmpC2.y ) - ( this.lv1.y + this.tmpC1.y ),
  4013. ( this.lv2.z + this.tmpC2.z ) - ( this.lv1.z + this.tmpC1.z )
  4014. );
  4015. rvn = _Math.dotVectors( c.nor, this.tmp );
  4016. c.tan.set(
  4017. this.tmp.x - rvn * c.nor.x,
  4018. this.tmp.y - rvn * c.nor.y,
  4019. this.tmp.z - rvn * c.nor.z
  4020. );
  4021. len = _Math.dotVectors( c.tan, c.tan );
  4022. if( len <= 0.04 ) {
  4023. c.tan.tangent( c.nor );
  4024. }
  4025. c.tan.normalize();
  4026. c.bin.crossVectors( c.nor, c.tan );
  4027. c.norU1.scale( c.nor, this.m1 );
  4028. c.norU2.scale( c.nor, this.m2 );
  4029. c.tanU1.scale( c.tan, this.m1 );
  4030. c.tanU2.scale( c.tan, this.m2 );
  4031. c.binU1.scale( c.bin, this.m1 );
  4032. c.binU2.scale( c.bin, this.m2 );
  4033. c.norT1.crossVectors( this.tmpP1, c.nor );
  4034. c.tanT1.crossVectors( this.tmpP1, c.tan );
  4035. c.binT1.crossVectors( this.tmpP1, c.bin );
  4036. c.norT2.crossVectors( this.tmpP2, c.nor );
  4037. c.tanT2.crossVectors( this.tmpP2, c.tan );
  4038. c.binT2.crossVectors( this.tmpP2, c.bin );
  4039. i1 = this.i1;
  4040. i2 = this.i2;
  4041. c.norTU1.copy( c.norT1 ).applyMatrix3( i1, true );
  4042. c.tanTU1.copy( c.tanT1 ).applyMatrix3( i1, true );
  4043. c.binTU1.copy( c.binT1 ).applyMatrix3( i1, true );
  4044. c.norTU2.copy( c.norT2 ).applyMatrix3( i2, true );
  4045. c.tanTU2.copy( c.tanT2 ).applyMatrix3( i2, true );
  4046. c.binTU2.copy( c.binT2 ).applyMatrix3( i2, true );
  4047. /*c.norTU1.mulMat( this.i1, c.norT1 );
  4048. c.tanTU1.mulMat( this.i1, c.tanT1 );
  4049. c.binTU1.mulMat( this.i1, c.binT1 );
  4050. c.norTU2.mulMat( this.i2, c.norT2 );
  4051. c.tanTU2.mulMat( this.i2, c.tanT2 );
  4052. c.binTU2.mulMat( this.i2, c.binT2 );*/
  4053. this.tmpC1.crossVectors( c.norTU1, this.tmpP1 );
  4054. this.tmpC2.crossVectors( c.norTU2, this.tmpP2 );
  4055. this.tmp.add( this.tmpC1, this.tmpC2 );
  4056. c.norDen = 1 / ( m1m2 +_Math.dotVectors( c.nor, this.tmp ));
  4057. this.tmpC1.crossVectors( c.tanTU1, this.tmpP1 );
  4058. this.tmpC2.crossVectors( c.tanTU2, this.tmpP2 );
  4059. this.tmp.add( this.tmpC1, this.tmpC2 );
  4060. c.tanDen = 1 / ( m1m2 +_Math.dotVectors( c.tan, this.tmp ));
  4061. this.tmpC1.crossVectors( c.binTU1, this.tmpP1 );
  4062. this.tmpC2.crossVectors( c.binTU2, this.tmpP2 );
  4063. this.tmp.add( this.tmpC1, this.tmpC2 );
  4064. c.binDen = 1 / ( m1m2 +_Math.dotVectors( c.bin, this.tmp ));
  4065. if( p.warmStarted ){
  4066. norImp = p.normalImpulse;
  4067. this.lv1.addScaledVector( c.norU1, norImp );
  4068. this.av1.addScaledVector( c.norTU1, norImp );
  4069. this.lv2.subScaledVector( c.norU2, norImp );
  4070. this.av2.subScaledVector( c.norTU2, norImp );
  4071. c.norImp = norImp;
  4072. c.tanImp = 0;
  4073. c.binImp = 0;
  4074. rvn = 0; // disable bouncing
  4075. } else {
  4076. c.norImp=0;
  4077. c.tanImp=0;
  4078. c.binImp=0;
  4079. }
  4080. if(rvn>-1) rvn=0; // disable bouncing
  4081. norTar = this.restitution*-rvn;
  4082. sepV = -(p.penetration+0.005)*invTimeStep*0.05; // allow 0.5cm error
  4083. if(norTar<sepV) norTar=sepV;
  4084. c.norTar = norTar;
  4085. c.last = i==this.num-1;
  4086. c = c.next;
  4087. }
  4088. },
  4089. solve: function(){
  4090. this.tmplv1.copy( this.lv1 );
  4091. this.tmplv2.copy( this.lv2 );
  4092. this.tmpav1.copy( this.av1 );
  4093. this.tmpav2.copy( this.av2 );
  4094. var oldImp1, newImp1, oldImp2, newImp2, rvn, norImp, tanImp, binImp, max, len;
  4095. var c = this.cs;
  4096. while(true){
  4097. norImp = c.norImp;
  4098. tanImp = c.tanImp;
  4099. binImp = c.binImp;
  4100. max = -norImp * this.friction;
  4101. this.tmp.sub( this.tmplv2, this.tmplv1 );
  4102. rvn = _Math.dotVectors( this.tmp, c.tan ) + _Math.dotVectors( this.tmpav2, c.tanT2 ) - _Math.dotVectors( this.tmpav1, c.tanT1 );
  4103. oldImp1 = tanImp;
  4104. newImp1 = rvn*c.tanDen;
  4105. tanImp += newImp1;
  4106. rvn = _Math.dotVectors( this.tmp, c.bin ) + _Math.dotVectors( this.tmpav2, c.binT2 ) - _Math.dotVectors( this.tmpav1, c.binT1 );
  4107. oldImp2 = binImp;
  4108. newImp2 = rvn*c.binDen;
  4109. binImp += newImp2;
  4110. // cone friction clamp
  4111. len = tanImp*tanImp + binImp*binImp;
  4112. if(len > max * max ){
  4113. len = max/_Math.sqrt(len);
  4114. tanImp *= len;
  4115. binImp *= len;
  4116. }
  4117. newImp1 = tanImp-oldImp1;
  4118. newImp2 = binImp-oldImp2;
  4119. //
  4120. this.tmp.set(
  4121. c.tanU1.x*newImp1 + c.binU1.x*newImp2,
  4122. c.tanU1.y*newImp1 + c.binU1.y*newImp2,
  4123. c.tanU1.z*newImp1 + c.binU1.z*newImp2
  4124. );
  4125. this.tmplv1.addEqual( this.tmp );
  4126. this.tmp.set(
  4127. c.tanTU1.x*newImp1 + c.binTU1.x*newImp2,
  4128. c.tanTU1.y*newImp1 + c.binTU1.y*newImp2,
  4129. c.tanTU1.z*newImp1 + c.binTU1.z*newImp2
  4130. );
  4131. this.tmpav1.addEqual( this.tmp );
  4132. this.tmp.set(
  4133. c.tanU2.x*newImp1 + c.binU2.x*newImp2,
  4134. c.tanU2.y*newImp1 + c.binU2.y*newImp2,
  4135. c.tanU2.z*newImp1 + c.binU2.z*newImp2
  4136. );
  4137. this.tmplv2.subEqual( this.tmp );
  4138. this.tmp.set(
  4139. c.tanTU2.x*newImp1 + c.binTU2.x*newImp2,
  4140. c.tanTU2.y*newImp1 + c.binTU2.y*newImp2,
  4141. c.tanTU2.z*newImp1 + c.binTU2.z*newImp2
  4142. );
  4143. this.tmpav2.subEqual( this.tmp );
  4144. // restitution part
  4145. this.tmp.sub( this.tmplv2, this.tmplv1 );
  4146. rvn = _Math.dotVectors( this.tmp, c.nor ) + _Math.dotVectors( this.tmpav2, c.norT2 ) - _Math.dotVectors( this.tmpav1, c.norT1 );
  4147. oldImp1 = norImp;
  4148. newImp1 = (rvn-c.norTar)*c.norDen;
  4149. norImp += newImp1;
  4150. if( norImp > 0 ) norImp = 0;
  4151. newImp1 = norImp - oldImp1;
  4152. this.tmplv1.addScaledVector( c.norU1, newImp1 );
  4153. this.tmpav1.addScaledVector( c.norTU1, newImp1 );
  4154. this.tmplv2.subScaledVector( c.norU2, newImp1 );
  4155. this.tmpav2.subScaledVector( c.norTU2, newImp1 );
  4156. c.norImp = norImp;
  4157. c.tanImp = tanImp;
  4158. c.binImp = binImp;
  4159. if(c.last)break;
  4160. c = c.next;
  4161. }
  4162. this.lv1.copy( this.tmplv1 );
  4163. this.lv2.copy( this.tmplv2 );
  4164. this.av1.copy( this.tmpav1 );
  4165. this.av2.copy( this.tmpav2 );
  4166. },
  4167. postSolve: function(){
  4168. var c = this.cs, p;
  4169. var i = this.num;
  4170. while(i--){
  4171. //for(var i=0;i<this.num;i++){
  4172. p = this.ps[i];
  4173. p.normal.copy( c.nor );
  4174. p.tangent.copy( c.tan );
  4175. p.binormal.copy( c.bin );
  4176. p.normalImpulse = c.norImp;
  4177. p.tangentImpulse = c.tanImp;
  4178. p.binormalImpulse = c.binImp;
  4179. p.normalDenominator = c.norDen;
  4180. p.tangentDenominator = c.tanDen;
  4181. p.binormalDenominator = c.binDen;
  4182. c=c.next;
  4183. }
  4184. }
  4185. });
  4186. /**
  4187. * A contact is a pair of shapes whose axis-aligned bounding boxes are overlapping.
  4188. * @author saharan
  4189. */
  4190. function Contact(){
  4191. // The first shape.
  4192. this.shape1 = null;
  4193. // The second shape.
  4194. this.shape2 = null;
  4195. // The first rigid body.
  4196. this.body1 = null;
  4197. // The second rigid body.
  4198. this.body2 = null;
  4199. // The previous contact in the world.
  4200. this.prev = null;
  4201. // The next contact in the world.
  4202. this.next = null;
  4203. // Internal
  4204. this.persisting = false;
  4205. // Whether both the rigid bodies are sleeping or not.
  4206. this.sleeping = false;
  4207. // The collision detector between two shapes.
  4208. this.detector = null;
  4209. // The contact constraint of the contact.
  4210. this.constraint = null;
  4211. // Whether the shapes are touching or not.
  4212. this.touching = false;
  4213. // shapes is very close and touching
  4214. this.close = false;
  4215. this.dist = _Math.INF;
  4216. this.b1Link = new ContactLink( this );
  4217. this.b2Link = new ContactLink( this );
  4218. this.s1Link = new ContactLink( this );
  4219. this.s2Link = new ContactLink( this );
  4220. // The contact manifold of the contact.
  4221. this.manifold = new ContactManifold();
  4222. this.buffer = [
  4223. new ImpulseDataBuffer(),
  4224. new ImpulseDataBuffer(),
  4225. new ImpulseDataBuffer(),
  4226. new ImpulseDataBuffer()
  4227. ];
  4228. this.points = this.manifold.points;
  4229. this.constraint = new ContactConstraint( this.manifold );
  4230. }
  4231. Object.assign( Contact.prototype, {
  4232. Contact: true,
  4233. mixRestitution: function ( restitution1, restitution2 ) {
  4234. return _Math.sqrt(restitution1*restitution2);
  4235. },
  4236. mixFriction: function ( friction1, friction2 ) {
  4237. return _Math.sqrt(friction1*friction2);
  4238. },
  4239. /**
  4240. * Update the contact manifold.
  4241. */
  4242. updateManifold: function () {
  4243. this.constraint.restitution =this.mixRestitution(this.shape1.restitution,this.shape2.restitution);
  4244. this.constraint.friction=this.mixFriction(this.shape1.friction,this.shape2.friction);
  4245. var numBuffers=this.manifold.numPoints;
  4246. var i = numBuffers;
  4247. while(i--){
  4248. //for(var i=0;i<numBuffers;i++){
  4249. var b = this.buffer[i];
  4250. var p = this.points[i];
  4251. b.lp1X=p.localPoint1.x;
  4252. b.lp1Y=p.localPoint1.y;
  4253. b.lp1Z=p.localPoint1.z;
  4254. b.lp2X=p.localPoint2.x;
  4255. b.lp2Y=p.localPoint2.y;
  4256. b.lp2Z=p.localPoint2.z;
  4257. b.impulse=p.normalImpulse;
  4258. }
  4259. this.manifold.numPoints=0;
  4260. this.detector.detectCollision(this.shape1,this.shape2,this.manifold);
  4261. var num=this.manifold.numPoints;
  4262. if(num==0){
  4263. this.touching = false;
  4264. this.close = false;
  4265. this.dist = _Math.INF;
  4266. return;
  4267. }
  4268. if( this.touching || this.dist < 0.001 ) this.close = true;
  4269. this.touching=true;
  4270. i = num;
  4271. while(i--){
  4272. //for(i=0; i<num; i++){
  4273. p=this.points[i];
  4274. var lp1x=p.localPoint1.x;
  4275. var lp1y=p.localPoint1.y;
  4276. var lp1z=p.localPoint1.z;
  4277. var lp2x=p.localPoint2.x;
  4278. var lp2y=p.localPoint2.y;
  4279. var lp2z=p.localPoint2.z;
  4280. var index=-1;
  4281. var minDistance=0.0004;
  4282. var j = numBuffers;
  4283. while(j--){
  4284. //for(var j=0;j<numBuffers;j++){
  4285. b=this.buffer[j];
  4286. var dx=b.lp1X-lp1x;
  4287. var dy=b.lp1Y-lp1y;
  4288. var dz=b.lp1Z-lp1z;
  4289. var distance1=dx*dx+dy*dy+dz*dz;
  4290. dx=b.lp2X-lp2x;
  4291. dy=b.lp2Y-lp2y;
  4292. dz=b.lp2Z-lp2z;
  4293. var distance2=dx*dx+dy*dy+dz*dz;
  4294. if(distance1<distance2){
  4295. if(distance1<minDistance){
  4296. minDistance=distance1;
  4297. index=j;
  4298. }
  4299. }else{
  4300. if(distance2<minDistance){
  4301. minDistance=distance2;
  4302. index=j;
  4303. }
  4304. }
  4305. if( minDistance < this.dist ) this.dist = minDistance;
  4306. }
  4307. if(index!=-1){
  4308. var tmp=this.buffer[index];
  4309. this.buffer[index]=this.buffer[--numBuffers];
  4310. this.buffer[numBuffers]=tmp;
  4311. p.normalImpulse=tmp.impulse;
  4312. p.warmStarted=true;
  4313. }else{
  4314. p.normalImpulse=0;
  4315. p.warmStarted=false;
  4316. }
  4317. }
  4318. },
  4319. /**
  4320. * Attach the contact to the shapes.
  4321. * @param shape1
  4322. * @param shape2
  4323. */
  4324. attach:function(shape1,shape2){
  4325. this.shape1=shape1;
  4326. this.shape2=shape2;
  4327. this.body1=shape1.parent;
  4328. this.body2=shape2.parent;
  4329. this.manifold.body1=this.body1;
  4330. this.manifold.body2=this.body2;
  4331. this.constraint.body1=this.body1;
  4332. this.constraint.body2=this.body2;
  4333. this.constraint.attach();
  4334. this.s1Link.shape=shape2;
  4335. this.s1Link.body=this.body2;
  4336. this.s2Link.shape=shape1;
  4337. this.s2Link.body=this.body1;
  4338. if(shape1.contactLink!=null)(this.s1Link.next=shape1.contactLink).prev=this.s1Link;
  4339. else this.s1Link.next=null;
  4340. shape1.contactLink=this.s1Link;
  4341. shape1.numContacts++;
  4342. if(shape2.contactLink!=null)(this.s2Link.next=shape2.contactLink).prev=this.s2Link;
  4343. else this.s2Link.next=null;
  4344. shape2.contactLink=this.s2Link;
  4345. shape2.numContacts++;
  4346. this.b1Link.shape=shape2;
  4347. this.b1Link.body=this.body2;
  4348. this.b2Link.shape=shape1;
  4349. this.b2Link.body=this.body1;
  4350. if(this.body1.contactLink!=null)(this.b1Link.next=this.body1.contactLink).prev=this.b1Link;
  4351. else this.b1Link.next=null;
  4352. this.body1.contactLink=this.b1Link;
  4353. this.body1.numContacts++;
  4354. if(this.body2.contactLink!=null)(this.b2Link.next=this.body2.contactLink).prev=this.b2Link;
  4355. else this.b2Link.next=null;
  4356. this.body2.contactLink=this.b2Link;
  4357. this.body2.numContacts++;
  4358. this.prev=null;
  4359. this.next=null;
  4360. this.persisting=true;
  4361. this.sleeping=this.body1.sleeping&&this.body2.sleeping;
  4362. this.manifold.numPoints=0;
  4363. },
  4364. /**
  4365. * Detach the contact from the shapes.
  4366. */
  4367. detach:function(){
  4368. var prev=this.s1Link.prev;
  4369. var next=this.s1Link.next;
  4370. if(prev!==null)prev.next=next;
  4371. if(next!==null)next.prev=prev;
  4372. if(this.shape1.contactLink==this.s1Link)this.shape1.contactLink=next;
  4373. this.s1Link.prev=null;
  4374. this.s1Link.next=null;
  4375. this.s1Link.shape=null;
  4376. this.s1Link.body=null;
  4377. this.shape1.numContacts--;
  4378. prev=this.s2Link.prev;
  4379. next=this.s2Link.next;
  4380. if(prev!==null)prev.next=next;
  4381. if(next!==null)next.prev=prev;
  4382. if(this.shape2.contactLink==this.s2Link)this.shape2.contactLink=next;
  4383. this.s2Link.prev=null;
  4384. this.s2Link.next=null;
  4385. this.s2Link.shape=null;
  4386. this.s2Link.body=null;
  4387. this.shape2.numContacts--;
  4388. prev=this.b1Link.prev;
  4389. next=this.b1Link.next;
  4390. if(prev!==null)prev.next=next;
  4391. if(next!==null)next.prev=prev;
  4392. if(this.body1.contactLink==this.b1Link)this.body1.contactLink=next;
  4393. this.b1Link.prev=null;
  4394. this.b1Link.next=null;
  4395. this.b1Link.shape=null;
  4396. this.b1Link.body=null;
  4397. this.body1.numContacts--;
  4398. prev=this.b2Link.prev;
  4399. next=this.b2Link.next;
  4400. if(prev!==null)prev.next=next;
  4401. if(next!==null)next.prev=prev;
  4402. if(this.body2.contactLink==this.b2Link)this.body2.contactLink=next;
  4403. this.b2Link.prev=null;
  4404. this.b2Link.next=null;
  4405. this.b2Link.shape=null;
  4406. this.b2Link.body=null;
  4407. this.body2.numContacts--;
  4408. this.manifold.body1=null;
  4409. this.manifold.body2=null;
  4410. this.constraint.body1=null;
  4411. this.constraint.body2=null;
  4412. this.constraint.detach();
  4413. this.shape1=null;
  4414. this.shape2=null;
  4415. this.body1=null;
  4416. this.body2=null;
  4417. }
  4418. } );
  4419. /**
  4420. * The class of rigid body.
  4421. * Rigid body has the shape of a single or multiple collision processing,
  4422. * I can set the parameters individually.
  4423. * @author saharan
  4424. * @author lo-th
  4425. */
  4426. function RigidBody ( Position, Rotation ) {
  4427. this.position = Position || new Vec3();
  4428. this.orientation = Rotation || new Quat();
  4429. this.scale = 1;
  4430. this.invScale = 1;
  4431. // possible link to three Mesh;
  4432. this.mesh = null;
  4433. this.id = NaN;
  4434. this.name = "";
  4435. // The maximum number of shapes that can be added to a one rigid.
  4436. //this.MAX_SHAPES = 64;//64;
  4437. this.prev = null;
  4438. this.next = null;
  4439. // I represent the kind of rigid body.
  4440. // Please do not change from the outside this variable.
  4441. // If you want to change the type of rigid body, always
  4442. // Please specify the type you want to set the arguments of setupMass method.
  4443. this.type = BODY_NULL;
  4444. this.massInfo = new MassInfo();
  4445. this.newPosition = new Vec3();
  4446. this.controlPos = false;
  4447. this.newOrientation = new Quat();
  4448. this.newRotation = new Vec3();
  4449. this.currentRotation = new Vec3();
  4450. this.controlRot = false;
  4451. this.controlRotInTime = false;
  4452. this.quaternion = new Quat();
  4453. this.pos = new Vec3();
  4454. // Is the translational velocity.
  4455. this.linearVelocity = new Vec3();
  4456. // Is the angular velocity.
  4457. this.angularVelocity = new Vec3();
  4458. //--------------------------------------------
  4459. // Please do not change from the outside this variables.
  4460. //--------------------------------------------
  4461. // It is a world that rigid body has been added.
  4462. this.parent = null;
  4463. this.contactLink = null;
  4464. this.numContacts = 0;
  4465. // An array of shapes that are included in the rigid body.
  4466. this.shapes = null;
  4467. // The number of shapes that are included in the rigid body.
  4468. this.numShapes = 0;
  4469. // It is the link array of joint that is connected to the rigid body.
  4470. this.jointLink = null;
  4471. // The number of joints that are connected to the rigid body.
  4472. this.numJoints = 0;
  4473. // It is the world coordinate of the center of gravity in the sleep just before.
  4474. this.sleepPosition = new Vec3();
  4475. // It is a quaternion that represents the attitude of sleep just before.
  4476. this.sleepOrientation = new Quat();
  4477. // I will show this rigid body to determine whether it is a rigid body static.
  4478. this.isStatic = false;
  4479. // I indicates that this rigid body to determine whether it is a rigid body dynamic.
  4480. this.isDynamic = false;
  4481. this.isKinematic = false;
  4482. // It is a rotation matrix representing the orientation.
  4483. this.rotation = new Mat33();
  4484. //--------------------------------------------
  4485. // It will be recalculated automatically from the shape, which is included.
  4486. //--------------------------------------------
  4487. // This is the weight.
  4488. this.mass = 0;
  4489. // It is the reciprocal of the mass.
  4490. this.inverseMass = 0;
  4491. // It is the inverse of the inertia tensor in the world system.
  4492. this.inverseInertia = new Mat33();
  4493. // It is the inertia tensor in the initial state.
  4494. this.localInertia = new Mat33();
  4495. // It is the inverse of the inertia tensor in the initial state.
  4496. this.inverseLocalInertia = new Mat33();
  4497. this.tmpInertia = new Mat33();
  4498. // I indicates rigid body whether it has been added to the simulation Island.
  4499. this.addedToIsland = false;
  4500. // It shows how to sleep rigid body.
  4501. this.allowSleep = true;
  4502. // This is the time from when the rigid body at rest.
  4503. this.sleepTime = 0;
  4504. // I shows rigid body to determine whether it is a sleep state.
  4505. this.sleeping = false;
  4506. }
  4507. Object.assign( RigidBody.prototype, {
  4508. setParent: function ( world ) {
  4509. this.parent = world;
  4510. this.scale = this.parent.scale;
  4511. this.invScale = this.parent.invScale;
  4512. this.id = this.parent.numRigidBodies;
  4513. if( !this.name ) this.name = this.id;
  4514. this.updateMesh();
  4515. },
  4516. /**
  4517. * I'll add a shape to rigid body.
  4518. * If you add a shape, please call the setupMass method to step up to the start of the next.
  4519. * @param shape shape to Add
  4520. */
  4521. addShape:function(shape){
  4522. if(shape.parent){
  4523. printError("RigidBody", "It is not possible that you add a shape which already has an associated body.");
  4524. }
  4525. if(this.shapes!=null)( this.shapes.prev = shape ).next = this.shapes;
  4526. this.shapes = shape;
  4527. shape.parent = this;
  4528. if(this.parent) this.parent.addShape( shape );
  4529. this.numShapes++;
  4530. },
  4531. /**
  4532. * I will delete the shape from the rigid body.
  4533. * If you delete a shape, please call the setupMass method to step up to the start of the next.
  4534. * @param shape {Shape} to delete
  4535. * @return void
  4536. */
  4537. removeShape:function(shape){
  4538. var remove = shape;
  4539. if(remove.parent!=this)return;
  4540. var prev=remove.prev;
  4541. var next=remove.next;
  4542. if(prev!=null) prev.next=next;
  4543. if(next!=null) next.prev=prev;
  4544. if(this.shapes==remove)this.shapes=next;
  4545. remove.prev=null;
  4546. remove.next=null;
  4547. remove.parent=null;
  4548. if(this.parent)this.parent.removeShape(remove);
  4549. this.numShapes--;
  4550. },
  4551. remove: function () {
  4552. this.dispose();
  4553. },
  4554. dispose: function () {
  4555. this.parent.removeRigidBody( this );
  4556. },
  4557. checkContact: function( name ) {
  4558. this.parent.checkContact( this.name, name );
  4559. },
  4560. /**
  4561. * Calulates mass datas(center of gravity, mass, moment inertia, etc...).
  4562. * If the parameter type is set to BODY_STATIC, the rigid body will be fixed to the space.
  4563. * If the parameter adjustPosition is set to true, the shapes' relative positions and
  4564. * the rigid body's position will be adjusted to the center of gravity.
  4565. * @param type
  4566. * @param adjustPosition
  4567. * @return void
  4568. */
  4569. setupMass: function ( type, AdjustPosition ) {
  4570. var adjustPosition = ( AdjustPosition !== undefined ) ? AdjustPosition : true;
  4571. this.type = type || BODY_STATIC;
  4572. this.isDynamic = this.type === BODY_DYNAMIC;
  4573. this.isStatic = this.type === BODY_STATIC;
  4574. this.mass = 0;
  4575. this.localInertia.set(0,0,0,0,0,0,0,0,0);
  4576. var tmpM = new Mat33();
  4577. var tmpV = new Vec3();
  4578. for( var shape = this.shapes; shape !== null; shape = shape.next ){
  4579. shape.calculateMassInfo( this.massInfo );
  4580. var shapeMass = this.massInfo.mass;
  4581. tmpV.addScaledVector(shape.relativePosition, shapeMass);
  4582. this.mass += shapeMass;
  4583. this.rotateInertia( shape.relativeRotation, this.massInfo.inertia, tmpM );
  4584. this.localInertia.add( tmpM );
  4585. // add offset inertia
  4586. this.localInertia.addOffset( shapeMass, shape.relativePosition );
  4587. }
  4588. this.inverseMass = 1 / this.mass;
  4589. tmpV.scaleEqual( this.inverseMass );
  4590. if( adjustPosition ){
  4591. this.position.add( tmpV );
  4592. for( shape=this.shapes; shape !== null; shape = shape.next ){
  4593. shape.relativePosition.subEqual(tmpV);
  4594. }
  4595. // subtract offset inertia
  4596. this.localInertia.subOffset( this.mass, tmpV );
  4597. }
  4598. this.inverseLocalInertia.invert( this.localInertia );
  4599. //}
  4600. if( this.type === BODY_STATIC ){
  4601. this.inverseMass = 0;
  4602. this.inverseLocalInertia.set(0,0,0,0,0,0,0,0,0);
  4603. }
  4604. this.syncShapes();
  4605. this.awake();
  4606. },
  4607. /**
  4608. * Awake the rigid body.
  4609. */
  4610. awake:function(){
  4611. if( !this.allowSleep || !this.sleeping ) return;
  4612. this.sleeping = false;
  4613. this.sleepTime = 0;
  4614. // awake connected constraints
  4615. var cs = this.contactLink;
  4616. while(cs != null){
  4617. cs.body.sleepTime = 0;
  4618. cs.body.sleeping = false;
  4619. cs = cs.next;
  4620. }
  4621. var js = this.jointLink;
  4622. while(js != null){
  4623. js.body.sleepTime = 0;
  4624. js.body.sleeping = false;
  4625. js = js.next;
  4626. }
  4627. for(var shape = this.shapes; shape!=null; shape = shape.next){
  4628. shape.updateProxy();
  4629. }
  4630. },
  4631. /**
  4632. * Sleep the rigid body.
  4633. */
  4634. sleep:function(){
  4635. if( !this.allowSleep || this.sleeping ) return;
  4636. this.linearVelocity.set(0,0,0);
  4637. this.angularVelocity.set(0,0,0);
  4638. this.sleepPosition.copy( this.position );
  4639. this.sleepOrientation.copy( this.orientation );
  4640. this.sleepTime = 0;
  4641. this.sleeping = true;
  4642. for( var shape = this.shapes; shape != null; shape = shape.next ) {
  4643. shape.updateProxy();
  4644. }
  4645. },
  4646. testWakeUp: function(){
  4647. if( this.linearVelocity.testZero() || this.angularVelocity.testZero() || this.position.testDiff( this.sleepPosition ) || this.orientation.testDiff( this.sleepOrientation )) this.awake(); // awake the body
  4648. },
  4649. /**
  4650. * Get whether the rigid body has not any connection with others.
  4651. * @return {void}
  4652. */
  4653. isLonely: function () {
  4654. return this.numJoints==0 && this.numContacts==0;
  4655. },
  4656. /**
  4657. * The time integration of the motion of a rigid body, you can update the information such as the shape.
  4658. * This method is invoked automatically when calling the step of the World,
  4659. * There is no need to call from outside usually.
  4660. * @param timeStep time
  4661. * @return {void}
  4662. */
  4663. updatePosition: function ( timeStep ) {
  4664. switch(this.type){
  4665. case BODY_STATIC:
  4666. this.linearVelocity.set(0,0,0);
  4667. this.angularVelocity.set(0,0,0);
  4668. // ONLY FOR TEST
  4669. if(this.controlPos){
  4670. this.position.copy(this.newPosition);
  4671. this.controlPos = false;
  4672. }
  4673. if(this.controlRot){
  4674. this.orientation.copy(this.newOrientation);
  4675. this.controlRot = false;
  4676. }
  4677. /*this.linearVelocity.x=0;
  4678. this.linearVelocity.y=0;
  4679. this.linearVelocity.z=0;
  4680. this.angularVelocity.x=0;
  4681. this.angularVelocity.y=0;
  4682. this.angularVelocity.z=0;*/
  4683. break;
  4684. case BODY_DYNAMIC:
  4685. if( this.isKinematic ){
  4686. this.linearVelocity.set(0,0,0);
  4687. this.angularVelocity.set(0,0,0);
  4688. }
  4689. if(this.controlPos){
  4690. this.linearVelocity.subVectors( this.newPosition, this.position ).multiplyScalar(1/timeStep);
  4691. this.controlPos = false;
  4692. }
  4693. if(this.controlRot){
  4694. this.angularVelocity.copy( this.getAxis() );
  4695. this.orientation.copy( this.newOrientation );
  4696. this.controlRot = false;
  4697. }
  4698. this.position.addScaledVector(this.linearVelocity, timeStep);
  4699. this.orientation.addTime(this.angularVelocity, timeStep);
  4700. this.updateMesh();
  4701. break;
  4702. default: printError("RigidBody", "Invalid type.");
  4703. }
  4704. this.syncShapes();
  4705. this.updateMesh();
  4706. },
  4707. getAxis: function () {
  4708. return new Vec3( 0,1,0 ).applyMatrix3( this.inverseLocalInertia, true ).normalize();
  4709. },
  4710. rotateInertia: function ( rot, inertia, out ) {
  4711. this.tmpInertia.multiplyMatrices( rot, inertia );
  4712. out.multiplyMatrices( this.tmpInertia, rot, true );
  4713. },
  4714. syncShapes: function () {
  4715. this.rotation.setQuat( this.orientation );
  4716. this.rotateInertia( this.rotation, this.inverseLocalInertia, this.inverseInertia );
  4717. for(var shape = this.shapes; shape!=null; shape = shape.next){
  4718. shape.position.copy( shape.relativePosition ).applyMatrix3( this.rotation, true ).add( this.position );
  4719. // add by QuaziKb
  4720. shape.rotation.multiplyMatrices( this.rotation, shape.relativeRotation );
  4721. shape.updateProxy();
  4722. }
  4723. },
  4724. //---------------------------------------------
  4725. // APPLY IMPULSE FORCE
  4726. //---------------------------------------------
  4727. applyImpulse: function(position, force){
  4728. this.linearVelocity.addScaledVector(force, this.inverseMass);
  4729. var rel = new Vec3().copy( position ).sub( this.position ).cross( force ).applyMatrix3( this.inverseInertia, true );
  4730. this.angularVelocity.add( rel );
  4731. },
  4732. //---------------------------------------------
  4733. // SET DYNAMIQUE POSITION AND ROTATION
  4734. //---------------------------------------------
  4735. setPosition: function(pos){
  4736. this.newPosition.copy( pos ).multiplyScalar( this.invScale );
  4737. this.controlPos = true;
  4738. if( !this.isKinematic ) this.isKinematic = true;
  4739. },
  4740. setQuaternion: function(q){
  4741. this.newOrientation.set(q.x, q.y, q.z, q.w);
  4742. this.controlRot = true;
  4743. if( !this.isKinematic ) this.isKinematic = true;
  4744. },
  4745. setRotation: function ( rot ) {
  4746. this.newOrientation = new Quat().setFromEuler( rot.x * _Math.degtorad, rot.y * _Math.degtorad, rot.y * _Math.degtorad );//this.rotationVectToQuad( rot );
  4747. this.controlRot = true;
  4748. },
  4749. //---------------------------------------------
  4750. // RESET DYNAMIQUE POSITION AND ROTATION
  4751. //---------------------------------------------
  4752. resetPosition:function(x,y,z){
  4753. this.linearVelocity.set( 0, 0, 0 );
  4754. this.angularVelocity.set( 0, 0, 0 );
  4755. this.position.set( x, y, z ).multiplyScalar( this.invScale );
  4756. //this.position.set( x*OIMO.WorldScale.invScale, y*OIMO.WorldScale.invScale, z*OIMO.WorldScale.invScale );
  4757. this.awake();
  4758. },
  4759. resetQuaternion:function( q ){
  4760. this.angularVelocity.set(0,0,0);
  4761. this.orientation = new Quat( q.x, q.y, q.z, q.w );
  4762. this.awake();
  4763. },
  4764. resetRotation:function(x,y,z){
  4765. this.angularVelocity.set(0,0,0);
  4766. this.orientation = new Quat().setFromEuler( x * _Math.degtorad, y * _Math.degtorad, z * _Math.degtorad );//this.rotationVectToQuad( new Vec3(x,y,z) );
  4767. this.awake();
  4768. },
  4769. //---------------------------------------------
  4770. // GET POSITION AND ROTATION
  4771. //---------------------------------------------
  4772. getPosition:function () {
  4773. return this.pos;
  4774. },
  4775. getQuaternion: function () {
  4776. return this.quaternion;
  4777. },
  4778. //---------------------------------------------
  4779. // AUTO UPDATE THREE MESH
  4780. //---------------------------------------------
  4781. connectMesh: function ( mesh ) {
  4782. this.mesh = mesh;
  4783. this.updateMesh();
  4784. },
  4785. updateMesh: function(){
  4786. this.pos.scale( this.position, this.scale );
  4787. this.quaternion.copy( this.orientation );
  4788. if( this.mesh === null ) return;
  4789. this.mesh.position.copy( this.getPosition() );
  4790. this.mesh.quaternion.copy( this.getQuaternion() );
  4791. },
  4792. } );
  4793. /**
  4794. * A pair of shapes that may collide.
  4795. * @author saharan
  4796. */
  4797. function Pair ( s1, s2 ){
  4798. // The first shape.
  4799. this.shape1 = s1 || null;
  4800. // The second shape.
  4801. this.shape2 = s2 || null;
  4802. }
  4803. /**
  4804. * The broad-phase is used for collecting all possible pairs for collision.
  4805. */
  4806. function BroadPhase(){
  4807. this.types = BR_NULL;
  4808. this.numPairChecks = 0;
  4809. this.numPairs = 0;
  4810. this.pairs = [];
  4811. }
  4812. Object.assign( BroadPhase.prototype, {
  4813. BroadPhase: true,
  4814. // Create a new proxy.
  4815. createProxy: function ( shape ) {
  4816. printError("BroadPhase","Inheritance error.");
  4817. },
  4818. // Add the proxy into the broad-phase.
  4819. addProxy: function ( proxy ) {
  4820. printError("BroadPhase","Inheritance error.");
  4821. },
  4822. // Remove the proxy from the broad-phase.
  4823. removeProxy: function ( proxy ) {
  4824. printError("BroadPhase","Inheritance error.");
  4825. },
  4826. // Returns whether the pair is available or not.
  4827. isAvailablePair: function ( s1, s2 ) {
  4828. var b1 = s1.parent;
  4829. var b2 = s2.parent;
  4830. if( b1 == b2 || // same parents
  4831. (!b1.isDynamic && !b2.isDynamic) || // static or kinematic object
  4832. (s1.belongsTo&s2.collidesWith)==0 ||
  4833. (s2.belongsTo&s1.collidesWith)==0 // collision filtering
  4834. ){ return false; }
  4835. var js;
  4836. if(b1.numJoints<b2.numJoints) js = b1.jointLink;
  4837. else js = b2.jointLink;
  4838. while(js!==null){
  4839. var joint = js.joint;
  4840. if( !joint.allowCollision && ((joint.body1==b1 && joint.body2==b2) || (joint.body1==b2 && joint.body2==b1)) ){ return false; }
  4841. js = js.next;
  4842. }
  4843. return true;
  4844. },
  4845. // Detect overlapping pairs.
  4846. detectPairs: function () {
  4847. // clear old
  4848. this.pairs = [];
  4849. this.numPairs = 0;
  4850. this.numPairChecks = 0;
  4851. this.collectPairs();
  4852. },
  4853. collectPairs: function () {
  4854. Error("BroadPhase", "Inheritance error.");
  4855. },
  4856. addPair: function ( s1, s2 ) {
  4857. var pair = new Pair( s1, s2 );
  4858. this.pairs.push(pair);
  4859. this.numPairs++;
  4860. }
  4861. });
  4862. var count$1 = 0;
  4863. function ProxyIdCount() { return count$1++; }
  4864. /**
  4865. * A proxy is used for broad-phase collecting pairs that can be colliding.
  4866. *
  4867. * @author lo-th
  4868. */
  4869. function Proxy( shape ) {
  4870. //The parent shape.
  4871. this.shape = shape;
  4872. //The axis-aligned bounding box.
  4873. this.aabb = shape.aabb;
  4874. }
  4875. Object.assign( Proxy.prototype, {
  4876. Proxy: true,
  4877. // Update the proxy. Must be inherited by a child.
  4878. update: function(){
  4879. printError("Proxy","Inheritance error.");
  4880. }
  4881. });
  4882. /**
  4883. * A basic implementation of proxies.
  4884. *
  4885. * @author saharan
  4886. */
  4887. function BasicProxy ( shape ) {
  4888. Proxy.call( this, shape );
  4889. this.id = ProxyIdCount();
  4890. }
  4891. BasicProxy.prototype = Object.assign( Object.create( Proxy.prototype ), {
  4892. constructor: BasicProxy,
  4893. update: function () {
  4894. }
  4895. });
  4896. /**
  4897. * A broad-phase algorithm with brute-force search.
  4898. * This always checks for all possible pairs.
  4899. */
  4900. function BruteForceBroadPhase(){
  4901. BroadPhase.call( this );
  4902. this.types = BR_BRUTE_FORCE;
  4903. //this.numProxies=0;
  4904. ///this.maxProxies = 256;
  4905. this.proxies = [];
  4906. //this.proxies.length = 256;
  4907. }
  4908. BruteForceBroadPhase.prototype = Object.assign( Object.create( BroadPhase.prototype ), {
  4909. constructor: BruteForceBroadPhase,
  4910. createProxy: function ( shape ) {
  4911. return new BasicProxy( shape );
  4912. },
  4913. addProxy: function ( proxy ) {
  4914. /*if(this.numProxies==this.maxProxies){
  4915. //this.maxProxies<<=1;
  4916. this.maxProxies*=2;
  4917. var newProxies=[];
  4918. newProxies.length = this.maxProxies;
  4919. var i = this.numProxies;
  4920. while(i--){
  4921. //for(var i=0, l=this.numProxies;i<l;i++){
  4922. newProxies[i]=this.proxies[i];
  4923. }
  4924. this.proxies=newProxies;
  4925. }*/
  4926. //this.proxies[this.numProxies++] = proxy;
  4927. this.proxies.push( proxy );
  4928. //this.numProxies++;
  4929. },
  4930. removeProxy: function ( proxy ) {
  4931. var n = this.proxies.indexOf( proxy );
  4932. if ( n > -1 ){
  4933. this.proxies.splice( n, 1 );
  4934. //this.numProxies--;
  4935. }
  4936. /*var i = this.numProxies;
  4937. while(i--){
  4938. //for(var i=0, l=this.numProxies;i<l;i++){
  4939. if(this.proxies[i] == proxy){
  4940. this.proxies[i] = this.proxies[--this.numProxies];
  4941. this.proxies[this.numProxies] = null;
  4942. return;
  4943. }
  4944. }*/
  4945. },
  4946. collectPairs: function () {
  4947. var i = 0, j, p1, p2;
  4948. var px = this.proxies;
  4949. var l = px.length;//this.numProxies;
  4950. //var ar1 = [];
  4951. //var ar2 = [];
  4952. //for( i = px.length ; i-- ; ar1[ i ] = px[ i ] ){};
  4953. //for( i = px.length ; i-- ; ar2[ i ] = px[ i ] ){};
  4954. //var ar1 = JSON.parse(JSON.stringify(this.proxies))
  4955. //var ar2 = JSON.parse(JSON.stringify(this.proxies))
  4956. this.numPairChecks = l*(l-1)>>1;
  4957. //this.numPairChecks=this.numProxies*(this.numProxies-1)*0.5;
  4958. while( i < l ){
  4959. p1 = px[i++];
  4960. j = i + 1;
  4961. while( j < l ){
  4962. p2 = px[j++];
  4963. if ( p1.aabb.intersectTest( p2.aabb ) || !this.isAvailablePair( p1.shape, p2.shape ) ) continue;
  4964. this.addPair( p1.shape, p2.shape );
  4965. }
  4966. }
  4967. }
  4968. });
  4969. /**
  4970. * A projection axis for sweep and prune broad-phase.
  4971. * @author saharan
  4972. */
  4973. function SAPAxis (){
  4974. this.numElements = 0;
  4975. this.bufferSize = 256;
  4976. this.elements = [];
  4977. this.elements.length = this.bufferSize;
  4978. this.stack = new Float32Array( 64 );
  4979. }
  4980. Object.assign( SAPAxis.prototype, {
  4981. SAPAxis: true,
  4982. addElements: function ( min, max ) {
  4983. if(this.numElements+2>=this.bufferSize){
  4984. //this.bufferSize<<=1;
  4985. this.bufferSize*=2;
  4986. var newElements=[];
  4987. var i = this.numElements;
  4988. while(i--){
  4989. //for(var i=0, l=this.numElements; i<l; i++){
  4990. newElements[i] = this.elements[i];
  4991. }
  4992. }
  4993. this.elements[this.numElements++] = min;
  4994. this.elements[this.numElements++] = max;
  4995. },
  4996. removeElements: function ( min, max ) {
  4997. var minIndex=-1;
  4998. var maxIndex=-1;
  4999. for(var i=0, l=this.numElements; i<l; i++){
  5000. var e=this.elements[i];
  5001. if(e==min||e==max){
  5002. if(minIndex==-1){
  5003. minIndex=i;
  5004. }else{
  5005. maxIndex=i;
  5006. break;
  5007. }
  5008. }
  5009. }
  5010. for(i = minIndex+1, l = maxIndex; i < l; i++){
  5011. this.elements[i-1] = this.elements[i];
  5012. }
  5013. for(i = maxIndex+1, l = this.numElements; i < l; i++){
  5014. this.elements[i-2] = this.elements[i];
  5015. }
  5016. this.elements[--this.numElements] = null;
  5017. this.elements[--this.numElements] = null;
  5018. },
  5019. sort: function () {
  5020. var count = 0;
  5021. var threshold = 1;
  5022. while((this.numElements >> threshold) != 0 ) threshold++;
  5023. threshold = threshold * this.numElements >> 2;
  5024. count = 0;
  5025. var giveup = false;
  5026. var elements = this.elements;
  5027. for( var i = 1, l = this.numElements; i < l; i++){ // try insertion sort
  5028. var tmp=elements[i];
  5029. var pivot=tmp.value;
  5030. var tmp2=elements[i-1];
  5031. if(tmp2.value>pivot){
  5032. var j=i;
  5033. do{
  5034. elements[j]=tmp2;
  5035. if(--j==0)break;
  5036. tmp2=elements[j-1];
  5037. }while(tmp2.value>pivot);
  5038. elements[j]=tmp;
  5039. count+=i-j;
  5040. if(count>threshold){
  5041. giveup=true; // stop and use quick sort
  5042. break;
  5043. }
  5044. }
  5045. }
  5046. if(!giveup)return;
  5047. count=2;var stack=this.stack;
  5048. stack[0]=0;
  5049. stack[1]=this.numElements-1;
  5050. while(count>0){
  5051. var right=stack[--count];
  5052. var left=stack[--count];
  5053. var diff=right-left;
  5054. if(diff>16){ // quick sort
  5055. //var mid=left+(diff>>1);
  5056. var mid = left + (_Math.floor(diff*0.5));
  5057. tmp = elements[mid];
  5058. elements[mid] = elements[right];
  5059. elements[right] = tmp;
  5060. pivot = tmp.value;
  5061. i = left-1;
  5062. j = right;
  5063. while( true ){
  5064. var ei;
  5065. var ej;
  5066. do{ ei = elements[++i]; } while( ei.value < pivot);
  5067. do{ ej = elements[--j]; } while( pivot < ej.value && j != left );
  5068. if( i >= j ) break;
  5069. elements[i] = ej;
  5070. elements[j] = ei;
  5071. }
  5072. elements[right] = elements[i];
  5073. elements[i] = tmp;
  5074. if( i - left > right - i ) {
  5075. stack[count++] = left;
  5076. stack[count++] = i - 1;
  5077. stack[count++] = i + 1;
  5078. stack[count++] = right;
  5079. }else{
  5080. stack[count++] = i + 1;
  5081. stack[count++] = right;
  5082. stack[count++] = left;
  5083. stack[count++] = i - 1;
  5084. }
  5085. }else{
  5086. for( i = left + 1; i <= right; i++ ) {
  5087. tmp = elements[i];
  5088. pivot = tmp.value;
  5089. tmp2 = elements[i-1];
  5090. if( tmp2.value > pivot ) {
  5091. j = i;
  5092. do{
  5093. elements[j] = tmp2;
  5094. if( --j == 0 ) break;
  5095. tmp2 = elements[j-1];
  5096. }while( tmp2.value > pivot );
  5097. elements[j] = tmp;
  5098. }
  5099. }
  5100. }
  5101. }
  5102. },
  5103. calculateTestCount: function () {
  5104. var num = 1;
  5105. var sum = 0;
  5106. for(var i = 1, l = this.numElements; i<l; i++){
  5107. if(this.elements[i].max){
  5108. num--;
  5109. }else{
  5110. sum += num;
  5111. num++;
  5112. }
  5113. }
  5114. return sum;
  5115. }
  5116. });
  5117. /**
  5118. * An element of proxies.
  5119. * @author saharan
  5120. */
  5121. function SAPElement ( proxy, max ) {
  5122. // The parent proxy
  5123. this.proxy = proxy;
  5124. // The pair element.
  5125. this.pair = null;
  5126. // The minimum element on other axis.
  5127. this.min1 = null;
  5128. // The maximum element on other axis.
  5129. this.max1 = null;
  5130. // The minimum element on other axis.
  5131. this.min2 = null;
  5132. // The maximum element on other axis.
  5133. this.max2 = null;
  5134. // Whether the element has maximum value or not.
  5135. this.max = max;
  5136. // The value of the element.
  5137. this.value = 0;
  5138. }
  5139. /**
  5140. * A proxy for sweep and prune broad-phase.
  5141. * @author saharan
  5142. * @author lo-th
  5143. */
  5144. function SAPProxy ( sap, shape ){
  5145. Proxy.call( this, shape );
  5146. // Type of the axis to which the proxy belongs to. [0:none, 1:dynamic, 2:static]
  5147. this.belongsTo = 0;
  5148. // The maximum elements on each axis.
  5149. this.max = [];
  5150. // The minimum elements on each axis.
  5151. this.min = [];
  5152. this.sap = sap;
  5153. this.min[0] = new SAPElement( this, false );
  5154. this.max[0] = new SAPElement( this, true );
  5155. this.min[1] = new SAPElement( this, false );
  5156. this.max[1] = new SAPElement( this, true );
  5157. this.min[2] = new SAPElement( this, false );
  5158. this.max[2] = new SAPElement( this, true );
  5159. this.max[0].pair = this.min[0];
  5160. this.max[1].pair = this.min[1];
  5161. this.max[2].pair = this.min[2];
  5162. this.min[0].min1 = this.min[1];
  5163. this.min[0].max1 = this.max[1];
  5164. this.min[0].min2 = this.min[2];
  5165. this.min[0].max2 = this.max[2];
  5166. this.min[1].min1 = this.min[0];
  5167. this.min[1].max1 = this.max[0];
  5168. this.min[1].min2 = this.min[2];
  5169. this.min[1].max2 = this.max[2];
  5170. this.min[2].min1 = this.min[0];
  5171. this.min[2].max1 = this.max[0];
  5172. this.min[2].min2 = this.min[1];
  5173. this.min[2].max2 = this.max[1];
  5174. }
  5175. SAPProxy.prototype = Object.assign( Object.create( Proxy.prototype ), {
  5176. constructor: SAPProxy,
  5177. // Returns whether the proxy is dynamic or not.
  5178. isDynamic: function () {
  5179. var body = this.shape.parent;
  5180. return body.isDynamic && !body.sleeping;
  5181. },
  5182. update: function () {
  5183. var te = this.aabb.elements;
  5184. this.min[0].value = te[0];
  5185. this.min[1].value = te[1];
  5186. this.min[2].value = te[2];
  5187. this.max[0].value = te[3];
  5188. this.max[1].value = te[4];
  5189. this.max[2].value = te[5];
  5190. if( this.belongsTo == 1 && !this.isDynamic() || this.belongsTo == 2 && this.isDynamic() ){
  5191. this.sap.removeProxy(this);
  5192. this.sap.addProxy(this);
  5193. }
  5194. }
  5195. });
  5196. /**
  5197. * A broad-phase collision detection algorithm using sweep and prune.
  5198. * @author saharan
  5199. * @author lo-th
  5200. */
  5201. function SAPBroadPhase () {
  5202. BroadPhase.call( this);
  5203. this.types = BR_SWEEP_AND_PRUNE;
  5204. this.numElementsD = 0;
  5205. this.numElementsS = 0;
  5206. // dynamic proxies
  5207. this.axesD = [
  5208. new SAPAxis(),
  5209. new SAPAxis(),
  5210. new SAPAxis()
  5211. ];
  5212. // static or sleeping proxies
  5213. this.axesS = [
  5214. new SAPAxis(),
  5215. new SAPAxis(),
  5216. new SAPAxis()
  5217. ];
  5218. this.index1 = 0;
  5219. this.index2 = 1;
  5220. }
  5221. SAPBroadPhase.prototype = Object.assign( Object.create( BroadPhase.prototype ), {
  5222. constructor: SAPBroadPhase,
  5223. createProxy: function ( shape ) {
  5224. return new SAPProxy( this, shape );
  5225. },
  5226. addProxy: function ( proxy ) {
  5227. var p = proxy;
  5228. if(p.isDynamic()){
  5229. this.axesD[0].addElements( p.min[0], p.max[0] );
  5230. this.axesD[1].addElements( p.min[1], p.max[1] );
  5231. this.axesD[2].addElements( p.min[2], p.max[2] );
  5232. p.belongsTo = 1;
  5233. this.numElementsD += 2;
  5234. } else {
  5235. this.axesS[0].addElements( p.min[0], p.max[0] );
  5236. this.axesS[1].addElements( p.min[1], p.max[1] );
  5237. this.axesS[2].addElements( p.min[2], p.max[2] );
  5238. p.belongsTo = 2;
  5239. this.numElementsS += 2;
  5240. }
  5241. },
  5242. removeProxy: function ( proxy ) {
  5243. var p = proxy;
  5244. if ( p.belongsTo == 0 ) return;
  5245. /*else if ( p.belongsTo == 1 ) {
  5246. this.axesD[0].removeElements( p.min[0], p.max[0] );
  5247. this.axesD[1].removeElements( p.min[1], p.max[1] );
  5248. this.axesD[2].removeElements( p.min[2], p.max[2] );
  5249. this.numElementsD -= 2;
  5250. } else if ( p.belongsTo == 2 ) {
  5251. this.axesS[0].removeElements( p.min[0], p.max[0] );
  5252. this.axesS[1].removeElements( p.min[1], p.max[1] );
  5253. this.axesS[2].removeElements( p.min[2], p.max[2] );
  5254. this.numElementsS -= 2;
  5255. }*/
  5256. switch( p.belongsTo ){
  5257. case 1:
  5258. this.axesD[0].removeElements( p.min[0], p.max[0] );
  5259. this.axesD[1].removeElements( p.min[1], p.max[1] );
  5260. this.axesD[2].removeElements( p.min[2], p.max[2] );
  5261. this.numElementsD -= 2;
  5262. break;
  5263. case 2:
  5264. this.axesS[0].removeElements( p.min[0], p.max[0] );
  5265. this.axesS[1].removeElements( p.min[1], p.max[1] );
  5266. this.axesS[2].removeElements( p.min[2], p.max[2] );
  5267. this.numElementsS -= 2;
  5268. break;
  5269. }
  5270. p.belongsTo = 0;
  5271. },
  5272. collectPairs: function () {
  5273. if( this.numElementsD == 0 ) return;
  5274. var axis1 = this.axesD[this.index1];
  5275. var axis2 = this.axesD[this.index2];
  5276. axis1.sort();
  5277. axis2.sort();
  5278. var count1 = axis1.calculateTestCount();
  5279. var count2 = axis2.calculateTestCount();
  5280. var elementsD;
  5281. var elementsS;
  5282. if( count1 <= count2 ){// select the best axis
  5283. axis2 = this.axesS[this.index1];
  5284. axis2.sort();
  5285. elementsD = axis1.elements;
  5286. elementsS = axis2.elements;
  5287. }else{
  5288. axis1 = this.axesS[this.index2];
  5289. axis1.sort();
  5290. elementsD = axis2.elements;
  5291. elementsS = axis1.elements;
  5292. this.index1 ^= this.index2;
  5293. this.index2 ^= this.index1;
  5294. this.index1 ^= this.index2;
  5295. }
  5296. var activeD;
  5297. var activeS;
  5298. var p = 0;
  5299. var q = 0;
  5300. while( p < this.numElementsD ){
  5301. var e1;
  5302. var dyn;
  5303. if (q == this.numElementsS ){
  5304. e1 = elementsD[p];
  5305. dyn = true;
  5306. p++;
  5307. }else{
  5308. var d = elementsD[p];
  5309. var s = elementsS[q];
  5310. if( d.value < s.value ){
  5311. e1 = d;
  5312. dyn = true;
  5313. p++;
  5314. }else{
  5315. e1 = s;
  5316. dyn = false;
  5317. q++;
  5318. }
  5319. }
  5320. if( !e1.max ){
  5321. var s1 = e1.proxy.shape;
  5322. var min1 = e1.min1.value;
  5323. var max1 = e1.max1.value;
  5324. var min2 = e1.min2.value;
  5325. var max2 = e1.max2.value;
  5326. for( var e2 = activeD; e2 != null; e2 = e2.pair ) {// test for dynamic
  5327. var s2 = e2.proxy.shape;
  5328. this.numPairChecks++;
  5329. if( min1 > e2.max1.value || max1 < e2.min1.value || min2 > e2.max2.value || max2 < e2.min2.value || !this.isAvailablePair( s1, s2 ) ) continue;
  5330. this.addPair( s1, s2 );
  5331. }
  5332. if( dyn ){
  5333. for( e2 = activeS; e2 != null; e2 = e2.pair ) {// test for static
  5334. s2 = e2.proxy.shape;
  5335. this.numPairChecks++;
  5336. if( min1 > e2.max1.value || max1 < e2.min1.value|| min2 > e2.max2.value || max2 < e2.min2.value || !this.isAvailablePair(s1,s2) ) continue;
  5337. this.addPair( s1, s2 );
  5338. }
  5339. e1.pair = activeD;
  5340. activeD = e1;
  5341. }else{
  5342. e1.pair = activeS;
  5343. activeS = e1;
  5344. }
  5345. }else{
  5346. var min = e1.pair;
  5347. if( dyn ){
  5348. if( min == activeD ){
  5349. activeD = activeD.pair;
  5350. continue;
  5351. }else{
  5352. e1 = activeD;
  5353. }
  5354. }else{
  5355. if( min == activeS ){
  5356. activeS = activeS.pair;
  5357. continue;
  5358. }else{
  5359. e1 = activeS;
  5360. }
  5361. }
  5362. do{
  5363. e2 = e1.pair;
  5364. if( e2 == min ){
  5365. e1.pair = e2.pair;
  5366. break;
  5367. }
  5368. e1 = e2;
  5369. }while( e1 != null );
  5370. }
  5371. }
  5372. this.index2 = (this.index1|this.index2)^3;
  5373. }
  5374. });
  5375. /**
  5376. * A node of the dynamic bounding volume tree.
  5377. * @author saharan
  5378. */
  5379. function DBVTNode(){
  5380. // The first child node of this node.
  5381. this.child1 = null;
  5382. // The second child node of this node.
  5383. this.child2 = null;
  5384. // The parent node of this tree.
  5385. this.parent = null;
  5386. // The proxy of this node. This has no value if this node is not leaf.
  5387. this.proxy = null;
  5388. // The maximum distance from leaf nodes.
  5389. this.height = 0;
  5390. // The AABB of this node.
  5391. this.aabb = new AABB();
  5392. }
  5393. /**
  5394. * A dynamic bounding volume tree for the broad-phase algorithm.
  5395. *
  5396. * @author saharan
  5397. * @author lo-th
  5398. */
  5399. function DBVT(){
  5400. // The root of the tree.
  5401. this.root = null;
  5402. this.freeNodes = [];
  5403. this.freeNodes.length = 16384;
  5404. this.numFreeNodes = 0;
  5405. this.aabb = new AABB();
  5406. }
  5407. Object.assign( DBVT.prototype, {
  5408. DBVT: true,
  5409. moveLeaf: function( leaf ) {
  5410. this.deleteLeaf( leaf );
  5411. this.insertLeaf( leaf );
  5412. },
  5413. insertLeaf: function ( leaf ) {
  5414. if(this.root == null){
  5415. this.root = leaf;
  5416. return;
  5417. }
  5418. var lb = leaf.aabb;
  5419. var sibling = this.root;
  5420. var oldArea;
  5421. var newArea;
  5422. while(sibling.proxy == null){ // descend the node to search the best pair
  5423. var c1 = sibling.child1;
  5424. var c2 = sibling.child2;
  5425. var b = sibling.aabb;
  5426. var c1b = c1.aabb;
  5427. var c2b = c2.aabb;
  5428. oldArea = b.surfaceArea();
  5429. this.aabb.combine(lb,b);
  5430. newArea = this.aabb.surfaceArea();
  5431. var creatingCost = newArea*2;
  5432. var incrementalCost = (newArea-oldArea)*2; // cost of creating a new pair with the node
  5433. var discendingCost1 = incrementalCost;
  5434. this.aabb.combine(lb,c1b);
  5435. if(c1.proxy!=null){
  5436. // leaf cost = area(combined aabb)
  5437. discendingCost1+=this.aabb.surfaceArea();
  5438. }else{
  5439. // node cost = area(combined aabb) - area(old aabb)
  5440. discendingCost1+=this.aabb.surfaceArea()-c1b.surfaceArea();
  5441. }
  5442. var discendingCost2=incrementalCost;
  5443. this.aabb.combine(lb,c2b);
  5444. if(c2.proxy!=null){
  5445. // leaf cost = area(combined aabb)
  5446. discendingCost2+=this.aabb.surfaceArea();
  5447. }else{
  5448. // node cost = area(combined aabb) - area(old aabb)
  5449. discendingCost2+=this.aabb.surfaceArea()-c2b.surfaceArea();
  5450. }
  5451. if(discendingCost1<discendingCost2){
  5452. if(creatingCost<discendingCost1){
  5453. break;// stop descending
  5454. }else{
  5455. sibling = c1;// descend into first child
  5456. }
  5457. }else{
  5458. if(creatingCost<discendingCost2){
  5459. break;// stop descending
  5460. }else{
  5461. sibling = c2;// descend into second child
  5462. }
  5463. }
  5464. }
  5465. var oldParent = sibling.parent;
  5466. var newParent;
  5467. if(this.numFreeNodes>0){
  5468. newParent = this.freeNodes[--this.numFreeNodes];
  5469. }else{
  5470. newParent = new DBVTNode();
  5471. }
  5472. newParent.parent = oldParent;
  5473. newParent.child1 = leaf;
  5474. newParent.child2 = sibling;
  5475. newParent.aabb.combine(leaf.aabb,sibling.aabb);
  5476. newParent.height = sibling.height+1;
  5477. sibling.parent = newParent;
  5478. leaf.parent = newParent;
  5479. if(sibling == this.root){
  5480. // replace root
  5481. this.root = newParent;
  5482. }else{
  5483. // replace child
  5484. if(oldParent.child1 == sibling){
  5485. oldParent.child1 = newParent;
  5486. }else{
  5487. oldParent.child2 = newParent;
  5488. }
  5489. }
  5490. // update whole tree
  5491. do{
  5492. newParent = this.balance(newParent);
  5493. this.fix(newParent);
  5494. newParent = newParent.parent;
  5495. }while(newParent != null);
  5496. },
  5497. getBalance: function( node ) {
  5498. if(node.proxy!=null)return 0;
  5499. return node.child1.height-node.child2.height;
  5500. },
  5501. deleteLeaf: function( leaf ) {
  5502. if(leaf == this.root){
  5503. this.root = null;
  5504. return;
  5505. }
  5506. var parent = leaf.parent;
  5507. var sibling;
  5508. if(parent.child1==leaf){
  5509. sibling=parent.child2;
  5510. }else{
  5511. sibling=parent.child1;
  5512. }
  5513. if(parent==this.root){
  5514. this.root=sibling;
  5515. sibling.parent=null;
  5516. return;
  5517. }
  5518. var grandParent = parent.parent;
  5519. sibling.parent = grandParent;
  5520. if(grandParent.child1 == parent ) {
  5521. grandParent.child1 = sibling;
  5522. }else{
  5523. grandParent.child2 = sibling;
  5524. }
  5525. if(this.numFreeNodes<16384){
  5526. this.freeNodes[this.numFreeNodes++] = parent;
  5527. }
  5528. do{
  5529. grandParent = this.balance(grandParent);
  5530. this.fix(grandParent);
  5531. grandParent = grandParent.parent;
  5532. }while( grandParent != null );
  5533. },
  5534. balance: function( node ) {
  5535. var nh = node.height;
  5536. if(nh<2){
  5537. return node;
  5538. }
  5539. var p = node.parent;
  5540. var l = node.child1;
  5541. var r = node.child2;
  5542. var lh = l.height;
  5543. var rh = r.height;
  5544. var balance = lh-rh;
  5545. var t;// for bit operation
  5546. // [ N ]
  5547. // / \
  5548. // [ L ] [ R ]
  5549. // / \ / \
  5550. // [L-L] [L-R] [R-L] [R-R]
  5551. // Is the tree balanced?
  5552. if(balance>1){
  5553. var ll = l.child1;
  5554. var lr = l.child2;
  5555. var llh = ll.height;
  5556. var lrh = lr.height;
  5557. // Is L-L higher than L-R?
  5558. if(llh>lrh){
  5559. // set N to L-R
  5560. l.child2 = node;
  5561. node.parent = l;
  5562. // [ L ]
  5563. // / \
  5564. // [L-L] [ N ]
  5565. // / \ / \
  5566. // [...] [...] [ L ] [ R ]
  5567. // set L-R
  5568. node.child1 = lr;
  5569. lr.parent = node;
  5570. // [ L ]
  5571. // / \
  5572. // [L-L] [ N ]
  5573. // / \ / \
  5574. // [...] [...] [L-R] [ R ]
  5575. // fix bounds and heights
  5576. node.aabb.combine( lr.aabb, r.aabb );
  5577. t = lrh-rh;
  5578. node.height=lrh-(t&t>>31)+1;
  5579. l.aabb.combine(ll.aabb,node.aabb);
  5580. t=llh-nh;
  5581. l.height=llh-(t&t>>31)+1;
  5582. }else{
  5583. // set N to L-L
  5584. l.child1=node;
  5585. node.parent=l;
  5586. // [ L ]
  5587. // / \
  5588. // [ N ] [L-R]
  5589. // / \ / \
  5590. // [ L ] [ R ] [...] [...]
  5591. // set L-L
  5592. node.child1 = ll;
  5593. ll.parent = node;
  5594. // [ L ]
  5595. // / \
  5596. // [ N ] [L-R]
  5597. // / \ / \
  5598. // [L-L] [ R ] [...] [...]
  5599. // fix bounds and heights
  5600. node.aabb.combine(ll.aabb,r.aabb);
  5601. t = llh - rh;
  5602. node.height=llh-(t&t>>31)+1;
  5603. l.aabb.combine(node.aabb,lr.aabb);
  5604. t=nh-lrh;
  5605. l.height=nh-(t&t>>31)+1;
  5606. }
  5607. // set new parent of L
  5608. if(p!=null){
  5609. if(p.child1==node){
  5610. p.child1=l;
  5611. }else{
  5612. p.child2=l;
  5613. }
  5614. }else{
  5615. this.root=l;
  5616. }
  5617. l.parent=p;
  5618. return l;
  5619. }else if(balance<-1){
  5620. var rl = r.child1;
  5621. var rr = r.child2;
  5622. var rlh = rl.height;
  5623. var rrh = rr.height;
  5624. // Is R-L higher than R-R?
  5625. if( rlh > rrh ) {
  5626. // set N to R-R
  5627. r.child2 = node;
  5628. node.parent = r;
  5629. // [ R ]
  5630. // / \
  5631. // [R-L] [ N ]
  5632. // / \ / \
  5633. // [...] [...] [ L ] [ R ]
  5634. // set R-R
  5635. node.child2 = rr;
  5636. rr.parent = node;
  5637. // [ R ]
  5638. // / \
  5639. // [R-L] [ N ]
  5640. // / \ / \
  5641. // [...] [...] [ L ] [R-R]
  5642. // fix bounds and heights
  5643. node.aabb.combine(l.aabb,rr.aabb);
  5644. t = lh-rrh;
  5645. node.height = lh-(t&t>>31)+1;
  5646. r.aabb.combine(rl.aabb,node.aabb);
  5647. t = rlh-nh;
  5648. r.height = rlh-(t&t>>31)+1;
  5649. }else{
  5650. // set N to R-L
  5651. r.child1 = node;
  5652. node.parent = r;
  5653. // [ R ]
  5654. // / \
  5655. // [ N ] [R-R]
  5656. // / \ / \
  5657. // [ L ] [ R ] [...] [...]
  5658. // set R-L
  5659. node.child2 = rl;
  5660. rl.parent = node;
  5661. // [ R ]
  5662. // / \
  5663. // [ N ] [R-R]
  5664. // / \ / \
  5665. // [ L ] [R-L] [...] [...]
  5666. // fix bounds and heights
  5667. node.aabb.combine(l.aabb,rl.aabb);
  5668. t=lh-rlh;
  5669. node.height=lh-(t&t>>31)+1;
  5670. r.aabb.combine(node.aabb,rr.aabb);
  5671. t=nh-rrh;
  5672. r.height=nh-(t&t>>31)+1;
  5673. }
  5674. // set new parent of R
  5675. if(p!=null){
  5676. if(p.child1==node){
  5677. p.child1=r;
  5678. }else{
  5679. p.child2=r;
  5680. }
  5681. }else{
  5682. this.root=r;
  5683. }
  5684. r.parent=p;
  5685. return r;
  5686. }
  5687. return node;
  5688. },
  5689. fix: function ( node ) {
  5690. var c1 = node.child1;
  5691. var c2 = node.child2;
  5692. node.aabb.combine( c1.aabb, c2.aabb );
  5693. node.height = c1.height < c2.height ? c2.height+1 : c1.height+1;
  5694. }
  5695. });
  5696. /**
  5697. * A proxy for dynamic bounding volume tree broad-phase.
  5698. * @author saharan
  5699. */
  5700. function DBVTProxy ( shape ) {
  5701. Proxy.call( this, shape);
  5702. // The leaf of the proxy.
  5703. this.leaf = new DBVTNode();
  5704. this.leaf.proxy = this;
  5705. }
  5706. DBVTProxy.prototype = Object.assign( Object.create( Proxy.prototype ), {
  5707. constructor: DBVTProxy,
  5708. update: function () {
  5709. }
  5710. });
  5711. /**
  5712. * A broad-phase algorithm using dynamic bounding volume tree.
  5713. *
  5714. * @author saharan
  5715. * @author lo-th
  5716. */
  5717. function DBVTBroadPhase(){
  5718. BroadPhase.call( this );
  5719. this.types = BR_BOUNDING_VOLUME_TREE;
  5720. this.tree = new DBVT();
  5721. this.stack = [];
  5722. this.leaves = [];
  5723. this.numLeaves = 0;
  5724. }
  5725. DBVTBroadPhase.prototype = Object.assign( Object.create( BroadPhase.prototype ), {
  5726. constructor: DBVTBroadPhase,
  5727. createProxy: function ( shape ) {
  5728. return new DBVTProxy( shape );
  5729. },
  5730. addProxy: function ( proxy ) {
  5731. this.tree.insertLeaf( proxy.leaf );
  5732. this.leaves.push( proxy.leaf );
  5733. this.numLeaves++;
  5734. },
  5735. removeProxy: function ( proxy ) {
  5736. this.tree.deleteLeaf( proxy.leaf );
  5737. var n = this.leaves.indexOf( proxy.leaf );
  5738. if ( n > -1 ) {
  5739. this.leaves.splice(n,1);
  5740. this.numLeaves--;
  5741. }
  5742. },
  5743. collectPairs: function () {
  5744. if ( this.numLeaves < 2 ) return;
  5745. var leaf, margin = 0.1, i = this.numLeaves;
  5746. while(i--){
  5747. leaf = this.leaves[i];
  5748. if ( leaf.proxy.aabb.intersectTestTwo( leaf.aabb ) ){
  5749. leaf.aabb.copy( leaf.proxy.aabb, margin );
  5750. this.tree.deleteLeaf( leaf );
  5751. this.tree.insertLeaf( leaf );
  5752. this.collide( leaf, this.tree.root );
  5753. }
  5754. }
  5755. },
  5756. collide: function ( node1, node2 ) {
  5757. var stackCount = 2;
  5758. var s1, s2, n1, n2, l1, l2;
  5759. this.stack[0] = node1;
  5760. this.stack[1] = node2;
  5761. while( stackCount > 0 ){
  5762. n1 = this.stack[--stackCount];
  5763. n2 = this.stack[--stackCount];
  5764. l1 = n1.proxy != null;
  5765. l2 = n2.proxy != null;
  5766. this.numPairChecks++;
  5767. if( l1 && l2 ){
  5768. s1 = n1.proxy.shape;
  5769. s2 = n2.proxy.shape;
  5770. if ( s1 == s2 || s1.aabb.intersectTest( s2.aabb ) || !this.isAvailablePair( s1, s2 ) ) continue;
  5771. this.addPair(s1,s2);
  5772. }else{
  5773. if ( n1.aabb.intersectTest( n2.aabb ) ) continue;
  5774. /*if(stackCount+4>=this.maxStack){// expand the stack
  5775. //this.maxStack<<=1;
  5776. this.maxStack*=2;
  5777. var newStack = [];// vector
  5778. newStack.length = this.maxStack;
  5779. for(var i=0;i<stackCount;i++){
  5780. newStack[i] = this.stack[i];
  5781. }
  5782. this.stack = newStack;
  5783. }*/
  5784. if( l2 || !l1 && (n1.aabb.surfaceArea() > n2.aabb.surfaceArea()) ){
  5785. this.stack[stackCount++] = n1.child1;
  5786. this.stack[stackCount++] = n2;
  5787. this.stack[stackCount++] = n1.child2;
  5788. this.stack[stackCount++] = n2;
  5789. }else{
  5790. this.stack[stackCount++] = n1;
  5791. this.stack[stackCount++] = n2.child1;
  5792. this.stack[stackCount++] = n1;
  5793. this.stack[stackCount++] = n2.child2;
  5794. }
  5795. }
  5796. }
  5797. }
  5798. });
  5799. function CollisionDetector (){
  5800. this.flip = false;
  5801. }
  5802. Object.assign( CollisionDetector.prototype, {
  5803. CollisionDetector: true,
  5804. detectCollision: function ( shape1, shape2, manifold ) {
  5805. printError("CollisionDetector", "Inheritance error.");
  5806. }
  5807. } );
  5808. /**
  5809. * A collision detector which detects collisions between two boxes.
  5810. * @author saharan
  5811. */
  5812. function BoxBoxCollisionDetector() {
  5813. CollisionDetector.call( this );
  5814. this.clipVertices1 = new Float32Array( 24 ); // 8 vertices x,y,z
  5815. this.clipVertices2 = new Float32Array( 24 );
  5816. this.used = new Float32Array( 8 );
  5817. this.INF = 1/0;
  5818. }
  5819. BoxBoxCollisionDetector.prototype = Object.assign( Object.create( CollisionDetector.prototype ), {
  5820. constructor: BoxBoxCollisionDetector,
  5821. detectCollision: function ( shape1, shape2, manifold ) {
  5822. // What you are doing
  5823. // · I to prepare a separate axis of the fifteen
  5824. //-Six in each of three normal vectors of the xyz direction of the box both
  5825. // · Remaining nine 3x3 a vector perpendicular to the side of the box 2 and the side of the box 1
  5826. // · Calculate the depth to the separation axis
  5827. // Calculates the distance using the inner product and put the amount of embedment
  5828. // · However a vertical separation axis and side to weight a little to avoid vibration
  5829. // And end when there is a separate axis that is remote even one
  5830. // · I look for separation axis with little to dent most
  5831. // Men and if separation axis of the first six - end collision
  5832. // Heng If it separate axis of nine other - side collision
  5833. // Heng - case of a side collision
  5834. // · Find points of two sides on which you made ​​the separation axis
  5835. // Calculates the point of closest approach of a straight line consisting of separate axis points obtained, and the collision point
  5836. //-Surface - the case of the plane crash
  5837. //-Box A, box B and the other a box of better made ​​a separate axis
  5838. // • The surface A and the plane that made the separation axis of the box A, and B to the surface the face of the box B close in the opposite direction to the most isolated axis
  5839. // When viewed from the front surface A, and the cut part exceeding the area of the surface A is a surface B
  5840. //-Plane B becomes the 3-8 triangle, I a candidate for the collision point the vertex of surface B
  5841. // • If more than one candidate 5 exists, scraping up to four
  5842. // For potential collision points of all, to examine the distance between the surface A
  5843. // • If you were on the inside surface of A, and the collision point
  5844. var b1;
  5845. var b2;
  5846. if(shape1.id<shape2.id){
  5847. b1=(shape1);
  5848. b2=(shape2);
  5849. }else{
  5850. b1=(shape2);
  5851. b2=(shape1);
  5852. }
  5853. var V1 = b1.elements;
  5854. var V2 = b2.elements;
  5855. var D1 = b1.dimentions;
  5856. var D2 = b2.dimentions;
  5857. var p1=b1.position;
  5858. var p2=b2.position;
  5859. var p1x=p1.x;
  5860. var p1y=p1.y;
  5861. var p1z=p1.z;
  5862. var p2x=p2.x;
  5863. var p2y=p2.y;
  5864. var p2z=p2.z;
  5865. // diff
  5866. var dx=p2x-p1x;
  5867. var dy=p2y-p1y;
  5868. var dz=p2z-p1z;
  5869. // distance
  5870. var w1=b1.halfWidth;
  5871. var h1=b1.halfHeight;
  5872. var d1=b1.halfDepth;
  5873. var w2=b2.halfWidth;
  5874. var h2=b2.halfHeight;
  5875. var d2=b2.halfDepth;
  5876. // direction
  5877. // ----------------------------
  5878. // 15 separating axes
  5879. // 1~6: face
  5880. // 7~f: edge
  5881. // http://marupeke296.com/COL_3D_No13_OBBvsOBB.html
  5882. // ----------------------------
  5883. var a1x=D1[0];
  5884. var a1y=D1[1];
  5885. var a1z=D1[2];
  5886. var a2x=D1[3];
  5887. var a2y=D1[4];
  5888. var a2z=D1[5];
  5889. var a3x=D1[6];
  5890. var a3y=D1[7];
  5891. var a3z=D1[8];
  5892. var d1x=D1[9];
  5893. var d1y=D1[10];
  5894. var d1z=D1[11];
  5895. var d2x=D1[12];
  5896. var d2y=D1[13];
  5897. var d2z=D1[14];
  5898. var d3x=D1[15];
  5899. var d3y=D1[16];
  5900. var d3z=D1[17];
  5901. var a4x=D2[0];
  5902. var a4y=D2[1];
  5903. var a4z=D2[2];
  5904. var a5x=D2[3];
  5905. var a5y=D2[4];
  5906. var a5z=D2[5];
  5907. var a6x=D2[6];
  5908. var a6y=D2[7];
  5909. var a6z=D2[8];
  5910. var d4x=D2[9];
  5911. var d4y=D2[10];
  5912. var d4z=D2[11];
  5913. var d5x=D2[12];
  5914. var d5y=D2[13];
  5915. var d5z=D2[14];
  5916. var d6x=D2[15];
  5917. var d6y=D2[16];
  5918. var d6z=D2[17];
  5919. var a7x=a1y*a4z-a1z*a4y;
  5920. var a7y=a1z*a4x-a1x*a4z;
  5921. var a7z=a1x*a4y-a1y*a4x;
  5922. var a8x=a1y*a5z-a1z*a5y;
  5923. var a8y=a1z*a5x-a1x*a5z;
  5924. var a8z=a1x*a5y-a1y*a5x;
  5925. var a9x=a1y*a6z-a1z*a6y;
  5926. var a9y=a1z*a6x-a1x*a6z;
  5927. var a9z=a1x*a6y-a1y*a6x;
  5928. var aax=a2y*a4z-a2z*a4y;
  5929. var aay=a2z*a4x-a2x*a4z;
  5930. var aaz=a2x*a4y-a2y*a4x;
  5931. var abx=a2y*a5z-a2z*a5y;
  5932. var aby=a2z*a5x-a2x*a5z;
  5933. var abz=a2x*a5y-a2y*a5x;
  5934. var acx=a2y*a6z-a2z*a6y;
  5935. var acy=a2z*a6x-a2x*a6z;
  5936. var acz=a2x*a6y-a2y*a6x;
  5937. var adx=a3y*a4z-a3z*a4y;
  5938. var ady=a3z*a4x-a3x*a4z;
  5939. var adz=a3x*a4y-a3y*a4x;
  5940. var aex=a3y*a5z-a3z*a5y;
  5941. var aey=a3z*a5x-a3x*a5z;
  5942. var aez=a3x*a5y-a3y*a5x;
  5943. var afx=a3y*a6z-a3z*a6y;
  5944. var afy=a3z*a6x-a3x*a6z;
  5945. var afz=a3x*a6y-a3y*a6x;
  5946. // right or left flags
  5947. var right1;
  5948. var right2;
  5949. var right3;
  5950. var right4;
  5951. var right5;
  5952. var right6;
  5953. var right7;
  5954. var right8;
  5955. var right9;
  5956. var righta;
  5957. var rightb;
  5958. var rightc;
  5959. var rightd;
  5960. var righte;
  5961. var rightf;
  5962. // overlapping distances
  5963. var overlap1;
  5964. var overlap2;
  5965. var overlap3;
  5966. var overlap4;
  5967. var overlap5;
  5968. var overlap6;
  5969. var overlap7;
  5970. var overlap8;
  5971. var overlap9;
  5972. var overlapa;
  5973. var overlapb;
  5974. var overlapc;
  5975. var overlapd;
  5976. var overlape;
  5977. var overlapf;
  5978. // invalid flags
  5979. var invalid7=false;
  5980. var invalid8=false;
  5981. var invalid9=false;
  5982. var invalida=false;
  5983. var invalidb=false;
  5984. var invalidc=false;
  5985. var invalidd=false;
  5986. var invalide=false;
  5987. var invalidf=false;
  5988. // temporary variables
  5989. var len;
  5990. var len1;
  5991. var len2;
  5992. var dot1;
  5993. var dot2;
  5994. var dot3;
  5995. // try axis 1
  5996. len=a1x*dx+a1y*dy+a1z*dz;
  5997. right1=len>0;
  5998. if(!right1)len=-len;
  5999. len1=w1;
  6000. dot1=a1x*a4x+a1y*a4y+a1z*a4z;
  6001. dot2=a1x*a5x+a1y*a5y+a1z*a5z;
  6002. dot3=a1x*a6x+a1y*a6y+a1z*a6z;
  6003. if(dot1<0)dot1=-dot1;
  6004. if(dot2<0)dot2=-dot2;
  6005. if(dot3<0)dot3=-dot3;
  6006. len2=dot1*w2+dot2*h2+dot3*d2;
  6007. overlap1=len-len1-len2;
  6008. if(overlap1>0)return;
  6009. // try axis 2
  6010. len=a2x*dx+a2y*dy+a2z*dz;
  6011. right2=len>0;
  6012. if(!right2)len=-len;
  6013. len1=h1;
  6014. dot1=a2x*a4x+a2y*a4y+a2z*a4z;
  6015. dot2=a2x*a5x+a2y*a5y+a2z*a5z;
  6016. dot3=a2x*a6x+a2y*a6y+a2z*a6z;
  6017. if(dot1<0)dot1=-dot1;
  6018. if(dot2<0)dot2=-dot2;
  6019. if(dot3<0)dot3=-dot3;
  6020. len2=dot1*w2+dot2*h2+dot3*d2;
  6021. overlap2=len-len1-len2;
  6022. if(overlap2>0)return;
  6023. // try axis 3
  6024. len=a3x*dx+a3y*dy+a3z*dz;
  6025. right3=len>0;
  6026. if(!right3)len=-len;
  6027. len1=d1;
  6028. dot1=a3x*a4x+a3y*a4y+a3z*a4z;
  6029. dot2=a3x*a5x+a3y*a5y+a3z*a5z;
  6030. dot3=a3x*a6x+a3y*a6y+a3z*a6z;
  6031. if(dot1<0)dot1=-dot1;
  6032. if(dot2<0)dot2=-dot2;
  6033. if(dot3<0)dot3=-dot3;
  6034. len2=dot1*w2+dot2*h2+dot3*d2;
  6035. overlap3=len-len1-len2;
  6036. if(overlap3>0)return;
  6037. // try axis 4
  6038. len=a4x*dx+a4y*dy+a4z*dz;
  6039. right4=len>0;
  6040. if(!right4)len=-len;
  6041. dot1=a4x*a1x+a4y*a1y+a4z*a1z;
  6042. dot2=a4x*a2x+a4y*a2y+a4z*a2z;
  6043. dot3=a4x*a3x+a4y*a3y+a4z*a3z;
  6044. if(dot1<0)dot1=-dot1;
  6045. if(dot2<0)dot2=-dot2;
  6046. if(dot3<0)dot3=-dot3;
  6047. len1=dot1*w1+dot2*h1+dot3*d1;
  6048. len2=w2;
  6049. overlap4=(len-len1-len2)*1.0;
  6050. if(overlap4>0)return;
  6051. // try axis 5
  6052. len=a5x*dx+a5y*dy+a5z*dz;
  6053. right5=len>0;
  6054. if(!right5)len=-len;
  6055. dot1=a5x*a1x+a5y*a1y+a5z*a1z;
  6056. dot2=a5x*a2x+a5y*a2y+a5z*a2z;
  6057. dot3=a5x*a3x+a5y*a3y+a5z*a3z;
  6058. if(dot1<0)dot1=-dot1;
  6059. if(dot2<0)dot2=-dot2;
  6060. if(dot3<0)dot3=-dot3;
  6061. len1=dot1*w1+dot2*h1+dot3*d1;
  6062. len2=h2;
  6063. overlap5=(len-len1-len2)*1.0;
  6064. if(overlap5>0)return;
  6065. // try axis 6
  6066. len=a6x*dx+a6y*dy+a6z*dz;
  6067. right6=len>0;
  6068. if(!right6)len=-len;
  6069. dot1=a6x*a1x+a6y*a1y+a6z*a1z;
  6070. dot2=a6x*a2x+a6y*a2y+a6z*a2z;
  6071. dot3=a6x*a3x+a6y*a3y+a6z*a3z;
  6072. if(dot1<0)dot1=-dot1;
  6073. if(dot2<0)dot2=-dot2;
  6074. if(dot3<0)dot3=-dot3;
  6075. len1=dot1*w1+dot2*h1+dot3*d1;
  6076. len2=d2;
  6077. overlap6=(len-len1-len2)*1.0;
  6078. if(overlap6>0)return;
  6079. // try axis 7
  6080. len=a7x*a7x+a7y*a7y+a7z*a7z;
  6081. if(len>1e-5){
  6082. len=1/_Math.sqrt(len);
  6083. a7x*=len;
  6084. a7y*=len;
  6085. a7z*=len;
  6086. len=a7x*dx+a7y*dy+a7z*dz;
  6087. right7=len>0;
  6088. if(!right7)len=-len;
  6089. dot1=a7x*a2x+a7y*a2y+a7z*a2z;
  6090. dot2=a7x*a3x+a7y*a3y+a7z*a3z;
  6091. if(dot1<0)dot1=-dot1;
  6092. if(dot2<0)dot2=-dot2;
  6093. len1=dot1*h1+dot2*d1;
  6094. dot1=a7x*a5x+a7y*a5y+a7z*a5z;
  6095. dot2=a7x*a6x+a7y*a6y+a7z*a6z;
  6096. if(dot1<0)dot1=-dot1;
  6097. if(dot2<0)dot2=-dot2;
  6098. len2=dot1*h2+dot2*d2;
  6099. overlap7=len-len1-len2;
  6100. if(overlap7>0)return;
  6101. }else{
  6102. right7=false;
  6103. overlap7=0;
  6104. invalid7=true;
  6105. }
  6106. // try axis 8
  6107. len=a8x*a8x+a8y*a8y+a8z*a8z;
  6108. if(len>1e-5){
  6109. len=1/_Math.sqrt(len);
  6110. a8x*=len;
  6111. a8y*=len;
  6112. a8z*=len;
  6113. len=a8x*dx+a8y*dy+a8z*dz;
  6114. right8=len>0;
  6115. if(!right8)len=-len;
  6116. dot1=a8x*a2x+a8y*a2y+a8z*a2z;
  6117. dot2=a8x*a3x+a8y*a3y+a8z*a3z;
  6118. if(dot1<0)dot1=-dot1;
  6119. if(dot2<0)dot2=-dot2;
  6120. len1=dot1*h1+dot2*d1;
  6121. dot1=a8x*a4x+a8y*a4y+a8z*a4z;
  6122. dot2=a8x*a6x+a8y*a6y+a8z*a6z;
  6123. if(dot1<0)dot1=-dot1;
  6124. if(dot2<0)dot2=-dot2;
  6125. len2=dot1*w2+dot2*d2;
  6126. overlap8=len-len1-len2;
  6127. if(overlap8>0)return;
  6128. }else{
  6129. right8=false;
  6130. overlap8=0;
  6131. invalid8=true;
  6132. }
  6133. // try axis 9
  6134. len=a9x*a9x+a9y*a9y+a9z*a9z;
  6135. if(len>1e-5){
  6136. len=1/_Math.sqrt(len);
  6137. a9x*=len;
  6138. a9y*=len;
  6139. a9z*=len;
  6140. len=a9x*dx+a9y*dy+a9z*dz;
  6141. right9=len>0;
  6142. if(!right9)len=-len;
  6143. dot1=a9x*a2x+a9y*a2y+a9z*a2z;
  6144. dot2=a9x*a3x+a9y*a3y+a9z*a3z;
  6145. if(dot1<0)dot1=-dot1;
  6146. if(dot2<0)dot2=-dot2;
  6147. len1=dot1*h1+dot2*d1;
  6148. dot1=a9x*a4x+a9y*a4y+a9z*a4z;
  6149. dot2=a9x*a5x+a9y*a5y+a9z*a5z;
  6150. if(dot1<0)dot1=-dot1;
  6151. if(dot2<0)dot2=-dot2;
  6152. len2=dot1*w2+dot2*h2;
  6153. overlap9=len-len1-len2;
  6154. if(overlap9>0)return;
  6155. }else{
  6156. right9=false;
  6157. overlap9=0;
  6158. invalid9=true;
  6159. }
  6160. // try axis 10
  6161. len=aax*aax+aay*aay+aaz*aaz;
  6162. if(len>1e-5){
  6163. len=1/_Math.sqrt(len);
  6164. aax*=len;
  6165. aay*=len;
  6166. aaz*=len;
  6167. len=aax*dx+aay*dy+aaz*dz;
  6168. righta=len>0;
  6169. if(!righta)len=-len;
  6170. dot1=aax*a1x+aay*a1y+aaz*a1z;
  6171. dot2=aax*a3x+aay*a3y+aaz*a3z;
  6172. if(dot1<0)dot1=-dot1;
  6173. if(dot2<0)dot2=-dot2;
  6174. len1=dot1*w1+dot2*d1;
  6175. dot1=aax*a5x+aay*a5y+aaz*a5z;
  6176. dot2=aax*a6x+aay*a6y+aaz*a6z;
  6177. if(dot1<0)dot1=-dot1;
  6178. if(dot2<0)dot2=-dot2;
  6179. len2=dot1*h2+dot2*d2;
  6180. overlapa=len-len1-len2;
  6181. if(overlapa>0)return;
  6182. }else{
  6183. righta=false;
  6184. overlapa=0;
  6185. invalida=true;
  6186. }
  6187. // try axis 11
  6188. len=abx*abx+aby*aby+abz*abz;
  6189. if(len>1e-5){
  6190. len=1/_Math.sqrt(len);
  6191. abx*=len;
  6192. aby*=len;
  6193. abz*=len;
  6194. len=abx*dx+aby*dy+abz*dz;
  6195. rightb=len>0;
  6196. if(!rightb)len=-len;
  6197. dot1=abx*a1x+aby*a1y+abz*a1z;
  6198. dot2=abx*a3x+aby*a3y+abz*a3z;
  6199. if(dot1<0)dot1=-dot1;
  6200. if(dot2<0)dot2=-dot2;
  6201. len1=dot1*w1+dot2*d1;
  6202. dot1=abx*a4x+aby*a4y+abz*a4z;
  6203. dot2=abx*a6x+aby*a6y+abz*a6z;
  6204. if(dot1<0)dot1=-dot1;
  6205. if(dot2<0)dot2=-dot2;
  6206. len2=dot1*w2+dot2*d2;
  6207. overlapb=len-len1-len2;
  6208. if(overlapb>0)return;
  6209. }else{
  6210. rightb=false;
  6211. overlapb=0;
  6212. invalidb=true;
  6213. }
  6214. // try axis 12
  6215. len=acx*acx+acy*acy+acz*acz;
  6216. if(len>1e-5){
  6217. len=1/_Math.sqrt(len);
  6218. acx*=len;
  6219. acy*=len;
  6220. acz*=len;
  6221. len=acx*dx+acy*dy+acz*dz;
  6222. rightc=len>0;
  6223. if(!rightc)len=-len;
  6224. dot1=acx*a1x+acy*a1y+acz*a1z;
  6225. dot2=acx*a3x+acy*a3y+acz*a3z;
  6226. if(dot1<0)dot1=-dot1;
  6227. if(dot2<0)dot2=-dot2;
  6228. len1=dot1*w1+dot2*d1;
  6229. dot1=acx*a4x+acy*a4y+acz*a4z;
  6230. dot2=acx*a5x+acy*a5y+acz*a5z;
  6231. if(dot1<0)dot1=-dot1;
  6232. if(dot2<0)dot2=-dot2;
  6233. len2=dot1*w2+dot2*h2;
  6234. overlapc=len-len1-len2;
  6235. if(overlapc>0)return;
  6236. }else{
  6237. rightc=false;
  6238. overlapc=0;
  6239. invalidc=true;
  6240. }
  6241. // try axis 13
  6242. len=adx*adx+ady*ady+adz*adz;
  6243. if(len>1e-5){
  6244. len=1/_Math.sqrt(len);
  6245. adx*=len;
  6246. ady*=len;
  6247. adz*=len;
  6248. len=adx*dx+ady*dy+adz*dz;
  6249. rightd=len>0;
  6250. if(!rightd)len=-len;
  6251. dot1=adx*a1x+ady*a1y+adz*a1z;
  6252. dot2=adx*a2x+ady*a2y+adz*a2z;
  6253. if(dot1<0)dot1=-dot1;
  6254. if(dot2<0)dot2=-dot2;
  6255. len1=dot1*w1+dot2*h1;
  6256. dot1=adx*a5x+ady*a5y+adz*a5z;
  6257. dot2=adx*a6x+ady*a6y+adz*a6z;
  6258. if(dot1<0)dot1=-dot1;
  6259. if(dot2<0)dot2=-dot2;
  6260. len2=dot1*h2+dot2*d2;
  6261. overlapd=len-len1-len2;
  6262. if(overlapd>0)return;
  6263. }else{
  6264. rightd=false;
  6265. overlapd=0;
  6266. invalidd=true;
  6267. }
  6268. // try axis 14
  6269. len=aex*aex+aey*aey+aez*aez;
  6270. if(len>1e-5){
  6271. len=1/_Math.sqrt(len);
  6272. aex*=len;
  6273. aey*=len;
  6274. aez*=len;
  6275. len=aex*dx+aey*dy+aez*dz;
  6276. righte=len>0;
  6277. if(!righte)len=-len;
  6278. dot1=aex*a1x+aey*a1y+aez*a1z;
  6279. dot2=aex*a2x+aey*a2y+aez*a2z;
  6280. if(dot1<0)dot1=-dot1;
  6281. if(dot2<0)dot2=-dot2;
  6282. len1=dot1*w1+dot2*h1;
  6283. dot1=aex*a4x+aey*a4y+aez*a4z;
  6284. dot2=aex*a6x+aey*a6y+aez*a6z;
  6285. if(dot1<0)dot1=-dot1;
  6286. if(dot2<0)dot2=-dot2;
  6287. len2=dot1*w2+dot2*d2;
  6288. overlape=len-len1-len2;
  6289. if(overlape>0)return;
  6290. }else{
  6291. righte=false;
  6292. overlape=0;
  6293. invalide=true;
  6294. }
  6295. // try axis 15
  6296. len=afx*afx+afy*afy+afz*afz;
  6297. if(len>1e-5){
  6298. len=1/_Math.sqrt(len);
  6299. afx*=len;
  6300. afy*=len;
  6301. afz*=len;
  6302. len=afx*dx+afy*dy+afz*dz;
  6303. rightf=len>0;
  6304. if(!rightf)len=-len;
  6305. dot1=afx*a1x+afy*a1y+afz*a1z;
  6306. dot2=afx*a2x+afy*a2y+afz*a2z;
  6307. if(dot1<0)dot1=-dot1;
  6308. if(dot2<0)dot2=-dot2;
  6309. len1=dot1*w1+dot2*h1;
  6310. dot1=afx*a4x+afy*a4y+afz*a4z;
  6311. dot2=afx*a5x+afy*a5y+afz*a5z;
  6312. if(dot1<0)dot1=-dot1;
  6313. if(dot2<0)dot2=-dot2;
  6314. len2=dot1*w2+dot2*h2;
  6315. overlapf=len-len1-len2;
  6316. if(overlapf>0)return;
  6317. }else{
  6318. rightf=false;
  6319. overlapf=0;
  6320. invalidf=true;
  6321. }
  6322. // boxes are overlapping
  6323. var depth=overlap1;
  6324. var depth2=overlap1;
  6325. var minIndex=0;
  6326. var right=right1;
  6327. if(overlap2>depth2){
  6328. depth=overlap2;
  6329. depth2=overlap2;
  6330. minIndex=1;
  6331. right=right2;
  6332. }
  6333. if(overlap3>depth2){
  6334. depth=overlap3;
  6335. depth2=overlap3;
  6336. minIndex=2;
  6337. right=right3;
  6338. }
  6339. if(overlap4>depth2){
  6340. depth=overlap4;
  6341. depth2=overlap4;
  6342. minIndex=3;
  6343. right=right4;
  6344. }
  6345. if(overlap5>depth2){
  6346. depth=overlap5;
  6347. depth2=overlap5;
  6348. minIndex=4;
  6349. right=right5;
  6350. }
  6351. if(overlap6>depth2){
  6352. depth=overlap6;
  6353. depth2=overlap6;
  6354. minIndex=5;
  6355. right=right6;
  6356. }
  6357. if(overlap7-0.01>depth2&&!invalid7){
  6358. depth=overlap7;
  6359. depth2=overlap7-0.01;
  6360. minIndex=6;
  6361. right=right7;
  6362. }
  6363. if(overlap8-0.01>depth2&&!invalid8){
  6364. depth=overlap8;
  6365. depth2=overlap8-0.01;
  6366. minIndex=7;
  6367. right=right8;
  6368. }
  6369. if(overlap9-0.01>depth2&&!invalid9){
  6370. depth=overlap9;
  6371. depth2=overlap9-0.01;
  6372. minIndex=8;
  6373. right=right9;
  6374. }
  6375. if(overlapa-0.01>depth2&&!invalida){
  6376. depth=overlapa;
  6377. depth2=overlapa-0.01;
  6378. minIndex=9;
  6379. right=righta;
  6380. }
  6381. if(overlapb-0.01>depth2&&!invalidb){
  6382. depth=overlapb;
  6383. depth2=overlapb-0.01;
  6384. minIndex=10;
  6385. right=rightb;
  6386. }
  6387. if(overlapc-0.01>depth2&&!invalidc){
  6388. depth=overlapc;
  6389. depth2=overlapc-0.01;
  6390. minIndex=11;
  6391. right=rightc;
  6392. }
  6393. if(overlapd-0.01>depth2&&!invalidd){
  6394. depth=overlapd;
  6395. depth2=overlapd-0.01;
  6396. minIndex=12;
  6397. right=rightd;
  6398. }
  6399. if(overlape-0.01>depth2&&!invalide){
  6400. depth=overlape;
  6401. depth2=overlape-0.01;
  6402. minIndex=13;
  6403. right=righte;
  6404. }
  6405. if(overlapf-0.01>depth2&&!invalidf){
  6406. depth=overlapf;
  6407. minIndex=14;
  6408. right=rightf;
  6409. }
  6410. // normal
  6411. var nx=0;
  6412. var ny=0;
  6413. var nz=0;
  6414. // edge line or face side normal
  6415. var n1x=0;
  6416. var n1y=0;
  6417. var n1z=0;
  6418. var n2x=0;
  6419. var n2y=0;
  6420. var n2z=0;
  6421. // center of current face
  6422. var cx=0;
  6423. var cy=0;
  6424. var cz=0;
  6425. // face side
  6426. var s1x=0;
  6427. var s1y=0;
  6428. var s1z=0;
  6429. var s2x=0;
  6430. var s2y=0;
  6431. var s2z=0;
  6432. // swap b1 b2
  6433. var swap=false;
  6434. //_______________________________________
  6435. if(minIndex==0){// b1.x * b2
  6436. if(right){
  6437. cx=p1x+d1x; cy=p1y+d1y; cz=p1z+d1z;
  6438. nx=a1x; ny=a1y; nz=a1z;
  6439. }else{
  6440. cx=p1x-d1x; cy=p1y-d1y; cz=p1z-d1z;
  6441. nx=-a1x; ny=-a1y; nz=-a1z;
  6442. }
  6443. s1x=d2x; s1y=d2y; s1z=d2z;
  6444. n1x=-a2x; n1y=-a2y; n1z=-a2z;
  6445. s2x=d3x; s2y=d3y; s2z=d3z;
  6446. n2x=-a3x; n2y=-a3y; n2z=-a3z;
  6447. }
  6448. else if(minIndex==1){// b1.y * b2
  6449. if(right){
  6450. cx=p1x+d2x; cy=p1y+d2y; cz=p1z+d2z;
  6451. nx=a2x; ny=a2y; nz=a2z;
  6452. }else{
  6453. cx=p1x-d2x; cy=p1y-d2y; cz=p1z-d2z;
  6454. nx=-a2x; ny=-a2y; nz=-a2z;
  6455. }
  6456. s1x=d1x; s1y=d1y; s1z=d1z;
  6457. n1x=-a1x; n1y=-a1y; n1z=-a1z;
  6458. s2x=d3x; s2y=d3y; s2z=d3z;
  6459. n2x=-a3x; n2y=-a3y; n2z=-a3z;
  6460. }
  6461. else if(minIndex==2){// b1.z * b2
  6462. if(right){
  6463. cx=p1x+d3x; cy=p1y+d3y; cz=p1z+d3z;
  6464. nx=a3x; ny=a3y; nz=a3z;
  6465. }else{
  6466. cx=p1x-d3x; cy=p1y-d3y; cz=p1z-d3z;
  6467. nx=-a3x; ny=-a3y; nz=-a3z;
  6468. }
  6469. s1x=d1x; s1y=d1y; s1z=d1z;
  6470. n1x=-a1x; n1y=-a1y; n1z=-a1z;
  6471. s2x=d2x; s2y=d2y; s2z=d2z;
  6472. n2x=-a2x; n2y=-a2y; n2z=-a2z;
  6473. }
  6474. else if(minIndex==3){// b2.x * b1
  6475. swap=true;
  6476. if(!right){
  6477. cx=p2x+d4x; cy=p2y+d4y; cz=p2z+d4z;
  6478. nx=a4x; ny=a4y; nz=a4z;
  6479. }else{
  6480. cx=p2x-d4x; cy=p2y-d4y; cz=p2z-d4z;
  6481. nx=-a4x; ny=-a4y; nz=-a4z;
  6482. }
  6483. s1x=d5x; s1y=d5y; s1z=d5z;
  6484. n1x=-a5x; n1y=-a5y; n1z=-a5z;
  6485. s2x=d6x; s2y=d6y; s2z=d6z;
  6486. n2x=-a6x; n2y=-a6y; n2z=-a6z;
  6487. }
  6488. else if(minIndex==4){// b2.y * b1
  6489. swap=true;
  6490. if(!right){
  6491. cx=p2x+d5x; cy=p2y+d5y; cz=p2z+d5z;
  6492. nx=a5x; ny=a5y; nz=a5z;
  6493. }else{
  6494. cx=p2x-d5x; cy=p2y-d5y; cz=p2z-d5z;
  6495. nx=-a5x; ny=-a5y; nz=-a5z;
  6496. }
  6497. s1x=d4x; s1y=d4y; s1z=d4z;
  6498. n1x=-a4x; n1y=-a4y; n1z=-a4z;
  6499. s2x=d6x; s2y=d6y; s2z=d6z;
  6500. n2x=-a6x; n2y=-a6y; n2z=-a6z;
  6501. }
  6502. else if(minIndex==5){// b2.z * b1
  6503. swap=true;
  6504. if(!right){
  6505. cx=p2x+d6x; cy=p2y+d6y; cz=p2z+d6z;
  6506. nx=a6x; ny=a6y; nz=a6z;
  6507. }else{
  6508. cx=p2x-d6x; cy=p2y-d6y; cz=p2z-d6z;
  6509. nx=-a6x; ny=-a6y; nz=-a6z;
  6510. }
  6511. s1x=d4x; s1y=d4y; s1z=d4z;
  6512. n1x=-a4x; n1y=-a4y; n1z=-a4z;
  6513. s2x=d5x; s2y=d5y; s2z=d5z;
  6514. n2x=-a5x; n2y=-a5y; n2z=-a5z;
  6515. }
  6516. else if(minIndex==6){// b1.x * b2.x
  6517. nx=a7x; ny=a7y; nz=a7z;
  6518. n1x=a1x; n1y=a1y; n1z=a1z;
  6519. n2x=a4x; n2y=a4y; n2z=a4z;
  6520. }
  6521. else if(minIndex==7){// b1.x * b2.y
  6522. nx=a8x; ny=a8y; nz=a8z;
  6523. n1x=a1x; n1y=a1y; n1z=a1z;
  6524. n2x=a5x; n2y=a5y; n2z=a5z;
  6525. }
  6526. else if(minIndex==8){// b1.x * b2.z
  6527. nx=a9x; ny=a9y; nz=a9z;
  6528. n1x=a1x; n1y=a1y; n1z=a1z;
  6529. n2x=a6x; n2y=a6y; n2z=a6z;
  6530. }
  6531. else if(minIndex==9){// b1.y * b2.x
  6532. nx=aax; ny=aay; nz=aaz;
  6533. n1x=a2x; n1y=a2y; n1z=a2z;
  6534. n2x=a4x; n2y=a4y; n2z=a4z;
  6535. }
  6536. else if(minIndex==10){// b1.y * b2.y
  6537. nx=abx; ny=aby; nz=abz;
  6538. n1x=a2x; n1y=a2y; n1z=a2z;
  6539. n2x=a5x; n2y=a5y; n2z=a5z;
  6540. }
  6541. else if(minIndex==11){// b1.y * b2.z
  6542. nx=acx; ny=acy; nz=acz;
  6543. n1x=a2x; n1y=a2y; n1z=a2z;
  6544. n2x=a6x; n2y=a6y; n2z=a6z;
  6545. }
  6546. else if(minIndex==12){// b1.z * b2.x
  6547. nx=adx; ny=ady; nz=adz;
  6548. n1x=a3x; n1y=a3y; n1z=a3z;
  6549. n2x=a4x; n2y=a4y; n2z=a4z;
  6550. }
  6551. else if(minIndex==13){// b1.z * b2.y
  6552. nx=aex; ny=aey; nz=aez;
  6553. n1x=a3x; n1y=a3y; n1z=a3z;
  6554. n2x=a5x; n2y=a5y; n2z=a5z;
  6555. }
  6556. else if(minIndex==14){// b1.z * b2.z
  6557. nx=afx; ny=afy; nz=afz;
  6558. n1x=a3x; n1y=a3y; n1z=a3z;
  6559. n2x=a6x; n2y=a6y; n2z=a6z;
  6560. }
  6561. //__________________________________________
  6562. //var v;
  6563. if(minIndex>5){
  6564. if(!right){
  6565. nx=-nx; ny=-ny; nz=-nz;
  6566. }
  6567. var distance;
  6568. var maxDistance;
  6569. var vx;
  6570. var vy;
  6571. var vz;
  6572. var v1x;
  6573. var v1y;
  6574. var v1z;
  6575. var v2x;
  6576. var v2y;
  6577. var v2z;
  6578. //vertex1;
  6579. v1x=V1[0]; v1y=V1[1]; v1z=V1[2];
  6580. maxDistance=nx*v1x+ny*v1y+nz*v1z;
  6581. //vertex2;
  6582. vx=V1[3]; vy=V1[4]; vz=V1[5];
  6583. distance=nx*vx+ny*vy+nz*vz;
  6584. if(distance>maxDistance){
  6585. maxDistance=distance;
  6586. v1x=vx; v1y=vy; v1z=vz;
  6587. }
  6588. //vertex3;
  6589. vx=V1[6]; vy=V1[7]; vz=V1[8];
  6590. distance=nx*vx+ny*vy+nz*vz;
  6591. if(distance>maxDistance){
  6592. maxDistance=distance;
  6593. v1x=vx; v1y=vy; v1z=vz;
  6594. }
  6595. //vertex4;
  6596. vx=V1[9]; vy=V1[10]; vz=V1[11];
  6597. distance=nx*vx+ny*vy+nz*vz;
  6598. if(distance>maxDistance){
  6599. maxDistance=distance;
  6600. v1x=vx; v1y=vy; v1z=vz;
  6601. }
  6602. //vertex5;
  6603. vx=V1[12]; vy=V1[13]; vz=V1[14];
  6604. distance=nx*vx+ny*vy+nz*vz;
  6605. if(distance>maxDistance){
  6606. maxDistance=distance;
  6607. v1x=vx; v1y=vy; v1z=vz;
  6608. }
  6609. //vertex6;
  6610. vx=V1[15]; vy=V1[16]; vz=V1[17];
  6611. distance=nx*vx+ny*vy+nz*vz;
  6612. if(distance>maxDistance){
  6613. maxDistance=distance;
  6614. v1x=vx; v1y=vy; v1z=vz;
  6615. }
  6616. //vertex7;
  6617. vx=V1[18]; vy=V1[19]; vz=V1[20];
  6618. distance=nx*vx+ny*vy+nz*vz;
  6619. if(distance>maxDistance){
  6620. maxDistance=distance;
  6621. v1x=vx; v1y=vy; v1z=vz;
  6622. }
  6623. //vertex8;
  6624. vx=V1[21]; vy=V1[22]; vz=V1[23];
  6625. distance=nx*vx+ny*vy+nz*vz;
  6626. if(distance>maxDistance){
  6627. maxDistance=distance;
  6628. v1x=vx; v1y=vy; v1z=vz;
  6629. }
  6630. //vertex1;
  6631. v2x=V2[0]; v2y=V2[1]; v2z=V2[2];
  6632. maxDistance=nx*v2x+ny*v2y+nz*v2z;
  6633. //vertex2;
  6634. vx=V2[3]; vy=V2[4]; vz=V2[5];
  6635. distance=nx*vx+ny*vy+nz*vz;
  6636. if(distance<maxDistance){
  6637. maxDistance=distance;
  6638. v2x=vx; v2y=vy; v2z=vz;
  6639. }
  6640. //vertex3;
  6641. vx=V2[6]; vy=V2[7]; vz=V2[8];
  6642. distance=nx*vx+ny*vy+nz*vz;
  6643. if(distance<maxDistance){
  6644. maxDistance=distance;
  6645. v2x=vx; v2y=vy; v2z=vz;
  6646. }
  6647. //vertex4;
  6648. vx=V2[9]; vy=V2[10]; vz=V2[11];
  6649. distance=nx*vx+ny*vy+nz*vz;
  6650. if(distance<maxDistance){
  6651. maxDistance=distance;
  6652. v2x=vx; v2y=vy; v2z=vz;
  6653. }
  6654. //vertex5;
  6655. vx=V2[12]; vy=V2[13]; vz=V2[14];
  6656. distance=nx*vx+ny*vy+nz*vz;
  6657. if(distance<maxDistance){
  6658. maxDistance=distance;
  6659. v2x=vx; v2y=vy; v2z=vz;
  6660. }
  6661. //vertex6;
  6662. vx=V2[15]; vy=V2[16]; vz=V2[17];
  6663. distance=nx*vx+ny*vy+nz*vz;
  6664. if(distance<maxDistance){
  6665. maxDistance=distance;
  6666. v2x=vx; v2y=vy; v2z=vz;
  6667. }
  6668. //vertex7;
  6669. vx=V2[18]; vy=V2[19]; vz=V2[20];
  6670. distance=nx*vx+ny*vy+nz*vz;
  6671. if(distance<maxDistance){
  6672. maxDistance=distance;
  6673. v2x=vx; v2y=vy; v2z=vz;
  6674. }
  6675. //vertex8;
  6676. vx=V2[21]; vy=V2[22]; vz=V2[23];
  6677. distance=nx*vx+ny*vy+nz*vz;
  6678. if(distance<maxDistance){
  6679. maxDistance=distance;
  6680. v2x=vx; v2y=vy; v2z=vz;
  6681. }
  6682. vx=v2x-v1x; vy=v2y-v1y; vz=v2z-v1z;
  6683. dot1=n1x*n2x+n1y*n2y+n1z*n2z;
  6684. var t=(vx*(n1x-n2x*dot1)+vy*(n1y-n2y*dot1)+vz*(n1z-n2z*dot1))/(1-dot1*dot1);
  6685. manifold.addPoint(v1x+n1x*t+nx*depth*0.5,v1y+n1y*t+ny*depth*0.5,v1z+n1z*t+nz*depth*0.5,nx,ny,nz,depth,false);
  6686. return;
  6687. }
  6688. // now detect face-face collision...
  6689. // target quad
  6690. var q1x;
  6691. var q1y;
  6692. var q1z;
  6693. var q2x;
  6694. var q2y;
  6695. var q2z;
  6696. var q3x;
  6697. var q3y;
  6698. var q3z;
  6699. var q4x;
  6700. var q4y;
  6701. var q4z;
  6702. // search support face and vertex
  6703. var minDot=1;
  6704. var dot=0;
  6705. var minDotIndex=0;
  6706. if(swap){
  6707. dot=a1x*nx+a1y*ny+a1z*nz;
  6708. if(dot<minDot){
  6709. minDot=dot;
  6710. minDotIndex=0;
  6711. }
  6712. if(-dot<minDot){
  6713. minDot=-dot;
  6714. minDotIndex=1;
  6715. }
  6716. dot=a2x*nx+a2y*ny+a2z*nz;
  6717. if(dot<minDot){
  6718. minDot=dot;
  6719. minDotIndex=2;
  6720. }
  6721. if(-dot<minDot){
  6722. minDot=-dot;
  6723. minDotIndex=3;
  6724. }
  6725. dot=a3x*nx+a3y*ny+a3z*nz;
  6726. if(dot<minDot){
  6727. minDot=dot;
  6728. minDotIndex=4;
  6729. }
  6730. if(-dot<minDot){
  6731. minDot=-dot;
  6732. minDotIndex=5;
  6733. }
  6734. if(minDotIndex==0){// x+ face
  6735. q1x=V1[0]; q1y=V1[1]; q1z=V1[2];//vertex1
  6736. q2x=V1[6]; q2y=V1[7]; q2z=V1[8];//vertex3
  6737. q3x=V1[9]; q3y=V1[10]; q3z=V1[11];//vertex4
  6738. q4x=V1[3]; q4y=V1[4]; q4z=V1[5];//vertex2
  6739. }
  6740. else if(minDotIndex==1){// x- face
  6741. q1x=V1[15]; q1y=V1[16]; q1z=V1[17];//vertex6
  6742. q2x=V1[21]; q2y=V1[22]; q2z=V1[23];//vertex8
  6743. q3x=V1[18]; q3y=V1[19]; q3z=V1[20];//vertex7
  6744. q4x=V1[12]; q4y=V1[13]; q4z=V1[14];//vertex5
  6745. }
  6746. else if(minDotIndex==2){// y+ face
  6747. q1x=V1[12]; q1y=V1[13]; q1z=V1[14];//vertex5
  6748. q2x=V1[0]; q2y=V1[1]; q2z=V1[2];//vertex1
  6749. q3x=V1[3]; q3y=V1[4]; q3z=V1[5];//vertex2
  6750. q4x=V1[15]; q4y=V1[16]; q4z=V1[17];//vertex6
  6751. }
  6752. else if(minDotIndex==3){// y- face
  6753. q1x=V1[21]; q1y=V1[22]; q1z=V1[23];//vertex8
  6754. q2x=V1[9]; q2y=V1[10]; q2z=V1[11];//vertex4
  6755. q3x=V1[6]; q3y=V1[7]; q3z=V1[8];//vertex3
  6756. q4x=V1[18]; q4y=V1[19]; q4z=V1[20];//vertex7
  6757. }
  6758. else if(minDotIndex==4){// z+ face
  6759. q1x=V1[12]; q1y=V1[13]; q1z=V1[14];//vertex5
  6760. q2x=V1[18]; q2y=V1[19]; q2z=V1[20];//vertex7
  6761. q3x=V1[6]; q3y=V1[7]; q3z=V1[8];//vertex3
  6762. q4x=V1[0]; q4y=V1[1]; q4z=V1[2];//vertex1
  6763. }
  6764. else if(minDotIndex==5){// z- face
  6765. q1x=V1[3]; q1y=V1[4]; q1z=V1[5];//vertex2
  6766. //2x=V1[6]; q2y=V1[7]; q2z=V1[8];//vertex4 !!!
  6767. q2x=V2[9]; q2y=V2[10]; q2z=V2[11];//vertex4
  6768. q3x=V1[21]; q3y=V1[22]; q3z=V1[23];//vertex8
  6769. q4x=V1[15]; q4y=V1[16]; q4z=V1[17];//vertex6
  6770. }
  6771. }else{
  6772. dot=a4x*nx+a4y*ny+a4z*nz;
  6773. if(dot<minDot){
  6774. minDot=dot;
  6775. minDotIndex=0;
  6776. }
  6777. if(-dot<minDot){
  6778. minDot=-dot;
  6779. minDotIndex=1;
  6780. }
  6781. dot=a5x*nx+a5y*ny+a5z*nz;
  6782. if(dot<minDot){
  6783. minDot=dot;
  6784. minDotIndex=2;
  6785. }
  6786. if(-dot<minDot){
  6787. minDot=-dot;
  6788. minDotIndex=3;
  6789. }
  6790. dot=a6x*nx+a6y*ny+a6z*nz;
  6791. if(dot<minDot){
  6792. minDot=dot;
  6793. minDotIndex=4;
  6794. }
  6795. if(-dot<minDot){
  6796. minDot=-dot;
  6797. minDotIndex=5;
  6798. }
  6799. //______________________________________________________
  6800. if(minDotIndex==0){// x+ face
  6801. q1x=V2[0]; q1y=V2[1]; q1z=V2[2];//vertex1
  6802. q2x=V2[6]; q2y=V2[7]; q2z=V2[8];//vertex3
  6803. q3x=V2[9]; q3y=V2[10]; q3z=V2[11];//vertex4
  6804. q4x=V2[3]; q4y=V2[4]; q4z=V2[5];//vertex2
  6805. }
  6806. else if(minDotIndex==1){// x- face
  6807. q1x=V2[15]; q1y=V2[16]; q1z=V2[17];//vertex6
  6808. q2x=V2[21]; q2y=V2[22]; q2z=V2[23]; //vertex8
  6809. q3x=V2[18]; q3y=V2[19]; q3z=V2[20];//vertex7
  6810. q4x=V2[12]; q4y=V2[13]; q4z=V2[14];//vertex5
  6811. }
  6812. else if(minDotIndex==2){// y+ face
  6813. q1x=V2[12]; q1y=V2[13]; q1z=V2[14];//vertex5
  6814. q2x=V2[0]; q2y=V2[1]; q2z=V2[2];//vertex1
  6815. q3x=V2[3]; q3y=V2[4]; q3z=V2[5];//vertex2
  6816. q4x=V2[15]; q4y=V2[16]; q4z=V2[17];//vertex6
  6817. }
  6818. else if(minDotIndex==3){// y- face
  6819. q1x=V2[21]; q1y=V2[22]; q1z=V2[23];//vertex8
  6820. q2x=V2[9]; q2y=V2[10]; q2z=V2[11];//vertex4
  6821. q3x=V2[6]; q3y=V2[7]; q3z=V2[8];//vertex3
  6822. q4x=V2[18]; q4y=V2[19]; q4z=V2[20];//vertex7
  6823. }
  6824. else if(minDotIndex==4){// z+ face
  6825. q1x=V2[12]; q1y=V2[13]; q1z=V2[14];//vertex5
  6826. q2x=V2[18]; q2y=V2[19]; q2z=V2[20];//vertex7
  6827. q3x=V2[6]; q3y=V2[7]; q3z=V2[8];//vertex3
  6828. q4x=V2[0]; q4y=V2[1]; q4z=V2[2];//vertex1
  6829. }
  6830. else if(minDotIndex==5){// z- face
  6831. q1x=V2[3]; q1y=V2[4]; q1z=V2[5];//vertex2
  6832. q2x=V2[9]; q2y=V2[10]; q2z=V2[11];//vertex4
  6833. q3x=V2[21]; q3y=V2[22]; q3z=V2[23];//vertex8
  6834. q4x=V2[15]; q4y=V2[16]; q4z=V2[17];//vertex6
  6835. }
  6836. }
  6837. // clip vertices
  6838. var numClipVertices;
  6839. var numAddedClipVertices;
  6840. var index;
  6841. var x1;
  6842. var y1;
  6843. var z1;
  6844. var x2;
  6845. var y2;
  6846. var z2;
  6847. this.clipVertices1[0]=q1x;
  6848. this.clipVertices1[1]=q1y;
  6849. this.clipVertices1[2]=q1z;
  6850. this.clipVertices1[3]=q2x;
  6851. this.clipVertices1[4]=q2y;
  6852. this.clipVertices1[5]=q2z;
  6853. this.clipVertices1[6]=q3x;
  6854. this.clipVertices1[7]=q3y;
  6855. this.clipVertices1[8]=q3z;
  6856. this.clipVertices1[9]=q4x;
  6857. this.clipVertices1[10]=q4y;
  6858. this.clipVertices1[11]=q4z;
  6859. numAddedClipVertices=0;
  6860. x1=this.clipVertices1[9];
  6861. y1=this.clipVertices1[10];
  6862. z1=this.clipVertices1[11];
  6863. dot1=(x1-cx-s1x)*n1x+(y1-cy-s1y)*n1y+(z1-cz-s1z)*n1z;
  6864. //var i = 4;
  6865. //while(i--){
  6866. for(var i=0;i<4;i++){
  6867. index=i*3;
  6868. x2=this.clipVertices1[index];
  6869. y2=this.clipVertices1[index+1];
  6870. z2=this.clipVertices1[index+2];
  6871. dot2=(x2-cx-s1x)*n1x+(y2-cy-s1y)*n1y+(z2-cz-s1z)*n1z;
  6872. if(dot1>0){
  6873. if(dot2>0){
  6874. index=numAddedClipVertices*3;
  6875. numAddedClipVertices++;
  6876. this.clipVertices2[index]=x2;
  6877. this.clipVertices2[index+1]=y2;
  6878. this.clipVertices2[index+2]=z2;
  6879. }else{
  6880. index=numAddedClipVertices*3;
  6881. numAddedClipVertices++;
  6882. t=dot1/(dot1-dot2);
  6883. this.clipVertices2[index]=x1+(x2-x1)*t;
  6884. this.clipVertices2[index+1]=y1+(y2-y1)*t;
  6885. this.clipVertices2[index+2]=z1+(z2-z1)*t;
  6886. }
  6887. }else{
  6888. if(dot2>0){
  6889. index=numAddedClipVertices*3;
  6890. numAddedClipVertices++;
  6891. t=dot1/(dot1-dot2);
  6892. this.clipVertices2[index]=x1+(x2-x1)*t;
  6893. this.clipVertices2[index+1]=y1+(y2-y1)*t;
  6894. this.clipVertices2[index+2]=z1+(z2-z1)*t;
  6895. index=numAddedClipVertices*3;
  6896. numAddedClipVertices++;
  6897. this.clipVertices2[index]=x2;
  6898. this.clipVertices2[index+1]=y2;
  6899. this.clipVertices2[index+2]=z2;
  6900. }
  6901. }
  6902. x1=x2;
  6903. y1=y2;
  6904. z1=z2;
  6905. dot1=dot2;
  6906. }
  6907. numClipVertices=numAddedClipVertices;
  6908. if(numClipVertices==0)return;
  6909. numAddedClipVertices=0;
  6910. index=(numClipVertices-1)*3;
  6911. x1=this.clipVertices2[index];
  6912. y1=this.clipVertices2[index+1];
  6913. z1=this.clipVertices2[index+2];
  6914. dot1=(x1-cx-s2x)*n2x+(y1-cy-s2y)*n2y+(z1-cz-s2z)*n2z;
  6915. //i = numClipVertices;
  6916. //while(i--){
  6917. for(i=0;i<numClipVertices;i++){
  6918. index=i*3;
  6919. x2=this.clipVertices2[index];
  6920. y2=this.clipVertices2[index+1];
  6921. z2=this.clipVertices2[index+2];
  6922. dot2=(x2-cx-s2x)*n2x+(y2-cy-s2y)*n2y+(z2-cz-s2z)*n2z;
  6923. if(dot1>0){
  6924. if(dot2>0){
  6925. index=numAddedClipVertices*3;
  6926. numAddedClipVertices++;
  6927. this.clipVertices1[index]=x2;
  6928. this.clipVertices1[index+1]=y2;
  6929. this.clipVertices1[index+2]=z2;
  6930. }else{
  6931. index=numAddedClipVertices*3;
  6932. numAddedClipVertices++;
  6933. t=dot1/(dot1-dot2);
  6934. this.clipVertices1[index]=x1+(x2-x1)*t;
  6935. this.clipVertices1[index+1]=y1+(y2-y1)*t;
  6936. this.clipVertices1[index+2]=z1+(z2-z1)*t;
  6937. }
  6938. }else{
  6939. if(dot2>0){
  6940. index=numAddedClipVertices*3;
  6941. numAddedClipVertices++;
  6942. t=dot1/(dot1-dot2);
  6943. this.clipVertices1[index]=x1+(x2-x1)*t;
  6944. this.clipVertices1[index+1]=y1+(y2-y1)*t;
  6945. this.clipVertices1[index+2]=z1+(z2-z1)*t;
  6946. index=numAddedClipVertices*3;
  6947. numAddedClipVertices++;
  6948. this.clipVertices1[index]=x2;
  6949. this.clipVertices1[index+1]=y2;
  6950. this.clipVertices1[index+2]=z2;
  6951. }
  6952. }
  6953. x1=x2;
  6954. y1=y2;
  6955. z1=z2;
  6956. dot1=dot2;
  6957. }
  6958. numClipVertices=numAddedClipVertices;
  6959. if(numClipVertices==0)return;
  6960. numAddedClipVertices=0;
  6961. index=(numClipVertices-1)*3;
  6962. x1=this.clipVertices1[index];
  6963. y1=this.clipVertices1[index+1];
  6964. z1=this.clipVertices1[index+2];
  6965. dot1=(x1-cx+s1x)*-n1x+(y1-cy+s1y)*-n1y+(z1-cz+s1z)*-n1z;
  6966. //i = numClipVertices;
  6967. //while(i--){
  6968. for(i=0;i<numClipVertices;i++){
  6969. index=i*3;
  6970. x2=this.clipVertices1[index];
  6971. y2=this.clipVertices1[index+1];
  6972. z2=this.clipVertices1[index+2];
  6973. dot2=(x2-cx+s1x)*-n1x+(y2-cy+s1y)*-n1y+(z2-cz+s1z)*-n1z;
  6974. if(dot1>0){
  6975. if(dot2>0){
  6976. index=numAddedClipVertices*3;
  6977. numAddedClipVertices++;
  6978. this.clipVertices2[index]=x2;
  6979. this.clipVertices2[index+1]=y2;
  6980. this.clipVertices2[index+2]=z2;
  6981. }else{
  6982. index=numAddedClipVertices*3;
  6983. numAddedClipVertices++;
  6984. t=dot1/(dot1-dot2);
  6985. this.clipVertices2[index]=x1+(x2-x1)*t;
  6986. this.clipVertices2[index+1]=y1+(y2-y1)*t;
  6987. this.clipVertices2[index+2]=z1+(z2-z1)*t;
  6988. }
  6989. }else{
  6990. if(dot2>0){
  6991. index=numAddedClipVertices*3;
  6992. numAddedClipVertices++;
  6993. t=dot1/(dot1-dot2);
  6994. this.clipVertices2[index]=x1+(x2-x1)*t;
  6995. this.clipVertices2[index+1]=y1+(y2-y1)*t;
  6996. this.clipVertices2[index+2]=z1+(z2-z1)*t;
  6997. index=numAddedClipVertices*3;
  6998. numAddedClipVertices++;
  6999. this.clipVertices2[index]=x2;
  7000. this.clipVertices2[index+1]=y2;
  7001. this.clipVertices2[index+2]=z2;
  7002. }
  7003. }
  7004. x1=x2;
  7005. y1=y2;
  7006. z1=z2;
  7007. dot1=dot2;
  7008. }
  7009. numClipVertices=numAddedClipVertices;
  7010. if(numClipVertices==0)return;
  7011. numAddedClipVertices=0;
  7012. index=(numClipVertices-1)*3;
  7013. x1=this.clipVertices2[index];
  7014. y1=this.clipVertices2[index+1];
  7015. z1=this.clipVertices2[index+2];
  7016. dot1=(x1-cx+s2x)*-n2x+(y1-cy+s2y)*-n2y+(z1-cz+s2z)*-n2z;
  7017. //i = numClipVertices;
  7018. //while(i--){
  7019. for(i=0;i<numClipVertices;i++){
  7020. index=i*3;
  7021. x2=this.clipVertices2[index];
  7022. y2=this.clipVertices2[index+1];
  7023. z2=this.clipVertices2[index+2];
  7024. dot2=(x2-cx+s2x)*-n2x+(y2-cy+s2y)*-n2y+(z2-cz+s2z)*-n2z;
  7025. if(dot1>0){
  7026. if(dot2>0){
  7027. index=numAddedClipVertices*3;
  7028. numAddedClipVertices++;
  7029. this.clipVertices1[index]=x2;
  7030. this.clipVertices1[index+1]=y2;
  7031. this.clipVertices1[index+2]=z2;
  7032. }else{
  7033. index=numAddedClipVertices*3;
  7034. numAddedClipVertices++;
  7035. t=dot1/(dot1-dot2);
  7036. this.clipVertices1[index]=x1+(x2-x1)*t;
  7037. this.clipVertices1[index+1]=y1+(y2-y1)*t;
  7038. this.clipVertices1[index+2]=z1+(z2-z1)*t;
  7039. }
  7040. }else{
  7041. if(dot2>0){
  7042. index=numAddedClipVertices*3;
  7043. numAddedClipVertices++;
  7044. t=dot1/(dot1-dot2);
  7045. this.clipVertices1[index]=x1+(x2-x1)*t;
  7046. this.clipVertices1[index+1]=y1+(y2-y1)*t;
  7047. this.clipVertices1[index+2]=z1+(z2-z1)*t;
  7048. index=numAddedClipVertices*3;
  7049. numAddedClipVertices++;
  7050. this.clipVertices1[index]=x2;
  7051. this.clipVertices1[index+1]=y2;
  7052. this.clipVertices1[index+2]=z2;
  7053. }
  7054. }
  7055. x1=x2;
  7056. y1=y2;
  7057. z1=z2;
  7058. dot1=dot2;
  7059. }
  7060. numClipVertices=numAddedClipVertices;
  7061. if(swap){
  7062. var tb=b1;
  7063. b1=b2;
  7064. b2=tb;
  7065. }
  7066. if(numClipVertices==0)return;
  7067. var flipped=b1!=shape1;
  7068. if(numClipVertices>4){
  7069. x1=(q1x+q2x+q3x+q4x)*0.25;
  7070. y1=(q1y+q2y+q3y+q4y)*0.25;
  7071. z1=(q1z+q2z+q3z+q4z)*0.25;
  7072. n1x=q1x-x1;
  7073. n1y=q1y-y1;
  7074. n1z=q1z-z1;
  7075. n2x=q2x-x1;
  7076. n2y=q2y-y1;
  7077. n2z=q2z-z1;
  7078. var index1=0;
  7079. var index2=0;
  7080. var index3=0;
  7081. var index4=0;
  7082. var maxDot=-this.INF;
  7083. minDot=this.INF;
  7084. //i = numClipVertices;
  7085. //while(i--){
  7086. for(i=0;i<numClipVertices;i++){
  7087. this.used[i]=false;
  7088. index=i*3;
  7089. x1=this.clipVertices1[index];
  7090. y1=this.clipVertices1[index+1];
  7091. z1=this.clipVertices1[index+2];
  7092. dot=x1*n1x+y1*n1y+z1*n1z;
  7093. if(dot<minDot){
  7094. minDot=dot;
  7095. index1=i;
  7096. }
  7097. if(dot>maxDot){
  7098. maxDot=dot;
  7099. index3=i;
  7100. }
  7101. }
  7102. this.used[index1]=true;
  7103. this.used[index3]=true;
  7104. maxDot=-this.INF;
  7105. minDot=this.INF;
  7106. //i = numClipVertices;
  7107. //while(i--){
  7108. for(i=0;i<numClipVertices;i++){
  7109. if(this.used[i])continue;
  7110. index=i*3;
  7111. x1=this.clipVertices1[index];
  7112. y1=this.clipVertices1[index+1];
  7113. z1=this.clipVertices1[index+2];
  7114. dot=x1*n2x+y1*n2y+z1*n2z;
  7115. if(dot<minDot){
  7116. minDot=dot;
  7117. index2=i;
  7118. }
  7119. if(dot>maxDot){
  7120. maxDot=dot;
  7121. index4=i;
  7122. }
  7123. }
  7124. index=index1*3;
  7125. x1=this.clipVertices1[index];
  7126. y1=this.clipVertices1[index+1];
  7127. z1=this.clipVertices1[index+2];
  7128. dot=(x1-cx)*nx+(y1-cy)*ny+(z1-cz)*nz;
  7129. if(dot<0) manifold.addPoint(x1,y1,z1,nx,ny,nz,dot,flipped);
  7130. index=index2*3;
  7131. x1=this.clipVertices1[index];
  7132. y1=this.clipVertices1[index+1];
  7133. z1=this.clipVertices1[index+2];
  7134. dot=(x1-cx)*nx+(y1-cy)*ny+(z1-cz)*nz;
  7135. if(dot<0) manifold.addPoint(x1,y1,z1,nx,ny,nz,dot,flipped);
  7136. index=index3*3;
  7137. x1=this.clipVertices1[index];
  7138. y1=this.clipVertices1[index+1];
  7139. z1=this.clipVertices1[index+2];
  7140. dot=(x1-cx)*nx+(y1-cy)*ny+(z1-cz)*nz;
  7141. if(dot<0) manifold.addPoint(x1,y1,z1,nx,ny,nz,dot,flipped);
  7142. index=index4*3;
  7143. x1=this.clipVertices1[index];
  7144. y1=this.clipVertices1[index+1];
  7145. z1=this.clipVertices1[index+2];
  7146. dot=(x1-cx)*nx+(y1-cy)*ny+(z1-cz)*nz;
  7147. if(dot<0) manifold.addPoint(x1,y1,z1,nx,ny,nz,dot,flipped);
  7148. }else{
  7149. //i = numClipVertices;
  7150. //while(i--){
  7151. for(i=0;i<numClipVertices;i++){
  7152. index=i*3;
  7153. x1=this.clipVertices1[index];
  7154. y1=this.clipVertices1[index+1];
  7155. z1=this.clipVertices1[index+2];
  7156. dot=(x1-cx)*nx+(y1-cy)*ny+(z1-cz)*nz;
  7157. if(dot<0)manifold.addPoint(x1,y1,z1,nx,ny,nz,dot,flipped);
  7158. }
  7159. }
  7160. }
  7161. });
  7162. function BoxCylinderCollisionDetector (flip){
  7163. CollisionDetector.call( this );
  7164. this.flip = flip;
  7165. }
  7166. BoxCylinderCollisionDetector.prototype = Object.assign( Object.create( CollisionDetector.prototype ), {
  7167. constructor: BoxCylinderCollisionDetector,
  7168. getSep: function ( c1, c2, sep, pos, dep ) {
  7169. var t1x;
  7170. var t1y;
  7171. var t1z;
  7172. var t2x;
  7173. var t2y;
  7174. var t2z;
  7175. var sup=new Vec3();
  7176. var len;
  7177. var p1x;
  7178. var p1y;
  7179. var p1z;
  7180. var p2x;
  7181. var p2y;
  7182. var p2z;
  7183. var v01x=c1.position.x;
  7184. var v01y=c1.position.y;
  7185. var v01z=c1.position.z;
  7186. var v02x=c2.position.x;
  7187. var v02y=c2.position.y;
  7188. var v02z=c2.position.z;
  7189. var v0x=v02x-v01x;
  7190. var v0y=v02y-v01y;
  7191. var v0z=v02z-v01z;
  7192. if(v0x*v0x+v0y*v0y+v0z*v0z==0)v0y=0.001;
  7193. var nx=-v0x;
  7194. var ny=-v0y;
  7195. var nz=-v0z;
  7196. this.supportPointB(c1,-nx,-ny,-nz,sup);
  7197. var v11x=sup.x;
  7198. var v11y=sup.y;
  7199. var v11z=sup.z;
  7200. this.supportPointC(c2,nx,ny,nz,sup);
  7201. var v12x=sup.x;
  7202. var v12y=sup.y;
  7203. var v12z=sup.z;
  7204. var v1x=v12x-v11x;
  7205. var v1y=v12y-v11y;
  7206. var v1z=v12z-v11z;
  7207. if(v1x*nx+v1y*ny+v1z*nz<=0){
  7208. return false;
  7209. }
  7210. nx=v1y*v0z-v1z*v0y;
  7211. ny=v1z*v0x-v1x*v0z;
  7212. nz=v1x*v0y-v1y*v0x;
  7213. if(nx*nx+ny*ny+nz*nz==0){
  7214. sep.set( v1x-v0x, v1y-v0y, v1z-v0z ).normalize();
  7215. pos.set( (v11x+v12x)*0.5, (v11y+v12y)*0.5, (v11z+v12z)*0.5 );
  7216. return true;
  7217. }
  7218. this.supportPointB(c1,-nx,-ny,-nz,sup);
  7219. var v21x=sup.x;
  7220. var v21y=sup.y;
  7221. var v21z=sup.z;
  7222. this.supportPointC(c2,nx,ny,nz,sup);
  7223. var v22x=sup.x;
  7224. var v22y=sup.y;
  7225. var v22z=sup.z;
  7226. var v2x=v22x-v21x;
  7227. var v2y=v22y-v21y;
  7228. var v2z=v22z-v21z;
  7229. if(v2x*nx+v2y*ny+v2z*nz<=0){
  7230. return false;
  7231. }
  7232. t1x=v1x-v0x;
  7233. t1y=v1y-v0y;
  7234. t1z=v1z-v0z;
  7235. t2x=v2x-v0x;
  7236. t2y=v2y-v0y;
  7237. t2z=v2z-v0z;
  7238. nx=t1y*t2z-t1z*t2y;
  7239. ny=t1z*t2x-t1x*t2z;
  7240. nz=t1x*t2y-t1y*t2x;
  7241. if(nx*v0x+ny*v0y+nz*v0z>0){
  7242. t1x=v1x;
  7243. t1y=v1y;
  7244. t1z=v1z;
  7245. v1x=v2x;
  7246. v1y=v2y;
  7247. v1z=v2z;
  7248. v2x=t1x;
  7249. v2y=t1y;
  7250. v2z=t1z;
  7251. t1x=v11x;
  7252. t1y=v11y;
  7253. t1z=v11z;
  7254. v11x=v21x;
  7255. v11y=v21y;
  7256. v11z=v21z;
  7257. v21x=t1x;
  7258. v21y=t1y;
  7259. v21z=t1z;
  7260. t1x=v12x;
  7261. t1y=v12y;
  7262. t1z=v12z;
  7263. v12x=v22x;
  7264. v12y=v22y;
  7265. v12z=v22z;
  7266. v22x=t1x;
  7267. v22y=t1y;
  7268. v22z=t1z;
  7269. nx=-nx;
  7270. ny=-ny;
  7271. nz=-nz;
  7272. }
  7273. var iterations=0;
  7274. while(true){
  7275. if(++iterations>100){
  7276. return false;
  7277. }
  7278. this.supportPointB(c1,-nx,-ny,-nz,sup);
  7279. var v31x=sup.x;
  7280. var v31y=sup.y;
  7281. var v31z=sup.z;
  7282. this.supportPointC(c2,nx,ny,nz,sup);
  7283. var v32x=sup.x;
  7284. var v32y=sup.y;
  7285. var v32z=sup.z;
  7286. var v3x=v32x-v31x;
  7287. var v3y=v32y-v31y;
  7288. var v3z=v32z-v31z;
  7289. if(v3x*nx+v3y*ny+v3z*nz<=0){
  7290. return false;
  7291. }
  7292. if((v1y*v3z-v1z*v3y)*v0x+(v1z*v3x-v1x*v3z)*v0y+(v1x*v3y-v1y*v3x)*v0z<0){
  7293. v2x=v3x;
  7294. v2y=v3y;
  7295. v2z=v3z;
  7296. v21x=v31x;
  7297. v21y=v31y;
  7298. v21z=v31z;
  7299. v22x=v32x;
  7300. v22y=v32y;
  7301. v22z=v32z;
  7302. t1x=v1x-v0x;
  7303. t1y=v1y-v0y;
  7304. t1z=v1z-v0z;
  7305. t2x=v3x-v0x;
  7306. t2y=v3y-v0y;
  7307. t2z=v3z-v0z;
  7308. nx=t1y*t2z-t1z*t2y;
  7309. ny=t1z*t2x-t1x*t2z;
  7310. nz=t1x*t2y-t1y*t2x;
  7311. continue;
  7312. }
  7313. if((v3y*v2z-v3z*v2y)*v0x+(v3z*v2x-v3x*v2z)*v0y+(v3x*v2y-v3y*v2x)*v0z<0){
  7314. v1x=v3x;
  7315. v1y=v3y;
  7316. v1z=v3z;
  7317. v11x=v31x;
  7318. v11y=v31y;
  7319. v11z=v31z;
  7320. v12x=v32x;
  7321. v12y=v32y;
  7322. v12z=v32z;
  7323. t1x=v3x-v0x;
  7324. t1y=v3y-v0y;
  7325. t1z=v3z-v0z;
  7326. t2x=v2x-v0x;
  7327. t2y=v2y-v0y;
  7328. t2z=v2z-v0z;
  7329. nx=t1y*t2z-t1z*t2y;
  7330. ny=t1z*t2x-t1x*t2z;
  7331. nz=t1x*t2y-t1y*t2x;
  7332. continue;
  7333. }
  7334. var hit=false;
  7335. while(true){
  7336. t1x=v2x-v1x;
  7337. t1y=v2y-v1y;
  7338. t1z=v2z-v1z;
  7339. t2x=v3x-v1x;
  7340. t2y=v3y-v1y;
  7341. t2z=v3z-v1z;
  7342. nx=t1y*t2z-t1z*t2y;
  7343. ny=t1z*t2x-t1x*t2z;
  7344. nz=t1x*t2y-t1y*t2x;
  7345. len=1/_Math.sqrt(nx*nx+ny*ny+nz*nz);
  7346. nx*=len;
  7347. ny*=len;
  7348. nz*=len;
  7349. if(nx*v1x+ny*v1y+nz*v1z>=0&&!hit){
  7350. var b0=(v1y*v2z-v1z*v2y)*v3x+(v1z*v2x-v1x*v2z)*v3y+(v1x*v2y-v1y*v2x)*v3z;
  7351. var b1=(v3y*v2z-v3z*v2y)*v0x+(v3z*v2x-v3x*v2z)*v0y+(v3x*v2y-v3y*v2x)*v0z;
  7352. var b2=(v0y*v1z-v0z*v1y)*v3x+(v0z*v1x-v0x*v1z)*v3y+(v0x*v1y-v0y*v1x)*v3z;
  7353. var b3=(v2y*v1z-v2z*v1y)*v0x+(v2z*v1x-v2x*v1z)*v0y+(v2x*v1y-v2y*v1x)*v0z;
  7354. var sum=b0+b1+b2+b3;
  7355. if(sum<=0){
  7356. b0=0;
  7357. b1=(v2y*v3z-v2z*v3y)*nx+(v2z*v3x-v2x*v3z)*ny+(v2x*v3y-v2y*v3x)*nz;
  7358. b2=(v3y*v2z-v3z*v2y)*nx+(v3z*v2x-v3x*v2z)*ny+(v3x*v2y-v3y*v2x)*nz;
  7359. b3=(v1y*v2z-v1z*v2y)*nx+(v1z*v2x-v1x*v2z)*ny+(v1x*v2y-v1y*v2x)*nz;
  7360. sum=b1+b2+b3;
  7361. }
  7362. var inv=1/sum;
  7363. p1x=(v01x*b0+v11x*b1+v21x*b2+v31x*b3)*inv;
  7364. p1y=(v01y*b0+v11y*b1+v21y*b2+v31y*b3)*inv;
  7365. p1z=(v01z*b0+v11z*b1+v21z*b2+v31z*b3)*inv;
  7366. p2x=(v02x*b0+v12x*b1+v22x*b2+v32x*b3)*inv;
  7367. p2y=(v02y*b0+v12y*b1+v22y*b2+v32y*b3)*inv;
  7368. p2z=(v02z*b0+v12z*b1+v22z*b2+v32z*b3)*inv;
  7369. hit=true;
  7370. }
  7371. this.supportPointB(c1,-nx,-ny,-nz,sup);
  7372. var v41x=sup.x;
  7373. var v41y=sup.y;
  7374. var v41z=sup.z;
  7375. this.supportPointC(c2,nx,ny,nz,sup);
  7376. var v42x=sup.x;
  7377. var v42y=sup.y;
  7378. var v42z=sup.z;
  7379. var v4x=v42x-v41x;
  7380. var v4y=v42y-v41y;
  7381. var v4z=v42z-v41z;
  7382. var separation=-(v4x*nx+v4y*ny+v4z*nz);
  7383. if((v4x-v3x)*nx+(v4y-v3y)*ny+(v4z-v3z)*nz<=0.01||separation>=0){
  7384. if(hit){
  7385. sep.set( -nx, -ny, -nz );
  7386. pos.set( (p1x+p2x)*0.5, (p1y+p2y)*0.5, (p1z+p2z)*0.5 );
  7387. dep.x=separation;
  7388. return true;
  7389. }
  7390. return false;
  7391. }
  7392. if(
  7393. (v4y*v1z-v4z*v1y)*v0x+
  7394. (v4z*v1x-v4x*v1z)*v0y+
  7395. (v4x*v1y-v4y*v1x)*v0z<0
  7396. ){
  7397. if(
  7398. (v4y*v2z-v4z*v2y)*v0x+
  7399. (v4z*v2x-v4x*v2z)*v0y+
  7400. (v4x*v2y-v4y*v2x)*v0z<0
  7401. ){
  7402. v1x=v4x;
  7403. v1y=v4y;
  7404. v1z=v4z;
  7405. v11x=v41x;
  7406. v11y=v41y;
  7407. v11z=v41z;
  7408. v12x=v42x;
  7409. v12y=v42y;
  7410. v12z=v42z;
  7411. }else{
  7412. v3x=v4x;
  7413. v3y=v4y;
  7414. v3z=v4z;
  7415. v31x=v41x;
  7416. v31y=v41y;
  7417. v31z=v41z;
  7418. v32x=v42x;
  7419. v32y=v42y;
  7420. v32z=v42z;
  7421. }
  7422. }else{
  7423. if(
  7424. (v4y*v3z-v4z*v3y)*v0x+
  7425. (v4z*v3x-v4x*v3z)*v0y+
  7426. (v4x*v3y-v4y*v3x)*v0z<0
  7427. ){
  7428. v2x=v4x;
  7429. v2y=v4y;
  7430. v2z=v4z;
  7431. v21x=v41x;
  7432. v21y=v41y;
  7433. v21z=v41z;
  7434. v22x=v42x;
  7435. v22y=v42y;
  7436. v22z=v42z;
  7437. }else{
  7438. v1x=v4x;
  7439. v1y=v4y;
  7440. v1z=v4z;
  7441. v11x=v41x;
  7442. v11y=v41y;
  7443. v11z=v41z;
  7444. v12x=v42x;
  7445. v12y=v42y;
  7446. v12z=v42z;
  7447. }
  7448. }
  7449. }
  7450. }
  7451. //return false;
  7452. },
  7453. supportPointB: function( c, dx, dy, dz, out ) {
  7454. var rot=c.rotation.elements;
  7455. var ldx=rot[0]*dx+rot[3]*dy+rot[6]*dz;
  7456. var ldy=rot[1]*dx+rot[4]*dy+rot[7]*dz;
  7457. var ldz=rot[2]*dx+rot[5]*dy+rot[8]*dz;
  7458. var w=c.halfWidth;
  7459. var h=c.halfHeight;
  7460. var d=c.halfDepth;
  7461. var ox;
  7462. var oy;
  7463. var oz;
  7464. if(ldx<0)ox=-w;
  7465. else ox=w;
  7466. if(ldy<0)oy=-h;
  7467. else oy=h;
  7468. if(ldz<0)oz=-d;
  7469. else oz=d;
  7470. ldx=rot[0]*ox+rot[1]*oy+rot[2]*oz+c.position.x;
  7471. ldy=rot[3]*ox+rot[4]*oy+rot[5]*oz+c.position.y;
  7472. ldz=rot[6]*ox+rot[7]*oy+rot[8]*oz+c.position.z;
  7473. out.set( ldx, ldy, ldz );
  7474. },
  7475. supportPointC: function ( c, dx, dy, dz, out ) {
  7476. var rot=c.rotation.elements;
  7477. var ldx=rot[0]*dx+rot[3]*dy+rot[6]*dz;
  7478. var ldy=rot[1]*dx+rot[4]*dy+rot[7]*dz;
  7479. var ldz=rot[2]*dx+rot[5]*dy+rot[8]*dz;
  7480. var radx=ldx;
  7481. var radz=ldz;
  7482. var len=radx*radx+radz*radz;
  7483. var rad=c.radius;
  7484. var hh=c.halfHeight;
  7485. var ox;
  7486. var oy;
  7487. var oz;
  7488. if(len==0){
  7489. if(ldy<0){
  7490. ox=rad;
  7491. oy=-hh;
  7492. oz=0;
  7493. }else{
  7494. ox=rad;
  7495. oy=hh;
  7496. oz=0;
  7497. }
  7498. }else{
  7499. len=c.radius/_Math.sqrt(len);
  7500. if(ldy<0){
  7501. ox=radx*len;
  7502. oy=-hh;
  7503. oz=radz*len;
  7504. }else{
  7505. ox=radx*len;
  7506. oy=hh;
  7507. oz=radz*len;
  7508. }
  7509. }
  7510. ldx=rot[0]*ox+rot[1]*oy+rot[2]*oz+c.position.x;
  7511. ldy=rot[3]*ox+rot[4]*oy+rot[5]*oz+c.position.y;
  7512. ldz=rot[6]*ox+rot[7]*oy+rot[8]*oz+c.position.z;
  7513. out.set( ldx, ldy, ldz );
  7514. },
  7515. detectCollision: function( shape1, shape2, manifold ) {
  7516. var b;
  7517. var c;
  7518. if(this.flip){
  7519. b=shape2;
  7520. c=shape1;
  7521. }else{
  7522. b=shape1;
  7523. c=shape2;
  7524. }
  7525. var sep=new Vec3();
  7526. var pos=new Vec3();
  7527. var dep=new Vec3();
  7528. if(!this.getSep(b,c,sep,pos,dep))return;
  7529. var pbx=b.position.x;
  7530. var pby=b.position.y;
  7531. var pbz=b.position.z;
  7532. var pcx=c.position.x;
  7533. var pcy=c.position.y;
  7534. var pcz=c.position.z;
  7535. var bw=b.halfWidth;
  7536. var bh=b.halfHeight;
  7537. var bd=b.halfDepth;
  7538. var ch=c.halfHeight;
  7539. var r=c.radius;
  7540. var D = b.dimentions;
  7541. var nwx=D[0];//b.normalDirectionWidth.x;
  7542. var nwy=D[1];//b.normalDirectionWidth.y;
  7543. var nwz=D[2];//b.normalDirectionWidth.z;
  7544. var nhx=D[3];//b.normalDirectionHeight.x;
  7545. var nhy=D[4];//b.normalDirectionHeight.y;
  7546. var nhz=D[5];//b.normalDirectionHeight.z;
  7547. var ndx=D[6];//b.normalDirectionDepth.x;
  7548. var ndy=D[7];//b.normalDirectionDepth.y;
  7549. var ndz=D[8];//b.normalDirectionDepth.z;
  7550. var dwx=D[9];//b.halfDirectionWidth.x;
  7551. var dwy=D[10];//b.halfDirectionWidth.y;
  7552. var dwz=D[11];//b.halfDirectionWidth.z;
  7553. var dhx=D[12];//b.halfDirectionHeight.x;
  7554. var dhy=D[13];//b.halfDirectionHeight.y;
  7555. var dhz=D[14];//b.halfDirectionHeight.z;
  7556. var ddx=D[15];//b.halfDirectionDepth.x;
  7557. var ddy=D[16];//b.halfDirectionDepth.y;
  7558. var ddz=D[17];//b.halfDirectionDepth.z;
  7559. var ncx=c.normalDirection.x;
  7560. var ncy=c.normalDirection.y;
  7561. var ncz=c.normalDirection.z;
  7562. var dcx=c.halfDirection.x;
  7563. var dcy=c.halfDirection.y;
  7564. var dcz=c.halfDirection.z;
  7565. var nx=sep.x;
  7566. var ny=sep.y;
  7567. var nz=sep.z;
  7568. var dotw=nx*nwx+ny*nwy+nz*nwz;
  7569. var doth=nx*nhx+ny*nhy+nz*nhz;
  7570. var dotd=nx*ndx+ny*ndy+nz*ndz;
  7571. var dotc=nx*ncx+ny*ncy+nz*ncz;
  7572. var right1=dotw>0;
  7573. var right2=doth>0;
  7574. var right3=dotd>0;
  7575. var right4=dotc>0;
  7576. if(!right1)dotw=-dotw;
  7577. if(!right2)doth=-doth;
  7578. if(!right3)dotd=-dotd;
  7579. if(!right4)dotc=-dotc;
  7580. var state=0;
  7581. if(dotc>0.999){
  7582. if(dotw>0.999){
  7583. if(dotw>dotc)state=1;
  7584. else state=4;
  7585. }else if(doth>0.999){
  7586. if(doth>dotc)state=2;
  7587. else state=4;
  7588. }else if(dotd>0.999){
  7589. if(dotd>dotc)state=3;
  7590. else state=4;
  7591. }else state=4;
  7592. }else{
  7593. if(dotw>0.999)state=1;
  7594. else if(doth>0.999)state=2;
  7595. else if(dotd>0.999)state=3;
  7596. }
  7597. var cbx;
  7598. var cby;
  7599. var cbz;
  7600. var ccx;
  7601. var ccy;
  7602. var ccz;
  7603. var r00;
  7604. var r01;
  7605. var r02;
  7606. var r10;
  7607. var r11;
  7608. var r12;
  7609. var r20;
  7610. var r21;
  7611. var r22;
  7612. var px;
  7613. var py;
  7614. var pz;
  7615. var pd;
  7616. var dot;
  7617. var len;
  7618. var tx;
  7619. var ty;
  7620. var tz;
  7621. var td;
  7622. var dx;
  7623. var dy;
  7624. var dz;
  7625. var d1x;
  7626. var d1y;
  7627. var d1z;
  7628. var d2x;
  7629. var d2y;
  7630. var d2z;
  7631. var sx;
  7632. var sy;
  7633. var sz;
  7634. var sd;
  7635. var ex;
  7636. var ey;
  7637. var ez;
  7638. var ed;
  7639. var dot1;
  7640. var dot2;
  7641. var t1;
  7642. var dir1x;
  7643. var dir1y;
  7644. var dir1z;
  7645. var dir2x;
  7646. var dir2y;
  7647. var dir2z;
  7648. var dir1l;
  7649. var dir2l;
  7650. if(state==0){
  7651. //manifold.addPoint(pos.x,pos.y,pos.z,nx,ny,nz,dep.x,b,c,0,0,false);
  7652. manifold.addPoint(pos.x,pos.y,pos.z,nx,ny,nz,dep.x,this.flip);
  7653. }else if(state==4){
  7654. if(right4){
  7655. ccx=pcx-dcx;
  7656. ccy=pcy-dcy;
  7657. ccz=pcz-dcz;
  7658. nx=-ncx;
  7659. ny=-ncy;
  7660. nz=-ncz;
  7661. }else{
  7662. ccx=pcx+dcx;
  7663. ccy=pcy+dcy;
  7664. ccz=pcz+dcz;
  7665. nx=ncx;
  7666. ny=ncy;
  7667. nz=ncz;
  7668. }
  7669. var v1x;
  7670. var v1y;
  7671. var v1z;
  7672. var v2x;
  7673. var v2y;
  7674. var v2z;
  7675. var v3x;
  7676. var v3y;
  7677. var v3z;
  7678. var v4x;
  7679. var v4y;
  7680. var v4z;
  7681. dot=1;
  7682. state=0;
  7683. dot1=nwx*nx+nwy*ny+nwz*nz;
  7684. if(dot1<dot){
  7685. dot=dot1;
  7686. state=0;
  7687. }
  7688. if(-dot1<dot){
  7689. dot=-dot1;
  7690. state=1;
  7691. }
  7692. dot1=nhx*nx+nhy*ny+nhz*nz;
  7693. if(dot1<dot){
  7694. dot=dot1;
  7695. state=2;
  7696. }
  7697. if(-dot1<dot){
  7698. dot=-dot1;
  7699. state=3;
  7700. }
  7701. dot1=ndx*nx+ndy*ny+ndz*nz;
  7702. if(dot1<dot){
  7703. dot=dot1;
  7704. state=4;
  7705. }
  7706. if(-dot1<dot){
  7707. dot=-dot1;
  7708. state=5;
  7709. }
  7710. var v = b.elements;
  7711. switch(state){
  7712. case 0:
  7713. //v=b.vertex1;
  7714. v1x=v[0];//v.x;
  7715. v1y=v[1];//v.y;
  7716. v1z=v[2];//v.z;
  7717. //v=b.vertex3;
  7718. v2x=v[6];//v.x;
  7719. v2y=v[7];//v.y;
  7720. v2z=v[8];//v.z;
  7721. //v=b.vertex4;
  7722. v3x=v[9];//v.x;
  7723. v3y=v[10];//v.y;
  7724. v3z=v[11];//v.z;
  7725. //v=b.vertex2;
  7726. v4x=v[3];//v.x;
  7727. v4y=v[4];//v.y;
  7728. v4z=v[5];//v.z;
  7729. break;
  7730. case 1:
  7731. //v=b.vertex6;
  7732. v1x=v[15];//v.x;
  7733. v1y=v[16];//v.y;
  7734. v1z=v[17];//v.z;
  7735. //v=b.vertex8;
  7736. v2x=v[21];//v.x;
  7737. v2y=v[22];//v.y;
  7738. v2z=v[23];//v.z;
  7739. //v=b.vertex7;
  7740. v3x=v[18];//v.x;
  7741. v3y=v[19];//v.y;
  7742. v3z=v[20];//v.z;
  7743. //v=b.vertex5;
  7744. v4x=v[12];//v.x;
  7745. v4y=v[13];//v.y;
  7746. v4z=v[14];//v.z;
  7747. break;
  7748. case 2:
  7749. //v=b.vertex5;
  7750. v1x=v[12];//v.x;
  7751. v1y=v[13];//v.y;
  7752. v1z=v[14];//v.z;
  7753. //v=b.vertex1;
  7754. v2x=v[0];//v.x;
  7755. v2y=v[1];//v.y;
  7756. v2z=v[2];//v.z;
  7757. //v=b.vertex2;
  7758. v3x=v[3];//v.x;
  7759. v3y=v[4];//v.y;
  7760. v3z=v[5];//v.z;
  7761. //v=b.vertex6;
  7762. v4x=v[15];//v.x;
  7763. v4y=v[16];//v.y;
  7764. v4z=v[17];//v.z;
  7765. break;
  7766. case 3:
  7767. //v=b.vertex8;
  7768. v1x=v[21];//v.x;
  7769. v1y=v[22];//v.y;
  7770. v1z=v[23];//v.z;
  7771. //v=b.vertex4;
  7772. v2x=v[9];//v.x;
  7773. v2y=v[10];//v.y;
  7774. v2z=v[11];//v.z;
  7775. //v=b.vertex3;
  7776. v3x=v[6];//v.x;
  7777. v3y=v[7];//v.y;
  7778. v3z=v[8];//v.z;
  7779. //v=b.vertex7;
  7780. v4x=v[18];//v.x;
  7781. v4y=v[19];//v.y;
  7782. v4z=v[20];//v.z;
  7783. break;
  7784. case 4:
  7785. //v=b.vertex5;
  7786. v1x=v[12];//v.x;
  7787. v1y=v[13];//v.y;
  7788. v1z=v[14];//v.z;
  7789. //v=b.vertex7;
  7790. v2x=v[18];//v.x;
  7791. v2y=v[19];//v.y;
  7792. v2z=v[20];//v.z;
  7793. //v=b.vertex3;
  7794. v3x=v[6];//v.x;
  7795. v3y=v[7];//v.y;
  7796. v3z=v[8];//v.z;
  7797. //v=b.vertex1;
  7798. v4x=v[0];//v.x;
  7799. v4y=v[1];//v.y;
  7800. v4z=v[2];//v.z;
  7801. break;
  7802. case 5:
  7803. //v=b.vertex2;
  7804. v1x=v[3];//v.x;
  7805. v1y=v[4];//v.y;
  7806. v1z=v[5];//v.z;
  7807. //v=b.vertex4;
  7808. v2x=v[9];//v.x;
  7809. v2y=v[10];//v.y;
  7810. v2z=v[11];//v.z;
  7811. //v=b.vertex8;
  7812. v3x=v[21];//v.x;
  7813. v3y=v[22];//v.y;
  7814. v3z=v[23];//v.z;
  7815. //v=b.vertex6;
  7816. v4x=v[15];//v.x;
  7817. v4y=v[16];//v.y;
  7818. v4z=v[17];//v.z;
  7819. break;
  7820. }
  7821. pd=nx*(v1x-ccx)+ny*(v1y-ccy)+nz*(v1z-ccz);
  7822. if(pd<=0)manifold.addPoint(v1x,v1y,v1z,-nx,-ny,-nz,pd,this.flip);
  7823. pd=nx*(v2x-ccx)+ny*(v2y-ccy)+nz*(v2z-ccz);
  7824. if(pd<=0)manifold.addPoint(v2x,v2y,v2z,-nx,-ny,-nz,pd,this.flip);
  7825. pd=nx*(v3x-ccx)+ny*(v3y-ccy)+nz*(v3z-ccz);
  7826. if(pd<=0)manifold.addPoint(v3x,v3y,v3z,-nx,-ny,-nz,pd,this.flip);
  7827. pd=nx*(v4x-ccx)+ny*(v4y-ccy)+nz*(v4z-ccz);
  7828. if(pd<=0)manifold.addPoint(v4x,v4y,v4z,-nx,-ny,-nz,pd,this.flip);
  7829. }else{
  7830. switch(state){
  7831. case 1:
  7832. if(right1){
  7833. cbx=pbx+dwx;
  7834. cby=pby+dwy;
  7835. cbz=pbz+dwz;
  7836. nx=nwx;
  7837. ny=nwy;
  7838. nz=nwz;
  7839. }else{
  7840. cbx=pbx-dwx;
  7841. cby=pby-dwy;
  7842. cbz=pbz-dwz;
  7843. nx=-nwx;
  7844. ny=-nwy;
  7845. nz=-nwz;
  7846. }
  7847. dir1x=nhx;
  7848. dir1y=nhy;
  7849. dir1z=nhz;
  7850. dir1l=bh;
  7851. dir2x=ndx;
  7852. dir2y=ndy;
  7853. dir2z=ndz;
  7854. dir2l=bd;
  7855. break;
  7856. case 2:
  7857. if(right2){
  7858. cbx=pbx+dhx;
  7859. cby=pby+dhy;
  7860. cbz=pbz+dhz;
  7861. nx=nhx;
  7862. ny=nhy;
  7863. nz=nhz;
  7864. }else{
  7865. cbx=pbx-dhx;
  7866. cby=pby-dhy;
  7867. cbz=pbz-dhz;
  7868. nx=-nhx;
  7869. ny=-nhy;
  7870. nz=-nhz;
  7871. }
  7872. dir1x=nwx;
  7873. dir1y=nwy;
  7874. dir1z=nwz;
  7875. dir1l=bw;
  7876. dir2x=ndx;
  7877. dir2y=ndy;
  7878. dir2z=ndz;
  7879. dir2l=bd;
  7880. break;
  7881. case 3:
  7882. if(right3){
  7883. cbx=pbx+ddx;
  7884. cby=pby+ddy;
  7885. cbz=pbz+ddz;
  7886. nx=ndx;
  7887. ny=ndy;
  7888. nz=ndz;
  7889. }else{
  7890. cbx=pbx-ddx;
  7891. cby=pby-ddy;
  7892. cbz=pbz-ddz;
  7893. nx=-ndx;
  7894. ny=-ndy;
  7895. nz=-ndz;
  7896. }
  7897. dir1x=nwx;
  7898. dir1y=nwy;
  7899. dir1z=nwz;
  7900. dir1l=bw;
  7901. dir2x=nhx;
  7902. dir2y=nhy;
  7903. dir2z=nhz;
  7904. dir2l=bh;
  7905. break;
  7906. }
  7907. dot=nx*ncx+ny*ncy+nz*ncz;
  7908. if(dot<0)len=ch;
  7909. else len=-ch;
  7910. ccx=pcx+len*ncx;
  7911. ccy=pcy+len*ncy;
  7912. ccz=pcz+len*ncz;
  7913. if(dotc>=0.999999){
  7914. tx=-ny;
  7915. ty=nz;
  7916. tz=nx;
  7917. }else{
  7918. tx=nx;
  7919. ty=ny;
  7920. tz=nz;
  7921. }
  7922. len=tx*ncx+ty*ncy+tz*ncz;
  7923. dx=len*ncx-tx;
  7924. dy=len*ncy-ty;
  7925. dz=len*ncz-tz;
  7926. len=_Math.sqrt(dx*dx+dy*dy+dz*dz);
  7927. if(len==0)return;
  7928. len=r/len;
  7929. dx*=len;
  7930. dy*=len;
  7931. dz*=len;
  7932. tx=ccx+dx;
  7933. ty=ccy+dy;
  7934. tz=ccz+dz;
  7935. if(dot<-0.96||dot>0.96){
  7936. r00=ncx*ncx*1.5-0.5;
  7937. r01=ncx*ncy*1.5-ncz*0.866025403;
  7938. r02=ncx*ncz*1.5+ncy*0.866025403;
  7939. r10=ncy*ncx*1.5+ncz*0.866025403;
  7940. r11=ncy*ncy*1.5-0.5;
  7941. r12=ncy*ncz*1.5-ncx*0.866025403;
  7942. r20=ncz*ncx*1.5-ncy*0.866025403;
  7943. r21=ncz*ncy*1.5+ncx*0.866025403;
  7944. r22=ncz*ncz*1.5-0.5;
  7945. px=tx;
  7946. py=ty;
  7947. pz=tz;
  7948. pd=nx*(px-cbx)+ny*(py-cby)+nz*(pz-cbz);
  7949. tx=px-pd*nx-cbx;
  7950. ty=py-pd*ny-cby;
  7951. tz=pz-pd*nz-cbz;
  7952. sd=dir1x*tx+dir1y*ty+dir1z*tz;
  7953. ed=dir2x*tx+dir2y*ty+dir2z*tz;
  7954. if(sd<-dir1l)sd=-dir1l;
  7955. else if(sd>dir1l)sd=dir1l;
  7956. if(ed<-dir2l)ed=-dir2l;
  7957. else if(ed>dir2l)ed=dir2l;
  7958. tx=sd*dir1x+ed*dir2x;
  7959. ty=sd*dir1y+ed*dir2y;
  7960. tz=sd*dir1z+ed*dir2z;
  7961. px=cbx+tx;
  7962. py=cby+ty;
  7963. pz=cbz+tz;
  7964. manifold.addPoint(px,py,pz,nx,ny,nz,pd,this.flip);
  7965. px=dx*r00+dy*r01+dz*r02;
  7966. py=dx*r10+dy*r11+dz*r12;
  7967. pz=dx*r20+dy*r21+dz*r22;
  7968. px=(dx=px)+ccx;
  7969. py=(dy=py)+ccy;
  7970. pz=(dz=pz)+ccz;
  7971. pd=nx*(px-cbx)+ny*(py-cby)+nz*(pz-cbz);
  7972. if(pd<=0){
  7973. tx=px-pd*nx-cbx;
  7974. ty=py-pd*ny-cby;
  7975. tz=pz-pd*nz-cbz;
  7976. sd=dir1x*tx+dir1y*ty+dir1z*tz;
  7977. ed=dir2x*tx+dir2y*ty+dir2z*tz;
  7978. if(sd<-dir1l)sd=-dir1l;
  7979. else if(sd>dir1l)sd=dir1l;
  7980. if(ed<-dir2l)ed=-dir2l;
  7981. else if(ed>dir2l)ed=dir2l;
  7982. tx=sd*dir1x+ed*dir2x;
  7983. ty=sd*dir1y+ed*dir2y;
  7984. tz=sd*dir1z+ed*dir2z;
  7985. px=cbx+tx;
  7986. py=cby+ty;
  7987. pz=cbz+tz;
  7988. //manifold.addPoint(px,py,pz,nx,ny,nz,pd,b,c,2,0,false);
  7989. manifold.addPoint(px,py,pz,nx,ny,nz,pd,this.flip);
  7990. }
  7991. px=dx*r00+dy*r01+dz*r02;
  7992. py=dx*r10+dy*r11+dz*r12;
  7993. pz=dx*r20+dy*r21+dz*r22;
  7994. px=(dx=px)+ccx;
  7995. py=(dy=py)+ccy;
  7996. pz=(dz=pz)+ccz;
  7997. pd=nx*(px-cbx)+ny*(py-cby)+nz*(pz-cbz);
  7998. if(pd<=0){
  7999. tx=px-pd*nx-cbx;
  8000. ty=py-pd*ny-cby;
  8001. tz=pz-pd*nz-cbz;
  8002. sd=dir1x*tx+dir1y*ty+dir1z*tz;
  8003. ed=dir2x*tx+dir2y*ty+dir2z*tz;
  8004. if(sd<-dir1l)sd=-dir1l;
  8005. else if(sd>dir1l)sd=dir1l;
  8006. if(ed<-dir2l)ed=-dir2l;
  8007. else if(ed>dir2l)ed=dir2l;
  8008. tx=sd*dir1x+ed*dir2x;
  8009. ty=sd*dir1y+ed*dir2y;
  8010. tz=sd*dir1z+ed*dir2z;
  8011. px=cbx+tx;
  8012. py=cby+ty;
  8013. pz=cbz+tz;
  8014. //manifold.addPoint(px,py,pz,nx,ny,nz,pd,b,c,3,0,false);
  8015. manifold.addPoint(px,py,pz,nx,ny,nz,pd,this.flip);
  8016. }
  8017. }else{
  8018. sx=tx;
  8019. sy=ty;
  8020. sz=tz;
  8021. sd=nx*(sx-cbx)+ny*(sy-cby)+nz*(sz-cbz);
  8022. sx-=sd*nx;
  8023. sy-=sd*ny;
  8024. sz-=sd*nz;
  8025. if(dot>0){
  8026. ex=tx+dcx*2;
  8027. ey=ty+dcy*2;
  8028. ez=tz+dcz*2;
  8029. }else{
  8030. ex=tx-dcx*2;
  8031. ey=ty-dcy*2;
  8032. ez=tz-dcz*2;
  8033. }
  8034. ed=nx*(ex-cbx)+ny*(ey-cby)+nz*(ez-cbz);
  8035. ex-=ed*nx;
  8036. ey-=ed*ny;
  8037. ez-=ed*nz;
  8038. d1x=sx-cbx;
  8039. d1y=sy-cby;
  8040. d1z=sz-cbz;
  8041. d2x=ex-cbx;
  8042. d2y=ey-cby;
  8043. d2z=ez-cbz;
  8044. tx=ex-sx;
  8045. ty=ey-sy;
  8046. tz=ez-sz;
  8047. td=ed-sd;
  8048. dotw=d1x*dir1x+d1y*dir1y+d1z*dir1z;
  8049. doth=d2x*dir1x+d2y*dir1y+d2z*dir1z;
  8050. dot1=dotw-dir1l;
  8051. dot2=doth-dir1l;
  8052. if(dot1>0){
  8053. if(dot2>0)return;
  8054. t1=dot1/(dot1-dot2);
  8055. sx=sx+tx*t1;
  8056. sy=sy+ty*t1;
  8057. sz=sz+tz*t1;
  8058. sd=sd+td*t1;
  8059. d1x=sx-cbx;
  8060. d1y=sy-cby;
  8061. d1z=sz-cbz;
  8062. dotw=d1x*dir1x+d1y*dir1y+d1z*dir1z;
  8063. tx=ex-sx;
  8064. ty=ey-sy;
  8065. tz=ez-sz;
  8066. td=ed-sd;
  8067. }else if(dot2>0){
  8068. t1=dot1/(dot1-dot2);
  8069. ex=sx+tx*t1;
  8070. ey=sy+ty*t1;
  8071. ez=sz+tz*t1;
  8072. ed=sd+td*t1;
  8073. d2x=ex-cbx;
  8074. d2y=ey-cby;
  8075. d2z=ez-cbz;
  8076. doth=d2x*dir1x+d2y*dir1y+d2z*dir1z;
  8077. tx=ex-sx;
  8078. ty=ey-sy;
  8079. tz=ez-sz;
  8080. td=ed-sd;
  8081. }
  8082. dot1=dotw+dir1l;
  8083. dot2=doth+dir1l;
  8084. if(dot1<0){
  8085. if(dot2<0)return;
  8086. t1=dot1/(dot1-dot2);
  8087. sx=sx+tx*t1;
  8088. sy=sy+ty*t1;
  8089. sz=sz+tz*t1;
  8090. sd=sd+td*t1;
  8091. d1x=sx-cbx;
  8092. d1y=sy-cby;
  8093. d1z=sz-cbz;
  8094. tx=ex-sx;
  8095. ty=ey-sy;
  8096. tz=ez-sz;
  8097. td=ed-sd;
  8098. }else if(dot2<0){
  8099. t1=dot1/(dot1-dot2);
  8100. ex=sx+tx*t1;
  8101. ey=sy+ty*t1;
  8102. ez=sz+tz*t1;
  8103. ed=sd+td*t1;
  8104. d2x=ex-cbx;
  8105. d2y=ey-cby;
  8106. d2z=ez-cbz;
  8107. tx=ex-sx;
  8108. ty=ey-sy;
  8109. tz=ez-sz;
  8110. td=ed-sd;
  8111. }
  8112. dotw=d1x*dir2x+d1y*dir2y+d1z*dir2z;
  8113. doth=d2x*dir2x+d2y*dir2y+d2z*dir2z;
  8114. dot1=dotw-dir2l;
  8115. dot2=doth-dir2l;
  8116. if(dot1>0){
  8117. if(dot2>0)return;
  8118. t1=dot1/(dot1-dot2);
  8119. sx=sx+tx*t1;
  8120. sy=sy+ty*t1;
  8121. sz=sz+tz*t1;
  8122. sd=sd+td*t1;
  8123. d1x=sx-cbx;
  8124. d1y=sy-cby;
  8125. d1z=sz-cbz;
  8126. dotw=d1x*dir2x+d1y*dir2y+d1z*dir2z;
  8127. tx=ex-sx;
  8128. ty=ey-sy;
  8129. tz=ez-sz;
  8130. td=ed-sd;
  8131. }else if(dot2>0){
  8132. t1=dot1/(dot1-dot2);
  8133. ex=sx+tx*t1;
  8134. ey=sy+ty*t1;
  8135. ez=sz+tz*t1;
  8136. ed=sd+td*t1;
  8137. d2x=ex-cbx;
  8138. d2y=ey-cby;
  8139. d2z=ez-cbz;
  8140. doth=d2x*dir2x+d2y*dir2y+d2z*dir2z;
  8141. tx=ex-sx;
  8142. ty=ey-sy;
  8143. tz=ez-sz;
  8144. td=ed-sd;
  8145. }
  8146. dot1=dotw+dir2l;
  8147. dot2=doth+dir2l;
  8148. if(dot1<0){
  8149. if(dot2<0)return;
  8150. t1=dot1/(dot1-dot2);
  8151. sx=sx+tx*t1;
  8152. sy=sy+ty*t1;
  8153. sz=sz+tz*t1;
  8154. sd=sd+td*t1;
  8155. }else if(dot2<0){
  8156. t1=dot1/(dot1-dot2);
  8157. ex=sx+tx*t1;
  8158. ey=sy+ty*t1;
  8159. ez=sz+tz*t1;
  8160. ed=sd+td*t1;
  8161. }
  8162. if(sd<0){
  8163. //manifold.addPoint(sx,sy,sz,nx,ny,nz,sd,b,c,1,0,false);
  8164. manifold.addPoint(sx,sy,sz,nx,ny,nz,sd,this.flip);
  8165. }
  8166. if(ed<0){
  8167. //manifold.addPoint(ex,ey,ez,nx,ny,nz,ed,b,c,4,0,false);
  8168. manifold.addPoint(ex,ey,ez,nx,ny,nz,ed,this.flip);
  8169. }
  8170. }
  8171. }
  8172. }
  8173. });
  8174. function CylinderCylinderCollisionDetector() {
  8175. CollisionDetector.call( this );
  8176. }
  8177. CylinderCylinderCollisionDetector.prototype = Object.assign( Object.create( CollisionDetector.prototype ), {
  8178. constructor: CylinderCylinderCollisionDetector,
  8179. getSep: function ( c1, c2, sep, pos, dep ) {
  8180. var t1x;
  8181. var t1y;
  8182. var t1z;
  8183. var t2x;
  8184. var t2y;
  8185. var t2z;
  8186. var sup=new Vec3();
  8187. var len;
  8188. var p1x;
  8189. var p1y;
  8190. var p1z;
  8191. var p2x;
  8192. var p2y;
  8193. var p2z;
  8194. var v01x=c1.position.x;
  8195. var v01y=c1.position.y;
  8196. var v01z=c1.position.z;
  8197. var v02x=c2.position.x;
  8198. var v02y=c2.position.y;
  8199. var v02z=c2.position.z;
  8200. var v0x=v02x-v01x;
  8201. var v0y=v02y-v01y;
  8202. var v0z=v02z-v01z;
  8203. if(v0x*v0x+v0y*v0y+v0z*v0z==0)v0y=0.001;
  8204. var nx=-v0x;
  8205. var ny=-v0y;
  8206. var nz=-v0z;
  8207. this.supportPoint(c1,-nx,-ny,-nz,sup);
  8208. var v11x=sup.x;
  8209. var v11y=sup.y;
  8210. var v11z=sup.z;
  8211. this.supportPoint(c2,nx,ny,nz,sup);
  8212. var v12x=sup.x;
  8213. var v12y=sup.y;
  8214. var v12z=sup.z;
  8215. var v1x=v12x-v11x;
  8216. var v1y=v12y-v11y;
  8217. var v1z=v12z-v11z;
  8218. if(v1x*nx+v1y*ny+v1z*nz<=0){
  8219. return false;
  8220. }
  8221. nx=v1y*v0z-v1z*v0y;
  8222. ny=v1z*v0x-v1x*v0z;
  8223. nz=v1x*v0y-v1y*v0x;
  8224. if(nx*nx+ny*ny+nz*nz==0){
  8225. sep.set( v1x-v0x, v1y-v0y, v1z-v0z ).normalize();
  8226. pos.set( (v11x+v12x)*0.5, (v11y+v12y)*0.5, (v11z+v12z)*0.5 );
  8227. return true;
  8228. }
  8229. this.supportPoint(c1,-nx,-ny,-nz,sup);
  8230. var v21x=sup.x;
  8231. var v21y=sup.y;
  8232. var v21z=sup.z;
  8233. this.supportPoint(c2,nx,ny,nz,sup);
  8234. var v22x=sup.x;
  8235. var v22y=sup.y;
  8236. var v22z=sup.z;
  8237. var v2x=v22x-v21x;
  8238. var v2y=v22y-v21y;
  8239. var v2z=v22z-v21z;
  8240. if(v2x*nx+v2y*ny+v2z*nz<=0){
  8241. return false;
  8242. }
  8243. t1x=v1x-v0x;
  8244. t1y=v1y-v0y;
  8245. t1z=v1z-v0z;
  8246. t2x=v2x-v0x;
  8247. t2y=v2y-v0y;
  8248. t2z=v2z-v0z;
  8249. nx=t1y*t2z-t1z*t2y;
  8250. ny=t1z*t2x-t1x*t2z;
  8251. nz=t1x*t2y-t1y*t2x;
  8252. if(nx*v0x+ny*v0y+nz*v0z>0){
  8253. t1x=v1x;
  8254. t1y=v1y;
  8255. t1z=v1z;
  8256. v1x=v2x;
  8257. v1y=v2y;
  8258. v1z=v2z;
  8259. v2x=t1x;
  8260. v2y=t1y;
  8261. v2z=t1z;
  8262. t1x=v11x;
  8263. t1y=v11y;
  8264. t1z=v11z;
  8265. v11x=v21x;
  8266. v11y=v21y;
  8267. v11z=v21z;
  8268. v21x=t1x;
  8269. v21y=t1y;
  8270. v21z=t1z;
  8271. t1x=v12x;
  8272. t1y=v12y;
  8273. t1z=v12z;
  8274. v12x=v22x;
  8275. v12y=v22y;
  8276. v12z=v22z;
  8277. v22x=t1x;
  8278. v22y=t1y;
  8279. v22z=t1z;
  8280. nx=-nx;
  8281. ny=-ny;
  8282. nz=-nz;
  8283. }
  8284. var iterations=0;
  8285. while(true){
  8286. if(++iterations>100){
  8287. return false;
  8288. }
  8289. this.supportPoint(c1,-nx,-ny,-nz,sup);
  8290. var v31x=sup.x;
  8291. var v31y=sup.y;
  8292. var v31z=sup.z;
  8293. this.supportPoint(c2,nx,ny,nz,sup);
  8294. var v32x=sup.x;
  8295. var v32y=sup.y;
  8296. var v32z=sup.z;
  8297. var v3x=v32x-v31x;
  8298. var v3y=v32y-v31y;
  8299. var v3z=v32z-v31z;
  8300. if(v3x*nx+v3y*ny+v3z*nz<=0){
  8301. return false;
  8302. }
  8303. if((v1y*v3z-v1z*v3y)*v0x+(v1z*v3x-v1x*v3z)*v0y+(v1x*v3y-v1y*v3x)*v0z<0){
  8304. v2x=v3x;
  8305. v2y=v3y;
  8306. v2z=v3z;
  8307. v21x=v31x;
  8308. v21y=v31y;
  8309. v21z=v31z;
  8310. v22x=v32x;
  8311. v22y=v32y;
  8312. v22z=v32z;
  8313. t1x=v1x-v0x;
  8314. t1y=v1y-v0y;
  8315. t1z=v1z-v0z;
  8316. t2x=v3x-v0x;
  8317. t2y=v3y-v0y;
  8318. t2z=v3z-v0z;
  8319. nx=t1y*t2z-t1z*t2y;
  8320. ny=t1z*t2x-t1x*t2z;
  8321. nz=t1x*t2y-t1y*t2x;
  8322. continue;
  8323. }
  8324. if((v3y*v2z-v3z*v2y)*v0x+(v3z*v2x-v3x*v2z)*v0y+(v3x*v2y-v3y*v2x)*v0z<0){
  8325. v1x=v3x;
  8326. v1y=v3y;
  8327. v1z=v3z;
  8328. v11x=v31x;
  8329. v11y=v31y;
  8330. v11z=v31z;
  8331. v12x=v32x;
  8332. v12y=v32y;
  8333. v12z=v32z;
  8334. t1x=v3x-v0x;
  8335. t1y=v3y-v0y;
  8336. t1z=v3z-v0z;
  8337. t2x=v2x-v0x;
  8338. t2y=v2y-v0y;
  8339. t2z=v2z-v0z;
  8340. nx=t1y*t2z-t1z*t2y;
  8341. ny=t1z*t2x-t1x*t2z;
  8342. nz=t1x*t2y-t1y*t2x;
  8343. continue;
  8344. }
  8345. var hit=false;
  8346. while(true){
  8347. t1x=v2x-v1x;
  8348. t1y=v2y-v1y;
  8349. t1z=v2z-v1z;
  8350. t2x=v3x-v1x;
  8351. t2y=v3y-v1y;
  8352. t2z=v3z-v1z;
  8353. nx=t1y*t2z-t1z*t2y;
  8354. ny=t1z*t2x-t1x*t2z;
  8355. nz=t1x*t2y-t1y*t2x;
  8356. len=1/_Math.sqrt(nx*nx+ny*ny+nz*nz);
  8357. nx*=len;
  8358. ny*=len;
  8359. nz*=len;
  8360. if(nx*v1x+ny*v1y+nz*v1z>=0&&!hit){
  8361. var b0=(v1y*v2z-v1z*v2y)*v3x+(v1z*v2x-v1x*v2z)*v3y+(v1x*v2y-v1y*v2x)*v3z;
  8362. var b1=(v3y*v2z-v3z*v2y)*v0x+(v3z*v2x-v3x*v2z)*v0y+(v3x*v2y-v3y*v2x)*v0z;
  8363. var b2=(v0y*v1z-v0z*v1y)*v3x+(v0z*v1x-v0x*v1z)*v3y+(v0x*v1y-v0y*v1x)*v3z;
  8364. var b3=(v2y*v1z-v2z*v1y)*v0x+(v2z*v1x-v2x*v1z)*v0y+(v2x*v1y-v2y*v1x)*v0z;
  8365. var sum=b0+b1+b2+b3;
  8366. if(sum<=0){
  8367. b0=0;
  8368. b1=(v2y*v3z-v2z*v3y)*nx+(v2z*v3x-v2x*v3z)*ny+(v2x*v3y-v2y*v3x)*nz;
  8369. b2=(v3y*v2z-v3z*v2y)*nx+(v3z*v2x-v3x*v2z)*ny+(v3x*v2y-v3y*v2x)*nz;
  8370. b3=(v1y*v2z-v1z*v2y)*nx+(v1z*v2x-v1x*v2z)*ny+(v1x*v2y-v1y*v2x)*nz;
  8371. sum=b1+b2+b3;
  8372. }
  8373. var inv=1/sum;
  8374. p1x=(v01x*b0+v11x*b1+v21x*b2+v31x*b3)*inv;
  8375. p1y=(v01y*b0+v11y*b1+v21y*b2+v31y*b3)*inv;
  8376. p1z=(v01z*b0+v11z*b1+v21z*b2+v31z*b3)*inv;
  8377. p2x=(v02x*b0+v12x*b1+v22x*b2+v32x*b3)*inv;
  8378. p2y=(v02y*b0+v12y*b1+v22y*b2+v32y*b3)*inv;
  8379. p2z=(v02z*b0+v12z*b1+v22z*b2+v32z*b3)*inv;
  8380. hit=true;
  8381. }
  8382. this.supportPoint(c1,-nx,-ny,-nz,sup);
  8383. var v41x=sup.x;
  8384. var v41y=sup.y;
  8385. var v41z=sup.z;
  8386. this.supportPoint(c2,nx,ny,nz,sup);
  8387. var v42x=sup.x;
  8388. var v42y=sup.y;
  8389. var v42z=sup.z;
  8390. var v4x=v42x-v41x;
  8391. var v4y=v42y-v41y;
  8392. var v4z=v42z-v41z;
  8393. var separation=-(v4x*nx+v4y*ny+v4z*nz);
  8394. if((v4x-v3x)*nx+(v4y-v3y)*ny+(v4z-v3z)*nz<=0.01||separation>=0){
  8395. if(hit){
  8396. sep.set( -nx, -ny, -nz );
  8397. pos.set( (p1x+p2x)*0.5, (p1y+p2y)*0.5, (p1z+p2z)*0.5 );
  8398. dep.x=separation;
  8399. return true;
  8400. }
  8401. return false;
  8402. }
  8403. if(
  8404. (v4y*v1z-v4z*v1y)*v0x+
  8405. (v4z*v1x-v4x*v1z)*v0y+
  8406. (v4x*v1y-v4y*v1x)*v0z<0
  8407. ){
  8408. if(
  8409. (v4y*v2z-v4z*v2y)*v0x+
  8410. (v4z*v2x-v4x*v2z)*v0y+
  8411. (v4x*v2y-v4y*v2x)*v0z<0
  8412. ){
  8413. v1x=v4x;
  8414. v1y=v4y;
  8415. v1z=v4z;
  8416. v11x=v41x;
  8417. v11y=v41y;
  8418. v11z=v41z;
  8419. v12x=v42x;
  8420. v12y=v42y;
  8421. v12z=v42z;
  8422. }else{
  8423. v3x=v4x;
  8424. v3y=v4y;
  8425. v3z=v4z;
  8426. v31x=v41x;
  8427. v31y=v41y;
  8428. v31z=v41z;
  8429. v32x=v42x;
  8430. v32y=v42y;
  8431. v32z=v42z;
  8432. }
  8433. }else{
  8434. if(
  8435. (v4y*v3z-v4z*v3y)*v0x+
  8436. (v4z*v3x-v4x*v3z)*v0y+
  8437. (v4x*v3y-v4y*v3x)*v0z<0
  8438. ){
  8439. v2x=v4x;
  8440. v2y=v4y;
  8441. v2z=v4z;
  8442. v21x=v41x;
  8443. v21y=v41y;
  8444. v21z=v41z;
  8445. v22x=v42x;
  8446. v22y=v42y;
  8447. v22z=v42z;
  8448. }else{
  8449. v1x=v4x;
  8450. v1y=v4y;
  8451. v1z=v4z;
  8452. v11x=v41x;
  8453. v11y=v41y;
  8454. v11z=v41z;
  8455. v12x=v42x;
  8456. v12y=v42y;
  8457. v12z=v42z;
  8458. }
  8459. }
  8460. }
  8461. }
  8462. //return false;
  8463. },
  8464. supportPoint: function ( c, dx, dy, dz, out ) {
  8465. var rot=c.rotation.elements;
  8466. var ldx=rot[0]*dx+rot[3]*dy+rot[6]*dz;
  8467. var ldy=rot[1]*dx+rot[4]*dy+rot[7]*dz;
  8468. var ldz=rot[2]*dx+rot[5]*dy+rot[8]*dz;
  8469. var radx=ldx;
  8470. var radz=ldz;
  8471. var len=radx*radx+radz*radz;
  8472. var rad=c.radius;
  8473. var hh=c.halfHeight;
  8474. var ox;
  8475. var oy;
  8476. var oz;
  8477. if(len==0){
  8478. if(ldy<0){
  8479. ox=rad;
  8480. oy=-hh;
  8481. oz=0;
  8482. }else{
  8483. ox=rad;
  8484. oy=hh;
  8485. oz=0;
  8486. }
  8487. }else{
  8488. len=c.radius/_Math.sqrt(len);
  8489. if(ldy<0){
  8490. ox=radx*len;
  8491. oy=-hh;
  8492. oz=radz*len;
  8493. }else{
  8494. ox=radx*len;
  8495. oy=hh;
  8496. oz=radz*len;
  8497. }
  8498. }
  8499. ldx=rot[0]*ox+rot[1]*oy+rot[2]*oz+c.position.x;
  8500. ldy=rot[3]*ox+rot[4]*oy+rot[5]*oz+c.position.y;
  8501. ldz=rot[6]*ox+rot[7]*oy+rot[8]*oz+c.position.z;
  8502. out.set( ldx, ldy, ldz );
  8503. },
  8504. detectCollision: function ( shape1, shape2, manifold ) {
  8505. var c1;
  8506. var c2;
  8507. if(shape1.id<shape2.id){
  8508. c1=shape1;
  8509. c2=shape2;
  8510. }else{
  8511. c1=shape2;
  8512. c2=shape1;
  8513. }
  8514. var p1=c1.position;
  8515. var p2=c2.position;
  8516. var p1x=p1.x;
  8517. var p1y=p1.y;
  8518. var p1z=p1.z;
  8519. var p2x=p2.x;
  8520. var p2y=p2.y;
  8521. var p2z=p2.z;
  8522. var h1=c1.halfHeight;
  8523. var h2=c2.halfHeight;
  8524. var n1=c1.normalDirection;
  8525. var n2=c2.normalDirection;
  8526. var d1=c1.halfDirection;
  8527. var d2=c2.halfDirection;
  8528. var r1=c1.radius;
  8529. var r2=c2.radius;
  8530. var n1x=n1.x;
  8531. var n1y=n1.y;
  8532. var n1z=n1.z;
  8533. var n2x=n2.x;
  8534. var n2y=n2.y;
  8535. var n2z=n2.z;
  8536. var d1x=d1.x;
  8537. var d1y=d1.y;
  8538. var d1z=d1.z;
  8539. var d2x=d2.x;
  8540. var d2y=d2.y;
  8541. var d2z=d2.z;
  8542. var dx=p1x-p2x;
  8543. var dy=p1y-p2y;
  8544. var dz=p1z-p2z;
  8545. var len;
  8546. var c1x;
  8547. var c1y;
  8548. var c1z;
  8549. var c2x;
  8550. var c2y;
  8551. var c2z;
  8552. var tx;
  8553. var ty;
  8554. var tz;
  8555. var sx;
  8556. var sy;
  8557. var sz;
  8558. var ex;
  8559. var ey;
  8560. var ez;
  8561. var depth1;
  8562. var depth2;
  8563. var dot;
  8564. var t1;
  8565. var t2;
  8566. var sep=new Vec3();
  8567. var pos=new Vec3();
  8568. var dep=new Vec3();
  8569. if(!this.getSep(c1,c2,sep,pos,dep))return;
  8570. var dot1=sep.x*n1x+sep.y*n1y+sep.z*n1z;
  8571. var dot2=sep.x*n2x+sep.y*n2y+sep.z*n2z;
  8572. var right1=dot1>0;
  8573. var right2=dot2>0;
  8574. if(!right1)dot1=-dot1;
  8575. if(!right2)dot2=-dot2;
  8576. var state=0;
  8577. if(dot1>0.999||dot2>0.999){
  8578. if(dot1>dot2)state=1;
  8579. else state=2;
  8580. }
  8581. var nx;
  8582. var ny;
  8583. var nz;
  8584. var depth=dep.x;
  8585. var r00;
  8586. var r01;
  8587. var r02;
  8588. var r10;
  8589. var r11;
  8590. var r12;
  8591. var r20;
  8592. var r21;
  8593. var r22;
  8594. var px;
  8595. var py;
  8596. var pz;
  8597. var pd;
  8598. var a;
  8599. var b;
  8600. var e;
  8601. var f;
  8602. nx=sep.x;
  8603. ny=sep.y;
  8604. nz=sep.z;
  8605. switch(state){
  8606. case 0:
  8607. manifold.addPoint(pos.x,pos.y,pos.z,nx,ny,nz,depth,false);
  8608. break;
  8609. case 1:
  8610. if(right1){
  8611. c1x=p1x+d1x;
  8612. c1y=p1y+d1y;
  8613. c1z=p1z+d1z;
  8614. nx=n1x;
  8615. ny=n1y;
  8616. nz=n1z;
  8617. }else{
  8618. c1x=p1x-d1x;
  8619. c1y=p1y-d1y;
  8620. c1z=p1z-d1z;
  8621. nx=-n1x;
  8622. ny=-n1y;
  8623. nz=-n1z;
  8624. }
  8625. dot=nx*n2x+ny*n2y+nz*n2z;
  8626. if(dot<0)len=h2;
  8627. else len=-h2;
  8628. c2x=p2x+len*n2x;
  8629. c2y=p2y+len*n2y;
  8630. c2z=p2z+len*n2z;
  8631. if(dot2>=0.999999){
  8632. tx=-ny;
  8633. ty=nz;
  8634. tz=nx;
  8635. }else{
  8636. tx=nx;
  8637. ty=ny;
  8638. tz=nz;
  8639. }
  8640. len=tx*n2x+ty*n2y+tz*n2z;
  8641. dx=len*n2x-tx;
  8642. dy=len*n2y-ty;
  8643. dz=len*n2z-tz;
  8644. len=_Math.sqrt(dx*dx+dy*dy+dz*dz);
  8645. if(len==0)break;
  8646. len=r2/len;
  8647. dx*=len;
  8648. dy*=len;
  8649. dz*=len;
  8650. tx=c2x+dx;
  8651. ty=c2y+dy;
  8652. tz=c2z+dz;
  8653. if(dot<-0.96||dot>0.96){
  8654. r00=n2x*n2x*1.5-0.5;
  8655. r01=n2x*n2y*1.5-n2z*0.866025403;
  8656. r02=n2x*n2z*1.5+n2y*0.866025403;
  8657. r10=n2y*n2x*1.5+n2z*0.866025403;
  8658. r11=n2y*n2y*1.5-0.5;
  8659. r12=n2y*n2z*1.5-n2x*0.866025403;
  8660. r20=n2z*n2x*1.5-n2y*0.866025403;
  8661. r21=n2z*n2y*1.5+n2x*0.866025403;
  8662. r22=n2z*n2z*1.5-0.5;
  8663. px=tx;
  8664. py=ty;
  8665. pz=tz;
  8666. pd=nx*(px-c1x)+ny*(py-c1y)+nz*(pz-c1z);
  8667. tx=px-pd*nx-c1x;
  8668. ty=py-pd*ny-c1y;
  8669. tz=pz-pd*nz-c1z;
  8670. len=tx*tx+ty*ty+tz*tz;
  8671. if(len>r1*r1){
  8672. len=r1/_Math.sqrt(len);
  8673. tx*=len;
  8674. ty*=len;
  8675. tz*=len;
  8676. }
  8677. px=c1x+tx;
  8678. py=c1y+ty;
  8679. pz=c1z+tz;
  8680. manifold.addPoint(px,py,pz,nx,ny,nz,pd,false);
  8681. px=dx*r00+dy*r01+dz*r02;
  8682. py=dx*r10+dy*r11+dz*r12;
  8683. pz=dx*r20+dy*r21+dz*r22;
  8684. px=(dx=px)+c2x;
  8685. py=(dy=py)+c2y;
  8686. pz=(dz=pz)+c2z;
  8687. pd=nx*(px-c1x)+ny*(py-c1y)+nz*(pz-c1z);
  8688. if(pd<=0){
  8689. tx=px-pd*nx-c1x;
  8690. ty=py-pd*ny-c1y;
  8691. tz=pz-pd*nz-c1z;
  8692. len=tx*tx+ty*ty+tz*tz;
  8693. if(len>r1*r1){
  8694. len=r1/_Math.sqrt(len);
  8695. tx*=len;
  8696. ty*=len;
  8697. tz*=len;
  8698. }
  8699. px=c1x+tx;
  8700. py=c1y+ty;
  8701. pz=c1z+tz;
  8702. manifold.addPoint(px,py,pz,nx,ny,nz,pd,false);
  8703. }
  8704. px=dx*r00+dy*r01+dz*r02;
  8705. py=dx*r10+dy*r11+dz*r12;
  8706. pz=dx*r20+dy*r21+dz*r22;
  8707. px=(dx=px)+c2x;
  8708. py=(dy=py)+c2y;
  8709. pz=(dz=pz)+c2z;
  8710. pd=nx*(px-c1x)+ny*(py-c1y)+nz*(pz-c1z);
  8711. if(pd<=0){
  8712. tx=px-pd*nx-c1x;
  8713. ty=py-pd*ny-c1y;
  8714. tz=pz-pd*nz-c1z;
  8715. len=tx*tx+ty*ty+tz*tz;
  8716. if(len>r1*r1){
  8717. len=r1/_Math.sqrt(len);
  8718. tx*=len;
  8719. ty*=len;
  8720. tz*=len;
  8721. }
  8722. px=c1x+tx;
  8723. py=c1y+ty;
  8724. pz=c1z+tz;
  8725. manifold.addPoint(px,py,pz,nx,ny,nz,pd,false);
  8726. }
  8727. }else{
  8728. sx=tx;
  8729. sy=ty;
  8730. sz=tz;
  8731. depth1=nx*(sx-c1x)+ny*(sy-c1y)+nz*(sz-c1z);
  8732. sx-=depth1*nx;
  8733. sy-=depth1*ny;
  8734. sz-=depth1*nz;
  8735. if(dot>0){
  8736. ex=tx+n2x*h2*2;
  8737. ey=ty+n2y*h2*2;
  8738. ez=tz+n2z*h2*2;
  8739. }else{
  8740. ex=tx-n2x*h2*2;
  8741. ey=ty-n2y*h2*2;
  8742. ez=tz-n2z*h2*2;
  8743. }
  8744. depth2=nx*(ex-c1x)+ny*(ey-c1y)+nz*(ez-c1z);
  8745. ex-=depth2*nx;
  8746. ey-=depth2*ny;
  8747. ez-=depth2*nz;
  8748. dx=c1x-sx;
  8749. dy=c1y-sy;
  8750. dz=c1z-sz;
  8751. tx=ex-sx;
  8752. ty=ey-sy;
  8753. tz=ez-sz;
  8754. a=dx*dx+dy*dy+dz*dz;
  8755. b=dx*tx+dy*ty+dz*tz;
  8756. e=tx*tx+ty*ty+tz*tz;
  8757. f=b*b-e*(a-r1*r1);
  8758. if(f<0)break;
  8759. f=_Math.sqrt(f);
  8760. t1=(b+f)/e;
  8761. t2=(b-f)/e;
  8762. if(t2<t1){
  8763. len=t1;
  8764. t1=t2;
  8765. t2=len;
  8766. }
  8767. if(t2>1)t2=1;
  8768. if(t1<0)t1=0;
  8769. tx=sx+(ex-sx)*t1;
  8770. ty=sy+(ey-sy)*t1;
  8771. tz=sz+(ez-sz)*t1;
  8772. ex=sx+(ex-sx)*t2;
  8773. ey=sy+(ey-sy)*t2;
  8774. ez=sz+(ez-sz)*t2;
  8775. sx=tx;
  8776. sy=ty;
  8777. sz=tz;
  8778. len=depth1+(depth2-depth1)*t1;
  8779. depth2=depth1+(depth2-depth1)*t2;
  8780. depth1=len;
  8781. if(depth1<0) manifold.addPoint(sx,sy,sz,nx,ny,nz,pd,false);
  8782. if(depth2<0) manifold.addPoint(ex,ey,ez,nx,ny,nz,pd,false);
  8783. }
  8784. break;
  8785. case 2:
  8786. if(right2){
  8787. c2x=p2x-d2x;
  8788. c2y=p2y-d2y;
  8789. c2z=p2z-d2z;
  8790. nx=-n2x;
  8791. ny=-n2y;
  8792. nz=-n2z;
  8793. }else{
  8794. c2x=p2x+d2x;
  8795. c2y=p2y+d2y;
  8796. c2z=p2z+d2z;
  8797. nx=n2x;
  8798. ny=n2y;
  8799. nz=n2z;
  8800. }
  8801. dot=nx*n1x+ny*n1y+nz*n1z;
  8802. if(dot<0)len=h1;
  8803. else len=-h1;
  8804. c1x=p1x+len*n1x;
  8805. c1y=p1y+len*n1y;
  8806. c1z=p1z+len*n1z;
  8807. if(dot1>=0.999999){
  8808. tx=-ny;
  8809. ty=nz;
  8810. tz=nx;
  8811. }else{
  8812. tx=nx;
  8813. ty=ny;
  8814. tz=nz;
  8815. }
  8816. len=tx*n1x+ty*n1y+tz*n1z;
  8817. dx=len*n1x-tx;
  8818. dy=len*n1y-ty;
  8819. dz=len*n1z-tz;
  8820. len=_Math.sqrt(dx*dx+dy*dy+dz*dz);
  8821. if(len==0)break;
  8822. len=r1/len;
  8823. dx*=len;
  8824. dy*=len;
  8825. dz*=len;
  8826. tx=c1x+dx;
  8827. ty=c1y+dy;
  8828. tz=c1z+dz;
  8829. if(dot<-0.96||dot>0.96){
  8830. r00=n1x*n1x*1.5-0.5;
  8831. r01=n1x*n1y*1.5-n1z*0.866025403;
  8832. r02=n1x*n1z*1.5+n1y*0.866025403;
  8833. r10=n1y*n1x*1.5+n1z*0.866025403;
  8834. r11=n1y*n1y*1.5-0.5;
  8835. r12=n1y*n1z*1.5-n1x*0.866025403;
  8836. r20=n1z*n1x*1.5-n1y*0.866025403;
  8837. r21=n1z*n1y*1.5+n1x*0.866025403;
  8838. r22=n1z*n1z*1.5-0.5;
  8839. px=tx;
  8840. py=ty;
  8841. pz=tz;
  8842. pd=nx*(px-c2x)+ny*(py-c2y)+nz*(pz-c2z);
  8843. tx=px-pd*nx-c2x;
  8844. ty=py-pd*ny-c2y;
  8845. tz=pz-pd*nz-c2z;
  8846. len=tx*tx+ty*ty+tz*tz;
  8847. if(len>r2*r2){
  8848. len=r2/_Math.sqrt(len);
  8849. tx*=len;
  8850. ty*=len;
  8851. tz*=len;
  8852. }
  8853. px=c2x+tx;
  8854. py=c2y+ty;
  8855. pz=c2z+tz;
  8856. manifold.addPoint(px,py,pz,-nx,-ny,-nz,pd,false);
  8857. px=dx*r00+dy*r01+dz*r02;
  8858. py=dx*r10+dy*r11+dz*r12;
  8859. pz=dx*r20+dy*r21+dz*r22;
  8860. px=(dx=px)+c1x;
  8861. py=(dy=py)+c1y;
  8862. pz=(dz=pz)+c1z;
  8863. pd=nx*(px-c2x)+ny*(py-c2y)+nz*(pz-c2z);
  8864. if(pd<=0){
  8865. tx=px-pd*nx-c2x;
  8866. ty=py-pd*ny-c2y;
  8867. tz=pz-pd*nz-c2z;
  8868. len=tx*tx+ty*ty+tz*tz;
  8869. if(len>r2*r2){
  8870. len=r2/_Math.sqrt(len);
  8871. tx*=len;
  8872. ty*=len;
  8873. tz*=len;
  8874. }
  8875. px=c2x+tx;
  8876. py=c2y+ty;
  8877. pz=c2z+tz;
  8878. manifold.addPoint(px,py,pz,-nx,-ny,-nz,pd,false);
  8879. }
  8880. px=dx*r00+dy*r01+dz*r02;
  8881. py=dx*r10+dy*r11+dz*r12;
  8882. pz=dx*r20+dy*r21+dz*r22;
  8883. px=(dx=px)+c1x;
  8884. py=(dy=py)+c1y;
  8885. pz=(dz=pz)+c1z;
  8886. pd=nx*(px-c2x)+ny*(py-c2y)+nz*(pz-c2z);
  8887. if(pd<=0){
  8888. tx=px-pd*nx-c2x;
  8889. ty=py-pd*ny-c2y;
  8890. tz=pz-pd*nz-c2z;
  8891. len=tx*tx+ty*ty+tz*tz;
  8892. if(len>r2*r2){
  8893. len=r2/_Math.sqrt(len);
  8894. tx*=len;
  8895. ty*=len;
  8896. tz*=len;
  8897. }
  8898. px=c2x+tx;
  8899. py=c2y+ty;
  8900. pz=c2z+tz;
  8901. manifold.addPoint(px,py,pz,-nx,-ny,-nz,pd,false);
  8902. }
  8903. }else{
  8904. sx=tx;
  8905. sy=ty;
  8906. sz=tz;
  8907. depth1=nx*(sx-c2x)+ny*(sy-c2y)+nz*(sz-c2z);
  8908. sx-=depth1*nx;
  8909. sy-=depth1*ny;
  8910. sz-=depth1*nz;
  8911. if(dot>0){
  8912. ex=tx+n1x*h1*2;
  8913. ey=ty+n1y*h1*2;
  8914. ez=tz+n1z*h1*2;
  8915. }else{
  8916. ex=tx-n1x*h1*2;
  8917. ey=ty-n1y*h1*2;
  8918. ez=tz-n1z*h1*2;
  8919. }
  8920. depth2=nx*(ex-c2x)+ny*(ey-c2y)+nz*(ez-c2z);
  8921. ex-=depth2*nx;
  8922. ey-=depth2*ny;
  8923. ez-=depth2*nz;
  8924. dx=c2x-sx;
  8925. dy=c2y-sy;
  8926. dz=c2z-sz;
  8927. tx=ex-sx;
  8928. ty=ey-sy;
  8929. tz=ez-sz;
  8930. a=dx*dx+dy*dy+dz*dz;
  8931. b=dx*tx+dy*ty+dz*tz;
  8932. e=tx*tx+ty*ty+tz*tz;
  8933. f=b*b-e*(a-r2*r2);
  8934. if(f<0)break;
  8935. f=_Math.sqrt(f);
  8936. t1=(b+f)/e;
  8937. t2=(b-f)/e;
  8938. if(t2<t1){
  8939. len=t1;
  8940. t1=t2;
  8941. t2=len;
  8942. }
  8943. if(t2>1)t2=1;
  8944. if(t1<0)t1=0;
  8945. tx=sx+(ex-sx)*t1;
  8946. ty=sy+(ey-sy)*t1;
  8947. tz=sz+(ez-sz)*t1;
  8948. ex=sx+(ex-sx)*t2;
  8949. ey=sy+(ey-sy)*t2;
  8950. ez=sz+(ez-sz)*t2;
  8951. sx=tx;
  8952. sy=ty;
  8953. sz=tz;
  8954. len=depth1+(depth2-depth1)*t1;
  8955. depth2=depth1+(depth2-depth1)*t2;
  8956. depth1=len;
  8957. if(depth1<0){
  8958. manifold.addPoint(sx,sy,sz,-nx,-ny,-nz,depth1,false);
  8959. }
  8960. if(depth2<0){
  8961. manifold.addPoint(ex,ey,ez,-nx,-ny,-nz,depth2,false);
  8962. }
  8963. }
  8964. break;
  8965. }
  8966. }
  8967. });
  8968. /**
  8969. * A collision detector which detects collisions between sphere and box.
  8970. * @author saharan
  8971. */
  8972. function SphereBoxCollisionDetector ( flip ) {
  8973. CollisionDetector.call( this );
  8974. this.flip = flip;
  8975. }
  8976. SphereBoxCollisionDetector.prototype = Object.assign( Object.create( CollisionDetector.prototype ), {
  8977. constructor: SphereBoxCollisionDetector,
  8978. detectCollision: function ( shape1, shape2, manifold ) {
  8979. var s;
  8980. var b;
  8981. if(this.flip){
  8982. s=(shape2);
  8983. b=(shape1);
  8984. }else{
  8985. s=(shape1);
  8986. b=(shape2);
  8987. }
  8988. var D = b.dimentions;
  8989. var ps=s.position;
  8990. var psx=ps.x;
  8991. var psy=ps.y;
  8992. var psz=ps.z;
  8993. var pb=b.position;
  8994. var pbx=pb.x;
  8995. var pby=pb.y;
  8996. var pbz=pb.z;
  8997. var rad=s.radius;
  8998. var hw=b.halfWidth;
  8999. var hh=b.halfHeight;
  9000. var hd=b.halfDepth;
  9001. var dx=psx-pbx;
  9002. var dy=psy-pby;
  9003. var dz=psz-pbz;
  9004. var sx=D[0]*dx+D[1]*dy+D[2]*dz;
  9005. var sy=D[3]*dx+D[4]*dy+D[5]*dz;
  9006. var sz=D[6]*dx+D[7]*dy+D[8]*dz;
  9007. var cx;
  9008. var cy;
  9009. var cz;
  9010. var len;
  9011. var invLen;
  9012. var overlap=0;
  9013. if(sx>hw){
  9014. sx=hw;
  9015. }else if(sx<-hw){
  9016. sx=-hw;
  9017. }else{
  9018. overlap=1;
  9019. }
  9020. if(sy>hh){
  9021. sy=hh;
  9022. }else if(sy<-hh){
  9023. sy=-hh;
  9024. }else{
  9025. overlap|=2;
  9026. }
  9027. if(sz>hd){
  9028. sz=hd;
  9029. }else if(sz<-hd){
  9030. sz=-hd;
  9031. }else{
  9032. overlap|=4;
  9033. }
  9034. if(overlap==7){
  9035. // center of sphere is in the box
  9036. if(sx<0){
  9037. dx=hw+sx;
  9038. }else{
  9039. dx=hw-sx;
  9040. }
  9041. if(sy<0){
  9042. dy=hh+sy;
  9043. }else{
  9044. dy=hh-sy;
  9045. }
  9046. if(sz<0){
  9047. dz=hd+sz;
  9048. }else{
  9049. dz=hd-sz;
  9050. }
  9051. if(dx<dy){
  9052. if(dx<dz){
  9053. len=dx-hw;
  9054. if(sx<0){
  9055. sx=-hw;
  9056. dx=D[0];
  9057. dy=D[1];
  9058. dz=D[2];
  9059. }else{
  9060. sx=hw;
  9061. dx=-D[0];
  9062. dy=-D[1];
  9063. dz=-D[2];
  9064. }
  9065. }else{
  9066. len=dz-hd;
  9067. if(sz<0){
  9068. sz=-hd;
  9069. dx=D[6];
  9070. dy=D[7];
  9071. dz=D[8];
  9072. }else{
  9073. sz=hd;
  9074. dx=-D[6];
  9075. dy=-D[7];
  9076. dz=-D[8];
  9077. }
  9078. }
  9079. }else{
  9080. if(dy<dz){
  9081. len=dy-hh;
  9082. if(sy<0){
  9083. sy=-hh;
  9084. dx=D[3];
  9085. dy=D[4];
  9086. dz=D[5];
  9087. }else{
  9088. sy=hh;
  9089. dx=-D[3];
  9090. dy=-D[4];
  9091. dz=-D[5];
  9092. }
  9093. }else{
  9094. len=dz-hd;
  9095. if(sz<0){
  9096. sz=-hd;
  9097. dx=D[6];
  9098. dy=D[7];
  9099. dz=D[8];
  9100. }else{
  9101. sz=hd;
  9102. dx=-D[6];
  9103. dy=-D[7];
  9104. dz=-D[8];
  9105. }
  9106. }
  9107. }
  9108. cx=pbx+sx*D[0]+sy*D[3]+sz*D[6];
  9109. cy=pby+sx*D[1]+sy*D[4]+sz*D[7];
  9110. cz=pbz+sx*D[2]+sy*D[5]+sz*D[8];
  9111. manifold.addPoint(psx+rad*dx,psy+rad*dy,psz+rad*dz,dx,dy,dz,len-rad,this.flip);
  9112. }else{
  9113. cx=pbx+sx*D[0]+sy*D[3]+sz*D[6];
  9114. cy=pby+sx*D[1]+sy*D[4]+sz*D[7];
  9115. cz=pbz+sx*D[2]+sy*D[5]+sz*D[8];
  9116. dx=cx-ps.x;
  9117. dy=cy-ps.y;
  9118. dz=cz-ps.z;
  9119. len=dx*dx+dy*dy+dz*dz;
  9120. if(len>0&&len<rad*rad){
  9121. len=_Math.sqrt(len);
  9122. invLen=1/len;
  9123. dx*=invLen;
  9124. dy*=invLen;
  9125. dz*=invLen;
  9126. manifold.addPoint(psx+rad*dx,psy+rad*dy,psz+rad*dz,dx,dy,dz,len-rad,this.flip);
  9127. }
  9128. }
  9129. }
  9130. });
  9131. function SphereCylinderCollisionDetector ( flip ){
  9132. CollisionDetector.call( this );
  9133. this.flip = flip;
  9134. }
  9135. SphereCylinderCollisionDetector.prototype = Object.assign( Object.create( CollisionDetector.prototype ), {
  9136. constructor: SphereCylinderCollisionDetector,
  9137. detectCollision: function ( shape1, shape2, manifold ) {
  9138. var s;
  9139. var c;
  9140. if( this.flip ){
  9141. s = shape2;
  9142. c = shape1;
  9143. }else{
  9144. s = shape1;
  9145. c = shape2;
  9146. }
  9147. var ps = s.position;
  9148. var psx = ps.x;
  9149. var psy = ps.y;
  9150. var psz = ps.z;
  9151. var pc = c.position;
  9152. var pcx = pc.x;
  9153. var pcy = pc.y;
  9154. var pcz = pc.z;
  9155. var dirx = c.normalDirection.x;
  9156. var diry = c.normalDirection.y;
  9157. var dirz = c.normalDirection.z;
  9158. var rads = s.radius;
  9159. var radc = c.radius;
  9160. var rad2 = rads + radc;
  9161. var halfh = c.halfHeight;
  9162. var dx = psx - pcx;
  9163. var dy = psy - pcy;
  9164. var dz = psz - pcz;
  9165. var dot = dx * dirx + dy * diry + dz * dirz;
  9166. if ( dot < -halfh - rads || dot > halfh + rads ) return;
  9167. var cx = pcx + dot * dirx;
  9168. var cy = pcy + dot * diry;
  9169. var cz = pcz + dot * dirz;
  9170. var d2x = psx - cx;
  9171. var d2y = psy - cy;
  9172. var d2z = psz - cz;
  9173. var len = d2x * d2x + d2y * d2y + d2z * d2z;
  9174. if ( len > rad2 * rad2 ) return;
  9175. if ( len > radc * radc ) {
  9176. len = radc / _Math.sqrt( len );
  9177. d2x *= len;
  9178. d2y *= len;
  9179. d2z *= len;
  9180. }
  9181. if( dot < -halfh ) dot = -halfh;
  9182. else if( dot > halfh ) dot = halfh;
  9183. cx = pcx + dot * dirx + d2x;
  9184. cy = pcy + dot * diry + d2y;
  9185. cz = pcz + dot * dirz + d2z;
  9186. dx = cx - psx;
  9187. dy = cy - psy;
  9188. dz = cz - psz;
  9189. len = dx * dx + dy * dy + dz * dz;
  9190. var invLen;
  9191. if ( len > 0 && len < rads * rads ) {
  9192. len = _Math.sqrt(len);
  9193. invLen = 1 / len;
  9194. dx *= invLen;
  9195. dy *= invLen;
  9196. dz *= invLen;
  9197. ///result.addContactInfo(psx+dx*rads,psy+dy*rads,psz+dz*rads,dx,dy,dz,len-rads,s,c,0,0,false);
  9198. manifold.addPoint( psx + dx * rads, psy + dy * rads, psz + dz * rads, dx, dy, dz, len - rads, this.flip );
  9199. }
  9200. }
  9201. });
  9202. /**
  9203. * A collision detector which detects collisions between two spheres.
  9204. * @author saharan
  9205. */
  9206. function SphereSphereCollisionDetector (){
  9207. CollisionDetector.call( this );
  9208. }
  9209. SphereSphereCollisionDetector.prototype = Object.assign( Object.create( CollisionDetector.prototype ), {
  9210. constructor: SphereSphereCollisionDetector,
  9211. detectCollision: function ( shape1, shape2, manifold ) {
  9212. var s1 = shape1;
  9213. var s2 = shape2;
  9214. var p1 = s1.position;
  9215. var p2 = s2.position;
  9216. var dx = p2.x - p1.x;
  9217. var dy = p2.y - p1.y;
  9218. var dz = p2.z - p1.z;
  9219. var len = dx * dx + dy * dy + dz * dz;
  9220. var r1 = s1.radius;
  9221. var r2 = s2.radius;
  9222. var rad = r1 + r2;
  9223. if ( len > 0 && len < rad * rad ){
  9224. len = _Math.sqrt( len );
  9225. var invLen = 1 / len;
  9226. dx *= invLen;
  9227. dy *= invLen;
  9228. dz *= invLen;
  9229. manifold.addPoint( p1.x + dx * r1, p1.y + dy * r1, p1.z + dz * r1, dx, dy, dz, len - rad, false );
  9230. }
  9231. }
  9232. });
  9233. /**
  9234. * A collision detector which detects collisions between two spheres.
  9235. * @author saharan
  9236. * @author lo-th
  9237. */
  9238. function SpherePlaneCollisionDetector ( flip ){
  9239. CollisionDetector.call( this );
  9240. this.flip = flip;
  9241. this.n = new Vec3();
  9242. this.p = new Vec3();
  9243. }
  9244. SpherePlaneCollisionDetector.prototype = Object.assign( Object.create( CollisionDetector.prototype ), {
  9245. constructor: SpherePlaneCollisionDetector,
  9246. detectCollision: function ( shape1, shape2, manifold ) {
  9247. var n = this.n;
  9248. var p = this.p;
  9249. var s = this.flip ? shape2 : shape1;
  9250. var pn = this.flip ? shape1 : shape2;
  9251. var rad = s.radius;
  9252. var len;
  9253. n.sub( s.position, pn.position );
  9254. //var h = _Math.dotVectors( pn.normal, n );
  9255. n.x *= pn.normal.x;//+ rad;
  9256. n.y *= pn.normal.y;
  9257. n.z *= pn.normal.z;//+ rad;
  9258. var len = n.lengthSq();
  9259. if( len > 0 && len < rad * rad){//&& h > rad*rad ){
  9260. len = _Math.sqrt( len );
  9261. //len = _Math.sqrt( h );
  9262. n.copy(pn.normal).negate();
  9263. //n.scaleEqual( 1/len );
  9264. //(0, -1, 0)
  9265. //n.normalize();
  9266. p.copy( s.position ).addScaledVector( n, rad );
  9267. manifold.addPointVec( p, n, len - rad, this.flip );
  9268. }
  9269. }
  9270. });
  9271. /**
  9272. * A collision detector which detects collisions between two spheres.
  9273. * @author saharan
  9274. * @author lo-th
  9275. */
  9276. function BoxPlaneCollisionDetector ( flip ){
  9277. CollisionDetector.call( this );
  9278. this.flip = flip;
  9279. this.n = new Vec3();
  9280. this.p = new Vec3();
  9281. this.dix = new Vec3();
  9282. this.diy = new Vec3();
  9283. this.diz = new Vec3();
  9284. this.cc = new Vec3();
  9285. this.cc2 = new Vec3();
  9286. }
  9287. BoxPlaneCollisionDetector.prototype = Object.assign( Object.create( CollisionDetector.prototype ), {
  9288. constructor: BoxPlaneCollisionDetector,
  9289. detectCollision: function ( shape1, shape2, manifold ) {
  9290. var n = this.n;
  9291. var p = this.p;
  9292. var cc = this.cc;
  9293. var b = this.flip ? shape2 : shape1;
  9294. var pn = this.flip ? shape1 : shape2;
  9295. var D = b.dimentions;
  9296. var hw = b.halfWidth;
  9297. var hh = b.halfHeight;
  9298. var hd = b.halfDepth;
  9299. var len;
  9300. var overlap = 0;
  9301. this.dix.set( D[0], D[1], D[2] );
  9302. this.diy.set( D[3], D[4], D[5] );
  9303. this.diz.set( D[6], D[7], D[8] );
  9304. n.sub( b.position, pn.position );
  9305. n.x *= pn.normal.x;//+ rad;
  9306. n.y *= pn.normal.y;
  9307. n.z *= pn.normal.z;//+ rad;
  9308. cc.set(
  9309. _Math.dotVectors( this.dix, n ),
  9310. _Math.dotVectors( this.diy, n ),
  9311. _Math.dotVectors( this.diz, n )
  9312. );
  9313. if( cc.x > hw ) cc.x = hw;
  9314. else if( cc.x < -hw ) cc.x = -hw;
  9315. else overlap = 1;
  9316. if( cc.y > hh ) cc.y = hh;
  9317. else if( cc.y < -hh ) cc.y = -hh;
  9318. else overlap |= 2;
  9319. if( cc.z > hd ) cc.z = hd;
  9320. else if( cc.z < -hd ) cc.z = -hd;
  9321. else overlap |= 4;
  9322. if( overlap === 7 ){
  9323. // center of sphere is in the box
  9324. n.set(
  9325. cc.x < 0 ? hw + cc.x : hw - cc.x,
  9326. cc.y < 0 ? hh + cc.y : hh - cc.y,
  9327. cc.z < 0 ? hd + cc.z : hd - cc.z
  9328. );
  9329. if( n.x < n.y ){
  9330. if( n.x < n.z ){
  9331. len = n.x - hw;
  9332. if( cc.x < 0 ){
  9333. cc.x = -hw;
  9334. n.copy( this.dix );
  9335. }else{
  9336. cc.x = hw;
  9337. n.subEqual( this.dix );
  9338. }
  9339. }else{
  9340. len = n.z - hd;
  9341. if( cc.z < 0 ){
  9342. cc.z = -hd;
  9343. n.copy( this.diz );
  9344. }else{
  9345. cc.z = hd;
  9346. n.subEqual( this.diz );
  9347. }
  9348. }
  9349. }else{
  9350. if( n.y < n.z ){
  9351. len = n.y - hh;
  9352. if( cc.y < 0 ){
  9353. cc.y = -hh;
  9354. n.copy( this.diy );
  9355. }else{
  9356. cc.y = hh;
  9357. n.subEqual( this.diy );
  9358. }
  9359. }else{
  9360. len = n.z - hd;
  9361. if( cc.z < 0 ){
  9362. cc.z = -hd;
  9363. n.copy( this.diz );
  9364. }else{
  9365. cc.z = hd;
  9366. n.subEqual( this.diz );
  9367. }
  9368. }
  9369. }
  9370. p.copy( pn.position ).addScaledVector( n, 1 );
  9371. manifold.addPointVec( p, n, len, this.flip );
  9372. }
  9373. }
  9374. });
  9375. //import { TetraShape } from '../collision/shape/TetraShape';
  9376. /**
  9377. * The class of physical computing world.
  9378. * You must be added to the world physical all computing objects
  9379. *
  9380. * @author saharan
  9381. * @author lo-th
  9382. */
  9383. // timestep, broadphase, iterations, worldscale, random, stat
  9384. function World ( o ) {
  9385. if( !(o instanceof Object) ) o = {};
  9386. // this world scale defaut is 0.1 to 10 meters max for dynamique body
  9387. this.scale = o.worldscale || 1;
  9388. this.invScale = 1/this.scale;
  9389. // The time between each step
  9390. this.timeStep = o.timestep || 0.01666; // 1/60;
  9391. this.timerate = this.timeStep * 1000;
  9392. this.timer = null;
  9393. this.preLoop = null;//function(){};
  9394. this.postLoop = null;//function(){};
  9395. // The number of iterations for constraint solvers.
  9396. this.numIterations = o.iterations || 8;
  9397. // It is a wide-area collision judgment that is used in order to reduce as much as possible a detailed collision judgment.
  9398. switch( o.broadphase || 2 ){
  9399. case 1: this.broadPhase = new BruteForceBroadPhase(); break;
  9400. case 2: default: this.broadPhase = new SAPBroadPhase(); break;
  9401. case 3: this.broadPhase = new DBVTBroadPhase(); break;
  9402. }
  9403. this.Btypes = ['None','BruteForce','Sweep & Prune', 'Bounding Volume Tree' ];
  9404. this.broadPhaseType = this.Btypes[ o.broadphase || 2 ];
  9405. // This is the detailed information of the performance.
  9406. this.performance = null;
  9407. this.isStat = o.info === undefined ? false : o.info;
  9408. if( this.isStat ) this.performance = new InfoDisplay( this );
  9409. /**
  9410. * Whether the constraints randomizer is enabled or not.
  9411. *
  9412. * @property enableRandomizer
  9413. * @type {Boolean}
  9414. */
  9415. this.enableRandomizer = o.random !== undefined ? o.random : true;
  9416. // The rigid body list
  9417. this.rigidBodies=null;
  9418. // number of rigid body
  9419. this.numRigidBodies=0;
  9420. // The contact list
  9421. this.contacts=null;
  9422. this.unusedContacts=null;
  9423. // The number of contact
  9424. this.numContacts=0;
  9425. // The number of contact points
  9426. this.numContactPoints=0;
  9427. // The joint list
  9428. this.joints=null;
  9429. // The number of joints.
  9430. this.numJoints=0;
  9431. // The number of simulation islands.
  9432. this.numIslands=0;
  9433. // The gravity in the world.
  9434. this.gravity = new Vec3(0,-9.8,0);
  9435. if( o.gravity !== undefined ) this.gravity.fromArray( o.gravity );
  9436. var numShapeTypes = 5;//4;//3;
  9437. this.detectors=[];
  9438. this.detectors.length = numShapeTypes;
  9439. var i = numShapeTypes;
  9440. while(i--){
  9441. this.detectors[i]=[];
  9442. this.detectors[i].length = numShapeTypes;
  9443. }
  9444. this.detectors[SHAPE_SPHERE][SHAPE_SPHERE] = new SphereSphereCollisionDetector();
  9445. this.detectors[SHAPE_SPHERE][SHAPE_BOX] = new SphereBoxCollisionDetector(false);
  9446. this.detectors[SHAPE_BOX][SHAPE_SPHERE] = new SphereBoxCollisionDetector(true);
  9447. this.detectors[SHAPE_BOX][SHAPE_BOX] = new BoxBoxCollisionDetector();
  9448. // CYLINDER add
  9449. this.detectors[SHAPE_CYLINDER][SHAPE_CYLINDER] = new CylinderCylinderCollisionDetector();
  9450. this.detectors[SHAPE_CYLINDER][SHAPE_BOX] = new BoxCylinderCollisionDetector(true);
  9451. this.detectors[SHAPE_BOX][SHAPE_CYLINDER] = new BoxCylinderCollisionDetector(false);
  9452. this.detectors[SHAPE_CYLINDER][SHAPE_SPHERE] = new SphereCylinderCollisionDetector(true);
  9453. this.detectors[SHAPE_SPHERE][SHAPE_CYLINDER] = new SphereCylinderCollisionDetector(false);
  9454. // PLANE add
  9455. this.detectors[SHAPE_PLANE][SHAPE_SPHERE] = new SpherePlaneCollisionDetector(true);
  9456. this.detectors[SHAPE_SPHERE][SHAPE_PLANE] = new SpherePlaneCollisionDetector(false);
  9457. this.detectors[SHAPE_PLANE][SHAPE_BOX] = new BoxPlaneCollisionDetector(true);
  9458. this.detectors[SHAPE_BOX][SHAPE_PLANE] = new BoxPlaneCollisionDetector(false);
  9459. // TETRA add
  9460. //this.detectors[SHAPE_TETRA][SHAPE_TETRA] = new TetraTetraCollisionDetector();
  9461. this.randX = 65535;
  9462. this.randA = 98765;
  9463. this.randB = 123456789;
  9464. this.islandRigidBodies = [];
  9465. this.islandStack = [];
  9466. this.islandConstraints = [];
  9467. }
  9468. Object.assign( World.prototype, {
  9469. World: true,
  9470. play: function(){
  9471. if( this.timer !== null ) return;
  9472. var _this = this;
  9473. this.timer = setInterval( function(){ _this.step(); } , this.timerate );
  9474. //this.timer = setInterval( this.loop.bind(this) , this.timerate );
  9475. },
  9476. stop: function(){
  9477. if( this.timer === null ) return;
  9478. clearInterval( this.timer );
  9479. this.timer = null;
  9480. },
  9481. setGravity: function ( ar ) {
  9482. this.gravity.fromArray( ar );
  9483. },
  9484. getInfo: function () {
  9485. return this.isStat ? this.performance.show() : '';
  9486. },
  9487. // Reset the world and remove all rigid bodies, shapes, joints and any object from the world.
  9488. clear:function(){
  9489. this.stop();
  9490. this.preLoop = null;
  9491. this.postLoop = null;
  9492. this.randX = 65535;
  9493. while(this.joints!==null){
  9494. this.removeJoint( this.joints );
  9495. }
  9496. while(this.contacts!==null){
  9497. this.removeContact( this.contacts );
  9498. }
  9499. while(this.rigidBodies!==null){
  9500. this.removeRigidBody( this.rigidBodies );
  9501. }
  9502. },
  9503. /**
  9504. * I'll add a rigid body to the world.
  9505. * Rigid body that has been added will be the operands of each step.
  9506. * @param rigidBody Rigid body that you want to add
  9507. */
  9508. addRigidBody:function( rigidBody ){
  9509. if(rigidBody.parent){
  9510. printError("World", "It is not possible to be added to more than one world one of the rigid body");
  9511. }
  9512. rigidBody.setParent( this );
  9513. //rigidBody.awake();
  9514. for(var shape = rigidBody.shapes; shape !== null; shape = shape.next){
  9515. this.addShape( shape );
  9516. }
  9517. if(this.rigidBodies!==null)(this.rigidBodies.prev=rigidBody).next=this.rigidBodies;
  9518. this.rigidBodies = rigidBody;
  9519. this.numRigidBodies++;
  9520. },
  9521. /**
  9522. * I will remove the rigid body from the world.
  9523. * Rigid body that has been deleted is excluded from the calculation on a step-by-step basis.
  9524. * @param rigidBody Rigid body to be removed
  9525. */
  9526. removeRigidBody:function( rigidBody ){
  9527. var remove=rigidBody;
  9528. if(remove.parent!==this)return;
  9529. remove.awake();
  9530. var js=remove.jointLink;
  9531. while(js!=null){
  9532. var joint=js.joint;
  9533. js=js.next;
  9534. this.removeJoint(joint);
  9535. }
  9536. for(var shape=rigidBody.shapes; shape!==null; shape=shape.next){
  9537. this.removeShape(shape);
  9538. }
  9539. var prev=remove.prev;
  9540. var next=remove.next;
  9541. if(prev!==null) prev.next=next;
  9542. if(next!==null) next.prev=prev;
  9543. if(this.rigidBodies==remove) this.rigidBodies=next;
  9544. remove.prev=null;
  9545. remove.next=null;
  9546. remove.parent=null;
  9547. this.numRigidBodies--;
  9548. },
  9549. getByName: function( name ){
  9550. var body = this.rigidBodies;
  9551. while( body !== null ){
  9552. if( body.name === name ) return body;
  9553. body=body.next;
  9554. }
  9555. var joint = this.joints;
  9556. while( joint !== null ){
  9557. if( joint.name === name ) return joint;
  9558. joint = joint.next;
  9559. }
  9560. return null;
  9561. },
  9562. /**
  9563. * I'll add a shape to the world..
  9564. * Add to the rigid world, and if you add a shape to a rigid body that has been added to the world,
  9565. * Shape will be added to the world automatically, please do not call from outside this method.
  9566. * @param shape Shape you want to add
  9567. */
  9568. addShape:function ( shape ){
  9569. if(!shape.parent || !shape.parent.parent){
  9570. printError("World", "It is not possible to be added alone to shape world");
  9571. }
  9572. shape.proxy = this.broadPhase.createProxy(shape);
  9573. shape.updateProxy();
  9574. this.broadPhase.addProxy(shape.proxy);
  9575. },
  9576. /**
  9577. * I will remove the shape from the world.
  9578. * Add to the rigid world, and if you add a shape to a rigid body that has been added to the world,
  9579. * Shape will be added to the world automatically, please do not call from outside this method.
  9580. * @param shape Shape you want to delete
  9581. */
  9582. removeShape: function ( shape ){
  9583. this.broadPhase.removeProxy(shape.proxy);
  9584. shape.proxy = null;
  9585. },
  9586. /**
  9587. * I'll add a joint to the world.
  9588. * Joint that has been added will be the operands of each step.
  9589. * @param shape Joint to be added
  9590. */
  9591. addJoint: function ( joint ) {
  9592. if(joint.parent){
  9593. printError("World", "It is not possible to be added to more than one world one of the joint");
  9594. }
  9595. if(this.joints!=null)(this.joints.prev=joint).next=this.joints;
  9596. this.joints=joint;
  9597. joint.setParent( this );
  9598. this.numJoints++;
  9599. joint.awake();
  9600. joint.attach();
  9601. },
  9602. /**
  9603. * I will remove the joint from the world.
  9604. * Joint that has been added will be the operands of each step.
  9605. * @param shape Joint to be deleted
  9606. */
  9607. removeJoint: function ( joint ) {
  9608. var remove=joint;
  9609. var prev=remove.prev;
  9610. var next=remove.next;
  9611. if(prev!==null)prev.next=next;
  9612. if(next!==null)next.prev=prev;
  9613. if(this.joints==remove)this.joints=next;
  9614. remove.prev=null;
  9615. remove.next=null;
  9616. this.numJoints--;
  9617. remove.awake();
  9618. remove.detach();
  9619. remove.parent=null;
  9620. },
  9621. addContact: function ( s1, s2 ) {
  9622. var newContact;
  9623. if(this.unusedContacts!==null){
  9624. newContact=this.unusedContacts;
  9625. this.unusedContacts=this.unusedContacts.next;
  9626. }else{
  9627. newContact = new Contact();
  9628. }
  9629. newContact.attach(s1,s2);
  9630. newContact.detector = this.detectors[s1.type][s2.type];
  9631. if(this.contacts)(this.contacts.prev = newContact).next = this.contacts;
  9632. this.contacts = newContact;
  9633. this.numContacts++;
  9634. },
  9635. removeContact: function ( contact ) {
  9636. var prev = contact.prev;
  9637. var next = contact.next;
  9638. if(next) next.prev = prev;
  9639. if(prev) prev.next = next;
  9640. if(this.contacts == contact) this.contacts = next;
  9641. contact.prev = null;
  9642. contact.next = null;
  9643. contact.detach();
  9644. contact.next = this.unusedContacts;
  9645. this.unusedContacts = contact;
  9646. this.numContacts--;
  9647. },
  9648. getContact: function ( b1, b2 ) {
  9649. b1 = b1.constructor === RigidBody ? b1.name : b1;
  9650. b2 = b2.constructor === RigidBody ? b2.name : b2;
  9651. var n1, n2;
  9652. var contact = this.contacts;
  9653. while(contact!==null){
  9654. n1 = contact.body1.name;
  9655. n2 = contact.body2.name;
  9656. if((n1===b1 && n2===b2) || (n2===b1 && n1===b2)){ if(contact.touching) return contact; else return null;}
  9657. else contact = contact.next;
  9658. }
  9659. return null;
  9660. },
  9661. checkContact: function ( name1, name2 ) {
  9662. var n1, n2;
  9663. var contact = this.contacts;
  9664. while(contact!==null){
  9665. n1 = contact.body1.name || ' ';
  9666. n2 = contact.body2.name || ' ';
  9667. if((n1==name1 && n2==name2) || (n2==name1 && n1==name2)){ if(contact.touching) return true; else return false;}
  9668. else contact = contact.next;
  9669. }
  9670. //return false;
  9671. },
  9672. callSleep: function( body ) {
  9673. if( !body.allowSleep ) return false;
  9674. if( body.linearVelocity.lengthSq() > 0.04 ) return false;
  9675. if( body.angularVelocity.lengthSq() > 0.25 ) return false;
  9676. return true;
  9677. },
  9678. /**
  9679. * I will proceed only time step seconds time of World.
  9680. */
  9681. step: function () {
  9682. var stat = this.isStat;
  9683. if( stat ) this.performance.setTime( 0 );
  9684. var body = this.rigidBodies;
  9685. while( body !== null ){
  9686. body.addedToIsland = false;
  9687. if( body.sleeping ) body.testWakeUp();
  9688. body = body.next;
  9689. }
  9690. //------------------------------------------------------
  9691. // UPDATE BROADPHASE CONTACT
  9692. //------------------------------------------------------
  9693. if( stat ) this.performance.setTime( 1 );
  9694. this.broadPhase.detectPairs();
  9695. var pairs = this.broadPhase.pairs;
  9696. var i = this.broadPhase.numPairs;
  9697. //do{
  9698. while(i--){
  9699. //for(var i=0, l=numPairs; i<l; i++){
  9700. var pair = pairs[i];
  9701. var s1;
  9702. var s2;
  9703. if(pair.shape1.id<pair.shape2.id){
  9704. s1 = pair.shape1;
  9705. s2 = pair.shape2;
  9706. }else{
  9707. s1 = pair.shape2;
  9708. s2 = pair.shape1;
  9709. }
  9710. var link;
  9711. if( s1.numContacts < s2.numContacts ) link = s1.contactLink;
  9712. else link = s2.contactLink;
  9713. var exists = false;
  9714. while(link){
  9715. var contact = link.contact;
  9716. if( contact.shape1 == s1 && contact.shape2 == s2 ){
  9717. contact.persisting = true;
  9718. exists = true;// contact already exists
  9719. break;
  9720. }
  9721. link = link.next;
  9722. }
  9723. if(!exists){
  9724. this.addContact( s1, s2 );
  9725. }
  9726. }// while(i-- >0);
  9727. if( stat ) this.performance.calcBroadPhase();
  9728. //------------------------------------------------------
  9729. // UPDATE NARROWPHASE CONTACT
  9730. //------------------------------------------------------
  9731. // update & narrow phase
  9732. this.numContactPoints = 0;
  9733. contact = this.contacts;
  9734. while( contact!==null ){
  9735. if(!contact.persisting){
  9736. if ( contact.shape1.aabb.intersectTest( contact.shape2.aabb ) ) {
  9737. /*var aabb1=contact.shape1.aabb;
  9738. var aabb2=contact.shape2.aabb;
  9739. if(
  9740. aabb1.minX>aabb2.maxX || aabb1.maxX<aabb2.minX ||
  9741. aabb1.minY>aabb2.maxY || aabb1.maxY<aabb2.minY ||
  9742. aabb1.minZ>aabb2.maxZ || aabb1.maxZ<aabb2.minZ
  9743. ){*/
  9744. var next = contact.next;
  9745. this.removeContact(contact);
  9746. contact = next;
  9747. continue;
  9748. }
  9749. }
  9750. var b1 = contact.body1;
  9751. var b2 = contact.body2;
  9752. if( b1.isDynamic && !b1.sleeping || b2.isDynamic && !b2.sleeping ) contact.updateManifold();
  9753. this.numContactPoints += contact.manifold.numPoints;
  9754. contact.persisting = false;
  9755. contact.constraint.addedToIsland = false;
  9756. contact = contact.next;
  9757. }
  9758. if( stat ) this.performance.calcNarrowPhase();
  9759. //------------------------------------------------------
  9760. // SOLVE ISLANDS
  9761. //------------------------------------------------------
  9762. var invTimeStep = 1 / this.timeStep;
  9763. var joint;
  9764. var constraint;
  9765. for( joint = this.joints; joint !== null; joint = joint.next ){
  9766. joint.addedToIsland = false;
  9767. }
  9768. // clear old island array
  9769. this.islandRigidBodies = [];
  9770. this.islandConstraints = [];
  9771. this.islandStack = [];
  9772. if( stat ) this.performance.setTime( 1 );
  9773. this.numIslands = 0;
  9774. // build and solve simulation islands
  9775. for( var base = this.rigidBodies; base !== null; base = base.next ){
  9776. if( base.addedToIsland || base.isStatic || base.sleeping ) continue;// ignore
  9777. if( base.isLonely() ){// update single body
  9778. if( base.isDynamic ){
  9779. base.linearVelocity.addScaledVector( this.gravity, this.timeStep );
  9780. /*base.linearVelocity.x+=this.gravity.x*this.timeStep;
  9781. base.linearVelocity.y+=this.gravity.y*this.timeStep;
  9782. base.linearVelocity.z+=this.gravity.z*this.timeStep;*/
  9783. }
  9784. if( this.callSleep( base ) ) {
  9785. base.sleepTime += this.timeStep;
  9786. if( base.sleepTime > 0.5 ) base.sleep();
  9787. else base.updatePosition( this.timeStep );
  9788. }else{
  9789. base.sleepTime = 0;
  9790. base.updatePosition( this.timeStep );
  9791. }
  9792. this.numIslands++;
  9793. continue;
  9794. }
  9795. var islandNumRigidBodies = 0;
  9796. var islandNumConstraints = 0;
  9797. var stackCount = 1;
  9798. // add rigid body to stack
  9799. this.islandStack[0] = base;
  9800. base.addedToIsland = true;
  9801. // build an island
  9802. do{
  9803. // get rigid body from stack
  9804. body = this.islandStack[--stackCount];
  9805. this.islandStack[stackCount] = null;
  9806. body.sleeping = false;
  9807. // add rigid body to the island
  9808. this.islandRigidBodies[islandNumRigidBodies++] = body;
  9809. if(body.isStatic) continue;
  9810. // search connections
  9811. for( var cs = body.contactLink; cs !== null; cs = cs.next ) {
  9812. var contact = cs.contact;
  9813. constraint = contact.constraint;
  9814. if( constraint.addedToIsland || !contact.touching ) continue;// ignore
  9815. // add constraint to the island
  9816. this.islandConstraints[islandNumConstraints++] = constraint;
  9817. constraint.addedToIsland = true;
  9818. var next = cs.body;
  9819. if(next.addedToIsland) continue;
  9820. // add rigid body to stack
  9821. this.islandStack[stackCount++] = next;
  9822. next.addedToIsland = true;
  9823. }
  9824. for( var js = body.jointLink; js !== null; js = js.next ) {
  9825. constraint = js.joint;
  9826. if(constraint.addedToIsland) continue;// ignore
  9827. // add constraint to the island
  9828. this.islandConstraints[islandNumConstraints++] = constraint;
  9829. constraint.addedToIsland = true;
  9830. next = js.body;
  9831. if( next.addedToIsland || !next.isDynamic ) continue;
  9832. // add rigid body to stack
  9833. this.islandStack[stackCount++] = next;
  9834. next.addedToIsland = true;
  9835. }
  9836. } while( stackCount != 0 );
  9837. // update velocities
  9838. var gVel = new Vec3().addScaledVector( this.gravity, this.timeStep );
  9839. /*var gx=this.gravity.x*this.timeStep;
  9840. var gy=this.gravity.y*this.timeStep;
  9841. var gz=this.gravity.z*this.timeStep;*/
  9842. var j = islandNumRigidBodies;
  9843. while (j--){
  9844. //or(var j=0, l=islandNumRigidBodies; j<l; j++){
  9845. body = this.islandRigidBodies[j];
  9846. if(body.isDynamic){
  9847. body.linearVelocity.addEqual(gVel);
  9848. /*body.linearVelocity.x+=gx;
  9849. body.linearVelocity.y+=gy;
  9850. body.linearVelocity.z+=gz;*/
  9851. }
  9852. }
  9853. // randomizing order
  9854. if(this.enableRandomizer){
  9855. //for(var j=1, l=islandNumConstraints; j<l; j++){
  9856. j = islandNumConstraints;
  9857. while(j--){ if(j!==0){
  9858. var swap = (this.randX=(this.randX*this.randA+this.randB&0x7fffffff))/2147483648.0*j|0;
  9859. constraint = this.islandConstraints[j];
  9860. this.islandConstraints[j] = this.islandConstraints[swap];
  9861. this.islandConstraints[swap] = constraint;
  9862. }
  9863. }
  9864. }
  9865. // solve contraints
  9866. j = islandNumConstraints;
  9867. while(j--){
  9868. //for(j=0, l=islandNumConstraints; j<l; j++){
  9869. this.islandConstraints[j].preSolve( this.timeStep, invTimeStep );// pre-solve
  9870. }
  9871. var k = this.numIterations;
  9872. while(k--){
  9873. //for(var k=0, l=this.numIterations; k<l; k++){
  9874. j = islandNumConstraints;
  9875. while(j--){
  9876. //for(j=0, m=islandNumConstraints; j<m; j++){
  9877. this.islandConstraints[j].solve();// main-solve
  9878. }
  9879. }
  9880. j = islandNumConstraints;
  9881. while(j--){
  9882. //for(j=0, l=islandNumConstraints; j<l; j++){
  9883. this.islandConstraints[j].postSolve();// post-solve
  9884. this.islandConstraints[j] = null;// gc
  9885. }
  9886. // sleeping check
  9887. var sleepTime = 10;
  9888. j = islandNumRigidBodies;
  9889. while(j--){
  9890. //for(j=0, l=islandNumRigidBodies;j<l;j++){
  9891. body = this.islandRigidBodies[j];
  9892. if( this.callSleep( body ) ){
  9893. body.sleepTime += this.timeStep;
  9894. if( body.sleepTime < sleepTime ) sleepTime = body.sleepTime;
  9895. }else{
  9896. body.sleepTime = 0;
  9897. sleepTime = 0;
  9898. continue;
  9899. }
  9900. }
  9901. if(sleepTime > 0.5){
  9902. // sleep the island
  9903. j = islandNumRigidBodies;
  9904. while(j--){
  9905. //for(j=0, l=islandNumRigidBodies;j<l;j++){
  9906. this.islandRigidBodies[j].sleep();
  9907. this.islandRigidBodies[j] = null;// gc
  9908. }
  9909. }else{
  9910. // update positions
  9911. j = islandNumRigidBodies;
  9912. while(j--){
  9913. //for(j=0, l=islandNumRigidBodies;j<l;j++){
  9914. this.islandRigidBodies[j].updatePosition( this.timeStep );
  9915. this.islandRigidBodies[j] = null;// gc
  9916. }
  9917. }
  9918. this.numIslands++;
  9919. }
  9920. //------------------------------------------------------
  9921. // END SIMULATION
  9922. //------------------------------------------------------
  9923. if( stat ) this.performance.calcEnd();
  9924. if( this.postLoop !== null ) this.postLoop();
  9925. },
  9926. // remove someting to world
  9927. remove: function( obj ){
  9928. },
  9929. // add someting to world
  9930. add: function( o ){
  9931. o = o || {};
  9932. var type = o.type || "box";
  9933. if( type.constructor === String ) type = [ type ];
  9934. var isJoint = type[0].substring( 0, 5 ) === 'joint' ? true : false;
  9935. if( isJoint ) return this.initJoint( type[0], o );
  9936. else return this.initBody( type, o );
  9937. },
  9938. initBody: function( type, o ){
  9939. var invScale = this.invScale;
  9940. // body dynamic or static
  9941. var move = o.move || false;
  9942. var kinematic = o.kinematic || false;
  9943. // POSITION
  9944. // body position
  9945. var p = o.pos || [0,0,0];
  9946. p = p.map( function(x) { return x * invScale; } );
  9947. // shape position
  9948. var p2 = o.posShape || [0,0,0];
  9949. p2 = p2.map( function(x) { return x * invScale; } );
  9950. // ROTATION
  9951. // body rotation in degree
  9952. var r = o.rot || [0,0,0];
  9953. r = r.map( function(x) { return x * _Math.degtorad; } );
  9954. // shape rotation in degree
  9955. var r2 = o.rotShape || [0,0,0];
  9956. r2 = r.map( function(x) { return x * _Math.degtorad; } );
  9957. // SIZE
  9958. // shape size
  9959. var s = o.size === undefined ? [1,1,1] : o.size;
  9960. if( s.length === 1 ){ s[1] = s[0]; }
  9961. if( s.length === 2 ){ s[2] = s[0]; }
  9962. s = s.map( function(x) { return x * invScale; } );
  9963. // body physics settings
  9964. var sc = new ShapeConfig();
  9965. // The density of the shape.
  9966. if( o.density !== undefined ) sc.density = o.density;
  9967. // The coefficient of friction of the shape.
  9968. if( o.friction !== undefined ) sc.friction = o.friction;
  9969. // The coefficient of restitution of the shape.
  9970. if( o.restitution !== undefined ) sc.restitution = o.restitution;
  9971. // The bits of the collision groups to which the shape belongs.
  9972. if( o.belongsTo !== undefined ) sc.belongsTo = o.belongsTo;
  9973. // The bits of the collision groups with which the shape collides.
  9974. if( o.collidesWith !== undefined ) sc.collidesWith = o.collidesWith;
  9975. if(o.config !== undefined ){
  9976. if( o.config[0] !== undefined ) sc.density = o.config[0];
  9977. if( o.config[1] !== undefined ) sc.friction = o.config[1];
  9978. if( o.config[2] !== undefined ) sc.restitution = o.config[2];
  9979. if( o.config[3] !== undefined ) sc.belongsTo = o.config[3];
  9980. if( o.config[4] !== undefined ) sc.collidesWith = o.config[4];
  9981. }
  9982. /* if(o.massPos){
  9983. o.massPos = o.massPos.map(function(x) { return x * invScale; });
  9984. sc.relativePosition.set( o.massPos[0], o.massPos[1], o.massPos[2] );
  9985. }
  9986. if(o.massRot){
  9987. o.massRot = o.massRot.map(function(x) { return x * _Math.degtorad; });
  9988. var q = new Quat().setFromEuler( o.massRot[0], o.massRot[1], o.massRot[2] );
  9989. sc.relativeRotation = new Mat33().setQuat( q );//_Math.EulerToMatrix( o.massRot[0], o.massRot[1], o.massRot[2] );
  9990. }*/
  9991. var position = new Vec3( p[0], p[1], p[2] );
  9992. var rotation = new Quat().setFromEuler( r[0], r[1], r[2] );
  9993. // rigidbody
  9994. var body = new RigidBody( position, rotation );
  9995. //var body = new RigidBody( p[0], p[1], p[2], r[0], r[1], r[2], r[3], this.scale, this.invScale );
  9996. // SHAPES
  9997. var shape, n;
  9998. for( var i = 0; i < type.length; i++ ){
  9999. n = i * 3;
  10000. if( p2[n] !== undefined ) sc.relativePosition.set( p2[n], p2[n+1], p2[n+2] );
  10001. if( r2[n] !== undefined ) sc.relativeRotation.setQuat( new Quat().setFromEuler( r2[n], r2[n+1], r2[n+2] ) );
  10002. switch( type[i] ){
  10003. case "sphere": shape = new Sphere( sc, s[n] ); break;
  10004. case "cylinder": shape = new Cylinder( sc, s[n], s[n+1] ); break;
  10005. case "box": shape = new Box( sc, s[n], s[n+1], s[n+2] ); break;
  10006. case "plane": shape = new Plane( sc ); break
  10007. }
  10008. body.addShape( shape );
  10009. }
  10010. // body can sleep or not
  10011. if( o.neverSleep || kinematic) body.allowSleep = false;
  10012. else body.allowSleep = true;
  10013. body.isKinematic = kinematic;
  10014. // body static or dynamic
  10015. if( move ){
  10016. if(o.massPos || o.massRot) body.setupMass( BODY_DYNAMIC, false );
  10017. else body.setupMass( BODY_DYNAMIC, true );
  10018. // body can sleep or not
  10019. //if( o.neverSleep ) body.allowSleep = false;
  10020. //else body.allowSleep = true;
  10021. } else {
  10022. body.setupMass( BODY_STATIC );
  10023. }
  10024. if( o.name !== undefined ) body.name = o.name;
  10025. //else if( move ) body.name = this.numRigidBodies;
  10026. // finaly add to physics world
  10027. this.addRigidBody( body );
  10028. // force sleep on not
  10029. if( move ){
  10030. if( o.sleep ) body.sleep();
  10031. else body.awake();
  10032. }
  10033. return body;
  10034. },
  10035. initJoint: function( type, o ){
  10036. //var type = type;
  10037. var invScale = this.invScale;
  10038. var axe1 = o.axe1 || [1,0,0];
  10039. var axe2 = o.axe2 || [1,0,0];
  10040. var pos1 = o.pos1 || [0,0,0];
  10041. var pos2 = o.pos2 || [0,0,0];
  10042. pos1 = pos1.map(function(x){ return x * invScale; });
  10043. pos2 = pos2.map(function(x){ return x * invScale; });
  10044. var min, max;
  10045. if( type === "jointDistance" ){
  10046. min = o.min || 0;
  10047. max = o.max || 10;
  10048. min = min * invScale;
  10049. max = max * invScale;
  10050. }else{
  10051. min = o.min || 57.29578;
  10052. max = o.max || 0;
  10053. min = min * _Math.degtorad;
  10054. max = max * _Math.degtorad;
  10055. }
  10056. var limit = o.limit || null;
  10057. var spring = o.spring || null;
  10058. var motor = o.motor || null;
  10059. // joint setting
  10060. var jc = new JointConfig();
  10061. jc.scale = this.scale;
  10062. jc.invScale = this.invScale;
  10063. jc.allowCollision = o.collision || false;
  10064. jc.localAxis1.set( axe1[0], axe1[1], axe1[2] );
  10065. jc.localAxis2.set( axe2[0], axe2[1], axe2[2] );
  10066. jc.localAnchorPoint1.set( pos1[0], pos1[1], pos1[2] );
  10067. jc.localAnchorPoint2.set( pos2[0], pos2[1], pos2[2] );
  10068. var b1 = null;
  10069. var b2 = null;
  10070. if( o.body1 === undefined || o.body2 === undefined ) return printError('World', "Can't add joint if attach rigidbodys not define !" );
  10071. if ( o.body1.constructor === String ) { b1 = this.getByName( o.body1 ); }
  10072. else if ( o.body1.constructor === Number ) { b1 = this.getByName( o.body1 ); }
  10073. else if ( o.body1.constructor === RigidBody ) { b1 = o.body1; }
  10074. if ( o.body2.constructor === String ) { b2 = this.getByName( o.body2 ); }
  10075. else if ( o.body2.constructor === Number ) { b2 = this.getByName( o.body2 ); }
  10076. else if ( o.body2.constructor === RigidBody ) { b2 = o.body2; }
  10077. if( b1 === null || b2 === null ) return printError('World', "Can't add joint attach rigidbodys not find !" );
  10078. jc.body1 = b1;
  10079. jc.body2 = b2;
  10080. var joint;
  10081. switch( type ){
  10082. case "jointDistance": joint = new DistanceJoint(jc, min, max);
  10083. if(spring !== null) joint.limitMotor.setSpring( spring[0], spring[1] );
  10084. if(motor !== null) joint.limitMotor.setMotor( motor[0], motor[1] );
  10085. break;
  10086. case "jointHinge": case "joint": joint = new HingeJoint(jc, min, max);
  10087. if(spring !== null) joint.limitMotor.setSpring( spring[0], spring[1] );// soften the joint ex: 100, 0.2
  10088. if(motor !== null) joint.limitMotor.setMotor( motor[0], motor[1] );
  10089. break;
  10090. case "jointPrisme": joint = new PrismaticJoint(jc, min, max); break;
  10091. case "jointSlide": joint = new SliderJoint(jc, min, max); break;
  10092. case "jointBall": joint = new BallAndSocketJoint(jc); break;
  10093. case "jointWheel": joint = new WheelJoint(jc);
  10094. if(limit !== null) joint.rotationalLimitMotor1.setLimit( limit[0], limit[1] );
  10095. if(spring !== null) joint.rotationalLimitMotor1.setSpring( spring[0], spring[1] );
  10096. if(motor !== null) joint.rotationalLimitMotor1.setMotor( motor[0], motor[1] );
  10097. break;
  10098. }
  10099. joint.name = o.name || '';
  10100. // finaly add to physics world
  10101. this.addJoint( joint );
  10102. return joint;
  10103. },
  10104. } );
  10105. // test version
  10106. //export { RigidBody } from './core/RigidBody_X.js';
  10107. //export { World } from './core/World_X.js';
  10108. exports.Math = _Math;
  10109. exports.Vec3 = Vec3;
  10110. exports.Quat = Quat;
  10111. exports.Mat33 = Mat33;
  10112. exports.Shape = Shape;
  10113. exports.Box = Box;
  10114. exports.Sphere = Sphere;
  10115. exports.Cylinder = Cylinder;
  10116. exports.Plane = Plane;
  10117. exports.Particle = Particle;
  10118. exports.ShapeConfig = ShapeConfig;
  10119. exports.LimitMotor = LimitMotor;
  10120. exports.HingeJoint = HingeJoint;
  10121. exports.BallAndSocketJoint = BallAndSocketJoint;
  10122. exports.DistanceJoint = DistanceJoint;
  10123. exports.PrismaticJoint = PrismaticJoint;
  10124. exports.SliderJoint = SliderJoint;
  10125. exports.WheelJoint = WheelJoint;
  10126. exports.JointConfig = JointConfig;
  10127. exports.RigidBody = RigidBody;
  10128. exports.World = World;
  10129. exports.REVISION = REVISION;
  10130. exports.BR_NULL = BR_NULL;
  10131. exports.BR_BRUTE_FORCE = BR_BRUTE_FORCE;
  10132. exports.BR_SWEEP_AND_PRUNE = BR_SWEEP_AND_PRUNE;
  10133. exports.BR_BOUNDING_VOLUME_TREE = BR_BOUNDING_VOLUME_TREE;
  10134. exports.BODY_NULL = BODY_NULL;
  10135. exports.BODY_DYNAMIC = BODY_DYNAMIC;
  10136. exports.BODY_STATIC = BODY_STATIC;
  10137. exports.BODY_KINEMATIC = BODY_KINEMATIC;
  10138. exports.BODY_GHOST = BODY_GHOST;
  10139. exports.SHAPE_NULL = SHAPE_NULL;
  10140. exports.SHAPE_SPHERE = SHAPE_SPHERE;
  10141. exports.SHAPE_BOX = SHAPE_BOX;
  10142. exports.SHAPE_CYLINDER = SHAPE_CYLINDER;
  10143. exports.SHAPE_PLANE = SHAPE_PLANE;
  10144. exports.SHAPE_PARTICLE = SHAPE_PARTICLE;
  10145. exports.SHAPE_TETRA = SHAPE_TETRA;
  10146. exports.JOINT_NULL = JOINT_NULL;
  10147. exports.JOINT_DISTANCE = JOINT_DISTANCE;
  10148. exports.JOINT_BALL_AND_SOCKET = JOINT_BALL_AND_SOCKET;
  10149. exports.JOINT_HINGE = JOINT_HINGE;
  10150. exports.JOINT_WHEEL = JOINT_WHEEL;
  10151. exports.JOINT_SLIDER = JOINT_SLIDER;
  10152. exports.JOINT_PRISMATIC = JOINT_PRISMATIC;
  10153. exports.AABB_PROX = AABB_PROX;
  10154. exports.printError = printError;
  10155. exports.InfoDisplay = InfoDisplay;
  10156. Object.defineProperty(exports, '__esModule', { value: true });
  10157. })));