Oimo.js 250 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302
  1. /**
  2. * OimoPhysics DEV 1.1.0a
  3. * @author Saharan / http://el-ement.com/
  4. *
  5. * Oimo.js 2014
  6. * @author LoTh / http://3dflashlo.wordpress.com/
  7. */
  8. var OIMO = { REVISION: 'DEV.1.1.1a' };
  9. OIMO.SHAPE_SPHERE = 0x1;
  10. OIMO.SHAPE_BOX = 0x2;
  11. OIMO.WORLD_SCALE = 1;
  12. OIMO.INV_SCALE = 1;
  13. OIMO.TO_RAD = Math.PI / 180;
  14. OIMO.nextID = 0;
  15. var OIMO_ARRAY_TYPE;
  16. if(!OIMO_ARRAY_TYPE) { OIMO_ARRAY_TYPE = (typeof Float32Array !== 'undefined') ? Float32Array : Array; }
  17. OIMO.World = function(TimeStep, BroadPhaseType, Iterations){
  18. var broadPhaseType = BroadPhaseType || 2;
  19. this.timeStep= TimeStep || 1/60;
  20. this.numIterations = Iterations || 8;
  21. this.rigidBodies=null;
  22. this.numRigidBodies=0;
  23. this.contacts=null;
  24. this.unusedContacts=null;
  25. this.numContacts=0;
  26. this.numContactPoints=0;
  27. this.joints=null;
  28. this.numJoints=0;
  29. this.numIslands=0;
  30. switch(broadPhaseType){
  31. case 1: this.broadPhase=new OIMO.BruteForceBroadPhase(); break;
  32. case 2: default: this.broadPhase=new OIMO.SAPBroadPhase(); break;
  33. case 3: this.broadPhase=new OIMO.DBVTBroadPhase(); break;
  34. }
  35. this.gravity=new OIMO.Vec3(0,-9.80665,0);
  36. this.performance=new OIMO.Performance();
  37. var numShapeTypes=3;
  38. this.detectors=[];
  39. this.detectors.length = numShapeTypes;
  40. var i=numShapeTypes;
  41. while(i--){
  42. //for(var i=0, l=numShapeTypes; i<l; i++){
  43. this.detectors[i]=[];
  44. this.detectors[i].length = numShapeTypes;
  45. }
  46. this.detectors[OIMO.SHAPE_SPHERE][OIMO.SHAPE_SPHERE]=new OIMO.SphereSphereCollisionDetector();
  47. this.detectors[OIMO.SHAPE_SPHERE][OIMO.SHAPE_BOX]=new OIMO.SphereBoxCollisionDetector(false);
  48. this.detectors[OIMO.SHAPE_BOX][OIMO.SHAPE_SPHERE]=new OIMO.SphereBoxCollisionDetector(true);
  49. this.detectors[OIMO.SHAPE_BOX][OIMO.SHAPE_BOX]=new OIMO.BoxBoxCollisionDetector();
  50. this.randX=65535;
  51. this.randA=98765;
  52. this.randB=123456789;
  53. this.maxIslandRigidBodies=64;
  54. this.islandRigidBodies=[];
  55. this.islandRigidBodies.length = this.maxIslandRigidBodies;
  56. this.islandStack=[];
  57. this.islandStack.length = this.maxIslandRigidBodies;
  58. this.maxIslandConstraints=128;
  59. this.islandConstraints=[];
  60. this.islandConstraints.length = this.maxIslandConstraints;
  61. this.enableRandomizer=true;
  62. };
  63. OIMO.World.prototype = {
  64. constructor: OIMO.World,
  65. clear:function(){
  66. this.randX=65535;
  67. while(this.joints!==null){
  68. this.removeJoint(this.joints);
  69. }
  70. while(this.contacts!==null){
  71. this.removeContact(this.contacts);
  72. }
  73. while(this.rigidBodies!==null){
  74. this.removeRigidBody(this.rigidBodies);
  75. }
  76. OIMO.nextID=0;
  77. },
  78. addRigidBody:function(rigidBody){
  79. if(rigidBody.parent){
  80. throw new Error("It is not possible to be added to more than one world one of the rigid body");
  81. }
  82. rigidBody.parent=this;
  83. rigidBody.awake();
  84. for(var shape=rigidBody.shapes; shape!==null; shape=shape.next){
  85. this.addShape(shape);
  86. }
  87. if(this.rigidBodies!==null)(this.rigidBodies.prev=rigidBody).next=this.rigidBodies;
  88. this.rigidBodies = rigidBody;
  89. this.numRigidBodies++;
  90. },
  91. removeRigidBody:function(rigidBody){
  92. var remove=rigidBody;
  93. if(remove.parent!==this)return;
  94. remove.awake();
  95. var js=remove.jointLink;
  96. while(js!=null){
  97. var joint=js.joint;
  98. js=js.next;
  99. this.removeJoint(joint);
  100. }
  101. for(var shape=rigidBody.shapes; shape!==null; shape=shape.next){
  102. this.removeShape(shape);
  103. }
  104. var prev=remove.prev;
  105. var next=remove.next;
  106. if(prev!==null)prev.next=next;
  107. if(next!==null)next.prev=prev;
  108. if(this.rigidBodies===remove)this.rigidBodies=next;
  109. remove.prev=null;
  110. remove.next=null;
  111. remove.parent=null;
  112. this.numRigidBodies--;
  113. },
  114. getByName:function(name){
  115. var result = null;
  116. var body=this.rigidBodies;
  117. while(body!==null){
  118. if(body.name!== "" && body.name === name) result = body;
  119. body=body.next;
  120. }
  121. var joint=this.joints;
  122. while(joint!==null){
  123. if(joint.name!== "" && joint.name === name) result = joint;
  124. joint=joint.next;
  125. }
  126. return result;
  127. },
  128. addShape:function(shape){
  129. if(!shape.parent || !shape.parent.parent){
  130. throw new Error("It is not possible to be added alone to shape world");
  131. }
  132. shape.proxy = this.broadPhase.createProxy(shape);
  133. shape.updateProxy();
  134. this.broadPhase.addProxy(shape.proxy);
  135. },
  136. removeShape:function(shape){
  137. this.broadPhase.removeProxy(shape.proxy);
  138. shape.proxy=null;
  139. },
  140. addJoint:function(joint){
  141. if(joint.parent){
  142. throw new Error("It is not possible to be added to more than one world one of the joint");
  143. }
  144. if(this.joints!=null)(this.joints.prev=joint).next=this.joints;
  145. this.joints=joint;
  146. joint.parent=this;
  147. this.numJoints++;
  148. joint.awake();
  149. joint.attach();
  150. },
  151. removeJoint:function(joint){
  152. var remove=joint;
  153. var prev=remove.prev;
  154. var next=remove.next;
  155. if(prev!==null)prev.next=next;
  156. if(next!==null)next.prev=prev;
  157. if(this.joints==remove)this.joints=next;
  158. remove.prev=null;
  159. remove.next=null;
  160. this.numJoints--;
  161. remove.awake();
  162. remove.detach();
  163. remove.parent=null;
  164. },
  165. step:function(){
  166. var time1=Date.now();
  167. var body=this.rigidBodies;
  168. while(body!==null){
  169. body.addedToIsland=false;
  170. if(body.sleeping){
  171. if( body.linearVelocity.testZero() || body.position.testDiff(body.sleepPosition) || body.orientation.testDiff(body.sleepOrientation)){ body.awake(); }
  172. /*var lv=body.linearVelocity;
  173. var av=body.linearVelocity;
  174. var p=body.position;
  175. var sp=body.sleepPosition;
  176. var o=body.orientation;
  177. var so=body.sleepOrientation;
  178. if( lv.x!==0 || lv.y!==0 || lv.z!==0 || av.x!==0 || av.y!==0 || av.z!==0 ||
  179. p.x!==sp.x || p.y!==sp.y || p.z!==sp.z ||
  180. o.s!==so.s || o.x!==so.x || o.y!==so.y || o.z!==so.z
  181. ){ body.awake(); }*/
  182. }
  183. body=body.next;
  184. }
  185. this.updateContacts();
  186. this.solveIslands();
  187. var time2=Date.now();
  188. // fps update
  189. if (time2 - 1000 > this.performance.time_prev) {
  190. this.performance.time_prev = time2;
  191. this.performance.fpsint = this.performance.fps;
  192. this.performance.fps = 0;
  193. } this.performance.fps++;
  194. this.performance.totalTime=time2-time1;
  195. this.performance.updatingTime=this.performance.totalTime-(this.performance.broadPhaseTime+this.performance.narrowPhaseTime+this.performance.solvingTime);
  196. },
  197. updateContacts:function(){
  198. var time1=Date.now();
  199. this.broadPhase.detectPairs();
  200. var pairs=this.broadPhase.pairs;
  201. var numPairs=this.broadPhase.numPairs;
  202. var i = numPairs;
  203. while(i--){
  204. //for(var i=0, l=numPairs; i<l; i++){
  205. var pair=pairs[i];
  206. var s1;
  207. var s2;
  208. if(pair.shape1.id<pair.shape2.id){
  209. s1=pair.shape1;
  210. s2=pair.shape2;
  211. }else{
  212. s1=pair.shape2;
  213. s2=pair.shape1;
  214. }
  215. var link;
  216. if(s1.numContacts<s2.numContacts){
  217. link=s1.contactLink;
  218. }else{
  219. link=s2.contactLink;
  220. }
  221. var exists=false;
  222. while(link){
  223. var contact=link.contact;
  224. if(contact.shape1==s1&&contact.shape2==s2){
  225. contact.persisting=true;
  226. exists=true;
  227. break;
  228. }
  229. link=link.next;
  230. }
  231. if(!exists){
  232. this.addContact(s1,s2);
  233. }
  234. }
  235. var time2=Date.now();
  236. this.performance.broadPhaseTime=time2-time1;
  237. this.numContactPoints=0;
  238. contact=this.contacts;
  239. while(contact!==null){
  240. if(!contact.persisting){
  241. var aabb1=contact.shape1.aabb;
  242. var aabb2=contact.shape2.aabb;
  243. if(
  244. aabb1.minX>aabb2.maxX || aabb1.maxX<aabb2.minX ||
  245. aabb1.minY>aabb2.maxY || aabb1.maxY<aabb2.minY ||
  246. aabb1.minZ>aabb2.maxZ || aabb1.maxZ<aabb2.minZ
  247. ){
  248. var next=contact.next;
  249. this.removeContact(contact);
  250. contact=next;
  251. continue;
  252. }
  253. }
  254. var b1=contact.body1;
  255. var b2=contact.body2;
  256. if(b1.isDynamic && !b1.sleeping || b2.isDynamic && !b2.sleeping){
  257. contact.updateManifold();
  258. }
  259. this.numContactPoints+=contact.manifold.numPoints;
  260. contact.persisting=false;
  261. contact.constraint.addedToIsland=false;
  262. contact=contact.next;
  263. }
  264. var time3=Date.now();
  265. this.performance.narrowPhaseTime=time3-time2;
  266. },
  267. addContact:function(s1,s2){
  268. var newContact;
  269. if(this.unusedContacts!==null){
  270. newContact=this.unusedContacts;
  271. this.unusedContacts=this.unusedContacts.next;
  272. }else{
  273. newContact=new OIMO.Contact();
  274. }
  275. newContact.attach(s1,s2);
  276. newContact.detector=this.detectors[s1.type][s2.type];
  277. if(this.contacts)(this.contacts.prev=newContact).next=this.contacts;
  278. this.contacts=newContact;
  279. this.numContacts++;
  280. },
  281. removeContact:function(contact){
  282. var prev=contact.prev;
  283. var next=contact.next;
  284. if(next)next.prev=prev;
  285. if(prev)prev.next=next;
  286. if(this.contacts==contact)this.contacts=next;
  287. contact.prev=null;
  288. contact.next=null;
  289. contact.detach();
  290. contact.next=this.unusedContacts;
  291. this.unusedContacts=contact;
  292. this.numContacts--;
  293. },
  294. calSleep:function(body){
  295. if(!body.allowSleep)return false;
  296. if(body.linearVelocity.len()>0.04)return false;
  297. if(body.angularVelocity.len()>0.25)return false;
  298. /*var v=body.linearVelocity;
  299. if(v.x*v.x+v.y*v.y+v.z*v.z>0.04){return false;}
  300. v=body.angularVelocity;
  301. if(v.x*v.x+v.y*v.y+v.z*v.z>0.25){return false;}*/
  302. return true;
  303. },
  304. solveIslands:function(){
  305. var invTimeStep=1/this.timeStep;
  306. var body;
  307. var joint;
  308. var constraint;
  309. var num;
  310. for(joint=this.joints; joint!==null; joint=joint.next){
  311. joint.addedToIsland=false;
  312. }
  313. // expand island buffers
  314. if(this.maxIslandRigidBodies<this.numRigidBodies){
  315. //this.maxIslandRigidBodies=this.numRigidBodies<<1;
  316. this.maxIslandRigidBodies=this.numRigidBodies*2;
  317. this.islandRigidBodies=[];
  318. this.islandStack=[];
  319. this.islandRigidBodies.length = this.maxIslandRigidBodies;
  320. this.islandStack.length = this.maxIslandRigidBodies;
  321. }
  322. var numConstraints=this.numJoints+this.numContacts;
  323. if(this.maxIslandConstraints<numConstraints){
  324. //this.maxIslandConstraints=numConstraints<<1;
  325. this.maxIslandConstraints=numConstraints*2;
  326. this.islandConstraints=[];
  327. this.islandConstraints.length = this.maxIslandConstraints;
  328. }
  329. var time1=Date.now();
  330. this.numIslands=0;
  331. // build and solve simulation islands
  332. for(var base=this.rigidBodies; base!==null; base=base.next){
  333. if(base.addedToIsland || base.isStatic || base.sleeping){
  334. continue;// ignore
  335. }
  336. if(base.isLonely()){// update single body
  337. if(base.isDynamic){
  338. base.linearVelocity.addTime(this.gravity, this.timeStep);
  339. /*base.linearVelocity.x+=this.gravity.x*this.timeStep;
  340. base.linearVelocity.y+=this.gravity.y*this.timeStep;
  341. base.linearVelocity.z+=this.gravity.z*this.timeStep;*/
  342. }
  343. if(this.calSleep(base)){
  344. base.sleepTime+=this.timeStep;
  345. if(base.sleepTime>0.5){
  346. base.sleep();
  347. }else{
  348. base.updatePosition(this.timeStep);
  349. }
  350. }else{
  351. base.sleepTime=0;
  352. base.updatePosition(this.timeStep);
  353. }
  354. this.numIslands++;
  355. continue;
  356. }
  357. var islandNumRigidBodies=0;
  358. var islandNumConstraints=0;
  359. var stackCount=1;
  360. // add rigid body to stack
  361. this.islandStack[0]=base;
  362. base.addedToIsland=true;
  363. // build an island
  364. do{
  365. // get rigid body from stack
  366. body=this.islandStack[--stackCount];
  367. this.islandStack[stackCount]=null;
  368. body.sleeping=false;
  369. // add rigid body to the island
  370. this.islandRigidBodies[islandNumRigidBodies++]=body;
  371. if(body.isStatic){
  372. continue;
  373. }
  374. // search connections
  375. for(var cs=body.contactLink; cs!==null; cs=cs.next){
  376. var contact=cs.contact;
  377. constraint=contact.constraint;
  378. if(constraint.addedToIsland||!contact.touching){
  379. continue;// ignore
  380. }
  381. // add constraint to the island
  382. this.islandConstraints[islandNumConstraints++]=constraint;
  383. constraint.addedToIsland=true;
  384. var next=cs.body;
  385. if(next.addedToIsland){
  386. continue;
  387. }
  388. // add rigid body to stack
  389. this.islandStack[stackCount++]=next;
  390. next.addedToIsland=true;
  391. }
  392. for(var js=body.jointLink; js!==null; js=js.next){
  393. constraint=js.joint;
  394. if(constraint.addedToIsland){
  395. continue;// ignore
  396. }
  397. // add constraint to the island
  398. this.islandConstraints[islandNumConstraints++]=constraint;
  399. constraint.addedToIsland=true;
  400. next=js.body;
  401. if(next.addedToIsland || !next.isDynamic){
  402. continue;
  403. }
  404. // add rigid body to stack
  405. this.islandStack[stackCount++]=next;
  406. next.addedToIsland=true;
  407. }
  408. }while(stackCount!=0);
  409. // update velocities
  410. var gVel = new OIMO.Vec3().addTime(this.gravity, this.timeStep)
  411. /*var gx=this.gravity.x*this.timeStep;
  412. var gy=this.gravity.y*this.timeStep;
  413. var gz=this.gravity.z*this.timeStep;*/
  414. var j = islandNumRigidBodies;
  415. while (j--){
  416. //or(var j=0, l=islandNumRigidBodies; j<l; j++){
  417. body=this.islandRigidBodies[j];
  418. if(body.isDynamic){
  419. body.linearVelocity.addEqual(gVel);
  420. /*body.linearVelocity.x+=gx;
  421. body.linearVelocity.y+=gy;
  422. body.linearVelocity.z+=gz;*/
  423. }
  424. }
  425. // randomizing order
  426. if(this.enableRandomizer){
  427. j = islandNumConstraints;
  428. while(j--){
  429. if(j!==0){
  430. //for(j=1, l=islandNumConstraints; j<l; j++){
  431. var swap=(this.randX=(this.randX*this.randA+this.randB&0x7fffffff))/2147483648.0*j|0;
  432. constraint=this.islandConstraints[j];
  433. this.islandConstraints[j]=this.islandConstraints[swap];
  434. this.islandConstraints[swap]=constraint;
  435. }
  436. }
  437. }
  438. // solve contraints
  439. j = islandNumConstraints;
  440. while(j--){
  441. //for(j=0, l=islandNumConstraints; j<l; j++){
  442. this.islandConstraints[j].preSolve(this.timeStep,invTimeStep);// pre-solve
  443. }
  444. var k = this.numIterations;
  445. while(k--){
  446. //for(var k=0, l=this.numIterations; k<l; k++){
  447. j = islandNumConstraints;
  448. while(j--){
  449. //for(j=0, m=islandNumConstraints; j<m; j++){
  450. this.islandConstraints[j].solve();// main-solve
  451. }
  452. }
  453. j = islandNumConstraints;
  454. while(j--){
  455. //for(j=0, l=islandNumConstraints; j<l; j++){
  456. this.islandConstraints[j].postSolve();// post-solve
  457. this.islandConstraints[j]=null;// gc
  458. }
  459. // sleeping check
  460. var sleepTime=10;
  461. j = islandNumRigidBodies;
  462. while(j--){
  463. //for(j=0, l=islandNumRigidBodies;j<l;j++){
  464. body=this.islandRigidBodies[j];
  465. if(this.calSleep(body)){
  466. body.sleepTime+=this.timeStep;
  467. if(body.sleepTime<sleepTime)sleepTime=body.sleepTime;
  468. }else{
  469. body.sleepTime=0;
  470. sleepTime=0;
  471. continue;
  472. }
  473. }
  474. if(sleepTime>0.5){
  475. // sleep the island
  476. j = islandNumRigidBodies;
  477. while(j--){
  478. //for(j=0, l=islandNumRigidBodies;j<l;j++){
  479. this.islandRigidBodies[j].sleep();
  480. this.islandRigidBodies[j]=null;// gc
  481. }
  482. }else{
  483. // update positions
  484. j = islandNumRigidBodies;
  485. while(j--){
  486. //for(j=0, l=islandNumRigidBodies;j<l;j++){
  487. this.islandRigidBodies[j].updatePosition(this.timeStep);
  488. this.islandRigidBodies[j]=null;// gc
  489. }
  490. }
  491. this.numIslands++;
  492. }
  493. var time2=Date.now();
  494. this.performance.solvingTime=time2-time1;
  495. }
  496. }
  497. OIMO.RigidBody = function(X,Y,Z,Rad,Ax,Ay,Az){
  498. var rad = Rad || 0;
  499. var ax = Ax || 0;
  500. var ay = Ay || 0;
  501. var az = Az || 0;
  502. var x = X || 0;
  503. var y = Y || 0;
  504. var z = Z || 0;
  505. this.name = "";
  506. this.BODY_DYNAMIC=0x1;
  507. this.BODY_STATIC=0x2;
  508. this.MAX_SHAPES=64;
  509. this.prev=null;
  510. this.next=null;
  511. this.type=0;
  512. this.isDynamic=false;
  513. this.isStatic=false;
  514. this.position=null;
  515. this.mass=NaN;
  516. this.inverseMass=NaN;
  517. this.shapes=null;
  518. this.numShapes=0;
  519. this.parent=null;
  520. this.contactLink=null;
  521. this.numContacts=0;
  522. this.jointLink=null;
  523. this.numJoints=0;
  524. this.addedToIsland=false;
  525. this.sleeping=false;
  526. this.massInfo= new OIMO.MassInfo();
  527. this.position=new OIMO.Vec3(x,y,z);
  528. var len=ax*ax+ay*ay+az*az;
  529. if(len>0){
  530. len=1/Math.sqrt(len);
  531. ax*=len;
  532. ay*=len;
  533. az*=len;
  534. }
  535. var sin=Math.sin(rad*0.5);
  536. var cos=Math.cos(rad*0.5);
  537. this.orientation=new OIMO.Quat(cos,sin*ax,sin*ay,sin*az);
  538. this.linearVelocity=new OIMO.Vec3();
  539. this.angularVelocity=new OIMO.Vec3();
  540. this.sleepPosition=new OIMO.Vec3();
  541. this.sleepOrientation=new OIMO.Quat();
  542. this.rotation=new OIMO.Mat33();
  543. this.inverseInertia=new OIMO.Mat33();
  544. this.localInertia=new OIMO.Mat33();
  545. this.inverseLocalInertia=new OIMO.Mat33();
  546. this.matrix = new OIMO.Mat44();
  547. this.allowSleep=true;
  548. this.sleepTime=0;
  549. }
  550. OIMO.RigidBody.prototype = {
  551. constructor: OIMO.RigidBody,
  552. addShape:function(shape){
  553. if(shape.parent){
  554. throw new Error("It is not possible that you add to the multi-rigid body the shape of one");
  555. }
  556. if(this.shapes!=null)(this.shapes.prev=shape).next=this.shapes;
  557. this.shapes=shape;
  558. shape.parent=this;
  559. if(this.parent)this.parent.addShape(shape);
  560. this.numShapes++;
  561. },
  562. removeShape:function(shape){
  563. var remove=shape;
  564. if(remove.parent!=this)return;
  565. var prev=remove.prev;
  566. var next=remove.next;
  567. if(prev!=null)prev.next=next;
  568. if(next!=null)next.prev=prev;
  569. if(this.shapes==remove)this.shapes=next;
  570. remove.prev=null;
  571. remove.next=null;
  572. remove.parent=null;
  573. if(this.parent)this.parent.removeShape(remove);
  574. this.numShapes--;
  575. },
  576. setupMass:function(Type,AdjustPosition){
  577. var adjustPosition = ( AdjustPosition !== undefined ) ? AdjustPosition : true;
  578. var type = Type || this.BODY_DYNAMIC;
  579. //var te = this.localInertia.elements;
  580. this.type=type;
  581. this.isDynamic=type==this.BODY_DYNAMIC;
  582. this.isStatic=type==this.BODY_STATIC;
  583. this.mass=0;
  584. this.localInertia.init(0,0,0,0,0,0,0,0,0);
  585. var te = this.localInertia.elements;
  586. //
  587. var tmpM=new OIMO.Mat33();
  588. var tmpV=new OIMO.Vec3();
  589. for(var shape=this.shapes;shape!=null;shape=shape.next){
  590. shape.calculateMassInfo(this.massInfo);
  591. var shapeMass=this.massInfo.mass;
  592. var relX=shape.relativePosition.x;
  593. var relY=shape.relativePosition.y;
  594. var relZ=shape.relativePosition.z;
  595. /*tmpV.x+=relX*shapeMass;
  596. tmpV.y+=relY*shapeMass;
  597. tmpV.z+=relZ*shapeMass;*/
  598. tmpV.addScale(shape.relativePosition, shapeMass);
  599. this.mass+=shapeMass;
  600. this.rotateInertia(shape.relativeRotation,this.massInfo.inertia,tmpM);
  601. this.localInertia.addEqual(tmpM);
  602. te[0]+=shapeMass*(relY*relY+relZ*relZ);
  603. te[4]+=shapeMass*(relX*relX+relZ*relZ);
  604. te[8]+=shapeMass*(relX*relX+relY*relY);
  605. var xy=shapeMass*relX*relY;
  606. var yz=shapeMass*relY*relZ;
  607. var zx=shapeMass*relZ*relX;
  608. te[1]-=xy;
  609. te[3]-=xy;
  610. te[2]-=yz;
  611. te[6]-=yz;
  612. te[5]-=zx;
  613. te[7]-=zx;
  614. }
  615. this.inverseMass=1/this.mass;
  616. tmpV.scaleEqual(this.inverseMass);
  617. if(adjustPosition){
  618. this.position.addEqual(tmpV);
  619. for(shape=this.shapes;shape!=null;shape=shape.next){
  620. shape.relativePosition.subEqual(tmpV);
  621. }
  622. relX=tmpV.x;
  623. relY=tmpV.y;
  624. relZ=tmpV.z;
  625. //var te = this.localInertia.elements;
  626. te[0]-=this.mass*(relY*relY+relZ*relZ);
  627. te[4]-=this.mass*(relX*relX+relZ*relZ);
  628. te[8]-=this.mass*(relX*relX+relY*relY);
  629. xy=this.mass*relX*relY;
  630. yz=this.mass*relY*relZ;
  631. zx=this.mass*relZ*relX;
  632. te[1]+=xy;
  633. te[3]+=xy;
  634. te[2]+=yz;
  635. te[6]+=yz;
  636. te[5]+=zx;
  637. te[7]+=zx;
  638. }
  639. this.inverseLocalInertia.invert(this.localInertia);
  640. if(type==this.BODY_STATIC){
  641. this.inverseMass=0;
  642. this.inverseLocalInertia.init(0,0,0,0,0,0,0,0,0);
  643. }
  644. this.syncShapes();
  645. this.awake();
  646. },
  647. awake:function(){
  648. if(!this.allowSleep||!this.sleeping)return;
  649. this.sleeping=false;
  650. this.sleepTime=0;
  651. var cs=this.contactLink;
  652. while(cs!=null){
  653. cs.body.sleepTime=0;
  654. cs.body.sleeping=false;
  655. cs=cs.next;
  656. }
  657. var js=this.jointLink;
  658. while(js!=null){
  659. js.body.sleepTime=0;
  660. js.body.sleeping=false;
  661. js=js.next;
  662. }
  663. for(var shape=this.shapes;shape!=null;shape=shape.next){
  664. shape.updateProxy();
  665. }
  666. },
  667. sleep:function(){
  668. if(!this.allowSleep||this.sleeping)return;
  669. this.linearVelocity.init();
  670. this.angularVelocity.init();
  671. this.sleepPosition.copy(this.position);
  672. this.sleepOrientation.copy(this.orientation);
  673. /*this.linearVelocity.x=0;
  674. this.linearVelocity.y=0;
  675. this.linearVelocity.z=0;
  676. this.angularVelocity.x=0;
  677. this.angularVelocity.y=0;
  678. this.angularVelocity.z=0;
  679. this.sleepPosition.x=this.position.x;
  680. this.sleepPosition.y=this.position.y;
  681. this.sleepPosition.z=this.position.z;*/
  682. /*this.sleepOrientation.s=this.orientation.s;
  683. this.sleepOrientation.x=this.orientation.x;
  684. this.sleepOrientation.y=this.orientation.y;
  685. this.sleepOrientation.z=this.orientation.z;*/
  686. this.sleepTime=0;
  687. this.sleeping=true;
  688. for(var shape=this.shapes;shape!=null;shape=shape.next){
  689. shape.updateProxy();
  690. }
  691. },
  692. isLonely:function(){
  693. return this.numJoints==0&&this.numContacts==0;
  694. },
  695. updatePosition:function(timeStep){
  696. switch(this.type){
  697. case this.BODY_STATIC:
  698. this.linearVelocity.init();
  699. this.angularVelocity.init();
  700. /*this.linearVelocity.x=0;
  701. this.linearVelocity.y=0;
  702. this.linearVelocity.z=0;
  703. this.angularVelocity.x=0;
  704. this.angularVelocity.y=0;
  705. this.angularVelocity.z=0;*/
  706. break;
  707. case this.BODY_DYNAMIC:
  708. this.position.addTime(this.linearVelocity, timeStep);
  709. /*var vx=this.linearVelocity.x;
  710. var vy=this.linearVelocity.y;
  711. var vz=this.linearVelocity.z;
  712. this.position.x+=vx*timeStep;
  713. this.position.y+=vy*timeStep;
  714. this.position.z+=vz*timeStep;*/
  715. this.orientation.addTime(this.angularVelocity, timeStep);
  716. /* var vx=this.angularVelocity.x;
  717. var vy=this.angularVelocity.y;
  718. var vz=this.angularVelocity.z;
  719. var os=this.orientation.s;
  720. var ox=this.orientation.x;
  721. var oy=this.orientation.y;
  722. var oz=this.orientation.z;
  723. timeStep*=0.5;
  724. var s=(-vx*ox-vy*oy-vz*oz)*timeStep;
  725. var x=(vx*os+vy*oz-vz*oy)*timeStep;
  726. var y=(-vx*oz+vy*os+vz*ox)*timeStep;
  727. var z=(vx*oy-vy*ox+vz*os)*timeStep;
  728. os+=s;
  729. ox+=x;
  730. oy+=y;
  731. oz+=z;
  732. s=1/Math.sqrt(os*os+ox*ox+oy*oy+oz*oz);
  733. this.orientation.s=os*s;
  734. this.orientation.x=ox*s;
  735. this.orientation.y=oy*s;
  736. this.orientation.z=oz*s;*/
  737. break;
  738. default:
  739. throw new Error("Invalid type.");
  740. }
  741. this.syncShapes();
  742. },
  743. rotateInertia:function(rot,inertia,out){
  744. var tm1 = rot.elements;
  745. var tm2 = inertia.elements;
  746. var a0 = tm1[0], a3 = tm1[3], a6 = tm1[6];
  747. var a1 = tm1[1], a4 = tm1[4], a7 = tm1[7];
  748. var a2 = tm1[2], a5 = tm1[5], a8 = tm1[8];
  749. var b0 = tm2[0], b3 = tm2[3], b6 = tm2[6];
  750. var b1 = tm2[1], b4 = tm2[4], b7 = tm2[7];
  751. var b2 = tm2[2], b5 = tm2[5], b8 = tm2[8];
  752. var e00 = a0*b0 + a1*b3 + a2*b6;
  753. var e01 = a0*b1 + a1*b4 + a2*b7;
  754. var e02 = a0*b2 + a1*b5 + a2*b8;
  755. var e10 = a3*b0 + a4*b3 + a5*b6;
  756. var e11 = a3*b1 + a4*b4 + a5*b7;
  757. var e12 = a3*b2 + a4*b5 + a5*b8;
  758. var e20 = a6*b0 + a7*b3 + a8*b6;
  759. var e21 = a6*b1 + a7*b4 + a8*b7;
  760. var e22 = a6*b2 + a7*b5 + a8*b8;
  761. var oe = out.elements;
  762. oe[0] = e00*a0 + e01*a1 + e02*a2;
  763. oe[1] = e00*a3 + e01*a4 + e02*a5;
  764. oe[2] = e00*a6 + e01*a7 + e02*a8;
  765. oe[3] = e10*a0 + e11*a1 + e12*a2;
  766. oe[4] = e10*a3 + e11*a4 + e12*a5;
  767. oe[5] = e10*a6 + e11*a7 + e12*a8;
  768. oe[6] = e20*a0 + e21*a1 + e22*a2;
  769. oe[7] = e20*a3 + e21*a4 + e22*a5;
  770. oe[8] = e20*a6 + e21*a7 + e22*a8;
  771. },
  772. syncShapes:function(){
  773. var s=this.orientation.s;
  774. var x=this.orientation.x;
  775. var y=this.orientation.y;
  776. var z=this.orientation.z;
  777. var x2=2*x;
  778. var y2=2*y;
  779. var z2=2*z;
  780. var xx=x*x2;
  781. var yy=y*y2;
  782. var zz=z*z2;
  783. var xy=x*y2;
  784. var yz=y*z2;
  785. var xz=x*z2;
  786. var sx=s*x2;
  787. var sy=s*y2;
  788. var sz=s*z2;
  789. var tr = this.rotation.elements;
  790. tr[0]=1-yy-zz;
  791. tr[1]=xy-sz;
  792. tr[2]=xz+sy;
  793. tr[3]=xy+sz;
  794. tr[4]=1-xx-zz;
  795. tr[5]=yz-sx;
  796. tr[6]=xz-sy;
  797. tr[7]=yz+sx;
  798. tr[8]=1-xx-yy;
  799. this.rotateInertia(this.rotation,this.inverseLocalInertia,this.inverseInertia);
  800. for(var shape=this.shapes;shape!=null;shape=shape.next){
  801. //var relPos=shape.relativePosition;
  802. //var relRot=shape.relativeRotation;
  803. //var rot=shape.rotation;
  804. /*var lx=relPos.x;
  805. var ly=relPos.y;
  806. var lz=relPos.z;
  807. shape.position.x=this.position.x+lx*tr[0]+ly*tr[1]+lz*tr[2];
  808. shape.position.y=this.position.y+lx*tr[3]+ly*tr[4]+lz*tr[5];
  809. shape.position.z=this.position.z+lx*tr[6]+ly*tr[7]+lz*tr[8];*/
  810. shape.position.mul(this.position,shape.relativePosition,this.rotation);
  811. shape.rotation.mul(shape.relativeRotation,this.rotation);
  812. shape.updateProxy();
  813. }
  814. },
  815. applyImpulse:function(position,force){
  816. this.linearVelocity.addScale(force, this.inverseMass);
  817. /*this.linearVelocity.x+=force.x*this.inverseMass;
  818. this.linearVelocity.y+=force.y*this.inverseMass;
  819. this.linearVelocity.z+=force.z*this.inverseMass;*/
  820. var rel=new OIMO.Vec3();
  821. rel.sub(position,this.position).cross(rel,force).mulMat(this.inverseInertia,rel);
  822. this.angularVelocity.addEqual(rel);
  823. /*this.angularVelocity.x+=rel.x;
  824. this.angularVelocity.y+=rel.y;
  825. this.angularVelocity.z+=rel.z;*/
  826. },
  827. // for three js
  828. setPosition:function(x,y,z){
  829. this.position.init(x*OIMO.INV_SCALE,y*OIMO.INV_SCALE,z*OIMO.INV_SCALE);
  830. this.linearVelocity.init();
  831. this.angularVelocity.init();
  832. },
  833. setOrientation:function(x,y,z){
  834. // angle in radian
  835. var r = OIMO.EulerToAxis(x, y, z);
  836. var rad = r[0], ax = r[1], ay = r[2], az = r[3];
  837. var len=ax*ax+ay*ay+az*az;
  838. if(len>0){
  839. len=1/Math.sqrt(len);
  840. ax*=len;
  841. ay*=len;
  842. az*=len;
  843. }
  844. var sin=Math.sin(rad*0.5);
  845. var cos=Math.cos(rad*0.5);
  846. this.orientation = new OIMO.Quat(cos,sin*ax,sin*ay,sin*az);
  847. this.angularVelocity.init();
  848. },
  849. getMatrix:function(){
  850. var m = this.matrix.elements;
  851. var r,p;
  852. if(!this.sleeping){
  853. // rotation matrix
  854. r = this.rotation.elements;
  855. m[0] = r[0];
  856. m[1] = r[3];
  857. m[2] = r[6];
  858. m[3] = 0;
  859. m[4] = r[1];
  860. m[5] = r[4];
  861. m[6] = r[7];
  862. m[7] = 0;
  863. m[8] = r[2];
  864. m[9] = r[5];
  865. m[10] = r[8];
  866. m[11] = 0;
  867. // position matrix
  868. p = this.position;
  869. m[12] = p.x*OIMO.WORLD_SCALE;
  870. m[13] = p.y*OIMO.WORLD_SCALE;
  871. m[14] = p.z*OIMO.WORLD_SCALE;
  872. m[15] = 0;
  873. } else {
  874. m[15] = 1;
  875. }
  876. return m;
  877. }
  878. }
  879. OIMO.Body = function(Obj){
  880. var obj = Obj || {};
  881. if(!obj.world) return;
  882. this.name = obj.name || '';
  883. var move = obj.move || false;
  884. var noSleep = obj.noSleep || false;
  885. // position
  886. var p = obj.pos || [0,0,0];
  887. p = p.map(function(x) { return x * OIMO.INV_SCALE; });
  888. // scale
  889. var s = obj.size || [1,1,1];
  890. s = s.map(function(x) { return x * OIMO.INV_SCALE; });
  891. // rotation in degre
  892. var rot = obj.rot || [0,0,0];
  893. rot = rot.map(function(x) { return x * OIMO.TO_RAD; });
  894. var r = [];
  895. for (var i=0; i<rot.length/3; i++){
  896. var tmp = OIMO.EulerToAxis(rot[i+0], rot[i+1], rot[i+2]);
  897. r.push(tmp[0]); r.push(tmp[1]); r.push(tmp[2]); r.push(tmp[3]);
  898. }
  899. // physics setting
  900. var sc = obj.sc || new OIMO.ShapeConfig();
  901. if(obj.config){
  902. sc.density = obj.config[0] || 1;
  903. sc.friction = obj.config[1] || 0.4;
  904. sc.restitution = obj.config[2] || 0.2;
  905. sc.belongsTo = obj.config[3] || 1;
  906. sc.collidesWith = obj.config[4] || 0xffffffff;
  907. }
  908. if(obj.massPos){
  909. obj.massPos = obj.massPos.map(function(x) { return x * OIMO.INV_SCALE; });
  910. sc.relativePosition.init(obj.massPos[0], obj.massPos[1], obj.massPos[2]);
  911. }
  912. if(obj.massRot){
  913. obj.massRot = obj.massRot.map(function(x) { return x * OIMO.TO_RAD; });
  914. sc.relativeRotation = OIMO.EulerToMatrix(obj.massRot[0], obj.massRot[1], obj.massRot[2]);
  915. }
  916. // the rigidbody
  917. this.body = new OIMO.RigidBody(p[0], p[1], p[2], r[0], r[1], r[2], r[3]);
  918. // the shapes
  919. var shapes = [];
  920. var type = obj.type || "box";
  921. if( typeof type === 'string' ) type = [type];// single shape
  922. var n, n2;
  923. for(var i=0; i<type.length; i++){
  924. n = i*3;
  925. n2 = i*4;
  926. switch(type[i]){
  927. case "sphere": shapes[i] = new OIMO.SphereShape(sc, s[n+0]); break;
  928. case "cylinder": shapes[i] = new OIMO.BoxShape(sc, s[n+0], s[n+1], s[n+2]); break; // fake cylinder
  929. case "box": shapes[i] = new OIMO.BoxShape(sc, s[n+0], s[n+1], s[n+2]); break;
  930. }
  931. this.body.addShape(shapes[i]);
  932. if(i>0){
  933. //shapes[i].position.init(p[0]+p[n+0], p[1]+p[n+1], p[2]+p[n+2] );
  934. shapes[i].relativePosition = new OIMO.Vec3( p[n+0], p[n+1], p[n+2] );
  935. if(r[n2+0]) shapes[i].relativeRotation = [ r[n2+0], r[n2+1], r[n2+2], r[n2+3] ];
  936. }
  937. }
  938. // static or move
  939. if(move){
  940. if(obj.massPos || obj.massRot)this.body.setupMass(0x1, false);
  941. else this.body.setupMass(0x1, true);
  942. if(noSleep) this.body.allowSleep = false;
  943. else this.body.allowSleep = true;
  944. } else {
  945. this.body.setupMass(0x2);
  946. }
  947. // finaly add to physics world
  948. this.body.name = this.name;
  949. obj.world.addRigidBody(this.body);
  950. }
  951. OIMO.Link = function(Obj){
  952. var obj = Obj || {};
  953. if(!obj.world) return;
  954. this.name = obj.name || '';
  955. var type = obj.type || "jointHinge";
  956. var axe1 = obj.axe1 || [1,0,0];
  957. var axe2 = obj.axe2 || [1,0,0];
  958. var pos1 = obj.pos1 || [0,0,0];
  959. var pos2 = obj.pos2 || [0,0,0];
  960. pos1 = pos1.map(function(x) { return x * OIMO.INV_SCALE; });
  961. pos2 = pos2.map(function(x) { return x * OIMO.INV_SCALE; });
  962. var min, max;
  963. if(type==="jointDistance"){
  964. min = obj.min || 0;
  965. max = obj.max || 10;
  966. min = min*OIMO.INV_SCALE;
  967. max = max*OIMO.INV_SCALE;
  968. }else{
  969. min = obj.min || 57.29578;
  970. max = obj.max || 0;
  971. min = min*OIMO.TO_RAD;
  972. max = max*OIMO.TO_RAD;
  973. }
  974. var limit = obj.limit || null;
  975. var spring = obj.spring || null;
  976. // joint setting
  977. var jc = new OIMO.JointConfig();
  978. jc.allowCollision = obj.collision || false;;
  979. jc.localAxis1.init(axe1[0], axe1[1], axe1[2]);
  980. jc.localAxis2.init(axe2[0], axe2[1], axe2[2]);
  981. jc.localAnchorPoint1.init(pos1[0], pos1[1], pos1[2]);
  982. jc.localAnchorPoint2.init(pos2[0], pos2[1], pos2[2]);
  983. if (typeof obj.body1 == 'string' || obj.body1 instanceof String) obj.body1 = obj.world.getByName(obj.body1);
  984. if (typeof obj.body2 == 'string' || obj.body2 instanceof String) obj.body2 = obj.world.getByName(obj.body2);
  985. jc.body1 = obj.body1;
  986. jc.body2 = obj.body2;
  987. switch(type){
  988. case "jointDistance": this.joint = new OIMO.DistanceJoint(jc, min, max);
  989. if(spring !== null)this.joint.limitMotor.setSpring(spring[0], spring[1]);
  990. break;
  991. case "jointHinge": this.joint = new OIMO.HingeJoint(jc, min, max);
  992. if(spring !== null)this.joint.limitMotor.setSpring(spring[0], spring[1]);// soften the joint ex: 100, 0.2
  993. break;
  994. case "jointPrisme": this.joint = new OIMO.PrismaticJoint(jc, min, max); break;
  995. case "jointSlide": this.joint = new OIMO.SliderJoint(jc, min, max); break;
  996. case "jointBall": this.joint = new OIMO.BallAndSocketJoint(jc); break;
  997. case "jointWheel": this.joint = new OIMO.WheelJoint(jc);
  998. if(limit !== null) this.joint.rotationalLimitMotor1.setLimit(limit[0], limit[1]);
  999. if(spring !== null) this.joint.rotationalLimitMotor1.setSpring(spring[0], spring[1]);
  1000. break;
  1001. }
  1002. // finaly add to physics world
  1003. this.joint.name = this.name;
  1004. obj.world.addJoint(this.joint);
  1005. }
  1006. OIMO.Performance = function(){
  1007. this.time_prev=0;
  1008. this.fpsint=0;
  1009. this.fps=0;
  1010. this.broadPhaseTime=0;
  1011. this.narrowPhaseTime=0;
  1012. this.solvingTime=0;
  1013. this.updatingTime=0;
  1014. this.totalTime=0;
  1015. }
  1016. OIMO.Mat44 = function(n11, n12, n13, n14, n21, n22, n23, n24, n31, n32, n33, n34, n41, n42, n43, n44){
  1017. this.elements = new OIMO_ARRAY_TYPE(16);
  1018. var te = this.elements;
  1019. te[0] = ( n11 !== undefined ) ? n11 : 1; te[4] = n12 || 0; te[8] = n13 || 0; te[12] = n14 || 0;
  1020. te[1] = n21 || 0; te[5] = ( n22 !== undefined ) ? n22 : 1; te[9] = n23 || 0; te[13] = n24 || 0;
  1021. te[2] = n31 || 0; te[6] = n32 || 0; te[10] = ( n33 !== undefined ) ? n33 : 1; te[14] = n34 || 0;
  1022. te[3] = n41 || 0; te[7] = n42 || 0; te[11] = n43 || 0; te[15] = ( n44 !== undefined ) ? n44 : 1;
  1023. };
  1024. OIMO.Mat44.prototype = {
  1025. constructor: OIMO.Mat44,
  1026. set: function ( n11, n12, n13, n14, n21, n22, n23, n24, n31, n32, n33, n34, n41, n42, n43, n44 ) {
  1027. var te = this.elements;
  1028. te[0] = n11; te[4] = n12; te[8] = n13; te[12] = n14;
  1029. te[1] = n21; te[5] = n22; te[9] = n23; te[13] = n24;
  1030. te[2] = n31; te[6] = n32; te[10] = n33; te[14] = n34;
  1031. te[3] = n41; te[7] = n42; te[11] = n43; te[15] = n44;
  1032. return this;
  1033. }
  1034. }
  1035. OIMO.Mat33 = function(e00,e01,e02,e10,e11,e12,e20,e21,e22){
  1036. this.elements = new OIMO_ARRAY_TYPE(9);
  1037. var te = this.elements;
  1038. this.init(
  1039. ( e00 !== undefined ) ? e00 : 1, e01 || 0, e02 || 0,
  1040. e10 || 0, ( e11 !== undefined ) ? e11 : 1, e12 || 0,
  1041. e20 || 0, e21 || 0, ( e22 !== undefined ) ? e22 : 1
  1042. );
  1043. };
  1044. OIMO.Mat33.prototype = {
  1045. constructor: OIMO.Mat33,
  1046. init: function(e00,e01,e02,e10,e11,e12,e20,e21,e22){
  1047. var te = this.elements;
  1048. te[0] = e00; te[1] = e01; te[2] = e02;
  1049. te[3] = e10; te[4] = e11; te[5] = e12;
  1050. te[6] = e20; te[7] = e21; te[8] = e22;
  1051. return this;
  1052. },
  1053. add: function(m1,m2){
  1054. var te = this.elements;
  1055. var tem1 = m1.elements;
  1056. var tem2 = m2.elements;
  1057. te[0] = tem1[0] + tem2[0]; te[1] = tem1[1] + tem2[1]; te[2] = tem1[2] + tem2[2];
  1058. te[3] = tem1[3] + tem2[3]; te[4] = tem1[4] + tem2[4]; te[5] = tem1[5] + tem2[5];
  1059. te[6] = tem1[6] + tem2[6]; te[7] = tem1[7] + tem2[7]; te[8] = tem1[8] + tem2[8];
  1060. return this;
  1061. },
  1062. addEqual: function(m){
  1063. var te = this.elements;
  1064. var tem = m.elements;
  1065. te[0] += tem[0]; te[1] += tem[1]; te[2] += tem[2];
  1066. te[3] += tem[3]; te[4] += tem[4]; te[5] += tem[5];
  1067. te[6] += tem[6]; te[7] += tem[7]; te[8] += tem[8];
  1068. return this;
  1069. },
  1070. sub: function(m1,m2){
  1071. var te = this.elements;
  1072. var tem1 = m1.elements;
  1073. var tem2 = m2.elements;
  1074. te[0] = tem1[0] - tem2[0]; te[1] = tem1[1] - tem2[1]; te[2] = tem1[2] - tem2[2];
  1075. te[3] = tem1[3] - tem2[3]; te[4] = tem1[4] - tem2[4]; te[5] = tem1[5] - tem2[5];
  1076. te[6] = tem1[6] - tem2[6]; te[7] = tem1[7] - tem2[7]; te[8] = tem1[8] - tem2[8];
  1077. return this;
  1078. },
  1079. subEqual:function(m){
  1080. var te = this.elements;
  1081. var tem = m.elements;
  1082. te[0] -= tem[0]; te[1] -= tem[1]; te[2] -= tem[2];
  1083. te[3] -= tem[3]; te[4] -= tem[4]; te[5] -= tem[5];
  1084. te[6] -= tem[6]; te[7] -= tem[7]; te[8] -= tem[8];
  1085. return this;
  1086. },
  1087. scale: function(m,s){
  1088. var te = this.elements;
  1089. var tm = m.elements;
  1090. te[0] = tm[0] * s; te[1] = tm[1] * s; te[2] = tm[2] * s;
  1091. te[3] = tm[3] * s; te[4] = tm[4] * s; te[5] = tm[5] * s;
  1092. te[6] = tm[6] * s; te[7] = tm[7] * s; te[8] = tm[8] * s;
  1093. return this;
  1094. },
  1095. scaleEqual: function(s){
  1096. var te = this.elements;
  1097. te[0] *= s; te[1] *= s; te[2] *= s;
  1098. te[3] *= s; te[4] *= s; te[5] *= s;
  1099. te[6] *= s; te[7] *= s; te[8] *= s;
  1100. return this;
  1101. },
  1102. mul: function(m1,m2){
  1103. var te = this.elements;
  1104. var tm1 = m1.elements;
  1105. var tm2 = m2.elements;
  1106. var a0 = tm1[0], a3 = tm1[3], a6 = tm1[6];
  1107. var a1 = tm1[1], a4 = tm1[4], a7 = tm1[7];
  1108. var a2 = tm1[2], a5 = tm1[5], a8 = tm1[8];
  1109. var b0 = tm2[0], b3 = tm2[3], b6 = tm2[6];
  1110. var b1 = tm2[1], b4 = tm2[4], b7 = tm2[7];
  1111. var b2 = tm2[2], b5 = tm2[5], b8 = tm2[8];
  1112. te[0] = a0*b0 + a1*b3 + a2*b6;
  1113. te[1] = a0*b1 + a1*b4 + a2*b7;
  1114. te[2] = a0*b2 + a1*b5 + a2*b8;
  1115. te[3] = a3*b0 + a4*b3 + a5*b6;
  1116. te[4] = a3*b1 + a4*b4 + a5*b7;
  1117. te[5] = a3*b2 + a4*b5 + a5*b8;
  1118. te[6] = a6*b0 + a7*b3 + a8*b6;
  1119. te[7] = a6*b1 + a7*b4 + a8*b7;
  1120. te[8] = a6*b2 + a7*b5 + a8*b8;
  1121. return this;
  1122. },
  1123. mulScale: function(m,sx,sy,sz,Prepend){
  1124. var prepend = Prepend || false;
  1125. var te = this.elements;
  1126. var tm = m.elements;
  1127. if(prepend){
  1128. te[0] = sx*tm[0]; te[1] = sx*tm[1]; te[2] = sx*tm[2];
  1129. te[3] = sy*tm[3]; te[4] = sy*tm[4]; te[5] = sy*tm[5];
  1130. te[6] = sz*tm[6]; te[7] = sz*tm[7]; te[8] = sz*tm[8];
  1131. }else{
  1132. te[0] = tm[0]*sx; te[1] = tm[1]*sy; te[2] = tm[2]*sz;
  1133. te[3] = tm[3]*sx; te[4] = tm[4]*sy; te[5] = tm[5]*sz;
  1134. te[6] = tm[6]*sx; te[7] = tm[7]*sy; te[8] = tm[8]*sz;
  1135. }
  1136. return this;
  1137. },
  1138. mulRotate: function(m,rad,ax,ay,az,Prepend){
  1139. var prepend = Prepend || false;
  1140. var s=Math.sin(rad);
  1141. var c=Math.cos(rad);
  1142. var c1=1-c;
  1143. var r00=ax*ax*c1+c;
  1144. var r01=ax*ay*c1-az*s;
  1145. var r02=ax*az*c1+ay*s;
  1146. var r10=ay*ax*c1+az*s;
  1147. var r11=ay*ay*c1+c;
  1148. var r12=ay*az*c1-ax*s;
  1149. var r20=az*ax*c1-ay*s;
  1150. var r21=az*ay*c1+ax*s;
  1151. var r22=az*az*c1+c;
  1152. var tm = m.elements;
  1153. var a0 = tm[0], a3 = tm[3], a6 = tm[6];
  1154. var a1 = tm[1], a4 = tm[4], a7 = tm[7];
  1155. var a2 = tm[2], a5 = tm[5], a8 = tm[8];
  1156. var te = this.elements;
  1157. if(prepend){
  1158. te[0]=r00*a0+r01*a3+r02*a6;
  1159. te[1]=r00*a1+r01*a4+r02*a7;
  1160. te[2]=r00*a2+r01*a5+r02*a8;
  1161. te[3]=r10*a0+r11*a3+r12*a6;
  1162. te[4]=r10*a1+r11*a4+r12*a7;
  1163. te[5]=r10*a2+r11*a5+r12*a8;
  1164. te[6]=r20*a0+r21*a3+r22*a6;
  1165. te[7]=r20*a1+r21*a4+r22*a7;
  1166. te[8]=r20*a2+r21*a5+r22*a8;
  1167. }else{
  1168. te[0]=a0*r00+a1*r10+a2*r20;
  1169. te[1]=a0*r01+a1*r11+a2*r21;
  1170. te[2]=a0*r02+a1*r12+a2*r22;
  1171. te[3]=a3*r00+a4*r10+a5*r20;
  1172. te[4]=a3*r01+a4*r11+a5*r21;
  1173. te[5]=a3*r02+a4*r12+a5*r22;
  1174. te[6]=a6*r00+a7*r10+a8*r20;
  1175. te[7]=a6*r01+a7*r11+a8*r21;
  1176. te[8]=a6*r02+a7*r12+a8*r22;
  1177. }
  1178. return this;
  1179. },
  1180. transpose: function(m){
  1181. var te = this.elements;
  1182. var tm = m.elements;
  1183. te[0] = tm[0]; te[1] = tm[3]; te[2] = tm[6];
  1184. te[3] = tm[1]; te[4] = tm[4]; te[5] = tm[7];
  1185. te[6] = tm[2]; te[7] = tm[5]; te[8] = tm[8];
  1186. return this;
  1187. },
  1188. setQuat: function(q){
  1189. var x2=2*q.x;
  1190. var y2=2*q.y;
  1191. var z2=2*q.z;
  1192. var xx=q.x*x2;
  1193. var yy=q.y*y2;
  1194. var zz=q.z*z2;
  1195. var xy=q.x*y2;
  1196. var yz=q.y*z2;
  1197. var xz=q.x*z2;
  1198. var sx=q.s*x2;
  1199. var sy=q.s*y2;
  1200. var sz=q.s*z2;
  1201. var te = this.elements;
  1202. te[0]=1-yy-zz;
  1203. te[1]=xy-sz;
  1204. te[2]=xz+sy;
  1205. te[3]=xy+sz;
  1206. te[4]=1-xx-zz;
  1207. te[5]=yz-sx;
  1208. te[6]=xz-sy;
  1209. te[7]=yz+sx;
  1210. te[8]=1-xx-yy;
  1211. return this;
  1212. },
  1213. invert: function(m){
  1214. var te = this.elements;
  1215. var tm = m.elements;
  1216. var a0 = tm[0], a3 = tm[3], a6 = tm[6];
  1217. var a1 = tm[1], a4 = tm[4], a7 = tm[7];
  1218. var a2 = tm[2], a5 = tm[5], a8 = tm[8];
  1219. var dt= a0 * (a4*a8-a7*a5) + a3 * (a7*a2-a1*a8) + a6 * (a1*a5-a4*a2);
  1220. if(dt!=0){dt=1/dt;}
  1221. te[0] = dt*(a4*a8 - a5*a7);
  1222. te[1] = dt*(a2*a7 - a1*a8);
  1223. te[2] = dt*(a1*a5 - a2*a4);
  1224. te[3] = dt*(a5*a6 - a3*a8);
  1225. te[4] = dt*(a0*a8 - a2*a6);
  1226. te[5] = dt*(a2*a3 - a0*a5);
  1227. te[6] = dt*(a3*a7 - a4*a6);
  1228. te[7] = dt*(a1*a6 - a0*a7);
  1229. te[8] = dt*(a0*a4 - a1*a3);
  1230. return this;
  1231. },
  1232. copy: function(m){
  1233. var te = this.elements;
  1234. var tem = m.elements;
  1235. te[0] = tem[0]; te[1] = tem[1]; te[2] = tem[2];
  1236. te[3] = tem[3]; te[4] = tem[4]; te[5] = tem[5];
  1237. te[6] = tem[6]; te[7] = tem[7]; te[8] = tem[8];
  1238. return this;
  1239. },
  1240. toEuler: function(){ // not work !!
  1241. function clamp( x ) {
  1242. return Math.min( Math.max( x, -1 ), 1 );
  1243. }
  1244. var te = this.elements;
  1245. var m11 = te[0], m12 = te[3], m13 = te[6];
  1246. var m21 = te[1], m22 = te[4], m23 = te[7];
  1247. var m31 = te[2], m32 = te[5], m33 = te[8];
  1248. var p = new OIMO.Vec3();
  1249. var d = new OIMO.Quat();
  1250. var s;
  1251. p.y = Math.asin( clamp( m13 ) );
  1252. if ( Math.abs( m13 ) < 0.99999 ) {
  1253. p.x = Math.atan2( - m23, m33 );
  1254. p.z = Math.atan2( - m12, m11 );
  1255. } else {
  1256. p.x = Math.atan2( m32, m22 );
  1257. p.z = 0;
  1258. }
  1259. return p;
  1260. },
  1261. clone: function(){
  1262. var te = this.elements;
  1263. return new OIMO.Mat33(
  1264. te[0], te[1], te[2],
  1265. te[3], te[4], te[5],
  1266. te[6], te[7], te[8]
  1267. );
  1268. },
  1269. toString: function(){
  1270. var te = this.elements;
  1271. var text=
  1272. "Mat33|"+te[0].toFixed(4)+", "+te[1].toFixed(4)+", "+te[2].toFixed(4)+"|\n"+
  1273. " |"+te[3].toFixed(4)+", "+te[4].toFixed(4)+", "+te[5].toFixed(4)+"|\n"+
  1274. " |"+te[6].toFixed(4)+", "+te[7].toFixed(4)+", "+te[8].toFixed(4)+"|" ;
  1275. return text;
  1276. }
  1277. };
  1278. OIMO.Quat = function( s, x, y, z){
  1279. this.s=( s !== undefined ) ? s : 1;
  1280. this.x=x || 0;
  1281. this.y=y || 0;
  1282. this.z=z || 0;
  1283. };
  1284. OIMO.Quat.prototype = {
  1285. constructor: OIMO.Quat,
  1286. init: function(s,x,y,z){
  1287. this.s=( s !== undefined ) ? s : 1;
  1288. this.x=x || 0;
  1289. this.y=y || 0;
  1290. this.z=z || 0;
  1291. return this;
  1292. },
  1293. add: function(q1,q2){
  1294. this.s=q1.s+q2.s;
  1295. this.x=q1.x+q2.x;
  1296. this.y=q1.y+q2.y;
  1297. this.z=q1.z+q2.z;
  1298. return this;
  1299. },
  1300. addTime: function(v,t){
  1301. var os=this.s;
  1302. var ox=this.x;
  1303. var oy=this.y;
  1304. var oz=this.z;
  1305. t*=0.5;
  1306. var s=(-v.x*ox-v.y*oy-v.z*oz)*t;
  1307. var x=(v.x*os+v.y*oz-v.z*oy)*t;
  1308. var y=(-v.x*oz+v.y*os+v.z*ox)*t;
  1309. var z=(v.x*oy-v.y*ox+v.z*os)*t;
  1310. os+=s; ox+=x; oy+=y; oz+=z;
  1311. s=1/Math.sqrt(os*os+ox*ox+oy*oy+oz*oz);
  1312. this.s=os*s;
  1313. this.x=ox*s;
  1314. this.y=oy*s;
  1315. this.z=oz*s;
  1316. return this;
  1317. },
  1318. sub: function(q1,q2){
  1319. this.s=q1.s-q2.s;
  1320. this.x=q1.x-q2.x;
  1321. this.y=q1.y-q2.y;
  1322. this.z=q1.z-q2.z;
  1323. return this;
  1324. },
  1325. scale: function(q,s){
  1326. this.s=q.s*s;
  1327. this.x=q.x*s;
  1328. this.y=q.y*s;
  1329. this.z=q.z*s;
  1330. return this;
  1331. },
  1332. mul: function(q1,q2){
  1333. var s=q1.s*q2.s-q1.x*q2.x-q1.y*q2.y-q1.z*q2.z;
  1334. var x=q1.s*q2.x+q1.x*q2.s+q1.y*q2.z-q1.z*q2.y;
  1335. var y=q1.s*q2.y-q1.x*q2.z+q1.y*q2.s+q1.z*q2.x;
  1336. var z=q1.s*q2.z+q1.x*q2.y-q1.y*q2.x+q1.z*q2.s;
  1337. this.s=s;
  1338. this.x=x;
  1339. this.y=y;
  1340. this.z=z;
  1341. return this;
  1342. },
  1343. arc: function(v1,v2){
  1344. var x1=v1.x;
  1345. var y1=v1.y;
  1346. var z1=v1.z;
  1347. var x2=v2.x;
  1348. var y2=v2.y;
  1349. var z2=v2.z;
  1350. var d=x1*x2+y1*y2+z1*z2;
  1351. if(d==-1){
  1352. x2=y1*x1-z1*z1;
  1353. y2=-z1*y1-x1*x1;
  1354. z2=x1*z1+y1*y1;
  1355. d=1/Math.sqrt(x2*x2+y2*y2+z2*z2);
  1356. this.s=0;
  1357. this.x=x2*d;
  1358. this.y=y2*d;
  1359. this.z=z2*d;
  1360. return this;
  1361. }
  1362. var cx=y1*z2-z1*y2;
  1363. var cy=z1*x2-x1*z2;
  1364. var cz=x1*y2-y1*x2;
  1365. this.s=Math.sqrt((1+d)*0.5);
  1366. d=0.5/this.s;
  1367. this.x=cx*d;
  1368. this.y=cy*d;
  1369. this.z=cz*d;
  1370. return this;
  1371. },
  1372. normalize: function(q){
  1373. var len=Math.sqrt(q.s*q.s+q.x*q.x+q.y*q.y+q.z*q.z);
  1374. if(len>0){len=1/len;}
  1375. this.s=q.s*len;
  1376. this.x=q.x*len;
  1377. this.y=q.y*len;
  1378. this.z=q.z*len;
  1379. return this;
  1380. },
  1381. invert: function(q){
  1382. this.s=q.s;
  1383. this.x=-q.x;
  1384. this.y=-q.y;
  1385. this.z=-q.z;
  1386. return this;
  1387. },
  1388. length: function(){
  1389. return Math.sqrt(this.s*this.s+this.x*this.x+this.y*this.y+this.z*this.z);
  1390. },
  1391. copy: function(q){
  1392. this.s=q.s;
  1393. this.x=q.x;
  1394. this.y=q.y;
  1395. this.z=q.z;
  1396. return this;
  1397. },
  1398. testDiff: function(q){
  1399. if(this.s!==q.s || this.x!==q.x || this.y!==q.y || this.z!==q.z) return true;
  1400. else return false;
  1401. },
  1402. clone: function(q){
  1403. return new OIMO.Quat(this.s,this.x,this.y,this.z);
  1404. },
  1405. toString: function(){
  1406. return"Quat["+this.s.toFixed(4)+", ("+this.x.toFixed(4)+", "+this.y.toFixed(4)+", "+this.z.toFixed(4)+")]";
  1407. }
  1408. }
  1409. OIMO.Vec3 = function(x,y,z){
  1410. this.x=x || 0;
  1411. this.y=y || 0;
  1412. this.z=z || 0;
  1413. };
  1414. OIMO.Vec3.prototype = {
  1415. constructor: OIMO.Vec3,
  1416. init: function(x,y,z){
  1417. this.x=x || 0;
  1418. this.y=y || 0;
  1419. this.z=z || 0;
  1420. return this;
  1421. },
  1422. set: function(x,y,z){
  1423. this.x=x;
  1424. this.y=y;
  1425. this.z=z;
  1426. return this;
  1427. },
  1428. add: function(v1,v2){
  1429. this.x=v1.x+v2.x;
  1430. this.y=v1.y+v2.y;
  1431. this.z=v1.z+v2.z;
  1432. return this;
  1433. },
  1434. addEqual: function(v){
  1435. this.x+=v.x;
  1436. this.y+=v.y;
  1437. this.z+=v.z;
  1438. return this;
  1439. },
  1440. addTime: function(v, t){
  1441. this.x+=v.x*t;
  1442. this.y+=v.y*t;
  1443. this.z+=v.z*t;
  1444. return this;
  1445. },
  1446. sub: function(v1,v2){
  1447. this.x=v1.x-v2.x;
  1448. this.y=v1.y-v2.y;
  1449. this.z=v1.z-v2.z;
  1450. return this;
  1451. },
  1452. subEqual: function(v){
  1453. this.x-=v.x;
  1454. this.y-=v.y;
  1455. this.z-=v.z;
  1456. return this;
  1457. },
  1458. addScale: function(v,s){
  1459. this.x+=v.x*s;
  1460. this.y+=v.y*s;
  1461. this.z+=v.z*s;
  1462. return this;
  1463. },
  1464. scale: function(v,s){
  1465. this.x=v.x*s;
  1466. this.y=v.y*s;
  1467. this.z=v.z*s;
  1468. return this;
  1469. },
  1470. scaleEqual: function(s){
  1471. this.x*=s;
  1472. this.y*=s;
  1473. this.z*=s;
  1474. return this;
  1475. },
  1476. dot: function(v){
  1477. return this.x*v.x+this.y*v.y+this.z*v.z;
  1478. },
  1479. cross: function(v1,v2){
  1480. var x=v1.y*v2.z-v1.z*v2.y;
  1481. var y=v1.z*v2.x-v1.x*v2.z;
  1482. var z=v1.x*v2.y-v1.y*v2.x;
  1483. this.x=x;
  1484. this.y=y;
  1485. this.z=z;
  1486. return this;
  1487. },
  1488. mul: function(o, v, m){
  1489. var te = m.elements;
  1490. this.x=o.x+v.x*te[0]+v.y*te[1]+v.z*te[2];
  1491. this.y=o.y+v.x*te[3]+v.y*te[4]+v.z*te[5];
  1492. this.z=o.z+v.x*te[6]+v.y*te[7]+v.z*te[8];
  1493. return this;
  1494. },
  1495. mulMat: function(m,v){
  1496. var te = m.elements;
  1497. var x=te[0]*v.x+te[1]*v.y+te[2]*v.z;
  1498. var y=te[3]*v.x+te[4]*v.y+te[5]*v.z;
  1499. var z=te[6]*v.x+te[7]*v.y+te[8]*v.z;
  1500. this.x=x;
  1501. this.y=y;
  1502. this.z=z;
  1503. return this;
  1504. },
  1505. normalize: function(v){
  1506. var length=Math.sqrt(v.x*v.x+v.y*v.y+v.z*v.z);
  1507. if(length>0)length=1/length;
  1508. this.x=v.x*length;
  1509. this.y=v.y*length;
  1510. this.z=v.z*length;
  1511. return this;
  1512. },
  1513. invert: function(v){
  1514. this.x=-v.x;
  1515. this.y=-v.y;
  1516. this.z=-v.z;
  1517. return this;
  1518. },
  1519. length: function(){
  1520. return Math.sqrt(this.x*this.x+this.y*this.y+this.z*this.z);
  1521. },
  1522. len: function(){
  1523. return (this.x*this.x+this.y*this.y+this.z*this.z);
  1524. },
  1525. copy: function(v){
  1526. this.x=v.x;
  1527. this.y=v.y;
  1528. this.z=v.z;
  1529. return this;
  1530. },
  1531. testZero: function(){
  1532. if(this.x!==0 || this.y!==0 || this.z!==0) return true;
  1533. else return false;
  1534. },
  1535. testDiff: function(v){
  1536. if(this.x!==v.x || this.y!==v.y || this.z!==v.z) return true;
  1537. else return false;
  1538. },
  1539. clone: function(){
  1540. return new OIMO.Vec3(this.x,this.y,this.z);
  1541. },
  1542. toString: function(){
  1543. return"Vec3["+this.x.toFixed(4)+", "+this.y.toFixed(4)+", "+this.z.toFixed(4)+"]";
  1544. }
  1545. }
  1546. OIMO.EulerToAxis = function( ox, oy, oz ){// angles in radians
  1547. var c1 = Math.cos(oy*0.5);//heading
  1548. var s1 = Math.sin(oy*0.5);
  1549. var c2 = Math.cos(oz*0.5);//altitude
  1550. var s2 = Math.sin(oz*0.5);
  1551. var c3 = Math.cos(ox*0.5);//bank
  1552. var s3 = Math.sin(ox*0.5);
  1553. var c1c2 = c1*c2;
  1554. var s1s2 = s1*s2;
  1555. var w =c1c2*c3 - s1s2*s3;
  1556. var x =c1c2*s3 + s1s2*c3;
  1557. var y =s1*c2*c3 + c1*s2*s3;
  1558. var z =c1*s2*c3 - s1*c2*s3;
  1559. var angle = 2 * Math.acos(w);
  1560. var norm = x*x+y*y+z*z;
  1561. if (norm < 0.001) {
  1562. x=1;
  1563. y=z=0;
  1564. } else {
  1565. norm = Math.sqrt(norm);
  1566. x /= norm;
  1567. y /= norm;
  1568. z /= norm;
  1569. }
  1570. return [angle, x, y, z];
  1571. }
  1572. OIMO.EulerToMatrix = function( ox, oy, oz ) {// angles in radians
  1573. var ch = Math.cos(oy);//heading
  1574. var sh = Math.sin(oy);
  1575. var ca = Math.cos(oz);//altitude
  1576. var sa = Math.sin(oz);
  1577. var cb = Math.cos(ox);//bank
  1578. var sb = Math.sin(ox);
  1579. var mtx = new OIMO.Mat33();
  1580. var te = mtx.elements;
  1581. te[0] = ch * ca;
  1582. te[1] = sh*sb - ch*sa*cb;
  1583. te[2] = ch*sa*sb + sh*cb;
  1584. te[3] = sa;
  1585. te[4] = ca*cb;
  1586. te[5] = -ca*sb;
  1587. te[6] = -sh*ca;
  1588. te[7] = sh*sa*cb + ch*sb;
  1589. te[8] = -sh*sa*sb + ch*cb;
  1590. return mtx;
  1591. }
  1592. OIMO.MatrixToEuler = function(mtx){// angles in radians
  1593. var te = mtx.elements;
  1594. var x, y, z;
  1595. if (te[3] > 0.998) { // singularity at north pole
  1596. y = Math.atan2(te[2],te[8]);
  1597. z = Math.PI/2;
  1598. x = 0;
  1599. } else if (te[3] < -0.998) { // singularity at south pole
  1600. y = Math.atan2(te[2],te[8]);
  1601. z = -Math.PI/2;
  1602. x = 0;
  1603. } else {
  1604. y = Math.atan2(-te[6],te[0]);
  1605. x = Math.atan2(-te[5],te[4]);
  1606. z = Math.asin(te[3]);
  1607. }
  1608. return [x, y, z];
  1609. }
  1610. OIMO.Distance3d = function(p1, p2){
  1611. var xd = p2[0]-p1[0];
  1612. var yd = p2[1]-p1[1];
  1613. var zd = p2[2]-p1[2];
  1614. return Math.sqrt(xd*xd + yd*yd + zd*zd);
  1615. }
  1616. OIMO.Constraint = function(){
  1617. this.parent=null;
  1618. this.body1=null;
  1619. this.body2=null;
  1620. this.addedToIsland=false;
  1621. }
  1622. OIMO.Constraint.prototype = {
  1623. constructor: OIMO.Constraint,
  1624. preSolve:function(timeStep,invTimeStep){
  1625. throw new Error("Inheritance error.");
  1626. },
  1627. solve:function(){
  1628. throw new Error("Inheritance error.");
  1629. },
  1630. postSolve:function(){
  1631. throw new Error("Inheritance error.");
  1632. }
  1633. }
  1634. OIMO.Joint = function(config){
  1635. OIMO.Constraint.call( this );
  1636. this.name = "";
  1637. this.JOINT_DISTANCE=0x1;
  1638. this.JOINT_BALL_AND_SOCKET=0x2;
  1639. this.JOINT_HINGE=0x3;
  1640. this.JOINT_WHEEL=0x4;
  1641. this.JOINT_SLIDER=0x5;
  1642. this.JOINT_PRISMATIC=0x6;
  1643. this.type=0;
  1644. this.prev=null;
  1645. this.next=null;
  1646. this.body1=config.body1;
  1647. this.body2=config.body2;
  1648. this.localAnchorPoint1=new OIMO.Vec3().copy(config.localAnchorPoint1);
  1649. this.localAnchorPoint2=new OIMO.Vec3().copy(config.localAnchorPoint2);
  1650. this.relativeAnchorPoint1=new OIMO.Vec3();
  1651. this.relativeAnchorPoint2=new OIMO.Vec3();
  1652. this.anchorPoint1=new OIMO.Vec3();
  1653. this.anchorPoint2=new OIMO.Vec3();
  1654. this.allowCollision=config.allowCollision;
  1655. this.b1Link=new OIMO.JointLink(this);
  1656. this.b2Link=new OIMO.JointLink(this);
  1657. this.matrix = new OIMO.Mat44();
  1658. }
  1659. OIMO.Joint.prototype = Object.create( OIMO.Constraint.prototype );
  1660. OIMO.Joint.prototype.updateAnchorPoints = function () {
  1661. var p1=this.body1.position;
  1662. var p2=this.body2.position;
  1663. var tr1 = this.body1.rotation.elements;
  1664. var tr2 = this.body2.rotation.elements;
  1665. var l1x=this.localAnchorPoint1.x;
  1666. var l1y=this.localAnchorPoint1.y;
  1667. var l1z=this.localAnchorPoint1.z;
  1668. var l2x=this.localAnchorPoint2.x;
  1669. var l2y=this.localAnchorPoint2.y;
  1670. var l2z=this.localAnchorPoint2.z;
  1671. var r1x=l1x*tr1[0]+l1y*tr1[1]+l1z*tr1[2];
  1672. var r1y=l1x*tr1[3]+l1y*tr1[4]+l1z*tr1[5];
  1673. var r1z=l1x*tr1[6]+l1y*tr1[7]+l1z*tr1[8];
  1674. var r2x=l2x*tr2[0]+l2y*tr2[1]+l2z*tr2[2];
  1675. var r2y=l2x*tr2[3]+l2y*tr2[4]+l2z*tr2[5];
  1676. var r2z=l2x*tr2[6]+l2y*tr2[7]+l2z*tr2[8];
  1677. this.relativeAnchorPoint1.x=r1x;
  1678. this.relativeAnchorPoint1.y=r1y;
  1679. this.relativeAnchorPoint1.z=r1z;
  1680. this.relativeAnchorPoint2.x=r2x;
  1681. this.relativeAnchorPoint2.y=r2y;
  1682. this.relativeAnchorPoint2.z=r2z;
  1683. var p1x=r1x+p1.x;
  1684. var p1y=r1y+p1.y;
  1685. var p1z=r1z+p1.z;
  1686. var p2x=r2x+p2.x;
  1687. var p2y=r2y+p2.y;
  1688. var p2z=r2z+p2.z;
  1689. this.anchorPoint1.x=p1x;
  1690. this.anchorPoint1.y=p1y;
  1691. this.anchorPoint1.z=p1z;
  1692. this.anchorPoint2.x=p2x;
  1693. this.anchorPoint2.y=p2y;
  1694. this.anchorPoint2.z=p2z;
  1695. }
  1696. OIMO.Joint.prototype.attach = function () {
  1697. this.b1Link.body=this.body2;
  1698. this.b2Link.body=this.body1;
  1699. if(this.body1.jointLink!=null)(this.b1Link.next=this.body1.jointLink).prev=this.b1Link;
  1700. else this.b1Link.next=null;
  1701. this.body1.jointLink=this.b1Link;
  1702. this.body1.numJoints++;
  1703. if(this.body2.jointLink!=null)(this.b2Link.next=this.body2.jointLink).prev=this.b2Link;
  1704. else this.b2Link.next=null;
  1705. this.body2.jointLink=this.b2Link;
  1706. this.body2.numJoints++;
  1707. }
  1708. OIMO.Joint.prototype.detach = function () {
  1709. var prev=this.b1Link.prev;
  1710. var next=this.b1Link.next;
  1711. if(prev!=null)prev.next=next;
  1712. if(next!=null)next.prev=prev;
  1713. if(this.body1.jointLink==this.b1Link)this.body1.jointLink=next;
  1714. this.b1Link.prev=null;
  1715. this.b1Link.next=null;
  1716. this.b1Link.body=null;
  1717. this.body1.numJoints--;
  1718. prev=this.b2Link.prev;
  1719. next=this.b2Link.next;
  1720. if(prev!=null)prev.next=next;
  1721. if(next!=null)next.prev=prev;
  1722. if(this.body2.jointLink==this.b2Link)this.body2.jointLink=next;
  1723. this.b2Link.prev=null;
  1724. this.b2Link.next=null;
  1725. this.b2Link.body=null;
  1726. this.body2.numJoints--;
  1727. this.b1Link.body=null;
  1728. this.b2Link.body=null;
  1729. }
  1730. OIMO.Joint.prototype.awake = function () {
  1731. this.body1.awake();
  1732. this.body2.awake();
  1733. }
  1734. OIMO.Joint.prototype.preSolve = function (timeStep,invTimeStep) {
  1735. }
  1736. OIMO.Joint.prototype.solve = function () {
  1737. }
  1738. OIMO.Joint.prototype.postSolve = function () {
  1739. }
  1740. // for three js
  1741. OIMO.Joint.prototype.getMatrix = function () {
  1742. var m = this.matrix.elements;
  1743. var p1 = this.anchorPoint1;
  1744. var p2 = this.anchorPoint2;
  1745. m[0] = p1.x * OIMO.WORLD_SCALE;
  1746. m[1] = p1.y * OIMO.WORLD_SCALE;
  1747. m[2] = p1.z * OIMO.WORLD_SCALE;
  1748. m[3] = 0;
  1749. m[4] = p2.x * OIMO.WORLD_SCALE;
  1750. m[5] = p2.y * OIMO.WORLD_SCALE;
  1751. m[6] = p2.z * OIMO.WORLD_SCALE;
  1752. m[7] = 0;
  1753. return m;
  1754. }
  1755. OIMO.JointConfig = function(){
  1756. this.body1 = null;
  1757. this.body2 = null;
  1758. this.localAnchorPoint1=new OIMO.Vec3();
  1759. this.localAnchorPoint2=new OIMO.Vec3();
  1760. this.localAxis1=new OIMO.Vec3();
  1761. this.localAxis2=new OIMO.Vec3();
  1762. this.allowCollision=false;
  1763. }
  1764. OIMO.JointLink = function(joint){
  1765. this.prev = null;
  1766. this.next = null;
  1767. this.body = null;
  1768. this.joint = joint;
  1769. }
  1770. OIMO.LimitMotor = function(axis,fixed){
  1771. this.axis=axis;
  1772. this.angle=0;
  1773. if(fixed)this.lowerLimit=0;
  1774. else this.lowerLimit=1;
  1775. this.upperLimit=0;
  1776. this.motorSpeed=0;
  1777. this.maxMotorForce=0;
  1778. this.frequency=0;
  1779. this.dampingRatio=0;
  1780. }
  1781. OIMO.LimitMotor.prototype = {
  1782. constructor: OIMO.LimitMotor,
  1783. setLimit:function(lowerLimit,upperLimit){
  1784. this.lowerLimit=lowerLimit;
  1785. this.upperLimit=upperLimit;
  1786. },
  1787. setMotor:function(motorSpeed,maxMotorForce){
  1788. this.motorSpeed=motorSpeed;
  1789. this.maxMotorForce=maxMotorForce;
  1790. },
  1791. setSpring:function(frequency,dampingRatio){
  1792. this.frequency=frequency;
  1793. this.dampingRatio=dampingRatio;
  1794. }
  1795. }
  1796. OIMO.BallAndSocketJoint = function(config){
  1797. OIMO.Joint.call( this, config);
  1798. this.type=this.JOINT_BALL_AND_SOCKET;
  1799. this.lc=new OIMO.LinearConstraint(this);
  1800. }
  1801. OIMO.BallAndSocketJoint.prototype = Object.create( OIMO.Joint.prototype );
  1802. OIMO.BallAndSocketJoint.prototype.preSolve = function (timeStep,invTimeStep) {
  1803. this.updateAnchorPoints();
  1804. this.lc.preSolve(timeStep,invTimeStep);
  1805. }
  1806. OIMO.BallAndSocketJoint.prototype.solve = function () {
  1807. this.lc.solve();
  1808. }
  1809. OIMO.BallAndSocketJoint.prototype.postSolve = function () {
  1810. }
  1811. OIMO.DistanceJoint = function(config,minDistance,maxDistance){
  1812. OIMO.Joint.call( this, config);
  1813. this.type=this.JOINT_DISTANCE;
  1814. this.normal=new OIMO.Vec3();
  1815. this.limitMotor=new OIMO.LimitMotor(this.normal,true);
  1816. this.limitMotor.lowerLimit=minDistance;
  1817. this.limitMotor.upperLimit=maxDistance;
  1818. this.t=new OIMO.TranslationalConstraint(this,this.limitMotor);
  1819. }
  1820. OIMO.DistanceJoint.prototype = Object.create( OIMO.Joint.prototype );
  1821. OIMO.DistanceJoint.prototype.preSolve = function (timeStep,invTimeStep) {
  1822. this.updateAnchorPoints();
  1823. var nx=this.anchorPoint2.x-this.anchorPoint1.x;
  1824. var ny=this.anchorPoint2.y-this.anchorPoint1.y;
  1825. var nz=this.anchorPoint2.z-this.anchorPoint1.z;
  1826. var len=Math.sqrt(nx*nx+ny*ny+nz*nz);
  1827. if(len>0)len=1/len;
  1828. this.normal.init(nx*len,ny*len,nz*len);
  1829. this.t.preSolve(timeStep,invTimeStep);
  1830. }
  1831. OIMO.DistanceJoint.prototype.solve = function () {
  1832. this.t.solve();
  1833. }
  1834. OIMO.DistanceJoint.prototype.postSolve = function () {
  1835. }
  1836. OIMO.HingeJoint = function(config,lowerAngleLimit,upperAngleLimit){
  1837. OIMO.Joint.call( this, config);
  1838. this.localAxis1=new OIMO.Vec3().normalize(config.localAxis1);
  1839. this.localAxis2=new OIMO.Vec3().normalize(config.localAxis2);
  1840. var len;
  1841. this.localAxis1X=this.localAxis1.x;
  1842. this.localAxis1Y=this.localAxis1.y;
  1843. this.localAxis1Z=this.localAxis1.z;
  1844. this.localAngAxis1X=this.localAxis1Y*this.localAxis1X-this.localAxis1Z*this.localAxis1Z;
  1845. this.localAngAxis1Y=-this.localAxis1Z*this.localAxis1Y-this.localAxis1X*this.localAxis1X;
  1846. this.localAngAxis1Z=this.localAxis1X*this.localAxis1Z+this.localAxis1Y*this.localAxis1Y;
  1847. len=1/Math.sqrt(this.localAngAxis1X*this.localAngAxis1X+this.localAngAxis1Y*this.localAngAxis1Y+this.localAngAxis1Z*this.localAngAxis1Z);
  1848. this.localAngAxis1X*=len;
  1849. this.localAngAxis1Y*=len;
  1850. this.localAngAxis1Z*=len;
  1851. this.localAxis2X=this.localAxis2.x;
  1852. this.localAxis2Y=this.localAxis2.y;
  1853. this.localAxis2Z=this.localAxis2.z;
  1854. var arc=new OIMO.Mat33().setQuat(new OIMO.Quat().arc(this.localAxis1,this.localAxis2));
  1855. var tarc = arc.elements;
  1856. this.localAngAxis2X=this.localAngAxis1X*tarc[0]+this.localAngAxis1Y*tarc[1]+this.localAngAxis1Z*tarc[2];
  1857. this.localAngAxis2Y=this.localAngAxis1X*tarc[3]+this.localAngAxis1Y*tarc[4]+this.localAngAxis1Z*tarc[5];
  1858. this.localAngAxis2Z=this.localAngAxis1X*tarc[6]+this.localAngAxis1Y*tarc[7]+this.localAngAxis1Z*tarc[8];
  1859. this.type=this.JOINT_HINGE;
  1860. this.nor=new OIMO.Vec3();
  1861. this.tan=new OIMO.Vec3();
  1862. this.bin=new OIMO.Vec3();
  1863. this.limitMotor=new OIMO.LimitMotor(this.nor,false);
  1864. this.limitMotor.lowerLimit=lowerAngleLimit;
  1865. this.limitMotor.upperLimit=upperAngleLimit;
  1866. this.lc=new OIMO.LinearConstraint(this);
  1867. this.r3=new OIMO.Rotational3Constraint(this,this.limitMotor,new OIMO.LimitMotor(this.tan,true),new OIMO.LimitMotor(this.bin,true));
  1868. }
  1869. OIMO.HingeJoint.prototype = Object.create( OIMO.Joint.prototype );
  1870. OIMO.HingeJoint.prototype.preSolve = function (timeStep,invTimeStep) {
  1871. var tmpM;
  1872. var tmp1X;
  1873. var tmp1Y;
  1874. var tmp1Z;
  1875. this.updateAnchorPoints();
  1876. tmpM=this.body1.rotation.elements;
  1877. var axis1X=this.localAxis1X*tmpM[0]+this.localAxis1Y*tmpM[1]+this.localAxis1Z*tmpM[2];
  1878. var axis1Y=this.localAxis1X*tmpM[3]+this.localAxis1Y*tmpM[4]+this.localAxis1Z*tmpM[5];
  1879. var axis1Z=this.localAxis1X*tmpM[6]+this.localAxis1Y*tmpM[7]+this.localAxis1Z*tmpM[8];
  1880. var angAxis1X=this.localAngAxis1X*tmpM[0]+this.localAngAxis1Y*tmpM[1]+this.localAngAxis1Z*tmpM[2];
  1881. var angAxis1Y=this.localAngAxis1X*tmpM[3]+this.localAngAxis1Y*tmpM[4]+this.localAngAxis1Z*tmpM[5];
  1882. var angAxis1Z=this.localAngAxis1X*tmpM[6]+this.localAngAxis1Y*tmpM[7]+this.localAngAxis1Z*tmpM[8];
  1883. tmpM=this.body2.rotation.elements;
  1884. var axis2X=this.localAxis2X*tmpM[0]+this.localAxis2Y*tmpM[1]+this.localAxis2Z*tmpM[2];
  1885. var axis2Y=this.localAxis2X*tmpM[3]+this.localAxis2Y*tmpM[4]+this.localAxis2Z*tmpM[5];
  1886. var axis2Z=this.localAxis2X*tmpM[6]+this.localAxis2Y*tmpM[7]+this.localAxis2Z*tmpM[8];
  1887. var angAxis2X=this.localAngAxis2X*tmpM[0]+this.localAngAxis2Y*tmpM[1]+this.localAngAxis2Z*tmpM[2];
  1888. var angAxis2Y=this.localAngAxis2X*tmpM[3]+this.localAngAxis2Y*tmpM[4]+this.localAngAxis2Z*tmpM[5];
  1889. var angAxis2Z=this.localAngAxis2X*tmpM[6]+this.localAngAxis2Y*tmpM[7]+this.localAngAxis2Z*tmpM[8];
  1890. var nx=axis1X*this.body2.inverseMass+axis2X*this.body1.inverseMass;
  1891. var ny=axis1Y*this.body2.inverseMass+axis2Y*this.body1.inverseMass;
  1892. var nz=axis1Z*this.body2.inverseMass+axis2Z*this.body1.inverseMass;
  1893. tmp1X=Math.sqrt(nx*nx+ny*ny+nz*nz);
  1894. if(tmp1X>0)tmp1X=1/tmp1X;
  1895. nx*=tmp1X;
  1896. ny*=tmp1X;
  1897. nz*=tmp1X;
  1898. var tx=ny*nx-nz*nz;
  1899. var ty=-nz*ny-nx*nx;
  1900. var tz=nx*nz+ny*ny;
  1901. tmp1X=1/Math.sqrt(tx*tx+ty*ty+tz*tz);
  1902. tx*=tmp1X;
  1903. ty*=tmp1X;
  1904. tz*=tmp1X;
  1905. var bx=ny*tz-nz*ty;
  1906. var by=nz*tx-nx*tz;
  1907. var bz=nx*ty-ny*tx;
  1908. this.nor.init(nx,ny,nz);
  1909. this.tan.init(tx,ty,tz);
  1910. this.bin.init(bx,by,bz);
  1911. if(
  1912. nx*(angAxis1Y*angAxis2Z-angAxis1Z*angAxis2Y)+
  1913. ny*(angAxis1Z*angAxis2X-angAxis1X*angAxis2Z)+
  1914. nz*(angAxis1X*angAxis2Y-angAxis1Y*angAxis2X)<0
  1915. ){
  1916. this.limitMotor.angle=-this.acosClamp(angAxis1X*angAxis2X+angAxis1Y*angAxis2Y+angAxis1Z*angAxis2Z);
  1917. }else{
  1918. this.limitMotor.angle=this.acosClamp(angAxis1X*angAxis2X+angAxis1Y*angAxis2Y+angAxis1Z*angAxis2Z);
  1919. }
  1920. tmp1X=axis1Y*axis2Z-axis1Z*axis2Y;
  1921. tmp1Y=axis1Z*axis2X-axis1X*axis2Z;
  1922. tmp1Z=axis1X*axis2Y-axis1Y*axis2X;
  1923. this.r3.limitMotor2.angle=tx*tmp1X+ty*tmp1Y+tz*tmp1Z;
  1924. this.r3.limitMotor3.angle=bx*tmp1X+by*tmp1Y+bz*tmp1Z;
  1925. this.r3.preSolve(timeStep,invTimeStep);
  1926. this.lc.preSolve(timeStep,invTimeStep);
  1927. }
  1928. OIMO.HingeJoint.prototype.solve = function () {
  1929. this.r3.solve();
  1930. this.lc.solve();
  1931. }
  1932. OIMO.HingeJoint.prototype.postSolve = function () {
  1933. }
  1934. OIMO.HingeJoint.prototype.acosClamp = function(cos){
  1935. if(cos>1)return 0;
  1936. else if(cos<-1)return Math.PI;
  1937. else return Math.acos(cos);
  1938. }
  1939. OIMO.PrismaticJoint = function(config,lowerTranslation,upperTranslation){
  1940. OIMO.Joint.call( this, config);
  1941. this.localAxis1=new OIMO.Vec3().normalize(config.localAxis1);
  1942. this.localAxis2=new OIMO.Vec3().normalize(config.localAxis2);
  1943. this.localAxis1X=this.localAxis1.x;
  1944. this.localAxis1Y=this.localAxis1.y;
  1945. this.localAxis1Z=this.localAxis1.z;
  1946. this.localAxis2X=this.localAxis2.x;
  1947. this.localAxis2Y=this.localAxis2.y;
  1948. this.localAxis2Z=this.localAxis2.z;
  1949. this.type=this.JOINT_PRISMATIC;
  1950. this.nor=new OIMO.Vec3();
  1951. this.tan=new OIMO.Vec3();
  1952. this.bin=new OIMO.Vec3();
  1953. this.ac=new OIMO.AngularConstraint(this,new OIMO.Quat().arc(this.localAxis1,this.localAxis2));
  1954. this.limitMotor=new OIMO.LimitMotor(this.nor,true);
  1955. this.limitMotor.lowerLimit=lowerTranslation;
  1956. this.limitMotor.upperLimit=upperTranslation;
  1957. this.t3=new OIMO.Translational3Constraint(this,this.limitMotor,new OIMO.LimitMotor(this.tan,true),new OIMO.LimitMotor(this.bin,true));
  1958. }
  1959. OIMO.PrismaticJoint.prototype = Object.create( OIMO.Joint.prototype );
  1960. OIMO.PrismaticJoint.prototype.preSolve = function (timeStep,invTimeStep) {
  1961. var tmpM;
  1962. var tmp1X;
  1963. var tmp1Y;
  1964. var tmp1Z;
  1965. this.updateAnchorPoints();
  1966. tmpM=this.body1.rotation.elements;
  1967. var axis1X=this.localAxis1X*tmpM[0]+this.localAxis1Y*tmpM[1]+this.localAxis1Z*tmpM[2];
  1968. var axis1Y=this.localAxis1X*tmpM[3]+this.localAxis1Y*tmpM[4]+this.localAxis1Z*tmpM[5];
  1969. var axis1Z=this.localAxis1X*tmpM[6]+this.localAxis1Y*tmpM[7]+this.localAxis1Z*tmpM[8];
  1970. tmpM=this.body2.rotation.elements;
  1971. var axis2X=this.localAxis2X*tmpM[0]+this.localAxis2Y*tmpM[1]+this.localAxis2Z*tmpM[2];
  1972. var axis2Y=this.localAxis2X*tmpM[3]+this.localAxis2Y*tmpM[4]+this.localAxis2Z*tmpM[5];
  1973. var axis2Z=this.localAxis2X*tmpM[6]+this.localAxis2Y*tmpM[7]+this.localAxis2Z*tmpM[8];
  1974. var nx=axis1X*this.body2.inverseMass+axis2X*this.body1.inverseMass;
  1975. var ny=axis1Y*this.body2.inverseMass+axis2Y*this.body1.inverseMass;
  1976. var nz=axis1Z*this.body2.inverseMass+axis2Z*this.body1.inverseMass;
  1977. tmp1X=Math.sqrt(nx*nx+ny*ny+nz*nz);
  1978. if(tmp1X>0)tmp1X=1/tmp1X;
  1979. nx*=tmp1X;
  1980. ny*=tmp1X;
  1981. nz*=tmp1X;
  1982. var tx=ny*nx-nz*nz;
  1983. var ty=-nz*ny-nx*nx;
  1984. var tz=nx*nz+ny*ny;
  1985. tmp1X=1/Math.sqrt(tx*tx+ty*ty+tz*tz);
  1986. tx*=tmp1X;
  1987. ty*=tmp1X;
  1988. tz*=tmp1X;
  1989. var bx=ny*tz-nz*ty;
  1990. var by=nz*tx-nx*tz;
  1991. var bz=nx*ty-ny*tx;
  1992. this.nor.init(nx,ny,nz);
  1993. this.tan.init(tx,ty,tz);
  1994. this.bin.init(bx,by,bz);
  1995. this.ac.preSolve(timeStep,invTimeStep);
  1996. this.t3.preSolve(timeStep,invTimeStep);
  1997. }
  1998. OIMO.PrismaticJoint.prototype.solve = function () {
  1999. this.ac.solve();
  2000. this.t3.solve();
  2001. }
  2002. OIMO.PrismaticJoint.prototype.postSolve = function () {
  2003. }
  2004. OIMO.SliderJoint = function(config,lowerTranslation,upperTranslation){
  2005. OIMO.Joint.call( this, config);
  2006. this.localAxis1=new OIMO.Vec3().normalize(config.localAxis1);
  2007. this.localAxis2=new OIMO.Vec3().normalize(config.localAxis2);
  2008. var len;
  2009. this.localAxis1X=this.localAxis1.x;
  2010. this.localAxis1Y=this.localAxis1.y;
  2011. this.localAxis1Z=this.localAxis1.z;
  2012. this.localAngAxis1X=this.localAxis1Y*this.localAxis1X-this.localAxis1Z*this.localAxis1Z;
  2013. this.localAngAxis1Y=-this.localAxis1Z*this.localAxis1Y-this.localAxis1X*this.localAxis1X;
  2014. this.localAngAxis1Z=this.localAxis1X*this.localAxis1Z+this.localAxis1Y*this.localAxis1Y;
  2015. len=1/Math.sqrt(this.localAngAxis1X*this.localAngAxis1X+this.localAngAxis1Y*this.localAngAxis1Y+this.localAngAxis1Z*this.localAngAxis1Z);
  2016. this.localAngAxis1X*=len;
  2017. this.localAngAxis1Y*=len;
  2018. this.localAngAxis1Z*=len;
  2019. this.localAxis2X=this.localAxis2.x;
  2020. this.localAxis2Y=this.localAxis2.y;
  2021. this.localAxis2Z=this.localAxis2.z;
  2022. var arc=new OIMO.Mat33().setQuat(new OIMO.Quat().arc(this.localAxis1,this.localAxis2));
  2023. var tarc = arc.elements;
  2024. this.localAngAxis2X=this.localAngAxis1X*tarc[0]+this.localAngAxis1Y*tarc[1]+this.localAngAxis1Z*tarc[2];
  2025. this.localAngAxis2Y=this.localAngAxis1X*tarc[3]+this.localAngAxis1Y*tarc[4]+this.localAngAxis1Z*tarc[5];
  2026. this.localAngAxis2Z=this.localAngAxis1X*tarc[6]+this.localAngAxis1Y*tarc[7]+this.localAngAxis1Z*tarc[8];
  2027. this.type=this.joint.Joint.JOINT_SLIDER;
  2028. this.nor=new OIMO.Vec3();
  2029. this.tan=new OIMO.Vec3();
  2030. this.bin=new OIMO.Vec3();
  2031. this.rotationalLimitMotor=new OIMO.LimitMotor(this.nor,false);
  2032. this.r3=new OIMO.Rotational3Constraint(this,this.rotationalLimitMotor,new OIMO.LimitMotor(this.tan,true),new OIMO.LimitMotor(this.bin,true));
  2033. this.translationalLimitMotor=new OIMO.LimitMotor(this.nor,true);
  2034. this.translationalLimitMotor.lowerLimit=lowerTranslation;
  2035. this.translationalLimitMotor.upperLimit=upperTranslation;
  2036. this.t3=new OIMO.Translational3Constraint(this,this.translationalLimitMotor,new OIMO.LimitMotor(this.tan,true),new OIMO.LimitMotor(this.bin,true));
  2037. }
  2038. OIMO.SliderJoint.prototype = Object.create( OIMO.Joint.prototype );
  2039. OIMO.SliderJoint.prototype.preSolve = function (timeStep,invTimeStep) {
  2040. var tmpM;
  2041. var tmp1X;
  2042. var tmp1Y;
  2043. var tmp1Z;
  2044. this.updateAnchorPoints();
  2045. tmpM=this.body1.rotation.elements;
  2046. var axis1X=this.localAxis1X*tmpM[0]+this.localAxis1Y*tmpM[1]+this.localAxis1Z*tmpM[2];
  2047. var axis1Y=this.localAxis1X*tmpM[3]+this.localAxis1Y*tmpM[4]+this.localAxis1Z*tmpM[5];
  2048. var axis1Z=this.localAxis1X*tmpM[6]+this.localAxis1Y*tmpM[7]+this.localAxis1Z*tmpM[8];
  2049. var angAxis1X=this.localAngAxis1X*tmpM[0]+this.localAngAxis1Y*tmpM[1]+this.localAngAxis1Z*tmpM[2];
  2050. var angAxis1Y=this.localAngAxis1X*tmpM[3]+this.localAngAxis1Y*tmpM[4]+this.localAngAxis1Z*tmpM[5];
  2051. var angAxis1Z=this.localAngAxis1X*tmpM[6]+this.localAngAxis1Y*tmpM[7]+this.localAngAxis1Z*tmpM[8];
  2052. tmpM=this.body2.rotation.elements;
  2053. var axis2X=this.localAxis2X*tmpM[0]+this.localAxis2Y*tmpM[1]+this.localAxis2Z*tmpM[2];
  2054. var axis2Y=this.localAxis2X*tmpM[3]+this.localAxis2Y*tmpM[4]+this.localAxis2Z*tmpM[5];
  2055. var axis2Z=this.localAxis2X*tmpM[6]+this.localAxis2Y*tmpM[7]+this.localAxis2Z*tmpM[8];
  2056. var angAxis2X=this.localAngAxis2X*tmpM[0]+this.localAngAxis2Y*tmpM[1]+this.localAngAxis2Z*tmpM[2];
  2057. var angAxis2Y=this.localAngAxis2X*tmpM[3]+this.localAngAxis2Y*tmpM[4]+this.localAngAxis2Z*tmpM[5];
  2058. var angAxis2Z=this.localAngAxis2X*tmpM[6]+this.localAngAxis2Y*tmpM[7]+this.localAngAxis2Z*tmpM[8];
  2059. var nx=axis1X*this.body2.inverseMass+axis2X*this.body1.inverseMass;
  2060. var ny=axis1Y*this.body2.inverseMass+axis2Y*this.body1.inverseMass;
  2061. var nz=axis1Z*this.body2.inverseMass+axis2Z*this.body1.inverseMass;
  2062. tmp1X=Math.sqrt(nx*nx+ny*ny+nz*nz);
  2063. if(tmp1X>0)tmp1X=1/tmp1X;
  2064. nx*=tmp1X;
  2065. ny*=tmp1X;
  2066. nz*=tmp1X;
  2067. var tx=ny*nx-nz*nz;
  2068. var ty=-nz*ny-nx*nx;
  2069. var tz=nx*nz+ny*ny;
  2070. tmp1X=1/Math.sqrt(tx*tx+ty*ty+tz*tz);
  2071. tx*=tmp1X;
  2072. ty*=tmp1X;
  2073. tz*=tmp1X;
  2074. var bx=ny*tz-nz*ty;
  2075. var by=nz*tx-nx*tz;
  2076. var bz=nx*ty-ny*tx;
  2077. this.nor.init(nx,ny,nz);
  2078. this.tan.init(tx,ty,tz);
  2079. this.bin.init(bx,by,bz);
  2080. if(
  2081. nx*(angAxis1Y*angAxis2Z-angAxis1Z*angAxis2Y)+
  2082. ny*(angAxis1Z*angAxis2X-angAxis1X*angAxis2Z)+
  2083. nz*(angAxis1X*angAxis2Y-angAxis1Y*angAxis2X)<0
  2084. ){
  2085. this.rotationalLimitMotor.angle=-this.acosClamp(angAxis1X*angAxis2X+angAxis1Y*angAxis2Y+angAxis1Z*angAxis2Z);
  2086. }else{
  2087. this.rotationalLimitMotor.angle=this.acosClamp(angAxis1X*angAxis2X+angAxis1Y*angAxis2Y+angAxis1Z*angAxis2Z);
  2088. }
  2089. tmp1X=axis1Y*axis2Z-axis1Z*axis2Y;
  2090. tmp1Y=axis1Z*axis2X-axis1X*axis2Z;
  2091. tmp1Z=axis1X*axis2Y-axis1Y*axis2X;
  2092. this.r3.limitMotor2.angle=tx*tmp1X+ty*tmp1Y+tz*tmp1Z;
  2093. this.r3.limitMotor3.angle=bx*tmp1X+by*tmp1Y+bz*tmp1Z;
  2094. this.r3.preSolve(timeStep,invTimeStep);
  2095. this.t3.preSolve(timeStep,invTimeStep);
  2096. }
  2097. OIMO.SliderJoint.prototype.solve = function () {
  2098. this.r3.solve();
  2099. this.t3.solve();
  2100. }
  2101. OIMO.SliderJoint.prototype.postSolve = function () {
  2102. }
  2103. OIMO.SliderJoint.prototype.acosClamp = function(cos){
  2104. if(cos>1)return 0;
  2105. else if(cos<-1)return Math.PI;
  2106. else return Math.acos(cos);
  2107. }
  2108. OIMO.WheelJoint = function(config){
  2109. OIMO.Joint.call( this, config);
  2110. this.localAxis1=new OIMO.Vec3().normalize(config.localAxis1);
  2111. this.localAxis2=new OIMO.Vec3().normalize(config.localAxis2);
  2112. var len;
  2113. this.localAxis1X=this.localAxis1.x;
  2114. this.localAxis1Y=this.localAxis1.y;
  2115. this.localAxis1Z=this.localAxis1.z;
  2116. this.localAxis2X=this.localAxis2.x;
  2117. this.localAxis2Y=this.localAxis2.y;
  2118. this.localAxis2Z=this.localAxis2.z;
  2119. var dot=this.localAxis1X*this.localAxis2X+this.localAxis1Y*this.localAxis2Y+this.localAxis1Z*this.localAxis2Z;
  2120. if(dot>-1&&dot<1){
  2121. this.localAngAxis1X=this.localAxis2X-dot*this.localAxis1X;
  2122. this.localAngAxis1Y=this.localAxis2Y-dot*this.localAxis1Y;
  2123. this.localAngAxis1Z=this.localAxis2Z-dot*this.localAxis1Z;
  2124. this.localAngAxis2X=this.localAxis1X-dot*this.localAxis2X;
  2125. this.localAngAxis2Y=this.localAxis1Y-dot*this.localAxis2Y;
  2126. this.localAngAxis2Z=this.localAxis1Z-dot*this.localAxis2Z;
  2127. len=1/Math.sqrt(this.localAngAxis1X*this.localAngAxis1X+this.localAngAxis1Y*this.localAngAxis1Y+this.localAngAxis1Z*this.localAngAxis1Z);
  2128. this.localAngAxis1X*=len;
  2129. this.localAngAxis1Y*=len;
  2130. this.localAngAxis1Z*=len;
  2131. len=1/Math.sqrt(this.localAngAxis2X*this.localAngAxis2X+this.localAngAxis2Y*this.localAngAxis2Y+this.localAngAxis2Z*this.localAngAxis2Z);
  2132. this.localAngAxis2X*=len;
  2133. this.localAngAxis2Y*=len;
  2134. this.localAngAxis2Z*=len;
  2135. }else{
  2136. this.localAngAxis1X=this.localAxis1Y*this.localAxis1X-this.localAxis1Z*this.localAxis1Z;
  2137. this.localAngAxis1Y=-this.localAxis1Z*this.localAxis1Y-this.localAxis1X*this.localAxis1X;
  2138. this.localAngAxis1Z=this.localAxis1X*this.localAxis1Z+this.localAxis1Y*this.localAxis1Y;
  2139. len=1/Math.sqrt(this.localAngAxis1X*this.localAngAxis1X+this.localAngAxis1Y*this.localAngAxis1Y+this.localAngAxis1Z*this.localAngAxis1Z);
  2140. this.localAngAxis1X*=len;
  2141. this.localAngAxis1Y*=len;
  2142. this.localAngAxis1Z*=len;
  2143. var arc=new OIMO.Mat33().setQuat(new OIMO.Quat().arc(this.localAxis1,this.localAxis2));
  2144. var tarc = arc.elements;
  2145. this.localAngAxis2X=this.localAngAxis1X*tarc[0]+this.localAngAxis1Y*tarc[1]+this.localAngAxis1Z*tarc[2];
  2146. this.localAngAxis2Y=this.localAngAxis1X*tarc[3]+this.localAngAxis1Y*tarc[4]+this.localAngAxis1Z*tarc[5];
  2147. this.localAngAxis2Z=this.localAngAxis1X*tarc[6]+this.localAngAxis1Y*tarc[7]+this.localAngAxis1Z*tarc[8];
  2148. }
  2149. this.type=this.JOINT_WHEEL;
  2150. this.nor=new OIMO.Vec3();
  2151. this.tan=new OIMO.Vec3();
  2152. this.bin=new OIMO.Vec3();
  2153. this.translationalLimitMotor=new OIMO.LimitMotor(this.tan,true);
  2154. this.translationalLimitMotor.frequency=8;
  2155. this.translationalLimitMotor.dampingRatio=1;
  2156. this.rotationalLimitMotor1=new OIMO.LimitMotor(this.tan,false);
  2157. this.rotationalLimitMotor2=new OIMO.LimitMotor(this.bin,false);
  2158. this.t3=new OIMO.Translational3Constraint(this,new OIMO.LimitMotor(this.nor,true),this.translationalLimitMotor,new OIMO.LimitMotor(this.bin,true));
  2159. this.t3.weight=1;
  2160. this.r3=new OIMO.Rotational3Constraint(this,new OIMO.LimitMotor(this.nor,true),this.rotationalLimitMotor1,this.rotationalLimitMotor2);
  2161. }
  2162. OIMO.WheelJoint.prototype = Object.create( OIMO.Joint.prototype );
  2163. OIMO.WheelJoint.prototype.preSolve = function (timeStep,invTimeStep) {
  2164. var tmpM;
  2165. var tmp1X;
  2166. var tmp1Y;
  2167. var tmp1Z;
  2168. this.updateAnchorPoints();
  2169. tmpM=this.body1.rotation.elements;
  2170. var x1=this.localAxis1X*tmpM[0]+this.localAxis1Y*tmpM[1]+this.localAxis1Z*tmpM[2];
  2171. var y1=this.localAxis1X*tmpM[3]+this.localAxis1Y*tmpM[4]+this.localAxis1Z*tmpM[5];
  2172. var z1=this.localAxis1X*tmpM[6]+this.localAxis1Y*tmpM[7]+this.localAxis1Z*tmpM[8];
  2173. var angAxis1X=this.localAngAxis1X*tmpM[0]+this.localAngAxis1Y*tmpM[1]+this.localAngAxis1Z*tmpM[2];
  2174. var angAxis1Y=this.localAngAxis1X*tmpM[3]+this.localAngAxis1Y*tmpM[4]+this.localAngAxis1Z*tmpM[5];
  2175. var angAxis1Z=this.localAngAxis1X*tmpM[6]+this.localAngAxis1Y*tmpM[7]+this.localAngAxis1Z*tmpM[8];
  2176. tmpM=this.body2.rotation.elements;
  2177. var x2=this.localAxis2X*tmpM[0]+this.localAxis2Y*tmpM[1]+this.localAxis2Z*tmpM[2];
  2178. var y2=this.localAxis2X*tmpM[3]+this.localAxis2Y*tmpM[4]+this.localAxis2Z*tmpM[5];
  2179. var z2=this.localAxis2X*tmpM[6]+this.localAxis2Y*tmpM[7]+this.localAxis2Z*tmpM[8];
  2180. var angAxis2X=this.localAngAxis2X*tmpM[0]+this.localAngAxis2Y*tmpM[1]+this.localAngAxis2Z*tmpM[2];
  2181. var angAxis2Y=this.localAngAxis2X*tmpM[3]+this.localAngAxis2Y*tmpM[4]+this.localAngAxis2Z*tmpM[5];
  2182. var angAxis2Z=this.localAngAxis2X*tmpM[6]+this.localAngAxis2Y*tmpM[7]+this.localAngAxis2Z*tmpM[8];
  2183. this.r3.limitMotor1.angle=x1*x2+y1*y2+z1*z2;
  2184. if(
  2185. x1*(angAxis1Y*z2-angAxis1Z*y2)+
  2186. y1*(angAxis1Z*x2-angAxis1X*z2)+
  2187. z1*(angAxis1X*y2-angAxis1Y*x2)<0
  2188. ){
  2189. this.rotationalLimitMotor1.angle=-this.acosClamp(angAxis1X*x2+angAxis1Y*y2+angAxis1Z*z2);
  2190. }else{
  2191. this.rotationalLimitMotor1.angle=this.acosClamp(angAxis1X*x2+angAxis1Y*y2+angAxis1Z*z2);
  2192. }
  2193. if(
  2194. x2*(angAxis2Y*z1-angAxis2Z*y1)+
  2195. y2*(angAxis2Z*x1-angAxis2X*z1)+
  2196. z2*(angAxis2X*y1-angAxis2Y*x1)<0
  2197. ){
  2198. this.rotationalLimitMotor2.angle=this.acosClamp(angAxis2X*x1+angAxis2Y*y1+angAxis2Z*z1);
  2199. }else{
  2200. this.rotationalLimitMotor2.angle=-this.acosClamp(angAxis2X*x1+angAxis2Y*y1+angAxis2Z*z1);
  2201. }
  2202. var nx=y2*z1-z2*y1;
  2203. var ny=z2*x1-x2*z1;
  2204. var nz=x2*y1-y2*x1;
  2205. tmp1X=Math.sqrt(nx*nx+ny*ny+nz*nz);
  2206. if(tmp1X>0)tmp1X=1/tmp1X;
  2207. nx*=tmp1X;
  2208. ny*=tmp1X;
  2209. nz*=tmp1X;
  2210. var tx=ny*z2-nz*y2;
  2211. var ty=nz*x2-nx*z2;
  2212. var tz=nx*y2-ny*x2;
  2213. tmp1X=Math.sqrt(tx*tx+ty*ty+tz*tz);
  2214. if(tmp1X>0)tmp1X=1/tmp1X;
  2215. tx*=tmp1X;
  2216. ty*=tmp1X;
  2217. tz*=tmp1X;
  2218. var bx=y1*nz-z1*ny;
  2219. var by=z1*nx-x1*nz;
  2220. var bz=x1*ny-y1*nx;
  2221. tmp1X=Math.sqrt(bx*bx+by*by+bz*bz);
  2222. if(tmp1X>0)tmp1X=1/tmp1X;
  2223. bx*=tmp1X;
  2224. by*=tmp1X;
  2225. bz*=tmp1X;
  2226. this.nor.init(nx,ny,nz);
  2227. this.tan.init(tx,ty,tz);
  2228. this.bin.init(bx,by,bz);
  2229. this.r3.preSolve(timeStep,invTimeStep);
  2230. this.t3.preSolve(timeStep,invTimeStep);
  2231. }
  2232. OIMO.WheelJoint.prototype.solve = function () {
  2233. this.r3.solve();
  2234. this.t3.solve();
  2235. }
  2236. OIMO.WheelJoint.prototype.postSolve = function () {
  2237. }
  2238. OIMO.WheelJoint.prototype.acosClamp = function(cos){
  2239. if(cos>1)return 0;
  2240. else if(cos<-1)return Math.PI;
  2241. else return Math.acos(cos);
  2242. }
  2243. OIMO.AngularConstraint = function(joint,targetOrientation){
  2244. this.i1e00=NaN;
  2245. this.i1e01=NaN;
  2246. this.i1e02=NaN;
  2247. this.i1e10=NaN;
  2248. this.i1e11=NaN;
  2249. this.i1e12=NaN;
  2250. this.i1e20=NaN;
  2251. this.i1e21=NaN;
  2252. this.i1e22=NaN;
  2253. this.i2e00=NaN;
  2254. this.i2e01=NaN;
  2255. this.i2e02=NaN;
  2256. this.i2e10=NaN;
  2257. this.i2e11=NaN;
  2258. this.i2e12=NaN;
  2259. this.i2e20=NaN;
  2260. this.i2e21=NaN;
  2261. this.i2e22=NaN;
  2262. this.d00=NaN;
  2263. this.d01=NaN;
  2264. this.d02=NaN;
  2265. this.d10=NaN;
  2266. this.d11=NaN;
  2267. this.d12=NaN;
  2268. this.d20=NaN;
  2269. this.d21=NaN;
  2270. this.d22=NaN;
  2271. this.ax=NaN;
  2272. this.ay=NaN;
  2273. this.az=NaN;
  2274. this.velx=NaN;
  2275. this.vely=NaN;
  2276. this.velz=NaN;
  2277. this.joint=joint;
  2278. this.targetOrientation=new OIMO.Quat().invert(targetOrientation);
  2279. this.relativeOrientation=new OIMO.Quat();
  2280. this.b1=joint.body1;
  2281. this.b2=joint.body2;
  2282. this.a1=this.b1.angularVelocity;
  2283. this.a2=this.b2.angularVelocity;
  2284. this.i1=this.b1.inverseInertia;
  2285. this.i2=this.b2.inverseInertia;
  2286. this.impx=0;
  2287. this.impy=0;
  2288. this.impz=0;
  2289. }
  2290. OIMO.AngularConstraint.prototype = {
  2291. constructor: OIMO.AngularConstraint,
  2292. preSolve:function(timeStep,invTimeStep){
  2293. var ti1 = this.i1.elements;
  2294. var ti2 = this.i2.elements;
  2295. this.i1e00=ti1[0];
  2296. this.i1e01=ti1[1];
  2297. this.i1e02=ti1[2];
  2298. this.i1e10=ti1[3];
  2299. this.i1e11=ti1[4];
  2300. this.i1e12=ti1[5];
  2301. this.i1e20=ti1[6];
  2302. this.i1e21=ti1[7];
  2303. this.i1e22=ti1[8];
  2304. this.i2e00=ti2[0];
  2305. this.i2e01=ti2[1];
  2306. this.i2e02=ti2[2];
  2307. this.i2e10=ti2[3];
  2308. this.i2e11=ti2[4];
  2309. this.i2e12=ti2[5];
  2310. this.i2e20=ti2[6];
  2311. this.i2e21=ti2[7];
  2312. this.i2e22=ti2[8];
  2313. var v00=this.i1e00+this.i2e00;
  2314. var v01=this.i1e01+this.i2e01;
  2315. var v02=this.i1e02+this.i2e02;
  2316. var v10=this.i1e10+this.i2e10;
  2317. var v11=this.i1e11+this.i2e11;
  2318. var v12=this.i1e12+this.i2e12;
  2319. var v20=this.i1e20+this.i2e20;
  2320. var v21=this.i1e21+this.i2e21;
  2321. var v22=this.i1e22+this.i2e22;
  2322. var inv=1/(
  2323. v00*(v11*v22-v21*v12)+
  2324. v10*(v21*v02-v01*v22)+
  2325. v20*(v01*v12-v11*v02)
  2326. );
  2327. this.d00=(v11*v22-v12*v21)*inv;
  2328. this.d01=(v02*v21-v01*v22)*inv;
  2329. this.d02=(v01*v12-v02*v11)*inv;
  2330. this.d10=(v12*v20-v10*v22)*inv;
  2331. this.d11=(v00*v22-v02*v20)*inv;
  2332. this.d12=(v02*v10-v00*v12)*inv;
  2333. this.d20=(v10*v21-v11*v20)*inv;
  2334. this.d21=(v01*v20-v00*v21)*inv;
  2335. this.d22=(v00*v11-v01*v10)*inv;
  2336. this.relativeOrientation.invert(this.b1.orientation);
  2337. this.relativeOrientation.mul(this.targetOrientation,this.relativeOrientation);
  2338. this.relativeOrientation.mul(this.b2.orientation,this.relativeOrientation);
  2339. inv=this.relativeOrientation.s*2;
  2340. this.velx=this.relativeOrientation.x*inv;
  2341. this.vely=this.relativeOrientation.y*inv;
  2342. this.velz=this.relativeOrientation.z*inv;
  2343. var len=Math.sqrt(this.velx*this.velx+this.vely*this.vely+this.velz*this.velz);
  2344. if(len>0.02){
  2345. len=(0.02-len)/len*invTimeStep*0.05;
  2346. this.velx*=len;
  2347. this.vely*=len;
  2348. this.velz*=len;
  2349. }else{
  2350. this.velx=0;
  2351. this.vely=0;
  2352. this.velz=0;
  2353. }
  2354. this.a1.x+=this.impx*this.i1e00+this.impy*this.i1e01+this.impz*this.i1e02;
  2355. this.a1.y+=this.impx*this.i1e10+this.impy*this.i1e11+this.impz*this.i1e12;
  2356. this.a1.z+=this.impx*this.i1e20+this.impy*this.i1e21+this.impz*this.i1e22;
  2357. this.a2.x-=this.impx*this.i2e00+this.impy*this.i2e01+this.impz*this.i2e02;
  2358. this.a2.y-=this.impx*this.i2e10+this.impy*this.i2e11+this.impz*this.i2e12;
  2359. this.a2.z-=this.impx*this.i2e20+this.impy*this.i2e21+this.impz*this.i2e22;
  2360. },
  2361. solve:function(){
  2362. var rvx=this.a2.x-this.a1.x-this.velx;
  2363. var rvy=this.a2.y-this.a1.y-this.vely;
  2364. var rvz=this.a2.z-this.a1.z-this.velz;
  2365. var nimpx=rvx*this.d00+rvy*this.d01+rvz*this.d02;
  2366. var nimpy=rvx*this.d10+rvy*this.d11+rvz*this.d12;
  2367. var nimpz=rvx*this.d20+rvy*this.d21+rvz*this.d22;
  2368. this.impx+=nimpx;
  2369. this.impy+=nimpy;
  2370. this.impz+=nimpz;
  2371. this.a1.x+=nimpx*this.i1e00+nimpy*this.i1e01+nimpz*this.i1e02;
  2372. this.a1.y+=nimpx*this.i1e10+nimpy*this.i1e11+nimpz*this.i1e12;
  2373. this.a1.z+=nimpx*this.i1e20+nimpy*this.i1e21+nimpz*this.i1e22;
  2374. this.a2.x-=nimpx*this.i2e00+nimpy*this.i2e01+nimpz*this.i2e02;
  2375. this.a2.y-=nimpx*this.i2e10+nimpy*this.i2e11+nimpz*this.i2e12;
  2376. this.a2.z-=nimpx*this.i2e20+nimpy*this.i2e21+nimpz*this.i2e22;
  2377. }
  2378. }
  2379. OIMO.LinearConstraint = function(joint){
  2380. this.m1=NaN;
  2381. this.m2=NaN;
  2382. this.i1e00=NaN;
  2383. this.i1e01=NaN;
  2384. this.i1e02=NaN;
  2385. this.i1e10=NaN;
  2386. this.i1e11=NaN;
  2387. this.i1e12=NaN;
  2388. this.i1e20=NaN;
  2389. this.i1e21=NaN;
  2390. this.i1e22=NaN;
  2391. this.i2e00=NaN;
  2392. this.i2e01=NaN;
  2393. this.i2e02=NaN;
  2394. this.i2e10=NaN;
  2395. this.i2e11=NaN;
  2396. this.i2e12=NaN;
  2397. this.i2e20=NaN;
  2398. this.i2e21=NaN;
  2399. this.i2e22=NaN;
  2400. this.d00=NaN;
  2401. this.d01=NaN;
  2402. this.d02=NaN;
  2403. this.d10=NaN;
  2404. this.d11=NaN;
  2405. this.d12=NaN;
  2406. this.d20=NaN;
  2407. this.d21=NaN;
  2408. this.d22=NaN;
  2409. this.r1x=NaN;
  2410. this.r1y=NaN;
  2411. this.r1z=NaN;
  2412. this.r2x=NaN;
  2413. this.r2y=NaN;
  2414. this.r2z=NaN;
  2415. this.ax1x=NaN;
  2416. this.ax1y=NaN;
  2417. this.ax1z=NaN;
  2418. this.ay1x=NaN;
  2419. this.ay1y=NaN;
  2420. this.ay1z=NaN;
  2421. this.az1x=NaN;
  2422. this.az1y=NaN;
  2423. this.az1z=NaN;
  2424. this.ax2x=NaN;
  2425. this.ax2y=NaN;
  2426. this.ax2z=NaN;
  2427. this.ay2x=NaN;
  2428. this.ay2y=NaN;
  2429. this.ay2z=NaN;
  2430. this.az2x=NaN;
  2431. this.az2y=NaN;
  2432. this.az2z=NaN;
  2433. this.vel=NaN;
  2434. this.velx=NaN;
  2435. this.vely=NaN;
  2436. this.velz=NaN;
  2437. this.joint=joint;
  2438. this.r1=joint.relativeAnchorPoint1;
  2439. this.r2=joint.relativeAnchorPoint2;
  2440. this.p1=joint.anchorPoint1;
  2441. this.p2=joint.anchorPoint2;
  2442. this.b1=joint.body1;
  2443. this.b2=joint.body2;
  2444. this.l1=this.b1.linearVelocity;
  2445. this.l2=this.b2.linearVelocity;
  2446. this.a1=this.b1.angularVelocity;
  2447. this.a2=this.b2.angularVelocity;
  2448. this.i1=this.b1.inverseInertia;
  2449. this.i2=this.b2.inverseInertia;
  2450. this.impx=0;
  2451. this.impy=0;
  2452. this.impz=0;
  2453. }
  2454. OIMO.LinearConstraint.prototype = {
  2455. constructor: OIMO.LinearConstraint,
  2456. preSolve:function(timeStep,invTimeStep){
  2457. this.r1x=this.r1.x;
  2458. this.r1y=this.r1.y;
  2459. this.r1z=this.r1.z;
  2460. this.r2x=this.r2.x;
  2461. this.r2y=this.r2.y;
  2462. this.r2z=this.r2.z;
  2463. this.m1=this.b1.inverseMass;
  2464. this.m2=this.b2.inverseMass;
  2465. var ti1 = this.i1.elements;
  2466. var ti2 = this.i2.elements;
  2467. this.i1e00=ti1[0];
  2468. this.i1e01=ti1[1];
  2469. this.i1e02=ti1[2];
  2470. this.i1e10=ti1[3];
  2471. this.i1e11=ti1[4];
  2472. this.i1e12=ti1[5];
  2473. this.i1e20=ti1[6];
  2474. this.i1e21=ti1[7];
  2475. this.i1e22=ti1[8];
  2476. this.i2e00=ti2[0];
  2477. this.i2e01=ti2[1];
  2478. this.i2e02=ti2[2];
  2479. this.i2e10=ti2[3];
  2480. this.i2e11=ti2[4];
  2481. this.i2e12=ti2[5];
  2482. this.i2e20=ti2[6];
  2483. this.i2e21=ti2[7];
  2484. this.i2e22=ti2[8];
  2485. this.ax1x=this.r1z*this.i1e01+-this.r1y*this.i1e02;
  2486. this.ax1y=this.r1z*this.i1e11+-this.r1y*this.i1e12;
  2487. this.ax1z=this.r1z*this.i1e21+-this.r1y*this.i1e22;
  2488. this.ay1x=-this.r1z*this.i1e00+this.r1x*this.i1e02;
  2489. this.ay1y=-this.r1z*this.i1e10+this.r1x*this.i1e12;
  2490. this.ay1z=-this.r1z*this.i1e20+this.r1x*this.i1e22;
  2491. this.az1x=this.r1y*this.i1e00+-this.r1x*this.i1e01;
  2492. this.az1y=this.r1y*this.i1e10+-this.r1x*this.i1e11;
  2493. this.az1z=this.r1y*this.i1e20+-this.r1x*this.i1e21;
  2494. this.ax2x=this.r2z*this.i2e01+-this.r2y*this.i2e02;
  2495. this.ax2y=this.r2z*this.i2e11+-this.r2y*this.i2e12;
  2496. this.ax2z=this.r2z*this.i2e21+-this.r2y*this.i2e22;
  2497. this.ay2x=-this.r2z*this.i2e00+this.r2x*this.i2e02;
  2498. this.ay2y=-this.r2z*this.i2e10+this.r2x*this.i2e12;
  2499. this.ay2z=-this.r2z*this.i2e20+this.r2x*this.i2e22;
  2500. this.az2x=this.r2y*this.i2e00+-this.r2x*this.i2e01;
  2501. this.az2y=this.r2y*this.i2e10+-this.r2x*this.i2e11;
  2502. this.az2z=this.r2y*this.i2e20+-this.r2x*this.i2e21;
  2503. var k00=this.m1+this.m2;
  2504. var k01=0;
  2505. var k02=0;
  2506. var k10=0;
  2507. var k11=k00;
  2508. var k12=0;
  2509. var k20=0;
  2510. var k21=0;
  2511. var k22=k00;
  2512. k00+=this.i1e11*this.r1z*this.r1z-(this.i1e21+this.i1e12)*this.r1y*this.r1z+this.i1e22*this.r1y*this.r1y;
  2513. k01+=(this.i1e20*this.r1y+this.i1e12*this.r1x)*this.r1z-this.i1e10*this.r1z*this.r1z-this.i1e22*this.r1x*this.r1y;
  2514. k02+=(this.i1e10*this.r1y-this.i1e11*this.r1x)*this.r1z-this.i1e20*this.r1y*this.r1y+this.i1e21*this.r1x*this.r1y;
  2515. k10+=(this.i1e02*this.r1y+this.i1e21*this.r1x)*this.r1z-this.i1e01*this.r1z*this.r1z-this.i1e22*this.r1x*this.r1y;
  2516. k11+=this.i1e00*this.r1z*this.r1z-(this.i1e20+this.i1e02)*this.r1x*this.r1z+this.i1e22*this.r1x*this.r1x;
  2517. k12+=(this.i1e01*this.r1x-this.i1e00*this.r1y)*this.r1z-this.i1e21*this.r1x*this.r1x+this.i1e20*this.r1x*this.r1y;
  2518. k20+=(this.i1e01*this.r1y-this.i1e11*this.r1x)*this.r1z-this.i1e02*this.r1y*this.r1y+this.i1e12*this.r1x*this.r1y;
  2519. k21+=(this.i1e10*this.r1x-this.i1e00*this.r1y)*this.r1z-this.i1e12*this.r1x*this.r1x+this.i1e02*this.r1x*this.r1y;
  2520. k22+=this.i1e00*this.r1y*this.r1y-(this.i1e10+this.i1e01)*this.r1x*this.r1y+this.i1e11*this.r1x*this.r1x;
  2521. k00+=this.i2e11*this.r2z*this.r2z-(this.i2e21+this.i2e12)*this.r2y*this.r2z+this.i2e22*this.r2y*this.r2y;
  2522. k01+=(this.i2e20*this.r2y+this.i2e12*this.r2x)*this.r2z-this.i2e10*this.r2z*this.r2z-this.i2e22*this.r2x*this.r2y;
  2523. k02+=(this.i2e10*this.r2y-this.i2e11*this.r2x)*this.r2z-this.i2e20*this.r2y*this.r2y+this.i2e21*this.r2x*this.r2y;
  2524. k10+=(this.i2e02*this.r2y+this.i2e21*this.r2x)*this.r2z-this.i2e01*this.r2z*this.r2z-this.i2e22*this.r2x*this.r2y;
  2525. k11+=this.i2e00*this.r2z*this.r2z-(this.i2e20+this.i2e02)*this.r2x*this.r2z+this.i2e22*this.r2x*this.r2x;
  2526. k12+=(this.i2e01*this.r2x-this.i2e00*this.r2y)*this.r2z-this.i2e21*this.r2x*this.r2x+this.i2e20*this.r2x*this.r2y;
  2527. k20+=(this.i2e01*this.r2y-this.i2e11*this.r2x)*this.r2z-this.i2e02*this.r2y*this.r2y+this.i2e12*this.r2x*this.r2y;
  2528. k21+=(this.i2e10*this.r2x-this.i2e00*this.r2y)*this.r2z-this.i2e12*this.r2x*this.r2x+this.i2e02*this.r2x*this.r2y;
  2529. k22+=this.i2e00*this.r2y*this.r2y-(this.i2e10+this.i2e01)*this.r2x*this.r2y+this.i2e11*this.r2x*this.r2x;
  2530. var inv=1/(
  2531. k00*(k11*k22-k21*k12)+
  2532. k10*(k21*k02-k01*k22)+
  2533. k20*(k01*k12-k11*k02)
  2534. );
  2535. this.d00=(k11*k22-k12*k21)*inv;
  2536. this.d01=(k02*k21-k01*k22)*inv;
  2537. this.d02=(k01*k12-k02*k11)*inv;
  2538. this.d10=(k12*k20-k10*k22)*inv;
  2539. this.d11=(k00*k22-k02*k20)*inv;
  2540. this.d12=(k02*k10-k00*k12)*inv;
  2541. this.d20=(k10*k21-k11*k20)*inv;
  2542. this.d21=(k01*k20-k00*k21)*inv;
  2543. this.d22=(k00*k11-k01*k10)*inv;
  2544. this.velx=this.p2.x-this.p1.x;
  2545. this.vely=this.p2.y-this.p1.y;
  2546. this.velz=this.p2.z-this.p1.z;
  2547. var len=Math.sqrt(this.velx*this.velx+this.vely*this.vely+this.velz*this.velz);
  2548. if(len>0.005){
  2549. len=(0.005-len)/len*invTimeStep*0.05;
  2550. this.velx*=len;
  2551. this.vely*=len;
  2552. this.velz*=len;
  2553. }else{
  2554. this.velx=0;
  2555. this.vely=0;
  2556. this.velz=0;
  2557. }
  2558. this.impx*=0.95;
  2559. this.impy*=0.95;
  2560. this.impz*=0.95;
  2561. this.l1.x+=this.impx*this.m1;
  2562. this.l1.y+=this.impy*this.m1;
  2563. this.l1.z+=this.impz*this.m1;
  2564. this.a1.x+=this.impx*this.ax1x+this.impy*this.ay1x+this.impz*this.az1x;
  2565. this.a1.y+=this.impx*this.ax1y+this.impy*this.ay1y+this.impz*this.az1y;
  2566. this.a1.z+=this.impx*this.ax1z+this.impy*this.ay1z+this.impz*this.az1z;
  2567. this.l2.x-=this.impx*this.m2;
  2568. this.l2.y-=this.impy*this.m2;
  2569. this.l2.z-=this.impz*this.m2;
  2570. this.a2.x-=this.impx*this.ax2x+this.impy*this.ay2x+this.impz*this.az2x;
  2571. this.a2.y-=this.impx*this.ax2y+this.impy*this.ay2y+this.impz*this.az2y;
  2572. this.a2.z-=this.impx*this.ax2z+this.impy*this.ay2z+this.impz*this.az2z;
  2573. },
  2574. solve:function(){
  2575. 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;
  2576. 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;
  2577. 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;
  2578. var nimpx=rvx*this.d00+rvy*this.d01+rvz*this.d02;
  2579. var nimpy=rvx*this.d10+rvy*this.d11+rvz*this.d12;
  2580. var nimpz=rvx*this.d20+rvy*this.d21+rvz*this.d22;
  2581. this.impx+=nimpx;
  2582. this.impy+=nimpy;
  2583. this.impz+=nimpz;
  2584. this.l1.x+=nimpx*this.m1;
  2585. this.l1.y+=nimpy*this.m1;
  2586. this.l1.z+=nimpz*this.m1;
  2587. this.a1.x+=nimpx*this.ax1x+nimpy*this.ay1x+nimpz*this.az1x;
  2588. this.a1.y+=nimpx*this.ax1y+nimpy*this.ay1y+nimpz*this.az1y;
  2589. this.a1.z+=nimpx*this.ax1z+nimpy*this.ay1z+nimpz*this.az1z;
  2590. this.l2.x-=nimpx*this.m2;
  2591. this.l2.y-=nimpy*this.m2;
  2592. this.l2.z-=nimpz*this.m2;
  2593. this.a2.x-=nimpx*this.ax2x+nimpy*this.ay2x+nimpz*this.az2x;
  2594. this.a2.y-=nimpx*this.ax2y+nimpy*this.ay2y+nimpz*this.az2y;
  2595. this.a2.z-=nimpx*this.ax2z+nimpy*this.ay2z+nimpz*this.az2z;
  2596. }
  2597. }
  2598. OIMO.Rotational3Constraint = function(joint,limitMotor1,limitMotor2,limitMotor3){
  2599. this.cfm1=NaN;
  2600. this.cfm2=NaN;
  2601. this.cfm3=NaN;
  2602. this.i1e00=NaN;
  2603. this.i1e01=NaN;
  2604. this.i1e02=NaN;
  2605. this.i1e10=NaN;
  2606. this.i1e11=NaN;
  2607. this.i1e12=NaN;
  2608. this.i1e20=NaN;
  2609. this.i1e21=NaN;
  2610. this.i1e22=NaN;
  2611. this.i2e00=NaN;
  2612. this.i2e01=NaN;
  2613. this.i2e02=NaN;
  2614. this.i2e10=NaN;
  2615. this.i2e11=NaN;
  2616. this.i2e12=NaN;
  2617. this.i2e20=NaN;
  2618. this.i2e21=NaN;
  2619. this.i2e22=NaN;
  2620. this.ax1=NaN;
  2621. this.ay1=NaN;
  2622. this.az1=NaN;
  2623. this.ax2=NaN;
  2624. this.ay2=NaN;
  2625. this.az2=NaN;
  2626. this.ax3=NaN;
  2627. this.ay3=NaN;
  2628. this.az3=NaN;
  2629. this.a1x1=NaN;
  2630. this.a1y1=NaN;
  2631. this.a1z1=NaN;
  2632. this.a2x1=NaN;
  2633. this.a2y1=NaN;
  2634. this.a2z1=NaN;
  2635. this.a1x2=NaN;
  2636. this.a1y2=NaN;
  2637. this.a1z2=NaN;
  2638. this.a2x2=NaN;
  2639. this.a2y2=NaN;
  2640. this.a2z2=NaN;
  2641. this.a1x3=NaN;
  2642. this.a1y3=NaN;
  2643. this.a1z3=NaN;
  2644. this.a2x3=NaN;
  2645. this.a2y3=NaN;
  2646. this.a2z3=NaN;
  2647. this.lowerLimit1=NaN;
  2648. this.upperLimit1=NaN;
  2649. this.limitVelocity1=NaN;
  2650. this.limitState1=0;
  2651. this.enableMotor1=false;
  2652. this.motorSpeed1=NaN;
  2653. this.maxMotorForce1=NaN;
  2654. this.maxMotorImpulse1=NaN;
  2655. this.lowerLimit2=NaN;
  2656. this.upperLimit2=NaN;
  2657. this.limitVelocity2=NaN;
  2658. this.limitState2=0;
  2659. this.enableMotor2=false;
  2660. this.motorSpeed2=NaN;
  2661. this.maxMotorForce2=NaN;
  2662. this.maxMotorImpulse2=NaN;
  2663. this.lowerLimit3=NaN;
  2664. this.upperLimit3=NaN;
  2665. this.limitVelocity3=NaN;
  2666. this.limitState3=0;
  2667. this.enableMotor3=false;
  2668. this.motorSpeed3=NaN;
  2669. this.maxMotorForce3=NaN;
  2670. this.maxMotorImpulse3=NaN;
  2671. this.k00=NaN;
  2672. this.k01=NaN;
  2673. this.k02=NaN;
  2674. this.k10=NaN;
  2675. this.k11=NaN;
  2676. this.k12=NaN;
  2677. this.k20=NaN;
  2678. this.k21=NaN;
  2679. this.k22=NaN;
  2680. this.kv00=NaN;
  2681. this.kv11=NaN;
  2682. this.kv22=NaN;
  2683. this.dv00=NaN;
  2684. this.dv11=NaN;
  2685. this.dv22=NaN;
  2686. this.d00=NaN;
  2687. this.d01=NaN;
  2688. this.d02=NaN;
  2689. this.d10=NaN;
  2690. this.d11=NaN;
  2691. this.d12=NaN;
  2692. this.d20=NaN;
  2693. this.d21=NaN;
  2694. this.d22=NaN;
  2695. this.limitMotor1=limitMotor1;
  2696. this.limitMotor2=limitMotor2;
  2697. this.limitMotor3=limitMotor3;
  2698. this.b1=joint.body1;
  2699. this.b2=joint.body2;
  2700. this.a1=this.b1.angularVelocity;
  2701. this.a2=this.b2.angularVelocity;
  2702. this.i1=this.b1.inverseInertia;
  2703. this.i2=this.b2.inverseInertia;
  2704. this.limitImpulse1=0;
  2705. this.motorImpulse1=0;
  2706. this.limitImpulse2=0;
  2707. this.motorImpulse2=0;
  2708. this.limitImpulse3=0;
  2709. this.motorImpulse3=0;
  2710. }
  2711. OIMO.Rotational3Constraint.prototype = {
  2712. constructor: OIMO.Rotational3Constraint,
  2713. preSolve:function(timeStep,invTimeStep){
  2714. this.ax1=this.limitMotor1.axis.x;
  2715. this.ay1=this.limitMotor1.axis.y;
  2716. this.az1=this.limitMotor1.axis.z;
  2717. this.ax2=this.limitMotor2.axis.x;
  2718. this.ay2=this.limitMotor2.axis.y;
  2719. this.az2=this.limitMotor2.axis.z;
  2720. this.ax3=this.limitMotor3.axis.x;
  2721. this.ay3=this.limitMotor3.axis.y;
  2722. this.az3=this.limitMotor3.axis.z;
  2723. this.lowerLimit1=this.limitMotor1.lowerLimit;
  2724. this.upperLimit1=this.limitMotor1.upperLimit;
  2725. this.motorSpeed1=this.limitMotor1.motorSpeed;
  2726. this.maxMotorForce1=this.limitMotor1.maxMotorForce;
  2727. this.enableMotor1=this.maxMotorForce1>0;
  2728. this.lowerLimit2=this.limitMotor2.lowerLimit;
  2729. this.upperLimit2=this.limitMotor2.upperLimit;
  2730. this.motorSpeed2=this.limitMotor2.motorSpeed;
  2731. this.maxMotorForce2=this.limitMotor2.maxMotorForce;
  2732. this.enableMotor2=this.maxMotorForce2>0;
  2733. this.lowerLimit3=this.limitMotor3.lowerLimit;
  2734. this.upperLimit3=this.limitMotor3.upperLimit;
  2735. this.motorSpeed3=this.limitMotor3.motorSpeed;
  2736. this.maxMotorForce3=this.limitMotor3.maxMotorForce;
  2737. this.enableMotor3=this.maxMotorForce3>0;
  2738. var ti1 = this.i1.elements;
  2739. var ti2 = this.i2.elements;
  2740. this.i1e00=ti1[0];
  2741. this.i1e01=ti1[1];
  2742. this.i1e02=ti1[2];
  2743. this.i1e10=ti1[3];
  2744. this.i1e11=ti1[4];
  2745. this.i1e12=ti1[5];
  2746. this.i1e20=ti1[6];
  2747. this.i1e21=ti1[7];
  2748. this.i1e22=ti1[8];
  2749. this.i2e00=ti2[0];
  2750. this.i2e01=ti2[1];
  2751. this.i2e02=ti2[2];
  2752. this.i2e10=ti2[3];
  2753. this.i2e11=ti2[4];
  2754. this.i2e12=ti2[5];
  2755. this.i2e20=ti2[6];
  2756. this.i2e21=ti2[7];
  2757. this.i2e22=ti2[8];
  2758. var frequency1=this.limitMotor1.frequency;
  2759. var frequency2=this.limitMotor2.frequency;
  2760. var frequency3=this.limitMotor3.frequency;
  2761. var enableSpring1=frequency1>0;
  2762. var enableSpring2=frequency2>0;
  2763. var enableSpring3=frequency3>0;
  2764. var enableLimit1=this.lowerLimit1<=this.upperLimit1;
  2765. var enableLimit2=this.lowerLimit2<=this.upperLimit2;
  2766. var enableLimit3=this.lowerLimit3<=this.upperLimit3;
  2767. var angle1=this.limitMotor1.angle;
  2768. if(enableLimit1){
  2769. if(this.lowerLimit1==this.upperLimit1){
  2770. if(this.limitState1!=0){
  2771. this.limitState1=0;
  2772. this.limitImpulse1=0;
  2773. }
  2774. this.limitVelocity1=this.lowerLimit1-angle1;
  2775. }else if(angle1<this.lowerLimit1){
  2776. if(this.limitState1!=-1){
  2777. this.limitState1=-1;
  2778. this.limitImpulse1=0;
  2779. }
  2780. this.limitVelocity1=this.lowerLimit1-angle1;
  2781. }else if(angle1>this.upperLimit1){
  2782. if(this.limitState1!=1){
  2783. this.limitState1=1;
  2784. this.limitImpulse1=0;
  2785. }
  2786. this.limitVelocity1=this.upperLimit1-angle1;
  2787. }else{
  2788. this.limitState1=2;
  2789. this.limitImpulse1=0;
  2790. this.limitVelocity1=0;
  2791. }
  2792. if(!enableSpring1){
  2793. if(this.limitVelocity1>0.02)this.limitVelocity1-=0.02;
  2794. else if(this.limitVelocity1<-0.02)this.limitVelocity1+=0.02;
  2795. else this.limitVelocity1=0;
  2796. }
  2797. }else{
  2798. this.limitState1=2;
  2799. this.limitImpulse1=0;
  2800. }
  2801. var angle2=this.limitMotor2.angle;
  2802. if(enableLimit2){
  2803. if(this.lowerLimit2==this.upperLimit2){
  2804. if(this.limitState2!=0){
  2805. this.limitState2=0;
  2806. this.limitImpulse2=0;
  2807. }
  2808. this.limitVelocity2=this.lowerLimit2-angle2;
  2809. }else if(angle2<this.lowerLimit2){
  2810. if(this.limitState2!=-1){
  2811. this.limitState2=-1;
  2812. this.limitImpulse2=0;
  2813. }
  2814. this.limitVelocity2=this.lowerLimit2-angle2;
  2815. }else if(angle2>this.upperLimit2){
  2816. if(this.limitState2!=1){
  2817. this.limitState2=1;
  2818. this.limitImpulse2=0;
  2819. }
  2820. this.limitVelocity2=this.upperLimit2-angle2;
  2821. }else{
  2822. this.limitState2=2;
  2823. this.limitImpulse2=0;
  2824. this.limitVelocity2=0;
  2825. }
  2826. if(!enableSpring2){
  2827. if(this.limitVelocity2>0.02)this.limitVelocity2-=0.02;
  2828. else if(this.limitVelocity2<-0.02)this.limitVelocity2+=0.02;
  2829. else this.limitVelocity2=0;
  2830. }
  2831. }else{
  2832. this.limitState2=2;
  2833. this.limitImpulse2=0;
  2834. }
  2835. var angle3=this.limitMotor3.angle;
  2836. if(enableLimit3){
  2837. if(this.lowerLimit3==this.upperLimit3){
  2838. if(this.limitState3!=0){
  2839. this.limitState3=0;
  2840. this.limitImpulse3=0;
  2841. }
  2842. this.limitVelocity3=this.lowerLimit3-angle3;
  2843. }else if(angle3<this.lowerLimit3){
  2844. if(this.limitState3!=-1){
  2845. this.limitState3=-1;
  2846. this.limitImpulse3=0;
  2847. }
  2848. this.limitVelocity3=this.lowerLimit3-angle3;
  2849. }else if(angle3>this.upperLimit3){
  2850. if(this.limitState3!=1){
  2851. this.limitState3=1;
  2852. this.limitImpulse3=0;
  2853. }
  2854. this.limitVelocity3=this.upperLimit3-angle3;
  2855. }else{
  2856. this.limitState3=2;
  2857. this.limitImpulse3=0;
  2858. this.limitVelocity3=0;
  2859. }
  2860. if(!enableSpring3){
  2861. if(this.limitVelocity3>0.02)this.limitVelocity3-=0.02;
  2862. else if(this.limitVelocity3<-0.02)this.limitVelocity3+=0.02;
  2863. else this.limitVelocity3=0;
  2864. }
  2865. }else{
  2866. this.limitState3=2;
  2867. this.limitImpulse3=0;
  2868. }
  2869. if(this.enableMotor1&&(this.limitState1!=0||enableSpring1)){
  2870. this.maxMotorImpulse1=this.maxMotorForce1*timeStep;
  2871. }else{
  2872. this.motorImpulse1=0;
  2873. this.maxMotorImpulse1=0;
  2874. }
  2875. if(this.enableMotor2&&(this.limitState2!=0||enableSpring2)){
  2876. this.maxMotorImpulse2=this.maxMotorForce2*timeStep;
  2877. }else{
  2878. this.motorImpulse2=0;
  2879. this.maxMotorImpulse2=0;
  2880. }
  2881. if(this.enableMotor3&&(this.limitState3!=0||enableSpring3)){
  2882. this.maxMotorImpulse3=this.maxMotorForce3*timeStep;
  2883. }else{
  2884. this.motorImpulse3=0;
  2885. this.maxMotorImpulse3=0;
  2886. }
  2887. this.a1x1=this.ax1*this.i1e00+this.ay1*this.i1e01+this.az1*this.i1e02;
  2888. this.a1y1=this.ax1*this.i1e10+this.ay1*this.i1e11+this.az1*this.i1e12;
  2889. this.a1z1=this.ax1*this.i1e20+this.ay1*this.i1e21+this.az1*this.i1e22;
  2890. this.a2x1=this.ax1*this.i2e00+this.ay1*this.i2e01+this.az1*this.i2e02;
  2891. this.a2y1=this.ax1*this.i2e10+this.ay1*this.i2e11+this.az1*this.i2e12;
  2892. this.a2z1=this.ax1*this.i2e20+this.ay1*this.i2e21+this.az1*this.i2e22;
  2893. this.a1x2=this.ax2*this.i1e00+this.ay2*this.i1e01+this.az2*this.i1e02;
  2894. this.a1y2=this.ax2*this.i1e10+this.ay2*this.i1e11+this.az2*this.i1e12;
  2895. this.a1z2=this.ax2*this.i1e20+this.ay2*this.i1e21+this.az2*this.i1e22;
  2896. this.a2x2=this.ax2*this.i2e00+this.ay2*this.i2e01+this.az2*this.i2e02;
  2897. this.a2y2=this.ax2*this.i2e10+this.ay2*this.i2e11+this.az2*this.i2e12;
  2898. this.a2z2=this.ax2*this.i2e20+this.ay2*this.i2e21+this.az2*this.i2e22;
  2899. this.a1x3=this.ax3*this.i1e00+this.ay3*this.i1e01+this.az3*this.i1e02;
  2900. this.a1y3=this.ax3*this.i1e10+this.ay3*this.i1e11+this.az3*this.i1e12;
  2901. this.a1z3=this.ax3*this.i1e20+this.ay3*this.i1e21+this.az3*this.i1e22;
  2902. this.a2x3=this.ax3*this.i2e00+this.ay3*this.i2e01+this.az3*this.i2e02;
  2903. this.a2y3=this.ax3*this.i2e10+this.ay3*this.i2e11+this.az3*this.i2e12;
  2904. this.a2z3=this.ax3*this.i2e20+this.ay3*this.i2e21+this.az3*this.i2e22;
  2905. this.k00=this.ax1*(this.a1x1+this.a2x1)+this.ay1*(this.a1y1+this.a2y1)+this.az1*(this.a1z1+this.a2z1);
  2906. this.k01=this.ax1*(this.a1x2+this.a2x2)+this.ay1*(this.a1y2+this.a2y2)+this.az1*(this.a1z2+this.a2z2);
  2907. this.k02=this.ax1*(this.a1x3+this.a2x3)+this.ay1*(this.a1y3+this.a2y3)+this.az1*(this.a1z3+this.a2z3);
  2908. this.k10=this.ax2*(this.a1x1+this.a2x1)+this.ay2*(this.a1y1+this.a2y1)+this.az2*(this.a1z1+this.a2z1);
  2909. this.k11=this.ax2*(this.a1x2+this.a2x2)+this.ay2*(this.a1y2+this.a2y2)+this.az2*(this.a1z2+this.a2z2);
  2910. this.k12=this.ax2*(this.a1x3+this.a2x3)+this.ay2*(this.a1y3+this.a2y3)+this.az2*(this.a1z3+this.a2z3);
  2911. this.k20=this.ax3*(this.a1x1+this.a2x1)+this.ay3*(this.a1y1+this.a2y1)+this.az3*(this.a1z1+this.a2z1);
  2912. this.k21=this.ax3*(this.a1x2+this.a2x2)+this.ay3*(this.a1y2+this.a2y2)+this.az3*(this.a1z2+this.a2z2);
  2913. this.k22=this.ax3*(this.a1x3+this.a2x3)+this.ay3*(this.a1y3+this.a2y3)+this.az3*(this.a1z3+this.a2z3);
  2914. this.kv00=this.k00;
  2915. this.kv11=this.k11;
  2916. this.kv22=this.k22;
  2917. this.dv00=1/this.kv00;
  2918. this.dv11=1/this.kv11;
  2919. this.dv22=1/this.kv22;
  2920. if(enableSpring1&&this.limitState1!=2){
  2921. var omega=6.2831853*frequency1;
  2922. var k=omega*omega*timeStep;
  2923. var dmp=invTimeStep/(k+2*this.limitMotor1.dampingRatio*omega);
  2924. this.cfm1=this.kv00*dmp;
  2925. this.limitVelocity1*=k*dmp;
  2926. }else{
  2927. this.cfm1=0;
  2928. this.limitVelocity1*=invTimeStep*0.05;
  2929. }
  2930. if(enableSpring2&&this.limitState2!=2){
  2931. omega=6.2831853*frequency2;
  2932. k=omega*omega*timeStep;
  2933. dmp=invTimeStep/(k+2*this.limitMotor2.dampingRatio*omega);
  2934. this.cfm2=this.kv11*dmp;
  2935. this.limitVelocity2*=k*dmp;
  2936. }else{
  2937. this.cfm2=0;
  2938. this.limitVelocity2*=invTimeStep*0.05;
  2939. }
  2940. if(enableSpring3&&this.limitState3!=2){
  2941. omega=6.2831853*frequency3;
  2942. k=omega*omega*timeStep;
  2943. dmp=invTimeStep/(k+2*this.limitMotor3.dampingRatio*omega);
  2944. this.cfm3=this.kv22*dmp;
  2945. this.limitVelocity3*=k*dmp;
  2946. }else{
  2947. this.cfm3=0;
  2948. this.limitVelocity3*=invTimeStep*0.05;
  2949. }
  2950. this.k00+=this.cfm1;
  2951. this.k11+=this.cfm2;
  2952. this.k22+=this.cfm3;
  2953. var inv=1/(
  2954. this.k00*(this.k11*this.k22-this.k21*this.k12)+
  2955. this.k10*(this.k21*this.k02-this.k01*this.k22)+
  2956. this.k20*(this.k01*this.k12-this.k11*this.k02)
  2957. );
  2958. this.d00=(this.k11*this.k22-this.k12*this.k21)*inv;
  2959. this.d01=(this.k02*this.k21-this.k01*this.k22)*inv;
  2960. this.d02=(this.k01*this.k12-this.k02*this.k11)*inv;
  2961. this.d10=(this.k12*this.k20-this.k10*this.k22)*inv;
  2962. this.d11=(this.k00*this.k22-this.k02*this.k20)*inv;
  2963. this.d12=(this.k02*this.k10-this.k00*this.k12)*inv;
  2964. this.d20=(this.k10*this.k21-this.k11*this.k20)*inv;
  2965. this.d21=(this.k01*this.k20-this.k00*this.k21)*inv;
  2966. this.d22=(this.k00*this.k11-this.k01*this.k10)*inv;
  2967. this.limitImpulse1*=0.95;
  2968. this.motorImpulse1*=0.95;
  2969. this.limitImpulse2*=0.95;
  2970. this.motorImpulse2*=0.95;
  2971. this.limitImpulse3*=0.95;
  2972. this.motorImpulse3*=0.95;
  2973. var totalImpulse1=this.limitImpulse1+this.motorImpulse1;
  2974. var totalImpulse2=this.limitImpulse2+this.motorImpulse2;
  2975. var totalImpulse3=this.limitImpulse3+this.motorImpulse3;
  2976. this.a1.x+=totalImpulse1*this.a1x1+totalImpulse2*this.a1x2+totalImpulse3*this.a1x3;
  2977. this.a1.y+=totalImpulse1*this.a1y1+totalImpulse2*this.a1y2+totalImpulse3*this.a1y3;
  2978. this.a1.z+=totalImpulse1*this.a1z1+totalImpulse2*this.a1z2+totalImpulse3*this.a1z3;
  2979. this.a2.x-=totalImpulse1*this.a2x1+totalImpulse2*this.a2x2+totalImpulse3*this.a2x3;
  2980. this.a2.y-=totalImpulse1*this.a2y1+totalImpulse2*this.a2y2+totalImpulse3*this.a2y3;
  2981. this.a2.z-=totalImpulse1*this.a2z1+totalImpulse2*this.a2z2+totalImpulse3*this.a2z3;
  2982. },
  2983. solve_:function(){
  2984. var rvx=this.a2.x-this.a1.x;
  2985. var rvy=this.a2.y-this.a1.y;
  2986. var rvz=this.a2.z-this.a1.z;
  2987. this.limitVelocity3=30;
  2988. var rvn1=rvx*this.ax1+rvy*this.ay1+rvz*this.az1-this.limitVelocity1;
  2989. var rvn2=rvx*this.ax2+rvy*this.ay2+rvz*this.az2-this.limitVelocity2;
  2990. var rvn3=rvx*this.ax3+rvy*this.ay3+rvz*this.az3-this.limitVelocity3;
  2991. var dLimitImpulse1=rvn1*this.d00+rvn2*this.d01+rvn3*this.d02;
  2992. var dLimitImpulse2=rvn1*this.d10+rvn2*this.d11+rvn3*this.d12;
  2993. var dLimitImpulse3=rvn1*this.d20+rvn2*this.d21+rvn3*this.d22;
  2994. this.limitImpulse1+=dLimitImpulse1;
  2995. this.limitImpulse2+=dLimitImpulse2;
  2996. this.limitImpulse3+=dLimitImpulse3;
  2997. this.a1.x+=dLimitImpulse1*this.a1x1+dLimitImpulse2*this.a1x2+dLimitImpulse3*this.a1x3;
  2998. this.a1.y+=dLimitImpulse1*this.a1y1+dLimitImpulse2*this.a1y2+dLimitImpulse3*this.a1y3;
  2999. this.a1.z+=dLimitImpulse1*this.a1z1+dLimitImpulse2*this.a1z2+dLimitImpulse3*this.a1z3;
  3000. this.a2.x-=dLimitImpulse1*this.a2x1+dLimitImpulse2*this.a2x2+dLimitImpulse3*this.a2x3;
  3001. this.a2.y-=dLimitImpulse1*this.a2y1+dLimitImpulse2*this.a2y2+dLimitImpulse3*this.a2y3;
  3002. this.a2.z-=dLimitImpulse1*this.a2z1+dLimitImpulse2*this.a2z2+dLimitImpulse3*this.a2z3;
  3003. },
  3004. solve:function(){
  3005. var rvx=this.a2.x-this.a1.x;
  3006. var rvy=this.a2.y-this.a1.y;
  3007. var rvz=this.a2.z-this.a1.z;
  3008. var rvn1=rvx*this.ax1+rvy*this.ay1+rvz*this.az1;
  3009. var rvn2=rvx*this.ax2+rvy*this.ay2+rvz*this.az2;
  3010. var rvn3=rvx*this.ax3+rvy*this.ay3+rvz*this.az3;
  3011. var oldMotorImpulse1=this.motorImpulse1;
  3012. var oldMotorImpulse2=this.motorImpulse2;
  3013. var oldMotorImpulse3=this.motorImpulse3;
  3014. var dMotorImpulse1=0;
  3015. var dMotorImpulse2=0;
  3016. var dMotorImpulse3=0;
  3017. if(this.enableMotor1){
  3018. dMotorImpulse1=(rvn1-this.motorSpeed1)*this.dv00;
  3019. this.motorImpulse1+=dMotorImpulse1;
  3020. if(this.motorImpulse1>this.maxMotorImpulse1){
  3021. this.motorImpulse1=this.maxMotorImpulse1;
  3022. }else if(this.motorImpulse1<-this.maxMotorImpulse1){
  3023. this.motorImpulse1=-this.maxMotorImpulse1;
  3024. }
  3025. dMotorImpulse1=this.motorImpulse1-oldMotorImpulse1;
  3026. }
  3027. if(this.enableMotor2){
  3028. dMotorImpulse2=(rvn2-this.motorSpeed2)*this.dv11;
  3029. this.motorImpulse2+=dMotorImpulse2;
  3030. if(this.motorImpulse2>this.maxMotorImpulse2){
  3031. this.motorImpulse2=this.maxMotorImpulse2;
  3032. }else if(this.motorImpulse2<-this.maxMotorImpulse2){
  3033. this.motorImpulse2=-this.maxMotorImpulse2;
  3034. }
  3035. dMotorImpulse2=this.motorImpulse2-oldMotorImpulse2;
  3036. }
  3037. if(this.enableMotor3){
  3038. dMotorImpulse3=(rvn3-this.motorSpeed3)*this.dv22;
  3039. this.motorImpulse3+=dMotorImpulse3;
  3040. if(this.motorImpulse3>this.maxMotorImpulse3){
  3041. this.motorImpulse3=this.maxMotorImpulse3;
  3042. }else if(this.motorImpulse3<-this.maxMotorImpulse3){
  3043. this.motorImpulse3=-this.maxMotorImpulse3;
  3044. }
  3045. dMotorImpulse3=this.motorImpulse3-oldMotorImpulse3;
  3046. }
  3047. rvn1+=dMotorImpulse1*this.kv00+dMotorImpulse2*this.k01+dMotorImpulse3*this.k02;
  3048. rvn2+=dMotorImpulse1*this.k10+dMotorImpulse2*this.kv11+dMotorImpulse3*this.k12;
  3049. rvn3+=dMotorImpulse1*this.k20+dMotorImpulse2*this.k21+dMotorImpulse3*this.kv22;
  3050. rvn1-=this.limitVelocity1+this.limitImpulse1*this.cfm1;
  3051. rvn2-=this.limitVelocity2+this.limitImpulse2*this.cfm2;
  3052. rvn3-=this.limitVelocity3+this.limitImpulse3*this.cfm3;
  3053. var oldLimitImpulse1=this.limitImpulse1;
  3054. var oldLimitImpulse2=this.limitImpulse2;
  3055. var oldLimitImpulse3=this.limitImpulse3;
  3056. var dLimitImpulse1=rvn1*this.d00+rvn2*this.d01+rvn3*this.d02;
  3057. var dLimitImpulse2=rvn1*this.d10+rvn2*this.d11+rvn3*this.d12;
  3058. var dLimitImpulse3=rvn1*this.d20+rvn2*this.d21+rvn3*this.d22;
  3059. this.limitImpulse1+=dLimitImpulse1;
  3060. this.limitImpulse2+=dLimitImpulse2;
  3061. this.limitImpulse3+=dLimitImpulse3;
  3062. var clampState=0;
  3063. if(this.limitState1==2||this.limitImpulse1*this.limitState1<0){
  3064. dLimitImpulse1=-oldLimitImpulse1;
  3065. rvn2+=dLimitImpulse1*this.k10;
  3066. rvn3+=dLimitImpulse1*this.k20;
  3067. clampState|=1;
  3068. }
  3069. if(this.limitState2==2||this.limitImpulse2*this.limitState2<0){
  3070. dLimitImpulse2=-oldLimitImpulse2;
  3071. rvn1+=dLimitImpulse2*this.k01;
  3072. rvn3+=dLimitImpulse2*this.k21;
  3073. clampState|=2;
  3074. }
  3075. if(this.limitState3==2||this.limitImpulse3*this.limitState3<0){
  3076. dLimitImpulse3=-oldLimitImpulse3;
  3077. rvn1+=dLimitImpulse3*this.k02;
  3078. rvn2+=dLimitImpulse3*this.k12;
  3079. clampState|=4;
  3080. }
  3081. var det;
  3082. switch(clampState){
  3083. case 1:
  3084. det=1/(this.k11*this.k22-this.k12*this.k21);
  3085. dLimitImpulse2=(this.k22*rvn2+-this.k12*rvn3)*det;
  3086. dLimitImpulse3=(-this.k21*rvn2+this.k11*rvn3)*det;
  3087. break;
  3088. case 2:
  3089. det=1/(this.k00*this.k22-this.k02*this.k20);
  3090. dLimitImpulse1=(this.k22*rvn1+-this.k02*rvn3)*det;
  3091. dLimitImpulse3=(-this.k20*rvn1+this.k00*rvn3)*det;
  3092. break;
  3093. case 3:
  3094. dLimitImpulse3=rvn3/this.k22;
  3095. break;
  3096. case 4:
  3097. det=1/(this.k00*this.k11-this.k01*this.k10);
  3098. dLimitImpulse1=(this.k11*rvn1+-this.k01*rvn2)*det;
  3099. dLimitImpulse2=(-this.k10*rvn1+this.k00*rvn2)*det;
  3100. break;
  3101. case 5:
  3102. dLimitImpulse2=rvn2/this.k11;
  3103. break;
  3104. case 6:
  3105. dLimitImpulse1=rvn1/this.k00;
  3106. break;
  3107. }
  3108. this.limitImpulse1=dLimitImpulse1+oldLimitImpulse1;
  3109. this.limitImpulse2=dLimitImpulse2+oldLimitImpulse2;
  3110. this.limitImpulse3=dLimitImpulse3+oldLimitImpulse3;
  3111. var dImpulse1=dMotorImpulse1+dLimitImpulse1;
  3112. var dImpulse2=dMotorImpulse2+dLimitImpulse2;
  3113. var dImpulse3=dMotorImpulse3+dLimitImpulse3;
  3114. this.a1.x+=dImpulse1*this.a1x1+dImpulse2*this.a1x2+dImpulse3*this.a1x3;
  3115. this.a1.y+=dImpulse1*this.a1y1+dImpulse2*this.a1y2+dImpulse3*this.a1y3;
  3116. this.a1.z+=dImpulse1*this.a1z1+dImpulse2*this.a1z2+dImpulse3*this.a1z3;
  3117. this.a2.x-=dImpulse1*this.a2x1+dImpulse2*this.a2x2+dImpulse3*this.a2x3;
  3118. this.a2.y-=dImpulse1*this.a2y1+dImpulse2*this.a2y2+dImpulse3*this.a2y3;
  3119. this.a2.z-=dImpulse1*this.a2z1+dImpulse2*this.a2z2+dImpulse3*this.a2z3;
  3120. rvx=this.a2.x-this.a1.x;
  3121. rvy=this.a2.y-this.a1.y;
  3122. rvz=this.a2.z-this.a1.z;
  3123. rvn2=rvx*this.ax2+rvy*this.ay2+rvz*this.az2;
  3124. }
  3125. }
  3126. OIMO.RotationalConstraint = function(joint,limitMotor){
  3127. this.cfm=NaN;
  3128. this.i1e00=NaN;
  3129. this.i1e01=NaN;
  3130. this.i1e02=NaN;
  3131. this.i1e10=NaN;
  3132. this.i1e11=NaN;
  3133. this.i1e12=NaN;
  3134. this.i1e20=NaN;
  3135. this.i1e21=NaN;
  3136. this.i1e22=NaN;
  3137. this.i2e00=NaN;
  3138. this.i2e01=NaN;
  3139. this.i2e02=NaN;
  3140. this.i2e10=NaN;
  3141. this.i2e11=NaN;
  3142. this.i2e12=NaN;
  3143. this.i2e20=NaN;
  3144. this.i2e21=NaN;
  3145. this.i2e22=NaN;
  3146. this.motorDenom=NaN;
  3147. this.invMotorDenom=NaN;
  3148. this.invDenom=NaN;
  3149. this.ax=NaN;
  3150. this.ay=NaN;
  3151. this.az=NaN;
  3152. this.a1x=NaN;
  3153. this.a1y=NaN;
  3154. this.a1z=NaN;
  3155. this.a2x=NaN;
  3156. this.a2y=NaN;
  3157. this.a2z=NaN;
  3158. this.enableLimit=false;
  3159. this.lowerLimit=NaN;
  3160. this.upperLimit=NaN;
  3161. this.limitVelocity=NaN;
  3162. this.limitState=0;
  3163. this.enableMotor=false;
  3164. this.motorSpeed=NaN;
  3165. this.maxMotorForce=NaN;
  3166. this.maxMotorImpulse=NaN;
  3167. this.limitMotor=limitMotor;
  3168. this.b1=joint.body1;
  3169. this.b2=joint.body2;
  3170. this.a1=this.b1.angularVelocity;
  3171. this.a2=this.b2.angularVelocity;
  3172. this.i1=this.b1.inverseInertia;
  3173. this.i2=this.b2.inverseInertia;
  3174. this.limitImpulse=0;
  3175. this.motorImpulse=0;
  3176. }
  3177. OIMO.RotationalConstraint.prototype = {
  3178. constructor: OIMO.RotationalConstraint,
  3179. preSolve:function(timeStep,invTimeStep){
  3180. this.ax=this.limitMotor.axis.x;
  3181. this.ay=this.limitMotor.axis.y;
  3182. this.az=this.limitMotor.axis.z;
  3183. this.lowerLimit=this.limitMotor.lowerLimit;
  3184. this.upperLimit=this.limitMotor.upperLimit;
  3185. this.motorSpeed=this.limitMotor.motorSpeed;
  3186. this.maxMotorForce=this.limitMotor.maxMotorForce;
  3187. this.enableMotor=this.maxMotorForce>0;
  3188. var ti1 = this.i1.elements;
  3189. var ti2 = this.i2.elements;
  3190. this.i1e00=ti1[0];
  3191. this.i1e01=ti1[1];
  3192. this.i1e02=ti1[2];
  3193. this.i1e10=ti1[3];
  3194. this.i1e11=ti1[4];
  3195. this.i1e12=ti1[5];
  3196. this.i1e20=ti1[6];
  3197. this.i1e21=ti1[7];
  3198. this.i1e22=ti1[8];
  3199. this.i2e00=ti2[0];
  3200. this.i2e01=ti2[1];
  3201. this.i2e02=ti2[2];
  3202. this.i2e10=ti2[3];
  3203. this.i2e11=ti2[4];
  3204. this.i2e12=ti2[5];
  3205. this.i2e20=ti2[6];
  3206. this.i2e21=ti2[7];
  3207. this.i2e22=ti2[8];
  3208. var frequency=this.limitMotor.frequency;
  3209. var enableSpring=frequency>0;
  3210. var enableLimit=this.lowerLimit<=this.upperLimit;
  3211. var angle=this.limitMotor.angle;
  3212. if(enableLimit){
  3213. if(this.lowerLimit==this.upperLimit){
  3214. if(this.limitState!=0){
  3215. this.limitState=0;
  3216. this.limitImpulse=0;
  3217. }
  3218. this.limitVelocity=this.lowerLimit-angle;
  3219. }else if(angle<this.lowerLimit){
  3220. if(this.limitState!=-1){
  3221. this.limitState=-1;
  3222. this.limitImpulse=0;
  3223. }
  3224. this.limitVelocity=this.lowerLimit-angle;
  3225. }else if(angle>this.upperLimit){
  3226. if(this.limitState!=1){
  3227. this.limitState=1;
  3228. this.limitImpulse=0;
  3229. }
  3230. this.limitVelocity=this.upperLimit-angle;
  3231. }else{
  3232. this.limitState=2;
  3233. this.limitImpulse=0;
  3234. this.limitVelocity=0;
  3235. }
  3236. if(!enableSpring){
  3237. if(this.limitVelocity>0.02)this.limitVelocity-=0.02;
  3238. else if(this.limitVelocity<-0.02)this.limitVelocity+=0.02;
  3239. else this.limitVelocity=0;
  3240. }
  3241. }else{
  3242. this.limitState=2;
  3243. this.limitImpulse=0;
  3244. }
  3245. if(this.enableMotor&&(this.limitState!=0||enableSpring)){
  3246. this.maxMotorImpulse=this.maxMotorForce*timeStep;
  3247. }else{
  3248. this.motorImpulse=0;
  3249. this.maxMotorImpulse=0;
  3250. }
  3251. this.a1x=this.ax*this.i1e00+this.ay*this.i1e01+this.az*this.i1e02;
  3252. this.a1y=this.ax*this.i1e10+this.ay*this.i1e11+this.az*this.i1e12;
  3253. this.a1z=this.ax*this.i1e20+this.ay*this.i1e21+this.az*this.i1e22;
  3254. this.a2x=this.ax*this.i2e00+this.ay*this.i2e01+this.az*this.i2e02;
  3255. this.a2y=this.ax*this.i2e10+this.ay*this.i2e11+this.az*this.i2e12;
  3256. this.a2z=this.ax*this.i2e20+this.ay*this.i2e21+this.az*this.i2e22;
  3257. this.motorDenom=this.ax*(this.a1x+this.a2x)+this.ay*(this.a1y+this.a2y)+this.az*(this.a1z+this.a2z);
  3258. this.invMotorDenom=1/this.motorDenom;
  3259. if(enableSpring&&this.limitState!=2){
  3260. var omega=6.2831853*frequency;
  3261. var k=omega*omega*timeStep;
  3262. var dmp=invTimeStep/(k+2*this.limitMotor.dampingRatio*omega);
  3263. this.cfm=this.motorDenom*dmp;
  3264. this.limitVelocity*=k*dmp;
  3265. }else{
  3266. this.cfm=0;
  3267. this.limitVelocity*=invTimeStep*0.05;
  3268. }
  3269. this.invDenom=1/(this.motorDenom+this.cfm);
  3270. this.limitImpulse*=0.95;
  3271. this.motorImpulse*=0.95;
  3272. var totalImpulse=this.limitImpulse+this.motorImpulse;
  3273. this.a1.x+=totalImpulse*this.a1x;
  3274. this.a1.y+=totalImpulse*this.a1y;
  3275. this.a1.z+=totalImpulse*this.a1z;
  3276. this.a2.x-=totalImpulse*this.a2x;
  3277. this.a2.y-=totalImpulse*this.a2y;
  3278. this.a2.z-=totalImpulse*this.a2z;
  3279. },
  3280. solve:function(){
  3281. var rvn=this.ax*(this.a2.x-this.a1.x)+this.ay*(this.a2.y-this.a1.y)+this.az*(this.a2.z-this.a1.z);
  3282. var newMotorImpulse;
  3283. if(this.enableMotor){
  3284. newMotorImpulse=(rvn-this.motorSpeed)*this.invMotorDenom;
  3285. var oldMotorImpulse=this.motorImpulse;
  3286. this.motorImpulse+=newMotorImpulse;
  3287. if(this.motorImpulse>this.maxMotorImpulse)this.motorImpulse=this.maxMotorImpulse;
  3288. else if(this.motorImpulse<-this.maxMotorImpulse)this.motorImpulse=-this.maxMotorImpulse;
  3289. newMotorImpulse=this.motorImpulse-oldMotorImpulse;
  3290. rvn-=newMotorImpulse*this.motorDenom;
  3291. }else newMotorImpulse=0;
  3292. var newLimitImpulse;
  3293. if(this.limitState!=2){
  3294. newLimitImpulse=(rvn-this.limitVelocity-this.limitImpulse*this.cfm)*this.invDenom;
  3295. var oldLimitImpulse=this.limitImpulse;
  3296. this.limitImpulse+=newLimitImpulse;
  3297. if(this.limitImpulse*this.limitState<0)this.limitImpulse=0;
  3298. newLimitImpulse=this.limitImpulse-oldLimitImpulse;
  3299. }else newLimitImpulse=0;
  3300. var totalImpulse=newLimitImpulse+newMotorImpulse;
  3301. this.a1.x+=totalImpulse*this.a1x;
  3302. this.a1.y+=totalImpulse*this.a1y;
  3303. this.a1.z+=totalImpulse*this.a1z;
  3304. this.a2.x-=totalImpulse*this.a2x;
  3305. this.a2.y-=totalImpulse*this.a2y;
  3306. this.a2.z-=totalImpulse*this.a2z;
  3307. }
  3308. }
  3309. OIMO.Translational3Constraint = function(joint,limitMotor1,limitMotor2,limitMotor3){
  3310. this.m1=NaN;
  3311. this.m2=NaN;
  3312. this.i1e00=NaN;
  3313. this.i1e01=NaN;
  3314. this.i1e02=NaN;
  3315. this.i1e10=NaN;
  3316. this.i1e11=NaN;
  3317. this.i1e12=NaN;
  3318. this.i1e20=NaN;
  3319. this.i1e21=NaN;
  3320. this.i1e22=NaN;
  3321. this.i2e00=NaN;
  3322. this.i2e01=NaN;
  3323. this.i2e02=NaN;
  3324. this.i2e10=NaN;
  3325. this.i2e11=NaN;
  3326. this.i2e12=NaN;
  3327. this.i2e20=NaN;
  3328. this.i2e21=NaN;
  3329. this.i2e22=NaN;
  3330. this.ax1=NaN;
  3331. this.ay1=NaN;
  3332. this.az1=NaN;
  3333. this.ax2=NaN;
  3334. this.ay2=NaN;
  3335. this.az2=NaN;
  3336. this.ax3=NaN;
  3337. this.ay3=NaN;
  3338. this.az3=NaN;
  3339. this.r1x=NaN;
  3340. this.r1y=NaN;
  3341. this.r1z=NaN;
  3342. this.r2x=NaN;
  3343. this.r2y=NaN;
  3344. this.r2z=NaN;
  3345. this.t1x1=NaN;
  3346. this.t1y1=NaN;
  3347. this.t1z1=NaN;
  3348. this.t2x1=NaN;
  3349. this.t2y1=NaN;
  3350. this.t2z1=NaN;
  3351. this.l1x1=NaN;
  3352. this.l1y1=NaN;
  3353. this.l1z1=NaN;
  3354. this.l2x1=NaN;
  3355. this.l2y1=NaN;
  3356. this.l2z1=NaN;
  3357. this.a1x1=NaN;
  3358. this.a1y1=NaN;
  3359. this.a1z1=NaN;
  3360. this.a2x1=NaN;
  3361. this.a2y1=NaN;
  3362. this.a2z1=NaN;
  3363. this.t1x2=NaN;
  3364. this.t1y2=NaN;
  3365. this.t1z2=NaN;
  3366. this.t2x2=NaN;
  3367. this.t2y2=NaN;
  3368. this.t2z2=NaN;
  3369. this.l1x2=NaN;
  3370. this.l1y2=NaN;
  3371. this.l1z2=NaN;
  3372. this.l2x2=NaN;
  3373. this.l2y2=NaN;
  3374. this.l2z2=NaN;
  3375. this.a1x2=NaN;
  3376. this.a1y2=NaN;
  3377. this.a1z2=NaN;
  3378. this.a2x2=NaN;
  3379. this.a2y2=NaN;
  3380. this.a2z2=NaN;
  3381. this.t1x3=NaN;
  3382. this.t1y3=NaN;
  3383. this.t1z3=NaN;
  3384. this.t2x3=NaN;
  3385. this.t2y3=NaN;
  3386. this.t2z3=NaN;
  3387. this.l1x3=NaN;
  3388. this.l1y3=NaN;
  3389. this.l1z3=NaN;
  3390. this.l2x3=NaN;
  3391. this.l2y3=NaN;
  3392. this.l2z3=NaN;
  3393. this.a1x3=NaN;
  3394. this.a1y3=NaN;
  3395. this.a1z3=NaN;
  3396. this.a2x3=NaN;
  3397. this.a2y3=NaN;
  3398. this.a2z3=NaN;
  3399. this.lowerLimit1=NaN;
  3400. this.upperLimit1=NaN;
  3401. this.limitVelocity1=NaN;
  3402. this.limitState1=0;
  3403. this.enableMotor1=false;
  3404. this.motorSpeed1=NaN;
  3405. this.maxMotorForce1=NaN;
  3406. this.maxMotorImpulse1=NaN;
  3407. this.lowerLimit2=NaN;
  3408. this.upperLimit2=NaN;
  3409. this.limitVelocity2=NaN;
  3410. this.limitState2=0;
  3411. this.enableMotor2=false;
  3412. this.motorSpeed2=NaN;
  3413. this.maxMotorForce2=NaN;
  3414. this.maxMotorImpulse2=NaN;
  3415. this.lowerLimit3=NaN;
  3416. this.upperLimit3=NaN;
  3417. this.limitVelocity3=NaN;
  3418. this.limitState3=0;
  3419. this.enableMotor3=false;
  3420. this.motorSpeed3=NaN;
  3421. this.maxMotorForce3=NaN;
  3422. this.maxMotorImpulse3=NaN;
  3423. this.k00=NaN;
  3424. this.k01=NaN;
  3425. this.k02=NaN;
  3426. this.k10=NaN;
  3427. this.k11=NaN;
  3428. this.k12=NaN;
  3429. this.k20=NaN;
  3430. this.k21=NaN;
  3431. this.k22=NaN;
  3432. this.kv00=NaN;
  3433. this.kv11=NaN;
  3434. this.kv22=NaN;
  3435. this.dv00=NaN;
  3436. this.dv11=NaN;
  3437. this.dv22=NaN;
  3438. this.d00=NaN;
  3439. this.d01=NaN;
  3440. this.d02=NaN;
  3441. this.d10=NaN;
  3442. this.d11=NaN;
  3443. this.d12=NaN;
  3444. this.d20=NaN;
  3445. this.d21=NaN;
  3446. this.d22=NaN;
  3447. this.limitMotor1=limitMotor1;
  3448. this.limitMotor2=limitMotor2;
  3449. this.limitMotor3=limitMotor3;
  3450. this.b1=joint.body1;
  3451. this.b2=joint.body2;
  3452. this.p1=joint.anchorPoint1;
  3453. this.p2=joint.anchorPoint2;
  3454. this.r1=joint.relativeAnchorPoint1;
  3455. this.r2=joint.relativeAnchorPoint2;
  3456. this.l1=this.b1.linearVelocity;
  3457. this.l2=this.b2.linearVelocity;
  3458. this.a1=this.b1.angularVelocity;
  3459. this.a2=this.b2.angularVelocity;
  3460. this.i1=this.b1.inverseInertia;
  3461. this.i2=this.b2.inverseInertia;
  3462. this.limitImpulse1=0;
  3463. this.motorImpulse1=0;
  3464. this.limitImpulse2=0;
  3465. this.motorImpulse2=0;
  3466. this.limitImpulse3=0;
  3467. this.motorImpulse3=0;
  3468. this.cfm1=0;
  3469. this.cfm2=0;
  3470. this.cfm3=0;
  3471. this.weight=-1;
  3472. }
  3473. OIMO.Translational3Constraint.prototype = {
  3474. constructor: OIMO.Translational3Constraint,
  3475. preSolve:function(timeStep,invTimeStep){
  3476. this.ax1=this.limitMotor1.axis.x;
  3477. this.ay1=this.limitMotor1.axis.y;
  3478. this.az1=this.limitMotor1.axis.z;
  3479. this.ax2=this.limitMotor2.axis.x;
  3480. this.ay2=this.limitMotor2.axis.y;
  3481. this.az2=this.limitMotor2.axis.z;
  3482. this.ax3=this.limitMotor3.axis.x;
  3483. this.ay3=this.limitMotor3.axis.y;
  3484. this.az3=this.limitMotor3.axis.z;
  3485. this.lowerLimit1=this.limitMotor1.lowerLimit;
  3486. this.upperLimit1=this.limitMotor1.upperLimit;
  3487. this.motorSpeed1=this.limitMotor1.motorSpeed;
  3488. this.maxMotorForce1=this.limitMotor1.maxMotorForce;
  3489. this.enableMotor1=this.maxMotorForce1>0;
  3490. this.lowerLimit2=this.limitMotor2.lowerLimit;
  3491. this.upperLimit2=this.limitMotor2.upperLimit;
  3492. this.motorSpeed2=this.limitMotor2.motorSpeed;
  3493. this.maxMotorForce2=this.limitMotor2.maxMotorForce;
  3494. this.enableMotor2=this.maxMotorForce2>0;
  3495. this.lowerLimit3=this.limitMotor3.lowerLimit;
  3496. this.upperLimit3=this.limitMotor3.upperLimit;
  3497. this.motorSpeed3=this.limitMotor3.motorSpeed;
  3498. this.maxMotorForce3=this.limitMotor3.maxMotorForce;
  3499. this.enableMotor3=this.maxMotorForce3>0;
  3500. this.m1=this.b1.inverseMass;
  3501. this.m2=this.b2.inverseMass;
  3502. var ti1 = this.i1.elements;
  3503. var ti2 = this.i2.elements;
  3504. this.i1e00=ti1[0];
  3505. this.i1e01=ti1[1];
  3506. this.i1e02=ti1[2];
  3507. this.i1e10=ti1[3];
  3508. this.i1e11=ti1[4];
  3509. this.i1e12=ti1[5];
  3510. this.i1e20=ti1[6];
  3511. this.i1e21=ti1[7];
  3512. this.i1e22=ti1[8];
  3513. this.i2e00=ti2[0];
  3514. this.i2e01=ti2[1];
  3515. this.i2e02=ti2[2];
  3516. this.i2e10=ti2[3];
  3517. this.i2e11=ti2[4];
  3518. this.i2e12=ti2[5];
  3519. this.i2e20=ti2[6];
  3520. this.i2e21=ti2[7];
  3521. this.i2e22=ti2[8];
  3522. var dx=this.p2.x-this.p1.x;
  3523. var dy=this.p2.y-this.p1.y;
  3524. var dz=this.p2.z-this.p1.z;
  3525. var d1=dx*this.ax1+dy*this.ay1+dz*this.az1;
  3526. var d2=dx*this.ax2+dy*this.ay2+dz*this.az2;
  3527. var d3=dx*this.ax3+dy*this.ay3+dz*this.az3;
  3528. var frequency1=this.limitMotor1.frequency;
  3529. var frequency2=this.limitMotor2.frequency;
  3530. var frequency3=this.limitMotor3.frequency;
  3531. var enableSpring1=frequency1>0;
  3532. var enableSpring2=frequency2>0;
  3533. var enableSpring3=frequency3>0;
  3534. var enableLimit1=this.lowerLimit1<=this.upperLimit1;
  3535. var enableLimit2=this.lowerLimit2<=this.upperLimit2;
  3536. var enableLimit3=this.lowerLimit3<=this.upperLimit3;
  3537. if(enableSpring1&&d1>20||d1<-20){
  3538. enableSpring1=false;
  3539. }
  3540. if(enableSpring2&&d2>20||d2<-20){
  3541. enableSpring2=false;
  3542. }
  3543. if(enableSpring3&&d3>20||d3<-20){
  3544. enableSpring3=false;
  3545. }
  3546. if(enableLimit1){
  3547. if(this.lowerLimit1==this.upperLimit1){
  3548. if(this.limitState1!=0){
  3549. this.limitState1=0;
  3550. this.limitImpulse1=0;
  3551. }
  3552. this.limitVelocity1=this.lowerLimit1-d1;
  3553. if(!enableSpring1)d1=this.lowerLimit1;
  3554. }else if(d1<this.lowerLimit1){
  3555. if(this.limitState1!=-1){
  3556. this.limitState1=-1;
  3557. this.limitImpulse1=0;
  3558. }
  3559. this.limitVelocity1=this.lowerLimit1-d1;
  3560. if(!enableSpring1)d1=this.lowerLimit1;
  3561. }else if(d1>this.upperLimit1){
  3562. if(this.limitState1!=1){
  3563. this.limitState1=1;
  3564. this.limitImpulse1=0;
  3565. }
  3566. this.limitVelocity1=this.upperLimit1-d1;
  3567. if(!enableSpring1)d1=this.upperLimit1;
  3568. }else{
  3569. this.limitState1=2;
  3570. this.limitImpulse1=0;
  3571. this.limitVelocity1=0;
  3572. }
  3573. if(!enableSpring1){
  3574. if(this.limitVelocity1>0.005)this.limitVelocity1-=0.005;
  3575. else if(this.limitVelocity1<-0.005)this.limitVelocity1+=0.005;
  3576. else this.limitVelocity1=0;
  3577. }
  3578. }else{
  3579. this.limitState1=2;
  3580. this.limitImpulse1=0;
  3581. }
  3582. if(enableLimit2){
  3583. if(this.lowerLimit2==this.upperLimit2){
  3584. if(this.limitState2!=0){
  3585. this.limitState2=0;
  3586. this.limitImpulse2=0;
  3587. }
  3588. this.limitVelocity2=this.lowerLimit2-d2;
  3589. if(!enableSpring2)d2=this.lowerLimit2;
  3590. }else if(d2<this.lowerLimit2){
  3591. if(this.limitState2!=-1){
  3592. this.limitState2=-1;
  3593. this.limitImpulse2=0;
  3594. }
  3595. this.limitVelocity2=this.lowerLimit2-d2;
  3596. if(!enableSpring2)d2=this.lowerLimit2;
  3597. }else if(d2>this.upperLimit2){
  3598. if(this.limitState2!=1){
  3599. this.limitState2=1;
  3600. this.limitImpulse2=0;
  3601. }
  3602. this.limitVelocity2=this.upperLimit2-d2;
  3603. if(!enableSpring2)d2=this.upperLimit2;
  3604. }else{
  3605. this.limitState2=2;
  3606. this.limitImpulse2=0;
  3607. this.limitVelocity2=0;
  3608. }
  3609. if(!enableSpring2){
  3610. if(this.limitVelocity2>0.005)this.limitVelocity2-=0.005;
  3611. else if(this.limitVelocity2<-0.005)this.limitVelocity2+=0.005;
  3612. else this.limitVelocity2=0;
  3613. }
  3614. }else{
  3615. this.limitState2=2;
  3616. this.limitImpulse2=0;
  3617. }
  3618. if(enableLimit3){
  3619. if(this.lowerLimit3==this.upperLimit3){
  3620. if(this.limitState3!=0){
  3621. this.limitState3=0;
  3622. this.limitImpulse3=0;
  3623. }
  3624. this.limitVelocity3=this.lowerLimit3-d3;
  3625. if(!enableSpring3)d3=this.lowerLimit3;
  3626. }else if(d3<this.lowerLimit3){
  3627. if(this.limitState3!=-1){
  3628. this.limitState3=-1;
  3629. this.limitImpulse3=0;
  3630. }
  3631. this.limitVelocity3=this.lowerLimit3-d3;
  3632. if(!enableSpring3)d3=this.lowerLimit3;
  3633. }else if(d3>this.upperLimit3){
  3634. if(this.limitState3!=1){
  3635. this.limitState3=1;
  3636. this.limitImpulse3=0;
  3637. }
  3638. this.limitVelocity3=this.upperLimit3-d3;
  3639. if(!enableSpring3)d3=this.upperLimit3;
  3640. }else{
  3641. this.limitState3=2;
  3642. this.limitImpulse3=0;
  3643. this.limitVelocity3=0;
  3644. }
  3645. if(!enableSpring3){
  3646. if(this.limitVelocity3>0.005)this.limitVelocity3-=0.005;
  3647. else if(this.limitVelocity3<-0.005)this.limitVelocity3+=0.005;
  3648. else this.limitVelocity3=0;
  3649. }
  3650. }else{
  3651. this.limitState3=2;
  3652. this.limitImpulse3=0;
  3653. }
  3654. if(this.enableMotor1&&(this.limitState1!=0||enableSpring1)){
  3655. this.maxMotorImpulse1=this.maxMotorForce1*timeStep;
  3656. }else{
  3657. this.motorImpulse1=0;
  3658. this.maxMotorImpulse1=0;
  3659. }
  3660. if(this.enableMotor2&&(this.limitState2!=0||enableSpring2)){
  3661. this.maxMotorImpulse2=this.maxMotorForce2*timeStep;
  3662. }else{
  3663. this.motorImpulse2=0;
  3664. this.maxMotorImpulse2=0;
  3665. }
  3666. if(this.enableMotor3&&(this.limitState3!=0||enableSpring3)){
  3667. this.maxMotorImpulse3=this.maxMotorForce3*timeStep;
  3668. }else{
  3669. this.motorImpulse3=0;
  3670. this.maxMotorImpulse3=0;
  3671. }
  3672. var rdx=d1*this.ax1+d2*this.ax2+d3*this.ax2;
  3673. var rdy=d1*this.ay1+d2*this.ay2+d3*this.ay2;
  3674. var rdz=d1*this.az1+d2*this.az2+d3*this.az2;
  3675. var w1=this.m2/(this.m1+this.m2);
  3676. if(this.weight>=0)w1=this.weight;
  3677. var w2=1-w1;
  3678. this.r1x=this.r1.x+rdx*w1;
  3679. this.r1y=this.r1.y+rdy*w1;
  3680. this.r1z=this.r1.z+rdz*w1;
  3681. this.r2x=this.r2.x-rdx*w2;
  3682. this.r2y=this.r2.y-rdy*w2;
  3683. this.r2z=this.r2.z-rdz*w2;
  3684. this.t1x1=this.r1y*this.az1-this.r1z*this.ay1;
  3685. this.t1y1=this.r1z*this.ax1-this.r1x*this.az1;
  3686. this.t1z1=this.r1x*this.ay1-this.r1y*this.ax1;
  3687. this.t2x1=this.r2y*this.az1-this.r2z*this.ay1;
  3688. this.t2y1=this.r2z*this.ax1-this.r2x*this.az1;
  3689. this.t2z1=this.r2x*this.ay1-this.r2y*this.ax1;
  3690. this.l1x1=this.ax1*this.m1;
  3691. this.l1y1=this.ay1*this.m1;
  3692. this.l1z1=this.az1*this.m1;
  3693. this.l2x1=this.ax1*this.m2;
  3694. this.l2y1=this.ay1*this.m2;
  3695. this.l2z1=this.az1*this.m2;
  3696. this.a1x1=this.t1x1*this.i1e00+this.t1y1*this.i1e01+this.t1z1*this.i1e02;
  3697. this.a1y1=this.t1x1*this.i1e10+this.t1y1*this.i1e11+this.t1z1*this.i1e12;
  3698. this.a1z1=this.t1x1*this.i1e20+this.t1y1*this.i1e21+this.t1z1*this.i1e22;
  3699. this.a2x1=this.t2x1*this.i2e00+this.t2y1*this.i2e01+this.t2z1*this.i2e02;
  3700. this.a2y1=this.t2x1*this.i2e10+this.t2y1*this.i2e11+this.t2z1*this.i2e12;
  3701. this.a2z1=this.t2x1*this.i2e20+this.t2y1*this.i2e21+this.t2z1*this.i2e22;
  3702. this.t1x2=this.r1y*this.az2-this.r1z*this.ay2;
  3703. this.t1y2=this.r1z*this.ax2-this.r1x*this.az2;
  3704. this.t1z2=this.r1x*this.ay2-this.r1y*this.ax2;
  3705. this.t2x2=this.r2y*this.az2-this.r2z*this.ay2;
  3706. this.t2y2=this.r2z*this.ax2-this.r2x*this.az2;
  3707. this.t2z2=this.r2x*this.ay2-this.r2y*this.ax2;
  3708. this.l1x2=this.ax2*this.m1;
  3709. this.l1y2=this.ay2*this.m1;
  3710. this.l1z2=this.az2*this.m1;
  3711. this.l2x2=this.ax2*this.m2;
  3712. this.l2y2=this.ay2*this.m2;
  3713. this.l2z2=this.az2*this.m2;
  3714. this.a1x2=this.t1x2*this.i1e00+this.t1y2*this.i1e01+this.t1z2*this.i1e02;
  3715. this.a1y2=this.t1x2*this.i1e10+this.t1y2*this.i1e11+this.t1z2*this.i1e12;
  3716. this.a1z2=this.t1x2*this.i1e20+this.t1y2*this.i1e21+this.t1z2*this.i1e22;
  3717. this.a2x2=this.t2x2*this.i2e00+this.t2y2*this.i2e01+this.t2z2*this.i2e02;
  3718. this.a2y2=this.t2x2*this.i2e10+this.t2y2*this.i2e11+this.t2z2*this.i2e12;
  3719. this.a2z2=this.t2x2*this.i2e20+this.t2y2*this.i2e21+this.t2z2*this.i2e22;
  3720. this.t1x3=this.r1y*this.az3-this.r1z*this.ay3;
  3721. this.t1y3=this.r1z*this.ax3-this.r1x*this.az3;
  3722. this.t1z3=this.r1x*this.ay3-this.r1y*this.ax3;
  3723. this.t2x3=this.r2y*this.az3-this.r2z*this.ay3;
  3724. this.t2y3=this.r2z*this.ax3-this.r2x*this.az3;
  3725. this.t2z3=this.r2x*this.ay3-this.r2y*this.ax3;
  3726. this.l1x3=this.ax3*this.m1;
  3727. this.l1y3=this.ay3*this.m1;
  3728. this.l1z3=this.az3*this.m1;
  3729. this.l2x3=this.ax3*this.m2;
  3730. this.l2y3=this.ay3*this.m2;
  3731. this.l2z3=this.az3*this.m2;
  3732. this.a1x3=this.t1x3*this.i1e00+this.t1y3*this.i1e01+this.t1z3*this.i1e02;
  3733. this.a1y3=this.t1x3*this.i1e10+this.t1y3*this.i1e11+this.t1z3*this.i1e12;
  3734. this.a1z3=this.t1x3*this.i1e20+this.t1y3*this.i1e21+this.t1z3*this.i1e22;
  3735. this.a2x3=this.t2x3*this.i2e00+this.t2y3*this.i2e01+this.t2z3*this.i2e02;
  3736. this.a2y3=this.t2x3*this.i2e10+this.t2y3*this.i2e11+this.t2z3*this.i2e12;
  3737. this.a2z3=this.t2x3*this.i2e20+this.t2y3*this.i2e21+this.t2z3*this.i2e22;
  3738. var m12=this.m1+this.m2;
  3739. this.k00=(this.ax1*this.ax1+this.ay1*this.ay1+this.az1*this.az1)*m12;
  3740. this.k01=(this.ax1*this.ax2+this.ay1*this.ay2+this.az1*this.az2)*m12;
  3741. this.k02=(this.ax1*this.ax3+this.ay1*this.ay3+this.az1*this.az3)*m12;
  3742. this.k10=(this.ax2*this.ax1+this.ay2*this.ay1+this.az2*this.az1)*m12;
  3743. this.k11=(this.ax2*this.ax2+this.ay2*this.ay2+this.az2*this.az2)*m12;
  3744. this.k12=(this.ax2*this.ax3+this.ay2*this.ay3+this.az2*this.az3)*m12;
  3745. this.k20=(this.ax3*this.ax1+this.ay3*this.ay1+this.az3*this.az1)*m12;
  3746. this.k21=(this.ax3*this.ax2+this.ay3*this.ay2+this.az3*this.az2)*m12;
  3747. this.k22=(this.ax3*this.ax3+this.ay3*this.ay3+this.az3*this.az3)*m12;
  3748. this.k00+=this.t1x1*this.a1x1+this.t1y1*this.a1y1+this.t1z1*this.a1z1;
  3749. this.k01+=this.t1x1*this.a1x2+this.t1y1*this.a1y2+this.t1z1*this.a1z2;
  3750. this.k02+=this.t1x1*this.a1x3+this.t1y1*this.a1y3+this.t1z1*this.a1z3;
  3751. this.k10+=this.t1x2*this.a1x1+this.t1y2*this.a1y1+this.t1z2*this.a1z1;
  3752. this.k11+=this.t1x2*this.a1x2+this.t1y2*this.a1y2+this.t1z2*this.a1z2;
  3753. this.k12+=this.t1x2*this.a1x3+this.t1y2*this.a1y3+this.t1z2*this.a1z3;
  3754. this.k20+=this.t1x3*this.a1x1+this.t1y3*this.a1y1+this.t1z3*this.a1z1;
  3755. this.k21+=this.t1x3*this.a1x2+this.t1y3*this.a1y2+this.t1z3*this.a1z2;
  3756. this.k22+=this.t1x3*this.a1x3+this.t1y3*this.a1y3+this.t1z3*this.a1z3;
  3757. this.k00+=this.t2x1*this.a2x1+this.t2y1*this.a2y1+this.t2z1*this.a2z1;
  3758. this.k01+=this.t2x1*this.a2x2+this.t2y1*this.a2y2+this.t2z1*this.a2z2;
  3759. this.k02+=this.t2x1*this.a2x3+this.t2y1*this.a2y3+this.t2z1*this.a2z3;
  3760. this.k10+=this.t2x2*this.a2x1+this.t2y2*this.a2y1+this.t2z2*this.a2z1;
  3761. this.k11+=this.t2x2*this.a2x2+this.t2y2*this.a2y2+this.t2z2*this.a2z2;
  3762. this.k12+=this.t2x2*this.a2x3+this.t2y2*this.a2y3+this.t2z2*this.a2z3;
  3763. this.k20+=this.t2x3*this.a2x1+this.t2y3*this.a2y1+this.t2z3*this.a2z1;
  3764. this.k21+=this.t2x3*this.a2x2+this.t2y3*this.a2y2+this.t2z3*this.a2z2;
  3765. this.k22+=this.t2x3*this.a2x3+this.t2y3*this.a2y3+this.t2z3*this.a2z3;
  3766. this.kv00=this.k00;
  3767. this.kv11=this.k11;
  3768. this.kv22=this.k22;
  3769. this.dv00=1/this.kv00;
  3770. this.dv11=1/this.kv11;
  3771. this.dv22=1/this.kv22;
  3772. if(enableSpring1&&this.limitState1!=2){
  3773. var omega=6.2831853*frequency1;
  3774. var k=omega*omega*timeStep;
  3775. var dmp=invTimeStep/(k+2*this.limitMotor1.dampingRatio*omega);
  3776. this.cfm1=this.kv00*dmp;
  3777. this.limitVelocity1*=k*dmp;
  3778. }else{
  3779. this.cfm1=0;
  3780. this.limitVelocity1*=invTimeStep*0.05;
  3781. }
  3782. if(enableSpring2&&this.limitState2!=2){
  3783. omega=6.2831853*frequency2;
  3784. k=omega*omega*timeStep;
  3785. dmp=invTimeStep/(k+2*this.limitMotor2.dampingRatio*omega);
  3786. this.cfm2=this.kv11*dmp;
  3787. this.limitVelocity2*=k*dmp;
  3788. }else{
  3789. this.cfm2=0;
  3790. this.limitVelocity2*=invTimeStep*0.05;
  3791. }
  3792. if(enableSpring3&&this.limitState3!=2){
  3793. omega=6.2831853*frequency3;
  3794. k=omega*omega*timeStep;
  3795. dmp=invTimeStep/(k+2*this.limitMotor3.dampingRatio*omega);
  3796. this.cfm3=this.kv22*dmp;
  3797. this.limitVelocity3*=k*dmp;
  3798. }else{
  3799. this.cfm3=0;
  3800. this.limitVelocity3*=invTimeStep*0.05;
  3801. }
  3802. this.k00+=this.cfm1;
  3803. this.k11+=this.cfm2;
  3804. this.k22+=this.cfm3;
  3805. var inv=1/(
  3806. this.k00*(this.k11*this.k22-this.k21*this.k12)+
  3807. this.k10*(this.k21*this.k02-this.k01*this.k22)+
  3808. this.k20*(this.k01*this.k12-this.k11*this.k02)
  3809. );
  3810. this.d00=(this.k11*this.k22-this.k12*this.k21)*inv;
  3811. this.d01=(this.k02*this.k21-this.k01*this.k22)*inv;
  3812. this.d02=(this.k01*this.k12-this.k02*this.k11)*inv;
  3813. this.d10=(this.k12*this.k20-this.k10*this.k22)*inv;
  3814. this.d11=(this.k00*this.k22-this.k02*this.k20)*inv;
  3815. this.d12=(this.k02*this.k10-this.k00*this.k12)*inv;
  3816. this.d20=(this.k10*this.k21-this.k11*this.k20)*inv;
  3817. this.d21=(this.k01*this.k20-this.k00*this.k21)*inv;
  3818. this.d22=(this.k00*this.k11-this.k01*this.k10)*inv;
  3819. var totalImpulse1=this.limitImpulse1+this.motorImpulse1;
  3820. var totalImpulse2=this.limitImpulse2+this.motorImpulse2;
  3821. var totalImpulse3=this.limitImpulse3+this.motorImpulse3;
  3822. this.l1.x+=totalImpulse1*this.l1x1+totalImpulse2*this.l1x2+totalImpulse3*this.l1x3;
  3823. this.l1.y+=totalImpulse1*this.l1y1+totalImpulse2*this.l1y2+totalImpulse3*this.l1y3;
  3824. this.l1.z+=totalImpulse1*this.l1z1+totalImpulse2*this.l1z2+totalImpulse3*this.l1z3;
  3825. this.a1.x+=totalImpulse1*this.a1x1+totalImpulse2*this.a1x2+totalImpulse3*this.a1x3;
  3826. this.a1.y+=totalImpulse1*this.a1y1+totalImpulse2*this.a1y2+totalImpulse3*this.a1y3;
  3827. this.a1.z+=totalImpulse1*this.a1z1+totalImpulse2*this.a1z2+totalImpulse3*this.a1z3;
  3828. this.l2.x-=totalImpulse1*this.l2x1+totalImpulse2*this.l2x2+totalImpulse3*this.l2x3;
  3829. this.l2.y-=totalImpulse1*this.l2y1+totalImpulse2*this.l2y2+totalImpulse3*this.l2y3;
  3830. this.l2.z-=totalImpulse1*this.l2z1+totalImpulse2*this.l2z2+totalImpulse3*this.l2z3;
  3831. this.a2.x-=totalImpulse1*this.a2x1+totalImpulse2*this.a2x2+totalImpulse3*this.a2x3;
  3832. this.a2.y-=totalImpulse1*this.a2y1+totalImpulse2*this.a2y2+totalImpulse3*this.a2y3;
  3833. this.a2.z-=totalImpulse1*this.a2z1+totalImpulse2*this.a2z2+totalImpulse3*this.a2z3;
  3834. },
  3835. solve:function(){
  3836. 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;
  3837. 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;
  3838. 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;
  3839. var rvn1=rvx*this.ax1+rvy*this.ay1+rvz*this.az1;
  3840. var rvn2=rvx*this.ax2+rvy*this.ay2+rvz*this.az2;
  3841. var rvn3=rvx*this.ax3+rvy*this.ay3+rvz*this.az3;
  3842. var oldMotorImpulse1=this.motorImpulse1;
  3843. var oldMotorImpulse2=this.motorImpulse2;
  3844. var oldMotorImpulse3=this.motorImpulse3;
  3845. var dMotorImpulse1=0;
  3846. var dMotorImpulse2=0;
  3847. var dMotorImpulse3=0;
  3848. if(this.enableMotor1){
  3849. dMotorImpulse1=(rvn1-this.motorSpeed1)*this.dv00;
  3850. this.motorImpulse1+=dMotorImpulse1;
  3851. if(this.motorImpulse1>this.maxMotorImpulse1){
  3852. this.motorImpulse1=this.maxMotorImpulse1;
  3853. }else if(this.motorImpulse1<-this.maxMotorImpulse1){
  3854. this.motorImpulse1=-this.maxMotorImpulse1;
  3855. }
  3856. dMotorImpulse1=this.motorImpulse1-oldMotorImpulse1;
  3857. }
  3858. if(this.enableMotor2){
  3859. dMotorImpulse2=(rvn2-this.motorSpeed2)*this.dv11;
  3860. this.motorImpulse2+=dMotorImpulse2;
  3861. if(this.motorImpulse2>this.maxMotorImpulse2){
  3862. this.motorImpulse2=this.maxMotorImpulse2;
  3863. }else if(this.motorImpulse2<-this.maxMotorImpulse2){
  3864. this.motorImpulse2=-this.maxMotorImpulse2;
  3865. }
  3866. dMotorImpulse2=this.motorImpulse2-oldMotorImpulse2;
  3867. }
  3868. if(this.enableMotor3){
  3869. dMotorImpulse3=(rvn3-this.motorSpeed3)*this.dv22;
  3870. this.motorImpulse3+=dMotorImpulse3;
  3871. if(this.motorImpulse3>this.maxMotorImpulse3){
  3872. this.motorImpulse3=this.maxMotorImpulse3;
  3873. }else if(this.motorImpulse3<-this.maxMotorImpulse3){
  3874. this.motorImpulse3=-this.maxMotorImpulse3;
  3875. }
  3876. dMotorImpulse3=this.motorImpulse3-oldMotorImpulse3;
  3877. }
  3878. rvn1+=dMotorImpulse1*this.kv00+dMotorImpulse2*this.k01+dMotorImpulse3*this.k02;
  3879. rvn2+=dMotorImpulse1*this.k10+dMotorImpulse2*this.kv11+dMotorImpulse3*this.k12;
  3880. rvn3+=dMotorImpulse1*this.k20+dMotorImpulse2*this.k21+dMotorImpulse3*this.kv22;
  3881. rvn1-=this.limitVelocity1+this.limitImpulse1*this.cfm1;
  3882. rvn2-=this.limitVelocity2+this.limitImpulse2*this.cfm2;
  3883. rvn3-=this.limitVelocity3+this.limitImpulse3*this.cfm3;
  3884. var oldLimitImpulse1=this.limitImpulse1;
  3885. var oldLimitImpulse2=this.limitImpulse2;
  3886. var oldLimitImpulse3=this.limitImpulse3;
  3887. var dLimitImpulse1=rvn1*this.d00+rvn2*this.d01+rvn3*this.d02;
  3888. var dLimitImpulse2=rvn1*this.d10+rvn2*this.d11+rvn3*this.d12;
  3889. var dLimitImpulse3=rvn1*this.d20+rvn2*this.d21+rvn3*this.d22;
  3890. this.limitImpulse1+=dLimitImpulse1;
  3891. this.limitImpulse2+=dLimitImpulse2;
  3892. this.limitImpulse3+=dLimitImpulse3;
  3893. var clampState=0;
  3894. if(this.limitState1==2||this.limitImpulse1*this.limitState1<0){
  3895. dLimitImpulse1=-oldLimitImpulse1;
  3896. rvn2+=dLimitImpulse1*this.k10;
  3897. rvn3+=dLimitImpulse1*this.k20;
  3898. clampState|=1;
  3899. }
  3900. if(this.limitState2==2||this.limitImpulse2*this.limitState2<0){
  3901. dLimitImpulse2=-oldLimitImpulse2;
  3902. rvn1+=dLimitImpulse2*this.k01;
  3903. rvn3+=dLimitImpulse2*this.k21;
  3904. clampState|=2;
  3905. }
  3906. if(this.limitState3==2||this.limitImpulse3*this.limitState3<0){
  3907. dLimitImpulse3=-oldLimitImpulse3;
  3908. rvn1+=dLimitImpulse3*this.k02;
  3909. rvn2+=dLimitImpulse3*this.k12;
  3910. clampState|=4;
  3911. }
  3912. var det;
  3913. switch(clampState){
  3914. case 1:
  3915. det=1/(this.k11*this.k22-this.k12*this.k21);
  3916. dLimitImpulse2=(this.k22*rvn2+-this.k12*rvn3)*det;
  3917. dLimitImpulse3=(-this.k21*rvn2+this.k11*rvn3)*det;
  3918. break;
  3919. case 2:
  3920. det=1/(this.k00*this.k22-this.k02*this.k20);
  3921. dLimitImpulse1=(this.k22*rvn1+-this.k02*rvn3)*det;
  3922. dLimitImpulse3=(-this.k20*rvn1+this.k00*rvn3)*det;
  3923. break;
  3924. case 3:
  3925. dLimitImpulse3=rvn3/this.k22;
  3926. break;
  3927. case 4:
  3928. det=1/(this.k00*this.k11-this.k01*this.k10);
  3929. dLimitImpulse1=(this.k11*rvn1+-this.k01*rvn2)*det;
  3930. dLimitImpulse2=(-this.k10*rvn1+this.k00*rvn2)*det;
  3931. break;
  3932. case 5:
  3933. dLimitImpulse2=rvn2/this.k11;
  3934. break;
  3935. case 6:
  3936. dLimitImpulse1=rvn1/this.k00;
  3937. break;
  3938. }
  3939. this.limitImpulse1=oldLimitImpulse1+dLimitImpulse1;
  3940. this.limitImpulse2=oldLimitImpulse2+dLimitImpulse2;
  3941. this.limitImpulse3=oldLimitImpulse3+dLimitImpulse3;
  3942. var dImpulse1=dMotorImpulse1+dLimitImpulse1;
  3943. var dImpulse2=dMotorImpulse2+dLimitImpulse2;
  3944. var dImpulse3=dMotorImpulse3+dLimitImpulse3;
  3945. this.l1.x+=dImpulse1*this.l1x1+dImpulse2*this.l1x2+dImpulse3*this.l1x3;
  3946. this.l1.y+=dImpulse1*this.l1y1+dImpulse2*this.l1y2+dImpulse3*this.l1y3;
  3947. this.l1.z+=dImpulse1*this.l1z1+dImpulse2*this.l1z2+dImpulse3*this.l1z3;
  3948. this.a1.x+=dImpulse1*this.a1x1+dImpulse2*this.a1x2+dImpulse3*this.a1x3;
  3949. this.a1.y+=dImpulse1*this.a1y1+dImpulse2*this.a1y2+dImpulse3*this.a1y3;
  3950. this.a1.z+=dImpulse1*this.a1z1+dImpulse2*this.a1z2+dImpulse3*this.a1z3;
  3951. this.l2.x-=dImpulse1*this.l2x1+dImpulse2*this.l2x2+dImpulse3*this.l2x3;
  3952. this.l2.y-=dImpulse1*this.l2y1+dImpulse2*this.l2y2+dImpulse3*this.l2y3;
  3953. this.l2.z-=dImpulse1*this.l2z1+dImpulse2*this.l2z2+dImpulse3*this.l2z3;
  3954. this.a2.x-=dImpulse1*this.a2x1+dImpulse2*this.a2x2+dImpulse3*this.a2x3;
  3955. this.a2.y-=dImpulse1*this.a2y1+dImpulse2*this.a2y2+dImpulse3*this.a2y3;
  3956. this.a2.z-=dImpulse1*this.a2z1+dImpulse2*this.a2z2+dImpulse3*this.a2z3;
  3957. }
  3958. }
  3959. OIMO.TranslationalConstraint = function(joint,limitMotor){
  3960. this.cfm=NaN;
  3961. this.m1=NaN;
  3962. this.m2=NaN;
  3963. this.i1e00=NaN;
  3964. this.i1e01=NaN;
  3965. this.i1e02=NaN;
  3966. this.i1e10=NaN;
  3967. this.i1e11=NaN;
  3968. this.i1e12=NaN;
  3969. this.i1e20=NaN;
  3970. this.i1e21=NaN;
  3971. this.i1e22=NaN;
  3972. this.i2e00=NaN;
  3973. this.i2e01=NaN;
  3974. this.i2e02=NaN;
  3975. this.i2e10=NaN;
  3976. this.i2e11=NaN;
  3977. this.i2e12=NaN;
  3978. this.i2e20=NaN;
  3979. this.i2e21=NaN;
  3980. this.i2e22=NaN;
  3981. this.motorDenom=NaN;
  3982. this.invMotorDenom=NaN;
  3983. this.invDenom=NaN;
  3984. this.ax=NaN;
  3985. this.ay=NaN;
  3986. this.az=NaN;
  3987. this.r1x=NaN;
  3988. this.r1y=NaN;
  3989. this.r1z=NaN;
  3990. this.r2x=NaN;
  3991. this.r2y=NaN;
  3992. this.r2z=NaN;
  3993. this.t1x=NaN;
  3994. this.t1y=NaN;
  3995. this.t1z=NaN;
  3996. this.t2x=NaN;
  3997. this.t2y=NaN;
  3998. this.t2z=NaN;
  3999. this.l1x=NaN;
  4000. this.l1y=NaN;
  4001. this.l1z=NaN;
  4002. this.l2x=NaN;
  4003. this.l2y=NaN;
  4004. this.l2z=NaN;
  4005. this.a1x=NaN;
  4006. this.a1y=NaN;
  4007. this.a1z=NaN;
  4008. this.a2x=NaN;
  4009. this.a2y=NaN;
  4010. this.a2z=NaN;
  4011. this.lowerLimit=NaN;
  4012. this.upperLimit=NaN;
  4013. this.limitVelocity=NaN;
  4014. this.limitState=0;
  4015. this.enableMotor=false;
  4016. this.motorSpeed=NaN;
  4017. this.maxMotorForce=NaN;
  4018. this.maxMotorImpulse=NaN;
  4019. this.limitMotor=limitMotor;
  4020. this.b1=joint.body1;
  4021. this.b2=joint.body2;
  4022. this.p1=joint.anchorPoint1;
  4023. this.p2=joint.anchorPoint2;
  4024. this.r1=joint.relativeAnchorPoint1;
  4025. this.r2=joint.relativeAnchorPoint2;
  4026. this.l1=this.b1.linearVelocity;
  4027. this.l2=this.b2.linearVelocity;
  4028. this.a1=this.b1.angularVelocity;
  4029. this.a2=this.b2.angularVelocity;
  4030. this.i1=this.b1.inverseInertia;
  4031. this.i2=this.b2.inverseInertia;
  4032. this.limitImpulse=0;
  4033. this.motorImpulse=0;
  4034. }
  4035. OIMO.TranslationalConstraint.prototype = {
  4036. constructor: OIMO.TranslationalConstraint,
  4037. preSolve:function(timeStep,invTimeStep){
  4038. this.ax=this.limitMotor.axis.x;
  4039. this.ay=this.limitMotor.axis.y;
  4040. this.az=this.limitMotor.axis.z;
  4041. this.lowerLimit=this.limitMotor.lowerLimit;
  4042. this.upperLimit=this.limitMotor.upperLimit;
  4043. this.motorSpeed=this.limitMotor.motorSpeed;
  4044. this.maxMotorForce=this.limitMotor.maxMotorForce;
  4045. this.enableMotor=this.maxMotorForce>0;
  4046. this.m1=this.b1.inverseMass;
  4047. this.m2=this.b2.inverseMass;
  4048. var ti1 = this.i1.elements;
  4049. var ti2 = this.i2.elements;
  4050. this.i1e00=ti1[0];
  4051. this.i1e01=ti1[1];
  4052. this.i1e02=ti1[2];
  4053. this.i1e10=ti1[3];
  4054. this.i1e11=ti1[4];
  4055. this.i1e12=ti1[5];
  4056. this.i1e20=ti1[6];
  4057. this.i1e21=ti1[7];
  4058. this.i1e22=ti1[8];
  4059. this.i2e00=ti2[0];
  4060. this.i2e01=ti2[1];
  4061. this.i2e02=ti2[2];
  4062. this.i2e10=ti2[3];
  4063. this.i2e11=ti2[4];
  4064. this.i2e12=ti2[5];
  4065. this.i2e20=ti2[6];
  4066. this.i2e21=ti2[7];
  4067. this.i2e22=ti2[8];
  4068. var dx=this.p2.x-this.p1.x;
  4069. var dy=this.p2.y-this.p1.y;
  4070. var dz=this.p2.z-this.p1.z;
  4071. var d=dx*this.ax+dy*this.ay+dz*this.az;
  4072. var frequency=this.limitMotor.frequency;
  4073. var enableSpring=frequency>0;
  4074. var enableLimit=this.lowerLimit<=this.upperLimit;
  4075. if(enableSpring&&d>20||d<-20){
  4076. enableSpring=false;
  4077. }
  4078. if(enableLimit){
  4079. if(this.lowerLimit==this.upperLimit){
  4080. if(this.limitState!=0){
  4081. this.limitState=0;
  4082. this.limitImpulse=0;
  4083. }
  4084. this.limitVelocity=this.lowerLimit-d;
  4085. if(!enableSpring)d=this.lowerLimit;
  4086. }else if(d<this.lowerLimit){
  4087. if(this.limitState!=-1){
  4088. this.limitState=-1;
  4089. this.limitImpulse=0;
  4090. }
  4091. this.limitVelocity=this.lowerLimit-d;
  4092. if(!enableSpring)d=this.lowerLimit;
  4093. }else if(d>this.upperLimit){
  4094. if(this.limitState!=1){
  4095. this.limitState=1;
  4096. this.limitImpulse=0;
  4097. }
  4098. this.limitVelocity=this.upperLimit-d;
  4099. if(!enableSpring)d=this.upperLimit;
  4100. }else{
  4101. this.limitState=2;
  4102. this.limitImpulse=0;
  4103. this.limitVelocity=0;
  4104. }
  4105. if(!enableSpring){
  4106. if(this.limitVelocity>0.005)this.limitVelocity-=0.005;
  4107. else if(this.limitVelocity<-0.005)this.limitVelocity+=0.005;
  4108. else this.limitVelocity=0;
  4109. }
  4110. }else{
  4111. this.limitState=2;
  4112. this.limitImpulse=0;
  4113. }
  4114. if(this.enableMotor&&(this.limitState!=0||enableSpring)){
  4115. this.maxMotorImpulse=this.maxMotorForce*timeStep;
  4116. }else{
  4117. this.motorImpulse=0;
  4118. this.maxMotorImpulse=0;
  4119. }
  4120. var rdx=d*this.ax;
  4121. var rdy=d*this.ay;
  4122. var rdz=d*this.az;
  4123. var w1=this.m1/(this.m1+this.m2);
  4124. var w2=1-w1;
  4125. this.r1x=this.r1.x+rdx*w1;
  4126. this.r1y=this.r1.y+rdy*w1;
  4127. this.r1z=this.r1.z+rdz*w1;
  4128. this.r2x=this.r2.x-rdx*w2;
  4129. this.r2y=this.r2.y-rdy*w2;
  4130. this.r2z=this.r2.z-rdz*w2;
  4131. this.t1x=this.r1y*this.az-this.r1z*this.ay;
  4132. this.t1y=this.r1z*this.ax-this.r1x*this.az;
  4133. this.t1z=this.r1x*this.ay-this.r1y*this.ax;
  4134. this.t2x=this.r2y*this.az-this.r2z*this.ay;
  4135. this.t2y=this.r2z*this.ax-this.r2x*this.az;
  4136. this.t2z=this.r2x*this.ay-this.r2y*this.ax;
  4137. this.l1x=this.ax*this.m1;
  4138. this.l1y=this.ay*this.m1;
  4139. this.l1z=this.az*this.m1;
  4140. this.l2x=this.ax*this.m2;
  4141. this.l2y=this.ay*this.m2;
  4142. this.l2z=this.az*this.m2;
  4143. this.a1x=this.t1x*this.i1e00+this.t1y*this.i1e01+this.t1z*this.i1e02;
  4144. this.a1y=this.t1x*this.i1e10+this.t1y*this.i1e11+this.t1z*this.i1e12;
  4145. this.a1z=this.t1x*this.i1e20+this.t1y*this.i1e21+this.t1z*this.i1e22;
  4146. this.a2x=this.t2x*this.i2e00+this.t2y*this.i2e01+this.t2z*this.i2e02;
  4147. this.a2y=this.t2x*this.i2e10+this.t2y*this.i2e11+this.t2z*this.i2e12;
  4148. this.a2z=this.t2x*this.i2e20+this.t2y*this.i2e21+this.t2z*this.i2e22;
  4149. this.motorDenom=
  4150. this.m1+this.m2+
  4151. this.ax*(this.a1y*this.r1z-this.a1z*this.r1y+this.a2y*this.r2z-this.a2z*this.r2y)+
  4152. this.ay*(this.a1z*this.r1x-this.a1x*this.r1z+this.a2z*this.r2x-this.a2x*this.r2z)+
  4153. this.az*(this.a1x*this.r1y-this.a1y*this.r1x+this.a2x*this.r2y-this.a2y*this.r2x)
  4154. ;
  4155. this.invMotorDenom=1/this.motorDenom;
  4156. if(enableSpring&&this.limitState!=2){
  4157. var omega=6.2831853*frequency;
  4158. var k=omega*omega*timeStep;
  4159. var dmp=invTimeStep/(k+2*this.limitMotor.dampingRatio*omega);
  4160. this.cfm=this.motorDenom*dmp;
  4161. this.limitVelocity*=k*dmp;
  4162. }else{
  4163. this.cfm=0;
  4164. this.limitVelocity*=invTimeStep*0.05;
  4165. }
  4166. this.invDenom=1/(this.motorDenom+this.cfm);
  4167. var totalImpulse=this.limitImpulse+this.motorImpulse;
  4168. this.l1.x+=totalImpulse*this.l1x;
  4169. this.l1.y+=totalImpulse*this.l1y;
  4170. this.l1.z+=totalImpulse*this.l1z;
  4171. this.a1.x+=totalImpulse*this.a1x;
  4172. this.a1.y+=totalImpulse*this.a1y;
  4173. this.a1.z+=totalImpulse*this.a1z;
  4174. this.l2.x-=totalImpulse*this.l2x;
  4175. this.l2.y-=totalImpulse*this.l2y;
  4176. this.l2.z-=totalImpulse*this.l2z;
  4177. this.a2.x-=totalImpulse*this.a2x;
  4178. this.a2.y-=totalImpulse*this.a2y;
  4179. this.a2.z-=totalImpulse*this.a2z;
  4180. },
  4181. solve:function(){
  4182. var rvn=
  4183. this.ax*(this.l2.x-this.l1.x)+this.ay*(this.l2.y-this.l1.y)+this.az*(this.l2.z-this.l1.z)+
  4184. 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
  4185. ;
  4186. var newMotorImpulse;
  4187. if(this.enableMotor){
  4188. newMotorImpulse=(rvn-this.motorSpeed)*this.invMotorDenom;
  4189. var oldMotorImpulse=this.motorImpulse;
  4190. this.motorImpulse+=newMotorImpulse;
  4191. if(this.motorImpulse>this.maxMotorImpulse)this.motorImpulse=this.maxMotorImpulse;
  4192. else if(this.motorImpulse<-this.maxMotorImpulse)this.motorImpulse=-this.maxMotorImpulse;
  4193. newMotorImpulse=this.motorImpulse-oldMotorImpulse;
  4194. rvn-=newMotorImpulse*this.motorDenom;
  4195. }else newMotorImpulse=0;
  4196. var newLimitImpulse;
  4197. if(this.limitState!=2){
  4198. newLimitImpulse=(rvn-this.limitVelocity-this.limitImpulse*this.cfm)*this.invDenom;
  4199. var oldLimitImpulse=this.limitImpulse;
  4200. this.limitImpulse+=newLimitImpulse;
  4201. if(this.limitImpulse*this.limitState<0)this.limitImpulse=0;
  4202. newLimitImpulse=this.limitImpulse-oldLimitImpulse;
  4203. }else newLimitImpulse=0;
  4204. var totalImpulse=newLimitImpulse+newMotorImpulse;
  4205. this.l1.x+=totalImpulse*this.l1x;
  4206. this.l1.y+=totalImpulse*this.l1y;
  4207. this.l1.z+=totalImpulse*this.l1z;
  4208. this.a1.x+=totalImpulse*this.a1x;
  4209. this.a1.y+=totalImpulse*this.a1y;
  4210. this.a1.z+=totalImpulse*this.a1z;
  4211. this.l2.x-=totalImpulse*this.l2x;
  4212. this.l2.y-=totalImpulse*this.l2y;
  4213. this.l2.z-=totalImpulse*this.l2z;
  4214. this.a2.x-=totalImpulse*this.a2x;
  4215. this.a2.y-=totalImpulse*this.a2y;
  4216. this.a2.z-=totalImpulse*this.a2z;
  4217. }
  4218. }
  4219. OIMO.Contact = function(){
  4220. this.shape1=null;
  4221. this.shape2=null;
  4222. this.body1=null;
  4223. this.body2=null;
  4224. this.prev=null;
  4225. this.next=null;
  4226. this.persisting=false;
  4227. this.sleeping=false;
  4228. this.detector=null;
  4229. this.constraint=null;
  4230. this.touching=false;
  4231. this.b1Link=new OIMO.ContactLink(this);
  4232. this.b2Link=new OIMO.ContactLink(this);
  4233. this.s1Link=new OIMO.ContactLink(this);
  4234. this.s2Link=new OIMO.ContactLink(this);
  4235. this.manifold=new OIMO.ContactManifold();
  4236. this.buffer=[];// vector 4
  4237. this.buffer.length = 4;
  4238. this.buffer[0]=new OIMO.ImpulseDataBuffer();
  4239. this.buffer[1]=new OIMO.ImpulseDataBuffer();
  4240. this.buffer[2]=new OIMO.ImpulseDataBuffer();
  4241. this.buffer[3]=new OIMO.ImpulseDataBuffer();
  4242. this.points=this.manifold.points;
  4243. this.constraint=new OIMO.ContactConstraint(this.manifold);
  4244. }
  4245. OIMO.Contact.prototype = {
  4246. constructor: OIMO.Contact,
  4247. mixRestitution:function(restitution1,restitution2){
  4248. return Math.sqrt(restitution1*restitution2);
  4249. },
  4250. mixFriction:function(friction1,friction2){
  4251. return Math.sqrt(friction1*friction2);
  4252. },
  4253. updateManifold:function(){
  4254. this.constraint.restitution=this.mixRestitution(this.shape1.restitution,this.shape2.restitution);
  4255. this.constraint.friction=this.mixFriction(this.shape1.friction,this.shape2.friction);
  4256. var numBuffers=this.manifold.numPoints;
  4257. for(var i=0;i<numBuffers;i++){
  4258. var b=this.buffer[i];
  4259. var p=this.points[i];
  4260. b.lp1X=p.localPoint1.x;
  4261. b.lp1Y=p.localPoint1.y;
  4262. b.lp1Z=p.localPoint1.z;
  4263. b.lp2X=p.localPoint2.x;
  4264. b.lp2Y=p.localPoint2.y;
  4265. b.lp2Z=p.localPoint2.z;
  4266. b.impulse=p.normalImpulse;
  4267. }
  4268. this.manifold.numPoints=0;
  4269. this.detector.detectCollision(this.shape1,this.shape2,this.manifold);
  4270. var num=this.manifold.numPoints;
  4271. if(num==0){
  4272. this.touching=false;
  4273. return;
  4274. }
  4275. this.touching=true;
  4276. for(i=0; i<num; i++){
  4277. p=this.points[i];
  4278. var lp1x=p.localPoint1.x;
  4279. var lp1y=p.localPoint1.y;
  4280. var lp1z=p.localPoint1.z;
  4281. var lp2x=p.localPoint2.x;
  4282. var lp2y=p.localPoint2.y;
  4283. var lp2z=p.localPoint2.z;
  4284. var index=-1;
  4285. var minDistance=0.0004;
  4286. for(var j=0;j<numBuffers;j++){
  4287. b=this.buffer[j];
  4288. var dx=b.lp1X-lp1x;
  4289. var dy=b.lp1Y-lp1y;
  4290. var dz=b.lp1Z-lp1z;
  4291. var distance1=dx*dx+dy*dy+dz*dz;
  4292. dx=b.lp2X-lp2x;
  4293. dy=b.lp2Y-lp2y;
  4294. dz=b.lp2Z-lp2z;
  4295. var distance2=dx*dx+dy*dy+dz*dz;
  4296. if(distance1<distance2){
  4297. if(distance1<minDistance){
  4298. minDistance=distance1;
  4299. index=j;
  4300. }
  4301. }else{
  4302. if(distance2<minDistance){
  4303. minDistance=distance2;
  4304. index=j;
  4305. }
  4306. }
  4307. }
  4308. if(index!=-1){
  4309. var tmp=this.buffer[index];
  4310. this.buffer[index]=this.buffer[--numBuffers];
  4311. this.buffer[numBuffers]=tmp;
  4312. p.normalImpulse=tmp.impulse;
  4313. p.warmStarted=true;
  4314. }else{
  4315. p.normalImpulse=0;
  4316. p.warmStarted=false;
  4317. }
  4318. }
  4319. },
  4320. attach:function(shape1,shape2){
  4321. this.shape1=shape1;
  4322. this.shape2=shape2;
  4323. this.body1=shape1.parent;
  4324. this.body2=shape2.parent;
  4325. this.manifold.body1=this.body1;
  4326. this.manifold.body2=this.body2;
  4327. this.constraint.body1=this.body1;
  4328. this.constraint.body2=this.body2;
  4329. this.constraint.attach();
  4330. this.s1Link.shape=shape2;
  4331. this.s1Link.body=this.body2;
  4332. this.s2Link.shape=shape1;
  4333. this.s2Link.body=this.body1;
  4334. if(shape1.contactLink!=null)(this.s1Link.next=shape1.contactLink).prev=this.s1Link;
  4335. else this.s1Link.next=null;
  4336. shape1.contactLink=this.s1Link;
  4337. shape1.numContacts++;
  4338. if(shape2.contactLink!=null)(this.s2Link.next=shape2.contactLink).prev=this.s2Link;
  4339. else this.s2Link.next=null;
  4340. shape2.contactLink=this.s2Link;
  4341. shape2.numContacts++;
  4342. this.b1Link.shape=shape2;
  4343. this.b1Link.body=this.body2;
  4344. this.b2Link.shape=shape1;
  4345. this.b2Link.body=this.body1;
  4346. if(this.body1.contactLink!=null)(this.b1Link.next=this.body1.contactLink).prev=this.b1Link;
  4347. else this.b1Link.next=null;
  4348. this.body1.contactLink=this.b1Link;
  4349. this.body1.numContacts++;
  4350. if(this.body2.contactLink!=null)(this.b2Link.next=this.body2.contactLink).prev=this.b2Link;
  4351. else this.b2Link.next=null;
  4352. this.body2.contactLink=this.b2Link;
  4353. this.body2.numContacts++;
  4354. this.prev=null;
  4355. this.next=null;
  4356. this.persisting=true;
  4357. this.sleeping=this.body1.sleeping&&this.body2.sleeping;
  4358. this.manifold.numPoints=0;
  4359. },
  4360. detach:function(){
  4361. var prev=this.s1Link.prev;
  4362. var next=this.s1Link.next;
  4363. if(prev!=null)prev.next=next;
  4364. if(next!=null)next.prev=prev;
  4365. if(this.shape1.contactLink==this.s1Link)this.shape1.contactLink=next;
  4366. this.s1Link.prev=null;
  4367. this.s1Link.next=null;
  4368. this.s1Link.shape=null;
  4369. this.s1Link.body=null;
  4370. this.shape1.numContacts--;
  4371. prev=this.s2Link.prev;
  4372. next=this.s2Link.next;
  4373. if(prev!=null)prev.next=next;
  4374. if(next!=null)next.prev=prev;
  4375. if(this.shape2.contactLink==this.s2Link)this.shape2.contactLink=next;
  4376. this.s2Link.prev=null;
  4377. this.s2Link.next=null;
  4378. this.s2Link.shape=null;
  4379. this.s2Link.body=null;
  4380. this.shape2.numContacts--;
  4381. prev=this.b1Link.prev;
  4382. next=this.b1Link.next;
  4383. if(prev!=null)prev.next=next;
  4384. if(next!=null)next.prev=prev;
  4385. if(this.body1.contactLink==this.b1Link)this.body1.contactLink=next;
  4386. this.b1Link.prev=null;
  4387. this.b1Link.next=null;
  4388. this.b1Link.shape=null;
  4389. this.b1Link.body=null;
  4390. this.body1.numContacts--;
  4391. prev=this.b2Link.prev;
  4392. next=this.b2Link.next;
  4393. if(prev!=null)prev.next=next;
  4394. if(next!=null)next.prev=prev;
  4395. if(this.body2.contactLink==this.b2Link)this.body2.contactLink=next;
  4396. this.b2Link.prev=null;
  4397. this.b2Link.next=null;
  4398. this.b2Link.shape=null;
  4399. this.b2Link.body=null;
  4400. this.body2.numContacts--;
  4401. this.manifold.body1=null;
  4402. this.manifold.body2=null;
  4403. this.constraint.body1=null;
  4404. this.constraint.body2=null;
  4405. this.constraint.detach();
  4406. this.shape1=null;
  4407. this.shape2=null;
  4408. this.body1=null;
  4409. this.body2=null;
  4410. }
  4411. }
  4412. OIMO.ContactConstraint = function(manifold){
  4413. OIMO.Constraint.call( this);
  4414. this.restitution=NaN;
  4415. this.friction=NaN;
  4416. this.p1=null;
  4417. this.p2=null;
  4418. this.lv1=null;
  4419. this.lv2=null;
  4420. this.av1=null;
  4421. this.av2=null;
  4422. this.i1=null;
  4423. this.i2=null;
  4424. this.i1e00=NaN;
  4425. this.i1e01=NaN;
  4426. this.i1e02=NaN;
  4427. this.i1e10=NaN;
  4428. this.i1e11=NaN;
  4429. this.i1e12=NaN;
  4430. this.i1e20=NaN;
  4431. this.i1e21=NaN;
  4432. this.i1e22=NaN;
  4433. this.i2e00=NaN;
  4434. this.i2e01=NaN;
  4435. this.i2e02=NaN;
  4436. this.i2e10=NaN;
  4437. this.i2e11=NaN;
  4438. this.i2e12=NaN;
  4439. this.i2e20=NaN;
  4440. this.i2e21=NaN;
  4441. this.i2e22=NaN;
  4442. this.m1=NaN;
  4443. this.m2=NaN;
  4444. this.num=0;
  4445. this.manifold=manifold;
  4446. this.ps=manifold.points;
  4447. this.cs=new OIMO.ContactPointDataBuffer();
  4448. this.cs.next=new OIMO.ContactPointDataBuffer();
  4449. this.cs.next.next=new OIMO.ContactPointDataBuffer();
  4450. this.cs.next.next.next=new OIMO.ContactPointDataBuffer();
  4451. }
  4452. OIMO.ContactConstraint.prototype = Object.create( OIMO.Constraint.prototype );
  4453. OIMO.ContactConstraint.prototype.attach = function(){
  4454. this.p1=this.body1.position;
  4455. this.p2=this.body2.position;
  4456. this.lv1=this.body1.linearVelocity;
  4457. this.av1=this.body1.angularVelocity;
  4458. this.lv2=this.body2.linearVelocity;
  4459. this.av2=this.body2.angularVelocity;
  4460. this.i1=this.body1.inverseInertia;
  4461. this.i2=this.body2.inverseInertia;
  4462. }
  4463. OIMO.ContactConstraint.prototype.detach = function(){
  4464. this.p1=null;
  4465. this.p2=null;
  4466. this.lv1=null;
  4467. this.lv2=null;
  4468. this.av1=null;
  4469. this.av2=null;
  4470. this.i1=null;
  4471. this.i2=null;
  4472. }
  4473. OIMO.ContactConstraint.prototype.preSolve = function(timeStep,invTimeStep){
  4474. this.m1=this.body1.inverseMass;
  4475. this.m2=this.body2.inverseMass;
  4476. var ti1 = this.i1.elements;
  4477. var ti2 = this.i2.elements;
  4478. this.i1e00=ti1[0];
  4479. this.i1e01=ti1[1];
  4480. this.i1e02=ti1[2];
  4481. this.i1e10=ti1[3];
  4482. this.i1e11=ti1[4];
  4483. this.i1e12=ti1[5];
  4484. this.i1e20=ti1[6];
  4485. this.i1e21=ti1[7];
  4486. this.i1e22=ti1[8];
  4487. this.i2e00=ti2[0];
  4488. this.i2e01=ti2[1];
  4489. this.i2e02=ti2[2];
  4490. this.i2e10=ti2[3];
  4491. this.i2e11=ti2[4];
  4492. this.i2e12=ti2[5];
  4493. this.i2e20=ti2[6];
  4494. this.i2e21=ti2[7];
  4495. this.i2e22=ti2[8];
  4496. var p1x=this.p1.x;
  4497. var p1y=this.p1.y;
  4498. var p1z=this.p1.z;
  4499. var p2x=this.p2.x;
  4500. var p2y=this.p2.y;
  4501. var p2z=this.p2.z;
  4502. var m1m2=this.m1+this.m2;
  4503. this.num=this.manifold.numPoints;
  4504. var c=this.cs;
  4505. for(var i=0;i<this.num;i++){
  4506. var p=this.ps[i];
  4507. var tmp1X;
  4508. var tmp1Y;
  4509. var tmp1Z;
  4510. var tmp2X;
  4511. var tmp2Y;
  4512. var tmp2Z;
  4513. tmp1X=p.position.x;
  4514. tmp1Y=p.position.y;
  4515. tmp1Z=p.position.z;
  4516. var rp1X=tmp1X-p1x;
  4517. var rp1Y=tmp1Y-p1y;
  4518. var rp1Z=tmp1Z-p1z;
  4519. var rp2X=tmp1X-p2x;
  4520. var rp2Y=tmp1Y-p2y;
  4521. var rp2Z=tmp1Z-p2z;
  4522. c.rp1X=rp1X;
  4523. c.rp1Y=rp1Y;
  4524. c.rp1Z=rp1Z;
  4525. c.rp2X=rp2X;
  4526. c.rp2Y=rp2Y;
  4527. c.rp2Z=rp2Z;
  4528. c.norImp=p.normalImpulse;
  4529. c.tanImp=p.tangentImpulse;
  4530. c.binImp=p.binormalImpulse;
  4531. var norX=p.normal.x;
  4532. var norY=p.normal.y;
  4533. var norZ=p.normal.z;
  4534. var rvX=(this.lv2.x+this.av2.y*rp2Z-this.av2.z*rp2Y)-(this.lv1.x+this.av1.y*rp1Z-this.av1.z*rp1Y);
  4535. var rvY=(this.lv2.y+this.av2.z*rp2X-this.av2.x*rp2Z)-(this.lv1.y+this.av1.z*rp1X-this.av1.x*rp1Z);
  4536. var rvZ=(this.lv2.z+this.av2.x*rp2Y-this.av2.y*rp2X)-(this.lv1.z+this.av1.x*rp1Y-this.av1.y*rp1X);
  4537. var rvn=norX*rvX+norY*rvY+norZ*rvZ;
  4538. var tanX=rvX-rvn*norX;
  4539. var tanY=rvY-rvn*norY;
  4540. var tanZ=rvZ-rvn*norZ;
  4541. var len=tanX*tanX+tanY*tanY+tanZ*tanZ;
  4542. if(len>0.04){
  4543. len=1/Math.sqrt(len);
  4544. }else{
  4545. tanX=norY*norX-norZ*norZ;
  4546. tanY=-norZ*norY-norX*norX;
  4547. tanZ=norX*norZ+norY*norY;
  4548. len=1/Math.sqrt(tanX*tanX+tanY*tanY+tanZ*tanZ);
  4549. }
  4550. tanX*=len;
  4551. tanY*=len;
  4552. tanZ*=len;
  4553. var binX=norY*tanZ-norZ*tanY;
  4554. var binY=norZ*tanX-norX*tanZ;
  4555. var binZ=norX*tanY-norY*tanX;
  4556. c.norX=norX;
  4557. c.norY=norY;
  4558. c.norZ=norZ;
  4559. c.tanX=tanX;
  4560. c.tanY=tanY;
  4561. c.tanZ=tanZ;
  4562. c.binX=binX;
  4563. c.binY=binY;
  4564. c.binZ=binZ;
  4565. c.norU1X=norX*this.m1;
  4566. c.norU1Y=norY*this.m1;
  4567. c.norU1Z=norZ*this.m1;
  4568. c.norU2X=norX*this.m2;
  4569. c.norU2Y=norY*this.m2;
  4570. c.norU2Z=norZ*this.m2;
  4571. c.tanU1X=tanX*this.m1;
  4572. c.tanU1Y=tanY*this.m1;
  4573. c.tanU1Z=tanZ*this.m1;
  4574. c.tanU2X=tanX*this.m2;
  4575. c.tanU2Y=tanY*this.m2;
  4576. c.tanU2Z=tanZ*this.m2;
  4577. c.binU1X=binX*this.m1;
  4578. c.binU1Y=binY*this.m1;
  4579. c.binU1Z=binZ*this.m1;
  4580. c.binU2X=binX*this.m2;
  4581. c.binU2Y=binY*this.m2;
  4582. c.binU2Z=binZ*this.m2;
  4583. var norT1X=rp1Y*norZ-rp1Z*norY;
  4584. var norT1Y=rp1Z*norX-rp1X*norZ;
  4585. var norT1Z=rp1X*norY-rp1Y*norX;
  4586. var norT2X=rp2Y*norZ-rp2Z*norY;
  4587. var norT2Y=rp2Z*norX-rp2X*norZ;
  4588. var norT2Z=rp2X*norY-rp2Y*norX;
  4589. var tanT1X=rp1Y*tanZ-rp1Z*tanY;
  4590. var tanT1Y=rp1Z*tanX-rp1X*tanZ;
  4591. var tanT1Z=rp1X*tanY-rp1Y*tanX;
  4592. var tanT2X=rp2Y*tanZ-rp2Z*tanY;
  4593. var tanT2Y=rp2Z*tanX-rp2X*tanZ;
  4594. var tanT2Z=rp2X*tanY-rp2Y*tanX;
  4595. var binT1X=rp1Y*binZ-rp1Z*binY;
  4596. var binT1Y=rp1Z*binX-rp1X*binZ;
  4597. var binT1Z=rp1X*binY-rp1Y*binX;
  4598. var binT2X=rp2Y*binZ-rp2Z*binY;
  4599. var binT2Y=rp2Z*binX-rp2X*binZ;
  4600. var binT2Z=rp2X*binY-rp2Y*binX;
  4601. var norTU1X=norT1X*this.i1e00+norT1Y*this.i1e01+norT1Z*this.i1e02;
  4602. var norTU1Y=norT1X*this.i1e10+norT1Y*this.i1e11+norT1Z*this.i1e12;
  4603. var norTU1Z=norT1X*this.i1e20+norT1Y*this.i1e21+norT1Z*this.i1e22;
  4604. var norTU2X=norT2X*this.i2e00+norT2Y*this.i2e01+norT2Z*this.i2e02;
  4605. var norTU2Y=norT2X*this.i2e10+norT2Y*this.i2e11+norT2Z*this.i2e12;
  4606. var norTU2Z=norT2X*this.i2e20+norT2Y*this.i2e21+norT2Z*this.i2e22;
  4607. var tanTU1X=tanT1X*this.i1e00+tanT1Y*this.i1e01+tanT1Z*this.i1e02;
  4608. var tanTU1Y=tanT1X*this.i1e10+tanT1Y*this.i1e11+tanT1Z*this.i1e12;
  4609. var tanTU1Z=tanT1X*this.i1e20+tanT1Y*this.i1e21+tanT1Z*this.i1e22;
  4610. var tanTU2X=tanT2X*this.i2e00+tanT2Y*this.i2e01+tanT2Z*this.i2e02;
  4611. var tanTU2Y=tanT2X*this.i2e10+tanT2Y*this.i2e11+tanT2Z*this.i2e12;
  4612. var tanTU2Z=tanT2X*this.i2e20+tanT2Y*this.i2e21+tanT2Z*this.i2e22;
  4613. var binTU1X=binT1X*this.i1e00+binT1Y*this.i1e01+binT1Z*this.i1e02;
  4614. var binTU1Y=binT1X*this.i1e10+binT1Y*this.i1e11+binT1Z*this.i1e12;
  4615. var binTU1Z=binT1X*this.i1e20+binT1Y*this.i1e21+binT1Z*this.i1e22;
  4616. var binTU2X=binT2X*this.i2e00+binT2Y*this.i2e01+binT2Z*this.i2e02;
  4617. var binTU2Y=binT2X*this.i2e10+binT2Y*this.i2e11+binT2Z*this.i2e12;
  4618. var binTU2Z=binT2X*this.i2e20+binT2Y*this.i2e21+binT2Z*this.i2e22;
  4619. c.norT1X=norT1X;
  4620. c.norT1Y=norT1Y;
  4621. c.norT1Z=norT1Z;
  4622. c.tanT1X=tanT1X;
  4623. c.tanT1Y=tanT1Y;
  4624. c.tanT1Z=tanT1Z;
  4625. c.binT1X=binT1X;
  4626. c.binT1Y=binT1Y;
  4627. c.binT1Z=binT1Z;
  4628. c.norT2X=norT2X;
  4629. c.norT2Y=norT2Y;
  4630. c.norT2Z=norT2Z;
  4631. c.tanT2X=tanT2X;
  4632. c.tanT2Y=tanT2Y;
  4633. c.tanT2Z=tanT2Z;
  4634. c.binT2X=binT2X;
  4635. c.binT2Y=binT2Y;
  4636. c.binT2Z=binT2Z;
  4637. c.norTU1X=norTU1X;
  4638. c.norTU1Y=norTU1Y;
  4639. c.norTU1Z=norTU1Z;
  4640. c.tanTU1X=tanTU1X;
  4641. c.tanTU1Y=tanTU1Y;
  4642. c.tanTU1Z=tanTU1Z;
  4643. c.binTU1X=binTU1X;
  4644. c.binTU1Y=binTU1Y;
  4645. c.binTU1Z=binTU1Z;
  4646. c.norTU2X=norTU2X;
  4647. c.norTU2Y=norTU2Y;
  4648. c.norTU2Z=norTU2Z;
  4649. c.tanTU2X=tanTU2X;
  4650. c.tanTU2Y=tanTU2Y;
  4651. c.tanTU2Z=tanTU2Z;
  4652. c.binTU2X=binTU2X;
  4653. c.binTU2Y=binTU2Y;
  4654. c.binTU2Z=binTU2Z;
  4655. tmp1X=norT1X*this.i1e00+norT1Y*this.i1e01+norT1Z*this.i1e02;
  4656. tmp1Y=norT1X*this.i1e10+norT1Y*this.i1e11+norT1Z*this.i1e12;
  4657. tmp1Z=norT1X*this.i1e20+norT1Y*this.i1e21+norT1Z*this.i1e22;
  4658. tmp2X=tmp1Y*rp1Z-tmp1Z*rp1Y;
  4659. tmp2Y=tmp1Z*rp1X-tmp1X*rp1Z;
  4660. tmp2Z=tmp1X*rp1Y-tmp1Y*rp1X;
  4661. tmp1X=norT2X*this.i2e00+norT2Y*this.i2e01+norT2Z*this.i2e02;
  4662. tmp1Y=norT2X*this.i2e10+norT2Y*this.i2e11+norT2Z*this.i2e12;
  4663. tmp1Z=norT2X*this.i2e20+norT2Y*this.i2e21+norT2Z*this.i2e22;
  4664. tmp2X+=tmp1Y*rp2Z-tmp1Z*rp2Y;
  4665. tmp2Y+=tmp1Z*rp2X-tmp1X*rp2Z;
  4666. tmp2Z+=tmp1X*rp2Y-tmp1Y*rp2X;
  4667. var norDen=1/(m1m2+norX*tmp2X+norY*tmp2Y+norZ*tmp2Z);
  4668. tmp1X=tanT1X*this.i1e00+tanT1Y*this.i1e01+tanT1Z*this.i1e02;
  4669. tmp1Y=tanT1X*this.i1e10+tanT1Y*this.i1e11+tanT1Z*this.i1e12;
  4670. tmp1Z=tanT1X*this.i1e20+tanT1Y*this.i1e21+tanT1Z*this.i1e22;
  4671. tmp2X=tmp1Y*rp1Z-tmp1Z*rp1Y;
  4672. tmp2Y=tmp1Z*rp1X-tmp1X*rp1Z;
  4673. tmp2Z=tmp1X*rp1Y-tmp1Y*rp1X;
  4674. tmp1X=tanT2X*this.i2e00+tanT2Y*this.i2e01+tanT2Z*this.i2e02;
  4675. tmp1Y=tanT2X*this.i2e10+tanT2Y*this.i2e11+tanT2Z*this.i2e12;
  4676. tmp1Z=tanT2X*this.i2e20+tanT2Y*this.i2e21+tanT2Z*this.i2e22;
  4677. tmp2X+=tmp1Y*rp2Z-tmp1Z*rp2Y;
  4678. tmp2Y+=tmp1Z*rp2X-tmp1X*rp2Z;
  4679. tmp2Z+=tmp1X*rp2Y-tmp1Y*rp2X;
  4680. var tanDen=1/(m1m2+tanX*tmp2X+tanY*tmp2Y+tanZ*tmp2Z);
  4681. tmp1X=binT1X*this.i1e00+binT1Y*this.i1e01+binT1Z*this.i1e02;
  4682. tmp1Y=binT1X*this.i1e10+binT1Y*this.i1e11+binT1Z*this.i1e12;
  4683. tmp1Z=binT1X*this.i1e20+binT1Y*this.i1e21+binT1Z*this.i1e22;
  4684. tmp2X=tmp1Y*rp1Z-tmp1Z*rp1Y;
  4685. tmp2Y=tmp1Z*rp1X-tmp1X*rp1Z;
  4686. tmp2Z=tmp1X*rp1Y-tmp1Y*rp1X;
  4687. tmp1X=binT2X*this.i2e00+binT2Y*this.i2e01+binT2Z*this.i2e02;
  4688. tmp1Y=binT2X*this.i2e10+binT2Y*this.i2e11+binT2Z*this.i2e12;
  4689. tmp1Z=binT2X*this.i2e20+binT2Y*this.i2e21+binT2Z*this.i2e22;
  4690. tmp2X+=tmp1Y*rp2Z-tmp1Z*rp2Y;
  4691. tmp2Y+=tmp1Z*rp2X-tmp1X*rp2Z;
  4692. tmp2Z+=tmp1X*rp2Y-tmp1Y*rp2X;
  4693. var binDen=1/(m1m2+binX*tmp2X+binY*tmp2Y+binZ*tmp2Z);
  4694. c.norDen=norDen;
  4695. c.tanDen=tanDen;
  4696. c.binDen=binDen;
  4697. if(p.warmStarted){
  4698. var norImp=p.normalImpulse;
  4699. this.lv1.x+=c.norU1X*norImp;
  4700. this.lv1.y+=c.norU1Y*norImp;
  4701. this.lv1.z+=c.norU1Z*norImp;
  4702. this.av1.x+=norTU1X*norImp;
  4703. this.av1.y+=norTU1Y*norImp;
  4704. this.av1.z+=norTU1Z*norImp;
  4705. this.lv2.x-=c.norU2X*norImp;
  4706. this.lv2.y-=c.norU2Y*norImp;
  4707. this.lv2.z-=c.norU2Z*norImp;
  4708. this.av2.x-=norTU2X*norImp;
  4709. this.av2.y-=norTU2Y*norImp;
  4710. this.av2.z-=norTU2Z*norImp;
  4711. c.norImp=norImp;
  4712. c.tanImp=0;
  4713. c.binImp=0;
  4714. rvn=0;
  4715. }else{
  4716. c.norImp=0;
  4717. c.tanImp=0;
  4718. c.binImp=0;
  4719. }
  4720. if(rvn>-1){
  4721. rvn=0;
  4722. }
  4723. var norTar=this.restitution*-rvn;
  4724. var sepV=-(p.penetration+0.005)*invTimeStep*0.05;
  4725. if(norTar<sepV)norTar=sepV;
  4726. c.norTar=norTar;
  4727. c.last=i==this.num-1;
  4728. c=c.next;
  4729. }
  4730. }
  4731. OIMO.ContactConstraint.prototype.solve = function(){
  4732. var lv1x=this.lv1.x;
  4733. var lv1y=this.lv1.y;
  4734. var lv1z=this.lv1.z;
  4735. var lv2x=this.lv2.x;
  4736. var lv2y=this.lv2.y;
  4737. var lv2z=this.lv2.z;
  4738. var av1x=this.av1.x;
  4739. var av1y=this.av1.y;
  4740. var av1z=this.av1.z;
  4741. var av2x=this.av2.x;
  4742. var av2y=this.av2.y;
  4743. var av2z=this.av2.z;
  4744. var c=this.cs;
  4745. while(true){
  4746. var oldImp1;
  4747. var newImp1;
  4748. var oldImp2;
  4749. var newImp2;
  4750. var rvn;
  4751. var norImp=c.norImp;
  4752. var tanImp=c.tanImp;
  4753. var binImp=c.binImp;
  4754. var max=-norImp*this.friction;
  4755. var rvX=lv2x-lv1x;
  4756. var rvY=lv2y-lv1y;
  4757. var rvZ=lv2z-lv1z;
  4758. rvn=
  4759. rvX*c.tanX+rvY*c.tanY+rvZ*c.tanZ+
  4760. av2x*c.tanT2X+av2y*c.tanT2Y+av2z*c.tanT2Z-
  4761. av1x*c.tanT1X-av1y*c.tanT1Y-av1z*c.tanT1Z
  4762. ;
  4763. oldImp1=tanImp;
  4764. newImp1=rvn*c.tanDen;
  4765. tanImp+=newImp1;
  4766. rvn=
  4767. rvX*c.binX+rvY*c.binY+rvZ*c.binZ+
  4768. av2x*c.binT2X+av2y*c.binT2Y+av2z*c.binT2Z-
  4769. av1x*c.binT1X-av1y*c.binT1Y-av1z*c.binT1Z
  4770. ;
  4771. oldImp2=binImp;
  4772. newImp2=rvn*c.binDen;
  4773. binImp+=newImp2;
  4774. var len=tanImp*tanImp+binImp*binImp;
  4775. if(len>max*max){
  4776. len=max/Math.sqrt(len);
  4777. tanImp*=len;
  4778. binImp*=len;
  4779. }
  4780. newImp1=tanImp-oldImp1;
  4781. newImp2=binImp-oldImp2;
  4782. lv1x+=c.tanU1X*newImp1+c.binU1X*newImp2;
  4783. lv1y+=c.tanU1Y*newImp1+c.binU1Y*newImp2;
  4784. lv1z+=c.tanU1Z*newImp1+c.binU1Z*newImp2;
  4785. av1x+=c.tanTU1X*newImp1+c.binTU1X*newImp2;
  4786. av1y+=c.tanTU1Y*newImp1+c.binTU1Y*newImp2;
  4787. av1z+=c.tanTU1Z*newImp1+c.binTU1Z*newImp2;
  4788. lv2x-=c.tanU2X*newImp1+c.binU2X*newImp2;
  4789. lv2y-=c.tanU2Y*newImp1+c.binU2Y*newImp2;
  4790. lv2z-=c.tanU2Z*newImp1+c.binU2Z*newImp2;
  4791. av2x-=c.tanTU2X*newImp1+c.binTU2X*newImp2;
  4792. av2y-=c.tanTU2Y*newImp1+c.binTU2Y*newImp2;
  4793. av2z-=c.tanTU2Z*newImp1+c.binTU2Z*newImp2;
  4794. rvn=
  4795. (lv2x-lv1x)*c.norX+(lv2y-lv1y)*c.norY+(lv2z-lv1z)*c.norZ+
  4796. av2x*c.norT2X+av2y*c.norT2Y+av2z*c.norT2Z-
  4797. av1x*c.norT1X-av1y*c.norT1Y-av1z*c.norT1Z
  4798. ;
  4799. oldImp1=norImp;
  4800. newImp1=(rvn-c.norTar)*c.norDen;
  4801. norImp+=newImp1;
  4802. if(norImp>0)norImp=0;
  4803. newImp1=norImp-oldImp1;
  4804. lv1x+=c.norU1X*newImp1;
  4805. lv1y+=c.norU1Y*newImp1;
  4806. lv1z+=c.norU1Z*newImp1;
  4807. av1x+=c.norTU1X*newImp1;
  4808. av1y+=c.norTU1Y*newImp1;
  4809. av1z+=c.norTU1Z*newImp1;
  4810. lv2x-=c.norU2X*newImp1;
  4811. lv2y-=c.norU2Y*newImp1;
  4812. lv2z-=c.norU2Z*newImp1;
  4813. av2x-=c.norTU2X*newImp1;
  4814. av2y-=c.norTU2Y*newImp1;
  4815. av2z-=c.norTU2Z*newImp1;
  4816. c.norImp=norImp;
  4817. c.tanImp=tanImp;
  4818. c.binImp=binImp;
  4819. if(c.last)break;
  4820. c=c.next;
  4821. }
  4822. this.lv1.x=lv1x;
  4823. this.lv1.y=lv1y;
  4824. this.lv1.z=lv1z;
  4825. this.lv2.x=lv2x;
  4826. this.lv2.y=lv2y;
  4827. this.lv2.z=lv2z;
  4828. this.av1.x=av1x;
  4829. this.av1.y=av1y;
  4830. this.av1.z=av1z;
  4831. this.av2.x=av2x;
  4832. this.av2.y=av2y;
  4833. this.av2.z=av2z;
  4834. }
  4835. OIMO.ContactConstraint.prototype.postSolve = function(){
  4836. var c=this.cs;
  4837. for(var i=0;i<this.num;i++){
  4838. var p=this.ps[i];
  4839. p.normal.x=c.norX;
  4840. p.normal.y=c.norY;
  4841. p.normal.z=c.norZ;
  4842. p.tangent.x=c.tanX;
  4843. p.tangent.y=c.tanY;
  4844. p.tangent.z=c.tanZ;
  4845. p.binormal.x=c.binX;
  4846. p.binormal.y=c.binY;
  4847. p.binormal.z=c.binZ;
  4848. p.normalImpulse=c.norImp;
  4849. p.tangentImpulse=c.tanImp;
  4850. p.binormalImpulse=c.binImp;
  4851. p.normalDenominator=c.norDen;
  4852. p.tangentDenominator=c.tanDen;
  4853. p.binormalDenominator=c.binDen;
  4854. c=c.next;
  4855. }
  4856. }
  4857. OIMO.ContactLink = function(contact){
  4858. this.prev=null;
  4859. this.next=null;
  4860. this.shape=null;
  4861. this.body=null;
  4862. this.contact = contact;
  4863. }
  4864. OIMO.ContactManifold = function(){
  4865. this.body1 = null;
  4866. this.body2 = null;
  4867. this.numPoints = 0;
  4868. this.points = [];// vector 4
  4869. this.points.length = 4;
  4870. this.points[0] = new OIMO.ManifoldPoint();
  4871. this.points[1] = new OIMO.ManifoldPoint();
  4872. this.points[2] = new OIMO.ManifoldPoint();
  4873. this.points[3] = new OIMO.ManifoldPoint();
  4874. }
  4875. OIMO.ContactManifold.prototype = {
  4876. constructor: OIMO.ContactManifold,
  4877. reset:function(shape1,shape2){
  4878. this.body1=shape1.parent;
  4879. this.body2=shape2.parent;
  4880. this.numPoints=0;
  4881. },
  4882. addPoint:function(x,y,z,normalX,normalY,normalZ,penetration,flip){
  4883. var p=this.points[this.numPoints++];
  4884. p.position.x=x;
  4885. p.position.y=y;
  4886. p.position.z=z;
  4887. var r=this.body1.rotation;
  4888. var rx=x-this.body1.position.x;
  4889. var ry=y-this.body1.position.y;
  4890. var rz=z-this.body1.position.z;
  4891. var tr = r.elements;
  4892. p.localPoint1.x=rx*tr[0]+ry*tr[3]+rz*tr[6];
  4893. p.localPoint1.y=rx*tr[1]+ry*tr[4]+rz*tr[7];
  4894. p.localPoint1.z=rx*tr[2]+ry*tr[5]+rz*tr[8];
  4895. r=this.body2.rotation;
  4896. rx=x-this.body2.position.x;
  4897. ry=y-this.body2.position.y;
  4898. rz=z-this.body2.position.z;
  4899. p.localPoint2.x=rx*tr[0]+ry*tr[3]+rz*tr[6];
  4900. p.localPoint2.y=rx*tr[1]+ry*tr[4]+rz*tr[7];
  4901. p.localPoint2.z=rx*tr[2]+ry*tr[5]+rz*tr[8];
  4902. p.normalImpulse=0;
  4903. if(flip){
  4904. p.normal.x=-normalX;
  4905. p.normal.y=-normalY;
  4906. p.normal.z=-normalZ;
  4907. }else{
  4908. p.normal.x=normalX;
  4909. p.normal.y=normalY;
  4910. p.normal.z=normalZ;
  4911. }
  4912. p.penetration=penetration;
  4913. p.warmStarted=false;
  4914. }
  4915. }
  4916. OIMO.ContactPointDataBuffer = function(){
  4917. this.norX=NaN;
  4918. this.norY=NaN;
  4919. this.norZ=NaN;
  4920. this.tanX=NaN;
  4921. this.tanY=NaN;
  4922. this.tanZ=NaN;
  4923. this.binX=NaN;
  4924. this.binY=NaN;
  4925. this.binZ=NaN;
  4926. this.rp1X=NaN;
  4927. this.rp1Y=NaN;
  4928. this.rp1Z=NaN;
  4929. this.rp2X=NaN;
  4930. this.rp2Y=NaN;
  4931. this.rp2Z=NaN;
  4932. this.norU1X=NaN;
  4933. this.norU1Y=NaN;
  4934. this.norU1Z=NaN;
  4935. this.norU2X=NaN;
  4936. this.norU2Y=NaN;
  4937. this.norU2Z=NaN;
  4938. this.tanU1X=NaN;
  4939. this.tanU1Y=NaN;
  4940. this.tanU1Z=NaN;
  4941. this.tanU2X=NaN;
  4942. this.tanU2Y=NaN;
  4943. this.tanU2Z=NaN;
  4944. this.binU1X=NaN;
  4945. this.binU1Y=NaN;
  4946. this.binU1Z=NaN;
  4947. this.binU2X=NaN;
  4948. this.binU2Y=NaN;
  4949. this.binU2Z=NaN;
  4950. this.norT1X=NaN;
  4951. this.norT1Y=NaN;
  4952. this.norT1Z=NaN;
  4953. this.norT2X=NaN;
  4954. this.norT2Y=NaN;
  4955. this.norT2Z=NaN;
  4956. this.tanT1X=NaN;
  4957. this.tanT1Y=NaN;
  4958. this.tanT1Z=NaN;
  4959. this.tanT2X=NaN;
  4960. this.tanT2Y=NaN;
  4961. this.tanT2Z=NaN;
  4962. this.binT1X=NaN;
  4963. this.binT1Y=NaN;
  4964. this.binT1Z=NaN;
  4965. this.binT2X=NaN;
  4966. this.binT2Y=NaN;
  4967. this.binT2Z=NaN;
  4968. this.norTU1X=NaN;
  4969. this.norTU1Y=NaN;
  4970. this.norTU1Z=NaN;
  4971. this.norTU2X=NaN;
  4972. this.norTU2Y=NaN;
  4973. this.norTU2Z=NaN;
  4974. this.tanTU1X=NaN;
  4975. this.tanTU1Y=NaN;
  4976. this.tanTU1Z=NaN;
  4977. this.tanTU2X=NaN;
  4978. this.tanTU2Y=NaN;
  4979. this.tanTU2Z=NaN;
  4980. this.binTU1X=NaN;
  4981. this.binTU1Y=NaN;
  4982. this.binTU1Z=NaN;
  4983. this.binTU2X=NaN;
  4984. this.binTU2Y=NaN;
  4985. this.binTU2Z=NaN;
  4986. this.norImp=NaN;
  4987. this.tanImp=NaN;
  4988. this.binImp=NaN;
  4989. this.norDen=NaN;
  4990. this.tanDen=NaN;
  4991. this.binDen=NaN;
  4992. this.norTar=NaN;
  4993. this.next=null;
  4994. this.last=false;
  4995. }
  4996. OIMO.ImpulseDataBuffer = function(){
  4997. this.lp1X=NaN;
  4998. this.lp1Y=NaN;
  4999. this.lp1Z=NaN;
  5000. this.lp2X=NaN;
  5001. this.lp2Y=NaN;
  5002. this.lp2Z=NaN;
  5003. this.impulse=NaN;
  5004. }
  5005. OIMO.ManifoldPoint = function(){
  5006. this.warmStarted=false;
  5007. this.position=new OIMO.Vec3();
  5008. this.localPoint1=new OIMO.Vec3();
  5009. this.localPoint2=new OIMO.Vec3();
  5010. this.normal=new OIMO.Vec3();
  5011. this.tangent=new OIMO.Vec3();
  5012. this.binormal=new OIMO.Vec3();
  5013. this.normalImpulse=0;
  5014. this.tangentImpulse=0;
  5015. this.binormalImpulse=0;
  5016. this.normalDenominator=0;
  5017. this.tangentDenominator=0;
  5018. this.binormalDenominator=0;
  5019. this.penetration=0;
  5020. }
  5021. OIMO.MassInfo = function(){
  5022. this.mass=0;
  5023. this.inertia=new OIMO.Mat33();
  5024. }
  5025. OIMO.Shape = function(config){
  5026. this.prev=null;
  5027. this.next=null;
  5028. this.type=0;
  5029. this.proxy=null;
  5030. this.parent=null;
  5031. this.contactLink=null;
  5032. this.numContacts=0;
  5033. this.id=OIMO.nextID++;
  5034. this.position=new OIMO.Vec3();
  5035. this.relativePosition=new OIMO.Vec3().copy(config.relativePosition);
  5036. this.rotation=new OIMO.Mat33();
  5037. this.relativeRotation=new OIMO.Mat33().copy(config.relativeRotation);
  5038. this.aabb=new OIMO.AABB();
  5039. this.density=config.density;
  5040. this.friction=config.friction;
  5041. this.restitution=config.restitution;
  5042. this.belongsTo=config.belongsTo;
  5043. this.collidesWith=config.collidesWith;
  5044. }
  5045. OIMO.Shape.prototype = {
  5046. constructor: OIMO.Shape,
  5047. calculateMassInfo:function(out){
  5048. throw new Error("Inheritance error.");
  5049. },
  5050. updateProxy:function(){
  5051. throw new Error("Inheritance error.");
  5052. }
  5053. }
  5054. OIMO.ShapeConfig = function(){
  5055. this.relativePosition=new OIMO.Vec3();
  5056. this.relativeRotation=new OIMO.Mat33();
  5057. this.friction=0.4;
  5058. this.restitution=0.2;
  5059. this.density=1;
  5060. this.belongsTo=1;
  5061. this.collidesWith=0xffffffff;
  5062. }
  5063. OIMO.BoxShape = function(config,Width,Height,Depth){
  5064. OIMO.Shape.call( this, config);
  5065. this.width=Width;
  5066. this.height=Height;
  5067. this.depth=Depth;
  5068. this.halfWidth=Width*0.5;
  5069. this.halfHeight=Height*0.5;
  5070. this.halfDepth=Depth*0.5;
  5071. this.dimentions = new OIMO_ARRAY_TYPE(18);
  5072. this.elements = new OIMO_ARRAY_TYPE(24);
  5073. this.type=OIMO.SHAPE_BOX;
  5074. }
  5075. OIMO.BoxShape.prototype = Object.create( OIMO.Shape.prototype );
  5076. OIMO.BoxShape.prototype.calculateMassInfo = function(out){
  5077. var mass=this.width*this.height*this.depth*this.density;
  5078. out.mass=mass;
  5079. out.inertia.init(
  5080. mass*(this.height*this.height+this.depth*this.depth)/12,0,0,
  5081. 0,mass*(this.width*this.width+this.depth*this.depth)/12,0,
  5082. 0,0,mass*(this.width*this.width+this.height*this.height)/12
  5083. );
  5084. }
  5085. OIMO.BoxShape.prototype.updateProxy = function(){
  5086. var te = this.rotation.elements;
  5087. var di=this.dimentions;
  5088. // Width
  5089. di[0]=te[0];
  5090. di[1]=te[3];
  5091. di[2]=te[6];
  5092. // Height
  5093. di[3]=te[1];
  5094. di[4]=te[4];
  5095. di[5]=te[7];
  5096. // Depth
  5097. di[6]=te[2];
  5098. di[7]=te[5];
  5099. di[8]=te[8];
  5100. // halp Width
  5101. di[9]=te[0]*this.halfWidth;
  5102. di[10]=te[3]*this.halfWidth;
  5103. di[11]=te[6]*this.halfWidth;
  5104. // halp Height
  5105. di[12]=te[1]*this.halfHeight;
  5106. di[13]=te[4]*this.halfHeight;
  5107. di[14]=te[7]*this.halfHeight;
  5108. // halp Depth
  5109. di[15]=te[2]*this.halfDepth;
  5110. di[16]=te[5]*this.halfDepth;
  5111. di[17]=te[8]*this.halfDepth;
  5112. var wx=di[9];
  5113. var wy=di[10];
  5114. var wz=di[11];
  5115. var hx=di[12];
  5116. var hy=di[13];
  5117. var hz=di[14];
  5118. var dx=di[15];
  5119. var dy=di[16];
  5120. var dz=di[17];
  5121. var x=this.position.x;
  5122. var y=this.position.y;
  5123. var z=this.position.z;
  5124. var v=this.elements;
  5125. //v1
  5126. v[0]=x+wx+hx+dx;
  5127. v[1]=y+wy+hy+dy;
  5128. v[2]=z+wz+hz+dz;
  5129. //v2
  5130. v[3]=x+wx+hx-dx;
  5131. v[4]=y+wy+hy-dy;
  5132. v[5]=z+wz+hz-dz;
  5133. //v3
  5134. v[6]=x+wx-hx+dx;
  5135. v[7]=y+wy-hy+dy;
  5136. v[8]=z+wz-hz+dz;
  5137. //v4
  5138. v[9]=x+wx-hx-dx;
  5139. v[10]=y+wy-hy-dy;
  5140. v[11]=z+wz-hz-dz;
  5141. //v5
  5142. v[12]=x-wx+hx+dx;
  5143. v[13]=y-wy+hy+dy;
  5144. v[14]=z-wz+hz+dz;
  5145. //v6
  5146. v[15]=x-wx+hx-dx;
  5147. v[16]=y-wy+hy-dy;
  5148. v[17]=z-wz+hz-dz;
  5149. //v7
  5150. v[18]=x-wx-hx+dx;
  5151. v[19]=y-wy-hy+dy;
  5152. v[20]=z-wz-hz+dz;
  5153. //v8
  5154. v[21]=x-wx-hx-dx;
  5155. v[22]=y-wy-hy-dy;
  5156. v[23]=z-wz-hz-dz;
  5157. var w = (di[9]<0) ? -di[9] : di[9];
  5158. var h = (di[10]<0) ? -di[10] : di[10];
  5159. var d = (di[11]<0) ? -di[11] : di[11];
  5160. w = (di[12]<0) ? w-di[12] : w+di[12];
  5161. h = (di[13]<0) ? h-di[13] : h+di[13];
  5162. d = (di[14]<0) ? d-di[14] : d+di[14];
  5163. w = (di[15]<0) ? w-di[15] : w+di[15];
  5164. h = (di[16]<0) ? h-di[16] : h+di[16];
  5165. d = (di[17]<0) ? d-di[17] : d+di[17];
  5166. this.aabb.init(
  5167. this.position.x-w-0.005,this.position.x+w+0.005,
  5168. this.position.y-h-0.005,this.position.y+h+0.005,
  5169. this.position.z-d-0.005,this.position.z+d+0.005
  5170. );
  5171. if(this.proxy!==null){
  5172. this.proxy.update();
  5173. }
  5174. }
  5175. OIMO.SphereShape = function(config,radius){
  5176. OIMO.Shape.call( this, config);
  5177. this.radius=radius;
  5178. this.type=OIMO.SHAPE_SPHERE;
  5179. }
  5180. OIMO.SphereShape.prototype = Object.create( OIMO.Shape.prototype );
  5181. OIMO.SphereShape.prototype.calculateMassInfo = function(out){
  5182. var mass=4/3*Math.PI*this.radius*this.radius*this.radius*this.density;
  5183. out.mass=mass;
  5184. var inertia=mass*this.radius*this.radius*2/5;
  5185. out.inertia.init( inertia,0,0, 0,inertia,0, 0,0,inertia );
  5186. }
  5187. OIMO.SphereShape.prototype.updateProxy = function(){
  5188. this.aabb.init(
  5189. this.position.x-this.radius-0.005,this.position.x+this.radius+0.005,
  5190. this.position.y-this.radius-0.005,this.position.y+this.radius+0.005,
  5191. this.position.z-this.radius-0.005,this.position.z+this.radius+0.005
  5192. );
  5193. if(this.proxy!==null){ this.proxy.update(); }
  5194. }
  5195. OIMO.CollisionDetector = function(){
  5196. this.flip = false;
  5197. }
  5198. OIMO.CollisionDetector.prototype = {
  5199. constructor: OIMO.CollisionDetector,
  5200. detectCollision:function(shape1,shape2,manifold){
  5201. throw new Error("Inheritance error.");
  5202. }
  5203. }
  5204. OIMO.BoxBoxCollisionDetector = function(){
  5205. OIMO.CollisionDetector.call( this );
  5206. this.clipVertices1=new OIMO_ARRAY_TYPE(24);
  5207. this.clipVertices2=new OIMO_ARRAY_TYPE(24);
  5208. this.used=new OIMO_ARRAY_TYPE(8);
  5209. this.INF = 1/0;
  5210. }
  5211. OIMO.BoxBoxCollisionDetector.prototype = Object.create( OIMO.CollisionDetector.prototype );
  5212. OIMO.BoxBoxCollisionDetector.prototype.detectCollision = function(shape1,shape2,manifold){
  5213. var b1;
  5214. var b2;
  5215. if(shape1.id<shape2.id){
  5216. b1=(shape1);
  5217. b2=(shape2);
  5218. }else{
  5219. b1=(shape2);
  5220. b2=(shape1);
  5221. }
  5222. var V1 = b1.elements;
  5223. var V2 = b2.elements;
  5224. var D1 = b1.dimentions;
  5225. var D2 = b2.dimentions;
  5226. var p1=b1.position;
  5227. var p2=b2.position;
  5228. var p1x=p1.x;
  5229. var p1y=p1.y;
  5230. var p1z=p1.z;
  5231. var p2x=p2.x;
  5232. var p2y=p2.y;
  5233. var p2z=p2.z;
  5234. var dx=p2x-p1x;
  5235. var dy=p2y-p1y;
  5236. var dz=p2z-p1z;
  5237. var w1=b1.halfWidth;
  5238. var h1=b1.halfHeight;
  5239. var d1=b1.halfDepth;
  5240. var w2=b2.halfWidth;
  5241. var h2=b2.halfHeight;
  5242. var d2=b2.halfDepth;
  5243. var a1x=D1[0];
  5244. var a1y=D1[1];
  5245. var a1z=D1[2];
  5246. var a2x=D1[3];
  5247. var a2y=D1[4];
  5248. var a2z=D1[5];
  5249. var a3x=D1[6];
  5250. var a3y=D1[7];
  5251. var a3z=D1[8];
  5252. var d1x=D1[9];
  5253. var d1y=D1[10];
  5254. var d1z=D1[11];
  5255. var d2x=D1[12];
  5256. var d2y=D1[13];
  5257. var d2z=D1[14];
  5258. var d3x=D1[15];
  5259. var d3y=D1[16];
  5260. var d3z=D1[17];
  5261. var a4x=D2[0];
  5262. var a4y=D2[1];
  5263. var a4z=D2[2];
  5264. var a5x=D2[3];
  5265. var a5y=D2[4];
  5266. var a5z=D2[5];
  5267. var a6x=D2[6];
  5268. var a6y=D2[7];
  5269. var a6z=D2[8];
  5270. var d4x=D2[9];
  5271. var d4y=D2[10];
  5272. var d4z=D2[11];
  5273. var d5x=D2[12];
  5274. var d5y=D2[13];
  5275. var d5z=D2[14];
  5276. var d6x=D2[15];
  5277. var d6y=D2[16];
  5278. var d6z=D2[17];
  5279. var a7x=a1y*a4z-a1z*a4y;
  5280. var a7y=a1z*a4x-a1x*a4z;
  5281. var a7z=a1x*a4y-a1y*a4x;
  5282. var a8x=a1y*a5z-a1z*a5y;
  5283. var a8y=a1z*a5x-a1x*a5z;
  5284. var a8z=a1x*a5y-a1y*a5x;
  5285. var a9x=a1y*a6z-a1z*a6y;
  5286. var a9y=a1z*a6x-a1x*a6z;
  5287. var a9z=a1x*a6y-a1y*a6x;
  5288. var aax=a2y*a4z-a2z*a4y;
  5289. var aay=a2z*a4x-a2x*a4z;
  5290. var aaz=a2x*a4y-a2y*a4x;
  5291. var abx=a2y*a5z-a2z*a5y;
  5292. var aby=a2z*a5x-a2x*a5z;
  5293. var abz=a2x*a5y-a2y*a5x;
  5294. var acx=a2y*a6z-a2z*a6y;
  5295. var acy=a2z*a6x-a2x*a6z;
  5296. var acz=a2x*a6y-a2y*a6x;
  5297. var adx=a3y*a4z-a3z*a4y;
  5298. var ady=a3z*a4x-a3x*a4z;
  5299. var adz=a3x*a4y-a3y*a4x;
  5300. var aex=a3y*a5z-a3z*a5y;
  5301. var aey=a3z*a5x-a3x*a5z;
  5302. var aez=a3x*a5y-a3y*a5x;
  5303. var afx=a3y*a6z-a3z*a6y;
  5304. var afy=a3z*a6x-a3x*a6z;
  5305. var afz=a3x*a6y-a3y*a6x;
  5306. var right1;
  5307. var right2;
  5308. var right3;
  5309. var right4;
  5310. var right5;
  5311. var right6;
  5312. var right7;
  5313. var right8;
  5314. var right9;
  5315. var righta;
  5316. var rightb;
  5317. var rightc;
  5318. var rightd;
  5319. var righte;
  5320. var rightf;
  5321. var overlap1;
  5322. var overlap2;
  5323. var overlap3;
  5324. var overlap4;
  5325. var overlap5;
  5326. var overlap6;
  5327. var overlap7;
  5328. var overlap8;
  5329. var overlap9;
  5330. var overlapa;
  5331. var overlapb;
  5332. var overlapc;
  5333. var overlapd;
  5334. var overlape;
  5335. var overlapf;
  5336. var invalid7=false;
  5337. var invalid8=false;
  5338. var invalid9=false;
  5339. var invalida=false;
  5340. var invalidb=false;
  5341. var invalidc=false;
  5342. var invalidd=false;
  5343. var invalide=false;
  5344. var invalidf=false;
  5345. var len;
  5346. var len1;
  5347. var len2;
  5348. var dot1;
  5349. var dot2;
  5350. var dot3;
  5351. len=a1x*dx+a1y*dy+a1z*dz;
  5352. right1=len>0;
  5353. if(!right1)len=-len;
  5354. len1=w1;
  5355. dot1=a1x*a4x+a1y*a4y+a1z*a4z;
  5356. dot2=a1x*a5x+a1y*a5y+a1z*a5z;
  5357. dot3=a1x*a6x+a1y*a6y+a1z*a6z;
  5358. if(dot1<0)dot1=-dot1;
  5359. if(dot2<0)dot2=-dot2;
  5360. if(dot3<0)dot3=-dot3;
  5361. len2=dot1*w2+dot2*h2+dot3*d2;
  5362. overlap1=len-len1-len2;
  5363. if(overlap1>0)return;
  5364. len=a2x*dx+a2y*dy+a2z*dz;
  5365. right2=len>0;
  5366. if(!right2)len=-len;
  5367. len1=h1;
  5368. dot1=a2x*a4x+a2y*a4y+a2z*a4z;
  5369. dot2=a2x*a5x+a2y*a5y+a2z*a5z;
  5370. dot3=a2x*a6x+a2y*a6y+a2z*a6z;
  5371. if(dot1<0)dot1=-dot1;
  5372. if(dot2<0)dot2=-dot2;
  5373. if(dot3<0)dot3=-dot3;
  5374. len2=dot1*w2+dot2*h2+dot3*d2;
  5375. overlap2=len-len1-len2;
  5376. if(overlap2>0)return;
  5377. len=a3x*dx+a3y*dy+a3z*dz;
  5378. right3=len>0;
  5379. if(!right3)len=-len;
  5380. len1=d1;
  5381. dot1=a3x*a4x+a3y*a4y+a3z*a4z;
  5382. dot2=a3x*a5x+a3y*a5y+a3z*a5z;
  5383. dot3=a3x*a6x+a3y*a6y+a3z*a6z;
  5384. if(dot1<0)dot1=-dot1;
  5385. if(dot2<0)dot2=-dot2;
  5386. if(dot3<0)dot3=-dot3;
  5387. len2=dot1*w2+dot2*h2+dot3*d2;
  5388. overlap3=len-len1-len2;
  5389. if(overlap3>0)return;
  5390. len=a4x*dx+a4y*dy+a4z*dz;
  5391. right4=len>0;
  5392. if(!right4)len=-len;
  5393. dot1=a4x*a1x+a4y*a1y+a4z*a1z;
  5394. dot2=a4x*a2x+a4y*a2y+a4z*a2z;
  5395. dot3=a4x*a3x+a4y*a3y+a4z*a3z;
  5396. if(dot1<0)dot1=-dot1;
  5397. if(dot2<0)dot2=-dot2;
  5398. if(dot3<0)dot3=-dot3;
  5399. len1=dot1*w1+dot2*h1+dot3*d1;
  5400. len2=w2;
  5401. overlap4=(len-len1-len2)*1.0;
  5402. if(overlap4>0)return;
  5403. len=a5x*dx+a5y*dy+a5z*dz;
  5404. right5=len>0;
  5405. if(!right5)len=-len;
  5406. dot1=a5x*a1x+a5y*a1y+a5z*a1z;
  5407. dot2=a5x*a2x+a5y*a2y+a5z*a2z;
  5408. dot3=a5x*a3x+a5y*a3y+a5z*a3z;
  5409. if(dot1<0)dot1=-dot1;
  5410. if(dot2<0)dot2=-dot2;
  5411. if(dot3<0)dot3=-dot3;
  5412. len1=dot1*w1+dot2*h1+dot3*d1;
  5413. len2=h2;
  5414. overlap5=(len-len1-len2)*1.0;
  5415. if(overlap5>0)return;
  5416. len=a6x*dx+a6y*dy+a6z*dz;
  5417. right6=len>0;
  5418. if(!right6)len=-len;
  5419. dot1=a6x*a1x+a6y*a1y+a6z*a1z;
  5420. dot2=a6x*a2x+a6y*a2y+a6z*a2z;
  5421. dot3=a6x*a3x+a6y*a3y+a6z*a3z;
  5422. if(dot1<0)dot1=-dot1;
  5423. if(dot2<0)dot2=-dot2;
  5424. if(dot3<0)dot3=-dot3;
  5425. len1=dot1*w1+dot2*h1+dot3*d1;
  5426. len2=d2;
  5427. overlap6=(len-len1-len2)*1.0;
  5428. if(overlap6>0)return;
  5429. len=a7x*a7x+a7y*a7y+a7z*a7z;
  5430. if(len>1e-5){
  5431. len=1/Math.sqrt(len);
  5432. a7x*=len;
  5433. a7y*=len;
  5434. a7z*=len;
  5435. len=a7x*dx+a7y*dy+a7z*dz;
  5436. right7=len>0;
  5437. if(!right7)len=-len;
  5438. dot1=a7x*a2x+a7y*a2y+a7z*a2z;
  5439. dot2=a7x*a3x+a7y*a3y+a7z*a3z;
  5440. if(dot1<0)dot1=-dot1;
  5441. if(dot2<0)dot2=-dot2;
  5442. len1=dot1*h1+dot2*d1;
  5443. dot1=a7x*a5x+a7y*a5y+a7z*a5z;
  5444. dot2=a7x*a6x+a7y*a6y+a7z*a6z;
  5445. if(dot1<0)dot1=-dot1;
  5446. if(dot2<0)dot2=-dot2;
  5447. len2=dot1*h2+dot2*d2;
  5448. overlap7=len-len1-len2;
  5449. if(overlap7>0)return;
  5450. }else{
  5451. right7=false;
  5452. overlap7=0;
  5453. invalid7=true;
  5454. }
  5455. len=a8x*a8x+a8y*a8y+a8z*a8z;
  5456. if(len>1e-5){
  5457. len=1/Math.sqrt(len);
  5458. a8x*=len;
  5459. a8y*=len;
  5460. a8z*=len;
  5461. len=a8x*dx+a8y*dy+a8z*dz;
  5462. right8=len>0;
  5463. if(!right8)len=-len;
  5464. dot1=a8x*a2x+a8y*a2y+a8z*a2z;
  5465. dot2=a8x*a3x+a8y*a3y+a8z*a3z;
  5466. if(dot1<0)dot1=-dot1;
  5467. if(dot2<0)dot2=-dot2;
  5468. len1=dot1*h1+dot2*d1;
  5469. dot1=a8x*a4x+a8y*a4y+a8z*a4z;
  5470. dot2=a8x*a6x+a8y*a6y+a8z*a6z;
  5471. if(dot1<0)dot1=-dot1;
  5472. if(dot2<0)dot2=-dot2;
  5473. len2=dot1*w2+dot2*d2;
  5474. overlap8=len-len1-len2;
  5475. if(overlap8>0)return;
  5476. }else{
  5477. right8=false;
  5478. overlap8=0;
  5479. invalid8=true;
  5480. }
  5481. len=a9x*a9x+a9y*a9y+a9z*a9z;
  5482. if(len>1e-5){
  5483. len=1/Math.sqrt(len);
  5484. a9x*=len;
  5485. a9y*=len;
  5486. a9z*=len;
  5487. len=a9x*dx+a9y*dy+a9z*dz;
  5488. right9=len>0;
  5489. if(!right9)len=-len;
  5490. dot1=a9x*a2x+a9y*a2y+a9z*a2z;
  5491. dot2=a9x*a3x+a9y*a3y+a9z*a3z;
  5492. if(dot1<0)dot1=-dot1;
  5493. if(dot2<0)dot2=-dot2;
  5494. len1=dot1*h1+dot2*d1;
  5495. dot1=a9x*a4x+a9y*a4y+a9z*a4z;
  5496. dot2=a9x*a5x+a9y*a5y+a9z*a5z;
  5497. if(dot1<0)dot1=-dot1;
  5498. if(dot2<0)dot2=-dot2;
  5499. len2=dot1*w2+dot2*h2;
  5500. overlap9=len-len1-len2;
  5501. if(overlap9>0)return;
  5502. }else{
  5503. right9=false;
  5504. overlap9=0;
  5505. invalid9=true;
  5506. }
  5507. len=aax*aax+aay*aay+aaz*aaz;
  5508. if(len>1e-5){
  5509. len=1/Math.sqrt(len);
  5510. aax*=len;
  5511. aay*=len;
  5512. aaz*=len;
  5513. len=aax*dx+aay*dy+aaz*dz;
  5514. righta=len>0;
  5515. if(!righta)len=-len;
  5516. dot1=aax*a1x+aay*a1y+aaz*a1z;
  5517. dot2=aax*a3x+aay*a3y+aaz*a3z;
  5518. if(dot1<0)dot1=-dot1;
  5519. if(dot2<0)dot2=-dot2;
  5520. len1=dot1*w1+dot2*d1;
  5521. dot1=aax*a5x+aay*a5y+aaz*a5z;
  5522. dot2=aax*a6x+aay*a6y+aaz*a6z;
  5523. if(dot1<0)dot1=-dot1;
  5524. if(dot2<0)dot2=-dot2;
  5525. len2=dot1*h2+dot2*d2;
  5526. overlapa=len-len1-len2;
  5527. if(overlapa>0)return;
  5528. }else{
  5529. righta=false;
  5530. overlapa=0;
  5531. invalida=true;
  5532. }
  5533. len=abx*abx+aby*aby+abz*abz;
  5534. if(len>1e-5){
  5535. len=1/Math.sqrt(len);
  5536. abx*=len;
  5537. aby*=len;
  5538. abz*=len;
  5539. len=abx*dx+aby*dy+abz*dz;
  5540. rightb=len>0;
  5541. if(!rightb)len=-len;
  5542. dot1=abx*a1x+aby*a1y+abz*a1z;
  5543. dot2=abx*a3x+aby*a3y+abz*a3z;
  5544. if(dot1<0)dot1=-dot1;
  5545. if(dot2<0)dot2=-dot2;
  5546. len1=dot1*w1+dot2*d1;
  5547. dot1=abx*a4x+aby*a4y+abz*a4z;
  5548. dot2=abx*a6x+aby*a6y+abz*a6z;
  5549. if(dot1<0)dot1=-dot1;
  5550. if(dot2<0)dot2=-dot2;
  5551. len2=dot1*w2+dot2*d2;
  5552. overlapb=len-len1-len2;
  5553. if(overlapb>0)return;
  5554. }else{
  5555. rightb=false;
  5556. overlapb=0;
  5557. invalidb=true;
  5558. }
  5559. len=acx*acx+acy*acy+acz*acz;
  5560. if(len>1e-5){
  5561. len=1/Math.sqrt(len);
  5562. acx*=len;
  5563. acy*=len;
  5564. acz*=len;
  5565. len=acx*dx+acy*dy+acz*dz;
  5566. rightc=len>0;
  5567. if(!rightc)len=-len;
  5568. dot1=acx*a1x+acy*a1y+acz*a1z;
  5569. dot2=acx*a3x+acy*a3y+acz*a3z;
  5570. if(dot1<0)dot1=-dot1;
  5571. if(dot2<0)dot2=-dot2;
  5572. len1=dot1*w1+dot2*d1;
  5573. dot1=acx*a4x+acy*a4y+acz*a4z;
  5574. dot2=acx*a5x+acy*a5y+acz*a5z;
  5575. if(dot1<0)dot1=-dot1;
  5576. if(dot2<0)dot2=-dot2;
  5577. len2=dot1*w2+dot2*h2;
  5578. overlapc=len-len1-len2;
  5579. if(overlapc>0)return;
  5580. }else{
  5581. rightc=false;
  5582. overlapc=0;
  5583. invalidc=true;
  5584. }
  5585. len=adx*adx+ady*ady+adz*adz;
  5586. if(len>1e-5){
  5587. len=1/Math.sqrt(len);
  5588. adx*=len;
  5589. ady*=len;
  5590. adz*=len;
  5591. len=adx*dx+ady*dy+adz*dz;
  5592. rightd=len>0;
  5593. if(!rightd)len=-len;
  5594. dot1=adx*a1x+ady*a1y+adz*a1z;
  5595. dot2=adx*a2x+ady*a2y+adz*a2z;
  5596. if(dot1<0)dot1=-dot1;
  5597. if(dot2<0)dot2=-dot2;
  5598. len1=dot1*w1+dot2*h1;
  5599. dot1=adx*a5x+ady*a5y+adz*a5z;
  5600. dot2=adx*a6x+ady*a6y+adz*a6z;
  5601. if(dot1<0)dot1=-dot1;
  5602. if(dot2<0)dot2=-dot2;
  5603. len2=dot1*h2+dot2*d2;
  5604. overlapd=len-len1-len2;
  5605. if(overlapd>0)return;
  5606. }else{
  5607. rightd=false;
  5608. overlapd=0;
  5609. invalidd=true;
  5610. }
  5611. len=aex*aex+aey*aey+aez*aez;
  5612. if(len>1e-5){
  5613. len=1/Math.sqrt(len);
  5614. aex*=len;
  5615. aey*=len;
  5616. aez*=len;
  5617. len=aex*dx+aey*dy+aez*dz;
  5618. righte=len>0;
  5619. if(!righte)len=-len;
  5620. dot1=aex*a1x+aey*a1y+aez*a1z;
  5621. dot2=aex*a2x+aey*a2y+aez*a2z;
  5622. if(dot1<0)dot1=-dot1;
  5623. if(dot2<0)dot2=-dot2;
  5624. len1=dot1*w1+dot2*h1;
  5625. dot1=aex*a4x+aey*a4y+aez*a4z;
  5626. dot2=aex*a6x+aey*a6y+aez*a6z;
  5627. if(dot1<0)dot1=-dot1;
  5628. if(dot2<0)dot2=-dot2;
  5629. len2=dot1*w2+dot2*d2;
  5630. overlape=len-len1-len2;
  5631. if(overlape>0)return;
  5632. }else{
  5633. righte=false;
  5634. overlape=0;
  5635. invalide=true;
  5636. }
  5637. len=afx*afx+afy*afy+afz*afz;
  5638. if(len>1e-5){
  5639. len=1/Math.sqrt(len);
  5640. afx*=len;
  5641. afy*=len;
  5642. afz*=len;
  5643. len=afx*dx+afy*dy+afz*dz;
  5644. rightf=len>0;
  5645. if(!rightf)len=-len;
  5646. dot1=afx*a1x+afy*a1y+afz*a1z;
  5647. dot2=afx*a2x+afy*a2y+afz*a2z;
  5648. if(dot1<0)dot1=-dot1;
  5649. if(dot2<0)dot2=-dot2;
  5650. len1=dot1*w1+dot2*h1;
  5651. dot1=afx*a4x+afy*a4y+afz*a4z;
  5652. dot2=afx*a5x+afy*a5y+afz*a5z;
  5653. if(dot1<0)dot1=-dot1;
  5654. if(dot2<0)dot2=-dot2;
  5655. len2=dot1*w2+dot2*h2;
  5656. overlapf=len-len1-len2;
  5657. if(overlapf>0)return;
  5658. }else{
  5659. rightf=false;
  5660. overlapf=0;
  5661. invalidf=true;
  5662. }
  5663. var depth=overlap1;
  5664. var depth2=overlap1;
  5665. var minIndex=0;
  5666. var right=right1;
  5667. if(overlap2>depth2){
  5668. depth=overlap2;
  5669. depth2=overlap2;
  5670. minIndex=1;
  5671. right=right2;
  5672. }
  5673. if(overlap3>depth2){
  5674. depth=overlap3;
  5675. depth2=overlap3;
  5676. minIndex=2;
  5677. right=right3;
  5678. }
  5679. if(overlap4>depth2){
  5680. depth=overlap4;
  5681. depth2=overlap4;
  5682. minIndex=3;
  5683. right=right4;
  5684. }
  5685. if(overlap5>depth2){
  5686. depth=overlap5;
  5687. depth2=overlap5;
  5688. minIndex=4;
  5689. right=right5;
  5690. }
  5691. if(overlap6>depth2){
  5692. depth=overlap6;
  5693. depth2=overlap6;
  5694. minIndex=5;
  5695. right=right6;
  5696. }
  5697. if(overlap7-0.01>depth2&&!invalid7){
  5698. depth=overlap7;
  5699. depth2=overlap7-0.01;
  5700. minIndex=6;
  5701. right=right7;
  5702. }
  5703. if(overlap8-0.01>depth2&&!invalid8){
  5704. depth=overlap8;
  5705. depth2=overlap8-0.01;
  5706. minIndex=7;
  5707. right=right8;
  5708. }
  5709. if(overlap9-0.01>depth2&&!invalid9){
  5710. depth=overlap9;
  5711. depth2=overlap9-0.01;
  5712. minIndex=8;
  5713. right=right9;
  5714. }
  5715. if(overlapa-0.01>depth2&&!invalida){
  5716. depth=overlapa;
  5717. depth2=overlapa-0.01;
  5718. minIndex=9;
  5719. right=righta;
  5720. }
  5721. if(overlapb-0.01>depth2&&!invalidb){
  5722. depth=overlapb;
  5723. depth2=overlapb-0.01;
  5724. minIndex=10;
  5725. right=rightb;
  5726. }
  5727. if(overlapc-0.01>depth2&&!invalidc){
  5728. depth=overlapc;
  5729. depth2=overlapc-0.01;
  5730. minIndex=11;
  5731. right=rightc;
  5732. }
  5733. if(overlapd-0.01>depth2&&!invalidd){
  5734. depth=overlapd;
  5735. depth2=overlapd-0.01;
  5736. minIndex=12;
  5737. right=rightd;
  5738. }
  5739. if(overlape-0.01>depth2&&!invalide){
  5740. depth=overlape;
  5741. depth2=overlape-0.01;
  5742. minIndex=13;
  5743. right=righte;
  5744. }
  5745. if(overlapf-0.01>depth2&&!invalidf){
  5746. depth=overlapf;
  5747. minIndex=14;
  5748. right=rightf;
  5749. }
  5750. var nx=0;
  5751. var ny=0;
  5752. var nz=0;
  5753. var n1x=0;
  5754. var n1y=0;
  5755. var n1z=0;
  5756. var n2x=0;
  5757. var n2y=0;
  5758. var n2z=0;
  5759. var cx=0;
  5760. var cy=0;
  5761. var cz=0;
  5762. var s1x=0;
  5763. var s1y=0;
  5764. var s1z=0;
  5765. var s2x=0;
  5766. var s2y=0;
  5767. var s2z=0;
  5768. var swap=false;
  5769. switch(minIndex){
  5770. case 0:
  5771. if(right){
  5772. cx=p1x+d1x;
  5773. cy=p1y+d1y;
  5774. cz=p1z+d1z;
  5775. nx=a1x;
  5776. ny=a1y;
  5777. nz=a1z;
  5778. }else{
  5779. cx=p1x-d1x;
  5780. cy=p1y-d1y;
  5781. cz=p1z-d1z;
  5782. nx=-a1x;
  5783. ny=-a1y;
  5784. nz=-a1z;
  5785. }
  5786. s1x=d2x;
  5787. s1y=d2y;
  5788. s1z=d2z;
  5789. n1x=-a2x;
  5790. n1y=-a2y;
  5791. n1z=-a2z;
  5792. s2x=d3x;
  5793. s2y=d3y;
  5794. s2z=d3z;
  5795. n2x=-a3x;
  5796. n2y=-a3y;
  5797. n2z=-a3z;
  5798. break;
  5799. case 1:
  5800. if(right){
  5801. cx=p1x+d2x;
  5802. cy=p1y+d2y;
  5803. cz=p1z+d2z;
  5804. nx=a2x;
  5805. ny=a2y;
  5806. nz=a2z;
  5807. }else{
  5808. cx=p1x-d2x;
  5809. cy=p1y-d2y;
  5810. cz=p1z-d2z;
  5811. nx=-a2x;
  5812. ny=-a2y;
  5813. nz=-a2z;
  5814. }
  5815. s1x=d1x;
  5816. s1y=d1y;
  5817. s1z=d1z;
  5818. n1x=-a1x;
  5819. n1y=-a1y;
  5820. n1z=-a1z;
  5821. s2x=d3x;
  5822. s2y=d3y;
  5823. s2z=d3z;
  5824. n2x=-a3x;
  5825. n2y=-a3y;
  5826. n2z=-a3z;
  5827. break;
  5828. case 2:
  5829. if(right){
  5830. cx=p1x+d3x;
  5831. cy=p1y+d3y;
  5832. cz=p1z+d3z;
  5833. nx=a3x;
  5834. ny=a3y;
  5835. nz=a3z;
  5836. }else{
  5837. cx=p1x-d3x;
  5838. cy=p1y-d3y;
  5839. cz=p1z-d3z;
  5840. nx=-a3x;
  5841. ny=-a3y;
  5842. nz=-a3z;
  5843. }
  5844. s1x=d1x;
  5845. s1y=d1y;
  5846. s1z=d1z;
  5847. n1x=-a1x;
  5848. n1y=-a1y;
  5849. n1z=-a1z;
  5850. s2x=d2x;
  5851. s2y=d2y;
  5852. s2z=d2z;
  5853. n2x=-a2x;
  5854. n2y=-a2y;
  5855. n2z=-a2z;
  5856. break;
  5857. case 3:
  5858. swap=true;
  5859. if(!right){
  5860. cx=p2x+d4x;
  5861. cy=p2y+d4y;
  5862. cz=p2z+d4z;
  5863. nx=a4x;
  5864. ny=a4y;
  5865. nz=a4z;
  5866. }else{
  5867. cx=p2x-d4x;
  5868. cy=p2y-d4y;
  5869. cz=p2z-d4z;
  5870. nx=-a4x;
  5871. ny=-a4y;
  5872. nz=-a4z;
  5873. }
  5874. s1x=d5x;
  5875. s1y=d5y;
  5876. s1z=d5z;
  5877. n1x=-a5x;
  5878. n1y=-a5y;
  5879. n1z=-a5z;
  5880. s2x=d6x;
  5881. s2y=d6y;
  5882. s2z=d6z;
  5883. n2x=-a6x;
  5884. n2y=-a6y;
  5885. n2z=-a6z;
  5886. break;
  5887. case 4:
  5888. swap=true;
  5889. if(!right){
  5890. cx=p2x+d5x;
  5891. cy=p2y+d5y;
  5892. cz=p2z+d5z;
  5893. nx=a5x;
  5894. ny=a5y;
  5895. nz=a5z;
  5896. }else{
  5897. cx=p2x-d5x;
  5898. cy=p2y-d5y;
  5899. cz=p2z-d5z;
  5900. nx=-a5x;
  5901. ny=-a5y;
  5902. nz=-a5z;
  5903. }
  5904. s1x=d4x;
  5905. s1y=d4y;
  5906. s1z=d4z;
  5907. n1x=-a4x;
  5908. n1y=-a4y;
  5909. n1z=-a4z;
  5910. s2x=d6x;
  5911. s2y=d6y;
  5912. s2z=d6z;
  5913. n2x=-a6x;
  5914. n2y=-a6y;
  5915. n2z=-a6z;
  5916. break;
  5917. case 5:
  5918. swap=true;
  5919. if(!right){
  5920. cx=p2x+d6x;
  5921. cy=p2y+d6y;
  5922. cz=p2z+d6z;
  5923. nx=a6x;
  5924. ny=a6y;
  5925. nz=a6z;
  5926. }else{
  5927. cx=p2x-d6x;
  5928. cy=p2y-d6y;
  5929. cz=p2z-d6z;
  5930. nx=-a6x;
  5931. ny=-a6y;
  5932. nz=-a6z;
  5933. }
  5934. s1x=d4x;
  5935. s1y=d4y;
  5936. s1z=d4z;
  5937. n1x=-a4x;
  5938. n1y=-a4y;
  5939. n1z=-a4z;
  5940. s2x=d5x;
  5941. s2y=d5y;
  5942. s2z=d5z;
  5943. n2x=-a5x;
  5944. n2y=-a5y;
  5945. n2z=-a5z;
  5946. break;
  5947. case 6:
  5948. nx=a7x;
  5949. ny=a7y;
  5950. nz=a7z;
  5951. n1x=a1x;
  5952. n1y=a1y;
  5953. n1z=a1z;
  5954. n2x=a4x;
  5955. n2y=a4y;
  5956. n2z=a4z;
  5957. break;
  5958. case 7:
  5959. nx=a8x;
  5960. ny=a8y;
  5961. nz=a8z;
  5962. n1x=a1x;
  5963. n1y=a1y;
  5964. n1z=a1z;
  5965. n2x=a5x;
  5966. n2y=a5y;
  5967. n2z=a5z;
  5968. break;
  5969. case 8:
  5970. nx=a9x;
  5971. ny=a9y;
  5972. nz=a9z;
  5973. n1x=a1x;
  5974. n1y=a1y;
  5975. n1z=a1z;
  5976. n2x=a6x;
  5977. n2y=a6y;
  5978. n2z=a6z;
  5979. break;
  5980. case 9:
  5981. nx=aax;
  5982. ny=aay;
  5983. nz=aaz;
  5984. n1x=a2x;
  5985. n1y=a2y;
  5986. n1z=a2z;
  5987. n2x=a4x;
  5988. n2y=a4y;
  5989. n2z=a4z;
  5990. break;
  5991. case 10:
  5992. nx=abx;
  5993. ny=aby;
  5994. nz=abz;
  5995. n1x=a2x;
  5996. n1y=a2y;
  5997. n1z=a2z;
  5998. n2x=a5x;
  5999. n2y=a5y;
  6000. n2z=a5z;
  6001. break;
  6002. case 11:
  6003. nx=acx;
  6004. ny=acy;
  6005. nz=acz;
  6006. n1x=a2x;
  6007. n1y=a2y;
  6008. n1z=a2z;
  6009. n2x=a6x;
  6010. n2y=a6y;
  6011. n2z=a6z;
  6012. break;
  6013. case 12:
  6014. nx=adx;
  6015. ny=ady;
  6016. nz=adz;
  6017. n1x=a3x;
  6018. n1y=a3y;
  6019. n1z=a3z;
  6020. n2x=a4x;
  6021. n2y=a4y;
  6022. n2z=a4z;
  6023. break;
  6024. case 13:
  6025. nx=aex;
  6026. ny=aey;
  6027. nz=aez;
  6028. n1x=a3x;
  6029. n1y=a3y;
  6030. n1z=a3z;
  6031. n2x=a5x;
  6032. n2y=a5y;
  6033. n2z=a5z;
  6034. break;
  6035. case 14:
  6036. nx=afx;
  6037. ny=afy;
  6038. nz=afz;
  6039. n1x=a3x;
  6040. n1y=a3y;
  6041. n1z=a3z;
  6042. n2x=a6x;
  6043. n2y=a6y;
  6044. n2z=a6z;
  6045. break;
  6046. }
  6047. var v;
  6048. if(minIndex>5){
  6049. if(!right){
  6050. nx=-nx;
  6051. ny=-ny;
  6052. nz=-nz;
  6053. }
  6054. var distance;
  6055. var maxDistance;
  6056. var vx;
  6057. var vy;
  6058. var vz;
  6059. var v1x;
  6060. var v1y;
  6061. var v1z;
  6062. var v2x;
  6063. var v2y;
  6064. var v2z;
  6065. //vertex1;
  6066. v1x=V1[0];
  6067. v1y=V1[1];
  6068. v1z=V1[2];
  6069. maxDistance=nx*v1x+ny*v1y+nz*v1z;
  6070. //vertex2;
  6071. vx=V1[3];
  6072. vy=V1[4];
  6073. vz=V1[5];
  6074. distance=nx*vx+ny*vy+nz*vz;
  6075. if(distance>maxDistance){
  6076. maxDistance=distance;
  6077. v1x=vx;
  6078. v1y=vy;
  6079. v1z=vz;
  6080. }
  6081. //vertex3;
  6082. vx=V1[6];
  6083. vy=V1[7];
  6084. vz=V1[8];
  6085. distance=nx*vx+ny*vy+nz*vz;
  6086. if(distance>maxDistance){
  6087. maxDistance=distance;
  6088. v1x=vx;
  6089. v1y=vy;
  6090. v1z=vz;
  6091. }
  6092. //vertex4;
  6093. vx=V1[9];
  6094. vy=V1[10];
  6095. vz=V1[11];
  6096. distance=nx*vx+ny*vy+nz*vz;
  6097. if(distance>maxDistance){
  6098. maxDistance=distance;
  6099. v1x=vx;
  6100. v1y=vy;
  6101. v1z=vz;
  6102. }
  6103. //vertex5;
  6104. vx=V1[12];
  6105. vy=V1[13];
  6106. vz=V1[14];
  6107. distance=nx*vx+ny*vy+nz*vz;
  6108. if(distance>maxDistance){
  6109. maxDistance=distance;
  6110. v1x=vx;
  6111. v1y=vy;
  6112. v1z=vz;
  6113. }
  6114. //vertex6;
  6115. vx=V1[15];
  6116. vy=V1[16];
  6117. vz=V1[17];
  6118. distance=nx*vx+ny*vy+nz*vz;
  6119. if(distance>maxDistance){
  6120. maxDistance=distance;
  6121. v1x=vx;
  6122. v1y=vy;
  6123. v1z=vz;
  6124. }
  6125. //vertex7;
  6126. vx=V1[18];
  6127. vy=V1[19];
  6128. vz=V1[20];
  6129. distance=nx*vx+ny*vy+nz*vz;
  6130. if(distance>maxDistance){
  6131. maxDistance=distance;
  6132. v1x=vx;
  6133. v1y=vy;
  6134. v1z=vz;
  6135. }
  6136. //vertex8;
  6137. vx=V1[21];
  6138. vy=V1[22];
  6139. vz=V1[23];
  6140. distance=nx*vx+ny*vy+nz*vz;
  6141. if(distance>maxDistance){
  6142. maxDistance=distance;
  6143. v1x=vx;
  6144. v1y=vy;
  6145. v1z=vz;
  6146. }
  6147. //vertex1;
  6148. v2x=V2[0];
  6149. v2y=V2[1];
  6150. v2z=V2[2];
  6151. maxDistance=nx*v2x+ny*v2y+nz*v2z;
  6152. //vertex2;
  6153. vx=V2[3];
  6154. vy=V2[4];
  6155. vz=V2[5];
  6156. distance=nx*vx+ny*vy+nz*vz;
  6157. if(distance<maxDistance){
  6158. maxDistance=distance;
  6159. v2x=vx;
  6160. v2y=vy;
  6161. v2z=vz;
  6162. }
  6163. //vertex3;
  6164. vx=V2[6];
  6165. vy=V2[7];
  6166. vz=V2[8];
  6167. distance=nx*vx+ny*vy+nz*vz;
  6168. if(distance<maxDistance){
  6169. maxDistance=distance;
  6170. v2x=vx;
  6171. v2y=vy;
  6172. v2z=vz;
  6173. }
  6174. //vertex4;
  6175. vx=V2[9];
  6176. vy=V2[10];
  6177. vz=V2[11];
  6178. distance=nx*vx+ny*vy+nz*vz;
  6179. if(distance<maxDistance){
  6180. maxDistance=distance;
  6181. v2x=vx;
  6182. v2y=vy;
  6183. v2z=vz;
  6184. }
  6185. //vertex5;
  6186. vx=V2[12];
  6187. vy=V2[13];
  6188. vz=V2[14];
  6189. distance=nx*vx+ny*vy+nz*vz;
  6190. if(distance<maxDistance){
  6191. maxDistance=distance;
  6192. v2x=vx;
  6193. v2y=vy;
  6194. v2z=vz;
  6195. }
  6196. //vertex6;
  6197. vx=V2[15];
  6198. vy=V2[16];
  6199. vz=V2[17];
  6200. distance=nx*vx+ny*vy+nz*vz;
  6201. if(distance<maxDistance){
  6202. maxDistance=distance;
  6203. v2x=vx;
  6204. v2y=vy;
  6205. v2z=vz;
  6206. }
  6207. //vertex7;
  6208. vx=V2[18];
  6209. vy=V2[19];
  6210. vz=V2[20];
  6211. distance=nx*vx+ny*vy+nz*vz;
  6212. if(distance<maxDistance){
  6213. maxDistance=distance;
  6214. v2x=vx;
  6215. v2y=vy;
  6216. v2z=vz;
  6217. }
  6218. //vertex8;
  6219. vx=V2[21];
  6220. vy=V2[22];
  6221. vz=V2[23];
  6222. distance=nx*vx+ny*vy+nz*vz;
  6223. if(distance<maxDistance){
  6224. maxDistance=distance;
  6225. v2x=vx;
  6226. v2y=vy;
  6227. v2z=vz;
  6228. }
  6229. vx=v2x-v1x;
  6230. vy=v2y-v1y;
  6231. vz=v2z-v1z;
  6232. dot1=n1x*n2x+n1y*n2y+n1z*n2z;
  6233. var t=(vx*(n1x-n2x*dot1)+vy*(n1y-n2y*dot1)+vz*(n1z-n2z*dot1))/(1-dot1*dot1);
  6234. 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);
  6235. return;
  6236. }
  6237. var q1x;
  6238. var q1y;
  6239. var q1z;
  6240. var q2x;
  6241. var q2y;
  6242. var q2z;
  6243. var q3x;
  6244. var q3y;
  6245. var q3z;
  6246. var q4x;
  6247. var q4y;
  6248. var q4z;
  6249. var minDot=1;
  6250. var dot=0;
  6251. var minDotIndex=0;
  6252. if(swap){
  6253. dot=a1x*nx+a1y*ny+a1z*nz;
  6254. if(dot<minDot){
  6255. minDot=dot;
  6256. minDotIndex=0;
  6257. }
  6258. if(-dot<minDot){
  6259. minDot=-dot;
  6260. minDotIndex=1;
  6261. }
  6262. dot=a2x*nx+a2y*ny+a2z*nz;
  6263. if(dot<minDot){
  6264. minDot=dot;
  6265. minDotIndex=2;
  6266. }
  6267. if(-dot<minDot){
  6268. minDot=-dot;
  6269. minDotIndex=3;
  6270. }
  6271. dot=a3x*nx+a3y*ny+a3z*nz;
  6272. if(dot<minDot){
  6273. minDot=dot;
  6274. minDotIndex=4;
  6275. }
  6276. if(-dot<minDot){
  6277. minDot=-dot;
  6278. minDotIndex=5;
  6279. }
  6280. switch(minDotIndex){
  6281. case 0:
  6282. //vertex1;
  6283. q1x=V1[0];
  6284. q1y=V1[1];
  6285. q1z=V1[2];
  6286. //vertex3;
  6287. q2x=V1[6];
  6288. q2y=V1[7];
  6289. q2z=V1[8];
  6290. //vertex4;
  6291. q3x=V1[9];
  6292. q3y=V1[10];
  6293. q3z=V1[11];
  6294. //vertex2;
  6295. q4x=V1[3];
  6296. q4y=V1[4];
  6297. q4z=V1[5];
  6298. break;
  6299. case 1:
  6300. //vertex6;
  6301. q1x=V1[15];
  6302. q1y=V1[16];
  6303. q1z=V1[17];
  6304. //vertex8;
  6305. q2x=V1[21];
  6306. q2y=V1[22];
  6307. q2z=V1[23];
  6308. //vertex7;
  6309. q3x=V1[18];
  6310. q3y=V1[19];
  6311. q3z=V1[20];
  6312. //vertex5;
  6313. q4x=V1[12];
  6314. q4y=V1[13];
  6315. q4z=V1[14];
  6316. break;
  6317. case 2:
  6318. //vertex5;
  6319. q1x=V1[12];
  6320. q1y=V1[13];
  6321. q1z=V1[14];
  6322. //vertex1;
  6323. q2x=V1[0];
  6324. q2y=V1[1];
  6325. q2z=V1[2];
  6326. //vertex2;
  6327. q3x=V1[3];
  6328. q3y=V1[4];
  6329. q3z=V1[5];
  6330. //vertex6;
  6331. q4x=V1[15];
  6332. q4y=V1[16];
  6333. q4z=V1[17];
  6334. break;
  6335. case 3:
  6336. //vertex8;
  6337. q1x=V1[21];
  6338. q1y=V1[22];
  6339. q1z=V1[23];
  6340. //vertex4;
  6341. q2x=V1[9];
  6342. q2y=V1[10];
  6343. q2z=V1[11];
  6344. //vertex3;
  6345. q3x=V1[6];
  6346. q3y=V1[7];
  6347. q3z=V1[8];
  6348. //vertex7;
  6349. q4x=V1[18];
  6350. q4y=V1[19];
  6351. q4z=V1[20];
  6352. break;
  6353. case 4:
  6354. //vertex5;
  6355. q1x=V1[12];
  6356. q1y=V1[13];
  6357. q1z=V1[14];
  6358. //vertex7;
  6359. q2x=V1[18];
  6360. q2y=V1[19];
  6361. q2z=V1[20];
  6362. //vertex3;
  6363. q3x=V1[6];
  6364. q3y=V1[7];
  6365. q3z=V1[8];
  6366. //vertex1;
  6367. q4x=V1[0];
  6368. q4y=V1[1];
  6369. q4z=V1[2];
  6370. break;
  6371. case 5:
  6372. //vertex2;
  6373. q1x=V1[3];
  6374. q1y=V1[4];
  6375. q1z=V1[5];
  6376. //vertex4;
  6377. q2x=V1[6];
  6378. q2y=V1[7];
  6379. q2z=V1[8];
  6380. //vertex8;
  6381. q3x=V1[21];
  6382. q3y=V1[22];
  6383. q3z=V1[23];
  6384. //vertex6;
  6385. q4x=V1[15];
  6386. q4y=V1[16];
  6387. q4z=V1[17];
  6388. break;
  6389. }
  6390. }else{
  6391. dot=a4x*nx+a4y*ny+a4z*nz;
  6392. if(dot<minDot){
  6393. minDot=dot;
  6394. minDotIndex=0;
  6395. }
  6396. if(-dot<minDot){
  6397. minDot=-dot;
  6398. minDotIndex=1;
  6399. }
  6400. dot=a5x*nx+a5y*ny+a5z*nz;
  6401. if(dot<minDot){
  6402. minDot=dot;
  6403. minDotIndex=2;
  6404. }
  6405. if(-dot<minDot){
  6406. minDot=-dot;
  6407. minDotIndex=3;
  6408. }
  6409. dot=a6x*nx+a6y*ny+a6z*nz;
  6410. if(dot<minDot){
  6411. minDot=dot;
  6412. minDotIndex=4;
  6413. }
  6414. if(-dot<minDot){
  6415. minDot=-dot;
  6416. minDotIndex=5;
  6417. }
  6418. switch(minDotIndex){
  6419. case 0:
  6420. //vertex1;
  6421. q1x=V2[0];
  6422. q1y=V2[1];
  6423. q1z=V2[2];
  6424. //vertex3;
  6425. q2x=V2[6];
  6426. q2y=V2[7];
  6427. q2z=V2[8];
  6428. //vertex4;
  6429. q3x=V2[9];
  6430. q3y=V2[10];
  6431. q3z=V2[11];
  6432. //vertex2;
  6433. q4x=V2[3];
  6434. q4y=V2[4];
  6435. q4z=V2[5];
  6436. break;
  6437. case 1:
  6438. //vertex6;
  6439. q1x=V2[15];
  6440. q1y=V2[16];
  6441. q1z=V2[17];
  6442. //vertex8;
  6443. q2x=V2[21];
  6444. q2y=V2[22];
  6445. q2z=V2[23];
  6446. //vertex7;
  6447. q3x=V2[18];
  6448. q3y=V2[19];
  6449. q3z=V2[20];
  6450. //vertex5;
  6451. q4x=V2[12];
  6452. q4y=V2[13];
  6453. q4z=V2[14];
  6454. break;
  6455. case 2:
  6456. //vertex5;
  6457. q1x=V2[12];
  6458. q1y=V2[13];
  6459. q1z=V2[14];
  6460. //vertex1;
  6461. q2x=V2[0];
  6462. q2y=V2[1];
  6463. q2z=V2[2];
  6464. //vertex2;
  6465. q3x=V2[3];
  6466. q3y=V2[4];
  6467. q3z=V2[5];
  6468. //vertex6;
  6469. q4x=V2[15];
  6470. q4y=V2[16];
  6471. q4z=V2[17];
  6472. break;
  6473. case 3:
  6474. //vertex8;
  6475. q1x=V2[21];
  6476. q1y=V2[22];
  6477. q1z=V2[23];
  6478. //vertex4;
  6479. q2x=V2[9];
  6480. q2y=V2[10];
  6481. q2z=V2[11];
  6482. //vertex3;
  6483. q3x=V2[6];
  6484. q3y=V2[7];
  6485. q3z=V2[8];
  6486. //vertex7;
  6487. q4x=V2[18];
  6488. q4y=V2[19];
  6489. q4z=V2[20];
  6490. break;
  6491. case 4:
  6492. //ertex5;
  6493. q1x=V2[12];
  6494. q1y=V2[13];
  6495. q1z=V2[14];
  6496. //vertex7;
  6497. q2x=V2[18];
  6498. q2y=V2[19];
  6499. q2z=V2[20];
  6500. //vertex3;
  6501. q3x=V2[6];
  6502. q3y=V2[7];
  6503. q3z=V2[8];
  6504. //vertex1;
  6505. q4x=V2[0];
  6506. q4y=V2[1];
  6507. q4z=V2[2];
  6508. break;
  6509. case 5:
  6510. //vertex2;
  6511. q1x=V2[3];
  6512. q1y=V2[4];
  6513. q1z=V2[5];
  6514. //vertex4;
  6515. q2x=V2[9];
  6516. q2y=V2[10];
  6517. q2z=V2[11];
  6518. //vertex8;
  6519. q3x=V2[21];
  6520. q3y=V2[22];
  6521. q3z=V2[23];
  6522. //vertex6;
  6523. q4x=V2[15];
  6524. q4y=V2[16];
  6525. q4z=V2[17];
  6526. break;
  6527. }
  6528. }
  6529. var numClipVertices;
  6530. var numAddedClipVertices;
  6531. var index;
  6532. var x1;
  6533. var y1;
  6534. var z1;
  6535. var x2;
  6536. var y2;
  6537. var z2;
  6538. this.clipVertices1[0]=q1x;
  6539. this.clipVertices1[1]=q1y;
  6540. this.clipVertices1[2]=q1z;
  6541. this.clipVertices1[3]=q2x;
  6542. this.clipVertices1[4]=q2y;
  6543. this.clipVertices1[5]=q2z;
  6544. this.clipVertices1[6]=q3x;
  6545. this.clipVertices1[7]=q3y;
  6546. this.clipVertices1[8]=q3z;
  6547. this.clipVertices1[9]=q4x;
  6548. this.clipVertices1[10]=q4y;
  6549. this.clipVertices1[11]=q4z;
  6550. numAddedClipVertices=0;
  6551. x1=this.clipVertices1[9];
  6552. y1=this.clipVertices1[10];
  6553. z1=this.clipVertices1[11];
  6554. dot1=(x1-cx-s1x)*n1x+(y1-cy-s1y)*n1y+(z1-cz-s1z)*n1z;
  6555. for(var i=0;i<4;i++){
  6556. index=i*3;
  6557. x2=this.clipVertices1[index];
  6558. y2=this.clipVertices1[index+1];
  6559. z2=this.clipVertices1[index+2];
  6560. dot2=(x2-cx-s1x)*n1x+(y2-cy-s1y)*n1y+(z2-cz-s1z)*n1z;
  6561. if(dot1>0){
  6562. if(dot2>0){
  6563. index=numAddedClipVertices*3;
  6564. numAddedClipVertices++;
  6565. this.clipVertices2[index]=x2;
  6566. this.clipVertices2[index+1]=y2;
  6567. this.clipVertices2[index+2]=z2;
  6568. }else{
  6569. index=numAddedClipVertices*3;
  6570. numAddedClipVertices++;
  6571. t=dot1/(dot1-dot2);
  6572. this.clipVertices2[index]=x1+(x2-x1)*t;
  6573. this.clipVertices2[index+1]=y1+(y2-y1)*t;
  6574. this.clipVertices2[index+2]=z1+(z2-z1)*t;
  6575. }
  6576. }else{
  6577. if(dot2>0){
  6578. index=numAddedClipVertices*3;
  6579. numAddedClipVertices++;
  6580. t=dot1/(dot1-dot2);
  6581. this.clipVertices2[index]=x1+(x2-x1)*t;
  6582. this.clipVertices2[index+1]=y1+(y2-y1)*t;
  6583. this.clipVertices2[index+2]=z1+(z2-z1)*t;
  6584. index=numAddedClipVertices*3;
  6585. numAddedClipVertices++;
  6586. this.clipVertices2[index]=x2;
  6587. this.clipVertices2[index+1]=y2;
  6588. this.clipVertices2[index+2]=z2;
  6589. }
  6590. }
  6591. x1=x2;
  6592. y1=y2;
  6593. z1=z2;
  6594. dot1=dot2;
  6595. }
  6596. numClipVertices=numAddedClipVertices;
  6597. if(numClipVertices==0)return;
  6598. numAddedClipVertices=0;
  6599. index=(numClipVertices-1)*3;
  6600. x1=this.clipVertices2[index];
  6601. y1=this.clipVertices2[index+1];
  6602. z1=this.clipVertices2[index+2];
  6603. dot1=(x1-cx-s2x)*n2x+(y1-cy-s2y)*n2y+(z1-cz-s2z)*n2z;
  6604. for(i=0;i<numClipVertices;i++){
  6605. index=i*3;
  6606. x2=this.clipVertices2[index];
  6607. y2=this.clipVertices2[index+1];
  6608. z2=this.clipVertices2[index+2];
  6609. dot2=(x2-cx-s2x)*n2x+(y2-cy-s2y)*n2y+(z2-cz-s2z)*n2z;
  6610. if(dot1>0){
  6611. if(dot2>0){
  6612. index=numAddedClipVertices*3;
  6613. numAddedClipVertices++;
  6614. this.clipVertices1[index]=x2;
  6615. this.clipVertices1[index+1]=y2;
  6616. this.clipVertices1[index+2]=z2;
  6617. }else{
  6618. index=numAddedClipVertices*3;
  6619. numAddedClipVertices++;
  6620. t=dot1/(dot1-dot2);
  6621. this.clipVertices1[index]=x1+(x2-x1)*t;
  6622. this.clipVertices1[index+1]=y1+(y2-y1)*t;
  6623. this.clipVertices1[index+2]=z1+(z2-z1)*t;
  6624. }
  6625. }else{
  6626. if(dot2>0){
  6627. index=numAddedClipVertices*3;
  6628. numAddedClipVertices++;
  6629. t=dot1/(dot1-dot2);
  6630. this.clipVertices1[index]=x1+(x2-x1)*t;
  6631. this.clipVertices1[index+1]=y1+(y2-y1)*t;
  6632. this.clipVertices1[index+2]=z1+(z2-z1)*t;
  6633. index=numAddedClipVertices*3;
  6634. numAddedClipVertices++;
  6635. this.clipVertices1[index]=x2;
  6636. this.clipVertices1[index+1]=y2;
  6637. this.clipVertices1[index+2]=z2;
  6638. }
  6639. }
  6640. x1=x2;
  6641. y1=y2;
  6642. z1=z2;
  6643. dot1=dot2;
  6644. }
  6645. numClipVertices=numAddedClipVertices;
  6646. if(numClipVertices==0)return;
  6647. numAddedClipVertices=0;
  6648. index=(numClipVertices-1)*3;
  6649. x1=this.clipVertices1[index];
  6650. y1=this.clipVertices1[index+1];
  6651. z1=this.clipVertices1[index+2];
  6652. dot1=(x1-cx+s1x)*-n1x+(y1-cy+s1y)*-n1y+(z1-cz+s1z)*-n1z;
  6653. for(i=0;i<numClipVertices;i++){
  6654. index=i*3;
  6655. x2=this.clipVertices1[index];
  6656. y2=this.clipVertices1[index+1];
  6657. z2=this.clipVertices1[index+2];
  6658. dot2=(x2-cx+s1x)*-n1x+(y2-cy+s1y)*-n1y+(z2-cz+s1z)*-n1z;
  6659. if(dot1>0){
  6660. if(dot2>0){
  6661. index=numAddedClipVertices*3;
  6662. numAddedClipVertices++;
  6663. this.clipVertices2[index]=x2;
  6664. this.clipVertices2[index+1]=y2;
  6665. this.clipVertices2[index+2]=z2;
  6666. }else{
  6667. index=numAddedClipVertices*3;
  6668. numAddedClipVertices++;
  6669. t=dot1/(dot1-dot2);
  6670. this.clipVertices2[index]=x1+(x2-x1)*t;
  6671. this.clipVertices2[index+1]=y1+(y2-y1)*t;
  6672. this.clipVertices2[index+2]=z1+(z2-z1)*t;
  6673. }
  6674. }else{
  6675. if(dot2>0){
  6676. index=numAddedClipVertices*3;
  6677. numAddedClipVertices++;
  6678. t=dot1/(dot1-dot2);
  6679. this.clipVertices2[index]=x1+(x2-x1)*t;
  6680. this.clipVertices2[index+1]=y1+(y2-y1)*t;
  6681. this.clipVertices2[index+2]=z1+(z2-z1)*t;
  6682. index=numAddedClipVertices*3;
  6683. numAddedClipVertices++;
  6684. this.clipVertices2[index]=x2;
  6685. this.clipVertices2[index+1]=y2;
  6686. this.clipVertices2[index+2]=z2;
  6687. }
  6688. }
  6689. x1=x2;
  6690. y1=y2;
  6691. z1=z2;
  6692. dot1=dot2;
  6693. }
  6694. numClipVertices=numAddedClipVertices;
  6695. if(numClipVertices==0)return;
  6696. numAddedClipVertices=0;
  6697. index=(numClipVertices-1)*3;
  6698. x1=this.clipVertices2[index];
  6699. y1=this.clipVertices2[index+1];
  6700. z1=this.clipVertices2[index+2];
  6701. dot1=(x1-cx+s2x)*-n2x+(y1-cy+s2y)*-n2y+(z1-cz+s2z)*-n2z;
  6702. for(i=0;i<numClipVertices;i++){
  6703. index=i*3;
  6704. x2=this.clipVertices2[index];
  6705. y2=this.clipVertices2[index+1];
  6706. z2=this.clipVertices2[index+2];
  6707. dot2=(x2-cx+s2x)*-n2x+(y2-cy+s2y)*-n2y+(z2-cz+s2z)*-n2z;
  6708. if(dot1>0){
  6709. if(dot2>0){
  6710. index=numAddedClipVertices*3;
  6711. numAddedClipVertices++;
  6712. this.clipVertices1[index]=x2;
  6713. this.clipVertices1[index+1]=y2;
  6714. this.clipVertices1[index+2]=z2;
  6715. }else{
  6716. index=numAddedClipVertices*3;
  6717. numAddedClipVertices++;
  6718. t=dot1/(dot1-dot2);
  6719. this.clipVertices1[index]=x1+(x2-x1)*t;
  6720. this.clipVertices1[index+1]=y1+(y2-y1)*t;
  6721. this.clipVertices1[index+2]=z1+(z2-z1)*t;
  6722. }
  6723. }else{
  6724. if(dot2>0){
  6725. index=numAddedClipVertices*3;
  6726. numAddedClipVertices++;
  6727. t=dot1/(dot1-dot2);
  6728. this.clipVertices1[index]=x1+(x2-x1)*t;
  6729. this.clipVertices1[index+1]=y1+(y2-y1)*t;
  6730. this.clipVertices1[index+2]=z1+(z2-z1)*t;
  6731. index=numAddedClipVertices*3;
  6732. numAddedClipVertices++;
  6733. this.clipVertices1[index]=x2;
  6734. this.clipVertices1[index+1]=y2;
  6735. this.clipVertices1[index+2]=z2;
  6736. }
  6737. }
  6738. x1=x2;
  6739. y1=y2;
  6740. z1=z2;
  6741. dot1=dot2;
  6742. }
  6743. numClipVertices=numAddedClipVertices;
  6744. if(swap){
  6745. var tb=b1;
  6746. b1=b2;
  6747. b2=tb;
  6748. }
  6749. if(numClipVertices==0)return;
  6750. var flipped=b1!=shape1;
  6751. if(numClipVertices>4){
  6752. x1=(q1x+q2x+q3x+q4x)*0.25;
  6753. y1=(q1y+q2y+q3y+q4y)*0.25;
  6754. z1=(q1z+q2z+q3z+q4z)*0.25;
  6755. n1x=q1x-x1;
  6756. n1y=q1y-y1;
  6757. n1z=q1z-z1;
  6758. n2x=q2x-x1;
  6759. n2y=q2y-y1;
  6760. n2z=q2z-z1;
  6761. var index1=0;
  6762. var index2=0;
  6763. var index3=0;
  6764. var index4=0;
  6765. var maxDot=-this.INF;
  6766. minDot=this.INF;
  6767. for(i=0;i<numClipVertices;i++){
  6768. this.used[i]=false;
  6769. index=i*3;
  6770. x1=this.clipVertices1[index];
  6771. y1=this.clipVertices1[index+1];
  6772. z1=this.clipVertices1[index+2];
  6773. dot=x1*n1x+y1*n1y+z1*n1z;
  6774. if(dot<minDot){
  6775. minDot=dot;
  6776. index1=i;
  6777. }
  6778. if(dot>maxDot){
  6779. maxDot=dot;
  6780. index3=i;
  6781. }
  6782. }
  6783. this.used[index1]=true;
  6784. this.used[index3]=true;
  6785. maxDot=-this.INF;
  6786. minDot=this.INF;
  6787. for(i=0;i<numClipVertices;i++){
  6788. if(this.used[i])continue;
  6789. index=i*3;
  6790. x1=this.clipVertices1[index];
  6791. y1=this.clipVertices1[index+1];
  6792. z1=this.clipVertices1[index+2];
  6793. dot=x1*n2x+y1*n2y+z1*n2z;
  6794. if(dot<minDot){
  6795. minDot=dot;
  6796. index2=i;
  6797. }
  6798. if(dot>maxDot){
  6799. maxDot=dot;
  6800. index4=i;
  6801. }
  6802. }
  6803. index=index1*3;
  6804. x1=this.clipVertices1[index];
  6805. y1=this.clipVertices1[index+1];
  6806. z1=this.clipVertices1[index+2];
  6807. dot=(x1-cx)*nx+(y1-cy)*ny+(z1-cz)*nz;
  6808. if(dot<0){
  6809. manifold.addPoint(x1,y1,z1,nx,ny,nz,dot,flipped);
  6810. }
  6811. index=index2*3;
  6812. x1=this.clipVertices1[index];
  6813. y1=this.clipVertices1[index+1];
  6814. z1=this.clipVertices1[index+2];
  6815. dot=(x1-cx)*nx+(y1-cy)*ny+(z1-cz)*nz;
  6816. if(dot<0){
  6817. manifold.addPoint(x1,y1,z1,nx,ny,nz,dot,flipped);
  6818. }
  6819. index=index3*3;
  6820. x1=this.clipVertices1[index];
  6821. y1=this.clipVertices1[index+1];
  6822. z1=this.clipVertices1[index+2];
  6823. dot=(x1-cx)*nx+(y1-cy)*ny+(z1-cz)*nz;
  6824. if(dot<0){
  6825. manifold.addPoint(x1,y1,z1,nx,ny,nz,dot,flipped);
  6826. }
  6827. index=index4*3;
  6828. x1=this.clipVertices1[index];
  6829. y1=this.clipVertices1[index+1];
  6830. z1=this.clipVertices1[index+2];
  6831. dot=(x1-cx)*nx+(y1-cy)*ny+(z1-cz)*nz;
  6832. if(dot<0){
  6833. manifold.addPoint(x1,y1,z1,nx,ny,nz,dot,flipped);
  6834. }
  6835. }else{
  6836. for(i=0;i<numClipVertices;i++){
  6837. index=i*3;
  6838. x1=this.clipVertices1[index];
  6839. y1=this.clipVertices1[index+1];
  6840. z1=this.clipVertices1[index+2];
  6841. dot=(x1-cx)*nx+(y1-cy)*ny+(z1-cz)*nz;
  6842. if(dot<0){
  6843. manifold.addPoint(x1,y1,z1,nx,ny,nz,dot,flipped);
  6844. }
  6845. }
  6846. }
  6847. }
  6848. OIMO.SphereBoxCollisionDetector = function(flip){
  6849. OIMO.CollisionDetector.call( this );
  6850. this.flip=flip;
  6851. }
  6852. OIMO.SphereBoxCollisionDetector.prototype = Object.create( OIMO.CollisionDetector.prototype );
  6853. OIMO.SphereBoxCollisionDetector.prototype.detectCollision = function(shape1,shape2,manifold){
  6854. var s;
  6855. var b;
  6856. if(this.flip){
  6857. s=(shape2);
  6858. b=(shape1);
  6859. }else{
  6860. s=(shape1);
  6861. b=(shape2);
  6862. }
  6863. var D = b.dimentions;
  6864. var ps=s.position;
  6865. var psx=ps.x;
  6866. var psy=ps.y;
  6867. var psz=ps.z;
  6868. var pb=b.position;
  6869. var pbx=pb.x;
  6870. var pby=pb.y;
  6871. var pbz=pb.z;
  6872. var rad=s.radius;
  6873. var hw=b.halfWidth;
  6874. var hh=b.halfHeight;
  6875. var hd=b.halfDepth;
  6876. var dx=psx-pbx;
  6877. var dy=psy-pby;
  6878. var dz=psz-pbz;
  6879. var sx=D[0]*dx+D[1]*dy+D[2]*dz;
  6880. var sy=D[3]*dx+D[4]*dy+D[5]*dz;
  6881. var sz=D[6]*dx+D[7]*dy+D[8]*dz;
  6882. var cx;
  6883. var cy;
  6884. var cz;
  6885. var len;
  6886. var invLen;
  6887. var overlap=0;
  6888. if(sx>hw){
  6889. sx=hw;
  6890. }else if(sx<-hw){
  6891. sx=-hw;
  6892. }else{
  6893. overlap=1;
  6894. }
  6895. if(sy>hh){
  6896. sy=hh;
  6897. }else if(sy<-hh){
  6898. sy=-hh;
  6899. }else{
  6900. overlap|=2;
  6901. }
  6902. if(sz>hd){
  6903. sz=hd;
  6904. }else if(sz<-hd){
  6905. sz=-hd;
  6906. }else{
  6907. overlap|=4;
  6908. }
  6909. if(overlap==7){
  6910. if(sx<0){
  6911. dx=hw+sx;
  6912. }else{
  6913. dx=hw-sx;
  6914. }
  6915. if(sy<0){
  6916. dy=hh+sy;
  6917. }else{
  6918. dy=hh-sy;
  6919. }
  6920. if(sz<0){
  6921. dz=hd+sz;
  6922. }else{
  6923. dz=hd-sz;
  6924. }
  6925. if(dx<dy){
  6926. if(dx<dz){
  6927. len=dx-hw;
  6928. if(sx<0){
  6929. sx=-hw;
  6930. dx=D[0];
  6931. dy=D[1];
  6932. dz=D[2];
  6933. }else{
  6934. sx=hw;
  6935. dx=-D[0];
  6936. dy=-D[1];
  6937. dz=-D[2];
  6938. }
  6939. }else{
  6940. len=dz-hd;
  6941. if(sz<0){
  6942. sz=-hd;
  6943. dx=D[6];
  6944. dy=D[7];
  6945. dz=D[8];
  6946. }else{
  6947. sz=hd;
  6948. dx=-D[6];
  6949. dy=-D[7];
  6950. dz=-D[8];
  6951. }
  6952. }
  6953. }else{
  6954. if(dy<dz){
  6955. len=dy-hh;
  6956. if(sy<0){
  6957. sy=-hh;
  6958. dx=D[3];
  6959. dy=D[4];
  6960. dz=D[5];
  6961. }else{
  6962. sy=hh;
  6963. dx=-D[3];
  6964. dy=-D[4];
  6965. dz=-D[5];
  6966. }
  6967. }else{
  6968. len=dz-hd;
  6969. if(sz<0){
  6970. sz=-hd;
  6971. dx=D[6];
  6972. dy=D[7];
  6973. dz=D[8];
  6974. }else{
  6975. sz=hd;
  6976. dx=-D[6];
  6977. dy=-D[7];
  6978. dz=-D[8];
  6979. }
  6980. }
  6981. }
  6982. cx=pbx+sx*D[0]+sy*D[3]+sz*D[6];
  6983. cy=pby+sx*D[1]+sy*D[4]+sz*D[7];
  6984. cz=pbz+sx*D[2]+sy*D[5]+sz*D[8];
  6985. manifold.addPoint(psx+rad*dx,psy+rad*dy,psz+rad*dz,dx,dy,dz,len-rad,this.flip);
  6986. }else{
  6987. cx=pbx+sx*D[0]+sy*D[3]+sz*D[6];
  6988. cy=pby+sx*D[1]+sy*D[4]+sz*D[7];
  6989. cz=pbz+sx*D[2]+sy*D[5]+sz*D[8];
  6990. dx=cx-ps.x;
  6991. dy=cy-ps.y;
  6992. dz=cz-ps.z;
  6993. len=dx*dx+dy*dy+dz*dz;
  6994. if(len>0&&len<rad*rad){
  6995. len=Math.sqrt(len);
  6996. invLen=1/len;
  6997. dx*=invLen;
  6998. dy*=invLen;
  6999. dz*=invLen;
  7000. manifold.addPoint(psx+rad*dx,psy+rad*dy,psz+rad*dz,dx,dy,dz,len-rad,this.flip);
  7001. }
  7002. }
  7003. }
  7004. OIMO.SphereSphereCollisionDetector = function(){
  7005. OIMO.CollisionDetector.call( this );
  7006. }
  7007. OIMO.SphereSphereCollisionDetector.prototype = Object.create( OIMO.CollisionDetector.prototype );
  7008. OIMO.SphereSphereCollisionDetector.prototype.detectCollision = function(shape1,shape2,manifold){
  7009. var s1=(shape1);
  7010. var s2=(shape2);
  7011. var p1=s1.position;
  7012. var p2=s2.position;
  7013. var dx=p2.x-p1.x;
  7014. var dy=p2.y-p1.y;
  7015. var dz=p2.z-p1.z;
  7016. var len=dx*dx+dy*dy+dz*dz;
  7017. var r1=s1.radius;
  7018. var r2=s2.radius;
  7019. var rad=r1+r2;
  7020. if(len>0&&len<rad*rad){
  7021. len=Math.sqrt(len);
  7022. var invLen=1/len;
  7023. dx*=invLen;
  7024. dy*=invLen;
  7025. dz*=invLen;
  7026. manifold.addPoint(p1.x+dx*r1,p1.y+dy*r1,p1.z+dz*r1,dx,dy,dz,len-rad,false);
  7027. }
  7028. }
  7029. OIMO.AABB = function(minX,maxX,minY,maxY,minZ,maxZ){
  7030. this.minX=minX || 0;
  7031. this.maxX=maxX || 0;
  7032. this.minY=minY || 0;
  7033. this.maxY=maxY || 0;
  7034. this.minZ=minZ || 0;
  7035. this.maxZ=maxZ || 0;
  7036. }
  7037. OIMO.AABB.prototype = {
  7038. constructor: OIMO.AABB,
  7039. init:function(minX,maxX,minY,maxY,minZ,maxZ){
  7040. this.minX=minX;
  7041. this.maxX=maxX;
  7042. this.minY=minY;
  7043. this.maxY=maxY;
  7044. this.minZ=minZ;
  7045. this.maxZ=maxZ;
  7046. },
  7047. combine:function(aabb1,aabb2){
  7048. this.minX = (aabb1.minX<aabb2.minX) ? aabb1.minX : aabb2.minX;
  7049. this.maxX = (aabb1.maxX>aabb2.maxX) ? aabb1.maxX : aabb2.maxX;
  7050. this.minY = (aabb1.minY<aabb2.minY) ? aabb1.minY : aabb2.minY;
  7051. this.maxY = (aabb1.maxY>aabb2.maxY) ? aabb1.maxY : aabb2.maxY;
  7052. this.minZ = (aabb1.minZ<aabb2.minZ) ? aabb1.minZ : aabb2.minZ;
  7053. this.maxZ = (aabb1.maxZ>aabb2.maxZ) ? aabb1.maxZ : aabb2.maxZ;
  7054. /*var margin=0;
  7055. this.minX-=margin;
  7056. this.minY-=margin;
  7057. this.minZ-=margin;
  7058. this.maxX+=margin;
  7059. this.maxY+=margin;
  7060. this.maxZ+=margin;*/
  7061. },
  7062. surfaceArea:function(){
  7063. var h=this.maxY-this.minY;
  7064. var d=this.maxZ-this.minZ;
  7065. return 2*((this.maxX-this.minX)*(h+d)+h*d);
  7066. },
  7067. intersectsWithPoint:function(x,y,z){
  7068. return x>=this.minX&&x<=this.maxX&&y>=this.minY&&y<=this.maxY&&z>=this.minZ&&z<=this.maxZ;
  7069. }
  7070. }
  7071. OIMO.Proxy = function(shape){
  7072. this.shape=shape;
  7073. this.aabb=shape.aabb;
  7074. };
  7075. OIMO.Proxy.prototype = {
  7076. constructor: OIMO.Proxy,
  7077. update:function(){
  7078. throw new Error("Inheritance error.");
  7079. }
  7080. }
  7081. OIMO.BasicProxy = function(shape){
  7082. OIMO.Proxy.call( this, shape );
  7083. }
  7084. OIMO.BasicProxy.prototype = Object.create( OIMO.Proxy.prototype );
  7085. OIMO.BasicProxy.prototype.update = function () {
  7086. }
  7087. OIMO.BroadPhase = function(){
  7088. this.types = 0x0;
  7089. this.numPairChecks=0;
  7090. this.numPairs=0;
  7091. this.bufferSize=256;
  7092. this.pairs=[];// vector
  7093. this.pairs.length = this.bufferSize;
  7094. var i = this.bufferSize;
  7095. while(i--){
  7096. this.pairs[i] = new OIMO.Pair();
  7097. }
  7098. }
  7099. OIMO.BroadPhase.prototype = {
  7100. constructor: OIMO.BroadPhase,
  7101. createProxy:function(shape){
  7102. throw new Error("Inheritance error.");
  7103. },
  7104. addProxy:function(proxy){
  7105. throw new Error("Inheritance error.");
  7106. },
  7107. removeProxy:function(proxy){
  7108. throw new Error("Inheritance error.");
  7109. },
  7110. isAvailablePair:function(s1,s2){
  7111. var b1=s1.parent;
  7112. var b2=s2.parent;
  7113. if(
  7114. b1==b2||
  7115. (!b1.isDynamic&&!b2.isDynamic)||
  7116. (s1.belongsTo&s2.collidesWith)==0||
  7117. (s2.belongsTo&s1.collidesWith)==0
  7118. ){
  7119. return false;
  7120. }
  7121. var js;
  7122. if(b1.numJoints<b2.numJoints)js=b1.jointLink;
  7123. else js=b2.jointLink;
  7124. while(js!=null){
  7125. var joint=js.joint;
  7126. if(
  7127. !joint.allowCollision&&
  7128. (joint.body1==b1&&joint.body2==b2||
  7129. joint.body1==b2&&joint.body2==b1)
  7130. ){
  7131. return false;
  7132. }
  7133. js=js.next;
  7134. }
  7135. return true;
  7136. },
  7137. detectPairs:function(){
  7138. while(this.numPairs>0){
  7139. var pair=this.pairs[--this.numPairs];
  7140. pair.shape1=null;
  7141. pair.shape2=null;
  7142. }
  7143. this.numPairChecks=0;
  7144. this.collectPairs();
  7145. },
  7146. collectPairs:function(){
  7147. throw new Error("Inheritance error.");
  7148. },
  7149. addPair:function(s1,s2){
  7150. if(this.numPairs==this.bufferSize){
  7151. //var newBufferSize=this.bufferSize<<1;
  7152. var newBufferSize=this.bufferSize*2;
  7153. var newPairs=[];// vector
  7154. newPairs.length = this.bufferSize;
  7155. var i = this.bufferSize;
  7156. var j;
  7157. while(i--){
  7158. //for(var i=0, j=this.bufferSize;i<j;i++){
  7159. newPairs[i]=this.pairs[i];
  7160. }
  7161. for(i=this.bufferSize, j=newBufferSize;i<j;i++){
  7162. newPairs[i]=new OIMO.Pair();
  7163. }
  7164. this.pairs=newPairs;
  7165. this.bufferSize=newBufferSize;
  7166. }
  7167. var pair=this.pairs[this.numPairs++];
  7168. pair.shape1=s1;
  7169. pair.shape2=s2;
  7170. }
  7171. }
  7172. OIMO.BruteForceBroadPhase = function(){
  7173. OIMO.BroadPhase.call( this);
  7174. this.types = 0x1;
  7175. this.numProxies=0;
  7176. this.maxProxies = 256;
  7177. this.proxies = [];// Vector !
  7178. this.proxies.length = 256;
  7179. }
  7180. OIMO.BruteForceBroadPhase.prototype = Object.create( OIMO.BroadPhase.prototype );
  7181. OIMO.BruteForceBroadPhase.prototype.createProxy = function (shape) {
  7182. return new OIMO.BasicProxy(shape);
  7183. }
  7184. OIMO.BruteForceBroadPhase.prototype.addProxy = function (proxy) {
  7185. if(this.numProxies==this.maxProxies){
  7186. //this.maxProxies<<=1;
  7187. this.maxProxies*=2;
  7188. var newProxies=[];
  7189. var i = this.numProxies;
  7190. while(i--){
  7191. //for(var i=0, l=this.numProxies;i<l;i++){
  7192. newProxies[i]=this.proxies[i];
  7193. }
  7194. this.proxies=newProxies;
  7195. }
  7196. this.proxies[this.numProxies++]=proxy;
  7197. }
  7198. OIMO.BruteForceBroadPhase.prototype.removeProxy = function (proxy) {
  7199. for(var i=0, l=this.numProxies;i<l;i++){
  7200. if(this.proxies[i]==proxy){
  7201. this.proxies[i]=this.proxies[--this.numProxies];
  7202. this.proxies[this.numProxies]=null;
  7203. return;
  7204. }
  7205. }
  7206. }
  7207. OIMO.BruteForceBroadPhase.prototype.collectPairs = function () {
  7208. this.numPairChecks=this.numProxies*(this.numProxies-1)>>1;
  7209. //this.numPairChecks=this.numProxies*(this.numProxies-1)*0.5;
  7210. var i = this.numProxies;
  7211. while(i--){
  7212. //for(var i=0, l=this.numProxies;i<l;i++){
  7213. var p1=this.proxies[i];
  7214. var b1=p1.aabb;
  7215. var s1=p1.shape;
  7216. var j = this.numProxies;
  7217. while(j--){ if(j!==0){
  7218. //for(var j=i+1, m=this.numProxies;j<m;j++){
  7219. var p2=this.proxies[j];
  7220. var b2=p2.aabb;
  7221. var s2=p2.shape;
  7222. if(b1.maxX<b2.minX||b1.minX>b2.maxX|| b1.maxY<b2.minY||b1.minY>b2.maxY|| b1.maxZ<b2.minZ||b1.minZ>b2.maxZ|| !this.isAvailablePair(s1,s2) ){
  7223. continue;
  7224. }
  7225. this.addPair(s1,s2);
  7226. }}
  7227. }
  7228. }
  7229. OIMO.Pair = function(){
  7230. this.shape1=null;
  7231. this.shape2=null;
  7232. };
  7233. OIMO.SAPAxis = function(){
  7234. this.numElements=0;
  7235. this.stack=[];// vector !
  7236. this.stack.length = 64;
  7237. this.bufferSize=256;
  7238. this.elements=[];// vector !
  7239. this.elements.length = this.bufferSize;
  7240. };
  7241. OIMO.SAPAxis.prototype = {
  7242. constructor: OIMO.SAPAxis,
  7243. addElements:function(min,max){
  7244. if(this.numElements+2>=this.bufferSize){
  7245. //this.bufferSize<<=1;
  7246. this.bufferSize*=2;
  7247. var newElements=[];
  7248. for(var i=0, l=this.numElements; i<l; i++){
  7249. newElements[i]=this.elements[i];
  7250. }
  7251. }
  7252. this.elements[this.numElements++]=min;
  7253. this.elements[this.numElements++]=max;
  7254. },
  7255. removeElements:function(min,max){
  7256. var minIndex=-1;
  7257. var maxIndex=-1;
  7258. for(var i=0, l=this.numElements; i<l; i++){
  7259. var e=this.elements[i];
  7260. if(e==min||e==max){
  7261. if(minIndex==-1){
  7262. minIndex=i;
  7263. }else{
  7264. maxIndex=i;
  7265. break;
  7266. }
  7267. }
  7268. }
  7269. for(i=minIndex+1, l=maxIndex; i<l; i++){
  7270. this.elements[i-1]=this.elements[i];
  7271. }
  7272. for(i=maxIndex+1, l=this.numElements; i<l; i++){
  7273. this.elements[i-2]=this.elements[i];
  7274. }
  7275. this.elements[--this.numElements]=null;
  7276. this.elements[--this.numElements]=null;
  7277. },
  7278. sort:function(){
  7279. var count=0;
  7280. var threshold=1;
  7281. while((this.numElements>>threshold)!=0)threshold++;
  7282. threshold=threshold*this.numElements>>2;
  7283. count=0;
  7284. var giveup=false;
  7285. var elements=this.elements;
  7286. for(var i=1, l=this.numElements; i<l; i++){
  7287. var tmp=elements[i];
  7288. var pivot=tmp.value;
  7289. var tmp2=elements[i-1];
  7290. if(tmp2.value>pivot){
  7291. var j=i;
  7292. do{
  7293. elements[j]=tmp2;
  7294. if(--j==0)break;
  7295. tmp2=elements[j-1];
  7296. }while(tmp2.value>pivot);
  7297. elements[j]=tmp;
  7298. count+=i-j;
  7299. if(count>threshold){
  7300. giveup=true;
  7301. break;
  7302. }
  7303. }
  7304. }
  7305. if(!giveup)return;
  7306. count=2;var stack=this.stack;
  7307. stack[0]=0;
  7308. stack[1]=this.numElements-1;
  7309. while(count>0){
  7310. var right=stack[--count];
  7311. var left=stack[--count];
  7312. var diff=right-left;
  7313. if(diff>16){
  7314. var mid=left+(diff>>1);
  7315. //var mid=left+(diff*0.5);
  7316. tmp=elements[mid];
  7317. elements[mid]=elements[right];
  7318. elements[right]=tmp;
  7319. pivot=tmp.value;
  7320. i=left-1;
  7321. j=right;
  7322. while(true){
  7323. var ei;
  7324. var ej;
  7325. do{
  7326. ei=elements[++i];
  7327. }while(ei.value<pivot);
  7328. do{
  7329. ej=elements[--j];
  7330. }while(pivot<ej.value&&j!=left);
  7331. if(i>=j)break;
  7332. elements[i]=ej;
  7333. elements[j]=ei;
  7334. }
  7335. elements[right]=elements[i];
  7336. elements[i]=tmp;
  7337. if(i-left>right-i){
  7338. stack[count++]=left;
  7339. stack[count++]=i-1;
  7340. stack[count++]=i+1;
  7341. stack[count++]=right;
  7342. }else{
  7343. stack[count++]=i+1;
  7344. stack[count++]=right;
  7345. stack[count++]=left;
  7346. stack[count++]=i-1;
  7347. }
  7348. }else{
  7349. for(i=left+1;i<=right;i++){
  7350. tmp=elements[i];
  7351. pivot=tmp.value;
  7352. tmp2=elements[i-1];
  7353. if(tmp2.value>pivot){
  7354. j=i;
  7355. do{
  7356. elements[j]=tmp2;
  7357. if(--j==0)break;
  7358. tmp2=elements[j-1];
  7359. }while(tmp2.value>pivot);
  7360. elements[j]=tmp;
  7361. }
  7362. }
  7363. }
  7364. }
  7365. },
  7366. calculateTestCount:function(){
  7367. var num=1;
  7368. var sum=0;
  7369. for(var i=1, l=this.numElements; i<l; i++){
  7370. if(this.elements[i].max){
  7371. num--;
  7372. }else{
  7373. sum+=num;
  7374. num++;
  7375. }
  7376. }
  7377. return sum;
  7378. }
  7379. }
  7380. OIMO.SAPBroadPhase = function(){
  7381. OIMO.BroadPhase.call( this);
  7382. this.types = 0x2;
  7383. this.numElementsD = 0;
  7384. this.numElementsS = 0;
  7385. this.axesD = [];// vector !
  7386. this.axesS = [];// vector !
  7387. this.axesD.length = 3;
  7388. this.axesS.length = 3;
  7389. this.axesD[0]=new OIMO.SAPAxis();
  7390. this.axesD[1]=new OIMO.SAPAxis();
  7391. this.axesD[2]=new OIMO.SAPAxis();
  7392. this.axesS[0]=new OIMO.SAPAxis();
  7393. this.axesS[1]=new OIMO.SAPAxis();
  7394. this.axesS[2]=new OIMO.SAPAxis();
  7395. this.index1=0;
  7396. this.index2=1;
  7397. }
  7398. OIMO.SAPBroadPhase.prototype = Object.create( OIMO.BroadPhase.prototype );
  7399. OIMO.SAPBroadPhase.prototype.createProxy = function (shape) {
  7400. return new OIMO.SAPProxy(this,shape);
  7401. }
  7402. OIMO.SAPBroadPhase.prototype.addProxy = function (proxy) {
  7403. var p=(proxy);
  7404. if(p.isDynamic()){
  7405. this.axesD[0].addElements(p.min[0],p.max[0]);
  7406. this.axesD[1].addElements(p.min[1],p.max[1]);
  7407. this.axesD[2].addElements(p.min[2],p.max[2]);
  7408. p.belongsTo=1;
  7409. this.numElementsD+=2;
  7410. }else{
  7411. this.axesS[0].addElements(p.min[0],p.max[0]);
  7412. this.axesS[1].addElements(p.min[1],p.max[1]);
  7413. this.axesS[2].addElements(p.min[2],p.max[2]);
  7414. p.belongsTo=2;
  7415. this.numElementsS+=2;
  7416. }
  7417. }
  7418. OIMO.SAPBroadPhase.prototype.removeProxy = function (proxy) {
  7419. var p=(proxy);
  7420. if(p.belongsTo==0)return;
  7421. switch(p.belongsTo){
  7422. case 1:
  7423. this.axesD[0].removeElements(p.min[0],p.max[0]);
  7424. this.axesD[1].removeElements(p.min[1],p.max[1]);
  7425. this.axesD[2].removeElements(p.min[2],p.max[2]);
  7426. this.numElementsD-=2;
  7427. break;
  7428. case 2:
  7429. this.axesS[0].removeElements(p.min[0],p.max[0]);
  7430. this.axesS[1].removeElements(p.min[1],p.max[1]);
  7431. this.axesS[2].removeElements(p.min[2],p.max[2]);
  7432. this.numElementsS-=2;
  7433. break;
  7434. }
  7435. p.belongsTo=0;
  7436. }
  7437. OIMO.SAPBroadPhase.prototype.collectPairs = function () {
  7438. if(this.numElementsD==0)return;
  7439. var axis1=this.axesD[this.index1];
  7440. var axis2=this.axesD[this.index2];
  7441. axis1.sort();
  7442. axis2.sort();
  7443. var count1=axis1.calculateTestCount();
  7444. var count2=axis2.calculateTestCount();
  7445. var elementsD;
  7446. var elementsS;
  7447. if(count1<=count2){
  7448. axis2=this.axesS[this.index1];
  7449. axis2.sort();
  7450. elementsD=axis1.elements;
  7451. elementsS=axis2.elements;
  7452. }else{
  7453. axis1=this.axesS[this.index2];
  7454. axis1.sort();
  7455. elementsD=axis2.elements;
  7456. elementsS=axis1.elements;
  7457. this.index1^=this.index2;
  7458. this.index2^=this.index1;
  7459. this.index1^=this.index2;
  7460. }
  7461. var activeD;
  7462. var activeS;
  7463. var p=0;
  7464. var q=0;
  7465. while(p<this.numElementsD){
  7466. var e1;
  7467. var dyn;
  7468. if(q==this.numElementsS){
  7469. e1=elementsD[p];
  7470. dyn=true;
  7471. p++;
  7472. }else{
  7473. var d=elementsD[p];
  7474. var s=elementsS[q];
  7475. if(d.value<s.value){
  7476. e1=d;
  7477. dyn=true;
  7478. p++;
  7479. }else{
  7480. e1=s;
  7481. dyn=false;
  7482. q++;
  7483. }
  7484. }
  7485. if(!e1.max){
  7486. var s1=e1.proxy.shape;var min1=e1.min1.value;var max1=e1.max1.value;var min2=e1.min2.value;var max2=e1.max2.value;
  7487. for(var e2=activeD;e2!=null;e2=e2.pair){
  7488. var s2=e2.proxy.shape;
  7489. this.numPairChecks++;
  7490. if(
  7491. min1>e2.max1.value||max1<e2.min1.value||
  7492. min2>e2.max2.value||max2<e2.min2.value||
  7493. !this.isAvailablePair(s1,s2)
  7494. ){
  7495. continue;
  7496. }
  7497. this.addPair(s1,s2);
  7498. }
  7499. if(dyn){
  7500. for(e2=activeS;e2!=null;e2=e2.pair){
  7501. s2=e2.proxy.shape;
  7502. this.numPairChecks++;
  7503. if(
  7504. min1>e2.max1.value||max1<e2.min1.value||
  7505. min2>e2.max2.value||max2<e2.min2.value||
  7506. !this.isAvailablePair(s1,s2)
  7507. ){
  7508. continue;
  7509. }
  7510. this.addPair(s1,s2);
  7511. }
  7512. e1.pair=activeD;
  7513. activeD=e1;
  7514. }else{
  7515. e1.pair=activeS;
  7516. activeS=e1;
  7517. }
  7518. }else{
  7519. var min=e1.pair;
  7520. if(dyn){
  7521. if(min==activeD){
  7522. activeD=activeD.pair;
  7523. continue;
  7524. }else{
  7525. e1=activeD;
  7526. }
  7527. }else{
  7528. if(min==activeS){
  7529. activeS=activeS.pair;
  7530. continue;
  7531. }else{
  7532. e1=activeS;
  7533. }
  7534. }
  7535. do{
  7536. e2=e1.pair;
  7537. if(e2==min){
  7538. e1.pair=e2.pair;
  7539. break;
  7540. }
  7541. e1=e2;
  7542. }while(e1!=null);
  7543. }
  7544. }
  7545. this.index2=(this.index1|this.index2)^3;
  7546. }
  7547. OIMO.SAPElement = function(proxy,max){
  7548. this.pair = null;
  7549. this.min1 = null;
  7550. this.max1 = null;
  7551. this.min2 = null;
  7552. this.max2 = null;
  7553. this.proxy=proxy;
  7554. this.max=max;
  7555. this.value=0;
  7556. };
  7557. OIMO.SAPProxy = function(sap,shape){
  7558. OIMO.Proxy.call( this, shape);
  7559. this.belongsTo = 0;
  7560. this.max = [];
  7561. this.min = [];
  7562. this.sap=sap;
  7563. this.min[0]=new OIMO.SAPElement(this,false);
  7564. this.max[0]=new OIMO.SAPElement(this,true);
  7565. this.min[1]=new OIMO.SAPElement(this,false);
  7566. this.max[1]=new OIMO.SAPElement(this,true);
  7567. this.min[2]=new OIMO.SAPElement(this,false);
  7568. this.max[2]=new OIMO.SAPElement(this,true);
  7569. this.max[0].pair=this.min[0];
  7570. this.max[1].pair=this.min[1];
  7571. this.max[2].pair=this.min[2];
  7572. this.min[0].min1=this.min[1];
  7573. this.min[0].max1=this.max[1];
  7574. this.min[0].min2=this.min[2];
  7575. this.min[0].max2=this.max[2];
  7576. this.min[1].min1=this.min[0];
  7577. this.min[1].max1=this.max[0];
  7578. this.min[1].min2=this.min[2];
  7579. this.min[1].max2=this.max[2];
  7580. this.min[2].min1=this.min[0];
  7581. this.min[2].max1=this.max[0];
  7582. this.min[2].min2=this.min[1];
  7583. this.min[2].max2=this.max[1];
  7584. };
  7585. OIMO.SAPProxy.prototype = Object.create( OIMO.Proxy.prototype );
  7586. OIMO.SAPProxy.prototype.isDynamic = function () {
  7587. var body=this.shape.parent;
  7588. return body.isDynamic&&!body.sleeping;
  7589. }
  7590. OIMO.SAPProxy.prototype.update = function () {
  7591. this.min[0].value=this.aabb.minX;
  7592. this.max[0].value=this.aabb.maxX;
  7593. this.min[1].value=this.aabb.minY;
  7594. this.max[1].value=this.aabb.maxY;
  7595. this.min[2].value=this.aabb.minZ;
  7596. this.max[2].value=this.aabb.maxZ;
  7597. if(this.belongsTo==1&&!this.isDynamic()||this.belongsTo==2&&this.isDynamic()){
  7598. this.sap.removeProxy(this);
  7599. this.sap.addProxy(this);
  7600. }
  7601. }
  7602. OIMO.DBVT = function(){
  7603. this.root=null;
  7604. this.freeNodes=[];// vector
  7605. this.freeNodes.length = 16384;
  7606. this.numFreeNodes=0;
  7607. this.aabb=new OIMO.AABB();
  7608. }
  7609. OIMO.DBVT.prototype = {
  7610. constructor: OIMO.DBVT,
  7611. moveLeaf:function(leaf){
  7612. this.deleteLeaf(leaf);
  7613. this.insertLeaf(leaf);
  7614. },
  7615. insertLeaf:function(leaf){
  7616. if(this.root==null){
  7617. this.root=leaf;
  7618. return;
  7619. }
  7620. var lb=leaf.aabb;
  7621. var sibling=this.root;
  7622. var oldArea;
  7623. var newArea;
  7624. while(sibling.proxy==null){
  7625. var c1=sibling.child1;
  7626. var c2=sibling.child2;
  7627. var b=sibling.aabb;
  7628. var c1b=c1.aabb;
  7629. var c2b=c2.aabb;
  7630. oldArea=b.surfaceArea();
  7631. this.aabb.combine(lb,b);
  7632. newArea=this.aabb.surfaceArea();
  7633. var creatingCost=newArea*2;
  7634. var incrementalCost=(newArea-oldArea)*2;
  7635. var discendingCost1=incrementalCost;
  7636. this.aabb.combine(lb,c1b);
  7637. if(c1.proxy!=null){
  7638. discendingCost1+=this.aabb.surfaceArea();
  7639. }else{
  7640. discendingCost1+=this.aabb.surfaceArea()-c1b.surfaceArea();
  7641. }
  7642. var discendingCost2=incrementalCost;
  7643. this.aabb.combine(lb,c2b);
  7644. if(c2.proxy!=null){
  7645. discendingCost2+=this.aabb.surfaceArea();
  7646. }else{
  7647. discendingCost2+=this.aabb.surfaceArea()-c2b.surfaceArea();
  7648. }
  7649. if(discendingCost1<discendingCost2){
  7650. if(creatingCost<discendingCost1){
  7651. break;
  7652. }else{
  7653. sibling=c1;
  7654. }
  7655. }else{
  7656. if(creatingCost<discendingCost2){
  7657. break;
  7658. }else{
  7659. sibling=c2;
  7660. }
  7661. }
  7662. }
  7663. var oldParent=sibling.parent;
  7664. var newParent;
  7665. if(this.numFreeNodes>0){
  7666. newParent=this.freeNodes[--this.numFreeNodes];
  7667. }else{
  7668. newParent=new OIMO.DBVTNode();
  7669. }
  7670. newParent.parent=oldParent;
  7671. newParent.child1=leaf;
  7672. newParent.child2=sibling;
  7673. newParent.aabb.combine(leaf.aabb,sibling.aabb);
  7674. newParent.height=sibling.height+1;
  7675. sibling.parent=newParent;
  7676. leaf.parent=newParent;
  7677. if(sibling==this.root){
  7678. // replace root
  7679. this.root=newParent;
  7680. }else{
  7681. // replace child
  7682. if(oldParent.child1==sibling){
  7683. oldParent.child1=newParent;
  7684. }else{
  7685. oldParent.child2=newParent;
  7686. }
  7687. }
  7688. // update whole tree
  7689. do{
  7690. newParent=this.balance(newParent);
  7691. this.fix(newParent);
  7692. newParent=newParent.parent;
  7693. }while(newParent!=null);
  7694. },
  7695. getBalance:function(node){
  7696. if(node.proxy!=null)return 0;
  7697. return node.child1.height-node.child2.height;
  7698. },
  7699. print:function(node,indent,text){
  7700. var hasChild=node.proxy==null;
  7701. if(hasChild)text=this.print(node.child1,indent+1,text);
  7702. for(var i=indent*2;i>=0;i--){
  7703. text+=" ";
  7704. }
  7705. text+=(hasChild?this.getBalance(node):"["+node.proxy.aabb.minX+"]")+"\n";
  7706. if(hasChild)text=this.print(node.child2,indent+1,text);
  7707. return text;
  7708. },
  7709. deleteLeaf:function(leaf){
  7710. if(leaf==this.root){
  7711. this.root=null;
  7712. return;
  7713. }
  7714. var parent=leaf.parent;
  7715. var sibling;
  7716. if(parent.child1==leaf){
  7717. sibling=parent.child2;
  7718. }else{
  7719. sibling=parent.child1;
  7720. }
  7721. if(parent==this.root){
  7722. this.root=sibling;
  7723. sibling.parent=null;
  7724. return;
  7725. }
  7726. var grandParent=parent.parent;
  7727. sibling.parent=grandParent;
  7728. if(grandParent.child1==parent){
  7729. grandParent.child1=sibling;
  7730. }else{
  7731. grandParent.child2=sibling;
  7732. }
  7733. if(this.numFreeNodes<16384){
  7734. this.freeNodes[this.numFreeNodes++]=parent;
  7735. }
  7736. do{
  7737. grandParent=this.balance(grandParent);
  7738. this.fix(grandParent);
  7739. grandParent=grandParent.parent;
  7740. }while(grandParent!=null);
  7741. },
  7742. balance:function(node){
  7743. var nh=node.height;
  7744. if(nh<2){
  7745. return node;
  7746. }
  7747. var p=node.parent;
  7748. var l=node.child1;
  7749. var r=node.child2;
  7750. var lh=l.height;
  7751. var rh=r.height;
  7752. var balance=lh-rh;
  7753. var t;// for bit operation
  7754. // [ N ]
  7755. // / \
  7756. // [ L ] [ R ]
  7757. // / \ / \
  7758. // [L-L] [L-R] [R-L] [R-R]
  7759. // Is the tree balanced?
  7760. if(balance>1){
  7761. var ll=l.child1;
  7762. var lr=l.child2;
  7763. var llh=ll.height;
  7764. var lrh=lr.height;
  7765. // Is L-L higher than L-R?
  7766. if(llh>lrh){
  7767. l.child2=node;
  7768. node.parent=l;
  7769. // [ L ]
  7770. // / \
  7771. // [L-L] [ N ]
  7772. // / \ / \
  7773. // [...] [...] [ L ] [ R ]
  7774. // set L-R
  7775. node.child1=lr;
  7776. lr.parent=node;
  7777. // [ L ]
  7778. // / \
  7779. // [L-L] [ N ]
  7780. // / \ / \
  7781. // [...] [...] [L-R] [ R ]
  7782. // fix bounds and heights
  7783. node.aabb.combine(lr.aabb,r.aabb);
  7784. t=lrh-rh;
  7785. node.height=lrh-(t&t>>31)+1;
  7786. l.aabb.combine(ll.aabb,node.aabb);
  7787. t=llh-nh;
  7788. l.height=llh-(t&t>>31)+1;
  7789. }else{
  7790. // set N to L-L
  7791. l.child1=node;
  7792. node.parent=l;
  7793. // [ L ]
  7794. // / \
  7795. // [ N ] [L-R]
  7796. // / \ / \
  7797. // [ L ] [ R ] [...] [...]
  7798. // set L-L
  7799. node.child1=ll;
  7800. ll.parent=node;
  7801. // [ L ]
  7802. // / \
  7803. // [ N ] [L-R]
  7804. // / \ / \
  7805. // [L-L] [ R ] [...] [...]
  7806. // fix bounds and heights
  7807. node.aabb.combine(ll.aabb,r.aabb);
  7808. t=llh-rh;
  7809. node.height=llh-(t&t>>31)+1;
  7810. l.aabb.combine(node.aabb,lr.aabb);
  7811. t=nh-lrh;
  7812. l.height=nh-(t&t>>31)+1;
  7813. }
  7814. // set new parent of L
  7815. if(p!=null){
  7816. if(p.child1==node){
  7817. p.child1=l;
  7818. }else{
  7819. p.child2=l;
  7820. }
  7821. }else{
  7822. this.root=l;
  7823. }
  7824. l.parent=p;
  7825. return l;
  7826. }else if(balance<-1){
  7827. var rl=r.child1;
  7828. var rr=r.child2;
  7829. var rlh=rl.height;
  7830. var rrh=rr.height;
  7831. // Is R-L higher than R-R?
  7832. if(rlh>rrh){
  7833. // set N to R-R
  7834. r.child2=node;
  7835. node.parent=r;
  7836. // [ R ]
  7837. // / \
  7838. // [R-L] [ N ]
  7839. // / \ / \
  7840. // [...] [...] [ L ] [ R ]
  7841. // set R-R
  7842. node.child2=rr;
  7843. rr.parent=node;
  7844. // [ R ]
  7845. // / \
  7846. // [R-L] [ N ]
  7847. // / \ / \
  7848. // [...] [...] [ L ] [R-R]
  7849. // fix bounds and heights
  7850. node.aabb.combine(l.aabb,rr.aabb);
  7851. t=lh-rrh;
  7852. node.height=lh-(t&t>>31)+1;
  7853. r.aabb.combine(rl.aabb,node.aabb);
  7854. t=rlh-nh;
  7855. r.height=rlh-(t&t>>31)+1;
  7856. }else{
  7857. // set N to R-L
  7858. r.child1=node;
  7859. node.parent=r;
  7860. // [ R ]
  7861. // / \
  7862. // [ N ] [R-R]
  7863. // / \ / \
  7864. // [ L ] [ R ] [...] [...]
  7865. // set R-L
  7866. node.child2=rl;
  7867. rl.parent=node;
  7868. // [ R ]
  7869. // / \
  7870. // [ N ] [R-R]
  7871. // / \ / \
  7872. // [ L ] [R-L] [...] [...]
  7873. // fix bounds and heights
  7874. node.aabb.combine(l.aabb,rl.aabb);
  7875. t=lh-rlh;
  7876. node.height=lh-(t&t>>31)+1;
  7877. r.aabb.combine(node.aabb,rr.aabb);
  7878. t=nh-rrh;
  7879. r.height=nh-(t&t>>31)+1;
  7880. }
  7881. // set new parent of R
  7882. if(p!=null){
  7883. if(p.child1==node){
  7884. p.child1=r;
  7885. }else{
  7886. p.child2=r;
  7887. }
  7888. }else{
  7889. this.root=r;
  7890. }
  7891. r.parent=p;
  7892. return r;
  7893. }
  7894. return node;
  7895. },
  7896. fix:function(node){
  7897. var c1=node.child1;
  7898. var c2=node.child2;
  7899. node.aabb.combine(c1.aabb,c2.aabb);
  7900. var h1=c1.height;
  7901. var h2=c2.height;
  7902. if(h1<h2){
  7903. node.height=h2+1;
  7904. }else{
  7905. node.height=h1+1;
  7906. }
  7907. }
  7908. }
  7909. OIMO.DBVTBroadPhase = function(){
  7910. OIMO.BroadPhase.call( this);
  7911. this.types = 0x3;
  7912. this.numLeaves = 0;
  7913. this.maxLeaves = 0;
  7914. this.tree=new OIMO.DBVT();
  7915. this.maxStack=256;
  7916. this.stack=[];// vector
  7917. this.stack.length = this.maxStack;
  7918. this.maxLeaves=256;
  7919. this.leaves=[];// vector
  7920. this.leaves.length = this.maxLeaves;
  7921. }
  7922. OIMO.DBVTBroadPhase.prototype = Object.create( OIMO.BroadPhase.prototype );
  7923. OIMO.DBVTBroadPhase.prototype.createProxy = function (shape) {
  7924. return new OIMO.DBVTProxy(shape);
  7925. }
  7926. OIMO.DBVTBroadPhase.prototype.addProxy = function (proxy) {
  7927. var p=(proxy);
  7928. this.tree.insertLeaf(p.leaf);
  7929. if(this.numLeaves==this.maxLeaves){
  7930. //this.maxLeaves<<=1;
  7931. this.maxLeaves*=2;
  7932. var newLeaves=[];// vector
  7933. newLeaves.length = this.maxLeaves;
  7934. for(var i=0;i<this.numLeaves;i++){
  7935. newLeaves[i]=this.leaves[i];
  7936. }
  7937. this.leaves=newLeaves;
  7938. }
  7939. this.leaves[this.numLeaves++]=p.leaf;
  7940. }
  7941. OIMO.DBVTBroadPhase.prototype.removeProxy = function (proxy) {
  7942. var p=(proxy);
  7943. this.tree.deleteLeaf(p.leaf);
  7944. for(var i=0;i<this.numLeaves;i++){
  7945. if(this.leaves[i]==p.leaf){
  7946. this.leaves[i]=this.leaves[--this.numLeaves];
  7947. this.leaves[this.numLeaves]=null;
  7948. return;
  7949. }
  7950. }
  7951. }
  7952. OIMO.DBVTBroadPhase.prototype.collectPairs = function () {
  7953. if(this.numLeaves<2)return;
  7954. for(var i=0;i<this.numLeaves;i++){
  7955. var leaf=this.leaves[i];
  7956. var trueB=leaf.proxy.aabb;
  7957. var leafB=leaf.aabb;
  7958. if(
  7959. trueB.minX<leafB.minX||trueB.maxX>leafB.maxX||
  7960. trueB.minY<leafB.minY||trueB.maxY>leafB.maxY||
  7961. trueB.minZ<leafB.minZ||trueB.maxZ>leafB.maxZ
  7962. ){
  7963. var margin=0.1;
  7964. this.tree.deleteLeaf(leaf);
  7965. leafB.minX=trueB.minX-margin;
  7966. leafB.maxX=trueB.maxX+margin;
  7967. leafB.minY=trueB.minY-margin;
  7968. leafB.maxY=trueB.maxY+margin;
  7969. leafB.minZ=trueB.minZ-margin;
  7970. leafB.maxZ=trueB.maxZ+margin;
  7971. this.tree.insertLeaf(leaf);
  7972. this.collide(leaf,this.tree.root);
  7973. }
  7974. }
  7975. }
  7976. OIMO.DBVTBroadPhase.prototype.collide = function (node1,node2) {
  7977. var stackCount=2;
  7978. this.stack[0]=node1;
  7979. this.stack[1]=node2;
  7980. while(stackCount>0){
  7981. var n1=this.stack[--stackCount];
  7982. var n2=this.stack[--stackCount];
  7983. var l1=n1.proxy!=null;
  7984. var l2=n2.proxy!=null;
  7985. this.numPairChecks++;
  7986. if(l1&&l2){
  7987. var s1=n1.proxy.shape;
  7988. var s2=n2.proxy.shape;
  7989. var b1=s1.aabb;
  7990. var b2=s2.aabb;
  7991. if(
  7992. s1==s2||
  7993. b1.maxX<b2.minX||b1.minX>b2.maxX||
  7994. b1.maxY<b2.minY||b1.minY>b2.maxY||
  7995. b1.maxZ<b2.minZ||b1.minZ>b2.maxZ||
  7996. !this.isAvailablePair(s1,s2)
  7997. ){
  7998. continue;
  7999. }
  8000. this.addPair(s1,s2);
  8001. }else{
  8002. b1=n1.aabb;
  8003. b2=n2.aabb;
  8004. if(
  8005. b1.maxX<b2.minX||b1.minX>b2.maxX||
  8006. b1.maxY<b2.minY||b1.minY>b2.maxY||
  8007. b1.maxZ<b2.minZ||b1.minZ>b2.maxZ
  8008. ){
  8009. continue;
  8010. }
  8011. if(stackCount+4>=this.maxStack){
  8012. //this.maxStack<<=1;
  8013. this.maxStack*=2;
  8014. var newStack=[];// vector
  8015. newStack.length = this.maxStack;
  8016. for(var i=0;i<stackCount;i++){
  8017. newStack[i]=this.stack[i];
  8018. }
  8019. this.stack=newStack;
  8020. }
  8021. if(l2||!l1&&(n1.aabb.surfaceArea()>n2.aabb.surfaceArea())){
  8022. this.stack[stackCount++]=n1.child1;
  8023. this.stack[stackCount++]=n2;
  8024. this.stack[stackCount++]=n1.child2;
  8025. this.stack[stackCount++]=n2;
  8026. }else{
  8027. this.stack[stackCount++]=n1;
  8028. this.stack[stackCount++]=n2.child1;
  8029. this.stack[stackCount++]=n1;
  8030. this.stack[stackCount++]=n2.child2;
  8031. }
  8032. }
  8033. }
  8034. }
  8035. OIMO.DBVTNode = function(){
  8036. this.child1=null;
  8037. this.child2=null;
  8038. this.parent=null;
  8039. this.proxy=null;
  8040. this.height=0;
  8041. this.aabb=new OIMO.AABB();
  8042. }
  8043. OIMO.DBVTProxy = function(shape){
  8044. OIMO.Proxy.call( this, shape);
  8045. this.leaf=new OIMO.DBVTNode();
  8046. this.leaf.proxy=this;
  8047. }
  8048. OIMO.DBVTProxy.prototype = Object.create( OIMO.Proxy.prototype );
  8049. OIMO.DBVTProxy.prototype.update = function () {
  8050. }