cannon.js 384 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687
  1. /*
  2. * Copyright (c) 2015 cannon.js Authors
  3. *
  4. * Permission is hereby granted, free of charge, to any person
  5. * obtaining a copy of this software and associated documentation
  6. * files (the "Software"), to deal in the Software without
  7. * restriction, including without limitation the rights to use, copy,
  8. * modify, merge, publish, distribute, sublicense, and/or sell copies
  9. * of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be
  13. * included in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  19. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  20. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  21. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. !function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&false)define([],e);else{var f;"undefined"!=typeof window?f=window:"undefined"!=typeof global?f=global:"undefined"!=typeof self&&(f=self),f.CANNON=e()}}(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);throw new Error("Cannot find module '"+o+"'")}var f=n[o]={exports:{}};t[o][0].call(f.exports,function(e){var n=t[o][1][e];return s(n?n:e)},f,f.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(_dereq_,module,exports){
  24. module.exports={
  25. "name": "cannon",
  26. "version": "0.6.2",
  27. "description": "A lightweight 3D physics engine written in JavaScript.",
  28. "homepage": "https://github.com/schteppe/cannon.js",
  29. "author": "Stefan Hedman <[email protected]> (http://steffe.se)",
  30. "keywords": [
  31. "cannon.js",
  32. "cannon",
  33. "physics",
  34. "engine",
  35. "3d"
  36. ],
  37. "main": "./build/cannon.js",
  38. "engines": {
  39. "node": "*"
  40. },
  41. "repository": {
  42. "type": "git",
  43. "url": "https://github.com/schteppe/cannon.js.git"
  44. },
  45. "bugs": {
  46. "url": "https://github.com/schteppe/cannon.js/issues"
  47. },
  48. "licenses": [
  49. {
  50. "type": "MIT"
  51. }
  52. ],
  53. "devDependencies": {
  54. "jshint": "latest",
  55. "uglify-js": "latest",
  56. "nodeunit": "^0.9.0",
  57. "grunt": "~0.4.0",
  58. "grunt-contrib-jshint": "~0.1.1",
  59. "grunt-contrib-nodeunit": "^0.4.1",
  60. "grunt-contrib-concat": "~0.1.3",
  61. "grunt-contrib-uglify": "^0.5.1",
  62. "grunt-browserify": "^2.1.4",
  63. "grunt-contrib-yuidoc": "^0.5.2",
  64. "browserify": "*"
  65. },
  66. "dependencies": {}
  67. }
  68. },{}],2:[function(_dereq_,module,exports){
  69. // Export classes
  70. module.exports = {
  71. version : _dereq_('../package.json').version,
  72. AABB : _dereq_('./collision/AABB'),
  73. ArrayCollisionMatrix : _dereq_('./collision/ArrayCollisionMatrix'),
  74. Body : _dereq_('./objects/Body'),
  75. Box : _dereq_('./shapes/Box'),
  76. Broadphase : _dereq_('./collision/Broadphase'),
  77. Constraint : _dereq_('./constraints/Constraint'),
  78. ContactEquation : _dereq_('./equations/ContactEquation'),
  79. Narrowphase : _dereq_('./world/Narrowphase'),
  80. ConeTwistConstraint : _dereq_('./constraints/ConeTwistConstraint'),
  81. ContactMaterial : _dereq_('./material/ContactMaterial'),
  82. ConvexPolyhedron : _dereq_('./shapes/ConvexPolyhedron'),
  83. Cylinder : _dereq_('./shapes/Cylinder'),
  84. DistanceConstraint : _dereq_('./constraints/DistanceConstraint'),
  85. Equation : _dereq_('./equations/Equation'),
  86. EventTarget : _dereq_('./utils/EventTarget'),
  87. FrictionEquation : _dereq_('./equations/FrictionEquation'),
  88. GSSolver : _dereq_('./solver/GSSolver'),
  89. GridBroadphase : _dereq_('./collision/GridBroadphase'),
  90. Heightfield : _dereq_('./shapes/Heightfield'),
  91. HingeConstraint : _dereq_('./constraints/HingeConstraint'),
  92. LockConstraint : _dereq_('./constraints/LockConstraint'),
  93. Mat3 : _dereq_('./math/Mat3'),
  94. Material : _dereq_('./material/Material'),
  95. NaiveBroadphase : _dereq_('./collision/NaiveBroadphase'),
  96. ObjectCollisionMatrix : _dereq_('./collision/ObjectCollisionMatrix'),
  97. Pool : _dereq_('./utils/Pool'),
  98. Particle : _dereq_('./shapes/Particle'),
  99. Plane : _dereq_('./shapes/Plane'),
  100. PointToPointConstraint : _dereq_('./constraints/PointToPointConstraint'),
  101. Quaternion : _dereq_('./math/Quaternion'),
  102. Ray : _dereq_('./collision/Ray'),
  103. RaycastVehicle : _dereq_('./objects/RaycastVehicle'),
  104. RaycastResult : _dereq_('./collision/RaycastResult'),
  105. RigidVehicle : _dereq_('./objects/RigidVehicle'),
  106. RotationalEquation : _dereq_('./equations/RotationalEquation'),
  107. RotationalMotorEquation : _dereq_('./equations/RotationalMotorEquation'),
  108. SAPBroadphase : _dereq_('./collision/SAPBroadphase'),
  109. SPHSystem : _dereq_('./objects/SPHSystem'),
  110. Shape : _dereq_('./shapes/Shape'),
  111. Solver : _dereq_('./solver/Solver'),
  112. Sphere : _dereq_('./shapes/Sphere'),
  113. SplitSolver : _dereq_('./solver/SplitSolver'),
  114. Spring : _dereq_('./objects/Spring'),
  115. Trimesh : _dereq_('./shapes/Trimesh'),
  116. Vec3 : _dereq_('./math/Vec3'),
  117. Vec3Pool : _dereq_('./utils/Vec3Pool'),
  118. World : _dereq_('./world/World'),
  119. };
  120. },{"../package.json":1,"./collision/AABB":3,"./collision/ArrayCollisionMatrix":4,"./collision/Broadphase":5,"./collision/GridBroadphase":6,"./collision/NaiveBroadphase":7,"./collision/ObjectCollisionMatrix":8,"./collision/Ray":9,"./collision/RaycastResult":10,"./collision/SAPBroadphase":11,"./constraints/ConeTwistConstraint":12,"./constraints/Constraint":13,"./constraints/DistanceConstraint":14,"./constraints/HingeConstraint":15,"./constraints/LockConstraint":16,"./constraints/PointToPointConstraint":17,"./equations/ContactEquation":19,"./equations/Equation":20,"./equations/FrictionEquation":21,"./equations/RotationalEquation":22,"./equations/RotationalMotorEquation":23,"./material/ContactMaterial":24,"./material/Material":25,"./math/Mat3":27,"./math/Quaternion":28,"./math/Vec3":30,"./objects/Body":31,"./objects/RaycastVehicle":32,"./objects/RigidVehicle":33,"./objects/SPHSystem":34,"./objects/Spring":35,"./shapes/Box":37,"./shapes/ConvexPolyhedron":38,"./shapes/Cylinder":39,"./shapes/Heightfield":40,"./shapes/Particle":41,"./shapes/Plane":42,"./shapes/Shape":43,"./shapes/Sphere":44,"./shapes/Trimesh":45,"./solver/GSSolver":46,"./solver/Solver":47,"./solver/SplitSolver":48,"./utils/EventTarget":49,"./utils/Pool":51,"./utils/Vec3Pool":54,"./world/Narrowphase":55,"./world/World":56}],3:[function(_dereq_,module,exports){
  121. var Vec3 = _dereq_('../math/Vec3');
  122. var Utils = _dereq_('../utils/Utils');
  123. module.exports = AABB;
  124. /**
  125. * Axis aligned bounding box class.
  126. * @class AABB
  127. * @constructor
  128. * @param {Object} [options]
  129. * @param {Vec3} [options.upperBound]
  130. * @param {Vec3} [options.lowerBound]
  131. */
  132. function AABB(options){
  133. options = options || {};
  134. /**
  135. * The lower bound of the bounding box.
  136. * @property lowerBound
  137. * @type {Vec3}
  138. */
  139. this.lowerBound = new Vec3();
  140. if(options.lowerBound){
  141. this.lowerBound.copy(options.lowerBound);
  142. }
  143. /**
  144. * The upper bound of the bounding box.
  145. * @property upperBound
  146. * @type {Vec3}
  147. */
  148. this.upperBound = new Vec3();
  149. if(options.upperBound){
  150. this.upperBound.copy(options.upperBound);
  151. }
  152. }
  153. var tmp = new Vec3();
  154. /**
  155. * Set the AABB bounds from a set of points.
  156. * @method setFromPoints
  157. * @param {Array} points An array of Vec3's.
  158. * @param {Vec3} position
  159. * @param {Quaternion} quaternion
  160. * @param {number} skinSize
  161. * @return {AABB} The self object
  162. */
  163. AABB.prototype.setFromPoints = function(points, position, quaternion, skinSize){
  164. var l = this.lowerBound,
  165. u = this.upperBound,
  166. q = quaternion;
  167. // Set to the first point
  168. l.copy(points[0]);
  169. if(q){
  170. q.vmult(l, l);
  171. }
  172. u.copy(l);
  173. for(var i = 1; i<points.length; i++){
  174. var p = points[i];
  175. if(q){
  176. q.vmult(p, tmp);
  177. p = tmp;
  178. }
  179. if(p.x > u.x){ u.x = p.x; }
  180. if(p.x < l.x){ l.x = p.x; }
  181. if(p.y > u.y){ u.y = p.y; }
  182. if(p.y < l.y){ l.y = p.y; }
  183. if(p.z > u.z){ u.z = p.z; }
  184. if(p.z < l.z){ l.z = p.z; }
  185. }
  186. // Add offset
  187. if (position) {
  188. position.vadd(l, l);
  189. position.vadd(u, u);
  190. }
  191. if(skinSize){
  192. l.x -= skinSize;
  193. l.y -= skinSize;
  194. l.z -= skinSize;
  195. u.x += skinSize;
  196. u.y += skinSize;
  197. u.z += skinSize;
  198. }
  199. return this;
  200. };
  201. /**
  202. * Copy bounds from an AABB to this AABB
  203. * @method copy
  204. * @param {AABB} aabb Source to copy from
  205. * @return {AABB} The this object, for chainability
  206. */
  207. AABB.prototype.copy = function(aabb){
  208. this.lowerBound.copy(aabb.lowerBound);
  209. this.upperBound.copy(aabb.upperBound);
  210. return this;
  211. };
  212. /**
  213. * Clone an AABB
  214. * @method clone
  215. */
  216. AABB.prototype.clone = function(){
  217. return new AABB().copy(this);
  218. };
  219. /**
  220. * Extend this AABB so that it covers the given AABB too.
  221. * @method extend
  222. * @param {AABB} aabb
  223. */
  224. AABB.prototype.extend = function(aabb){
  225. // Extend lower bound
  226. var l = aabb.lowerBound.x;
  227. if(this.lowerBound.x > l){
  228. this.lowerBound.x = l;
  229. }
  230. // Upper
  231. var u = aabb.upperBound.x;
  232. if(this.upperBound.x < u){
  233. this.upperBound.x = u;
  234. }
  235. // Extend lower bound
  236. var l = aabb.lowerBound.y;
  237. if(this.lowerBound.y > l){
  238. this.lowerBound.y = l;
  239. }
  240. // Upper
  241. var u = aabb.upperBound.y;
  242. if(this.upperBound.y < u){
  243. this.upperBound.y = u;
  244. }
  245. // Extend lower bound
  246. var l = aabb.lowerBound.z;
  247. if(this.lowerBound.z > l){
  248. this.lowerBound.z = l;
  249. }
  250. // Upper
  251. var u = aabb.upperBound.z;
  252. if(this.upperBound.z < u){
  253. this.upperBound.z = u;
  254. }
  255. };
  256. /**
  257. * Returns true if the given AABB overlaps this AABB.
  258. * @method overlaps
  259. * @param {AABB} aabb
  260. * @return {Boolean}
  261. */
  262. AABB.prototype.overlaps = function(aabb){
  263. var l1 = this.lowerBound,
  264. u1 = this.upperBound,
  265. l2 = aabb.lowerBound,
  266. u2 = aabb.upperBound;
  267. // l2 u2
  268. // |---------|
  269. // |--------|
  270. // l1 u1
  271. return ((l2.x <= u1.x && u1.x <= u2.x) || (l1.x <= u2.x && u2.x <= u1.x)) &&
  272. ((l2.y <= u1.y && u1.y <= u2.y) || (l1.y <= u2.y && u2.y <= u1.y)) &&
  273. ((l2.z <= u1.z && u1.z <= u2.z) || (l1.z <= u2.z && u2.z <= u1.z));
  274. };
  275. /**
  276. * Returns true if the given AABB is fully contained in this AABB.
  277. * @method contains
  278. * @param {AABB} aabb
  279. * @return {Boolean}
  280. */
  281. AABB.prototype.contains = function(aabb){
  282. var l1 = this.lowerBound,
  283. u1 = this.upperBound,
  284. l2 = aabb.lowerBound,
  285. u2 = aabb.upperBound;
  286. // l2 u2
  287. // |---------|
  288. // |---------------|
  289. // l1 u1
  290. return (
  291. (l1.x <= l2.x && u1.x >= u2.x) &&
  292. (l1.y <= l2.y && u1.y >= u2.y) &&
  293. (l1.z <= l2.z && u1.z >= u2.z)
  294. );
  295. };
  296. /**
  297. * @method getCorners
  298. * @param {Vec3} a
  299. * @param {Vec3} b
  300. * @param {Vec3} c
  301. * @param {Vec3} d
  302. * @param {Vec3} e
  303. * @param {Vec3} f
  304. * @param {Vec3} g
  305. * @param {Vec3} h
  306. */
  307. AABB.prototype.getCorners = function(a, b, c, d, e, f, g, h){
  308. var l = this.lowerBound,
  309. u = this.upperBound;
  310. a.copy(l);
  311. b.set( u.x, l.y, l.z );
  312. c.set( u.x, u.y, l.z );
  313. d.set( l.x, u.y, u.z );
  314. e.set( u.x, l.y, l.z );
  315. f.set( l.x, u.y, l.z );
  316. g.set( l.x, l.y, u.z );
  317. h.copy(u);
  318. };
  319. var transformIntoFrame_corners = [
  320. new Vec3(),
  321. new Vec3(),
  322. new Vec3(),
  323. new Vec3(),
  324. new Vec3(),
  325. new Vec3(),
  326. new Vec3(),
  327. new Vec3()
  328. ];
  329. /**
  330. * Get the representation of an AABB in another frame.
  331. * @method toLocalFrame
  332. * @param {Transform} frame
  333. * @param {AABB} target
  334. * @return {AABB} The "target" AABB object.
  335. */
  336. AABB.prototype.toLocalFrame = function(frame, target){
  337. var corners = transformIntoFrame_corners;
  338. var a = corners[0];
  339. var b = corners[1];
  340. var c = corners[2];
  341. var d = corners[3];
  342. var e = corners[4];
  343. var f = corners[5];
  344. var g = corners[6];
  345. var h = corners[7];
  346. // Get corners in current frame
  347. this.getCorners(a, b, c, d, e, f, g, h);
  348. // Transform them to new local frame
  349. for(var i=0; i !== 8; i++){
  350. var corner = corners[i];
  351. frame.pointToLocal(corner, corner);
  352. }
  353. return target.setFromPoints(corners);
  354. };
  355. /**
  356. * Get the representation of an AABB in the global frame.
  357. * @method toWorldFrame
  358. * @param {Transform} frame
  359. * @param {AABB} target
  360. * @return {AABB} The "target" AABB object.
  361. */
  362. AABB.prototype.toWorldFrame = function(frame, target){
  363. var corners = transformIntoFrame_corners;
  364. var a = corners[0];
  365. var b = corners[1];
  366. var c = corners[2];
  367. var d = corners[3];
  368. var e = corners[4];
  369. var f = corners[5];
  370. var g = corners[6];
  371. var h = corners[7];
  372. // Get corners in current frame
  373. this.getCorners(a, b, c, d, e, f, g, h);
  374. // Transform them to new local frame
  375. for(var i=0; i !== 8; i++){
  376. var corner = corners[i];
  377. frame.pointToWorld(corner, corner);
  378. }
  379. return target.setFromPoints(corners);
  380. };
  381. },{"../math/Vec3":30,"../utils/Utils":53}],4:[function(_dereq_,module,exports){
  382. module.exports = ArrayCollisionMatrix;
  383. /**
  384. * Collision "matrix". It's actually a triangular-shaped array of whether two bodies are touching this step, for reference next step
  385. * @class ArrayCollisionMatrix
  386. * @constructor
  387. */
  388. function ArrayCollisionMatrix() {
  389. /**
  390. * The matrix storage
  391. * @property matrix
  392. * @type {Array}
  393. */
  394. this.matrix = [];
  395. }
  396. /**
  397. * Get an element
  398. * @method get
  399. * @param {Number} i
  400. * @param {Number} j
  401. * @return {Number}
  402. */
  403. ArrayCollisionMatrix.prototype.get = function(i, j) {
  404. i = i.index;
  405. j = j.index;
  406. if (j > i) {
  407. var temp = j;
  408. j = i;
  409. i = temp;
  410. }
  411. return this.matrix[(i*(i + 1)>>1) + j-1];
  412. };
  413. /**
  414. * Set an element
  415. * @method set
  416. * @param {Number} i
  417. * @param {Number} j
  418. * @param {Number} value
  419. */
  420. ArrayCollisionMatrix.prototype.set = function(i, j, value) {
  421. i = i.index;
  422. j = j.index;
  423. if (j > i) {
  424. var temp = j;
  425. j = i;
  426. i = temp;
  427. }
  428. this.matrix[(i*(i + 1)>>1) + j-1] = value ? 1 : 0;
  429. };
  430. /**
  431. * Sets all elements to zero
  432. * @method reset
  433. */
  434. ArrayCollisionMatrix.prototype.reset = function() {
  435. for (var i=0, l=this.matrix.length; i!==l; i++) {
  436. this.matrix[i]=0;
  437. }
  438. };
  439. /**
  440. * Sets the max number of objects
  441. * @method setNumObjects
  442. * @param {Number} n
  443. */
  444. ArrayCollisionMatrix.prototype.setNumObjects = function(n) {
  445. this.matrix.length = n*(n-1)>>1;
  446. };
  447. },{}],5:[function(_dereq_,module,exports){
  448. var Body = _dereq_('../objects/Body');
  449. var Vec3 = _dereq_('../math/Vec3');
  450. var Quaternion = _dereq_('../math/Quaternion');
  451. var Shape = _dereq_('../shapes/Shape');
  452. var Plane = _dereq_('../shapes/Plane');
  453. module.exports = Broadphase;
  454. /**
  455. * Base class for broadphase implementations
  456. * @class Broadphase
  457. * @constructor
  458. * @author schteppe
  459. */
  460. function Broadphase(){
  461. /**
  462. * The world to search for collisions in.
  463. * @property world
  464. * @type {World}
  465. */
  466. this.world = null;
  467. /**
  468. * If set to true, the broadphase uses bounding boxes for intersection test, else it uses bounding spheres.
  469. * @property useBoundingBoxes
  470. * @type {Boolean}
  471. */
  472. this.useBoundingBoxes = false;
  473. /**
  474. * Set to true if the objects in the world moved.
  475. * @property {Boolean} dirty
  476. */
  477. this.dirty = true;
  478. }
  479. /**
  480. * Get the collision pairs from the world
  481. * @method collisionPairs
  482. * @param {World} world The world to search in
  483. * @param {Array} p1 Empty array to be filled with body objects
  484. * @param {Array} p2 Empty array to be filled with body objects
  485. */
  486. Broadphase.prototype.collisionPairs = function(world,p1,p2){
  487. throw new Error("collisionPairs not implemented for this BroadPhase class!");
  488. };
  489. /**
  490. * Check if a body pair needs to be intersection tested at all.
  491. * @method needBroadphaseCollision
  492. * @param {Body} bodyA
  493. * @param {Body} bodyB
  494. * @return {bool}
  495. */
  496. var Broadphase_needBroadphaseCollision_STATIC_OR_KINEMATIC = Body.STATIC | Body.KINEMATIC;
  497. Broadphase.prototype.needBroadphaseCollision = function(bodyA,bodyB){
  498. // Check collision filter masks
  499. if( (bodyA.collisionFilterGroup & bodyB.collisionFilterMask)===0 || (bodyB.collisionFilterGroup & bodyA.collisionFilterMask)===0){
  500. return false;
  501. }
  502. // Check types
  503. if(((bodyA.type & Broadphase_needBroadphaseCollision_STATIC_OR_KINEMATIC)!==0 || bodyA.sleepState === Body.SLEEPING) &&
  504. ((bodyB.type & Broadphase_needBroadphaseCollision_STATIC_OR_KINEMATIC)!==0 || bodyB.sleepState === Body.SLEEPING)) {
  505. // Both bodies are static, kinematic or sleeping. Skip.
  506. return false;
  507. }
  508. return true;
  509. };
  510. /**
  511. * Check if the bounding volumes of two bodies intersect.
  512. * @method intersectionTest
  513. * @param {Body} bodyA
  514. * @param {Body} bodyB
  515. * @param {array} pairs1
  516. * @param {array} pairs2
  517. */
  518. Broadphase.prototype.intersectionTest = function(bodyA, bodyB, pairs1, pairs2){
  519. if(this.useBoundingBoxes){
  520. this.doBoundingBoxBroadphase(bodyA,bodyB,pairs1,pairs2);
  521. } else {
  522. this.doBoundingSphereBroadphase(bodyA,bodyB,pairs1,pairs2);
  523. }
  524. };
  525. /**
  526. * Check if the bounding spheres of two bodies are intersecting.
  527. * @method doBoundingSphereBroadphase
  528. * @param {Body} bodyA
  529. * @param {Body} bodyB
  530. * @param {Array} pairs1 bodyA is appended to this array if intersection
  531. * @param {Array} pairs2 bodyB is appended to this array if intersection
  532. */
  533. var Broadphase_collisionPairs_r = new Vec3(), // Temp objects
  534. Broadphase_collisionPairs_normal = new Vec3(),
  535. Broadphase_collisionPairs_quat = new Quaternion(),
  536. Broadphase_collisionPairs_relpos = new Vec3();
  537. Broadphase.prototype.doBoundingSphereBroadphase = function(bodyA,bodyB,pairs1,pairs2){
  538. var r = Broadphase_collisionPairs_r;
  539. bodyB.position.vsub(bodyA.position,r);
  540. var boundingRadiusSum2 = Math.pow(bodyA.boundingRadius + bodyB.boundingRadius, 2);
  541. var norm2 = r.norm2();
  542. if(norm2 < boundingRadiusSum2){
  543. pairs1.push(bodyA);
  544. pairs2.push(bodyB);
  545. }
  546. };
  547. /**
  548. * Check if the bounding boxes of two bodies are intersecting.
  549. * @method doBoundingBoxBroadphase
  550. * @param {Body} bodyA
  551. * @param {Body} bodyB
  552. * @param {Array} pairs1
  553. * @param {Array} pairs2
  554. */
  555. Broadphase.prototype.doBoundingBoxBroadphase = function(bodyA,bodyB,pairs1,pairs2){
  556. if(bodyA.aabbNeedsUpdate){
  557. bodyA.computeAABB();
  558. }
  559. if(bodyB.aabbNeedsUpdate){
  560. bodyB.computeAABB();
  561. }
  562. // Check AABB / AABB
  563. if(bodyA.aabb.overlaps(bodyB.aabb)){
  564. pairs1.push(bodyA);
  565. pairs2.push(bodyB);
  566. }
  567. };
  568. /**
  569. * Removes duplicate pairs from the pair arrays.
  570. * @method makePairsUnique
  571. * @param {Array} pairs1
  572. * @param {Array} pairs2
  573. */
  574. var Broadphase_makePairsUnique_temp = { keys:[] },
  575. Broadphase_makePairsUnique_p1 = [],
  576. Broadphase_makePairsUnique_p2 = [];
  577. Broadphase.prototype.makePairsUnique = function(pairs1,pairs2){
  578. var t = Broadphase_makePairsUnique_temp,
  579. p1 = Broadphase_makePairsUnique_p1,
  580. p2 = Broadphase_makePairsUnique_p2,
  581. N = pairs1.length;
  582. for(var i=0; i!==N; i++){
  583. p1[i] = pairs1[i];
  584. p2[i] = pairs2[i];
  585. }
  586. pairs1.length = 0;
  587. pairs2.length = 0;
  588. for(var i=0; i!==N; i++){
  589. var id1 = p1[i].id,
  590. id2 = p2[i].id;
  591. var key = id1 < id2 ? id1+","+id2 : id2+","+id1;
  592. t[key] = i;
  593. t.keys.push(key);
  594. }
  595. for(var i=0; i!==t.keys.length; i++){
  596. var key = t.keys.pop(),
  597. pairIndex = t[key];
  598. pairs1.push(p1[pairIndex]);
  599. pairs2.push(p2[pairIndex]);
  600. delete t[key];
  601. }
  602. };
  603. /**
  604. * To be implemented by subcasses
  605. * @method setWorld
  606. * @param {World} world
  607. */
  608. Broadphase.prototype.setWorld = function(world){
  609. };
  610. /**
  611. * Check if the bounding spheres of two bodies overlap.
  612. * @method boundingSphereCheck
  613. * @param {Body} bodyA
  614. * @param {Body} bodyB
  615. * @return {boolean}
  616. */
  617. var bsc_dist = new Vec3();
  618. Broadphase.boundingSphereCheck = function(bodyA,bodyB){
  619. var dist = bsc_dist;
  620. bodyA.position.vsub(bodyB.position,dist);
  621. return Math.pow(bodyA.shape.boundingSphereRadius + bodyB.shape.boundingSphereRadius,2) > dist.norm2();
  622. };
  623. /**
  624. * Returns all the bodies within the AABB.
  625. * @method aabbQuery
  626. * @param {World} world
  627. * @param {AABB} aabb
  628. * @param {array} result An array to store resulting bodies in.
  629. * @return {array}
  630. */
  631. Broadphase.prototype.aabbQuery = function(world, aabb, result){
  632. console.warn('.aabbQuery is not implemented in this Broadphase subclass.');
  633. return [];
  634. };
  635. },{"../math/Quaternion":28,"../math/Vec3":30,"../objects/Body":31,"../shapes/Plane":42,"../shapes/Shape":43}],6:[function(_dereq_,module,exports){
  636. module.exports = GridBroadphase;
  637. var Broadphase = _dereq_('./Broadphase');
  638. var Vec3 = _dereq_('../math/Vec3');
  639. var Shape = _dereq_('../shapes/Shape');
  640. /**
  641. * Axis aligned uniform grid broadphase.
  642. * @class GridBroadphase
  643. * @constructor
  644. * @extends Broadphase
  645. * @todo Needs support for more than just planes and spheres.
  646. * @param {Vec3} aabbMin
  647. * @param {Vec3} aabbMax
  648. * @param {Number} nx Number of boxes along x
  649. * @param {Number} ny Number of boxes along y
  650. * @param {Number} nz Number of boxes along z
  651. */
  652. function GridBroadphase(aabbMin,aabbMax,nx,ny,nz){
  653. Broadphase.apply(this);
  654. this.nx = nx || 10;
  655. this.ny = ny || 10;
  656. this.nz = nz || 10;
  657. this.aabbMin = aabbMin || new Vec3(100,100,100);
  658. this.aabbMax = aabbMax || new Vec3(-100,-100,-100);
  659. var nbins = this.nx * this.ny * this.nz;
  660. if (nbins <= 0) {
  661. throw "GridBroadphase: Each dimension's n must be >0";
  662. }
  663. this.bins = [];
  664. this.binLengths = []; //Rather than continually resizing arrays (thrashing the memory), just record length and allow them to grow
  665. this.bins.length = nbins;
  666. this.binLengths.length = nbins;
  667. for (var i=0;i<nbins;i++) {
  668. this.bins[i]=[];
  669. this.binLengths[i]=0;
  670. }
  671. }
  672. GridBroadphase.prototype = new Broadphase();
  673. GridBroadphase.prototype.constructor = GridBroadphase;
  674. /**
  675. * Get all the collision pairs in the physics world
  676. * @method collisionPairs
  677. * @param {World} world
  678. * @param {Array} pairs1
  679. * @param {Array} pairs2
  680. */
  681. var GridBroadphase_collisionPairs_d = new Vec3();
  682. var GridBroadphase_collisionPairs_binPos = new Vec3();
  683. GridBroadphase.prototype.collisionPairs = function(world,pairs1,pairs2){
  684. var N = world.numObjects(),
  685. bodies = world.bodies;
  686. var max = this.aabbMax,
  687. min = this.aabbMin,
  688. nx = this.nx,
  689. ny = this.ny,
  690. nz = this.nz;
  691. var xstep = ny*nz;
  692. var ystep = nz;
  693. var zstep = 1;
  694. var xmax = max.x,
  695. ymax = max.y,
  696. zmax = max.z,
  697. xmin = min.x,
  698. ymin = min.y,
  699. zmin = min.z;
  700. var xmult = nx / (xmax-xmin),
  701. ymult = ny / (ymax-ymin),
  702. zmult = nz / (zmax-zmin);
  703. var binsizeX = (xmax - xmin) / nx,
  704. binsizeY = (ymax - ymin) / ny,
  705. binsizeZ = (zmax - zmin) / nz;
  706. var binRadius = Math.sqrt(binsizeX*binsizeX + binsizeY*binsizeY + binsizeZ*binsizeZ) * 0.5;
  707. var types = Shape.types;
  708. var SPHERE = types.SPHERE,
  709. PLANE = types.PLANE,
  710. BOX = types.BOX,
  711. COMPOUND = types.COMPOUND,
  712. CONVEXPOLYHEDRON = types.CONVEXPOLYHEDRON;
  713. var bins=this.bins,
  714. binLengths=this.binLengths,
  715. Nbins=this.bins.length;
  716. // Reset bins
  717. for(var i=0; i!==Nbins; i++){
  718. binLengths[i] = 0;
  719. }
  720. var ceil = Math.ceil;
  721. var min = Math.min;
  722. var max = Math.max;
  723. function addBoxToBins(x0,y0,z0,x1,y1,z1,bi) {
  724. var xoff0 = ((x0 - xmin) * xmult)|0,
  725. yoff0 = ((y0 - ymin) * ymult)|0,
  726. zoff0 = ((z0 - zmin) * zmult)|0,
  727. xoff1 = ceil((x1 - xmin) * xmult),
  728. yoff1 = ceil((y1 - ymin) * ymult),
  729. zoff1 = ceil((z1 - zmin) * zmult);
  730. if (xoff0 < 0) { xoff0 = 0; } else if (xoff0 >= nx) { xoff0 = nx - 1; }
  731. if (yoff0 < 0) { yoff0 = 0; } else if (yoff0 >= ny) { yoff0 = ny - 1; }
  732. if (zoff0 < 0) { zoff0 = 0; } else if (zoff0 >= nz) { zoff0 = nz - 1; }
  733. if (xoff1 < 0) { xoff1 = 0; } else if (xoff1 >= nx) { xoff1 = nx - 1; }
  734. if (yoff1 < 0) { yoff1 = 0; } else if (yoff1 >= ny) { yoff1 = ny - 1; }
  735. if (zoff1 < 0) { zoff1 = 0; } else if (zoff1 >= nz) { zoff1 = nz - 1; }
  736. xoff0 *= xstep;
  737. yoff0 *= ystep;
  738. zoff0 *= zstep;
  739. xoff1 *= xstep;
  740. yoff1 *= ystep;
  741. zoff1 *= zstep;
  742. for (var xoff = xoff0; xoff <= xoff1; xoff += xstep) {
  743. for (var yoff = yoff0; yoff <= yoff1; yoff += ystep) {
  744. for (var zoff = zoff0; zoff <= zoff1; zoff += zstep) {
  745. var idx = xoff+yoff+zoff;
  746. bins[idx][binLengths[idx]++] = bi;
  747. }
  748. }
  749. }
  750. }
  751. // Put all bodies into the bins
  752. for(var i=0; i!==N; i++){
  753. var bi = bodies[i];
  754. var si = bi.shape;
  755. switch(si.type){
  756. case SPHERE:
  757. // Put in bin
  758. // check if overlap with other bins
  759. var x = bi.position.x,
  760. y = bi.position.y,
  761. z = bi.position.z;
  762. var r = si.radius;
  763. addBoxToBins(x-r, y-r, z-r, x+r, y+r, z+r, bi);
  764. break;
  765. case PLANE:
  766. if(si.worldNormalNeedsUpdate){
  767. si.computeWorldNormal(bi.quaternion);
  768. }
  769. var planeNormal = si.worldNormal;
  770. //Relative position from origin of plane object to the first bin
  771. //Incremented as we iterate through the bins
  772. var xreset = xmin + binsizeX*0.5 - bi.position.x,
  773. yreset = ymin + binsizeY*0.5 - bi.position.y,
  774. zreset = zmin + binsizeZ*0.5 - bi.position.z;
  775. var d = GridBroadphase_collisionPairs_d;
  776. d.set(xreset, yreset, zreset);
  777. for (var xi = 0, xoff = 0; xi !== nx; xi++, xoff += xstep, d.y = yreset, d.x += binsizeX) {
  778. for (var yi = 0, yoff = 0; yi !== ny; yi++, yoff += ystep, d.z = zreset, d.y += binsizeY) {
  779. for (var zi = 0, zoff = 0; zi !== nz; zi++, zoff += zstep, d.z += binsizeZ) {
  780. if (d.dot(planeNormal) < binRadius) {
  781. var idx = xoff + yoff + zoff;
  782. bins[idx][binLengths[idx]++] = bi;
  783. }
  784. }
  785. }
  786. }
  787. break;
  788. default:
  789. if (bi.aabbNeedsUpdate) {
  790. bi.computeAABB();
  791. }
  792. addBoxToBins(
  793. bi.aabb.lowerBound.x,
  794. bi.aabb.lowerBound.y,
  795. bi.aabb.lowerBound.z,
  796. bi.aabb.upperBound.x,
  797. bi.aabb.upperBound.y,
  798. bi.aabb.upperBound.z,
  799. bi);
  800. break;
  801. }
  802. }
  803. // Check each bin
  804. for(var i=0; i!==Nbins; i++){
  805. var binLength = binLengths[i];
  806. //Skip bins with no potential collisions
  807. if (binLength > 1) {
  808. var bin = bins[i];
  809. // Do N^2 broadphase inside
  810. for(var xi=0; xi!==binLength; xi++){
  811. var bi = bin[xi];
  812. for(var yi=0; yi!==xi; yi++){
  813. var bj = bin[yi];
  814. if(this.needBroadphaseCollision(bi,bj)){
  815. this.intersectionTest(bi,bj,pairs1,pairs2);
  816. }
  817. }
  818. }
  819. }
  820. }
  821. // for (var zi = 0, zoff=0; zi < nz; zi++, zoff+= zstep) {
  822. // console.log("layer "+zi);
  823. // for (var yi = 0, yoff=0; yi < ny; yi++, yoff += ystep) {
  824. // var row = '';
  825. // for (var xi = 0, xoff=0; xi < nx; xi++, xoff += xstep) {
  826. // var idx = xoff + yoff + zoff;
  827. // row += ' ' + binLengths[idx];
  828. // }
  829. // console.log(row);
  830. // }
  831. // }
  832. this.makePairsUnique(pairs1,pairs2);
  833. };
  834. },{"../math/Vec3":30,"../shapes/Shape":43,"./Broadphase":5}],7:[function(_dereq_,module,exports){
  835. module.exports = NaiveBroadphase;
  836. var Broadphase = _dereq_('./Broadphase');
  837. var AABB = _dereq_('./AABB');
  838. /**
  839. * Naive broadphase implementation, used in lack of better ones.
  840. * @class NaiveBroadphase
  841. * @constructor
  842. * @description The naive broadphase looks at all possible pairs without restriction, therefore it has complexity N^2 (which is bad)
  843. * @extends Broadphase
  844. */
  845. function NaiveBroadphase(){
  846. Broadphase.apply(this);
  847. }
  848. NaiveBroadphase.prototype = new Broadphase();
  849. NaiveBroadphase.prototype.constructor = NaiveBroadphase;
  850. /**
  851. * Get all the collision pairs in the physics world
  852. * @method collisionPairs
  853. * @param {World} world
  854. * @param {Array} pairs1
  855. * @param {Array} pairs2
  856. */
  857. NaiveBroadphase.prototype.collisionPairs = function(world,pairs1,pairs2){
  858. var bodies = world.bodies,
  859. n = bodies.length,
  860. i,j,bi,bj;
  861. // Naive N^2 ftw!
  862. for(i=0; i!==n; i++){
  863. for(j=0; j!==i; j++){
  864. bi = bodies[i];
  865. bj = bodies[j];
  866. if(!this.needBroadphaseCollision(bi,bj)){
  867. continue;
  868. }
  869. this.intersectionTest(bi,bj,pairs1,pairs2);
  870. }
  871. }
  872. };
  873. var tmpAABB = new AABB();
  874. /**
  875. * Returns all the bodies within an AABB.
  876. * @method aabbQuery
  877. * @param {World} world
  878. * @param {AABB} aabb
  879. * @param {array} result An array to store resulting bodies in.
  880. * @return {array}
  881. */
  882. NaiveBroadphase.prototype.aabbQuery = function(world, aabb, result){
  883. result = result || [];
  884. for(var i = 0; i < world.bodies.length; i++){
  885. var b = world.bodies[i];
  886. if(b.aabbNeedsUpdate){
  887. b.computeAABB();
  888. }
  889. // Ugly hack until Body gets aabb
  890. if(b.aabb.overlaps(aabb)){
  891. result.push(b);
  892. }
  893. }
  894. return result;
  895. };
  896. },{"./AABB":3,"./Broadphase":5}],8:[function(_dereq_,module,exports){
  897. module.exports = ObjectCollisionMatrix;
  898. /**
  899. * Records what objects are colliding with each other
  900. * @class ObjectCollisionMatrix
  901. * @constructor
  902. */
  903. function ObjectCollisionMatrix() {
  904. /**
  905. * The matrix storage
  906. * @property matrix
  907. * @type {Object}
  908. */
  909. this.matrix = {};
  910. }
  911. /**
  912. * @method get
  913. * @param {Number} i
  914. * @param {Number} j
  915. * @return {Number}
  916. */
  917. ObjectCollisionMatrix.prototype.get = function(i, j) {
  918. i = i.id;
  919. j = j.id;
  920. if (j > i) {
  921. var temp = j;
  922. j = i;
  923. i = temp;
  924. }
  925. return i+'-'+j in this.matrix;
  926. };
  927. /**
  928. * @method set
  929. * @param {Number} i
  930. * @param {Number} j
  931. * @param {Number} value
  932. */
  933. ObjectCollisionMatrix.prototype.set = function(i, j, value) {
  934. i = i.id;
  935. j = j.id;
  936. if (j > i) {
  937. var temp = j;
  938. j = i;
  939. i = temp;
  940. }
  941. if (value) {
  942. this.matrix[i+'-'+j] = true;
  943. }
  944. else {
  945. delete this.matrix[i+'-'+j];
  946. }
  947. };
  948. /**
  949. * Empty the matrix
  950. * @method reset
  951. */
  952. ObjectCollisionMatrix.prototype.reset = function() {
  953. this.matrix = {};
  954. };
  955. /**
  956. * Set max number of objects
  957. * @method setNumObjects
  958. * @param {Number} n
  959. */
  960. ObjectCollisionMatrix.prototype.setNumObjects = function(n) {
  961. };
  962. },{}],9:[function(_dereq_,module,exports){
  963. module.exports = Ray;
  964. var Vec3 = _dereq_('../math/Vec3');
  965. var Quaternion = _dereq_('../math/Quaternion');
  966. var Transform = _dereq_('../math/Transform');
  967. var ConvexPolyhedron = _dereq_('../shapes/ConvexPolyhedron');
  968. var Box = _dereq_('../shapes/Box');
  969. var RaycastResult = _dereq_('../collision/RaycastResult');
  970. var Shape = _dereq_('../shapes/Shape');
  971. var AABB = _dereq_('../collision/AABB');
  972. /**
  973. * A line in 3D space that intersects bodies and return points.
  974. * @class Ray
  975. * @constructor
  976. * @param {Vec3} from
  977. * @param {Vec3} to
  978. */
  979. function Ray(from, to){
  980. /**
  981. * @property {Vec3} from
  982. */
  983. this.from = from ? from.clone() : new Vec3();
  984. /**
  985. * @property {Vec3} to
  986. */
  987. this.to = to ? to.clone() : new Vec3();
  988. /**
  989. * @private
  990. * @property {Vec3} _direction
  991. */
  992. this._direction = new Vec3();
  993. /**
  994. * The precision of the ray. Used when checking parallelity etc.
  995. * @property {Number} precision
  996. */
  997. this.precision = 0.0001;
  998. /**
  999. * Set to true if you want the Ray to take .collisionResponse flags into account on bodies and shapes.
  1000. * @property {Boolean} checkCollisionResponse
  1001. */
  1002. this.checkCollisionResponse = true;
  1003. /**
  1004. * If set to true, the ray skips any hits with normal.dot(rayDirection) < 0.
  1005. * @property {Boolean} skipBackfaces
  1006. */
  1007. this.skipBackfaces = false;
  1008. /**
  1009. * @property {number} collisionFilterMask
  1010. * @default -1
  1011. */
  1012. this.collisionFilterMask = -1;
  1013. /**
  1014. * @property {number} collisionFilterGroup
  1015. * @default -1
  1016. */
  1017. this.collisionFilterGroup = -1;
  1018. /**
  1019. * The intersection mode. Should be Ray.ANY, Ray.ALL or Ray.CLOSEST.
  1020. * @property {number} mode
  1021. */
  1022. this.mode = Ray.ANY;
  1023. /**
  1024. * Current result object.
  1025. * @property {RaycastResult} result
  1026. */
  1027. this.result = new RaycastResult();
  1028. /**
  1029. * Will be set to true during intersectWorld() if the ray hit anything.
  1030. * @property {Boolean} hasHit
  1031. */
  1032. this.hasHit = false;
  1033. /**
  1034. * Current, user-provided result callback. Will be used if mode is Ray.ALL.
  1035. * @property {Function} callback
  1036. */
  1037. this.callback = function(result){};
  1038. }
  1039. Ray.prototype.constructor = Ray;
  1040. Ray.CLOSEST = 1;
  1041. Ray.ANY = 2;
  1042. Ray.ALL = 4;
  1043. var tmpAABB = new AABB();
  1044. var tmpArray = [];
  1045. /**
  1046. * Do itersection against all bodies in the given World.
  1047. * @method intersectWorld
  1048. * @param {World} world
  1049. * @param {object} options
  1050. * @return {Boolean} True if the ray hit anything, otherwise false.
  1051. */
  1052. Ray.prototype.intersectWorld = function (world, options) {
  1053. this.mode = options.mode || Ray.ANY;
  1054. this.result = options.result || new RaycastResult();
  1055. this.skipBackfaces = !!options.skipBackfaces;
  1056. this.collisionFilterMask = typeof(options.collisionFilterMask) !== 'undefined' ? options.collisionFilterMask : -1;
  1057. this.collisionFilterGroup = typeof(options.collisionFilterGroup) !== 'undefined' ? options.collisionFilterGroup : -1;
  1058. if(options.from){
  1059. this.from.copy(options.from);
  1060. }
  1061. if(options.to){
  1062. this.to.copy(options.to);
  1063. }
  1064. this.callback = options.callback || function(){};
  1065. this.hasHit = false;
  1066. this.result.reset();
  1067. this._updateDirection();
  1068. this.getAABB(tmpAABB);
  1069. tmpArray.length = 0;
  1070. world.broadphase.aabbQuery(world, tmpAABB, tmpArray);
  1071. this.intersectBodies(tmpArray);
  1072. return this.hasHit;
  1073. };
  1074. var v1 = new Vec3(),
  1075. v2 = new Vec3();
  1076. /*
  1077. * As per "Barycentric Technique" as named here http://www.blackpawn.com/texts/pointinpoly/default.html But without the division
  1078. */
  1079. Ray.pointInTriangle = pointInTriangle;
  1080. function pointInTriangle(p, a, b, c) {
  1081. c.vsub(a,v0);
  1082. b.vsub(a,v1);
  1083. p.vsub(a,v2);
  1084. var dot00 = v0.dot( v0 );
  1085. var dot01 = v0.dot( v1 );
  1086. var dot02 = v0.dot( v2 );
  1087. var dot11 = v1.dot( v1 );
  1088. var dot12 = v1.dot( v2 );
  1089. var u,v;
  1090. return ( (u = dot11 * dot02 - dot01 * dot12) >= 0 ) &&
  1091. ( (v = dot00 * dot12 - dot01 * dot02) >= 0 ) &&
  1092. ( u + v < ( dot00 * dot11 - dot01 * dot01 ) );
  1093. }
  1094. /**
  1095. * Shoot a ray at a body, get back information about the hit.
  1096. * @method intersectBody
  1097. * @private
  1098. * @param {Body} body
  1099. * @param {RaycastResult} [result] Deprecated - set the result property of the Ray instead.
  1100. */
  1101. var intersectBody_xi = new Vec3();
  1102. var intersectBody_qi = new Quaternion();
  1103. Ray.prototype.intersectBody = function (body, result) {
  1104. if(result){
  1105. this.result = result;
  1106. this._updateDirection();
  1107. }
  1108. var checkCollisionResponse = this.checkCollisionResponse;
  1109. if(checkCollisionResponse && !body.collisionResponse){
  1110. return;
  1111. }
  1112. if((this.collisionFilterGroup & body.collisionFilterMask)===0 || (body.collisionFilterGroup & this.collisionFilterMask)===0){
  1113. return;
  1114. }
  1115. var xi = intersectBody_xi;
  1116. var qi = intersectBody_qi;
  1117. for (var i = 0, N = body.shapes.length; i < N; i++) {
  1118. var shape = body.shapes[i];
  1119. if(checkCollisionResponse && !shape.collisionResponse){
  1120. continue; // Skip
  1121. }
  1122. body.quaternion.mult(body.shapeOrientations[i], qi);
  1123. body.quaternion.vmult(body.shapeOffsets[i], xi);
  1124. xi.vadd(body.position, xi);
  1125. this.intersectShape(
  1126. shape,
  1127. qi,
  1128. xi,
  1129. body
  1130. );
  1131. if(this.result._shouldStop){
  1132. break;
  1133. }
  1134. }
  1135. };
  1136. /**
  1137. * @method intersectBodies
  1138. * @param {Array} bodies An array of Body objects.
  1139. * @param {RaycastResult} [result] Deprecated
  1140. */
  1141. Ray.prototype.intersectBodies = function (bodies, result) {
  1142. if(result){
  1143. this.result = result;
  1144. this._updateDirection();
  1145. }
  1146. for ( var i = 0, l = bodies.length; !this.result._shouldStop && i < l; i ++ ) {
  1147. this.intersectBody(bodies[i]);
  1148. }
  1149. };
  1150. /**
  1151. * Updates the _direction vector.
  1152. * @private
  1153. * @method _updateDirection
  1154. */
  1155. Ray.prototype._updateDirection = function(){
  1156. this.to.vsub(this.from, this._direction);
  1157. this._direction.normalize();
  1158. };
  1159. /**
  1160. * @method intersectShape
  1161. * @private
  1162. * @param {Shape} shape
  1163. * @param {Quaternion} quat
  1164. * @param {Vec3} position
  1165. * @param {Body} body
  1166. */
  1167. Ray.prototype.intersectShape = function(shape, quat, position, body){
  1168. var from = this.from;
  1169. // Checking boundingSphere
  1170. var distance = distanceFromIntersection(from, this._direction, position);
  1171. if ( distance > shape.boundingSphereRadius ) {
  1172. return;
  1173. }
  1174. var intersectMethod = this[shape.type];
  1175. if(intersectMethod){
  1176. intersectMethod.call(this, shape, quat, position, body);
  1177. }
  1178. };
  1179. var vector = new Vec3();
  1180. var normal = new Vec3();
  1181. var intersectPoint = new Vec3();
  1182. var a = new Vec3();
  1183. var b = new Vec3();
  1184. var c = new Vec3();
  1185. var d = new Vec3();
  1186. var tmpRaycastResult = new RaycastResult();
  1187. /**
  1188. * @method intersectBox
  1189. * @private
  1190. * @param {Shape} shape
  1191. * @param {Quaternion} quat
  1192. * @param {Vec3} position
  1193. * @param {Body} body
  1194. */
  1195. Ray.prototype.intersectBox = function(shape, quat, position, body){
  1196. return this.intersectConvex(shape.convexPolyhedronRepresentation, quat, position, body);
  1197. };
  1198. Ray.prototype[Shape.types.BOX] = Ray.prototype.intersectBox;
  1199. /**
  1200. * @method intersectPlane
  1201. * @private
  1202. * @param {Shape} shape
  1203. * @param {Quaternion} quat
  1204. * @param {Vec3} position
  1205. * @param {Body} body
  1206. */
  1207. Ray.prototype.intersectPlane = function(shape, quat, position, body){
  1208. var from = this.from;
  1209. var to = this.to;
  1210. var direction = this._direction;
  1211. // Get plane normal
  1212. var worldNormal = new Vec3(0, 0, 1);
  1213. quat.vmult(worldNormal, worldNormal);
  1214. var len = new Vec3();
  1215. from.vsub(position, len);
  1216. var planeToFrom = len.dot(worldNormal);
  1217. to.vsub(position, len);
  1218. var planeToTo = len.dot(worldNormal);
  1219. if(planeToFrom * planeToTo > 0){
  1220. // "from" and "to" are on the same side of the plane... bail out
  1221. return;
  1222. }
  1223. if(from.distanceTo(to) < planeToFrom){
  1224. return;
  1225. }
  1226. var n_dot_dir = worldNormal.dot(direction);
  1227. if (Math.abs(n_dot_dir) < this.precision) {
  1228. // No intersection
  1229. return;
  1230. }
  1231. var planePointToFrom = new Vec3();
  1232. var dir_scaled_with_t = new Vec3();
  1233. var hitPointWorld = new Vec3();
  1234. from.vsub(position, planePointToFrom);
  1235. var t = -worldNormal.dot(planePointToFrom) / n_dot_dir;
  1236. direction.scale(t, dir_scaled_with_t);
  1237. from.vadd(dir_scaled_with_t, hitPointWorld);
  1238. this.reportIntersection(worldNormal, hitPointWorld, shape, body, -1);
  1239. };
  1240. Ray.prototype[Shape.types.PLANE] = Ray.prototype.intersectPlane;
  1241. /**
  1242. * Get the world AABB of the ray.
  1243. * @method getAABB
  1244. * @param {AABB} aabb
  1245. */
  1246. Ray.prototype.getAABB = function(result){
  1247. var to = this.to;
  1248. var from = this.from;
  1249. result.lowerBound.x = Math.min(to.x, from.x);
  1250. result.lowerBound.y = Math.min(to.y, from.y);
  1251. result.lowerBound.z = Math.min(to.z, from.z);
  1252. result.upperBound.x = Math.max(to.x, from.x);
  1253. result.upperBound.y = Math.max(to.y, from.y);
  1254. result.upperBound.z = Math.max(to.z, from.z);
  1255. };
  1256. var intersectConvexOptions = {
  1257. faceList: [0]
  1258. };
  1259. /**
  1260. * @method intersectHeightfield
  1261. * @private
  1262. * @param {Shape} shape
  1263. * @param {Quaternion} quat
  1264. * @param {Vec3} position
  1265. * @param {Body} body
  1266. */
  1267. Ray.prototype.intersectHeightfield = function(shape, quat, position, body){
  1268. var data = shape.data,
  1269. w = shape.elementSize,
  1270. worldPillarOffset = new Vec3();
  1271. // Convert the ray to local heightfield coordinates
  1272. var localRay = new Ray(this.from, this.to);
  1273. Transform.pointToLocalFrame(position, quat, localRay.from, localRay.from);
  1274. Transform.pointToLocalFrame(position, quat, localRay.to, localRay.to);
  1275. // Get the index of the data points to test against
  1276. var index = [];
  1277. var iMinX = null;
  1278. var iMinY = null;
  1279. var iMaxX = null;
  1280. var iMaxY = null;
  1281. var inside = shape.getIndexOfPosition(localRay.from.x, localRay.from.y, index, false);
  1282. if(inside){
  1283. iMinX = index[0];
  1284. iMinY = index[1];
  1285. iMaxX = index[0];
  1286. iMaxY = index[1];
  1287. }
  1288. inside = shape.getIndexOfPosition(localRay.to.x, localRay.to.y, index, false);
  1289. if(inside){
  1290. if (iMinX === null || index[0] < iMinX) { iMinX = index[0]; }
  1291. if (iMaxX === null || index[0] > iMaxX) { iMaxX = index[0]; }
  1292. if (iMinY === null || index[1] < iMinY) { iMinY = index[1]; }
  1293. if (iMaxY === null || index[1] > iMaxY) { iMaxY = index[1]; }
  1294. }
  1295. if(iMinX === null){
  1296. return;
  1297. }
  1298. var minMax = [];
  1299. shape.getRectMinMax(iMinX, iMinY, iMaxX, iMaxY, minMax);
  1300. var min = minMax[0];
  1301. var max = minMax[1];
  1302. // // Bail out if the ray can't touch the bounding box
  1303. // // TODO
  1304. // var aabb = new AABB();
  1305. // this.getAABB(aabb);
  1306. // if(aabb.intersects()){
  1307. // return;
  1308. // }
  1309. for(var i = iMinX; i <= iMaxX; i++){
  1310. for(var j = iMinY; j <= iMaxY; j++){
  1311. if(this.result._shouldStop){
  1312. return;
  1313. }
  1314. // Lower triangle
  1315. shape.getConvexTrianglePillar(i, j, false);
  1316. Transform.pointToWorldFrame(position, quat, shape.pillarOffset, worldPillarOffset);
  1317. this.intersectConvex(shape.pillarConvex, quat, worldPillarOffset, body, intersectConvexOptions);
  1318. if(this.result._shouldStop){
  1319. return;
  1320. }
  1321. // Upper triangle
  1322. shape.getConvexTrianglePillar(i, j, true);
  1323. Transform.pointToWorldFrame(position, quat, shape.pillarOffset, worldPillarOffset);
  1324. this.intersectConvex(shape.pillarConvex, quat, worldPillarOffset, body, intersectConvexOptions);
  1325. }
  1326. }
  1327. };
  1328. Ray.prototype[Shape.types.HEIGHTFIELD] = Ray.prototype.intersectHeightfield;
  1329. var Ray_intersectSphere_intersectionPoint = new Vec3();
  1330. var Ray_intersectSphere_normal = new Vec3();
  1331. /**
  1332. * @method intersectSphere
  1333. * @private
  1334. * @param {Shape} shape
  1335. * @param {Quaternion} quat
  1336. * @param {Vec3} position
  1337. * @param {Body} body
  1338. */
  1339. Ray.prototype.intersectSphere = function(shape, quat, position, body){
  1340. var from = this.from,
  1341. to = this.to,
  1342. r = shape.radius;
  1343. var a = Math.pow(to.x - from.x, 2) + Math.pow(to.y - from.y, 2) + Math.pow(to.z - from.z, 2);
  1344. var b = 2 * ((to.x - from.x) * (from.x - position.x) + (to.y - from.y) * (from.y - position.y) + (to.z - from.z) * (from.z - position.z));
  1345. var c = Math.pow(from.x - position.x, 2) + Math.pow(from.y - position.y, 2) + Math.pow(from.z - position.z, 2) - Math.pow(r, 2);
  1346. var delta = Math.pow(b, 2) - 4 * a * c;
  1347. var intersectionPoint = Ray_intersectSphere_intersectionPoint;
  1348. var normal = Ray_intersectSphere_normal;
  1349. if(delta < 0){
  1350. // No intersection
  1351. return;
  1352. } else if(delta === 0){
  1353. // single intersection point
  1354. from.lerp(to, delta, intersectionPoint);
  1355. intersectionPoint.vsub(position, normal);
  1356. normal.normalize();
  1357. this.reportIntersection(normal, intersectionPoint, shape, body, -1);
  1358. } else {
  1359. var d1 = (- b - Math.sqrt(delta)) / (2 * a);
  1360. var d2 = (- b + Math.sqrt(delta)) / (2 * a);
  1361. if(d1 >= 0 && d1 <= 1){
  1362. from.lerp(to, d1, intersectionPoint);
  1363. intersectionPoint.vsub(position, normal);
  1364. normal.normalize();
  1365. this.reportIntersection(normal, intersectionPoint, shape, body, -1);
  1366. }
  1367. if(this.result._shouldStop){
  1368. return;
  1369. }
  1370. if(d2 >= 0 && d2 <= 1){
  1371. from.lerp(to, d2, intersectionPoint);
  1372. intersectionPoint.vsub(position, normal);
  1373. normal.normalize();
  1374. this.reportIntersection(normal, intersectionPoint, shape, body, -1);
  1375. }
  1376. }
  1377. };
  1378. Ray.prototype[Shape.types.SPHERE] = Ray.prototype.intersectSphere;
  1379. var intersectConvex_normal = new Vec3();
  1380. var intersectConvex_minDistNormal = new Vec3();
  1381. var intersectConvex_minDistIntersect = new Vec3();
  1382. var intersectConvex_vector = new Vec3();
  1383. /**
  1384. * @method intersectConvex
  1385. * @private
  1386. * @param {Shape} shape
  1387. * @param {Quaternion} quat
  1388. * @param {Vec3} position
  1389. * @param {Body} body
  1390. * @param {object} [options]
  1391. * @param {array} [options.faceList]
  1392. */
  1393. Ray.prototype.intersectConvex = function intersectConvex(
  1394. shape,
  1395. quat,
  1396. position,
  1397. body,
  1398. options
  1399. ){
  1400. var minDistNormal = intersectConvex_minDistNormal;
  1401. var normal = intersectConvex_normal;
  1402. var vector = intersectConvex_vector;
  1403. var minDistIntersect = intersectConvex_minDistIntersect;
  1404. var faceList = (options && options.faceList) || null;
  1405. // Checking faces
  1406. var faces = shape.faces,
  1407. vertices = shape.vertices,
  1408. normals = shape.faceNormals;
  1409. var direction = this._direction;
  1410. var from = this.from;
  1411. var to = this.to;
  1412. var fromToDistance = from.distanceTo(to);
  1413. var minDist = -1;
  1414. var Nfaces = faceList ? faceList.length : faces.length;
  1415. var result = this.result;
  1416. for (var j = 0; !result._shouldStop && j < Nfaces; j++) {
  1417. var fi = faceList ? faceList[j] : j;
  1418. var face = faces[fi];
  1419. var faceNormal = normals[fi];
  1420. var q = quat;
  1421. var x = position;
  1422. // determine if ray intersects the plane of the face
  1423. // note: this works regardless of the direction of the face normal
  1424. // Get plane point in world coordinates...
  1425. vector.copy(vertices[face[0]]);
  1426. q.vmult(vector,vector);
  1427. vector.vadd(x,vector);
  1428. // ...but make it relative to the ray from. We'll fix this later.
  1429. vector.vsub(from,vector);
  1430. // Get plane normal
  1431. q.vmult(faceNormal,normal);
  1432. // If this dot product is negative, we have something interesting
  1433. var dot = direction.dot(normal);
  1434. // Bail out if ray and plane are parallel
  1435. if ( Math.abs( dot ) < this.precision ){
  1436. continue;
  1437. }
  1438. // calc distance to plane
  1439. var scalar = normal.dot(vector) / dot;
  1440. // if negative distance, then plane is behind ray
  1441. if (scalar < 0){
  1442. continue;
  1443. }
  1444. // if (dot < 0) {
  1445. // Intersection point is from + direction * scalar
  1446. direction.mult(scalar,intersectPoint);
  1447. intersectPoint.vadd(from,intersectPoint);
  1448. // a is the point we compare points b and c with.
  1449. a.copy(vertices[face[0]]);
  1450. q.vmult(a,a);
  1451. x.vadd(a,a);
  1452. for(var i = 1; !result._shouldStop && i < face.length - 1; i++){
  1453. // Transform 3 vertices to world coords
  1454. b.copy(vertices[face[i]]);
  1455. c.copy(vertices[face[i+1]]);
  1456. q.vmult(b,b);
  1457. q.vmult(c,c);
  1458. x.vadd(b,b);
  1459. x.vadd(c,c);
  1460. var distance = intersectPoint.distanceTo(from);
  1461. if(!(pointInTriangle(intersectPoint, a, b, c) || pointInTriangle(intersectPoint, b, a, c)) || distance > fromToDistance){
  1462. continue;
  1463. }
  1464. this.reportIntersection(normal, intersectPoint, shape, body, fi);
  1465. }
  1466. // }
  1467. }
  1468. };
  1469. Ray.prototype[Shape.types.CONVEXPOLYHEDRON] = Ray.prototype.intersectConvex;
  1470. var intersectTrimesh_normal = new Vec3();
  1471. var intersectTrimesh_localDirection = new Vec3();
  1472. var intersectTrimesh_localFrom = new Vec3();
  1473. var intersectTrimesh_localTo = new Vec3();
  1474. var intersectTrimesh_worldNormal = new Vec3();
  1475. var intersectTrimesh_worldIntersectPoint = new Vec3();
  1476. var intersectTrimesh_localAABB = new AABB();
  1477. var intersectTrimesh_triangles = [];
  1478. var intersectTrimesh_treeTransform = new Transform();
  1479. /**
  1480. * @method intersectTrimesh
  1481. * @private
  1482. * @param {Shape} shape
  1483. * @param {Quaternion} quat
  1484. * @param {Vec3} position
  1485. * @param {Body} body
  1486. * @param {object} [options]
  1487. * @todo Optimize by transforming the world to local space first.
  1488. * @todo Use Octree lookup
  1489. */
  1490. Ray.prototype.intersectTrimesh = function intersectTrimesh(
  1491. mesh,
  1492. quat,
  1493. position,
  1494. body,
  1495. options
  1496. ){
  1497. var normal = intersectTrimesh_normal;
  1498. var triangles = intersectTrimesh_triangles;
  1499. var treeTransform = intersectTrimesh_treeTransform;
  1500. var minDistNormal = intersectConvex_minDistNormal;
  1501. var vector = intersectConvex_vector;
  1502. var minDistIntersect = intersectConvex_minDistIntersect;
  1503. var localAABB = intersectTrimesh_localAABB;
  1504. var localDirection = intersectTrimesh_localDirection;
  1505. var localFrom = intersectTrimesh_localFrom;
  1506. var localTo = intersectTrimesh_localTo;
  1507. var worldIntersectPoint = intersectTrimesh_worldIntersectPoint;
  1508. var worldNormal = intersectTrimesh_worldNormal;
  1509. var faceList = (options && options.faceList) || null;
  1510. // Checking faces
  1511. var indices = mesh.indices,
  1512. vertices = mesh.vertices,
  1513. normals = mesh.faceNormals;
  1514. var from = this.from;
  1515. var to = this.to;
  1516. var direction = this._direction;
  1517. var minDist = -1;
  1518. treeTransform.position.copy(position);
  1519. treeTransform.quaternion.copy(quat);
  1520. // Transform ray to local space!
  1521. Transform.vectorToLocalFrame(position, quat, direction, localDirection);
  1522. //body.vectorToLocalFrame(direction, localDirection);
  1523. Transform.pointToLocalFrame(position, quat, from, localFrom);
  1524. //body.pointToLocalFrame(from, localFrom);
  1525. Transform.pointToLocalFrame(position, quat, to, localTo);
  1526. //body.pointToLocalFrame(to, localTo);
  1527. var fromToDistanceSquared = localFrom.distanceSquared(localTo);
  1528. mesh.tree.rayQuery(this, treeTransform, triangles);
  1529. for (var i = 0, N = triangles.length; !this.result._shouldStop && i !== N; i++) {
  1530. var trianglesIndex = triangles[i];
  1531. mesh.getNormal(trianglesIndex, normal);
  1532. // determine if ray intersects the plane of the face
  1533. // note: this works regardless of the direction of the face normal
  1534. // Get plane point in world coordinates...
  1535. mesh.getVertex(indices[trianglesIndex * 3], a);
  1536. // ...but make it relative to the ray from. We'll fix this later.
  1537. a.vsub(localFrom,vector);
  1538. // Get plane normal
  1539. // quat.vmult(normal, normal);
  1540. // If this dot product is negative, we have something interesting
  1541. var dot = localDirection.dot(normal);
  1542. // Bail out if ray and plane are parallel
  1543. // if (Math.abs( dot ) < this.precision){
  1544. // continue;
  1545. // }
  1546. // calc distance to plane
  1547. var scalar = normal.dot(vector) / dot;
  1548. // if negative distance, then plane is behind ray
  1549. if (scalar < 0){
  1550. continue;
  1551. }
  1552. // Intersection point is from + direction * scalar
  1553. localDirection.scale(scalar,intersectPoint);
  1554. intersectPoint.vadd(localFrom,intersectPoint);
  1555. // Get triangle vertices
  1556. mesh.getVertex(indices[trianglesIndex * 3 + 1], b);
  1557. mesh.getVertex(indices[trianglesIndex * 3 + 2], c);
  1558. var squaredDistance = intersectPoint.distanceSquared(localFrom);
  1559. if(!(pointInTriangle(intersectPoint, b, a, c) || pointInTriangle(intersectPoint, a, b, c)) || squaredDistance > fromToDistanceSquared){
  1560. continue;
  1561. }
  1562. // transform intersectpoint and normal to world
  1563. Transform.vectorToWorldFrame(quat, normal, worldNormal);
  1564. //body.vectorToWorldFrame(normal, worldNormal);
  1565. Transform.pointToWorldFrame(position, quat, intersectPoint, worldIntersectPoint);
  1566. //body.pointToWorldFrame(intersectPoint, worldIntersectPoint);
  1567. this.reportIntersection(worldNormal, worldIntersectPoint, mesh, body, trianglesIndex);
  1568. }
  1569. triangles.length = 0;
  1570. };
  1571. Ray.prototype[Shape.types.TRIMESH] = Ray.prototype.intersectTrimesh;
  1572. /**
  1573. * @method reportIntersection
  1574. * @private
  1575. * @param {Vec3} normal
  1576. * @param {Vec3} hitPointWorld
  1577. * @param {Shape} shape
  1578. * @param {Body} body
  1579. * @return {boolean} True if the intersections should continue
  1580. */
  1581. Ray.prototype.reportIntersection = function(normal, hitPointWorld, shape, body, hitFaceIndex){
  1582. var from = this.from;
  1583. var to = this.to;
  1584. var distance = from.distanceTo(hitPointWorld);
  1585. var result = this.result;
  1586. // Skip back faces?
  1587. if(this.skipBackfaces && normal.dot(this._direction) > 0){
  1588. return;
  1589. }
  1590. result.hitFaceIndex = typeof(hitFaceIndex) !== 'undefined' ? hitFaceIndex : -1;
  1591. switch(this.mode){
  1592. case Ray.ALL:
  1593. this.hasHit = true;
  1594. result.set(
  1595. from,
  1596. to,
  1597. normal,
  1598. hitPointWorld,
  1599. shape,
  1600. body,
  1601. distance
  1602. );
  1603. result.hasHit = true;
  1604. this.callback(result);
  1605. break;
  1606. case Ray.CLOSEST:
  1607. // Store if closer than current closest
  1608. if(distance < result.distance || !result.hasHit){
  1609. this.hasHit = true;
  1610. result.hasHit = true;
  1611. result.set(
  1612. from,
  1613. to,
  1614. normal,
  1615. hitPointWorld,
  1616. shape,
  1617. body,
  1618. distance
  1619. );
  1620. }
  1621. break;
  1622. case Ray.ANY:
  1623. // Report and stop.
  1624. this.hasHit = true;
  1625. result.hasHit = true;
  1626. result.set(
  1627. from,
  1628. to,
  1629. normal,
  1630. hitPointWorld,
  1631. shape,
  1632. body,
  1633. distance
  1634. );
  1635. result._shouldStop = true;
  1636. break;
  1637. }
  1638. };
  1639. var v0 = new Vec3(),
  1640. intersect = new Vec3();
  1641. function distanceFromIntersection(from, direction, position) {
  1642. // v0 is vector from from to position
  1643. position.vsub(from,v0);
  1644. var dot = v0.dot(direction);
  1645. // intersect = direction*dot + from
  1646. direction.mult(dot,intersect);
  1647. intersect.vadd(from,intersect);
  1648. var distance = position.distanceTo(intersect);
  1649. return distance;
  1650. }
  1651. },{"../collision/AABB":3,"../collision/RaycastResult":10,"../math/Quaternion":28,"../math/Transform":29,"../math/Vec3":30,"../shapes/Box":37,"../shapes/ConvexPolyhedron":38,"../shapes/Shape":43}],10:[function(_dereq_,module,exports){
  1652. var Vec3 = _dereq_('../math/Vec3');
  1653. module.exports = RaycastResult;
  1654. /**
  1655. * Storage for Ray casting data.
  1656. * @class RaycastResult
  1657. * @constructor
  1658. */
  1659. function RaycastResult(){
  1660. /**
  1661. * @property {Vec3} rayFromWorld
  1662. */
  1663. this.rayFromWorld = new Vec3();
  1664. /**
  1665. * @property {Vec3} rayToWorld
  1666. */
  1667. this.rayToWorld = new Vec3();
  1668. /**
  1669. * @property {Vec3} hitNormalWorld
  1670. */
  1671. this.hitNormalWorld = new Vec3();
  1672. /**
  1673. * @property {Vec3} hitPointWorld
  1674. */
  1675. this.hitPointWorld = new Vec3();
  1676. /**
  1677. * @property {boolean} hasHit
  1678. */
  1679. this.hasHit = false;
  1680. /**
  1681. * The hit shape, or null.
  1682. * @property {Shape} shape
  1683. */
  1684. this.shape = null;
  1685. /**
  1686. * The hit body, or null.
  1687. * @property {Body} body
  1688. */
  1689. this.body = null;
  1690. /**
  1691. * The index of the hit triangle, if the hit shape was a trimesh.
  1692. * @property {number} hitFaceIndex
  1693. * @default -1
  1694. */
  1695. this.hitFaceIndex = -1;
  1696. /**
  1697. * Distance to the hit. Will be set to -1 if there was no hit.
  1698. * @property {number} distance
  1699. * @default -1
  1700. */
  1701. this.distance = -1;
  1702. /**
  1703. * If the ray should stop traversing the bodies.
  1704. * @private
  1705. * @property {Boolean} _shouldStop
  1706. * @default false
  1707. */
  1708. this._shouldStop = false;
  1709. }
  1710. /**
  1711. * Reset all result data.
  1712. * @method reset
  1713. */
  1714. RaycastResult.prototype.reset = function () {
  1715. this.rayFromWorld.setZero();
  1716. this.rayToWorld.setZero();
  1717. this.hitNormalWorld.setZero();
  1718. this.hitPointWorld.setZero();
  1719. this.hasHit = false;
  1720. this.shape = null;
  1721. this.body = null;
  1722. this.hitFaceIndex = -1;
  1723. this.distance = -1;
  1724. this._shouldStop = false;
  1725. };
  1726. /**
  1727. * @method abort
  1728. */
  1729. RaycastResult.prototype.abort = function(){
  1730. this._shouldStop = true;
  1731. };
  1732. /**
  1733. * @method set
  1734. * @param {Vec3} rayFromWorld
  1735. * @param {Vec3} rayToWorld
  1736. * @param {Vec3} hitNormalWorld
  1737. * @param {Vec3} hitPointWorld
  1738. * @param {Shape} shape
  1739. * @param {Body} body
  1740. * @param {number} distance
  1741. */
  1742. RaycastResult.prototype.set = function(
  1743. rayFromWorld,
  1744. rayToWorld,
  1745. hitNormalWorld,
  1746. hitPointWorld,
  1747. shape,
  1748. body,
  1749. distance
  1750. ){
  1751. this.rayFromWorld.copy(rayFromWorld);
  1752. this.rayToWorld.copy(rayToWorld);
  1753. this.hitNormalWorld.copy(hitNormalWorld);
  1754. this.hitPointWorld.copy(hitPointWorld);
  1755. this.shape = shape;
  1756. this.body = body;
  1757. this.distance = distance;
  1758. };
  1759. },{"../math/Vec3":30}],11:[function(_dereq_,module,exports){
  1760. var Shape = _dereq_('../shapes/Shape');
  1761. var Broadphase = _dereq_('../collision/Broadphase');
  1762. module.exports = SAPBroadphase;
  1763. /**
  1764. * Sweep and prune broadphase along one axis.
  1765. *
  1766. * @class SAPBroadphase
  1767. * @constructor
  1768. * @param {World} [world]
  1769. * @extends Broadphase
  1770. */
  1771. function SAPBroadphase(world){
  1772. Broadphase.apply(this);
  1773. /**
  1774. * List of bodies currently in the broadphase.
  1775. * @property axisList
  1776. * @type {Array}
  1777. */
  1778. this.axisList = [];
  1779. /**
  1780. * The world to search in.
  1781. * @property world
  1782. * @type {World}
  1783. */
  1784. this.world = null;
  1785. /**
  1786. * Axis to sort the bodies along. Set to 0 for x axis, and 1 for y axis. For best performance, choose an axis that the bodies are spread out more on.
  1787. * @property axisIndex
  1788. * @type {Number}
  1789. */
  1790. this.axisIndex = 0;
  1791. var axisList = this.axisList;
  1792. this._addBodyHandler = function(e){
  1793. axisList.push(e.body);
  1794. };
  1795. this._removeBodyHandler = function(e){
  1796. var idx = axisList.indexOf(e.body);
  1797. if(idx !== -1){
  1798. axisList.splice(idx,1);
  1799. }
  1800. };
  1801. if(world){
  1802. this.setWorld(world);
  1803. }
  1804. }
  1805. SAPBroadphase.prototype = new Broadphase();
  1806. /**
  1807. * Change the world
  1808. * @method setWorld
  1809. * @param {World} world
  1810. */
  1811. SAPBroadphase.prototype.setWorld = function(world){
  1812. // Clear the old axis array
  1813. this.axisList.length = 0;
  1814. // Add all bodies from the new world
  1815. for(var i=0; i<world.bodies.length; i++){
  1816. this.axisList.push(world.bodies[i]);
  1817. }
  1818. // Remove old handlers, if any
  1819. world.removeEventListener("addBody", this._addBodyHandler);
  1820. world.removeEventListener("removeBody", this._removeBodyHandler);
  1821. // Add handlers to update the list of bodies.
  1822. world.addEventListener("addBody", this._addBodyHandler);
  1823. world.addEventListener("removeBody", this._removeBodyHandler);
  1824. this.world = world;
  1825. this.dirty = true;
  1826. };
  1827. /**
  1828. * @static
  1829. * @method insertionSortX
  1830. * @param {Array} a
  1831. * @return {Array}
  1832. */
  1833. SAPBroadphase.insertionSortX = function(a) {
  1834. for(var i=1,l=a.length;i<l;i++) {
  1835. var v = a[i];
  1836. for(var j=i - 1;j>=0;j--) {
  1837. if(a[j].aabb.lowerBound.x <= v.aabb.lowerBound.x){
  1838. break;
  1839. }
  1840. a[j+1] = a[j];
  1841. }
  1842. a[j+1] = v;
  1843. }
  1844. return a;
  1845. };
  1846. /**
  1847. * @static
  1848. * @method insertionSortY
  1849. * @param {Array} a
  1850. * @return {Array}
  1851. */
  1852. SAPBroadphase.insertionSortY = function(a) {
  1853. for(var i=1,l=a.length;i<l;i++) {
  1854. var v = a[i];
  1855. for(var j=i - 1;j>=0;j--) {
  1856. if(a[j].aabb.lowerBound.y <= v.aabb.lowerBound.y){
  1857. break;
  1858. }
  1859. a[j+1] = a[j];
  1860. }
  1861. a[j+1] = v;
  1862. }
  1863. return a;
  1864. };
  1865. /**
  1866. * @static
  1867. * @method insertionSortZ
  1868. * @param {Array} a
  1869. * @return {Array}
  1870. */
  1871. SAPBroadphase.insertionSortZ = function(a) {
  1872. for(var i=1,l=a.length;i<l;i++) {
  1873. var v = a[i];
  1874. for(var j=i - 1;j>=0;j--) {
  1875. if(a[j].aabb.lowerBound.z <= v.aabb.lowerBound.z){
  1876. break;
  1877. }
  1878. a[j+1] = a[j];
  1879. }
  1880. a[j+1] = v;
  1881. }
  1882. return a;
  1883. };
  1884. /**
  1885. * Collect all collision pairs
  1886. * @method collisionPairs
  1887. * @param {World} world
  1888. * @param {Array} p1
  1889. * @param {Array} p2
  1890. */
  1891. SAPBroadphase.prototype.collisionPairs = function(world,p1,p2){
  1892. var bodies = this.axisList,
  1893. N = bodies.length,
  1894. axisIndex = this.axisIndex,
  1895. i, j;
  1896. if(this.dirty){
  1897. this.sortList();
  1898. this.dirty = false;
  1899. }
  1900. // Look through the list
  1901. for(i=0; i !== N; i++){
  1902. var bi = bodies[i];
  1903. for(j=i+1; j < N; j++){
  1904. var bj = bodies[j];
  1905. if(!this.needBroadphaseCollision(bi,bj)){
  1906. continue;
  1907. }
  1908. if(!SAPBroadphase.checkBounds(bi,bj,axisIndex)){
  1909. break;
  1910. }
  1911. this.intersectionTest(bi,bj,p1,p2);
  1912. }
  1913. }
  1914. };
  1915. SAPBroadphase.prototype.sortList = function(){
  1916. var axisList = this.axisList;
  1917. var axisIndex = this.axisIndex;
  1918. var N = axisList.length;
  1919. // Update AABBs
  1920. for(var i = 0; i!==N; i++){
  1921. var bi = axisList[i];
  1922. if(bi.aabbNeedsUpdate){
  1923. bi.computeAABB();
  1924. }
  1925. }
  1926. // Sort the list
  1927. if(axisIndex === 0){
  1928. SAPBroadphase.insertionSortX(axisList);
  1929. } else if(axisIndex === 1){
  1930. SAPBroadphase.insertionSortY(axisList);
  1931. } else if(axisIndex === 2){
  1932. SAPBroadphase.insertionSortZ(axisList);
  1933. }
  1934. };
  1935. /**
  1936. * Check if the bounds of two bodies overlap, along the given SAP axis.
  1937. * @static
  1938. * @method checkBounds
  1939. * @param {Body} bi
  1940. * @param {Body} bj
  1941. * @param {Number} axisIndex
  1942. * @return {Boolean}
  1943. */
  1944. SAPBroadphase.checkBounds = function(bi, bj, axisIndex){
  1945. var biPos;
  1946. var bjPos;
  1947. if(axisIndex === 0){
  1948. biPos = bi.position.x;
  1949. bjPos = bj.position.x;
  1950. } else if(axisIndex === 1){
  1951. biPos = bi.position.y;
  1952. bjPos = bj.position.y;
  1953. } else if(axisIndex === 2){
  1954. biPos = bi.position.z;
  1955. bjPos = bj.position.z;
  1956. }
  1957. var ri = bi.boundingRadius,
  1958. rj = bj.boundingRadius,
  1959. boundA1 = biPos - ri,
  1960. boundA2 = biPos + ri,
  1961. boundB1 = bjPos - rj,
  1962. boundB2 = bjPos + rj;
  1963. return boundB1 < boundA2;
  1964. };
  1965. /**
  1966. * Computes the variance of the body positions and estimates the best
  1967. * axis to use. Will automatically set property .axisIndex.
  1968. * @method autoDetectAxis
  1969. */
  1970. SAPBroadphase.prototype.autoDetectAxis = function(){
  1971. var sumX=0,
  1972. sumX2=0,
  1973. sumY=0,
  1974. sumY2=0,
  1975. sumZ=0,
  1976. sumZ2=0,
  1977. bodies = this.axisList,
  1978. N = bodies.length,
  1979. invN=1/N;
  1980. for(var i=0; i!==N; i++){
  1981. var b = bodies[i];
  1982. var centerX = b.position.x;
  1983. sumX += centerX;
  1984. sumX2 += centerX*centerX;
  1985. var centerY = b.position.y;
  1986. sumY += centerY;
  1987. sumY2 += centerY*centerY;
  1988. var centerZ = b.position.z;
  1989. sumZ += centerZ;
  1990. sumZ2 += centerZ*centerZ;
  1991. }
  1992. var varianceX = sumX2 - sumX*sumX*invN,
  1993. varianceY = sumY2 - sumY*sumY*invN,
  1994. varianceZ = sumZ2 - sumZ*sumZ*invN;
  1995. if(varianceX > varianceY){
  1996. if(varianceX > varianceZ){
  1997. this.axisIndex = 0;
  1998. } else{
  1999. this.axisIndex = 2;
  2000. }
  2001. } else if(varianceY > varianceZ){
  2002. this.axisIndex = 1;
  2003. } else{
  2004. this.axisIndex = 2;
  2005. }
  2006. };
  2007. /**
  2008. * Returns all the bodies within an AABB.
  2009. * @method aabbQuery
  2010. * @param {World} world
  2011. * @param {AABB} aabb
  2012. * @param {array} result An array to store resulting bodies in.
  2013. * @return {array}
  2014. */
  2015. SAPBroadphase.prototype.aabbQuery = function(world, aabb, result){
  2016. result = result || [];
  2017. if(this.dirty){
  2018. this.sortList();
  2019. this.dirty = false;
  2020. }
  2021. var axisIndex = this.axisIndex, axis = 'x';
  2022. if(axisIndex === 1){ axis = 'y'; }
  2023. if(axisIndex === 2){ axis = 'z'; }
  2024. var axisList = this.axisList;
  2025. var lower = aabb.lowerBound[axis];
  2026. var upper = aabb.upperBound[axis];
  2027. for(var i = 0; i < axisList.length; i++){
  2028. var b = axisList[i];
  2029. if(b.aabbNeedsUpdate){
  2030. b.computeAABB();
  2031. }
  2032. if(b.aabb.overlaps(aabb)){
  2033. result.push(b);
  2034. }
  2035. }
  2036. return result;
  2037. };
  2038. },{"../collision/Broadphase":5,"../shapes/Shape":43}],12:[function(_dereq_,module,exports){
  2039. module.exports = ConeTwistConstraint;
  2040. var Constraint = _dereq_('./Constraint');
  2041. var PointToPointConstraint = _dereq_('./PointToPointConstraint');
  2042. var ConeEquation = _dereq_('../equations/ConeEquation');
  2043. var RotationalEquation = _dereq_('../equations/RotationalEquation');
  2044. var ContactEquation = _dereq_('../equations/ContactEquation');
  2045. var Vec3 = _dereq_('../math/Vec3');
  2046. /**
  2047. * @class ConeTwistConstraint
  2048. * @constructor
  2049. * @author schteppe
  2050. * @param {Body} bodyA
  2051. * @param {Body} bodyB
  2052. * @param {object} [options]
  2053. * @param {Vec3} [options.pivotA]
  2054. * @param {Vec3} [options.pivotB]
  2055. * @param {Vec3} [options.axisA]
  2056. * @param {Vec3} [options.axisB]
  2057. * @param {Number} [options.maxForce=1e6]
  2058. * @extends PointToPointConstraint
  2059. */
  2060. function ConeTwistConstraint(bodyA, bodyB, options){
  2061. options = options || {};
  2062. var maxForce = typeof(options.maxForce) !== 'undefined' ? options.maxForce : 1e6;
  2063. // Set pivot point in between
  2064. var pivotA = options.pivotA ? options.pivotA.clone() : new Vec3();
  2065. var pivotB = options.pivotB ? options.pivotB.clone() : new Vec3();
  2066. this.axisA = options.axisA ? options.axisA.clone() : new Vec3();
  2067. this.axisB = options.axisB ? options.axisB.clone() : new Vec3();
  2068. PointToPointConstraint.call(this, bodyA, pivotA, bodyB, pivotB, maxForce);
  2069. this.collideConnected = !!options.collideConnected;
  2070. this.angle = typeof(options.angle) !== 'undefined' ? options.angle : 0;
  2071. /**
  2072. * @property {ConeEquation} coneEquation
  2073. */
  2074. var c = this.coneEquation = new ConeEquation(bodyA,bodyB,options);
  2075. /**
  2076. * @property {RotationalEquation} twistEquation
  2077. */
  2078. var t = this.twistEquation = new RotationalEquation(bodyA,bodyB,options);
  2079. this.twistAngle = typeof(options.twistAngle) !== 'undefined' ? options.twistAngle : 0;
  2080. // Make the cone equation push the bodies toward the cone axis, not outward
  2081. c.maxForce = 0;
  2082. c.minForce = -maxForce;
  2083. // Make the twist equation add torque toward the initial position
  2084. t.maxForce = 0;
  2085. t.minForce = -maxForce;
  2086. this.equations.push(c, t);
  2087. }
  2088. ConeTwistConstraint.prototype = new PointToPointConstraint();
  2089. ConeTwistConstraint.constructor = ConeTwistConstraint;
  2090. var ConeTwistConstraint_update_tmpVec1 = new Vec3();
  2091. var ConeTwistConstraint_update_tmpVec2 = new Vec3();
  2092. ConeTwistConstraint.prototype.update = function(){
  2093. var bodyA = this.bodyA,
  2094. bodyB = this.bodyB,
  2095. cone = this.coneEquation,
  2096. twist = this.twistEquation;
  2097. PointToPointConstraint.prototype.update.call(this);
  2098. // Update the axes to the cone constraint
  2099. bodyA.vectorToWorldFrame(this.axisA, cone.axisA);
  2100. bodyB.vectorToWorldFrame(this.axisB, cone.axisB);
  2101. // Update the world axes in the twist constraint
  2102. this.axisA.tangents(twist.axisA, twist.axisA);
  2103. bodyA.vectorToWorldFrame(twist.axisA, twist.axisA);
  2104. this.axisB.tangents(twist.axisB, twist.axisB);
  2105. bodyB.vectorToWorldFrame(twist.axisB, twist.axisB);
  2106. cone.angle = this.angle;
  2107. twist.maxAngle = this.twistAngle;
  2108. };
  2109. },{"../equations/ConeEquation":18,"../equations/ContactEquation":19,"../equations/RotationalEquation":22,"../math/Vec3":30,"./Constraint":13,"./PointToPointConstraint":17}],13:[function(_dereq_,module,exports){
  2110. module.exports = Constraint;
  2111. var Utils = _dereq_('../utils/Utils');
  2112. /**
  2113. * Constraint base class
  2114. * @class Constraint
  2115. * @author schteppe
  2116. * @constructor
  2117. * @param {Body} bodyA
  2118. * @param {Body} bodyB
  2119. * @param {object} [options]
  2120. * @param {boolean} [options.collideConnected=true]
  2121. * @param {boolean} [options.wakeUpBodies=true]
  2122. */
  2123. function Constraint(bodyA, bodyB, options){
  2124. options = Utils.defaults(options,{
  2125. collideConnected : true,
  2126. wakeUpBodies : true,
  2127. });
  2128. /**
  2129. * Equations to be solved in this constraint
  2130. * @property equations
  2131. * @type {Array}
  2132. */
  2133. this.equations = [];
  2134. /**
  2135. * @property {Body} bodyA
  2136. */
  2137. this.bodyA = bodyA;
  2138. /**
  2139. * @property {Body} bodyB
  2140. */
  2141. this.bodyB = bodyB;
  2142. /**
  2143. * @property {Number} id
  2144. */
  2145. this.id = Constraint.idCounter++;
  2146. /**
  2147. * Set to true if you want the bodies to collide when they are connected.
  2148. * @property collideConnected
  2149. * @type {boolean}
  2150. */
  2151. this.collideConnected = options.collideConnected;
  2152. if(options.wakeUpBodies){
  2153. if(bodyA){
  2154. bodyA.wakeUp();
  2155. }
  2156. if(bodyB){
  2157. bodyB.wakeUp();
  2158. }
  2159. }
  2160. }
  2161. /**
  2162. * Update all the equations with data.
  2163. * @method update
  2164. */
  2165. Constraint.prototype.update = function(){
  2166. throw new Error("method update() not implmemented in this Constraint subclass!");
  2167. };
  2168. /**
  2169. * Enables all equations in the constraint.
  2170. * @method enable
  2171. */
  2172. Constraint.prototype.enable = function(){
  2173. var eqs = this.equations;
  2174. for(var i=0; i<eqs.length; i++){
  2175. eqs[i].enabled = true;
  2176. }
  2177. };
  2178. /**
  2179. * Disables all equations in the constraint.
  2180. * @method disable
  2181. */
  2182. Constraint.prototype.disable = function(){
  2183. var eqs = this.equations;
  2184. for(var i=0; i<eqs.length; i++){
  2185. eqs[i].enabled = false;
  2186. }
  2187. };
  2188. Constraint.idCounter = 0;
  2189. },{"../utils/Utils":53}],14:[function(_dereq_,module,exports){
  2190. module.exports = DistanceConstraint;
  2191. var Constraint = _dereq_('./Constraint');
  2192. var ContactEquation = _dereq_('../equations/ContactEquation');
  2193. /**
  2194. * Constrains two bodies to be at a constant distance from each others center of mass.
  2195. * @class DistanceConstraint
  2196. * @constructor
  2197. * @author schteppe
  2198. * @param {Body} bodyA
  2199. * @param {Body} bodyB
  2200. * @param {Number} [distance] The distance to keep. If undefined, it will be set to the current distance between bodyA and bodyB
  2201. * @param {Number} [maxForce=1e6]
  2202. * @extends Constraint
  2203. */
  2204. function DistanceConstraint(bodyA,bodyB,distance,maxForce){
  2205. Constraint.call(this,bodyA,bodyB);
  2206. if(typeof(distance)==="undefined") {
  2207. distance = bodyA.position.distanceTo(bodyB.position);
  2208. }
  2209. if(typeof(maxForce)==="undefined") {
  2210. maxForce = 1e6;
  2211. }
  2212. /**
  2213. * @property {number} distance
  2214. */
  2215. this.distance = distance;
  2216. /**
  2217. * @property {ContactEquation} distanceEquation
  2218. */
  2219. var eq = this.distanceEquation = new ContactEquation(bodyA, bodyB);
  2220. this.equations.push(eq);
  2221. // Make it bidirectional
  2222. eq.minForce = -maxForce;
  2223. eq.maxForce = maxForce;
  2224. }
  2225. DistanceConstraint.prototype = new Constraint();
  2226. DistanceConstraint.prototype.update = function(){
  2227. var bodyA = this.bodyA;
  2228. var bodyB = this.bodyB;
  2229. var eq = this.distanceEquation;
  2230. var halfDist = this.distance * 0.5;
  2231. var normal = eq.ni;
  2232. bodyB.position.vsub(bodyA.position, normal);
  2233. normal.normalize();
  2234. normal.mult(halfDist, eq.ri);
  2235. normal.mult(-halfDist, eq.rj);
  2236. };
  2237. },{"../equations/ContactEquation":19,"./Constraint":13}],15:[function(_dereq_,module,exports){
  2238. module.exports = HingeConstraint;
  2239. var Constraint = _dereq_('./Constraint');
  2240. var PointToPointConstraint = _dereq_('./PointToPointConstraint');
  2241. var RotationalEquation = _dereq_('../equations/RotationalEquation');
  2242. var RotationalMotorEquation = _dereq_('../equations/RotationalMotorEquation');
  2243. var ContactEquation = _dereq_('../equations/ContactEquation');
  2244. var Vec3 = _dereq_('../math/Vec3');
  2245. /**
  2246. * Hinge constraint. Think of it as a door hinge. It tries to keep the door in the correct place and with the correct orientation.
  2247. * @class HingeConstraint
  2248. * @constructor
  2249. * @author schteppe
  2250. * @param {Body} bodyA
  2251. * @param {Body} bodyB
  2252. * @param {object} [options]
  2253. * @param {Vec3} [options.pivotA] A point defined locally in bodyA. This defines the offset of axisA.
  2254. * @param {Vec3} [options.axisA] An axis that bodyA can rotate around, defined locally in bodyA.
  2255. * @param {Vec3} [options.pivotB]
  2256. * @param {Vec3} [options.axisB]
  2257. * @param {Number} [options.maxForce=1e6]
  2258. * @extends PointToPointConstraint
  2259. */
  2260. function HingeConstraint(bodyA, bodyB, options){
  2261. options = options || {};
  2262. var maxForce = typeof(options.maxForce) !== 'undefined' ? options.maxForce : 1e6;
  2263. var pivotA = options.pivotA ? options.pivotA.clone() : new Vec3();
  2264. var pivotB = options.pivotB ? options.pivotB.clone() : new Vec3();
  2265. PointToPointConstraint.call(this, bodyA, pivotA, bodyB, pivotB, maxForce);
  2266. /**
  2267. * Rotation axis, defined locally in bodyA.
  2268. * @property {Vec3} axisA
  2269. */
  2270. var axisA = this.axisA = options.axisA ? options.axisA.clone() : new Vec3(1,0,0);
  2271. axisA.normalize();
  2272. /**
  2273. * Rotation axis, defined locally in bodyB.
  2274. * @property {Vec3} axisB
  2275. */
  2276. var axisB = this.axisB = options.axisB ? options.axisB.clone() : new Vec3(1,0,0);
  2277. axisB.normalize();
  2278. /**
  2279. * @property {RotationalEquation} rotationalEquation1
  2280. */
  2281. var r1 = this.rotationalEquation1 = new RotationalEquation(bodyA,bodyB,options);
  2282. /**
  2283. * @property {RotationalEquation} rotationalEquation2
  2284. */
  2285. var r2 = this.rotationalEquation2 = new RotationalEquation(bodyA,bodyB,options);
  2286. /**
  2287. * @property {RotationalMotorEquation} motorEquation
  2288. */
  2289. var motor = this.motorEquation = new RotationalMotorEquation(bodyA,bodyB,maxForce);
  2290. motor.enabled = false; // Not enabled by default
  2291. // Equations to be fed to the solver
  2292. this.equations.push(
  2293. r1, // rotational1
  2294. r2, // rotational2
  2295. motor
  2296. );
  2297. }
  2298. HingeConstraint.prototype = new PointToPointConstraint();
  2299. HingeConstraint.constructor = HingeConstraint;
  2300. /**
  2301. * @method enableMotor
  2302. */
  2303. HingeConstraint.prototype.enableMotor = function(){
  2304. this.motorEquation.enabled = true;
  2305. };
  2306. /**
  2307. * @method disableMotor
  2308. */
  2309. HingeConstraint.prototype.disableMotor = function(){
  2310. this.motorEquation.enabled = false;
  2311. };
  2312. /**
  2313. * @method setMotorSpeed
  2314. * @param {number} speed
  2315. */
  2316. HingeConstraint.prototype.setMotorSpeed = function(speed){
  2317. this.motorEquation.targetVelocity = speed;
  2318. };
  2319. /**
  2320. * @method setMotorMaxForce
  2321. * @param {number} maxForce
  2322. */
  2323. HingeConstraint.prototype.setMotorMaxForce = function(maxForce){
  2324. this.motorEquation.maxForce = maxForce;
  2325. this.motorEquation.minForce = -maxForce;
  2326. };
  2327. var HingeConstraint_update_tmpVec1 = new Vec3();
  2328. var HingeConstraint_update_tmpVec2 = new Vec3();
  2329. HingeConstraint.prototype.update = function(){
  2330. var bodyA = this.bodyA,
  2331. bodyB = this.bodyB,
  2332. motor = this.motorEquation,
  2333. r1 = this.rotationalEquation1,
  2334. r2 = this.rotationalEquation2,
  2335. worldAxisA = HingeConstraint_update_tmpVec1,
  2336. worldAxisB = HingeConstraint_update_tmpVec2;
  2337. var axisA = this.axisA;
  2338. var axisB = this.axisB;
  2339. PointToPointConstraint.prototype.update.call(this);
  2340. // Get world axes
  2341. bodyA.quaternion.vmult(axisA, worldAxisA);
  2342. bodyB.quaternion.vmult(axisB, worldAxisB);
  2343. worldAxisA.tangents(r1.axisA, r2.axisA);
  2344. r1.axisB.copy(worldAxisB);
  2345. r2.axisB.copy(worldAxisB);
  2346. if(this.motorEquation.enabled){
  2347. bodyA.quaternion.vmult(this.axisA, motor.axisA);
  2348. bodyB.quaternion.vmult(this.axisB, motor.axisB);
  2349. }
  2350. };
  2351. },{"../equations/ContactEquation":19,"../equations/RotationalEquation":22,"../equations/RotationalMotorEquation":23,"../math/Vec3":30,"./Constraint":13,"./PointToPointConstraint":17}],16:[function(_dereq_,module,exports){
  2352. module.exports = LockConstraint;
  2353. var Constraint = _dereq_('./Constraint');
  2354. var PointToPointConstraint = _dereq_('./PointToPointConstraint');
  2355. var RotationalEquation = _dereq_('../equations/RotationalEquation');
  2356. var RotationalMotorEquation = _dereq_('../equations/RotationalMotorEquation');
  2357. var ContactEquation = _dereq_('../equations/ContactEquation');
  2358. var Vec3 = _dereq_('../math/Vec3');
  2359. /**
  2360. * Lock constraint. Will remove all degrees of freedom between the bodies.
  2361. * @class LockConstraint
  2362. * @constructor
  2363. * @author schteppe
  2364. * @param {Body} bodyA
  2365. * @param {Body} bodyB
  2366. * @param {object} [options]
  2367. * @param {Number} [options.maxForce=1e6]
  2368. * @extends PointToPointConstraint
  2369. */
  2370. function LockConstraint(bodyA, bodyB, options){
  2371. options = options || {};
  2372. var maxForce = typeof(options.maxForce) !== 'undefined' ? options.maxForce : 1e6;
  2373. // Set pivot point in between
  2374. var pivotA = new Vec3();
  2375. var pivotB = new Vec3();
  2376. var halfWay = new Vec3();
  2377. bodyA.position.vadd(bodyB.position, halfWay);
  2378. halfWay.scale(0.5, halfWay);
  2379. bodyB.pointToLocalFrame(halfWay, pivotB);
  2380. bodyA.pointToLocalFrame(halfWay, pivotA);
  2381. PointToPointConstraint.call(this, bodyA, pivotA, bodyB, pivotB, maxForce);
  2382. /**
  2383. * @property {RotationalEquation} rotationalEquation1
  2384. */
  2385. var r1 = this.rotationalEquation1 = new RotationalEquation(bodyA,bodyB,options);
  2386. /**
  2387. * @property {RotationalEquation} rotationalEquation2
  2388. */
  2389. var r2 = this.rotationalEquation2 = new RotationalEquation(bodyA,bodyB,options);
  2390. /**
  2391. * @property {RotationalEquation} rotationalEquation3
  2392. */
  2393. var r3 = this.rotationalEquation3 = new RotationalEquation(bodyA,bodyB,options);
  2394. this.equations.push(r1, r2, r3);
  2395. }
  2396. LockConstraint.prototype = new PointToPointConstraint();
  2397. LockConstraint.constructor = LockConstraint;
  2398. var LockConstraint_update_tmpVec1 = new Vec3();
  2399. var LockConstraint_update_tmpVec2 = new Vec3();
  2400. LockConstraint.prototype.update = function(){
  2401. var bodyA = this.bodyA,
  2402. bodyB = this.bodyB,
  2403. motor = this.motorEquation,
  2404. r1 = this.rotationalEquation1,
  2405. r2 = this.rotationalEquation2,
  2406. r3 = this.rotationalEquation3,
  2407. worldAxisA = LockConstraint_update_tmpVec1,
  2408. worldAxisB = LockConstraint_update_tmpVec2;
  2409. PointToPointConstraint.prototype.update.call(this);
  2410. bodyA.vectorToWorldFrame(Vec3.UNIT_X, r1.axisA);
  2411. bodyB.vectorToWorldFrame(Vec3.UNIT_Y, r1.axisB);
  2412. bodyA.vectorToWorldFrame(Vec3.UNIT_Y, r2.axisA);
  2413. bodyB.vectorToWorldFrame(Vec3.UNIT_Z, r2.axisB);
  2414. bodyA.vectorToWorldFrame(Vec3.UNIT_Z, r3.axisA);
  2415. bodyB.vectorToWorldFrame(Vec3.UNIT_X, r3.axisB);
  2416. };
  2417. },{"../equations/ContactEquation":19,"../equations/RotationalEquation":22,"../equations/RotationalMotorEquation":23,"../math/Vec3":30,"./Constraint":13,"./PointToPointConstraint":17}],17:[function(_dereq_,module,exports){
  2418. module.exports = PointToPointConstraint;
  2419. var Constraint = _dereq_('./Constraint');
  2420. var ContactEquation = _dereq_('../equations/ContactEquation');
  2421. var Vec3 = _dereq_('../math/Vec3');
  2422. /**
  2423. * Connects two bodies at given offset points.
  2424. * @class PointToPointConstraint
  2425. * @extends Constraint
  2426. * @constructor
  2427. * @param {Body} bodyA
  2428. * @param {Vec3} pivotA The point relative to the center of mass of bodyA which bodyA is constrained to.
  2429. * @param {Body} bodyB Body that will be constrained in a similar way to the same point as bodyA. We will therefore get a link between bodyA and bodyB. If not specified, bodyA will be constrained to a static point.
  2430. * @param {Vec3} pivotB See pivotA.
  2431. * @param {Number} maxForce The maximum force that should be applied to constrain the bodies.
  2432. *
  2433. * @example
  2434. * var bodyA = new Body({ mass: 1 });
  2435. * var bodyB = new Body({ mass: 1 });
  2436. * bodyA.position.set(-1, 0, 0);
  2437. * bodyB.position.set(1, 0, 0);
  2438. * bodyA.addShape(shapeA);
  2439. * bodyB.addShape(shapeB);
  2440. * world.addBody(bodyA);
  2441. * world.addBody(bodyB);
  2442. * var localPivotA = new Vec3(1, 0, 0);
  2443. * var localPivotB = new Vec3(-1, 0, 0);
  2444. * var constraint = new PointToPointConstraint(bodyA, localPivotA, bodyB, localPivotB);
  2445. * world.addConstraint(constraint);
  2446. */
  2447. function PointToPointConstraint(bodyA,pivotA,bodyB,pivotB,maxForce){
  2448. Constraint.call(this,bodyA,bodyB);
  2449. maxForce = typeof(maxForce) !== 'undefined' ? maxForce : 1e6;
  2450. /**
  2451. * Pivot, defined locally in bodyA.
  2452. * @property {Vec3} pivotA
  2453. */
  2454. this.pivotA = pivotA ? pivotA.clone() : new Vec3();
  2455. /**
  2456. * Pivot, defined locally in bodyB.
  2457. * @property {Vec3} pivotB
  2458. */
  2459. this.pivotB = pivotB ? pivotB.clone() : new Vec3();
  2460. /**
  2461. * @property {ContactEquation} equationX
  2462. */
  2463. var x = this.equationX = new ContactEquation(bodyA,bodyB);
  2464. /**
  2465. * @property {ContactEquation} equationY
  2466. */
  2467. var y = this.equationY = new ContactEquation(bodyA,bodyB);
  2468. /**
  2469. * @property {ContactEquation} equationZ
  2470. */
  2471. var z = this.equationZ = new ContactEquation(bodyA,bodyB);
  2472. // Equations to be fed to the solver
  2473. this.equations.push(x, y, z);
  2474. // Make the equations bidirectional
  2475. x.minForce = y.minForce = z.minForce = -maxForce;
  2476. x.maxForce = y.maxForce = z.maxForce = maxForce;
  2477. x.ni.set(1, 0, 0);
  2478. y.ni.set(0, 1, 0);
  2479. z.ni.set(0, 0, 1);
  2480. }
  2481. PointToPointConstraint.prototype = new Constraint();
  2482. PointToPointConstraint.prototype.update = function(){
  2483. var bodyA = this.bodyA;
  2484. var bodyB = this.bodyB;
  2485. var x = this.equationX;
  2486. var y = this.equationY;
  2487. var z = this.equationZ;
  2488. // Rotate the pivots to world space
  2489. bodyA.quaternion.vmult(this.pivotA,x.ri);
  2490. bodyB.quaternion.vmult(this.pivotB,x.rj);
  2491. y.ri.copy(x.ri);
  2492. y.rj.copy(x.rj);
  2493. z.ri.copy(x.ri);
  2494. z.rj.copy(x.rj);
  2495. };
  2496. },{"../equations/ContactEquation":19,"../math/Vec3":30,"./Constraint":13}],18:[function(_dereq_,module,exports){
  2497. module.exports = ConeEquation;
  2498. var Vec3 = _dereq_('../math/Vec3');
  2499. var Mat3 = _dereq_('../math/Mat3');
  2500. var Equation = _dereq_('./Equation');
  2501. /**
  2502. * Cone equation. Works to keep the given body world vectors aligned, or tilted within a given angle from each other.
  2503. * @class ConeEquation
  2504. * @constructor
  2505. * @author schteppe
  2506. * @param {Body} bodyA
  2507. * @param {Body} bodyB
  2508. * @param {Vec3} [options.axisA] Local axis in A
  2509. * @param {Vec3} [options.axisB] Local axis in B
  2510. * @param {Vec3} [options.angle] The "cone angle" to keep
  2511. * @param {number} [options.maxForce=1e6]
  2512. * @extends Equation
  2513. */
  2514. function ConeEquation(bodyA, bodyB, options){
  2515. options = options || {};
  2516. var maxForce = typeof(options.maxForce) !== 'undefined' ? options.maxForce : 1e6;
  2517. Equation.call(this,bodyA,bodyB,-maxForce, maxForce);
  2518. this.axisA = options.axisA ? options.axisA.clone() : new Vec3(1, 0, 0);
  2519. this.axisB = options.axisB ? options.axisB.clone() : new Vec3(0, 1, 0);
  2520. /**
  2521. * The cone angle to keep
  2522. * @property {number} angle
  2523. */
  2524. this.angle = typeof(options.angle) !== 'undefined' ? options.angle : 0;
  2525. }
  2526. ConeEquation.prototype = new Equation();
  2527. ConeEquation.prototype.constructor = ConeEquation;
  2528. var tmpVec1 = new Vec3();
  2529. var tmpVec2 = new Vec3();
  2530. ConeEquation.prototype.computeB = function(h){
  2531. var a = this.a,
  2532. b = this.b,
  2533. ni = this.axisA,
  2534. nj = this.axisB,
  2535. nixnj = tmpVec1,
  2536. njxni = tmpVec2,
  2537. GA = this.jacobianElementA,
  2538. GB = this.jacobianElementB;
  2539. // Caluclate cross products
  2540. ni.cross(nj, nixnj);
  2541. nj.cross(ni, njxni);
  2542. // The angle between two vector is:
  2543. // cos(theta) = a * b / (length(a) * length(b) = { len(a) = len(b) = 1 } = a * b
  2544. // g = a * b
  2545. // gdot = (b x a) * wi + (a x b) * wj
  2546. // G = [0 bxa 0 axb]
  2547. // W = [vi wi vj wj]
  2548. GA.rotational.copy(njxni);
  2549. GB.rotational.copy(nixnj);
  2550. var g = Math.cos(this.angle) - ni.dot(nj),
  2551. GW = this.computeGW(),
  2552. GiMf = this.computeGiMf();
  2553. var B = - g * a - GW * b - h * GiMf;
  2554. return B;
  2555. };
  2556. },{"../math/Mat3":27,"../math/Vec3":30,"./Equation":20}],19:[function(_dereq_,module,exports){
  2557. module.exports = ContactEquation;
  2558. var Equation = _dereq_('./Equation');
  2559. var Vec3 = _dereq_('../math/Vec3');
  2560. var Mat3 = _dereq_('../math/Mat3');
  2561. /**
  2562. * Contact/non-penetration constraint equation
  2563. * @class ContactEquation
  2564. * @constructor
  2565. * @author schteppe
  2566. * @param {Body} bodyA
  2567. * @param {Body} bodyB
  2568. * @extends Equation
  2569. */
  2570. function ContactEquation(bodyA, bodyB, maxForce){
  2571. maxForce = typeof(maxForce) !== 'undefined' ? maxForce : 1e6;
  2572. Equation.call(this, bodyA, bodyB, 0, maxForce);
  2573. /**
  2574. * @property restitution
  2575. * @type {Number}
  2576. */
  2577. this.restitution = 0.0; // "bounciness": u1 = -e*u0
  2578. /**
  2579. * World-oriented vector that goes from the center of bi to the contact point.
  2580. * @property {Vec3} ri
  2581. */
  2582. this.ri = new Vec3();
  2583. /**
  2584. * World-oriented vector that starts in body j position and goes to the contact point.
  2585. * @property {Vec3} rj
  2586. */
  2587. this.rj = new Vec3();
  2588. /**
  2589. * Contact normal, pointing out of body i.
  2590. * @property {Vec3} ni
  2591. */
  2592. this.ni = new Vec3();
  2593. }
  2594. ContactEquation.prototype = new Equation();
  2595. ContactEquation.prototype.constructor = ContactEquation;
  2596. var ContactEquation_computeB_temp1 = new Vec3(); // Temp vectors
  2597. var ContactEquation_computeB_temp2 = new Vec3();
  2598. var ContactEquation_computeB_temp3 = new Vec3();
  2599. ContactEquation.prototype.computeB = function(h){
  2600. var a = this.a,
  2601. b = this.b,
  2602. bi = this.bi,
  2603. bj = this.bj,
  2604. ri = this.ri,
  2605. rj = this.rj,
  2606. rixn = ContactEquation_computeB_temp1,
  2607. rjxn = ContactEquation_computeB_temp2,
  2608. vi = bi.velocity,
  2609. wi = bi.angularVelocity,
  2610. fi = bi.force,
  2611. taui = bi.torque,
  2612. vj = bj.velocity,
  2613. wj = bj.angularVelocity,
  2614. fj = bj.force,
  2615. tauj = bj.torque,
  2616. penetrationVec = ContactEquation_computeB_temp3,
  2617. GA = this.jacobianElementA,
  2618. GB = this.jacobianElementB,
  2619. n = this.ni;
  2620. // Caluclate cross products
  2621. ri.cross(n,rixn);
  2622. rj.cross(n,rjxn);
  2623. // g = xj+rj -(xi+ri)
  2624. // G = [ -ni -rixn ni rjxn ]
  2625. n.negate(GA.spatial);
  2626. rixn.negate(GA.rotational);
  2627. GB.spatial.copy(n);
  2628. GB.rotational.copy(rjxn);
  2629. // Calculate the penetration vector
  2630. penetrationVec.copy(bj.position);
  2631. penetrationVec.vadd(rj,penetrationVec);
  2632. penetrationVec.vsub(bi.position,penetrationVec);
  2633. penetrationVec.vsub(ri,penetrationVec);
  2634. var g = n.dot(penetrationVec);
  2635. // Compute iteration
  2636. var ePlusOne = this.restitution + 1;
  2637. var GW = ePlusOne * vj.dot(n) - ePlusOne * vi.dot(n) + wj.dot(rjxn) - wi.dot(rixn);
  2638. var GiMf = this.computeGiMf();
  2639. var B = - g * a - GW * b - h*GiMf;
  2640. return B;
  2641. };
  2642. var ContactEquation_getImpactVelocityAlongNormal_vi = new Vec3();
  2643. var ContactEquation_getImpactVelocityAlongNormal_vj = new Vec3();
  2644. var ContactEquation_getImpactVelocityAlongNormal_xi = new Vec3();
  2645. var ContactEquation_getImpactVelocityAlongNormal_xj = new Vec3();
  2646. var ContactEquation_getImpactVelocityAlongNormal_relVel = new Vec3();
  2647. /**
  2648. * Get the current relative velocity in the contact point.
  2649. * @method getImpactVelocityAlongNormal
  2650. * @return {number}
  2651. */
  2652. ContactEquation.prototype.getImpactVelocityAlongNormal = function(){
  2653. var vi = ContactEquation_getImpactVelocityAlongNormal_vi;
  2654. var vj = ContactEquation_getImpactVelocityAlongNormal_vj;
  2655. var xi = ContactEquation_getImpactVelocityAlongNormal_xi;
  2656. var xj = ContactEquation_getImpactVelocityAlongNormal_xj;
  2657. var relVel = ContactEquation_getImpactVelocityAlongNormal_relVel;
  2658. this.bi.position.vadd(this.ri, xi);
  2659. this.bj.position.vadd(this.rj, xj);
  2660. this.bi.getVelocityAtWorldPoint(xi, vi);
  2661. this.bj.getVelocityAtWorldPoint(xj, vj);
  2662. vi.vsub(vj, relVel);
  2663. return this.ni.dot(relVel);
  2664. };
  2665. },{"../math/Mat3":27,"../math/Vec3":30,"./Equation":20}],20:[function(_dereq_,module,exports){
  2666. module.exports = Equation;
  2667. var JacobianElement = _dereq_('../math/JacobianElement'),
  2668. Vec3 = _dereq_('../math/Vec3');
  2669. /**
  2670. * Equation base class
  2671. * @class Equation
  2672. * @constructor
  2673. * @author schteppe
  2674. * @param {Body} bi
  2675. * @param {Body} bj
  2676. * @param {Number} minForce Minimum (read: negative max) force to be applied by the constraint.
  2677. * @param {Number} maxForce Maximum (read: positive max) force to be applied by the constraint.
  2678. */
  2679. function Equation(bi,bj,minForce,maxForce){
  2680. this.id = Equation.id++;
  2681. /**
  2682. * @property {number} minForce
  2683. */
  2684. this.minForce = typeof(minForce)==="undefined" ? -1e6 : minForce;
  2685. /**
  2686. * @property {number} maxForce
  2687. */
  2688. this.maxForce = typeof(maxForce)==="undefined" ? 1e6 : maxForce;
  2689. /**
  2690. * @property bi
  2691. * @type {Body}
  2692. */
  2693. this.bi = bi;
  2694. /**
  2695. * @property bj
  2696. * @type {Body}
  2697. */
  2698. this.bj = bj;
  2699. /**
  2700. * SPOOK parameter
  2701. * @property {number} a
  2702. */
  2703. this.a = 0.0;
  2704. /**
  2705. * SPOOK parameter
  2706. * @property {number} b
  2707. */
  2708. this.b = 0.0;
  2709. /**
  2710. * SPOOK parameter
  2711. * @property {number} eps
  2712. */
  2713. this.eps = 0.0;
  2714. /**
  2715. * @property {JacobianElement} jacobianElementA
  2716. */
  2717. this.jacobianElementA = new JacobianElement();
  2718. /**
  2719. * @property {JacobianElement} jacobianElementB
  2720. */
  2721. this.jacobianElementB = new JacobianElement();
  2722. /**
  2723. * @property {boolean} enabled
  2724. * @default true
  2725. */
  2726. this.enabled = true;
  2727. // Set typical spook params
  2728. this.setSpookParams(1e7,4,1/60);
  2729. }
  2730. Equation.prototype.constructor = Equation;
  2731. Equation.id = 0;
  2732. /**
  2733. * Recalculates a,b,eps.
  2734. * @method setSpookParams
  2735. */
  2736. Equation.prototype.setSpookParams = function(stiffness,relaxation,timeStep){
  2737. var d = relaxation,
  2738. k = stiffness,
  2739. h = timeStep;
  2740. this.a = 4.0 / (h * (1 + 4 * d));
  2741. this.b = (4.0 * d) / (1 + 4 * d);
  2742. this.eps = 4.0 / (h * h * k * (1 + 4 * d));
  2743. };
  2744. /**
  2745. * Computes the RHS of the SPOOK equation
  2746. * @method computeB
  2747. * @return {Number}
  2748. */
  2749. Equation.prototype.computeB = function(a,b,h){
  2750. var GW = this.computeGW(),
  2751. Gq = this.computeGq(),
  2752. GiMf = this.computeGiMf();
  2753. return - Gq * a - GW * b - GiMf*h;
  2754. };
  2755. /**
  2756. * Computes G*q, where q are the generalized body coordinates
  2757. * @method computeGq
  2758. * @return {Number}
  2759. */
  2760. Equation.prototype.computeGq = function(){
  2761. var GA = this.jacobianElementA,
  2762. GB = this.jacobianElementB,
  2763. bi = this.bi,
  2764. bj = this.bj,
  2765. xi = bi.position,
  2766. xj = bj.position;
  2767. return GA.spatial.dot(xi) + GB.spatial.dot(xj);
  2768. };
  2769. var zero = new Vec3();
  2770. /**
  2771. * Computes G*W, where W are the body velocities
  2772. * @method computeGW
  2773. * @return {Number}
  2774. */
  2775. Equation.prototype.computeGW = function(){
  2776. var GA = this.jacobianElementA,
  2777. GB = this.jacobianElementB,
  2778. bi = this.bi,
  2779. bj = this.bj,
  2780. vi = bi.velocity,
  2781. vj = bj.velocity,
  2782. wi = bi.angularVelocity || zero,
  2783. wj = bj.angularVelocity || zero;
  2784. return GA.multiplyVectors(vi,wi) + GB.multiplyVectors(vj,wj);
  2785. };
  2786. /**
  2787. * Computes G*Wlambda, where W are the body velocities
  2788. * @method computeGWlambda
  2789. * @return {Number}
  2790. */
  2791. Equation.prototype.computeGWlambda = function(){
  2792. var GA = this.jacobianElementA,
  2793. GB = this.jacobianElementB,
  2794. bi = this.bi,
  2795. bj = this.bj,
  2796. vi = bi.vlambda,
  2797. vj = bj.vlambda,
  2798. wi = bi.wlambda || zero,
  2799. wj = bj.wlambda || zero;
  2800. return GA.multiplyVectors(vi,wi) + GB.multiplyVectors(vj,wj);
  2801. };
  2802. /**
  2803. * Computes G*inv(M)*f, where M is the mass matrix with diagonal blocks for each body, and f are the forces on the bodies.
  2804. * @method computeGiMf
  2805. * @return {Number}
  2806. */
  2807. var iMfi = new Vec3(),
  2808. iMfj = new Vec3(),
  2809. invIi_vmult_taui = new Vec3(),
  2810. invIj_vmult_tauj = new Vec3();
  2811. Equation.prototype.computeGiMf = function(){
  2812. var GA = this.jacobianElementA,
  2813. GB = this.jacobianElementB,
  2814. bi = this.bi,
  2815. bj = this.bj,
  2816. fi = bi.force,
  2817. ti = bi.torque,
  2818. fj = bj.force,
  2819. tj = bj.torque,
  2820. invMassi = bi.invMassSolve,
  2821. invMassj = bj.invMassSolve;
  2822. if(bi.invInertiaWorldSolve){ bi.invInertiaWorldSolve.vmult(ti,invIi_vmult_taui); }
  2823. else { invIi_vmult_taui.set(0,0,0); }
  2824. if(bj.invInertiaWorldSolve){ bj.invInertiaWorldSolve.vmult(tj,invIj_vmult_tauj); }
  2825. else { invIj_vmult_tauj.set(0,0,0); }
  2826. fi.mult(invMassi,iMfi);
  2827. fj.mult(invMassj,iMfj);
  2828. return GA.multiplyVectors(iMfi,invIi_vmult_taui) + GB.multiplyVectors(iMfj,invIj_vmult_tauj);
  2829. };
  2830. /**
  2831. * Computes G*inv(M)*G'
  2832. * @method computeGiMGt
  2833. * @return {Number}
  2834. */
  2835. var tmp = new Vec3();
  2836. Equation.prototype.computeGiMGt = function(){
  2837. var GA = this.jacobianElementA,
  2838. GB = this.jacobianElementB,
  2839. bi = this.bi,
  2840. bj = this.bj,
  2841. invMassi = bi.invMassSolve,
  2842. invMassj = bj.invMassSolve,
  2843. invIi = bi.invInertiaWorldSolve,
  2844. invIj = bj.invInertiaWorldSolve,
  2845. result = invMassi + invMassj;
  2846. if(invIi){
  2847. invIi.vmult(GA.rotational,tmp);
  2848. result += tmp.dot(GA.rotational);
  2849. }
  2850. if(invIj){
  2851. invIj.vmult(GB.rotational,tmp);
  2852. result += tmp.dot(GB.rotational);
  2853. }
  2854. return result;
  2855. };
  2856. var addToWlambda_temp = new Vec3(),
  2857. addToWlambda_Gi = new Vec3(),
  2858. addToWlambda_Gj = new Vec3(),
  2859. addToWlambda_ri = new Vec3(),
  2860. addToWlambda_rj = new Vec3(),
  2861. addToWlambda_Mdiag = new Vec3();
  2862. /**
  2863. * Add constraint velocity to the bodies.
  2864. * @method addToWlambda
  2865. * @param {Number} deltalambda
  2866. */
  2867. Equation.prototype.addToWlambda = function(deltalambda){
  2868. var GA = this.jacobianElementA,
  2869. GB = this.jacobianElementB,
  2870. bi = this.bi,
  2871. bj = this.bj,
  2872. temp = addToWlambda_temp;
  2873. // Add to linear velocity
  2874. // v_lambda += inv(M) * delta_lamba * G
  2875. GA.spatial.mult(bi.invMassSolve * deltalambda,temp);
  2876. bi.vlambda.vadd(temp, bi.vlambda);
  2877. GB.spatial.mult(bj.invMassSolve * deltalambda,temp);
  2878. bj.vlambda.vadd(temp, bj.vlambda);
  2879. // Add to angular velocity
  2880. if(bi.invInertiaWorldSolve){
  2881. bi.invInertiaWorldSolve.vmult(GA.rotational,temp);
  2882. temp.mult(deltalambda,temp);
  2883. bi.wlambda.vadd(temp,bi.wlambda);
  2884. }
  2885. if(bj.invInertiaWorldSolve){
  2886. bj.invInertiaWorldSolve.vmult(GB.rotational,temp);
  2887. temp.mult(deltalambda,temp);
  2888. bj.wlambda.vadd(temp,bj.wlambda);
  2889. }
  2890. };
  2891. /**
  2892. * Compute the denominator part of the SPOOK equation: C = G*inv(M)*G' + eps
  2893. * @method computeInvC
  2894. * @param {Number} eps
  2895. * @return {Number}
  2896. */
  2897. Equation.prototype.computeC = function(){
  2898. return this.computeGiMGt() + this.eps;
  2899. };
  2900. },{"../math/JacobianElement":26,"../math/Vec3":30}],21:[function(_dereq_,module,exports){
  2901. module.exports = FrictionEquation;
  2902. var Equation = _dereq_('./Equation');
  2903. var Vec3 = _dereq_('../math/Vec3');
  2904. var Mat3 = _dereq_('../math/Mat3');
  2905. /**
  2906. * Constrains the slipping in a contact along a tangent
  2907. * @class FrictionEquation
  2908. * @constructor
  2909. * @author schteppe
  2910. * @param {Body} bodyA
  2911. * @param {Body} bodyB
  2912. * @param {Number} slipForce should be +-F_friction = +-mu * F_normal = +-mu * m * g
  2913. * @extends Equation
  2914. */
  2915. function FrictionEquation(bodyA, bodyB, slipForce){
  2916. Equation.call(this,bodyA, bodyB, -slipForce, slipForce);
  2917. this.ri = new Vec3();
  2918. this.rj = new Vec3();
  2919. this.t = new Vec3(); // tangent
  2920. }
  2921. FrictionEquation.prototype = new Equation();
  2922. FrictionEquation.prototype.constructor = FrictionEquation;
  2923. var FrictionEquation_computeB_temp1 = new Vec3();
  2924. var FrictionEquation_computeB_temp2 = new Vec3();
  2925. FrictionEquation.prototype.computeB = function(h){
  2926. var a = this.a,
  2927. b = this.b,
  2928. bi = this.bi,
  2929. bj = this.bj,
  2930. ri = this.ri,
  2931. rj = this.rj,
  2932. rixt = FrictionEquation_computeB_temp1,
  2933. rjxt = FrictionEquation_computeB_temp2,
  2934. t = this.t;
  2935. // Caluclate cross products
  2936. ri.cross(t,rixt);
  2937. rj.cross(t,rjxt);
  2938. // G = [-t -rixt t rjxt]
  2939. // And remember, this is a pure velocity constraint, g is always zero!
  2940. var GA = this.jacobianElementA,
  2941. GB = this.jacobianElementB;
  2942. t.negate(GA.spatial);
  2943. rixt.negate(GA.rotational);
  2944. GB.spatial.copy(t);
  2945. GB.rotational.copy(rjxt);
  2946. var GW = this.computeGW();
  2947. var GiMf = this.computeGiMf();
  2948. var B = - GW * b - h * GiMf;
  2949. return B;
  2950. };
  2951. },{"../math/Mat3":27,"../math/Vec3":30,"./Equation":20}],22:[function(_dereq_,module,exports){
  2952. module.exports = RotationalEquation;
  2953. var Vec3 = _dereq_('../math/Vec3');
  2954. var Mat3 = _dereq_('../math/Mat3');
  2955. var Equation = _dereq_('./Equation');
  2956. /**
  2957. * Rotational constraint. Works to keep the local vectors orthogonal to each other in world space.
  2958. * @class RotationalEquation
  2959. * @constructor
  2960. * @author schteppe
  2961. * @param {Body} bodyA
  2962. * @param {Body} bodyB
  2963. * @param {Vec3} [options.axisA]
  2964. * @param {Vec3} [options.axisB]
  2965. * @param {number} [options.maxForce]
  2966. * @extends Equation
  2967. */
  2968. function RotationalEquation(bodyA, bodyB, options){
  2969. options = options || {};
  2970. var maxForce = typeof(options.maxForce) !== 'undefined' ? options.maxForce : 1e6;
  2971. Equation.call(this,bodyA,bodyB,-maxForce, maxForce);
  2972. this.axisA = options.axisA ? options.axisA.clone() : new Vec3(1, 0, 0);
  2973. this.axisB = options.axisB ? options.axisB.clone() : new Vec3(0, 1, 0);
  2974. this.maxAngle = Math.PI / 2;
  2975. }
  2976. RotationalEquation.prototype = new Equation();
  2977. RotationalEquation.prototype.constructor = RotationalEquation;
  2978. var tmpVec1 = new Vec3();
  2979. var tmpVec2 = new Vec3();
  2980. RotationalEquation.prototype.computeB = function(h){
  2981. var a = this.a,
  2982. b = this.b,
  2983. ni = this.axisA,
  2984. nj = this.axisB,
  2985. nixnj = tmpVec1,
  2986. njxni = tmpVec2,
  2987. GA = this.jacobianElementA,
  2988. GB = this.jacobianElementB;
  2989. // Caluclate cross products
  2990. ni.cross(nj, nixnj);
  2991. nj.cross(ni, njxni);
  2992. // g = ni * nj
  2993. // gdot = (nj x ni) * wi + (ni x nj) * wj
  2994. // G = [0 njxni 0 nixnj]
  2995. // W = [vi wi vj wj]
  2996. GA.rotational.copy(njxni);
  2997. GB.rotational.copy(nixnj);
  2998. var g = Math.cos(this.maxAngle) - ni.dot(nj),
  2999. GW = this.computeGW(),
  3000. GiMf = this.computeGiMf();
  3001. var B = - g * a - GW * b - h * GiMf;
  3002. return B;
  3003. };
  3004. },{"../math/Mat3":27,"../math/Vec3":30,"./Equation":20}],23:[function(_dereq_,module,exports){
  3005. module.exports = RotationalMotorEquation;
  3006. var Vec3 = _dereq_('../math/Vec3');
  3007. var Mat3 = _dereq_('../math/Mat3');
  3008. var Equation = _dereq_('./Equation');
  3009. /**
  3010. * Rotational motor constraint. Tries to keep the relative angular velocity of the bodies to a given value.
  3011. * @class RotationalMotorEquation
  3012. * @constructor
  3013. * @author schteppe
  3014. * @param {Body} bodyA
  3015. * @param {Body} bodyB
  3016. * @param {Number} maxForce
  3017. * @extends Equation
  3018. */
  3019. function RotationalMotorEquation(bodyA, bodyB, maxForce){
  3020. maxForce = typeof(maxForce)!=='undefined' ? maxForce : 1e6;
  3021. Equation.call(this,bodyA,bodyB,-maxForce,maxForce);
  3022. /**
  3023. * World oriented rotational axis
  3024. * @property {Vec3} axisA
  3025. */
  3026. this.axisA = new Vec3();
  3027. /**
  3028. * World oriented rotational axis
  3029. * @property {Vec3} axisB
  3030. */
  3031. this.axisB = new Vec3(); // World oriented rotational axis
  3032. /**
  3033. * Motor velocity
  3034. * @property {Number} targetVelocity
  3035. */
  3036. this.targetVelocity = 0;
  3037. }
  3038. RotationalMotorEquation.prototype = new Equation();
  3039. RotationalMotorEquation.prototype.constructor = RotationalMotorEquation;
  3040. RotationalMotorEquation.prototype.computeB = function(h){
  3041. var a = this.a,
  3042. b = this.b,
  3043. bi = this.bi,
  3044. bj = this.bj,
  3045. axisA = this.axisA,
  3046. axisB = this.axisB,
  3047. GA = this.jacobianElementA,
  3048. GB = this.jacobianElementB;
  3049. // g = 0
  3050. // gdot = axisA * wi - axisB * wj
  3051. // gdot = G * W = G * [vi wi vj wj]
  3052. // =>
  3053. // G = [0 axisA 0 -axisB]
  3054. GA.rotational.copy(axisA);
  3055. axisB.negate(GB.rotational);
  3056. var GW = this.computeGW() - this.targetVelocity,
  3057. GiMf = this.computeGiMf();
  3058. var B = - GW * b - h * GiMf;
  3059. return B;
  3060. };
  3061. },{"../math/Mat3":27,"../math/Vec3":30,"./Equation":20}],24:[function(_dereq_,module,exports){
  3062. var Utils = _dereq_('../utils/Utils');
  3063. module.exports = ContactMaterial;
  3064. /**
  3065. * Defines what happens when two materials meet.
  3066. * @class ContactMaterial
  3067. * @constructor
  3068. * @param {Material} m1
  3069. * @param {Material} m2
  3070. * @param {object} [options]
  3071. * @param {Number} [options.friction=0.3]
  3072. * @param {Number} [options.restitution=0.3]
  3073. * @param {number} [options.contactEquationStiffness=1e7]
  3074. * @param {number} [options.contactEquationRelaxation=3]
  3075. * @param {number} [options.frictionEquationStiffness=1e7]
  3076. * @param {Number} [options.frictionEquationRelaxation=3]
  3077. */
  3078. function ContactMaterial(m1, m2, options){
  3079. options = Utils.defaults(options, {
  3080. friction: 0.3,
  3081. restitution: 0.3,
  3082. contactEquationStiffness: 1e7,
  3083. contactEquationRelaxation: 3,
  3084. frictionEquationStiffness: 1e7,
  3085. frictionEquationRelaxation: 3
  3086. });
  3087. /**
  3088. * Identifier of this material
  3089. * @property {Number} id
  3090. */
  3091. this.id = ContactMaterial.idCounter++;
  3092. /**
  3093. * Participating materials
  3094. * @property {Array} materials
  3095. * @todo Should be .materialA and .materialB instead
  3096. */
  3097. this.materials = [m1, m2];
  3098. /**
  3099. * Friction coefficient
  3100. * @property {Number} friction
  3101. */
  3102. this.friction = options.friction;
  3103. /**
  3104. * Restitution coefficient
  3105. * @property {Number} restitution
  3106. */
  3107. this.restitution = options.restitution;
  3108. /**
  3109. * Stiffness of the produced contact equations
  3110. * @property {Number} contactEquationStiffness
  3111. */
  3112. this.contactEquationStiffness = options.contactEquationStiffness;
  3113. /**
  3114. * Relaxation time of the produced contact equations
  3115. * @property {Number} contactEquationRelaxation
  3116. */
  3117. this.contactEquationRelaxation = options.contactEquationRelaxation;
  3118. /**
  3119. * Stiffness of the produced friction equations
  3120. * @property {Number} frictionEquationStiffness
  3121. */
  3122. this.frictionEquationStiffness = options.frictionEquationStiffness;
  3123. /**
  3124. * Relaxation time of the produced friction equations
  3125. * @property {Number} frictionEquationRelaxation
  3126. */
  3127. this.frictionEquationRelaxation = options.frictionEquationRelaxation;
  3128. }
  3129. ContactMaterial.idCounter = 0;
  3130. },{"../utils/Utils":53}],25:[function(_dereq_,module,exports){
  3131. module.exports = Material;
  3132. /**
  3133. * Defines a physics material.
  3134. * @class Material
  3135. * @constructor
  3136. * @param {object} [options]
  3137. * @author schteppe
  3138. */
  3139. function Material(options){
  3140. var name = '';
  3141. options = options || {};
  3142. // Backwards compatibility fix
  3143. if(typeof(options) === 'string'){
  3144. name = options;
  3145. options = {};
  3146. } else if(typeof(options) === 'object') {
  3147. name = '';
  3148. }
  3149. /**
  3150. * @property name
  3151. * @type {String}
  3152. */
  3153. this.name = name;
  3154. /**
  3155. * material id.
  3156. * @property id
  3157. * @type {number}
  3158. */
  3159. this.id = Material.idCounter++;
  3160. /**
  3161. * Friction for this material. If non-negative, it will be used instead of the friction given by ContactMaterials. If there's no matching ContactMaterial, the value from .defaultContactMaterial in the World will be used.
  3162. * @property {number} friction
  3163. */
  3164. this.friction = typeof(options.friction) !== 'undefined' ? options.friction : -1;
  3165. /**
  3166. * Restitution for this material. If non-negative, it will be used instead of the restitution given by ContactMaterials. If there's no matching ContactMaterial, the value from .defaultContactMaterial in the World will be used.
  3167. * @property {number} restitution
  3168. */
  3169. this.restitution = typeof(options.restitution) !== 'undefined' ? options.restitution : -1;
  3170. }
  3171. Material.idCounter = 0;
  3172. },{}],26:[function(_dereq_,module,exports){
  3173. module.exports = JacobianElement;
  3174. var Vec3 = _dereq_('./Vec3');
  3175. /**
  3176. * An element containing 6 entries, 3 spatial and 3 rotational degrees of freedom.
  3177. * @class JacobianElement
  3178. * @constructor
  3179. */
  3180. function JacobianElement(){
  3181. /**
  3182. * @property {Vec3} spatial
  3183. */
  3184. this.spatial = new Vec3();
  3185. /**
  3186. * @property {Vec3} rotational
  3187. */
  3188. this.rotational = new Vec3();
  3189. }
  3190. /**
  3191. * Multiply with other JacobianElement
  3192. * @method multiplyElement
  3193. * @param {JacobianElement} element
  3194. * @return {Number}
  3195. */
  3196. JacobianElement.prototype.multiplyElement = function(element){
  3197. return element.spatial.dot(this.spatial) + element.rotational.dot(this.rotational);
  3198. };
  3199. /**
  3200. * Multiply with two vectors
  3201. * @method multiplyVectors
  3202. * @param {Vec3} spatial
  3203. * @param {Vec3} rotational
  3204. * @return {Number}
  3205. */
  3206. JacobianElement.prototype.multiplyVectors = function(spatial,rotational){
  3207. return spatial.dot(this.spatial) + rotational.dot(this.rotational);
  3208. };
  3209. },{"./Vec3":30}],27:[function(_dereq_,module,exports){
  3210. module.exports = Mat3;
  3211. var Vec3 = _dereq_('./Vec3');
  3212. /**
  3213. * A 3x3 matrix.
  3214. * @class Mat3
  3215. * @constructor
  3216. * @param array elements Array of nine elements. Optional.
  3217. * @author schteppe / http://github.com/schteppe
  3218. */
  3219. function Mat3(elements){
  3220. /**
  3221. * A vector of length 9, containing all matrix elements
  3222. * @property {Array} elements
  3223. */
  3224. if(elements){
  3225. this.elements = elements;
  3226. } else {
  3227. this.elements = [0,0,0,0,0,0,0,0,0];
  3228. }
  3229. }
  3230. /**
  3231. * Sets the matrix to identity
  3232. * @method identity
  3233. * @todo Should perhaps be renamed to setIdentity() to be more clear.
  3234. * @todo Create another function that immediately creates an identity matrix eg. eye()
  3235. */
  3236. Mat3.prototype.identity = function(){
  3237. var e = this.elements;
  3238. e[0] = 1;
  3239. e[1] = 0;
  3240. e[2] = 0;
  3241. e[3] = 0;
  3242. e[4] = 1;
  3243. e[5] = 0;
  3244. e[6] = 0;
  3245. e[7] = 0;
  3246. e[8] = 1;
  3247. };
  3248. /**
  3249. * Set all elements to zero
  3250. * @method setZero
  3251. */
  3252. Mat3.prototype.setZero = function(){
  3253. var e = this.elements;
  3254. e[0] = 0;
  3255. e[1] = 0;
  3256. e[2] = 0;
  3257. e[3] = 0;
  3258. e[4] = 0;
  3259. e[5] = 0;
  3260. e[6] = 0;
  3261. e[7] = 0;
  3262. e[8] = 0;
  3263. };
  3264. /**
  3265. * Sets the matrix diagonal elements from a Vec3
  3266. * @method setTrace
  3267. * @param {Vec3} vec3
  3268. */
  3269. Mat3.prototype.setTrace = function(vec3){
  3270. var e = this.elements;
  3271. e[0] = vec3.x;
  3272. e[4] = vec3.y;
  3273. e[8] = vec3.z;
  3274. };
  3275. /**
  3276. * Gets the matrix diagonal elements
  3277. * @method getTrace
  3278. * @return {Vec3}
  3279. */
  3280. Mat3.prototype.getTrace = function(target){
  3281. var target = target || new Vec3();
  3282. var e = this.elements;
  3283. target.x = e[0];
  3284. target.y = e[4];
  3285. target.z = e[8];
  3286. };
  3287. /**
  3288. * Matrix-Vector multiplication
  3289. * @method vmult
  3290. * @param {Vec3} v The vector to multiply with
  3291. * @param {Vec3} target Optional, target to save the result in.
  3292. */
  3293. Mat3.prototype.vmult = function(v,target){
  3294. target = target || new Vec3();
  3295. var e = this.elements,
  3296. x = v.x,
  3297. y = v.y,
  3298. z = v.z;
  3299. target.x = e[0]*x + e[1]*y + e[2]*z;
  3300. target.y = e[3]*x + e[4]*y + e[5]*z;
  3301. target.z = e[6]*x + e[7]*y + e[8]*z;
  3302. return target;
  3303. };
  3304. /**
  3305. * Matrix-scalar multiplication
  3306. * @method smult
  3307. * @param {Number} s
  3308. */
  3309. Mat3.prototype.smult = function(s){
  3310. for(var i=0; i<this.elements.length; i++){
  3311. this.elements[i] *= s;
  3312. }
  3313. };
  3314. /**
  3315. * Matrix multiplication
  3316. * @method mmult
  3317. * @param {Mat3} m Matrix to multiply with from left side.
  3318. * @return {Mat3} The result.
  3319. */
  3320. Mat3.prototype.mmult = function(m,target){
  3321. var r = target || new Mat3();
  3322. for(var i=0; i<3; i++){
  3323. for(var j=0; j<3; j++){
  3324. var sum = 0.0;
  3325. for(var k=0; k<3; k++){
  3326. sum += m.elements[i+k*3] * this.elements[k+j*3];
  3327. }
  3328. r.elements[i+j*3] = sum;
  3329. }
  3330. }
  3331. return r;
  3332. };
  3333. /**
  3334. * Scale each column of the matrix
  3335. * @method scale
  3336. * @param {Vec3} v
  3337. * @return {Mat3} The result.
  3338. */
  3339. Mat3.prototype.scale = function(v,target){
  3340. target = target || new Mat3();
  3341. var e = this.elements,
  3342. t = target.elements;
  3343. for(var i=0; i!==3; i++){
  3344. t[3*i + 0] = v.x * e[3*i + 0];
  3345. t[3*i + 1] = v.y * e[3*i + 1];
  3346. t[3*i + 2] = v.z * e[3*i + 2];
  3347. }
  3348. return target;
  3349. };
  3350. /**
  3351. * Solve Ax=b
  3352. * @method solve
  3353. * @param {Vec3} b The right hand side
  3354. * @param {Vec3} target Optional. Target vector to save in.
  3355. * @return {Vec3} The solution x
  3356. * @todo should reuse arrays
  3357. */
  3358. Mat3.prototype.solve = function(b,target){
  3359. target = target || new Vec3();
  3360. // Construct equations
  3361. var nr = 3; // num rows
  3362. var nc = 4; // num cols
  3363. var eqns = [];
  3364. for(var i=0; i<nr*nc; i++){
  3365. eqns.push(0);
  3366. }
  3367. var i,j;
  3368. for(i=0; i<3; i++){
  3369. for(j=0; j<3; j++){
  3370. eqns[i+nc*j] = this.elements[i+3*j];
  3371. }
  3372. }
  3373. eqns[3+4*0] = b.x;
  3374. eqns[3+4*1] = b.y;
  3375. eqns[3+4*2] = b.z;
  3376. // Compute right upper triangular version of the matrix - Gauss elimination
  3377. var n = 3, k = n, np;
  3378. var kp = 4; // num rows
  3379. var p, els;
  3380. do {
  3381. i = k - n;
  3382. if (eqns[i+nc*i] === 0) {
  3383. // the pivot is null, swap lines
  3384. for (j = i + 1; j < k; j++) {
  3385. if (eqns[i+nc*j] !== 0) {
  3386. np = kp;
  3387. do { // do ligne( i ) = ligne( i ) + ligne( k )
  3388. p = kp - np;
  3389. eqns[p+nc*i] += eqns[p+nc*j];
  3390. } while (--np);
  3391. break;
  3392. }
  3393. }
  3394. }
  3395. if (eqns[i+nc*i] !== 0) {
  3396. for (j = i + 1; j < k; j++) {
  3397. var multiplier = eqns[i+nc*j] / eqns[i+nc*i];
  3398. np = kp;
  3399. do { // do ligne( k ) = ligne( k ) - multiplier * ligne( i )
  3400. p = kp - np;
  3401. eqns[p+nc*j] = p <= i ? 0 : eqns[p+nc*j] - eqns[p+nc*i] * multiplier ;
  3402. } while (--np);
  3403. }
  3404. }
  3405. } while (--n);
  3406. // Get the solution
  3407. target.z = eqns[2*nc+3] / eqns[2*nc+2];
  3408. target.y = (eqns[1*nc+3] - eqns[1*nc+2]*target.z) / eqns[1*nc+1];
  3409. target.x = (eqns[0*nc+3] - eqns[0*nc+2]*target.z - eqns[0*nc+1]*target.y) / eqns[0*nc+0];
  3410. if(isNaN(target.x) || isNaN(target.y) || isNaN(target.z) || target.x===Infinity || target.y===Infinity || target.z===Infinity){
  3411. throw "Could not solve equation! Got x=["+target.toString()+"], b=["+b.toString()+"], A=["+this.toString()+"]";
  3412. }
  3413. return target;
  3414. };
  3415. /**
  3416. * Get an element in the matrix by index. Index starts at 0, not 1!!!
  3417. * @method e
  3418. * @param {Number} row
  3419. * @param {Number} column
  3420. * @param {Number} value Optional. If provided, the matrix element will be set to this value.
  3421. * @return {Number}
  3422. */
  3423. Mat3.prototype.e = function( row , column ,value){
  3424. if(value===undefined){
  3425. return this.elements[column+3*row];
  3426. } else {
  3427. // Set value
  3428. this.elements[column+3*row] = value;
  3429. }
  3430. };
  3431. /**
  3432. * Copy another matrix into this matrix object.
  3433. * @method copy
  3434. * @param {Mat3} source
  3435. * @return {Mat3} this
  3436. */
  3437. Mat3.prototype.copy = function(source){
  3438. for(var i=0; i < source.elements.length; i++){
  3439. this.elements[i] = source.elements[i];
  3440. }
  3441. return this;
  3442. };
  3443. /**
  3444. * Returns a string representation of the matrix.
  3445. * @method toString
  3446. * @return string
  3447. */
  3448. Mat3.prototype.toString = function(){
  3449. var r = "";
  3450. var sep = ",";
  3451. for(var i=0; i<9; i++){
  3452. r += this.elements[i] + sep;
  3453. }
  3454. return r;
  3455. };
  3456. /**
  3457. * reverse the matrix
  3458. * @method reverse
  3459. * @param {Mat3} target Optional. Target matrix to save in.
  3460. * @return {Mat3} The solution x
  3461. */
  3462. Mat3.prototype.reverse = function(target){
  3463. target = target || new Mat3();
  3464. // Construct equations
  3465. var nr = 3; // num rows
  3466. var nc = 6; // num cols
  3467. var eqns = [];
  3468. for(var i=0; i<nr*nc; i++){
  3469. eqns.push(0);
  3470. }
  3471. var i,j;
  3472. for(i=0; i<3; i++){
  3473. for(j=0; j<3; j++){
  3474. eqns[i+nc*j] = this.elements[i+3*j];
  3475. }
  3476. }
  3477. eqns[3+6*0] = 1;
  3478. eqns[3+6*1] = 0;
  3479. eqns[3+6*2] = 0;
  3480. eqns[4+6*0] = 0;
  3481. eqns[4+6*1] = 1;
  3482. eqns[4+6*2] = 0;
  3483. eqns[5+6*0] = 0;
  3484. eqns[5+6*1] = 0;
  3485. eqns[5+6*2] = 1;
  3486. // Compute right upper triangular version of the matrix - Gauss elimination
  3487. var n = 3, k = n, np;
  3488. var kp = nc; // num rows
  3489. var p;
  3490. do {
  3491. i = k - n;
  3492. if (eqns[i+nc*i] === 0) {
  3493. // the pivot is null, swap lines
  3494. for (j = i + 1; j < k; j++) {
  3495. if (eqns[i+nc*j] !== 0) {
  3496. np = kp;
  3497. do { // do line( i ) = line( i ) + line( k )
  3498. p = kp - np;
  3499. eqns[p+nc*i] += eqns[p+nc*j];
  3500. } while (--np);
  3501. break;
  3502. }
  3503. }
  3504. }
  3505. if (eqns[i+nc*i] !== 0) {
  3506. for (j = i + 1; j < k; j++) {
  3507. var multiplier = eqns[i+nc*j] / eqns[i+nc*i];
  3508. np = kp;
  3509. do { // do line( k ) = line( k ) - multiplier * line( i )
  3510. p = kp - np;
  3511. eqns[p+nc*j] = p <= i ? 0 : eqns[p+nc*j] - eqns[p+nc*i] * multiplier ;
  3512. } while (--np);
  3513. }
  3514. }
  3515. } while (--n);
  3516. // eliminate the upper left triangle of the matrix
  3517. i = 2;
  3518. do {
  3519. j = i-1;
  3520. do {
  3521. var multiplier = eqns[i+nc*j] / eqns[i+nc*i];
  3522. np = nc;
  3523. do {
  3524. p = nc - np;
  3525. eqns[p+nc*j] = eqns[p+nc*j] - eqns[p+nc*i] * multiplier ;
  3526. } while (--np);
  3527. } while (j--);
  3528. } while (--i);
  3529. // operations on the diagonal
  3530. i = 2;
  3531. do {
  3532. var multiplier = 1 / eqns[i+nc*i];
  3533. np = nc;
  3534. do {
  3535. p = nc - np;
  3536. eqns[p+nc*i] = eqns[p+nc*i] * multiplier ;
  3537. } while (--np);
  3538. } while (i--);
  3539. i = 2;
  3540. do {
  3541. j = 2;
  3542. do {
  3543. p = eqns[nr+j+nc*i];
  3544. if( isNaN( p ) || p ===Infinity ){
  3545. throw "Could not reverse! A=["+this.toString()+"]";
  3546. }
  3547. target.e( i , j , p );
  3548. } while (j--);
  3549. } while (i--);
  3550. return target;
  3551. };
  3552. /**
  3553. * Set the matrix from a quaterion
  3554. * @method setRotationFromQuaternion
  3555. * @param {Quaternion} q
  3556. */
  3557. Mat3.prototype.setRotationFromQuaternion = function( q ) {
  3558. var x = q.x, y = q.y, z = q.z, w = q.w,
  3559. x2 = x + x, y2 = y + y, z2 = z + z,
  3560. xx = x * x2, xy = x * y2, xz = x * z2,
  3561. yy = y * y2, yz = y * z2, zz = z * z2,
  3562. wx = w * x2, wy = w * y2, wz = w * z2,
  3563. e = this.elements;
  3564. e[3*0 + 0] = 1 - ( yy + zz );
  3565. e[3*0 + 1] = xy - wz;
  3566. e[3*0 + 2] = xz + wy;
  3567. e[3*1 + 0] = xy + wz;
  3568. e[3*1 + 1] = 1 - ( xx + zz );
  3569. e[3*1 + 2] = yz - wx;
  3570. e[3*2 + 0] = xz - wy;
  3571. e[3*2 + 1] = yz + wx;
  3572. e[3*2 + 2] = 1 - ( xx + yy );
  3573. return this;
  3574. };
  3575. /**
  3576. * Transpose the matrix
  3577. * @method transpose
  3578. * @param {Mat3} target Where to store the result.
  3579. * @return {Mat3} The target Mat3, or a new Mat3 if target was omitted.
  3580. */
  3581. Mat3.prototype.transpose = function( target ) {
  3582. target = target || new Mat3();
  3583. var Mt = target.elements,
  3584. M = this.elements;
  3585. for(var i=0; i!==3; i++){
  3586. for(var j=0; j!==3; j++){
  3587. Mt[3*i + j] = M[3*j + i];
  3588. }
  3589. }
  3590. return target;
  3591. };
  3592. },{"./Vec3":30}],28:[function(_dereq_,module,exports){
  3593. module.exports = Quaternion;
  3594. var Vec3 = _dereq_('./Vec3');
  3595. /**
  3596. * A Quaternion describes a rotation in 3D space. The Quaternion is mathematically defined as Q = x*i + y*j + z*k + w, where (i,j,k) are imaginary basis vectors. (x,y,z) can be seen as a vector related to the axis of rotation, while the real multiplier, w, is related to the amount of rotation.
  3597. * @class Quaternion
  3598. * @constructor
  3599. * @param {Number} x Multiplier of the imaginary basis vector i.
  3600. * @param {Number} y Multiplier of the imaginary basis vector j.
  3601. * @param {Number} z Multiplier of the imaginary basis vector k.
  3602. * @param {Number} w Multiplier of the real part.
  3603. * @see http://en.wikipedia.org/wiki/Quaternion
  3604. */
  3605. function Quaternion(x,y,z,w){
  3606. /**
  3607. * @property {Number} x
  3608. */
  3609. this.x = x!==undefined ? x : 0;
  3610. /**
  3611. * @property {Number} y
  3612. */
  3613. this.y = y!==undefined ? y : 0;
  3614. /**
  3615. * @property {Number} z
  3616. */
  3617. this.z = z!==undefined ? z : 0;
  3618. /**
  3619. * The multiplier of the real quaternion basis vector.
  3620. * @property {Number} w
  3621. */
  3622. this.w = w!==undefined ? w : 1;
  3623. }
  3624. /**
  3625. * Set the value of the quaternion.
  3626. * @method set
  3627. * @param {Number} x
  3628. * @param {Number} y
  3629. * @param {Number} z
  3630. * @param {Number} w
  3631. */
  3632. Quaternion.prototype.set = function(x,y,z,w){
  3633. this.x = x;
  3634. this.y = y;
  3635. this.z = z;
  3636. this.w = w;
  3637. };
  3638. /**
  3639. * Convert to a readable format
  3640. * @method toString
  3641. * @return string
  3642. */
  3643. Quaternion.prototype.toString = function(){
  3644. return this.x+","+this.y+","+this.z+","+this.w;
  3645. };
  3646. /**
  3647. * Convert to an Array
  3648. * @method toArray
  3649. * @return Array
  3650. */
  3651. Quaternion.prototype.toArray = function(){
  3652. return [this.x, this.y, this.z, this.w];
  3653. };
  3654. /**
  3655. * Set the quaternion components given an axis and an angle.
  3656. * @method setFromAxisAngle
  3657. * @param {Vec3} axis
  3658. * @param {Number} angle in radians
  3659. */
  3660. Quaternion.prototype.setFromAxisAngle = function(axis,angle){
  3661. var s = Math.sin(angle*0.5);
  3662. this.x = axis.x * s;
  3663. this.y = axis.y * s;
  3664. this.z = axis.z * s;
  3665. this.w = Math.cos(angle*0.5);
  3666. };
  3667. /**
  3668. * Converts the quaternion to axis/angle representation.
  3669. * @method toAxisAngle
  3670. * @param {Vec3} targetAxis Optional. A vector object to reuse for storing the axis.
  3671. * @return Array An array, first elemnt is the axis and the second is the angle in radians.
  3672. */
  3673. Quaternion.prototype.toAxisAngle = function(targetAxis){
  3674. targetAxis = targetAxis || new Vec3();
  3675. this.normalize(); // if w>1 acos and sqrt will produce errors, this cant happen if quaternion is normalised
  3676. var angle = 2 * Math.acos(this.w);
  3677. var s = Math.sqrt(1-this.w*this.w); // assuming quaternion normalised then w is less than 1, so term always positive.
  3678. if (s < 0.001) { // test to avoid divide by zero, s is always positive due to sqrt
  3679. // if s close to zero then direction of axis not important
  3680. targetAxis.x = this.x; // if it is important that axis is normalised then replace with x=1; y=z=0;
  3681. targetAxis.y = this.y;
  3682. targetAxis.z = this.z;
  3683. } else {
  3684. targetAxis.x = this.x / s; // normalise axis
  3685. targetAxis.y = this.y / s;
  3686. targetAxis.z = this.z / s;
  3687. }
  3688. return [targetAxis,angle];
  3689. };
  3690. var sfv_t1 = new Vec3(),
  3691. sfv_t2 = new Vec3();
  3692. /**
  3693. * Set the quaternion value given two vectors. The resulting rotation will be the needed rotation to rotate u to v.
  3694. * @method setFromVectors
  3695. * @param {Vec3} u
  3696. * @param {Vec3} v
  3697. */
  3698. Quaternion.prototype.setFromVectors = function(u,v){
  3699. if(u.isAntiparallelTo(v)){
  3700. var t1 = sfv_t1;
  3701. var t2 = sfv_t2;
  3702. u.tangents(t1,t2);
  3703. this.setFromAxisAngle(t1,Math.PI);
  3704. } else {
  3705. var a = u.cross(v);
  3706. this.x = a.x;
  3707. this.y = a.y;
  3708. this.z = a.z;
  3709. this.w = Math.sqrt(Math.pow(u.norm(),2) * Math.pow(v.norm(),2)) + u.dot(v);
  3710. this.normalize();
  3711. }
  3712. };
  3713. /**
  3714. * Quaternion multiplication
  3715. * @method mult
  3716. * @param {Quaternion} q
  3717. * @param {Quaternion} target Optional.
  3718. * @return {Quaternion}
  3719. */
  3720. var Quaternion_mult_va = new Vec3();
  3721. var Quaternion_mult_vb = new Vec3();
  3722. var Quaternion_mult_vaxvb = new Vec3();
  3723. Quaternion.prototype.mult = function(q,target){
  3724. target = target || new Quaternion();
  3725. var w = this.w,
  3726. va = Quaternion_mult_va,
  3727. vb = Quaternion_mult_vb,
  3728. vaxvb = Quaternion_mult_vaxvb;
  3729. va.set(this.x,this.y,this.z);
  3730. vb.set(q.x,q.y,q.z);
  3731. target.w = w*q.w - va.dot(vb);
  3732. va.cross(vb,vaxvb);
  3733. target.x = w * vb.x + q.w*va.x + vaxvb.x;
  3734. target.y = w * vb.y + q.w*va.y + vaxvb.y;
  3735. target.z = w * vb.z + q.w*va.z + vaxvb.z;
  3736. return target;
  3737. };
  3738. /**
  3739. * Get the inverse quaternion rotation.
  3740. * @method inverse
  3741. * @param {Quaternion} target
  3742. * @return {Quaternion}
  3743. */
  3744. Quaternion.prototype.inverse = function(target){
  3745. var x = this.x, y = this.y, z = this.z, w = this.w;
  3746. target = target || new Quaternion();
  3747. this.conjugate(target);
  3748. var inorm2 = 1/(x*x + y*y + z*z + w*w);
  3749. target.x *= inorm2;
  3750. target.y *= inorm2;
  3751. target.z *= inorm2;
  3752. target.w *= inorm2;
  3753. return target;
  3754. };
  3755. /**
  3756. * Get the quaternion conjugate
  3757. * @method conjugate
  3758. * @param {Quaternion} target
  3759. * @return {Quaternion}
  3760. */
  3761. Quaternion.prototype.conjugate = function(target){
  3762. target = target || new Quaternion();
  3763. target.x = -this.x;
  3764. target.y = -this.y;
  3765. target.z = -this.z;
  3766. target.w = this.w;
  3767. return target;
  3768. };
  3769. /**
  3770. * Normalize the quaternion. Note that this changes the values of the quaternion.
  3771. * @method normalize
  3772. */
  3773. Quaternion.prototype.normalize = function(){
  3774. var l = Math.sqrt(this.x*this.x+this.y*this.y+this.z*this.z+this.w*this.w);
  3775. if ( l === 0 ) {
  3776. this.x = 0;
  3777. this.y = 0;
  3778. this.z = 0;
  3779. this.w = 0;
  3780. } else {
  3781. l = 1 / l;
  3782. this.x *= l;
  3783. this.y *= l;
  3784. this.z *= l;
  3785. this.w *= l;
  3786. }
  3787. };
  3788. /**
  3789. * Approximation of quaternion normalization. Works best when quat is already almost-normalized.
  3790. * @method normalizeFast
  3791. * @see http://jsperf.com/fast-quaternion-normalization
  3792. * @author unphased, https://github.com/unphased
  3793. */
  3794. Quaternion.prototype.normalizeFast = function () {
  3795. var f = (3.0-(this.x*this.x+this.y*this.y+this.z*this.z+this.w*this.w))/2.0;
  3796. if ( f === 0 ) {
  3797. this.x = 0;
  3798. this.y = 0;
  3799. this.z = 0;
  3800. this.w = 0;
  3801. } else {
  3802. this.x *= f;
  3803. this.y *= f;
  3804. this.z *= f;
  3805. this.w *= f;
  3806. }
  3807. };
  3808. /**
  3809. * Multiply the quaternion by a vector
  3810. * @method vmult
  3811. * @param {Vec3} v
  3812. * @param {Vec3} target Optional
  3813. * @return {Vec3}
  3814. */
  3815. Quaternion.prototype.vmult = function(v,target){
  3816. target = target || new Vec3();
  3817. var x = v.x,
  3818. y = v.y,
  3819. z = v.z;
  3820. var qx = this.x,
  3821. qy = this.y,
  3822. qz = this.z,
  3823. qw = this.w;
  3824. // q*v
  3825. var ix = qw * x + qy * z - qz * y,
  3826. iy = qw * y + qz * x - qx * z,
  3827. iz = qw * z + qx * y - qy * x,
  3828. iw = -qx * x - qy * y - qz * z;
  3829. target.x = ix * qw + iw * -qx + iy * -qz - iz * -qy;
  3830. target.y = iy * qw + iw * -qy + iz * -qx - ix * -qz;
  3831. target.z = iz * qw + iw * -qz + ix * -qy - iy * -qx;
  3832. return target;
  3833. };
  3834. /**
  3835. * Copies value of source to this quaternion.
  3836. * @method copy
  3837. * @param {Quaternion} source
  3838. * @return {Quaternion} this
  3839. */
  3840. Quaternion.prototype.copy = function(source){
  3841. this.x = source.x;
  3842. this.y = source.y;
  3843. this.z = source.z;
  3844. this.w = source.w;
  3845. return this;
  3846. };
  3847. /**
  3848. * Convert the quaternion to euler angle representation. Order: YZX, as this page describes: http://www.euclideanspace.com/maths/standards/index.htm
  3849. * @method toEuler
  3850. * @param {Vec3} target
  3851. * @param string order Three-character string e.g. "YZX", which also is default.
  3852. */
  3853. Quaternion.prototype.toEuler = function(target,order){
  3854. order = order || "YZX";
  3855. var heading, attitude, bank;
  3856. var x = this.x, y = this.y, z = this.z, w = this.w;
  3857. switch(order){
  3858. case "YZX":
  3859. var test = x*y + z*w;
  3860. if (test > 0.499) { // singularity at north pole
  3861. heading = 2 * Math.atan2(x,w);
  3862. attitude = Math.PI/2;
  3863. bank = 0;
  3864. }
  3865. if (test < -0.499) { // singularity at south pole
  3866. heading = -2 * Math.atan2(x,w);
  3867. attitude = - Math.PI/2;
  3868. bank = 0;
  3869. }
  3870. if(isNaN(heading)){
  3871. var sqx = x*x;
  3872. var sqy = y*y;
  3873. var sqz = z*z;
  3874. heading = Math.atan2(2*y*w - 2*x*z , 1 - 2*sqy - 2*sqz); // Heading
  3875. attitude = Math.asin(2*test); // attitude
  3876. bank = Math.atan2(2*x*w - 2*y*z , 1 - 2*sqx - 2*sqz); // bank
  3877. }
  3878. break;
  3879. default:
  3880. throw new Error("Euler order "+order+" not supported yet.");
  3881. }
  3882. target.y = heading;
  3883. target.z = attitude;
  3884. target.x = bank;
  3885. };
  3886. /**
  3887. * See http://www.mathworks.com/matlabcentral/fileexchange/20696-function-to-convert-between-dcm-euler-angles-quaternions-and-euler-vectors/content/SpinCalc.m
  3888. * @method setFromEuler
  3889. * @param {Number} x
  3890. * @param {Number} y
  3891. * @param {Number} z
  3892. * @param {String} order The order to apply angles: 'XYZ' or 'YXZ' or any other combination
  3893. */
  3894. Quaternion.prototype.setFromEuler = function ( x, y, z, order ) {
  3895. order = order || "XYZ";
  3896. var c1 = Math.cos( x / 2 );
  3897. var c2 = Math.cos( y / 2 );
  3898. var c3 = Math.cos( z / 2 );
  3899. var s1 = Math.sin( x / 2 );
  3900. var s2 = Math.sin( y / 2 );
  3901. var s3 = Math.sin( z / 2 );
  3902. if ( order === 'XYZ' ) {
  3903. this.x = s1 * c2 * c3 + c1 * s2 * s3;
  3904. this.y = c1 * s2 * c3 - s1 * c2 * s3;
  3905. this.z = c1 * c2 * s3 + s1 * s2 * c3;
  3906. this.w = c1 * c2 * c3 - s1 * s2 * s3;
  3907. } else if ( order === 'YXZ' ) {
  3908. this.x = s1 * c2 * c3 + c1 * s2 * s3;
  3909. this.y = c1 * s2 * c3 - s1 * c2 * s3;
  3910. this.z = c1 * c2 * s3 - s1 * s2 * c3;
  3911. this.w = c1 * c2 * c3 + s1 * s2 * s3;
  3912. } else if ( order === 'ZXY' ) {
  3913. this.x = s1 * c2 * c3 - c1 * s2 * s3;
  3914. this.y = c1 * s2 * c3 + s1 * c2 * s3;
  3915. this.z = c1 * c2 * s3 + s1 * s2 * c3;
  3916. this.w = c1 * c2 * c3 - s1 * s2 * s3;
  3917. } else if ( order === 'ZYX' ) {
  3918. this.x = s1 * c2 * c3 - c1 * s2 * s3;
  3919. this.y = c1 * s2 * c3 + s1 * c2 * s3;
  3920. this.z = c1 * c2 * s3 - s1 * s2 * c3;
  3921. this.w = c1 * c2 * c3 + s1 * s2 * s3;
  3922. } else if ( order === 'YZX' ) {
  3923. this.x = s1 * c2 * c3 + c1 * s2 * s3;
  3924. this.y = c1 * s2 * c3 + s1 * c2 * s3;
  3925. this.z = c1 * c2 * s3 - s1 * s2 * c3;
  3926. this.w = c1 * c2 * c3 - s1 * s2 * s3;
  3927. } else if ( order === 'XZY' ) {
  3928. this.x = s1 * c2 * c3 - c1 * s2 * s3;
  3929. this.y = c1 * s2 * c3 - s1 * c2 * s3;
  3930. this.z = c1 * c2 * s3 + s1 * s2 * c3;
  3931. this.w = c1 * c2 * c3 + s1 * s2 * s3;
  3932. }
  3933. return this;
  3934. };
  3935. Quaternion.prototype.clone = function(){
  3936. return new Quaternion(this.x, this.y, this.z, this.w);
  3937. };
  3938. },{"./Vec3":30}],29:[function(_dereq_,module,exports){
  3939. var Vec3 = _dereq_('./Vec3');
  3940. var Quaternion = _dereq_('./Quaternion');
  3941. module.exports = Transform;
  3942. /**
  3943. * @class Transform
  3944. * @constructor
  3945. */
  3946. function Transform(options) {
  3947. options = options || {};
  3948. /**
  3949. * @property {Vec3} position
  3950. */
  3951. this.position = new Vec3();
  3952. if(options.position){
  3953. this.position.copy(options.position);
  3954. }
  3955. /**
  3956. * @property {Quaternion} quaternion
  3957. */
  3958. this.quaternion = new Quaternion();
  3959. if(options.quaternion){
  3960. this.quaternion.copy(options.quaternion);
  3961. }
  3962. }
  3963. var tmpQuat = new Quaternion();
  3964. /**
  3965. * @static
  3966. * @method pointToLocaFrame
  3967. * @param {Vec3} position
  3968. * @param {Quaternion} quaternion
  3969. * @param {Vec3} worldPoint
  3970. * @param {Vec3} result
  3971. */
  3972. Transform.pointToLocalFrame = function(position, quaternion, worldPoint, result){
  3973. var result = result || new Vec3();
  3974. worldPoint.vsub(position, result);
  3975. quaternion.conjugate(tmpQuat);
  3976. tmpQuat.vmult(result, result);
  3977. return result;
  3978. };
  3979. /**
  3980. * Get a global point in local transform coordinates.
  3981. * @method pointToLocal
  3982. * @param {Vec3} point
  3983. * @param {Vec3} result
  3984. * @return {Vec3} The "result" vector object
  3985. */
  3986. Transform.prototype.pointToLocal = function(worldPoint, result){
  3987. return Transform.pointToLocalFrame(this.position, this.quaternion, worldPoint, result);
  3988. };
  3989. /**
  3990. * @static
  3991. * @method pointToWorldFrame
  3992. * @param {Vec3} position
  3993. * @param {Vec3} quaternion
  3994. * @param {Vec3} localPoint
  3995. * @param {Vec3} result
  3996. */
  3997. Transform.pointToWorldFrame = function(position, quaternion, localPoint, result){
  3998. var result = result || new Vec3();
  3999. quaternion.vmult(localPoint, result);
  4000. result.vadd(position, result);
  4001. return result;
  4002. };
  4003. /**
  4004. * Get a local point in global transform coordinates.
  4005. * @method pointToWorld
  4006. * @param {Vec3} point
  4007. * @param {Vec3} result
  4008. * @return {Vec3} The "result" vector object
  4009. */
  4010. Transform.prototype.pointToWorld = function(localPoint, result){
  4011. return Transform.pointToWorldFrame(this.position, this.quaternion, localPoint, result);
  4012. };
  4013. Transform.prototype.vectorToWorldFrame = function(localVector, result){
  4014. var result = result || new Vec3();
  4015. this.quaternion.vmult(localVector, result);
  4016. return result;
  4017. };
  4018. Transform.vectorToWorldFrame = function(quaternion, localVector, result){
  4019. quaternion.vmult(localVector, result);
  4020. return result;
  4021. };
  4022. Transform.vectorToLocalFrame = function(position, quaternion, worldVector, result){
  4023. var result = result || new Vec3();
  4024. quaternion.w *= -1;
  4025. quaternion.vmult(worldVector, result);
  4026. quaternion.w *= -1;
  4027. return result;
  4028. };
  4029. },{"./Quaternion":28,"./Vec3":30}],30:[function(_dereq_,module,exports){
  4030. module.exports = Vec3;
  4031. var Mat3 = _dereq_('./Mat3');
  4032. /**
  4033. * 3-dimensional vector
  4034. * @class Vec3
  4035. * @constructor
  4036. * @param {Number} x
  4037. * @param {Number} y
  4038. * @param {Number} z
  4039. * @author schteppe
  4040. * @example
  4041. * var v = new Vec3(1, 2, 3);
  4042. * console.log('x=' + v.x); // x=1
  4043. */
  4044. function Vec3(x,y,z){
  4045. /**
  4046. * @property x
  4047. * @type {Number}
  4048. */
  4049. this.x = x||0.0;
  4050. /**
  4051. * @property y
  4052. * @type {Number}
  4053. */
  4054. this.y = y||0.0;
  4055. /**
  4056. * @property z
  4057. * @type {Number}
  4058. */
  4059. this.z = z||0.0;
  4060. }
  4061. /**
  4062. * @static
  4063. * @property {Vec3} ZERO
  4064. */
  4065. Vec3.ZERO = new Vec3(0, 0, 0);
  4066. /**
  4067. * @static
  4068. * @property {Vec3} UNIT_X
  4069. */
  4070. Vec3.UNIT_X = new Vec3(1, 0, 0);
  4071. /**
  4072. * @static
  4073. * @property {Vec3} UNIT_Y
  4074. */
  4075. Vec3.UNIT_Y = new Vec3(0, 1, 0);
  4076. /**
  4077. * @static
  4078. * @property {Vec3} UNIT_Z
  4079. */
  4080. Vec3.UNIT_Z = new Vec3(0, 0, 1);
  4081. /**
  4082. * Vector cross product
  4083. * @method cross
  4084. * @param {Vec3} v
  4085. * @param {Vec3} target Optional. Target to save in.
  4086. * @return {Vec3}
  4087. */
  4088. Vec3.prototype.cross = function(v,target){
  4089. var vx=v.x, vy=v.y, vz=v.z, x=this.x, y=this.y, z=this.z;
  4090. target = target || new Vec3();
  4091. target.x = (y * vz) - (z * vy);
  4092. target.y = (z * vx) - (x * vz);
  4093. target.z = (x * vy) - (y * vx);
  4094. return target;
  4095. };
  4096. /**
  4097. * Set the vectors' 3 elements
  4098. * @method set
  4099. * @param {Number} x
  4100. * @param {Number} y
  4101. * @param {Number} z
  4102. * @return Vec3
  4103. */
  4104. Vec3.prototype.set = function(x,y,z){
  4105. this.x = x;
  4106. this.y = y;
  4107. this.z = z;
  4108. return this;
  4109. };
  4110. /**
  4111. * Set all components of the vector to zero.
  4112. * @method setZero
  4113. */
  4114. Vec3.prototype.setZero = function(){
  4115. this.x = this.y = this.z = 0;
  4116. };
  4117. /**
  4118. * Vector addition
  4119. * @method vadd
  4120. * @param {Vec3} v
  4121. * @param {Vec3} target Optional.
  4122. * @return {Vec3}
  4123. */
  4124. Vec3.prototype.vadd = function(v,target){
  4125. if(target){
  4126. target.x = v.x + this.x;
  4127. target.y = v.y + this.y;
  4128. target.z = v.z + this.z;
  4129. } else {
  4130. return new Vec3(this.x + v.x,
  4131. this.y + v.y,
  4132. this.z + v.z);
  4133. }
  4134. };
  4135. /**
  4136. * Vector subtraction
  4137. * @method vsub
  4138. * @param {Vec3} v
  4139. * @param {Vec3} target Optional. Target to save in.
  4140. * @return {Vec3}
  4141. */
  4142. Vec3.prototype.vsub = function(v,target){
  4143. if(target){
  4144. target.x = this.x - v.x;
  4145. target.y = this.y - v.y;
  4146. target.z = this.z - v.z;
  4147. } else {
  4148. return new Vec3(this.x-v.x,
  4149. this.y-v.y,
  4150. this.z-v.z);
  4151. }
  4152. };
  4153. /**
  4154. * Get the cross product matrix a_cross from a vector, such that a x b = a_cross * b = c
  4155. * @method crossmat
  4156. * @see http://www8.cs.umu.se/kurser/TDBD24/VT06/lectures/Lecture6.pdf
  4157. * @return {Mat3}
  4158. */
  4159. Vec3.prototype.crossmat = function(){
  4160. return new Mat3([ 0, -this.z, this.y,
  4161. this.z, 0, -this.x,
  4162. -this.y, this.x, 0]);
  4163. };
  4164. /**
  4165. * Normalize the vector. Note that this changes the values in the vector.
  4166. * @method normalize
  4167. * @return {Number} Returns the norm of the vector
  4168. */
  4169. Vec3.prototype.normalize = function(){
  4170. var x=this.x, y=this.y, z=this.z;
  4171. var n = Math.sqrt(x*x + y*y + z*z);
  4172. if(n>0.0){
  4173. var invN = 1/n;
  4174. this.x *= invN;
  4175. this.y *= invN;
  4176. this.z *= invN;
  4177. } else {
  4178. // Make something up
  4179. this.x = 0;
  4180. this.y = 0;
  4181. this.z = 0;
  4182. }
  4183. return n;
  4184. };
  4185. /**
  4186. * Get the version of this vector that is of length 1.
  4187. * @method unit
  4188. * @param {Vec3} target Optional target to save in
  4189. * @return {Vec3} Returns the unit vector
  4190. */
  4191. Vec3.prototype.unit = function(target){
  4192. target = target || new Vec3();
  4193. var x=this.x, y=this.y, z=this.z;
  4194. var ninv = Math.sqrt(x*x + y*y + z*z);
  4195. if(ninv>0.0){
  4196. ninv = 1.0/ninv;
  4197. target.x = x * ninv;
  4198. target.y = y * ninv;
  4199. target.z = z * ninv;
  4200. } else {
  4201. target.x = 1;
  4202. target.y = 0;
  4203. target.z = 0;
  4204. }
  4205. return target;
  4206. };
  4207. /**
  4208. * Get the length of the vector
  4209. * @method norm
  4210. * @return {Number}
  4211. * @deprecated Use .length() instead
  4212. */
  4213. Vec3.prototype.norm = function(){
  4214. var x=this.x, y=this.y, z=this.z;
  4215. return Math.sqrt(x*x + y*y + z*z);
  4216. };
  4217. /**
  4218. * Get the length of the vector
  4219. * @method length
  4220. * @return {Number}
  4221. */
  4222. Vec3.prototype.length = Vec3.prototype.norm;
  4223. /**
  4224. * Get the squared length of the vector
  4225. * @method norm2
  4226. * @return {Number}
  4227. * @deprecated Use .lengthSquared() instead.
  4228. */
  4229. Vec3.prototype.norm2 = function(){
  4230. return this.dot(this);
  4231. };
  4232. /**
  4233. * Get the squared length of the vector.
  4234. * @method lengthSquared
  4235. * @return {Number}
  4236. */
  4237. Vec3.prototype.lengthSquared = Vec3.prototype.norm2;
  4238. /**
  4239. * Get distance from this point to another point
  4240. * @method distanceTo
  4241. * @param {Vec3} p
  4242. * @return {Number}
  4243. */
  4244. Vec3.prototype.distanceTo = function(p){
  4245. var x=this.x, y=this.y, z=this.z;
  4246. var px=p.x, py=p.y, pz=p.z;
  4247. return Math.sqrt((px-x)*(px-x)+
  4248. (py-y)*(py-y)+
  4249. (pz-z)*(pz-z));
  4250. };
  4251. /**
  4252. * Get squared distance from this point to another point
  4253. * @method distanceSquared
  4254. * @param {Vec3} p
  4255. * @return {Number}
  4256. */
  4257. Vec3.prototype.distanceSquared = function(p){
  4258. var x=this.x, y=this.y, z=this.z;
  4259. var px=p.x, py=p.y, pz=p.z;
  4260. return (px-x)*(px-x) + (py-y)*(py-y) + (pz-z)*(pz-z);
  4261. };
  4262. /**
  4263. * Multiply all the components of the vector with a scalar.
  4264. * @deprecated Use .scale instead
  4265. * @method mult
  4266. * @param {Number} scalar
  4267. * @param {Vec3} target The vector to save the result in.
  4268. * @return {Vec3}
  4269. * @deprecated Use .scale() instead
  4270. */
  4271. Vec3.prototype.mult = function(scalar,target){
  4272. target = target || new Vec3();
  4273. var x = this.x,
  4274. y = this.y,
  4275. z = this.z;
  4276. target.x = scalar * x;
  4277. target.y = scalar * y;
  4278. target.z = scalar * z;
  4279. return target;
  4280. };
  4281. /**
  4282. * Multiply the vector with a scalar.
  4283. * @method scale
  4284. * @param {Number} scalar
  4285. * @param {Vec3} target
  4286. * @return {Vec3}
  4287. */
  4288. Vec3.prototype.scale = Vec3.prototype.mult;
  4289. /**
  4290. * Calculate dot product
  4291. * @method dot
  4292. * @param {Vec3} v
  4293. * @return {Number}
  4294. */
  4295. Vec3.prototype.dot = function(v){
  4296. return this.x * v.x + this.y * v.y + this.z * v.z;
  4297. };
  4298. /**
  4299. * @method isZero
  4300. * @return bool
  4301. */
  4302. Vec3.prototype.isZero = function(){
  4303. return this.x===0 && this.y===0 && this.z===0;
  4304. };
  4305. /**
  4306. * Make the vector point in the opposite direction.
  4307. * @method negate
  4308. * @param {Vec3} target Optional target to save in
  4309. * @return {Vec3}
  4310. */
  4311. Vec3.prototype.negate = function(target){
  4312. target = target || new Vec3();
  4313. target.x = -this.x;
  4314. target.y = -this.y;
  4315. target.z = -this.z;
  4316. return target;
  4317. };
  4318. /**
  4319. * Compute two artificial tangents to the vector
  4320. * @method tangents
  4321. * @param {Vec3} t1 Vector object to save the first tangent in
  4322. * @param {Vec3} t2 Vector object to save the second tangent in
  4323. */
  4324. var Vec3_tangents_n = new Vec3();
  4325. var Vec3_tangents_randVec = new Vec3();
  4326. Vec3.prototype.tangents = function(t1,t2){
  4327. var norm = this.norm();
  4328. if(norm>0.0){
  4329. var n = Vec3_tangents_n;
  4330. var inorm = 1/norm;
  4331. n.set(this.x*inorm,this.y*inorm,this.z*inorm);
  4332. var randVec = Vec3_tangents_randVec;
  4333. if(Math.abs(n.x) < 0.9){
  4334. randVec.set(1,0,0);
  4335. n.cross(randVec,t1);
  4336. } else {
  4337. randVec.set(0,1,0);
  4338. n.cross(randVec,t1);
  4339. }
  4340. n.cross(t1,t2);
  4341. } else {
  4342. // The normal length is zero, make something up
  4343. t1.set(1, 0, 0);
  4344. t2.set(0, 1, 0);
  4345. }
  4346. };
  4347. /**
  4348. * Converts to a more readable format
  4349. * @method toString
  4350. * @return string
  4351. */
  4352. Vec3.prototype.toString = function(){
  4353. return this.x+","+this.y+","+this.z;
  4354. };
  4355. /**
  4356. * Converts to an array
  4357. * @method toArray
  4358. * @return Array
  4359. */
  4360. Vec3.prototype.toArray = function(){
  4361. return [this.x, this.y, this.z];
  4362. };
  4363. /**
  4364. * Copies value of source to this vector.
  4365. * @method copy
  4366. * @param {Vec3} source
  4367. * @return {Vec3} this
  4368. */
  4369. Vec3.prototype.copy = function(source){
  4370. this.x = source.x;
  4371. this.y = source.y;
  4372. this.z = source.z;
  4373. return this;
  4374. };
  4375. /**
  4376. * Do a linear interpolation between two vectors
  4377. * @method lerp
  4378. * @param {Vec3} v
  4379. * @param {Number} t A number between 0 and 1. 0 will make this function return u, and 1 will make it return v. Numbers in between will generate a vector in between them.
  4380. * @param {Vec3} target
  4381. */
  4382. Vec3.prototype.lerp = function(v,t,target){
  4383. var x=this.x, y=this.y, z=this.z;
  4384. target.x = x + (v.x-x)*t;
  4385. target.y = y + (v.y-y)*t;
  4386. target.z = z + (v.z-z)*t;
  4387. };
  4388. /**
  4389. * Check if a vector equals is almost equal to another one.
  4390. * @method almostEquals
  4391. * @param {Vec3} v
  4392. * @param {Number} precision
  4393. * @return bool
  4394. */
  4395. Vec3.prototype.almostEquals = function(v,precision){
  4396. if(precision===undefined){
  4397. precision = 1e-6;
  4398. }
  4399. if( Math.abs(this.x-v.x)>precision ||
  4400. Math.abs(this.y-v.y)>precision ||
  4401. Math.abs(this.z-v.z)>precision){
  4402. return false;
  4403. }
  4404. return true;
  4405. };
  4406. /**
  4407. * Check if a vector is almost zero
  4408. * @method almostZero
  4409. * @param {Number} precision
  4410. */
  4411. Vec3.prototype.almostZero = function(precision){
  4412. if(precision===undefined){
  4413. precision = 1e-6;
  4414. }
  4415. if( Math.abs(this.x)>precision ||
  4416. Math.abs(this.y)>precision ||
  4417. Math.abs(this.z)>precision){
  4418. return false;
  4419. }
  4420. return true;
  4421. };
  4422. var antip_neg = new Vec3();
  4423. /**
  4424. * Check if the vector is anti-parallel to another vector.
  4425. * @method isAntiparallelTo
  4426. * @param {Vec3} v
  4427. * @param {Number} precision Set to zero for exact comparisons
  4428. * @return {Boolean}
  4429. */
  4430. Vec3.prototype.isAntiparallelTo = function(v,precision){
  4431. this.negate(antip_neg);
  4432. return antip_neg.almostEquals(v,precision);
  4433. };
  4434. /**
  4435. * Clone the vector
  4436. * @method clone
  4437. * @return {Vec3}
  4438. */
  4439. Vec3.prototype.clone = function(){
  4440. return new Vec3(this.x, this.y, this.z);
  4441. };
  4442. },{"./Mat3":27}],31:[function(_dereq_,module,exports){
  4443. module.exports = Body;
  4444. var EventTarget = _dereq_('../utils/EventTarget');
  4445. var Shape = _dereq_('../shapes/Shape');
  4446. var Vec3 = _dereq_('../math/Vec3');
  4447. var Mat3 = _dereq_('../math/Mat3');
  4448. var Quaternion = _dereq_('../math/Quaternion');
  4449. var Material = _dereq_('../material/Material');
  4450. var AABB = _dereq_('../collision/AABB');
  4451. var Box = _dereq_('../shapes/Box');
  4452. /**
  4453. * Base class for all body types.
  4454. * @class Body
  4455. * @constructor
  4456. * @extends EventTarget
  4457. * @param {object} [options]
  4458. * @param {Vec3} [options.position]
  4459. * @param {Vec3} [options.velocity]
  4460. * @param {Vec3} [options.angularVelocity]
  4461. * @param {Quaternion} [options.quaternion]
  4462. * @param {number} [options.mass]
  4463. * @param {Material} [options.material]
  4464. * @param {number} [options.type]
  4465. * @param {number} [options.linearDamping=0.01]
  4466. * @param {number} [options.angularDamping=0.01]
  4467. * @param {boolean} [options.allowSleep=true]
  4468. * @param {number} [options.sleepSpeedLimit=0.1]
  4469. * @param {number} [options.sleepTimeLimit=1]
  4470. * @param {number} [options.collisionFilterGroup=1]
  4471. * @param {number} [options.collisionFilterMask=1]
  4472. * @param {boolean} [options.fixedRotation=false]
  4473. * @param {Body} [options.shape]
  4474. * @example
  4475. * var body = new Body({
  4476. * mass: 1
  4477. * });
  4478. * var shape = new Sphere(1);
  4479. * body.addShape(shape);
  4480. * world.add(body);
  4481. */
  4482. function Body(options){
  4483. options = options || {};
  4484. EventTarget.apply(this);
  4485. this.id = Body.idCounter++;
  4486. /**
  4487. * Reference to the world the body is living in
  4488. * @property world
  4489. * @type {World}
  4490. */
  4491. this.world = null;
  4492. /**
  4493. * Callback function that is used BEFORE stepping the system. Use it to apply forces, for example. Inside the function, "this" will refer to this Body object.
  4494. * @property preStep
  4495. * @type {Function}
  4496. * @deprecated Use World events instead
  4497. */
  4498. this.preStep = null;
  4499. /**
  4500. * Callback function that is used AFTER stepping the system. Inside the function, "this" will refer to this Body object.
  4501. * @property postStep
  4502. * @type {Function}
  4503. * @deprecated Use World events instead
  4504. */
  4505. this.postStep = null;
  4506. this.vlambda = new Vec3();
  4507. /**
  4508. * @property {Number} collisionFilterGroup
  4509. */
  4510. this.collisionFilterGroup = typeof(options.collisionFilterGroup) === 'number' ? options.collisionFilterGroup : 1;
  4511. /**
  4512. * @property {Number} collisionFilterMask
  4513. */
  4514. this.collisionFilterMask = typeof(options.collisionFilterMask) === 'number' ? options.collisionFilterMask : 1;
  4515. /**
  4516. * Whether to produce contact forces when in contact with other bodies. Note that contacts will be generated, but they will be disabled.
  4517. * @property {Number} collisionResponse
  4518. */
  4519. this.collisionResponse = true;
  4520. /**
  4521. * @property position
  4522. * @type {Vec3}
  4523. */
  4524. this.position = new Vec3();
  4525. if(options.position){
  4526. this.position.copy(options.position);
  4527. }
  4528. /**
  4529. * @property {Vec3} previousPosition
  4530. */
  4531. this.previousPosition = new Vec3();
  4532. /**
  4533. * Initial position of the body
  4534. * @property initPosition
  4535. * @type {Vec3}
  4536. */
  4537. this.initPosition = new Vec3();
  4538. /**
  4539. * @property velocity
  4540. * @type {Vec3}
  4541. */
  4542. this.velocity = new Vec3();
  4543. if(options.velocity){
  4544. this.velocity.copy(options.velocity);
  4545. }
  4546. /**
  4547. * @property initVelocity
  4548. * @type {Vec3}
  4549. */
  4550. this.initVelocity = new Vec3();
  4551. /**
  4552. * Linear force on the body
  4553. * @property force
  4554. * @type {Vec3}
  4555. */
  4556. this.force = new Vec3();
  4557. var mass = typeof(options.mass) === 'number' ? options.mass : 0;
  4558. /**
  4559. * @property mass
  4560. * @type {Number}
  4561. * @default 0
  4562. */
  4563. this.mass = mass;
  4564. /**
  4565. * @property invMass
  4566. * @type {Number}
  4567. */
  4568. this.invMass = mass > 0 ? 1.0 / mass : 0;
  4569. /**
  4570. * @property material
  4571. * @type {Material}
  4572. */
  4573. this.material = options.material || null;
  4574. /**
  4575. * @property linearDamping
  4576. * @type {Number}
  4577. */
  4578. this.linearDamping = typeof(options.linearDamping) === 'number' ? options.linearDamping : 0.01;
  4579. /**
  4580. * One of: Body.DYNAMIC, Body.STATIC and Body.KINEMATIC.
  4581. * @property type
  4582. * @type {Number}
  4583. */
  4584. this.type = (mass <= 0.0 ? Body.STATIC : Body.DYNAMIC);
  4585. if(typeof(options.type) === typeof(Body.STATIC)){
  4586. this.type = options.type;
  4587. }
  4588. /**
  4589. * If true, the body will automatically fall to sleep.
  4590. * @property allowSleep
  4591. * @type {Boolean}
  4592. * @default true
  4593. */
  4594. this.allowSleep = typeof(options.allowSleep) !== 'undefined' ? options.allowSleep : true;
  4595. /**
  4596. * Current sleep state.
  4597. * @property sleepState
  4598. * @type {Number}
  4599. */
  4600. this.sleepState = 0;
  4601. /**
  4602. * If the speed (the norm of the velocity) is smaller than this value, the body is considered sleepy.
  4603. * @property sleepSpeedLimit
  4604. * @type {Number}
  4605. * @default 0.1
  4606. */
  4607. this.sleepSpeedLimit = typeof(options.sleepSpeedLimit) !== 'undefined' ? options.sleepSpeedLimit : 0.1;
  4608. /**
  4609. * If the body has been sleepy for this sleepTimeLimit seconds, it is considered sleeping.
  4610. * @property sleepTimeLimit
  4611. * @type {Number}
  4612. * @default 1
  4613. */
  4614. this.sleepTimeLimit = typeof(options.sleepTimeLimit) !== 'undefined' ? options.sleepTimeLimit : 1;
  4615. this.timeLastSleepy = 0;
  4616. this._wakeUpAfterNarrowphase = false;
  4617. /**
  4618. * Rotational force on the body, around center of mass
  4619. * @property {Vec3} torque
  4620. */
  4621. this.torque = new Vec3();
  4622. /**
  4623. * Orientation of the body
  4624. * @property quaternion
  4625. * @type {Quaternion}
  4626. */
  4627. this.quaternion = new Quaternion();
  4628. if(options.quaternion){
  4629. this.quaternion.copy(options.quaternion);
  4630. }
  4631. /**
  4632. * @property initQuaternion
  4633. * @type {Quaternion}
  4634. */
  4635. this.initQuaternion = new Quaternion();
  4636. /**
  4637. * @property angularVelocity
  4638. * @type {Vec3}
  4639. */
  4640. this.angularVelocity = new Vec3();
  4641. if(options.angularVelocity){
  4642. this.angularVelocity.copy(options.angularVelocity);
  4643. }
  4644. /**
  4645. * @property initAngularVelocity
  4646. * @type {Vec3}
  4647. */
  4648. this.initAngularVelocity = new Vec3();
  4649. this.interpolatedPosition = new Vec3();
  4650. this.interpolatedQuaternion = new Quaternion();
  4651. /**
  4652. * @property shapes
  4653. * @type {array}
  4654. */
  4655. this.shapes = [];
  4656. /**
  4657. * @property shapeOffsets
  4658. * @type {array}
  4659. */
  4660. this.shapeOffsets = [];
  4661. /**
  4662. * @property shapeOrientations
  4663. * @type {array}
  4664. */
  4665. this.shapeOrientations = [];
  4666. /**
  4667. * @property inertia
  4668. * @type {Vec3}
  4669. */
  4670. this.inertia = new Vec3();
  4671. /**
  4672. * @property {Vec3} invInertia
  4673. */
  4674. this.invInertia = new Vec3();
  4675. /**
  4676. * @property {Mat3} invInertiaWorld
  4677. */
  4678. this.invInertiaWorld = new Mat3();
  4679. this.invMassSolve = 0;
  4680. /**
  4681. * @property {Vec3} invInertiaSolve
  4682. */
  4683. this.invInertiaSolve = new Vec3();
  4684. /**
  4685. * @property {Mat3} invInertiaWorldSolve
  4686. */
  4687. this.invInertiaWorldSolve = new Mat3();
  4688. /**
  4689. * Set to true if you don't want the body to rotate. Make sure to run .updateMassProperties() after changing this.
  4690. * @property {Boolean} fixedRotation
  4691. * @default false
  4692. */
  4693. this.fixedRotation = typeof(options.fixedRotation) !== "undefined" ? options.fixedRotation : false;
  4694. /**
  4695. * @property {Number} angularDamping
  4696. */
  4697. this.angularDamping = typeof(options.angularDamping) !== 'undefined' ? options.angularDamping : 0.01;
  4698. /**
  4699. * @property aabb
  4700. * @type {AABB}
  4701. */
  4702. this.aabb = new AABB();
  4703. /**
  4704. * Indicates if the AABB needs to be updated before use.
  4705. * @property aabbNeedsUpdate
  4706. * @type {Boolean}
  4707. */
  4708. this.aabbNeedsUpdate = true;
  4709. this.wlambda = new Vec3();
  4710. if(options.shape){
  4711. this.addShape(options.shape);
  4712. }
  4713. this.updateMassProperties();
  4714. }
  4715. Body.prototype = new EventTarget();
  4716. Body.prototype.constructor = Body;
  4717. /**
  4718. * A dynamic body is fully simulated. Can be moved manually by the user, but normally they move according to forces. A dynamic body can collide with all body types. A dynamic body always has finite, non-zero mass.
  4719. * @static
  4720. * @property DYNAMIC
  4721. * @type {Number}
  4722. */
  4723. Body.DYNAMIC = 1;
  4724. /**
  4725. * A static body does not move during simulation and behaves as if it has infinite mass. Static bodies can be moved manually by setting the position of the body. The velocity of a static body is always zero. Static bodies do not collide with other static or kinematic bodies.
  4726. * @static
  4727. * @property STATIC
  4728. * @type {Number}
  4729. */
  4730. Body.STATIC = 2;
  4731. /**
  4732. * A kinematic body moves under simulation according to its velocity. They do not respond to forces. They can be moved manually, but normally a kinematic body is moved by setting its velocity. A kinematic body behaves as if it has infinite mass. Kinematic bodies do not collide with other static or kinematic bodies.
  4733. * @static
  4734. * @property KINEMATIC
  4735. * @type {Number}
  4736. */
  4737. Body.KINEMATIC = 4;
  4738. /**
  4739. * @static
  4740. * @property AWAKE
  4741. * @type {number}
  4742. */
  4743. Body.AWAKE = 0;
  4744. /**
  4745. * @static
  4746. * @property SLEEPY
  4747. * @type {number}
  4748. */
  4749. Body.SLEEPY = 1;
  4750. /**
  4751. * @static
  4752. * @property SLEEPING
  4753. * @type {number}
  4754. */
  4755. Body.SLEEPING = 2;
  4756. Body.idCounter = 0;
  4757. /**
  4758. * Wake the body up.
  4759. * @method wakeUp
  4760. */
  4761. Body.prototype.wakeUp = function(){
  4762. var s = this.sleepState;
  4763. this.sleepState = 0;
  4764. if(s === Body.SLEEPING){
  4765. this.dispatchEvent({type:"wakeup"});
  4766. }
  4767. };
  4768. /**
  4769. * Force body sleep
  4770. * @method sleep
  4771. */
  4772. Body.prototype.sleep = function(){
  4773. this.sleepState = Body.SLEEPING;
  4774. this.velocity.set(0,0,0);
  4775. this.angularVelocity.set(0,0,0);
  4776. };
  4777. Body.sleepyEvent = {
  4778. type: "sleepy"
  4779. };
  4780. Body.sleepEvent = {
  4781. type: "sleep"
  4782. };
  4783. /**
  4784. * Called every timestep to update internal sleep timer and change sleep state if needed.
  4785. * @method sleepTick
  4786. * @param {Number} time The world time in seconds
  4787. */
  4788. Body.prototype.sleepTick = function(time){
  4789. if(this.allowSleep){
  4790. var sleepState = this.sleepState;
  4791. var speedSquared = this.velocity.norm2() + this.angularVelocity.norm2();
  4792. var speedLimitSquared = Math.pow(this.sleepSpeedLimit,2);
  4793. if(sleepState===Body.AWAKE && speedSquared < speedLimitSquared){
  4794. this.sleepState = Body.SLEEPY; // Sleepy
  4795. this.timeLastSleepy = time;
  4796. this.dispatchEvent(Body.sleepyEvent);
  4797. } else if(sleepState===Body.SLEEPY && speedSquared > speedLimitSquared){
  4798. this.wakeUp(); // Wake up
  4799. } else if(sleepState===Body.SLEEPY && (time - this.timeLastSleepy ) > this.sleepTimeLimit){
  4800. this.sleep(); // Sleeping
  4801. this.dispatchEvent(Body.sleepEvent);
  4802. }
  4803. }
  4804. };
  4805. /**
  4806. * If the body is sleeping, it should be immovable / have infinite mass during solve. We solve it by having a separate "solve mass".
  4807. * @method updateSolveMassProperties
  4808. */
  4809. Body.prototype.updateSolveMassProperties = function(){
  4810. if(this.sleepState === Body.SLEEPING || this.type === Body.KINEMATIC){
  4811. this.invMassSolve = 0;
  4812. this.invInertiaSolve.setZero();
  4813. this.invInertiaWorldSolve.setZero();
  4814. } else {
  4815. this.invMassSolve = this.invMass;
  4816. this.invInertiaSolve.copy(this.invInertia);
  4817. this.invInertiaWorldSolve.copy(this.invInertiaWorld);
  4818. }
  4819. };
  4820. /**
  4821. * Convert a world point to local body frame.
  4822. * @method pointToLocalFrame
  4823. * @param {Vec3} worldPoint
  4824. * @param {Vec3} result
  4825. * @return {Vec3}
  4826. */
  4827. Body.prototype.pointToLocalFrame = function(worldPoint,result){
  4828. var result = result || new Vec3();
  4829. worldPoint.vsub(this.position,result);
  4830. this.quaternion.conjugate().vmult(result,result);
  4831. return result;
  4832. };
  4833. /**
  4834. * Convert a world vector to local body frame.
  4835. * @method vectorToLocalFrame
  4836. * @param {Vec3} worldPoint
  4837. * @param {Vec3} result
  4838. * @return {Vec3}
  4839. */
  4840. Body.prototype.vectorToLocalFrame = function(worldVector, result){
  4841. var result = result || new Vec3();
  4842. this.quaternion.conjugate().vmult(worldVector,result);
  4843. return result;
  4844. };
  4845. /**
  4846. * Convert a local body point to world frame.
  4847. * @method pointToWorldFrame
  4848. * @param {Vec3} localPoint
  4849. * @param {Vec3} result
  4850. * @return {Vec3}
  4851. */
  4852. Body.prototype.pointToWorldFrame = function(localPoint,result){
  4853. var result = result || new Vec3();
  4854. this.quaternion.vmult(localPoint,result);
  4855. result.vadd(this.position,result);
  4856. return result;
  4857. };
  4858. /**
  4859. * Convert a local body point to world frame.
  4860. * @method vectorToWorldFrame
  4861. * @param {Vec3} localVector
  4862. * @param {Vec3} result
  4863. * @return {Vec3}
  4864. */
  4865. Body.prototype.vectorToWorldFrame = function(localVector, result){
  4866. var result = result || new Vec3();
  4867. this.quaternion.vmult(localVector, result);
  4868. return result;
  4869. };
  4870. var tmpVec = new Vec3();
  4871. var tmpQuat = new Quaternion();
  4872. /**
  4873. * Add a shape to the body with a local offset and orientation.
  4874. * @method addShape
  4875. * @param {Shape} shape
  4876. * @param {Vec3} offset
  4877. * @param {Quaternion} quaternion
  4878. * @return {Body} The body object, for chainability.
  4879. */
  4880. Body.prototype.addShape = function(shape, _offset, _orientation){
  4881. var offset = new Vec3();
  4882. var orientation = new Quaternion();
  4883. if(_offset){
  4884. offset.copy(_offset);
  4885. }
  4886. if(_orientation){
  4887. orientation.copy(_orientation);
  4888. }
  4889. this.shapes.push(shape);
  4890. this.shapeOffsets.push(offset);
  4891. this.shapeOrientations.push(orientation);
  4892. this.updateMassProperties();
  4893. this.updateBoundingRadius();
  4894. this.aabbNeedsUpdate = true;
  4895. return this;
  4896. };
  4897. /**
  4898. * Update the bounding radius of the body. Should be done if any of the shapes are changed.
  4899. * @method updateBoundingRadius
  4900. */
  4901. Body.prototype.updateBoundingRadius = function(){
  4902. var shapes = this.shapes,
  4903. shapeOffsets = this.shapeOffsets,
  4904. N = shapes.length,
  4905. radius = 0;
  4906. for(var i=0; i!==N; i++){
  4907. var shape = shapes[i];
  4908. shape.updateBoundingSphereRadius();
  4909. var offset = shapeOffsets[i].norm(),
  4910. r = shape.boundingSphereRadius;
  4911. if(offset + r > radius){
  4912. radius = offset + r;
  4913. }
  4914. }
  4915. this.boundingRadius = radius;
  4916. };
  4917. var computeAABB_shapeAABB = new AABB();
  4918. /**
  4919. * Updates the .aabb
  4920. * @method computeAABB
  4921. * @todo rename to updateAABB()
  4922. */
  4923. Body.prototype.computeAABB = function(){
  4924. var shapes = this.shapes,
  4925. shapeOffsets = this.shapeOffsets,
  4926. shapeOrientations = this.shapeOrientations,
  4927. N = shapes.length,
  4928. offset = tmpVec,
  4929. orientation = tmpQuat,
  4930. bodyQuat = this.quaternion,
  4931. aabb = this.aabb,
  4932. shapeAABB = computeAABB_shapeAABB;
  4933. for(var i=0; i!==N; i++){
  4934. var shape = shapes[i];
  4935. // Get shape world quaternion
  4936. shapeOrientations[i].mult(bodyQuat, orientation);
  4937. // Get shape world position
  4938. orientation.vmult(shapeOffsets[i], offset);
  4939. offset.vadd(this.position, offset);
  4940. // vec2.rotate(offset, shapeOffsets[i], bodyAngle);
  4941. // vec2.add(offset, offset, this.position);
  4942. // Get shape AABB
  4943. shape.calculateWorldAABB(offset, orientation, shapeAABB.lowerBound, shapeAABB.upperBound);
  4944. if(i === 0){
  4945. aabb.copy(shapeAABB);
  4946. } else {
  4947. aabb.extend(shapeAABB);
  4948. }
  4949. }
  4950. this.aabbNeedsUpdate = false;
  4951. };
  4952. var uiw_m1 = new Mat3(),
  4953. uiw_m2 = new Mat3(),
  4954. uiw_m3 = new Mat3();
  4955. /**
  4956. * Update .inertiaWorld and .invInertiaWorld
  4957. * @method updateInertiaWorld
  4958. */
  4959. Body.prototype.updateInertiaWorld = function(force){
  4960. var I = this.invInertia;
  4961. if (I.x === I.y && I.y === I.z && !force) {
  4962. // If inertia M = s*I, where I is identity and s a scalar, then
  4963. // R*M*R' = R*(s*I)*R' = s*R*I*R' = s*R*R' = s*I = M
  4964. // where R is the rotation matrix.
  4965. // In other words, we don't have to transform the inertia if all
  4966. // inertia diagonal entries are equal.
  4967. } else {
  4968. var m1 = uiw_m1,
  4969. m2 = uiw_m2,
  4970. m3 = uiw_m3;
  4971. m1.setRotationFromQuaternion(this.quaternion);
  4972. m1.transpose(m2);
  4973. m1.scale(I,m1);
  4974. m1.mmult(m2,this.invInertiaWorld);
  4975. //m3.getTrace(this.invInertiaWorld);
  4976. }
  4977. /*
  4978. this.quaternion.vmult(this.inertia,this.inertiaWorld);
  4979. this.quaternion.vmult(this.invInertia,this.invInertiaWorld);
  4980. */
  4981. };
  4982. /**
  4983. * Apply force to a world point. This could for example be a point on the Body surface. Applying force this way will add to Body.force and Body.torque.
  4984. * @method applyForce
  4985. * @param {Vec3} force The amount of force to add.
  4986. * @param {Vec3} worldPoint A world point to apply the force on.
  4987. */
  4988. var Body_applyForce_r = new Vec3();
  4989. var Body_applyForce_rotForce = new Vec3();
  4990. Body.prototype.applyForce = function(force,worldPoint){
  4991. if(this.type !== Body.DYNAMIC){
  4992. return;
  4993. }
  4994. // Compute point position relative to the body center
  4995. var r = Body_applyForce_r;
  4996. worldPoint.vsub(this.position,r);
  4997. // Compute produced rotational force
  4998. var rotForce = Body_applyForce_rotForce;
  4999. r.cross(force,rotForce);
  5000. // Add linear force
  5001. this.force.vadd(force,this.force);
  5002. // Add rotational force
  5003. this.torque.vadd(rotForce,this.torque);
  5004. };
  5005. /**
  5006. * Apply force to a local point in the body.
  5007. * @method applyLocalForce
  5008. * @param {Vec3} force The force vector to apply, defined locally in the body frame.
  5009. * @param {Vec3} localPoint A local point in the body to apply the force on.
  5010. */
  5011. var Body_applyLocalForce_worldForce = new Vec3();
  5012. var Body_applyLocalForce_worldPoint = new Vec3();
  5013. Body.prototype.applyLocalForce = function(localForce, localPoint){
  5014. if(this.type !== Body.DYNAMIC){
  5015. return;
  5016. }
  5017. var worldForce = Body_applyLocalForce_worldForce;
  5018. var worldPoint = Body_applyLocalForce_worldPoint;
  5019. // Transform the force vector to world space
  5020. this.vectorToWorldFrame(localForce, worldForce);
  5021. this.pointToWorldFrame(localPoint, worldPoint);
  5022. this.applyForce(worldForce, worldPoint);
  5023. };
  5024. /**
  5025. * Apply impulse to a world point. This could for example be a point on the Body surface. An impulse is a force added to a body during a short period of time (impulse = force * time). Impulses will be added to Body.velocity and Body.angularVelocity.
  5026. * @method applyImpulse
  5027. * @param {Vec3} impulse The amount of impulse to add.
  5028. * @param {Vec3} worldPoint A world point to apply the force on.
  5029. */
  5030. var Body_applyImpulse_r = new Vec3();
  5031. var Body_applyImpulse_velo = new Vec3();
  5032. var Body_applyImpulse_rotVelo = new Vec3();
  5033. Body.prototype.applyImpulse = function(impulse, worldPoint){
  5034. if(this.type !== Body.DYNAMIC){
  5035. return;
  5036. }
  5037. // Compute point position relative to the body center
  5038. var r = Body_applyImpulse_r;
  5039. worldPoint.vsub(this.position,r);
  5040. // Compute produced central impulse velocity
  5041. var velo = Body_applyImpulse_velo;
  5042. velo.copy(impulse);
  5043. velo.mult(this.invMass,velo);
  5044. // Add linear impulse
  5045. this.velocity.vadd(velo, this.velocity);
  5046. // Compute produced rotational impulse velocity
  5047. var rotVelo = Body_applyImpulse_rotVelo;
  5048. r.cross(impulse,rotVelo);
  5049. /*
  5050. rotVelo.x *= this.invInertia.x;
  5051. rotVelo.y *= this.invInertia.y;
  5052. rotVelo.z *= this.invInertia.z;
  5053. */
  5054. this.invInertiaWorld.vmult(rotVelo,rotVelo);
  5055. // Add rotational Impulse
  5056. this.angularVelocity.vadd(rotVelo, this.angularVelocity);
  5057. };
  5058. /**
  5059. * Apply locally-defined impulse to a local point in the body.
  5060. * @method applyLocalImpulse
  5061. * @param {Vec3} force The force vector to apply, defined locally in the body frame.
  5062. * @param {Vec3} localPoint A local point in the body to apply the force on.
  5063. */
  5064. var Body_applyLocalImpulse_worldImpulse = new Vec3();
  5065. var Body_applyLocalImpulse_worldPoint = new Vec3();
  5066. Body.prototype.applyLocalImpulse = function(localImpulse, localPoint){
  5067. if(this.type !== Body.DYNAMIC){
  5068. return;
  5069. }
  5070. var worldImpulse = Body_applyLocalImpulse_worldImpulse;
  5071. var worldPoint = Body_applyLocalImpulse_worldPoint;
  5072. // Transform the force vector to world space
  5073. this.vectorToWorldFrame(localImpulse, worldImpulse);
  5074. this.pointToWorldFrame(localPoint, worldPoint);
  5075. this.applyImpulse(worldImpulse, worldPoint);
  5076. };
  5077. var Body_updateMassProperties_halfExtents = new Vec3();
  5078. /**
  5079. * Should be called whenever you change the body shape or mass.
  5080. * @method updateMassProperties
  5081. */
  5082. Body.prototype.updateMassProperties = function(){
  5083. var halfExtents = Body_updateMassProperties_halfExtents;
  5084. this.invMass = this.mass > 0 ? 1.0 / this.mass : 0;
  5085. var I = this.inertia;
  5086. var fixed = this.fixedRotation;
  5087. // Approximate with AABB box
  5088. this.computeAABB();
  5089. halfExtents.set(
  5090. (this.aabb.upperBound.x-this.aabb.lowerBound.x) / 2,
  5091. (this.aabb.upperBound.y-this.aabb.lowerBound.y) / 2,
  5092. (this.aabb.upperBound.z-this.aabb.lowerBound.z) / 2
  5093. );
  5094. Box.calculateInertia(halfExtents, this.mass, I);
  5095. this.invInertia.set(
  5096. I.x > 0 && !fixed ? 1.0 / I.x : 0,
  5097. I.y > 0 && !fixed ? 1.0 / I.y : 0,
  5098. I.z > 0 && !fixed ? 1.0 / I.z : 0
  5099. );
  5100. this.updateInertiaWorld(true);
  5101. };
  5102. /**
  5103. * Get world velocity of a point in the body.
  5104. * @method getVelocityAtWorldPoint
  5105. * @param {Vec3} worldPoint
  5106. * @param {Vec3} result
  5107. * @return {Vec3} The result vector.
  5108. */
  5109. Body.prototype.getVelocityAtWorldPoint = function(worldPoint, result){
  5110. var r = new Vec3();
  5111. worldPoint.vsub(this.position, r);
  5112. this.angularVelocity.cross(r, result);
  5113. this.velocity.vadd(result, result);
  5114. return result;
  5115. };
  5116. },{"../collision/AABB":3,"../material/Material":25,"../math/Mat3":27,"../math/Quaternion":28,"../math/Vec3":30,"../shapes/Box":37,"../shapes/Shape":43,"../utils/EventTarget":49}],32:[function(_dereq_,module,exports){
  5117. var Body = _dereq_('./Body');
  5118. var Vec3 = _dereq_('../math/Vec3');
  5119. var Quaternion = _dereq_('../math/Quaternion');
  5120. var RaycastResult = _dereq_('../collision/RaycastResult');
  5121. var Ray = _dereq_('../collision/Ray');
  5122. var WheelInfo = _dereq_('../objects/WheelInfo');
  5123. module.exports = RaycastVehicle;
  5124. /**
  5125. * Vehicle helper class that casts rays from the wheel positions towards the ground and applies forces.
  5126. * @class RaycastVehicle
  5127. * @constructor
  5128. * @param {object} [options]
  5129. * @param {Body} [options.chassisBody] The car chassis body.
  5130. * @param {integer} [options.indexRightAxis] Axis to use for right. x=0, y=1, z=2
  5131. * @param {integer} [options.indexLeftAxis]
  5132. * @param {integer} [options.indexUpAxis]
  5133. */
  5134. function RaycastVehicle(options){
  5135. /**
  5136. * @property {Body} chassisBody
  5137. */
  5138. this.chassisBody = options.chassisBody;
  5139. /**
  5140. * An array of WheelInfo objects.
  5141. * @property {array} wheelInfos
  5142. */
  5143. this.wheelInfos = [];
  5144. /**
  5145. * Will be set to true if the car is sliding.
  5146. * @property {boolean} sliding
  5147. */
  5148. this.sliding = false;
  5149. /**
  5150. * @property {World} world
  5151. */
  5152. this.world = null;
  5153. /**
  5154. * Index of the right axis, 0=x, 1=y, 2=z
  5155. * @property {integer} indexRightAxis
  5156. * @default 1
  5157. */
  5158. this.indexRightAxis = typeof(options.indexRightAxis) !== 'undefined' ? options.indexRightAxis : 1;
  5159. /**
  5160. * Index of the forward axis, 0=x, 1=y, 2=z
  5161. * @property {integer} indexForwardAxis
  5162. * @default 0
  5163. */
  5164. this.indexForwardAxis = typeof(options.indexForwardAxis) !== 'undefined' ? options.indexForwardAxis : 0;
  5165. /**
  5166. * Index of the up axis, 0=x, 1=y, 2=z
  5167. * @property {integer} indexUpAxis
  5168. * @default 2
  5169. */
  5170. this.indexUpAxis = typeof(options.indexUpAxis) !== 'undefined' ? options.indexUpAxis : 2;
  5171. }
  5172. var tmpVec1 = new Vec3();
  5173. var tmpVec2 = new Vec3();
  5174. var tmpVec3 = new Vec3();
  5175. var tmpVec4 = new Vec3();
  5176. var tmpVec5 = new Vec3();
  5177. var tmpVec6 = new Vec3();
  5178. var tmpRay = new Ray();
  5179. /**
  5180. * Add a wheel. For information about the options, see WheelInfo.
  5181. * @method addWheel
  5182. * @param {object} [options]
  5183. */
  5184. RaycastVehicle.prototype.addWheel = function(options){
  5185. options = options || {};
  5186. var info = new WheelInfo(options);
  5187. var index = this.wheelInfos.length;
  5188. this.wheelInfos.push(info);
  5189. return index;
  5190. };
  5191. /**
  5192. * Set the steering value of a wheel.
  5193. * @method setSteeringValue
  5194. * @param {number} value
  5195. * @param {integer} wheelIndex
  5196. */
  5197. RaycastVehicle.prototype.setSteeringValue = function(value, wheelIndex){
  5198. var wheel = this.wheelInfos[wheelIndex];
  5199. wheel.steering = value;
  5200. };
  5201. var torque = new Vec3();
  5202. /**
  5203. * Set the wheel force to apply on one of the wheels each time step
  5204. * @method applyEngineForce
  5205. * @param {number} value
  5206. * @param {integer} wheelIndex
  5207. */
  5208. RaycastVehicle.prototype.applyEngineForce = function(value, wheelIndex){
  5209. this.wheelInfos[wheelIndex].engineForce = value;
  5210. };
  5211. /**
  5212. * Set the braking force of a wheel
  5213. * @method setBrake
  5214. * @param {number} brake
  5215. * @param {integer} wheelIndex
  5216. */
  5217. RaycastVehicle.prototype.setBrake = function(brake, wheelIndex){
  5218. this.wheelInfos[wheelIndex].brake = brake;
  5219. };
  5220. /**
  5221. * Add the vehicle including its constraints to the world.
  5222. * @method addToWorld
  5223. * @param {World} world
  5224. */
  5225. RaycastVehicle.prototype.addToWorld = function(world){
  5226. var constraints = this.constraints;
  5227. world.add(this.chassisBody);
  5228. var that = this;
  5229. this.preStepCallback = function(){
  5230. that.updateVehicle(world.dt);
  5231. };
  5232. world.addEventListener('preStep', this.preStepCallback);
  5233. this.world = world;
  5234. };
  5235. /**
  5236. * Get one of the wheel axles, world-oriented.
  5237. * @private
  5238. * @method getVehicleAxisWorld
  5239. * @param {integer} axisIndex
  5240. * @param {Vec3} result
  5241. */
  5242. RaycastVehicle.prototype.getVehicleAxisWorld = function(axisIndex, result){
  5243. result.set(
  5244. axisIndex === 0 ? 1 : 0,
  5245. axisIndex === 1 ? 1 : 0,
  5246. axisIndex === 2 ? 1 : 0
  5247. );
  5248. this.chassisBody.vectorToWorldFrame(result, result);
  5249. };
  5250. RaycastVehicle.prototype.updateVehicle = function(timeStep){
  5251. var wheelInfos = this.wheelInfos;
  5252. var numWheels = wheelInfos.length;
  5253. var chassisBody = this.chassisBody;
  5254. for (var i = 0; i < numWheels; i++) {
  5255. this.updateWheelTransform(i);
  5256. }
  5257. this.currentVehicleSpeedKmHour = 3.6 * chassisBody.velocity.norm();
  5258. var forwardWorld = new Vec3();
  5259. this.getVehicleAxisWorld(this.indexForwardAxis, forwardWorld);
  5260. if (forwardWorld.dot(chassisBody.velocity) < 0){
  5261. this.currentVehicleSpeedKmHour *= -1;
  5262. }
  5263. // simulate suspension
  5264. for (var i = 0; i < numWheels; i++) {
  5265. this.castRay(wheelInfos[i]);
  5266. }
  5267. this.updateSuspension(timeStep);
  5268. var impulse = new Vec3();
  5269. var relpos = new Vec3();
  5270. for (var i = 0; i < numWheels; i++) {
  5271. //apply suspension force
  5272. var wheel = wheelInfos[i];
  5273. var suspensionForce = wheel.suspensionForce;
  5274. if (suspensionForce > wheel.maxSuspensionForce) {
  5275. suspensionForce = wheel.maxSuspensionForce;
  5276. }
  5277. wheel.raycastResult.hitNormalWorld.scale(suspensionForce * timeStep, impulse);
  5278. wheel.raycastResult.hitPointWorld.vsub(chassisBody.position, relpos);
  5279. chassisBody.applyImpulse(impulse, wheel.raycastResult.hitPointWorld/*relpos*/);
  5280. }
  5281. this.updateFriction(timeStep);
  5282. var hitNormalWorldScaledWithProj = new Vec3();
  5283. var fwd = new Vec3();
  5284. var vel = new Vec3();
  5285. for (i = 0; i < numWheels; i++) {
  5286. var wheel = wheelInfos[i];
  5287. //var relpos = new Vec3();
  5288. //wheel.chassisConnectionPointWorld.vsub(chassisBody.position, relpos);
  5289. chassisBody.getVelocityAtWorldPoint(wheel.chassisConnectionPointWorld, vel);
  5290. // Hack to get the rotation in the correct direction
  5291. var m = 1;
  5292. switch(this.indexUpAxis){
  5293. case 1:
  5294. m = -1;
  5295. break;
  5296. }
  5297. if (wheel.isInContact) {
  5298. this.getVehicleAxisWorld(this.indexForwardAxis, fwd);
  5299. var proj = fwd.dot(wheel.raycastResult.hitNormalWorld);
  5300. wheel.raycastResult.hitNormalWorld.scale(proj, hitNormalWorldScaledWithProj);
  5301. fwd.vsub(hitNormalWorldScaledWithProj, fwd);
  5302. var proj2 = fwd.dot(vel);
  5303. wheel.deltaRotation = m * proj2 * timeStep / wheel.radius;
  5304. }
  5305. if((wheel.sliding || !wheel.isInContact) && wheel.engineForce !== 0 && wheel.useCustomSlidingRotationalSpeed){
  5306. // Apply custom rotation when accelerating and sliding
  5307. wheel.deltaRotation = (wheel.engineForce > 0 ? 1 : -1) * wheel.customSlidingRotationalSpeed * timeStep;
  5308. }
  5309. // Lock wheels
  5310. if(Math.abs(wheel.brake) > Math.abs(wheel.engineForce)){
  5311. wheel.deltaRotation = 0;
  5312. }
  5313. wheel.rotation += wheel.deltaRotation; // Use the old value
  5314. wheel.deltaRotation *= 0.99; // damping of rotation when not in contact
  5315. }
  5316. };
  5317. RaycastVehicle.prototype.updateSuspension = function(deltaTime) {
  5318. var chassisBody = this.chassisBody;
  5319. var chassisMass = chassisBody.mass;
  5320. var wheelInfos = this.wheelInfos;
  5321. var numWheels = wheelInfos.length;
  5322. for (var w_it = 0; w_it < numWheels; w_it++){
  5323. var wheel = wheelInfos[w_it];
  5324. if (wheel.isInContact){
  5325. var force;
  5326. // Spring
  5327. var susp_length = wheel.suspensionRestLength;
  5328. var current_length = wheel.suspensionLength;
  5329. var length_diff = (susp_length - current_length);
  5330. force = wheel.suspensionStiffness * length_diff * wheel.clippedInvContactDotSuspension;
  5331. // Damper
  5332. var projected_rel_vel = wheel.suspensionRelativeVelocity;
  5333. var susp_damping;
  5334. if (projected_rel_vel < 0) {
  5335. susp_damping = wheel.dampingCompression;
  5336. } else {
  5337. susp_damping = wheel.dampingRelaxation;
  5338. }
  5339. force -= susp_damping * projected_rel_vel;
  5340. wheel.suspensionForce = force * chassisMass;
  5341. if (wheel.suspensionForce < 0) {
  5342. wheel.suspensionForce = 0;
  5343. }
  5344. } else {
  5345. wheel.suspensionForce = 0;
  5346. }
  5347. }
  5348. };
  5349. /**
  5350. * Remove the vehicle including its constraints from the world.
  5351. * @method removeFromWorld
  5352. * @param {World} world
  5353. */
  5354. RaycastVehicle.prototype.removeFromWorld = function(world){
  5355. var constraints = this.constraints;
  5356. world.remove(this.chassisBody);
  5357. world.removeEventListener('preStep', this.preStepCallback);
  5358. this.world = null;
  5359. };
  5360. var castRay_rayvector = new Vec3();
  5361. var castRay_target = new Vec3();
  5362. RaycastVehicle.prototype.castRay = function(wheel) {
  5363. var rayvector = castRay_rayvector;
  5364. var target = castRay_target;
  5365. this.updateWheelTransformWorld(wheel);
  5366. var chassisBody = this.chassisBody;
  5367. var depth = -1;
  5368. var raylen = wheel.suspensionRestLength + wheel.radius;
  5369. wheel.directionWorld.scale(raylen, rayvector);
  5370. var source = wheel.chassisConnectionPointWorld;
  5371. source.vadd(rayvector, target);
  5372. var raycastResult = wheel.raycastResult;
  5373. var param = 0;
  5374. raycastResult.reset();
  5375. // Turn off ray collision with the chassis temporarily
  5376. var oldState = chassisBody.collisionResponse;
  5377. chassisBody.collisionResponse = false;
  5378. // Cast ray against world
  5379. this.world.rayTest(source, target, raycastResult);
  5380. chassisBody.collisionResponse = oldState;
  5381. var object = raycastResult.body;
  5382. wheel.raycastResult.groundObject = 0;
  5383. if (object) {
  5384. depth = raycastResult.distance;
  5385. wheel.raycastResult.hitNormalWorld = raycastResult.hitNormalWorld;
  5386. wheel.isInContact = true;
  5387. var hitDistance = raycastResult.distance;
  5388. wheel.suspensionLength = hitDistance - wheel.radius;
  5389. // clamp on max suspension travel
  5390. var minSuspensionLength = wheel.suspensionRestLength - wheel.maxSuspensionTravel;
  5391. var maxSuspensionLength = wheel.suspensionRestLength + wheel.maxSuspensionTravel;
  5392. if (wheel.suspensionLength < minSuspensionLength) {
  5393. wheel.suspensionLength = minSuspensionLength;
  5394. }
  5395. if (wheel.suspensionLength > maxSuspensionLength) {
  5396. wheel.suspensionLength = maxSuspensionLength;
  5397. wheel.raycastResult.reset();
  5398. }
  5399. var denominator = wheel.raycastResult.hitNormalWorld.dot(wheel.directionWorld);
  5400. var chassis_velocity_at_contactPoint = new Vec3();
  5401. chassisBody.getVelocityAtWorldPoint(wheel.raycastResult.hitPointWorld, chassis_velocity_at_contactPoint);
  5402. var projVel = wheel.raycastResult.hitNormalWorld.dot( chassis_velocity_at_contactPoint );
  5403. if (denominator >= -0.1) {
  5404. wheel.suspensionRelativeVelocity = 0;
  5405. wheel.clippedInvContactDotSuspension = 1 / 0.1;
  5406. } else {
  5407. var inv = -1 / denominator;
  5408. wheel.suspensionRelativeVelocity = projVel * inv;
  5409. wheel.clippedInvContactDotSuspension = inv;
  5410. }
  5411. } else {
  5412. //put wheel info as in rest position
  5413. wheel.suspensionLength = wheel.suspensionRestLength + 0 * wheel.maxSuspensionTravel;
  5414. wheel.suspensionRelativeVelocity = 0.0;
  5415. wheel.directionWorld.scale(-1, wheel.raycastResult.hitNormalWorld);
  5416. wheel.clippedInvContactDotSuspension = 1.0;
  5417. }
  5418. return depth;
  5419. };
  5420. RaycastVehicle.prototype.updateWheelTransformWorld = function(wheel){
  5421. wheel.isInContact = false;
  5422. var chassisBody = this.chassisBody;
  5423. chassisBody.pointToWorldFrame(wheel.chassisConnectionPointLocal, wheel.chassisConnectionPointWorld);
  5424. chassisBody.vectorToWorldFrame(wheel.directionLocal, wheel.directionWorld);
  5425. chassisBody.vectorToWorldFrame(wheel.axleLocal, wheel.axleWorld);
  5426. };
  5427. /**
  5428. * Update one of the wheel transform.
  5429. * Note when rendering wheels: during each step, wheel transforms are updated BEFORE the chassis; ie. their position becomes invalid after the step. Thus when you render wheels, you must update wheel transforms before rendering them. See raycastVehicle demo for an example.
  5430. * @method updateWheelTransform
  5431. * @param {integer} wheelIndex The wheel index to update.
  5432. */
  5433. RaycastVehicle.prototype.updateWheelTransform = function(wheelIndex){
  5434. var up = tmpVec4;
  5435. var right = tmpVec5;
  5436. var fwd = tmpVec6;
  5437. var wheel = this.wheelInfos[wheelIndex];
  5438. this.updateWheelTransformWorld(wheel);
  5439. wheel.directionLocal.scale(-1, up);
  5440. right.copy(wheel.axleLocal);
  5441. up.cross(right, fwd);
  5442. fwd.normalize();
  5443. right.normalize();
  5444. // Rotate around steering over the wheelAxle
  5445. var steering = wheel.steering;
  5446. var steeringOrn = new Quaternion();
  5447. steeringOrn.setFromAxisAngle(up, steering);
  5448. var rotatingOrn = new Quaternion();
  5449. rotatingOrn.setFromAxisAngle(right, wheel.rotation);
  5450. // World rotation of the wheel
  5451. var q = wheel.worldTransform.quaternion;
  5452. this.chassisBody.quaternion.mult(steeringOrn, q);
  5453. q.mult(rotatingOrn, q);
  5454. q.normalize();
  5455. // world position of the wheel
  5456. var p = wheel.worldTransform.position;
  5457. p.copy(wheel.directionWorld);
  5458. p.scale(wheel.suspensionLength, p);
  5459. p.vadd(wheel.chassisConnectionPointWorld, p);
  5460. };
  5461. var directions = [
  5462. new Vec3(1, 0, 0),
  5463. new Vec3(0, 1, 0),
  5464. new Vec3(0, 0, 1)
  5465. ];
  5466. /**
  5467. * Get the world transform of one of the wheels
  5468. * @method getWheelTransformWorld
  5469. * @param {integer} wheelIndex
  5470. * @return {Transform}
  5471. */
  5472. RaycastVehicle.prototype.getWheelTransformWorld = function(wheelIndex) {
  5473. return this.wheelInfos[wheelIndex].worldTransform;
  5474. };
  5475. var updateFriction_surfNormalWS_scaled_proj = new Vec3();
  5476. var updateFriction_axle = [];
  5477. var updateFriction_forwardWS = [];
  5478. var sideFrictionStiffness2 = 1;
  5479. RaycastVehicle.prototype.updateFriction = function(timeStep) {
  5480. var surfNormalWS_scaled_proj = updateFriction_surfNormalWS_scaled_proj;
  5481. //calculate the impulse, so that the wheels don't move sidewards
  5482. var wheelInfos = this.wheelInfos;
  5483. var numWheels = wheelInfos.length;
  5484. var chassisBody = this.chassisBody;
  5485. var forwardWS = updateFriction_forwardWS;
  5486. var axle = updateFriction_axle;
  5487. var numWheelsOnGround = 0;
  5488. for (var i = 0; i < numWheels; i++) {
  5489. var wheel = wheelInfos[i];
  5490. var groundObject = wheel.raycastResult.body;
  5491. if (groundObject){
  5492. numWheelsOnGround++;
  5493. }
  5494. wheel.sideImpulse = 0;
  5495. wheel.forwardImpulse = 0;
  5496. if(!forwardWS[i]){
  5497. forwardWS[i] = new Vec3();
  5498. }
  5499. if(!axle[i]){
  5500. axle[i] = new Vec3();
  5501. }
  5502. }
  5503. for (var i = 0; i < numWheels; i++){
  5504. var wheel = wheelInfos[i];
  5505. var groundObject = wheel.raycastResult.body;
  5506. if (groundObject) {
  5507. var axlei = axle[i];
  5508. var wheelTrans = this.getWheelTransformWorld(i);
  5509. // Get world axle
  5510. wheelTrans.vectorToWorldFrame(directions[this.indexRightAxis], axlei);
  5511. var surfNormalWS = wheel.raycastResult.hitNormalWorld;
  5512. var proj = axlei.dot(surfNormalWS);
  5513. surfNormalWS.scale(proj, surfNormalWS_scaled_proj);
  5514. axlei.vsub(surfNormalWS_scaled_proj, axlei);
  5515. axlei.normalize();
  5516. surfNormalWS.cross(axlei, forwardWS[i]);
  5517. forwardWS[i].normalize();
  5518. wheel.sideImpulse = resolveSingleBilateral(
  5519. chassisBody,
  5520. wheel.raycastResult.hitPointWorld,
  5521. groundObject,
  5522. wheel.raycastResult.hitPointWorld,
  5523. axlei
  5524. );
  5525. wheel.sideImpulse *= sideFrictionStiffness2;
  5526. }
  5527. }
  5528. var sideFactor = 1;
  5529. var fwdFactor = 0.5;
  5530. this.sliding = false;
  5531. for (var i = 0; i < numWheels; i++) {
  5532. var wheel = wheelInfos[i];
  5533. var groundObject = wheel.raycastResult.body;
  5534. var rollingFriction = 0;
  5535. wheel.slipInfo = 1;
  5536. if (groundObject) {
  5537. var defaultRollingFrictionImpulse = 0;
  5538. var maxImpulse = wheel.brake ? wheel.brake : defaultRollingFrictionImpulse;
  5539. // btWheelContactPoint contactPt(chassisBody,groundObject,wheelInfraycastInfo.hitPointWorld,forwardWS[wheel],maxImpulse);
  5540. // rollingFriction = calcRollingFriction(contactPt);
  5541. rollingFriction = calcRollingFriction(chassisBody, groundObject, wheel.raycastResult.hitPointWorld, forwardWS[i], maxImpulse);
  5542. rollingFriction += wheel.engineForce * timeStep;
  5543. // rollingFriction = 0;
  5544. var factor = maxImpulse / rollingFriction;
  5545. wheel.slipInfo *= factor;
  5546. }
  5547. //switch between active rolling (throttle), braking and non-active rolling friction (nthrottle/break)
  5548. wheel.forwardImpulse = 0;
  5549. wheel.skidInfo = 1;
  5550. if (groundObject) {
  5551. wheel.skidInfo = 1;
  5552. var maximp = wheel.suspensionForce * timeStep * wheel.frictionSlip;
  5553. var maximpSide = maximp;
  5554. var maximpSquared = maximp * maximpSide;
  5555. wheel.forwardImpulse = rollingFriction;//wheelInfo.engineForce* timeStep;
  5556. var x = wheel.forwardImpulse * fwdFactor;
  5557. var y = wheel.sideImpulse * sideFactor;
  5558. var impulseSquared = x * x + y * y;
  5559. wheel.sliding = false;
  5560. if (impulseSquared > maximpSquared) {
  5561. this.sliding = true;
  5562. wheel.sliding = true;
  5563. var factor = maximp / Math.sqrt(impulseSquared);
  5564. wheel.skidInfo *= factor;
  5565. }
  5566. }
  5567. }
  5568. if (this.sliding) {
  5569. for (var i = 0; i < numWheels; i++) {
  5570. var wheel = wheelInfos[i];
  5571. if (wheel.sideImpulse !== 0) {
  5572. if (wheel.skidInfo < 1){
  5573. wheel.forwardImpulse *= wheel.skidInfo;
  5574. wheel.sideImpulse *= wheel.skidInfo;
  5575. }
  5576. }
  5577. }
  5578. }
  5579. // apply the impulses
  5580. for (var i = 0; i < numWheels; i++) {
  5581. var wheel = wheelInfos[i];
  5582. var rel_pos = new Vec3();
  5583. //wheel.raycastResult.hitPointWorld.vsub(chassisBody.position, rel_pos);
  5584. // cannons applyimpulse is using world coord for the position
  5585. rel_pos.copy(wheel.raycastResult.hitPointWorld);
  5586. if (wheel.forwardImpulse !== 0) {
  5587. var impulse = new Vec3();
  5588. forwardWS[i].scale(wheel.forwardImpulse, impulse);
  5589. chassisBody.applyImpulse(impulse, rel_pos);
  5590. }
  5591. if (wheel.sideImpulse !== 0){
  5592. var groundObject = wheel.raycastResult.body;
  5593. var rel_pos2 = new Vec3();
  5594. //wheel.raycastResult.hitPointWorld.vsub(groundObject.position, rel_pos2);
  5595. rel_pos2.copy(wheel.raycastResult.hitPointWorld);
  5596. var sideImp = new Vec3();
  5597. axle[i].scale(wheel.sideImpulse, sideImp);
  5598. // Scale the relative position in the up direction with rollInfluence.
  5599. // If rollInfluence is 1, the impulse will be applied on the hitPoint (easy to roll over), if it is zero it will be applied in the same plane as the center of mass (not easy to roll over).
  5600. chassisBody.pointToLocalFrame(rel_pos, rel_pos);
  5601. rel_pos['xyz'[this.indexUpAxis]] *= wheel.rollInfluence;
  5602. chassisBody.pointToWorldFrame(rel_pos, rel_pos);
  5603. chassisBody.applyImpulse(sideImp, rel_pos);
  5604. //apply friction impulse on the ground
  5605. sideImp.scale(-1, sideImp);
  5606. groundObject.applyImpulse(sideImp, rel_pos2);
  5607. }
  5608. }
  5609. };
  5610. var calcRollingFriction_vel1 = new Vec3();
  5611. var calcRollingFriction_vel2 = new Vec3();
  5612. var calcRollingFriction_vel = new Vec3();
  5613. function calcRollingFriction(body0, body1, frictionPosWorld, frictionDirectionWorld, maxImpulse) {
  5614. var j1 = 0;
  5615. var contactPosWorld = frictionPosWorld;
  5616. // var rel_pos1 = new Vec3();
  5617. // var rel_pos2 = new Vec3();
  5618. var vel1 = calcRollingFriction_vel1;
  5619. var vel2 = calcRollingFriction_vel2;
  5620. var vel = calcRollingFriction_vel;
  5621. // contactPosWorld.vsub(body0.position, rel_pos1);
  5622. // contactPosWorld.vsub(body1.position, rel_pos2);
  5623. body0.getVelocityAtWorldPoint(contactPosWorld, vel1);
  5624. body1.getVelocityAtWorldPoint(contactPosWorld, vel2);
  5625. vel1.vsub(vel2, vel);
  5626. var vrel = frictionDirectionWorld.dot(vel);
  5627. var denom0 = computeImpulseDenominator(body0, frictionPosWorld, frictionDirectionWorld);
  5628. var denom1 = computeImpulseDenominator(body1, frictionPosWorld, frictionDirectionWorld);
  5629. var relaxation = 1;
  5630. var jacDiagABInv = relaxation / (denom0 + denom1);
  5631. // calculate j that moves us to zero relative velocity
  5632. j1 = -vrel * jacDiagABInv;
  5633. if (maxImpulse < j1) {
  5634. j1 = maxImpulse;
  5635. }
  5636. if (j1 < -maxImpulse) {
  5637. j1 = -maxImpulse;
  5638. }
  5639. return j1;
  5640. }
  5641. var computeImpulseDenominator_r0 = new Vec3();
  5642. var computeImpulseDenominator_c0 = new Vec3();
  5643. var computeImpulseDenominator_vec = new Vec3();
  5644. var computeImpulseDenominator_m = new Vec3();
  5645. function computeImpulseDenominator(body, pos, normal) {
  5646. var r0 = computeImpulseDenominator_r0;
  5647. var c0 = computeImpulseDenominator_c0;
  5648. var vec = computeImpulseDenominator_vec;
  5649. var m = computeImpulseDenominator_m;
  5650. pos.vsub(body.position, r0);
  5651. r0.cross(normal, c0);
  5652. body.invInertiaWorld.vmult(c0, m);
  5653. m.cross(r0, vec);
  5654. return body.invMass + normal.dot(vec);
  5655. }
  5656. var resolveSingleBilateral_vel1 = new Vec3();
  5657. var resolveSingleBilateral_vel2 = new Vec3();
  5658. var resolveSingleBilateral_vel = new Vec3();
  5659. //bilateral constraint between two dynamic objects
  5660. function resolveSingleBilateral(body1, pos1, body2, pos2, normal, impulse){
  5661. var normalLenSqr = normal.norm2();
  5662. if (normalLenSqr > 1.1){
  5663. return 0; // no impulse
  5664. }
  5665. // var rel_pos1 = new Vec3();
  5666. // var rel_pos2 = new Vec3();
  5667. // pos1.vsub(body1.position, rel_pos1);
  5668. // pos2.vsub(body2.position, rel_pos2);
  5669. var vel1 = resolveSingleBilateral_vel1;
  5670. var vel2 = resolveSingleBilateral_vel2;
  5671. var vel = resolveSingleBilateral_vel;
  5672. body1.getVelocityAtWorldPoint(pos1, vel1);
  5673. body2.getVelocityAtWorldPoint(pos2, vel2);
  5674. vel1.vsub(vel2, vel);
  5675. var rel_vel = normal.dot(vel);
  5676. var contactDamping = 0.2;
  5677. var massTerm = 1 / (body1.invMass + body2.invMass);
  5678. var impulse = - contactDamping * rel_vel * massTerm;
  5679. return impulse;
  5680. }
  5681. },{"../collision/Ray":9,"../collision/RaycastResult":10,"../math/Quaternion":28,"../math/Vec3":30,"../objects/WheelInfo":36,"./Body":31}],33:[function(_dereq_,module,exports){
  5682. var Body = _dereq_('./Body');
  5683. var Sphere = _dereq_('../shapes/Sphere');
  5684. var Box = _dereq_('../shapes/Box');
  5685. var Vec3 = _dereq_('../math/Vec3');
  5686. var HingeConstraint = _dereq_('../constraints/HingeConstraint');
  5687. module.exports = RigidVehicle;
  5688. /**
  5689. * Simple vehicle helper class with spherical rigid body wheels.
  5690. * @class RigidVehicle
  5691. * @constructor
  5692. * @param {Body} [options.chassisBody]
  5693. */
  5694. function RigidVehicle(options){
  5695. this.wheelBodies = [];
  5696. /**
  5697. * @property coordinateSystem
  5698. * @type {Vec3}
  5699. */
  5700. this.coordinateSystem = typeof(options.coordinateSystem)==='undefined' ? new Vec3(1, 2, 3) : options.coordinateSystem.clone();
  5701. /**
  5702. * @property {Body} chassisBody
  5703. */
  5704. this.chassisBody = options.chassisBody;
  5705. if(!this.chassisBody){
  5706. // No chassis body given. Create it!
  5707. var chassisShape = new Box(new Vec3(5, 2, 0.5));
  5708. this.chassisBody = new Body(1, chassisShape);
  5709. }
  5710. /**
  5711. * @property constraints
  5712. * @type {Array}
  5713. */
  5714. this.constraints = [];
  5715. this.wheelAxes = [];
  5716. this.wheelForces = [];
  5717. }
  5718. /**
  5719. * Add a wheel
  5720. * @method addWheel
  5721. * @param {object} options
  5722. * @param {boolean} [options.isFrontWheel]
  5723. * @param {Vec3} [options.position] Position of the wheel, locally in the chassis body.
  5724. * @param {Vec3} [options.direction] Slide direction of the wheel along the suspension.
  5725. * @param {Vec3} [options.axis] Axis of rotation of the wheel, locally defined in the chassis.
  5726. * @param {Body} [options.body] The wheel body.
  5727. */
  5728. RigidVehicle.prototype.addWheel = function(options){
  5729. options = options || {};
  5730. var wheelBody = options.body;
  5731. if(!wheelBody){
  5732. wheelBody = new Body(1, new Sphere(1.2));
  5733. }
  5734. this.wheelBodies.push(wheelBody);
  5735. this.wheelForces.push(0);
  5736. // Position constrain wheels
  5737. var zero = new Vec3();
  5738. var position = typeof(options.position) !== 'undefined' ? options.position.clone() : new Vec3();
  5739. // Set position locally to the chassis
  5740. var worldPosition = new Vec3();
  5741. this.chassisBody.pointToWorldFrame(position, worldPosition);
  5742. wheelBody.position.set(worldPosition.x, worldPosition.y, worldPosition.z);
  5743. // Constrain wheel
  5744. var axis = typeof(options.axis) !== 'undefined' ? options.axis.clone() : new Vec3(0, 1, 0);
  5745. this.wheelAxes.push(axis);
  5746. var hingeConstraint = new HingeConstraint(this.chassisBody, wheelBody, {
  5747. pivotA: position,
  5748. axisA: axis,
  5749. pivotB: Vec3.ZERO,
  5750. axisB: axis,
  5751. collideConnected: false
  5752. });
  5753. this.constraints.push(hingeConstraint);
  5754. return this.wheelBodies.length - 1;
  5755. };
  5756. /**
  5757. * Set the steering value of a wheel.
  5758. * @method setSteeringValue
  5759. * @param {number} value
  5760. * @param {integer} wheelIndex
  5761. * @todo check coordinateSystem
  5762. */
  5763. RigidVehicle.prototype.setSteeringValue = function(value, wheelIndex){
  5764. // Set angle of the hinge axis
  5765. var axis = this.wheelAxes[wheelIndex];
  5766. var c = Math.cos(value),
  5767. s = Math.sin(value),
  5768. x = axis.x,
  5769. y = axis.y;
  5770. this.constraints[wheelIndex].axisA.set(
  5771. c*x -s*y,
  5772. s*x +c*y,
  5773. 0
  5774. );
  5775. };
  5776. /**
  5777. * Set the target rotational speed of the hinge constraint.
  5778. * @method setMotorSpeed
  5779. * @param {number} value
  5780. * @param {integer} wheelIndex
  5781. */
  5782. RigidVehicle.prototype.setMotorSpeed = function(value, wheelIndex){
  5783. var hingeConstraint = this.constraints[wheelIndex];
  5784. hingeConstraint.enableMotor();
  5785. hingeConstraint.motorTargetVelocity = value;
  5786. };
  5787. /**
  5788. * Set the target rotational speed of the hinge constraint.
  5789. * @method disableMotor
  5790. * @param {number} value
  5791. * @param {integer} wheelIndex
  5792. */
  5793. RigidVehicle.prototype.disableMotor = function(wheelIndex){
  5794. var hingeConstraint = this.constraints[wheelIndex];
  5795. hingeConstraint.disableMotor();
  5796. };
  5797. var torque = new Vec3();
  5798. /**
  5799. * Set the wheel force to apply on one of the wheels each time step
  5800. * @method setWheelForce
  5801. * @param {number} value
  5802. * @param {integer} wheelIndex
  5803. */
  5804. RigidVehicle.prototype.setWheelForce = function(value, wheelIndex){
  5805. this.wheelForces[wheelIndex] = value;
  5806. };
  5807. /**
  5808. * Apply a torque on one of the wheels.
  5809. * @method applyWheelForce
  5810. * @param {number} value
  5811. * @param {integer} wheelIndex
  5812. */
  5813. RigidVehicle.prototype.applyWheelForce = function(value, wheelIndex){
  5814. var axis = this.wheelAxes[wheelIndex];
  5815. var wheelBody = this.wheelBodies[wheelIndex];
  5816. var bodyTorque = wheelBody.torque;
  5817. axis.scale(value, torque);
  5818. wheelBody.vectorToWorldFrame(torque, torque);
  5819. bodyTorque.vadd(torque, bodyTorque);
  5820. };
  5821. /**
  5822. * Add the vehicle including its constraints to the world.
  5823. * @method addToWorld
  5824. * @param {World} world
  5825. */
  5826. RigidVehicle.prototype.addToWorld = function(world){
  5827. var constraints = this.constraints;
  5828. var bodies = this.wheelBodies.concat([this.chassisBody]);
  5829. for (var i = 0; i < bodies.length; i++) {
  5830. world.add(bodies[i]);
  5831. }
  5832. for (var i = 0; i < constraints.length; i++) {
  5833. world.addConstraint(constraints[i]);
  5834. }
  5835. world.addEventListener('preStep', this._update.bind(this));
  5836. };
  5837. RigidVehicle.prototype._update = function(){
  5838. var wheelForces = this.wheelForces;
  5839. for (var i = 0; i < wheelForces.length; i++) {
  5840. this.applyWheelForce(wheelForces[i], i);
  5841. }
  5842. };
  5843. /**
  5844. * Remove the vehicle including its constraints from the world.
  5845. * @method removeFromWorld
  5846. * @param {World} world
  5847. */
  5848. RigidVehicle.prototype.removeFromWorld = function(world){
  5849. var constraints = this.constraints;
  5850. var bodies = this.wheelBodies.concat([this.chassisBody]);
  5851. for (var i = 0; i < bodies.length; i++) {
  5852. world.remove(bodies[i]);
  5853. }
  5854. for (var i = 0; i < constraints.length; i++) {
  5855. world.removeConstraint(constraints[i]);
  5856. }
  5857. };
  5858. var worldAxis = new Vec3();
  5859. /**
  5860. * Get current rotational velocity of a wheel
  5861. * @method getWheelSpeed
  5862. * @param {integer} wheelIndex
  5863. */
  5864. RigidVehicle.prototype.getWheelSpeed = function(wheelIndex){
  5865. var axis = this.wheelAxes[wheelIndex];
  5866. var wheelBody = this.wheelBodies[wheelIndex];
  5867. var w = wheelBody.angularVelocity;
  5868. this.chassisBody.vectorToWorldFrame(axis, worldAxis);
  5869. return w.dot(worldAxis);
  5870. };
  5871. },{"../constraints/HingeConstraint":15,"../math/Vec3":30,"../shapes/Box":37,"../shapes/Sphere":44,"./Body":31}],34:[function(_dereq_,module,exports){
  5872. module.exports = SPHSystem;
  5873. var Shape = _dereq_('../shapes/Shape');
  5874. var Vec3 = _dereq_('../math/Vec3');
  5875. var Quaternion = _dereq_('../math/Quaternion');
  5876. var Particle = _dereq_('../shapes/Particle');
  5877. var Body = _dereq_('../objects/Body');
  5878. var Material = _dereq_('../material/Material');
  5879. /**
  5880. * Smoothed-particle hydrodynamics system
  5881. * @class SPHSystem
  5882. * @constructor
  5883. */
  5884. function SPHSystem(){
  5885. this.particles = [];
  5886. /**
  5887. * Density of the system (kg/m3).
  5888. * @property {number} density
  5889. */
  5890. this.density = 1;
  5891. /**
  5892. * Distance below which two particles are considered to be neighbors.
  5893. * It should be adjusted so there are about 15-20 neighbor particles within this radius.
  5894. * @property {number} smoothingRadius
  5895. */
  5896. this.smoothingRadius = 1;
  5897. this.speedOfSound = 1;
  5898. /**
  5899. * Viscosity of the system.
  5900. * @property {number} viscosity
  5901. */
  5902. this.viscosity = 0.01;
  5903. this.eps = 0.000001;
  5904. // Stuff Computed per particle
  5905. this.pressures = [];
  5906. this.densities = [];
  5907. this.neighbors = [];
  5908. }
  5909. /**
  5910. * Add a particle to the system.
  5911. * @method add
  5912. * @param {Body} particle
  5913. */
  5914. SPHSystem.prototype.add = function(particle){
  5915. this.particles.push(particle);
  5916. if(this.neighbors.length < this.particles.length){
  5917. this.neighbors.push([]);
  5918. }
  5919. };
  5920. /**
  5921. * Remove a particle from the system.
  5922. * @method remove
  5923. * @param {Body} particle
  5924. */
  5925. SPHSystem.prototype.remove = function(particle){
  5926. var idx = this.particles.indexOf(particle);
  5927. if(idx !== -1){
  5928. this.particles.splice(idx,1);
  5929. if(this.neighbors.length > this.particles.length){
  5930. this.neighbors.pop();
  5931. }
  5932. }
  5933. };
  5934. /**
  5935. * Get neighbors within smoothing volume, save in the array neighbors
  5936. * @method getNeighbors
  5937. * @param {Body} particle
  5938. * @param {Array} neighbors
  5939. */
  5940. var SPHSystem_getNeighbors_dist = new Vec3();
  5941. SPHSystem.prototype.getNeighbors = function(particle,neighbors){
  5942. var N = this.particles.length,
  5943. id = particle.id,
  5944. R2 = this.smoothingRadius * this.smoothingRadius,
  5945. dist = SPHSystem_getNeighbors_dist;
  5946. for(var i=0; i!==N; i++){
  5947. var p = this.particles[i];
  5948. p.position.vsub(particle.position,dist);
  5949. if(id!==p.id && dist.norm2() < R2){
  5950. neighbors.push(p);
  5951. }
  5952. }
  5953. };
  5954. // Temp vectors for calculation
  5955. var SPHSystem_update_dist = new Vec3(),
  5956. SPHSystem_update_a_pressure = new Vec3(),
  5957. SPHSystem_update_a_visc = new Vec3(),
  5958. SPHSystem_update_gradW = new Vec3(),
  5959. SPHSystem_update_r_vec = new Vec3(),
  5960. SPHSystem_update_u = new Vec3(); // Relative velocity
  5961. SPHSystem.prototype.update = function(){
  5962. var N = this.particles.length,
  5963. dist = SPHSystem_update_dist,
  5964. cs = this.speedOfSound,
  5965. eps = this.eps;
  5966. for(var i=0; i!==N; i++){
  5967. var p = this.particles[i]; // Current particle
  5968. var neighbors = this.neighbors[i];
  5969. // Get neighbors
  5970. neighbors.length = 0;
  5971. this.getNeighbors(p,neighbors);
  5972. neighbors.push(this.particles[i]); // Add current too
  5973. var numNeighbors = neighbors.length;
  5974. // Accumulate density for the particle
  5975. var sum = 0.0;
  5976. for(var j=0; j!==numNeighbors; j++){
  5977. //printf("Current particle has position %f %f %f\n",objects[id].pos.x(),objects[id].pos.y(),objects[id].pos.z());
  5978. p.position.vsub(neighbors[j].position, dist);
  5979. var len = dist.norm();
  5980. var weight = this.w(len);
  5981. sum += neighbors[j].mass * weight;
  5982. }
  5983. // Save
  5984. this.densities[i] = sum;
  5985. this.pressures[i] = cs * cs * (this.densities[i] - this.density);
  5986. }
  5987. // Add forces
  5988. // Sum to these accelerations
  5989. var a_pressure= SPHSystem_update_a_pressure;
  5990. var a_visc = SPHSystem_update_a_visc;
  5991. var gradW = SPHSystem_update_gradW;
  5992. var r_vec = SPHSystem_update_r_vec;
  5993. var u = SPHSystem_update_u;
  5994. for(var i=0; i!==N; i++){
  5995. var particle = this.particles[i];
  5996. a_pressure.set(0,0,0);
  5997. a_visc.set(0,0,0);
  5998. // Init vars
  5999. var Pij;
  6000. var nabla;
  6001. var Vij;
  6002. // Sum up for all other neighbors
  6003. var neighbors = this.neighbors[i];
  6004. var numNeighbors = neighbors.length;
  6005. //printf("Neighbors: ");
  6006. for(var j=0; j!==numNeighbors; j++){
  6007. var neighbor = neighbors[j];
  6008. //printf("%d ",nj);
  6009. // Get r once for all..
  6010. particle.position.vsub(neighbor.position,r_vec);
  6011. var r = r_vec.norm();
  6012. // Pressure contribution
  6013. Pij = -neighbor.mass * (this.pressures[i] / (this.densities[i]*this.densities[i] + eps) + this.pressures[j] / (this.densities[j]*this.densities[j] + eps));
  6014. this.gradw(r_vec, gradW);
  6015. // Add to pressure acceleration
  6016. gradW.mult(Pij , gradW);
  6017. a_pressure.vadd(gradW, a_pressure);
  6018. // Viscosity contribution
  6019. neighbor.velocity.vsub(particle.velocity, u);
  6020. u.mult( 1.0 / (0.0001+this.densities[i] * this.densities[j]) * this.viscosity * neighbor.mass , u );
  6021. nabla = this.nablaw(r);
  6022. u.mult(nabla,u);
  6023. // Add to viscosity acceleration
  6024. a_visc.vadd( u, a_visc );
  6025. }
  6026. // Calculate force
  6027. a_visc.mult(particle.mass, a_visc);
  6028. a_pressure.mult(particle.mass, a_pressure);
  6029. // Add force to particles
  6030. particle.force.vadd(a_visc, particle.force);
  6031. particle.force.vadd(a_pressure, particle.force);
  6032. }
  6033. };
  6034. // Calculate the weight using the W(r) weightfunction
  6035. SPHSystem.prototype.w = function(r){
  6036. // 315
  6037. var h = this.smoothingRadius;
  6038. return 315.0/(64.0*Math.PI*Math.pow(h,9)) * Math.pow(h*h-r*r,3);
  6039. };
  6040. // calculate gradient of the weight function
  6041. SPHSystem.prototype.gradw = function(rVec,resultVec){
  6042. var r = rVec.norm(),
  6043. h = this.smoothingRadius;
  6044. rVec.mult(945.0/(32.0*Math.PI*Math.pow(h,9)) * Math.pow((h*h-r*r),2) , resultVec);
  6045. };
  6046. // Calculate nabla(W)
  6047. SPHSystem.prototype.nablaw = function(r){
  6048. var h = this.smoothingRadius;
  6049. var nabla = 945.0/(32.0*Math.PI*Math.pow(h,9)) * (h*h-r*r)*(7*r*r - 3*h*h);
  6050. return nabla;
  6051. };
  6052. },{"../material/Material":25,"../math/Quaternion":28,"../math/Vec3":30,"../objects/Body":31,"../shapes/Particle":41,"../shapes/Shape":43}],35:[function(_dereq_,module,exports){
  6053. var Vec3 = _dereq_('../math/Vec3');
  6054. module.exports = Spring;
  6055. /**
  6056. * A spring, connecting two bodies.
  6057. *
  6058. * @class Spring
  6059. * @constructor
  6060. * @param {Body} bodyA
  6061. * @param {Body} bodyB
  6062. * @param {Object} [options]
  6063. * @param {number} [options.restLength] A number > 0. Default: 1
  6064. * @param {number} [options.stiffness] A number >= 0. Default: 100
  6065. * @param {number} [options.damping] A number >= 0. Default: 1
  6066. * @param {Vec3} [options.worldAnchorA] Where to hook the spring to body A, in world coordinates.
  6067. * @param {Vec3} [options.worldAnchorB]
  6068. * @param {Vec3} [options.localAnchorA] Where to hook the spring to body A, in local body coordinates.
  6069. * @param {Vec3} [options.localAnchorB]
  6070. */
  6071. function Spring(bodyA,bodyB,options){
  6072. options = options || {};
  6073. /**
  6074. * Rest length of the spring.
  6075. * @property restLength
  6076. * @type {number}
  6077. */
  6078. this.restLength = typeof(options.restLength) === "number" ? options.restLength : 1;
  6079. /**
  6080. * Stiffness of the spring.
  6081. * @property stiffness
  6082. * @type {number}
  6083. */
  6084. this.stiffness = options.stiffness || 100;
  6085. /**
  6086. * Damping of the spring.
  6087. * @property damping
  6088. * @type {number}
  6089. */
  6090. this.damping = options.damping || 1;
  6091. /**
  6092. * First connected body.
  6093. * @property bodyA
  6094. * @type {Body}
  6095. */
  6096. this.bodyA = bodyA;
  6097. /**
  6098. * Second connected body.
  6099. * @property bodyB
  6100. * @type {Body}
  6101. */
  6102. this.bodyB = bodyB;
  6103. /**
  6104. * Anchor for bodyA in local bodyA coordinates.
  6105. * @property localAnchorA
  6106. * @type {Vec3}
  6107. */
  6108. this.localAnchorA = new Vec3();
  6109. /**
  6110. * Anchor for bodyB in local bodyB coordinates.
  6111. * @property localAnchorB
  6112. * @type {Vec3}
  6113. */
  6114. this.localAnchorB = new Vec3();
  6115. if(options.localAnchorA){
  6116. this.localAnchorA.copy(options.localAnchorA);
  6117. }
  6118. if(options.localAnchorB){
  6119. this.localAnchorB.copy(options.localAnchorB);
  6120. }
  6121. if(options.worldAnchorA){
  6122. this.setWorldAnchorA(options.worldAnchorA);
  6123. }
  6124. if(options.worldAnchorB){
  6125. this.setWorldAnchorB(options.worldAnchorB);
  6126. }
  6127. }
  6128. /**
  6129. * Set the anchor point on body A, using world coordinates.
  6130. * @method setWorldAnchorA
  6131. * @param {Vec3} worldAnchorA
  6132. */
  6133. Spring.prototype.setWorldAnchorA = function(worldAnchorA){
  6134. this.bodyA.pointToLocalFrame(worldAnchorA,this.localAnchorA);
  6135. };
  6136. /**
  6137. * Set the anchor point on body B, using world coordinates.
  6138. * @method setWorldAnchorB
  6139. * @param {Vec3} worldAnchorB
  6140. */
  6141. Spring.prototype.setWorldAnchorB = function(worldAnchorB){
  6142. this.bodyB.pointToLocalFrame(worldAnchorB,this.localAnchorB);
  6143. };
  6144. /**
  6145. * Get the anchor point on body A, in world coordinates.
  6146. * @method getWorldAnchorA
  6147. * @param {Vec3} result The vector to store the result in.
  6148. */
  6149. Spring.prototype.getWorldAnchorA = function(result){
  6150. this.bodyA.pointToWorldFrame(this.localAnchorA,result);
  6151. };
  6152. /**
  6153. * Get the anchor point on body B, in world coordinates.
  6154. * @method getWorldAnchorB
  6155. * @param {Vec3} result The vector to store the result in.
  6156. */
  6157. Spring.prototype.getWorldAnchorB = function(result){
  6158. this.bodyB.pointToWorldFrame(this.localAnchorB,result);
  6159. };
  6160. var applyForce_r = new Vec3(),
  6161. applyForce_r_unit = new Vec3(),
  6162. applyForce_u = new Vec3(),
  6163. applyForce_f = new Vec3(),
  6164. applyForce_worldAnchorA = new Vec3(),
  6165. applyForce_worldAnchorB = new Vec3(),
  6166. applyForce_ri = new Vec3(),
  6167. applyForce_rj = new Vec3(),
  6168. applyForce_ri_x_f = new Vec3(),
  6169. applyForce_rj_x_f = new Vec3(),
  6170. applyForce_tmp = new Vec3();
  6171. /**
  6172. * Apply the spring force to the connected bodies.
  6173. * @method applyForce
  6174. */
  6175. Spring.prototype.applyForce = function(){
  6176. var k = this.stiffness,
  6177. d = this.damping,
  6178. l = this.restLength,
  6179. bodyA = this.bodyA,
  6180. bodyB = this.bodyB,
  6181. r = applyForce_r,
  6182. r_unit = applyForce_r_unit,
  6183. u = applyForce_u,
  6184. f = applyForce_f,
  6185. tmp = applyForce_tmp;
  6186. var worldAnchorA = applyForce_worldAnchorA,
  6187. worldAnchorB = applyForce_worldAnchorB,
  6188. ri = applyForce_ri,
  6189. rj = applyForce_rj,
  6190. ri_x_f = applyForce_ri_x_f,
  6191. rj_x_f = applyForce_rj_x_f;
  6192. // Get world anchors
  6193. this.getWorldAnchorA(worldAnchorA);
  6194. this.getWorldAnchorB(worldAnchorB);
  6195. // Get offset points
  6196. worldAnchorA.vsub(bodyA.position,ri);
  6197. worldAnchorB.vsub(bodyB.position,rj);
  6198. // Compute distance vector between world anchor points
  6199. worldAnchorB.vsub(worldAnchorA,r);
  6200. var rlen = r.norm();
  6201. r_unit.copy(r);
  6202. r_unit.normalize();
  6203. // Compute relative velocity of the anchor points, u
  6204. bodyB.velocity.vsub(bodyA.velocity,u);
  6205. // Add rotational velocity
  6206. bodyB.angularVelocity.cross(rj,tmp);
  6207. u.vadd(tmp,u);
  6208. bodyA.angularVelocity.cross(ri,tmp);
  6209. u.vsub(tmp,u);
  6210. // F = - k * ( x - L ) - D * ( u )
  6211. r_unit.mult(-k*(rlen-l) - d*u.dot(r_unit), f);
  6212. // Add forces to bodies
  6213. bodyA.force.vsub(f,bodyA.force);
  6214. bodyB.force.vadd(f,bodyB.force);
  6215. // Angular force
  6216. ri.cross(f,ri_x_f);
  6217. rj.cross(f,rj_x_f);
  6218. bodyA.torque.vsub(ri_x_f,bodyA.torque);
  6219. bodyB.torque.vadd(rj_x_f,bodyB.torque);
  6220. };
  6221. },{"../math/Vec3":30}],36:[function(_dereq_,module,exports){
  6222. var Vec3 = _dereq_('../math/Vec3');
  6223. var Transform = _dereq_('../math/Transform');
  6224. var RaycastResult = _dereq_('../collision/RaycastResult');
  6225. var Utils = _dereq_('../utils/Utils');
  6226. module.exports = WheelInfo;
  6227. /**
  6228. * @class WheelInfo
  6229. * @constructor
  6230. * @param {Object} [options]
  6231. *
  6232. * @param {Vec3} [options.chassisConnectionPointLocal]
  6233. * @param {Vec3} [options.chassisConnectionPointWorld]
  6234. * @param {Vec3} [options.directionLocal]
  6235. * @param {Vec3} [options.directionWorld]
  6236. * @param {Vec3} [options.axleLocal]
  6237. * @param {Vec3} [options.axleWorld]
  6238. * @param {number} [options.suspensionRestLength=1]
  6239. * @param {number} [options.suspensionMaxLength=2]
  6240. * @param {number} [options.radius=1]
  6241. * @param {number} [options.suspensionStiffness=100]
  6242. * @param {number} [options.dampingCompression=10]
  6243. * @param {number} [options.dampingRelaxation=10]
  6244. * @param {number} [options.frictionSlip=10000]
  6245. * @param {number} [options.steering=0]
  6246. * @param {number} [options.rotation=0]
  6247. * @param {number} [options.deltaRotation=0]
  6248. * @param {number} [options.rollInfluence=0.01]
  6249. * @param {number} [options.maxSuspensionForce]
  6250. * @param {boolean} [options.isFrontWheel=true]
  6251. * @param {number} [options.clippedInvContactDotSuspension=1]
  6252. * @param {number} [options.suspensionRelativeVelocity=0]
  6253. * @param {number} [options.suspensionForce=0]
  6254. * @param {number} [options.skidInfo=0]
  6255. * @param {number} [options.suspensionLength=0]
  6256. * @param {number} [options.maxSuspensionTravel=1]
  6257. * @param {boolean} [options.useCustomSlidingRotationalSpeed=false]
  6258. * @param {number} [options.customSlidingRotationalSpeed=-0.1]
  6259. */
  6260. function WheelInfo(options){
  6261. options = Utils.defaults(options, {
  6262. chassisConnectionPointLocal: new Vec3(),
  6263. chassisConnectionPointWorld: new Vec3(),
  6264. directionLocal: new Vec3(),
  6265. directionWorld: new Vec3(),
  6266. axleLocal: new Vec3(),
  6267. axleWorld: new Vec3(),
  6268. suspensionRestLength: 1,
  6269. suspensionMaxLength: 2,
  6270. radius: 1,
  6271. suspensionStiffness: 100,
  6272. dampingCompression: 10,
  6273. dampingRelaxation: 10,
  6274. frictionSlip: 10000,
  6275. steering: 0,
  6276. rotation: 0,
  6277. deltaRotation: 0,
  6278. rollInfluence: 0.01,
  6279. maxSuspensionForce: Number.MAX_VALUE,
  6280. isFrontWheel: true,
  6281. clippedInvContactDotSuspension: 1,
  6282. suspensionRelativeVelocity: 0,
  6283. suspensionForce: 0,
  6284. skidInfo: 0,
  6285. suspensionLength: 0,
  6286. maxSuspensionTravel: 1,
  6287. useCustomSlidingRotationalSpeed: false,
  6288. customSlidingRotationalSpeed: -0.1
  6289. });
  6290. /**
  6291. * Max travel distance of the suspension, in meters.
  6292. * @property {number} maxSuspensionTravel
  6293. */
  6294. this.maxSuspensionTravel = options.maxSuspensionTravel;
  6295. /**
  6296. * Speed to apply to the wheel rotation when the wheel is sliding.
  6297. * @property {number} customSlidingRotationalSpeed
  6298. */
  6299. this.customSlidingRotationalSpeed = options.customSlidingRotationalSpeed;
  6300. /**
  6301. * If the customSlidingRotationalSpeed should be used.
  6302. * @property {Boolean} useCustomSlidingRotationalSpeed
  6303. */
  6304. this.useCustomSlidingRotationalSpeed = options.useCustomSlidingRotationalSpeed;
  6305. /**
  6306. * @property {Boolean} sliding
  6307. */
  6308. this.sliding = false;
  6309. /**
  6310. * Connection point, defined locally in the chassis body frame.
  6311. * @property {Vec3} chassisConnectionPointLocal
  6312. */
  6313. this.chassisConnectionPointLocal = options.chassisConnectionPointLocal.clone();
  6314. /**
  6315. * @property {Vec3} chassisConnectionPointWorld
  6316. */
  6317. this.chassisConnectionPointWorld = options.chassisConnectionPointWorld.clone();
  6318. /**
  6319. * @property {Vec3} directionLocal
  6320. */
  6321. this.directionLocal = options.directionLocal.clone();
  6322. /**
  6323. * @property {Vec3} directionWorld
  6324. */
  6325. this.directionWorld = options.directionWorld.clone();
  6326. /**
  6327. * @property {Vec3} axleLocal
  6328. */
  6329. this.axleLocal = options.axleLocal.clone();
  6330. /**
  6331. * @property {Vec3} axleWorld
  6332. */
  6333. this.axleWorld = options.axleWorld.clone();
  6334. /**
  6335. * @property {number} suspensionRestLength
  6336. */
  6337. this.suspensionRestLength = options.suspensionRestLength;
  6338. /**
  6339. * @property {number} suspensionMaxLength
  6340. */
  6341. this.suspensionMaxLength = options.suspensionMaxLength;
  6342. /**
  6343. * @property {number} radius
  6344. */
  6345. this.radius = options.radius;
  6346. /**
  6347. * @property {number} suspensionStiffness
  6348. */
  6349. this.suspensionStiffness = options.suspensionStiffness;
  6350. /**
  6351. * @property {number} dampingCompression
  6352. */
  6353. this.dampingCompression = options.dampingCompression;
  6354. /**
  6355. * @property {number} dampingRelaxation
  6356. */
  6357. this.dampingRelaxation = options.dampingRelaxation;
  6358. /**
  6359. * @property {number} frictionSlip
  6360. */
  6361. this.frictionSlip = options.frictionSlip;
  6362. /**
  6363. * @property {number} steering
  6364. */
  6365. this.steering = 0;
  6366. /**
  6367. * Rotation value, in radians.
  6368. * @property {number} rotation
  6369. */
  6370. this.rotation = 0;
  6371. /**
  6372. * @property {number} deltaRotation
  6373. */
  6374. this.deltaRotation = 0;
  6375. /**
  6376. * @property {number} rollInfluence
  6377. */
  6378. this.rollInfluence = options.rollInfluence;
  6379. /**
  6380. * @property {number} maxSuspensionForce
  6381. */
  6382. this.maxSuspensionForce = options.maxSuspensionForce;
  6383. /**
  6384. * @property {number} engineForce
  6385. */
  6386. this.engineForce = 0;
  6387. /**
  6388. * @property {number} brake
  6389. */
  6390. this.brake = 0;
  6391. /**
  6392. * @property {number} isFrontWheel
  6393. */
  6394. this.isFrontWheel = options.isFrontWheel;
  6395. /**
  6396. * @property {number} clippedInvContactDotSuspension
  6397. */
  6398. this.clippedInvContactDotSuspension = 1;
  6399. /**
  6400. * @property {number} suspensionRelativeVelocity
  6401. */
  6402. this.suspensionRelativeVelocity = 0;
  6403. /**
  6404. * @property {number} suspensionForce
  6405. */
  6406. this.suspensionForce = 0;
  6407. /**
  6408. * @property {number} skidInfo
  6409. */
  6410. this.skidInfo = 0;
  6411. /**
  6412. * @property {number} suspensionLength
  6413. */
  6414. this.suspensionLength = 0;
  6415. /**
  6416. * @property {number} sideImpulse
  6417. */
  6418. this.sideImpulse = 0;
  6419. /**
  6420. * @property {number} forwardImpulse
  6421. */
  6422. this.forwardImpulse = 0;
  6423. /**
  6424. * The result from raycasting
  6425. * @property {RaycastResult} raycastResult
  6426. */
  6427. this.raycastResult = new RaycastResult();
  6428. /**
  6429. * Wheel world transform
  6430. * @property {Transform} worldTransform
  6431. */
  6432. this.worldTransform = new Transform();
  6433. /**
  6434. * @property {boolean} isInContact
  6435. */
  6436. this.isInContact = false;
  6437. }
  6438. var chassis_velocity_at_contactPoint = new Vec3();
  6439. var relpos = new Vec3();
  6440. var chassis_velocity_at_contactPoint = new Vec3();
  6441. WheelInfo.prototype.updateWheel = function(chassis){
  6442. var raycastResult = this.raycastResult;
  6443. if (this.isInContact){
  6444. var project= raycastResult.hitNormalWorld.dot(raycastResult.directionWorld);
  6445. raycastResult.hitPointWorld.vsub(chassis.position, relpos);
  6446. chassis.getVelocityAtWorldPoint(relpos, chassis_velocity_at_contactPoint);
  6447. var projVel = raycastResult.hitNormalWorld.dot( chassis_velocity_at_contactPoint );
  6448. if (project >= -0.1) {
  6449. this.suspensionRelativeVelocity = 0.0;
  6450. this.clippedInvContactDotSuspension = 1.0 / 0.1;
  6451. } else {
  6452. var inv = -1 / project;
  6453. this.suspensionRelativeVelocity = projVel * inv;
  6454. this.clippedInvContactDotSuspension = inv;
  6455. }
  6456. } else {
  6457. // Not in contact : position wheel in a nice (rest length) position
  6458. raycastResult.suspensionLength = this.suspensionRestLength;
  6459. this.suspensionRelativeVelocity = 0.0;
  6460. raycastResult.directionWorld.scale(-1, raycastResult.hitNormalWorld);
  6461. this.clippedInvContactDotSuspension = 1.0;
  6462. }
  6463. };
  6464. },{"../collision/RaycastResult":10,"../math/Transform":29,"../math/Vec3":30,"../utils/Utils":53}],37:[function(_dereq_,module,exports){
  6465. module.exports = Box;
  6466. var Shape = _dereq_('./Shape');
  6467. var Vec3 = _dereq_('../math/Vec3');
  6468. var ConvexPolyhedron = _dereq_('./ConvexPolyhedron');
  6469. /**
  6470. * A 3d box shape.
  6471. * @class Box
  6472. * @constructor
  6473. * @param {Vec3} halfExtents
  6474. * @author schteppe
  6475. * @extends Shape
  6476. */
  6477. function Box(halfExtents){
  6478. Shape.call(this);
  6479. this.type = Shape.types.BOX;
  6480. /**
  6481. * @property halfExtents
  6482. * @type {Vec3}
  6483. */
  6484. this.halfExtents = halfExtents;
  6485. /**
  6486. * Used by the contact generator to make contacts with other convex polyhedra for example
  6487. * @property convexPolyhedronRepresentation
  6488. * @type {ConvexPolyhedron}
  6489. */
  6490. this.convexPolyhedronRepresentation = null;
  6491. this.updateConvexPolyhedronRepresentation();
  6492. this.updateBoundingSphereRadius();
  6493. }
  6494. Box.prototype = new Shape();
  6495. Box.prototype.constructor = Box;
  6496. /**
  6497. * Updates the local convex polyhedron representation used for some collisions.
  6498. * @method updateConvexPolyhedronRepresentation
  6499. */
  6500. Box.prototype.updateConvexPolyhedronRepresentation = function(){
  6501. var sx = this.halfExtents.x;
  6502. var sy = this.halfExtents.y;
  6503. var sz = this.halfExtents.z;
  6504. var V = Vec3;
  6505. var vertices = [
  6506. new V(-sx,-sy,-sz),
  6507. new V( sx,-sy,-sz),
  6508. new V( sx, sy,-sz),
  6509. new V(-sx, sy,-sz),
  6510. new V(-sx,-sy, sz),
  6511. new V( sx,-sy, sz),
  6512. new V( sx, sy, sz),
  6513. new V(-sx, sy, sz)
  6514. ];
  6515. var indices = [
  6516. [3,2,1,0], // -z
  6517. [4,5,6,7], // +z
  6518. [5,4,0,1], // -y
  6519. [2,3,7,6], // +y
  6520. [0,4,7,3], // -x
  6521. [1,2,6,5], // +x
  6522. ];
  6523. var axes = [
  6524. new V(0, 0, 1),
  6525. new V(0, 1, 0),
  6526. new V(1, 0, 0)
  6527. ];
  6528. var h = new ConvexPolyhedron(vertices, indices);
  6529. this.convexPolyhedronRepresentation = h;
  6530. h.material = this.material;
  6531. };
  6532. /**
  6533. * @method calculateLocalInertia
  6534. * @param {Number} mass
  6535. * @param {Vec3} target
  6536. * @return {Vec3}
  6537. */
  6538. Box.prototype.calculateLocalInertia = function(mass,target){
  6539. target = target || new Vec3();
  6540. Box.calculateInertia(this.halfExtents, mass, target);
  6541. return target;
  6542. };
  6543. Box.calculateInertia = function(halfExtents,mass,target){
  6544. var e = halfExtents;
  6545. target.x = 1.0 / 12.0 * mass * ( 2*e.y*2*e.y + 2*e.z*2*e.z );
  6546. target.y = 1.0 / 12.0 * mass * ( 2*e.x*2*e.x + 2*e.z*2*e.z );
  6547. target.z = 1.0 / 12.0 * mass * ( 2*e.y*2*e.y + 2*e.x*2*e.x );
  6548. };
  6549. /**
  6550. * Get the box 6 side normals
  6551. * @method getSideNormals
  6552. * @param {array} sixTargetVectors An array of 6 vectors, to store the resulting side normals in.
  6553. * @param {Quaternion} quat Orientation to apply to the normal vectors. If not provided, the vectors will be in respect to the local frame.
  6554. * @return {array}
  6555. */
  6556. Box.prototype.getSideNormals = function(sixTargetVectors,quat){
  6557. var sides = sixTargetVectors;
  6558. var ex = this.halfExtents;
  6559. sides[0].set( ex.x, 0, 0);
  6560. sides[1].set( 0, ex.y, 0);
  6561. sides[2].set( 0, 0, ex.z);
  6562. sides[3].set( -ex.x, 0, 0);
  6563. sides[4].set( 0, -ex.y, 0);
  6564. sides[5].set( 0, 0, -ex.z);
  6565. if(quat!==undefined){
  6566. for(var i=0; i!==sides.length; i++){
  6567. quat.vmult(sides[i],sides[i]);
  6568. }
  6569. }
  6570. return sides;
  6571. };
  6572. Box.prototype.volume = function(){
  6573. return 8.0 * this.halfExtents.x * this.halfExtents.y * this.halfExtents.z;
  6574. };
  6575. Box.prototype.updateBoundingSphereRadius = function(){
  6576. this.boundingSphereRadius = this.halfExtents.norm();
  6577. };
  6578. var worldCornerTempPos = new Vec3();
  6579. var worldCornerTempNeg = new Vec3();
  6580. Box.prototype.forEachWorldCorner = function(pos,quat,callback){
  6581. var e = this.halfExtents;
  6582. var corners = [[ e.x, e.y, e.z],
  6583. [ -e.x, e.y, e.z],
  6584. [ -e.x, -e.y, e.z],
  6585. [ -e.x, -e.y, -e.z],
  6586. [ e.x, -e.y, -e.z],
  6587. [ e.x, e.y, -e.z],
  6588. [ -e.x, e.y, -e.z],
  6589. [ e.x, -e.y, e.z]];
  6590. for(var i=0; i<corners.length; i++){
  6591. worldCornerTempPos.set(corners[i][0],corners[i][1],corners[i][2]);
  6592. quat.vmult(worldCornerTempPos,worldCornerTempPos);
  6593. pos.vadd(worldCornerTempPos,worldCornerTempPos);
  6594. callback(worldCornerTempPos.x,
  6595. worldCornerTempPos.y,
  6596. worldCornerTempPos.z);
  6597. }
  6598. };
  6599. var worldCornersTemp = [
  6600. new Vec3(),
  6601. new Vec3(),
  6602. new Vec3(),
  6603. new Vec3(),
  6604. new Vec3(),
  6605. new Vec3(),
  6606. new Vec3(),
  6607. new Vec3()
  6608. ];
  6609. Box.prototype.calculateWorldAABB = function(pos,quat,min,max){
  6610. var e = this.halfExtents;
  6611. worldCornersTemp[0].set(e.x, e.y, e.z);
  6612. worldCornersTemp[1].set(-e.x, e.y, e.z);
  6613. worldCornersTemp[2].set(-e.x, -e.y, e.z);
  6614. worldCornersTemp[3].set(-e.x, -e.y, -e.z);
  6615. worldCornersTemp[4].set(e.x, -e.y, -e.z);
  6616. worldCornersTemp[5].set(e.x, e.y, -e.z);
  6617. worldCornersTemp[6].set(-e.x, e.y, -e.z);
  6618. worldCornersTemp[7].set(e.x, -e.y, e.z);
  6619. var wc = worldCornersTemp[0];
  6620. quat.vmult(wc, wc);
  6621. pos.vadd(wc, wc);
  6622. max.copy(wc);
  6623. min.copy(wc);
  6624. for(var i=1; i<8; i++){
  6625. var wc = worldCornersTemp[i];
  6626. quat.vmult(wc, wc);
  6627. pos.vadd(wc, wc);
  6628. var x = wc.x;
  6629. var y = wc.y;
  6630. var z = wc.z;
  6631. if(x > max.x){
  6632. max.x = x;
  6633. }
  6634. if(y > max.y){
  6635. max.y = y;
  6636. }
  6637. if(z > max.z){
  6638. max.z = z;
  6639. }
  6640. if(x < min.x){
  6641. min.x = x;
  6642. }
  6643. if(y < min.y){
  6644. min.y = y;
  6645. }
  6646. if(z < min.z){
  6647. min.z = z;
  6648. }
  6649. }
  6650. // Get each axis max
  6651. // min.set(Infinity,Infinity,Infinity);
  6652. // max.set(-Infinity,-Infinity,-Infinity);
  6653. // this.forEachWorldCorner(pos,quat,function(x,y,z){
  6654. // if(x > max.x){
  6655. // max.x = x;
  6656. // }
  6657. // if(y > max.y){
  6658. // max.y = y;
  6659. // }
  6660. // if(z > max.z){
  6661. // max.z = z;
  6662. // }
  6663. // if(x < min.x){
  6664. // min.x = x;
  6665. // }
  6666. // if(y < min.y){
  6667. // min.y = y;
  6668. // }
  6669. // if(z < min.z){
  6670. // min.z = z;
  6671. // }
  6672. // });
  6673. };
  6674. },{"../math/Vec3":30,"./ConvexPolyhedron":38,"./Shape":43}],38:[function(_dereq_,module,exports){
  6675. module.exports = ConvexPolyhedron;
  6676. var Shape = _dereq_('./Shape');
  6677. var Vec3 = _dereq_('../math/Vec3');
  6678. var Quaternion = _dereq_('../math/Quaternion');
  6679. var Transform = _dereq_('../math/Transform');
  6680. /**
  6681. * A set of polygons describing a convex shape.
  6682. * @class ConvexPolyhedron
  6683. * @constructor
  6684. * @extends Shape
  6685. * @description The shape MUST be convex for the code to work properly. No polygons may be coplanar (contained
  6686. * in the same 3D plane), instead these should be merged into one polygon.
  6687. *
  6688. * @param {array} points An array of Vec3's
  6689. * @param {array} faces Array of integer arrays, describing which vertices that is included in each face.
  6690. *
  6691. * @author qiao / https://github.com/qiao (original author, see https://github.com/qiao/three.js/commit/85026f0c769e4000148a67d45a9e9b9c5108836f)
  6692. * @author schteppe / https://github.com/schteppe
  6693. * @see http://www.altdevblogaday.com/2011/05/13/contact-generation-between-3d-convex-meshes/
  6694. * @see http://bullet.googlecode.com/svn/trunk/src/BulletCollision/NarrowPhaseCollision/btPolyhedralContactClipping.cpp
  6695. *
  6696. * @todo Move the clipping functions to ContactGenerator?
  6697. * @todo Automatically merge coplanar polygons in constructor.
  6698. */
  6699. function ConvexPolyhedron(points, faces, uniqueAxes) {
  6700. var that = this;
  6701. Shape.call(this);
  6702. this.type = Shape.types.CONVEXPOLYHEDRON;
  6703. /**
  6704. * Array of Vec3
  6705. * @property vertices
  6706. * @type {Array}
  6707. */
  6708. this.vertices = points||[];
  6709. this.worldVertices = []; // World transformed version of .vertices
  6710. this.worldVerticesNeedsUpdate = true;
  6711. /**
  6712. * Array of integer arrays, indicating which vertices each face consists of
  6713. * @property faces
  6714. * @type {Array}
  6715. */
  6716. this.faces = faces||[];
  6717. /**
  6718. * Array of Vec3
  6719. * @property faceNormals
  6720. * @type {Array}
  6721. */
  6722. this.faceNormals = [];
  6723. this.computeNormals();
  6724. this.worldFaceNormalsNeedsUpdate = true;
  6725. this.worldFaceNormals = []; // World transformed version of .faceNormals
  6726. /**
  6727. * Array of Vec3
  6728. * @property uniqueEdges
  6729. * @type {Array}
  6730. */
  6731. this.uniqueEdges = [];
  6732. /**
  6733. * If given, these locally defined, normalized axes are the only ones being checked when doing separating axis check.
  6734. * @property {Array} uniqueAxes
  6735. */
  6736. this.uniqueAxes = uniqueAxes ? uniqueAxes.slice() : null;
  6737. this.computeEdges();
  6738. this.updateBoundingSphereRadius();
  6739. }
  6740. ConvexPolyhedron.prototype = new Shape();
  6741. ConvexPolyhedron.prototype.constructor = ConvexPolyhedron;
  6742. var computeEdges_tmpEdge = new Vec3();
  6743. /**
  6744. * Computes uniqueEdges
  6745. * @method computeEdges
  6746. */
  6747. ConvexPolyhedron.prototype.computeEdges = function(){
  6748. var faces = this.faces;
  6749. var vertices = this.vertices;
  6750. var nv = vertices.length;
  6751. var edges = this.uniqueEdges;
  6752. edges.length = 0;
  6753. var edge = computeEdges_tmpEdge;
  6754. for(var i=0; i !== faces.length; i++){
  6755. var face = faces[i];
  6756. var numVertices = face.length;
  6757. for(var j = 0; j !== numVertices; j++){
  6758. var k = ( j+1 ) % numVertices;
  6759. vertices[face[j]].vsub(vertices[face[k]], edge);
  6760. edge.normalize();
  6761. var found = false;
  6762. for(var p=0; p !== edges.length; p++){
  6763. if (edges[p].almostEquals(edge) || edges[p].almostEquals(edge)){
  6764. found = true;
  6765. break;
  6766. }
  6767. }
  6768. if (!found){
  6769. edges.push(edge.clone());
  6770. }
  6771. }
  6772. }
  6773. };
  6774. /**
  6775. * Compute the normals of the faces. Will reuse existing Vec3 objects in the .faceNormals array if they exist.
  6776. * @method computeNormals
  6777. */
  6778. ConvexPolyhedron.prototype.computeNormals = function(){
  6779. this.faceNormals.length = this.faces.length;
  6780. // Generate normals
  6781. for(var i=0; i<this.faces.length; i++){
  6782. // Check so all vertices exists for this face
  6783. for(var j=0; j<this.faces[i].length; j++){
  6784. if(!this.vertices[this.faces[i][j]]){
  6785. throw new Error("Vertex "+this.faces[i][j]+" not found!");
  6786. }
  6787. }
  6788. var n = this.faceNormals[i] || new Vec3();
  6789. this.getFaceNormal(i,n);
  6790. n.negate(n);
  6791. this.faceNormals[i] = n;
  6792. var vertex = this.vertices[this.faces[i][0]];
  6793. if(n.dot(vertex) < 0){
  6794. console.error(".faceNormals[" + i + "] = Vec3("+n.toString()+") looks like it points into the shape? The vertices follow. Make sure they are ordered CCW around the normal, using the right hand rule.");
  6795. for(var j=0; j<this.faces[i].length; j++){
  6796. console.warn(".vertices["+this.faces[i][j]+"] = Vec3("+this.vertices[this.faces[i][j]].toString()+")");
  6797. }
  6798. }
  6799. }
  6800. };
  6801. /**
  6802. * Get face normal given 3 vertices
  6803. * @static
  6804. * @method getFaceNormal
  6805. * @param {Vec3} va
  6806. * @param {Vec3} vb
  6807. * @param {Vec3} vc
  6808. * @param {Vec3} target
  6809. */
  6810. var cb = new Vec3();
  6811. var ab = new Vec3();
  6812. ConvexPolyhedron.computeNormal = function ( va, vb, vc, target ) {
  6813. vb.vsub(va,ab);
  6814. vc.vsub(vb,cb);
  6815. cb.cross(ab,target);
  6816. if ( !target.isZero() ) {
  6817. target.normalize();
  6818. }
  6819. };
  6820. /**
  6821. * Compute the normal of a face from its vertices
  6822. * @method getFaceNormal
  6823. * @param {Number} i
  6824. * @param {Vec3} target
  6825. */
  6826. ConvexPolyhedron.prototype.getFaceNormal = function(i,target){
  6827. var f = this.faces[i];
  6828. var va = this.vertices[f[0]];
  6829. var vb = this.vertices[f[1]];
  6830. var vc = this.vertices[f[2]];
  6831. return ConvexPolyhedron.computeNormal(va,vb,vc,target);
  6832. };
  6833. /**
  6834. * @method clipAgainstHull
  6835. * @param {Vec3} posA
  6836. * @param {Quaternion} quatA
  6837. * @param {ConvexPolyhedron} hullB
  6838. * @param {Vec3} posB
  6839. * @param {Quaternion} quatB
  6840. * @param {Vec3} separatingNormal
  6841. * @param {Number} minDist Clamp distance
  6842. * @param {Number} maxDist
  6843. * @param {array} result The an array of contact point objects, see clipFaceAgainstHull
  6844. * @see http://bullet.googlecode.com/svn/trunk/src/BulletCollision/NarrowPhaseCollision/btPolyhedralContactClipping.cpp
  6845. */
  6846. var cah_WorldNormal = new Vec3();
  6847. ConvexPolyhedron.prototype.clipAgainstHull = function(posA,quatA,hullB,posB,quatB,separatingNormal,minDist,maxDist,result){
  6848. var WorldNormal = cah_WorldNormal;
  6849. var hullA = this;
  6850. var curMaxDist = maxDist;
  6851. var closestFaceB = -1;
  6852. var dmax = -Number.MAX_VALUE;
  6853. for(var face=0; face < hullB.faces.length; face++){
  6854. WorldNormal.copy(hullB.faceNormals[face]);
  6855. quatB.vmult(WorldNormal,WorldNormal);
  6856. //posB.vadd(WorldNormal,WorldNormal);
  6857. var d = WorldNormal.dot(separatingNormal);
  6858. if (d > dmax){
  6859. dmax = d;
  6860. closestFaceB = face;
  6861. }
  6862. }
  6863. var worldVertsB1 = [];
  6864. var polyB = hullB.faces[closestFaceB];
  6865. var numVertices = polyB.length;
  6866. for(var e0=0; e0<numVertices; e0++){
  6867. var b = hullB.vertices[polyB[e0]];
  6868. var worldb = new Vec3();
  6869. worldb.copy(b);
  6870. quatB.vmult(worldb,worldb);
  6871. posB.vadd(worldb,worldb);
  6872. worldVertsB1.push(worldb);
  6873. }
  6874. if (closestFaceB>=0){
  6875. this.clipFaceAgainstHull(separatingNormal,
  6876. posA,
  6877. quatA,
  6878. worldVertsB1,
  6879. minDist,
  6880. maxDist,
  6881. result);
  6882. }
  6883. };
  6884. /**
  6885. * Find the separating axis between this hull and another
  6886. * @method findSeparatingAxis
  6887. * @param {ConvexPolyhedron} hullB
  6888. * @param {Vec3} posA
  6889. * @param {Quaternion} quatA
  6890. * @param {Vec3} posB
  6891. * @param {Quaternion} quatB
  6892. * @param {Vec3} target The target vector to save the axis in
  6893. * @return {bool} Returns false if a separation is found, else true
  6894. */
  6895. var fsa_faceANormalWS3 = new Vec3(),
  6896. fsa_Worldnormal1 = new Vec3(),
  6897. fsa_deltaC = new Vec3(),
  6898. fsa_worldEdge0 = new Vec3(),
  6899. fsa_worldEdge1 = new Vec3(),
  6900. fsa_Cross = new Vec3();
  6901. ConvexPolyhedron.prototype.findSeparatingAxis = function(hullB,posA,quatA,posB,quatB,target, faceListA, faceListB){
  6902. var faceANormalWS3 = fsa_faceANormalWS3,
  6903. Worldnormal1 = fsa_Worldnormal1,
  6904. deltaC = fsa_deltaC,
  6905. worldEdge0 = fsa_worldEdge0,
  6906. worldEdge1 = fsa_worldEdge1,
  6907. Cross = fsa_Cross;
  6908. var dmin = Number.MAX_VALUE;
  6909. var hullA = this;
  6910. var curPlaneTests=0;
  6911. if(!hullA.uniqueAxes){
  6912. var numFacesA = faceListA ? faceListA.length : hullA.faces.length;
  6913. // Test face normals from hullA
  6914. for(var i=0; i<numFacesA; i++){
  6915. var fi = faceListA ? faceListA[i] : i;
  6916. // Get world face normal
  6917. faceANormalWS3.copy(hullA.faceNormals[fi]);
  6918. quatA.vmult(faceANormalWS3,faceANormalWS3);
  6919. var d = hullA.testSepAxis(faceANormalWS3, hullB, posA, quatA, posB, quatB);
  6920. if(d===false){
  6921. return false;
  6922. }
  6923. if(d<dmin){
  6924. dmin = d;
  6925. target.copy(faceANormalWS3);
  6926. }
  6927. }
  6928. } else {
  6929. // Test unique axes
  6930. for(var i = 0; i !== hullA.uniqueAxes.length; i++){
  6931. // Get world axis
  6932. quatA.vmult(hullA.uniqueAxes[i],faceANormalWS3);
  6933. var d = hullA.testSepAxis(faceANormalWS3, hullB, posA, quatA, posB, quatB);
  6934. if(d===false){
  6935. return false;
  6936. }
  6937. if(d<dmin){
  6938. dmin = d;
  6939. target.copy(faceANormalWS3);
  6940. }
  6941. }
  6942. }
  6943. if(!hullB.uniqueAxes){
  6944. // Test face normals from hullB
  6945. var numFacesB = faceListB ? faceListB.length : hullB.faces.length;
  6946. for(var i=0;i<numFacesB;i++){
  6947. var fi = faceListB ? faceListB[i] : i;
  6948. Worldnormal1.copy(hullB.faceNormals[fi]);
  6949. quatB.vmult(Worldnormal1,Worldnormal1);
  6950. curPlaneTests++;
  6951. var d = hullA.testSepAxis(Worldnormal1, hullB,posA,quatA,posB,quatB);
  6952. if(d===false){
  6953. return false;
  6954. }
  6955. if(d<dmin){
  6956. dmin = d;
  6957. target.copy(Worldnormal1);
  6958. }
  6959. }
  6960. } else {
  6961. // Test unique axes in B
  6962. for(var i = 0; i !== hullB.uniqueAxes.length; i++){
  6963. quatB.vmult(hullB.uniqueAxes[i],Worldnormal1);
  6964. curPlaneTests++;
  6965. var d = hullA.testSepAxis(Worldnormal1, hullB,posA,quatA,posB,quatB);
  6966. if(d===false){
  6967. return false;
  6968. }
  6969. if(d<dmin){
  6970. dmin = d;
  6971. target.copy(Worldnormal1);
  6972. }
  6973. }
  6974. }
  6975. // Test edges
  6976. for(var e0=0; e0 !== hullA.uniqueEdges.length; e0++){
  6977. // Get world edge
  6978. quatA.vmult(hullA.uniqueEdges[e0],worldEdge0);
  6979. for(var e1=0; e1 !== hullB.uniqueEdges.length; e1++){
  6980. // Get world edge 2
  6981. quatB.vmult(hullB.uniqueEdges[e1], worldEdge1);
  6982. worldEdge0.cross(worldEdge1,Cross);
  6983. if(!Cross.almostZero()){
  6984. Cross.normalize();
  6985. var dist = hullA.testSepAxis(Cross, hullB, posA, quatA, posB, quatB);
  6986. if(dist === false){
  6987. return false;
  6988. }
  6989. if(dist < dmin){
  6990. dmin = dist;
  6991. target.copy(Cross);
  6992. }
  6993. }
  6994. }
  6995. }
  6996. posB.vsub(posA,deltaC);
  6997. if((deltaC.dot(target))>0.0){
  6998. target.negate(target);
  6999. }
  7000. return true;
  7001. };
  7002. var maxminA=[], maxminB=[];
  7003. /**
  7004. * Test separating axis against two hulls. Both hulls are projected onto the axis and the overlap size is returned if there is one.
  7005. * @method testSepAxis
  7006. * @param {Vec3} axis
  7007. * @param {ConvexPolyhedron} hullB
  7008. * @param {Vec3} posA
  7009. * @param {Quaternion} quatA
  7010. * @param {Vec3} posB
  7011. * @param {Quaternion} quatB
  7012. * @return {number} The overlap depth, or FALSE if no penetration.
  7013. */
  7014. ConvexPolyhedron.prototype.testSepAxis = function(axis, hullB, posA, quatA, posB, quatB){
  7015. var hullA=this;
  7016. ConvexPolyhedron.project(hullA, axis, posA, quatA, maxminA);
  7017. ConvexPolyhedron.project(hullB, axis, posB, quatB, maxminB);
  7018. var maxA = maxminA[0];
  7019. var minA = maxminA[1];
  7020. var maxB = maxminB[0];
  7021. var minB = maxminB[1];
  7022. if(maxA<minB || maxB<minA){
  7023. return false; // Separated
  7024. }
  7025. var d0 = maxA - minB;
  7026. var d1 = maxB - minA;
  7027. var depth = d0<d1 ? d0:d1;
  7028. return depth;
  7029. };
  7030. var cli_aabbmin = new Vec3(),
  7031. cli_aabbmax = new Vec3();
  7032. /**
  7033. * @method calculateLocalInertia
  7034. * @param {Number} mass
  7035. * @param {Vec3} target
  7036. */
  7037. ConvexPolyhedron.prototype.calculateLocalInertia = function(mass,target){
  7038. // Approximate with box inertia
  7039. // Exact inertia calculation is overkill, but see http://geometrictools.com/Documentation/PolyhedralMassProperties.pdf for the correct way to do it
  7040. this.computeLocalAABB(cli_aabbmin,cli_aabbmax);
  7041. var x = cli_aabbmax.x - cli_aabbmin.x,
  7042. y = cli_aabbmax.y - cli_aabbmin.y,
  7043. z = cli_aabbmax.z - cli_aabbmin.z;
  7044. target.x = 1.0 / 12.0 * mass * ( 2*y*2*y + 2*z*2*z );
  7045. target.y = 1.0 / 12.0 * mass * ( 2*x*2*x + 2*z*2*z );
  7046. target.z = 1.0 / 12.0 * mass * ( 2*y*2*y + 2*x*2*x );
  7047. };
  7048. /**
  7049. * @method getPlaneConstantOfFace
  7050. * @param {Number} face_i Index of the face
  7051. * @return {Number}
  7052. */
  7053. ConvexPolyhedron.prototype.getPlaneConstantOfFace = function(face_i){
  7054. var f = this.faces[face_i];
  7055. var n = this.faceNormals[face_i];
  7056. var v = this.vertices[f[0]];
  7057. var c = -n.dot(v);
  7058. return c;
  7059. };
  7060. /**
  7061. * Clip a face against a hull.
  7062. * @method clipFaceAgainstHull
  7063. * @param {Vec3} separatingNormal
  7064. * @param {Vec3} posA
  7065. * @param {Quaternion} quatA
  7066. * @param {Array} worldVertsB1 An array of Vec3 with vertices in the world frame.
  7067. * @param {Number} minDist Distance clamping
  7068. * @param {Number} maxDist
  7069. * @param Array result Array to store resulting contact points in. Will be objects with properties: point, depth, normal. These are represented in world coordinates.
  7070. */
  7071. var cfah_faceANormalWS = new Vec3(),
  7072. cfah_edge0 = new Vec3(),
  7073. cfah_WorldEdge0 = new Vec3(),
  7074. cfah_worldPlaneAnormal1 = new Vec3(),
  7075. cfah_planeNormalWS1 = new Vec3(),
  7076. cfah_worldA1 = new Vec3(),
  7077. cfah_localPlaneNormal = new Vec3(),
  7078. cfah_planeNormalWS = new Vec3();
  7079. ConvexPolyhedron.prototype.clipFaceAgainstHull = function(separatingNormal, posA, quatA, worldVertsB1, minDist, maxDist,result){
  7080. var faceANormalWS = cfah_faceANormalWS,
  7081. edge0 = cfah_edge0,
  7082. WorldEdge0 = cfah_WorldEdge0,
  7083. worldPlaneAnormal1 = cfah_worldPlaneAnormal1,
  7084. planeNormalWS1 = cfah_planeNormalWS1,
  7085. worldA1 = cfah_worldA1,
  7086. localPlaneNormal = cfah_localPlaneNormal,
  7087. planeNormalWS = cfah_planeNormalWS;
  7088. var hullA = this;
  7089. var worldVertsB2 = [];
  7090. var pVtxIn = worldVertsB1;
  7091. var pVtxOut = worldVertsB2;
  7092. // Find the face with normal closest to the separating axis
  7093. var closestFaceA = -1;
  7094. var dmin = Number.MAX_VALUE;
  7095. for(var face=0; face<hullA.faces.length; face++){
  7096. faceANormalWS.copy(hullA.faceNormals[face]);
  7097. quatA.vmult(faceANormalWS,faceANormalWS);
  7098. //posA.vadd(faceANormalWS,faceANormalWS);
  7099. var d = faceANormalWS.dot(separatingNormal);
  7100. if (d < dmin){
  7101. dmin = d;
  7102. closestFaceA = face;
  7103. }
  7104. }
  7105. if (closestFaceA < 0){
  7106. // console.log("--- did not find any closest face... ---");
  7107. return;
  7108. }
  7109. //console.log("closest A: ",closestFaceA);
  7110. // Get the face and construct connected faces
  7111. var polyA = hullA.faces[closestFaceA];
  7112. polyA.connectedFaces = [];
  7113. for(var i=0; i<hullA.faces.length; i++){
  7114. for(var j=0; j<hullA.faces[i].length; j++){
  7115. if(polyA.indexOf(hullA.faces[i][j])!==-1 /* Sharing a vertex*/ && i!==closestFaceA /* Not the one we are looking for connections from */ && polyA.connectedFaces.indexOf(i)===-1 /* Not already added */ ){
  7116. polyA.connectedFaces.push(i);
  7117. }
  7118. }
  7119. }
  7120. // Clip the polygon to the back of the planes of all faces of hull A, that are adjacent to the witness face
  7121. var numContacts = pVtxIn.length;
  7122. var numVerticesA = polyA.length;
  7123. var res = [];
  7124. for(var e0=0; e0<numVerticesA; e0++){
  7125. var a = hullA.vertices[polyA[e0]];
  7126. var b = hullA.vertices[polyA[(e0+1)%numVerticesA]];
  7127. a.vsub(b,edge0);
  7128. WorldEdge0.copy(edge0);
  7129. quatA.vmult(WorldEdge0,WorldEdge0);
  7130. posA.vadd(WorldEdge0,WorldEdge0);
  7131. worldPlaneAnormal1.copy(this.faceNormals[closestFaceA]);//transA.getBasis()* btVector3(polyA.m_plane[0],polyA.m_plane[1],polyA.m_plane[2]);
  7132. quatA.vmult(worldPlaneAnormal1,worldPlaneAnormal1);
  7133. posA.vadd(worldPlaneAnormal1,worldPlaneAnormal1);
  7134. WorldEdge0.cross(worldPlaneAnormal1,planeNormalWS1);
  7135. planeNormalWS1.negate(planeNormalWS1);
  7136. worldA1.copy(a);
  7137. quatA.vmult(worldA1,worldA1);
  7138. posA.vadd(worldA1,worldA1);
  7139. var planeEqWS1 = -worldA1.dot(planeNormalWS1);
  7140. var planeEqWS;
  7141. if(true){
  7142. var otherFace = polyA.connectedFaces[e0];
  7143. localPlaneNormal.copy(this.faceNormals[otherFace]);
  7144. var localPlaneEq = this.getPlaneConstantOfFace(otherFace);
  7145. planeNormalWS.copy(localPlaneNormal);
  7146. quatA.vmult(planeNormalWS,planeNormalWS);
  7147. //posA.vadd(planeNormalWS,planeNormalWS);
  7148. var planeEqWS = localPlaneEq - planeNormalWS.dot(posA);
  7149. } else {
  7150. planeNormalWS.copy(planeNormalWS1);
  7151. planeEqWS = planeEqWS1;
  7152. }
  7153. // Clip face against our constructed plane
  7154. this.clipFaceAgainstPlane(pVtxIn, pVtxOut, planeNormalWS, planeEqWS);
  7155. // Throw away all clipped points, but save the reamining until next clip
  7156. while(pVtxIn.length){
  7157. pVtxIn.shift();
  7158. }
  7159. while(pVtxOut.length){
  7160. pVtxIn.push(pVtxOut.shift());
  7161. }
  7162. }
  7163. //console.log("Resulting points after clip:",pVtxIn);
  7164. // only keep contact points that are behind the witness face
  7165. localPlaneNormal.copy(this.faceNormals[closestFaceA]);
  7166. var localPlaneEq = this.getPlaneConstantOfFace(closestFaceA);
  7167. planeNormalWS.copy(localPlaneNormal);
  7168. quatA.vmult(planeNormalWS,planeNormalWS);
  7169. var planeEqWS = localPlaneEq - planeNormalWS.dot(posA);
  7170. for (var i=0; i<pVtxIn.length; i++){
  7171. var depth = planeNormalWS.dot(pVtxIn[i]) + planeEqWS; //???
  7172. /*console.log("depth calc from normal=",planeNormalWS.toString()," and constant "+planeEqWS+" and vertex ",pVtxIn[i].toString()," gives "+depth);*/
  7173. if (depth <=minDist){
  7174. console.log("clamped: depth="+depth+" to minDist="+(minDist+""));
  7175. depth = minDist;
  7176. }
  7177. if (depth <=maxDist){
  7178. var point = pVtxIn[i];
  7179. if(depth<=0){
  7180. /*console.log("Got contact point ",point.toString(),
  7181. ", depth=",depth,
  7182. "contact normal=",separatingNormal.toString(),
  7183. "plane",planeNormalWS.toString(),
  7184. "planeConstant",planeEqWS);*/
  7185. var p = {
  7186. point:point,
  7187. normal:planeNormalWS,
  7188. depth: depth,
  7189. };
  7190. result.push(p);
  7191. }
  7192. }
  7193. }
  7194. };
  7195. /**
  7196. * Clip a face in a hull against the back of a plane.
  7197. * @method clipFaceAgainstPlane
  7198. * @param {Array} inVertices
  7199. * @param {Array} outVertices
  7200. * @param {Vec3} planeNormal
  7201. * @param {Number} planeConstant The constant in the mathematical plane equation
  7202. */
  7203. ConvexPolyhedron.prototype.clipFaceAgainstPlane = function(inVertices,outVertices, planeNormal, planeConstant){
  7204. var n_dot_first, n_dot_last;
  7205. var numVerts = inVertices.length;
  7206. if(numVerts < 2){
  7207. return outVertices;
  7208. }
  7209. var firstVertex = inVertices[inVertices.length-1],
  7210. lastVertex = inVertices[0];
  7211. n_dot_first = planeNormal.dot(firstVertex) + planeConstant;
  7212. for(var vi = 0; vi < numVerts; vi++){
  7213. lastVertex = inVertices[vi];
  7214. n_dot_last = planeNormal.dot(lastVertex) + planeConstant;
  7215. if(n_dot_first < 0){
  7216. if(n_dot_last < 0){
  7217. // Start < 0, end < 0, so output lastVertex
  7218. var newv = new Vec3();
  7219. newv.copy(lastVertex);
  7220. outVertices.push(newv);
  7221. } else {
  7222. // Start < 0, end >= 0, so output intersection
  7223. var newv = new Vec3();
  7224. firstVertex.lerp(lastVertex,
  7225. n_dot_first / (n_dot_first - n_dot_last),
  7226. newv);
  7227. outVertices.push(newv);
  7228. }
  7229. } else {
  7230. if(n_dot_last<0){
  7231. // Start >= 0, end < 0 so output intersection and end
  7232. var newv = new Vec3();
  7233. firstVertex.lerp(lastVertex,
  7234. n_dot_first / (n_dot_first - n_dot_last),
  7235. newv);
  7236. outVertices.push(newv);
  7237. outVertices.push(lastVertex);
  7238. }
  7239. }
  7240. firstVertex = lastVertex;
  7241. n_dot_first = n_dot_last;
  7242. }
  7243. return outVertices;
  7244. };
  7245. // Updates .worldVertices and sets .worldVerticesNeedsUpdate to false.
  7246. ConvexPolyhedron.prototype.computeWorldVertices = function(position,quat){
  7247. var N = this.vertices.length;
  7248. while(this.worldVertices.length < N){
  7249. this.worldVertices.push( new Vec3() );
  7250. }
  7251. var verts = this.vertices,
  7252. worldVerts = this.worldVertices;
  7253. for(var i=0; i!==N; i++){
  7254. quat.vmult( verts[i] , worldVerts[i] );
  7255. position.vadd( worldVerts[i] , worldVerts[i] );
  7256. }
  7257. this.worldVerticesNeedsUpdate = false;
  7258. };
  7259. var computeLocalAABB_worldVert = new Vec3();
  7260. ConvexPolyhedron.prototype.computeLocalAABB = function(aabbmin,aabbmax){
  7261. var n = this.vertices.length,
  7262. vertices = this.vertices,
  7263. worldVert = computeLocalAABB_worldVert;
  7264. aabbmin.set(Number.MAX_VALUE, Number.MAX_VALUE, Number.MAX_VALUE);
  7265. aabbmax.set(-Number.MAX_VALUE, -Number.MAX_VALUE, -Number.MAX_VALUE);
  7266. for(var i=0; i<n; i++){
  7267. var v = vertices[i];
  7268. if (v.x < aabbmin.x){
  7269. aabbmin.x = v.x;
  7270. } else if(v.x > aabbmax.x){
  7271. aabbmax.x = v.x;
  7272. }
  7273. if (v.y < aabbmin.y){
  7274. aabbmin.y = v.y;
  7275. } else if(v.y > aabbmax.y){
  7276. aabbmax.y = v.y;
  7277. }
  7278. if (v.z < aabbmin.z){
  7279. aabbmin.z = v.z;
  7280. } else if(v.z > aabbmax.z){
  7281. aabbmax.z = v.z;
  7282. }
  7283. }
  7284. };
  7285. /**
  7286. * Updates .worldVertices and sets .worldVerticesNeedsUpdate to false.
  7287. * @method computeWorldFaceNormals
  7288. * @param {Quaternion} quat
  7289. */
  7290. ConvexPolyhedron.prototype.computeWorldFaceNormals = function(quat){
  7291. var N = this.faceNormals.length;
  7292. while(this.worldFaceNormals.length < N){
  7293. this.worldFaceNormals.push( new Vec3() );
  7294. }
  7295. var normals = this.faceNormals,
  7296. worldNormals = this.worldFaceNormals;
  7297. for(var i=0; i!==N; i++){
  7298. quat.vmult( normals[i] , worldNormals[i] );
  7299. }
  7300. this.worldFaceNormalsNeedsUpdate = false;
  7301. };
  7302. /**
  7303. * @method updateBoundingSphereRadius
  7304. */
  7305. ConvexPolyhedron.prototype.updateBoundingSphereRadius = function(){
  7306. // Assume points are distributed with local (0,0,0) as center
  7307. var max2 = 0;
  7308. var verts = this.vertices;
  7309. for(var i=0, N=verts.length; i!==N; i++) {
  7310. var norm2 = verts[i].norm2();
  7311. if(norm2 > max2){
  7312. max2 = norm2;
  7313. }
  7314. }
  7315. this.boundingSphereRadius = Math.sqrt(max2);
  7316. };
  7317. var tempWorldVertex = new Vec3();
  7318. /**
  7319. * @method calculateWorldAABB
  7320. * @param {Vec3} pos
  7321. * @param {Quaternion} quat
  7322. * @param {Vec3} min
  7323. * @param {Vec3} max
  7324. */
  7325. ConvexPolyhedron.prototype.calculateWorldAABB = function(pos,quat,min,max){
  7326. var n = this.vertices.length, verts = this.vertices;
  7327. var minx,miny,minz,maxx,maxy,maxz;
  7328. for(var i=0; i<n; i++){
  7329. tempWorldVertex.copy(verts[i]);
  7330. quat.vmult(tempWorldVertex,tempWorldVertex);
  7331. pos.vadd(tempWorldVertex,tempWorldVertex);
  7332. var v = tempWorldVertex;
  7333. if (v.x < minx || minx===undefined){
  7334. minx = v.x;
  7335. } else if(v.x > maxx || maxx===undefined){
  7336. maxx = v.x;
  7337. }
  7338. if (v.y < miny || miny===undefined){
  7339. miny = v.y;
  7340. } else if(v.y > maxy || maxy===undefined){
  7341. maxy = v.y;
  7342. }
  7343. if (v.z < minz || minz===undefined){
  7344. minz = v.z;
  7345. } else if(v.z > maxz || maxz===undefined){
  7346. maxz = v.z;
  7347. }
  7348. }
  7349. min.set(minx,miny,minz);
  7350. max.set(maxx,maxy,maxz);
  7351. };
  7352. /**
  7353. * Get approximate convex volume
  7354. * @method volume
  7355. * @return {Number}
  7356. */
  7357. ConvexPolyhedron.prototype.volume = function(){
  7358. return 4.0 * Math.PI * this.boundingSphereRadius / 3.0;
  7359. };
  7360. /**
  7361. * Get an average of all the vertices positions
  7362. * @method getAveragePointLocal
  7363. * @param {Vec3} target
  7364. * @return {Vec3}
  7365. */
  7366. ConvexPolyhedron.prototype.getAveragePointLocal = function(target){
  7367. target = target || new Vec3();
  7368. var n = this.vertices.length,
  7369. verts = this.vertices;
  7370. for(var i=0; i<n; i++){
  7371. target.vadd(verts[i],target);
  7372. }
  7373. target.mult(1/n,target);
  7374. return target;
  7375. };
  7376. /**
  7377. * Transform all local points. Will change the .vertices
  7378. * @method transformAllPoints
  7379. * @param {Vec3} offset
  7380. * @param {Quaternion} quat
  7381. */
  7382. ConvexPolyhedron.prototype.transformAllPoints = function(offset,quat){
  7383. var n = this.vertices.length,
  7384. verts = this.vertices;
  7385. // Apply rotation
  7386. if(quat){
  7387. // Rotate vertices
  7388. for(var i=0; i<n; i++){
  7389. var v = verts[i];
  7390. quat.vmult(v,v);
  7391. }
  7392. // Rotate face normals
  7393. for(var i=0; i<this.faceNormals.length; i++){
  7394. var v = this.faceNormals[i];
  7395. quat.vmult(v,v);
  7396. }
  7397. /*
  7398. // Rotate edges
  7399. for(var i=0; i<this.uniqueEdges.length; i++){
  7400. var v = this.uniqueEdges[i];
  7401. quat.vmult(v,v);
  7402. }*/
  7403. }
  7404. // Apply offset
  7405. if(offset){
  7406. for(var i=0; i<n; i++){
  7407. var v = verts[i];
  7408. v.vadd(offset,v);
  7409. }
  7410. }
  7411. };
  7412. /**
  7413. * Checks whether p is inside the polyhedra. Must be in local coords. The point lies outside of the convex hull of the other points if and only if the direction of all the vectors from it to those other points are on less than one half of a sphere around it.
  7414. * @method pointIsInside
  7415. * @param {Vec3} p A point given in local coordinates
  7416. * @return {Boolean}
  7417. */
  7418. var ConvexPolyhedron_pointIsInside = new Vec3();
  7419. var ConvexPolyhedron_vToP = new Vec3();
  7420. var ConvexPolyhedron_vToPointInside = new Vec3();
  7421. ConvexPolyhedron.prototype.pointIsInside = function(p){
  7422. var n = this.vertices.length,
  7423. verts = this.vertices,
  7424. faces = this.faces,
  7425. normals = this.faceNormals;
  7426. var positiveResult = null;
  7427. var N = this.faces.length;
  7428. var pointInside = ConvexPolyhedron_pointIsInside;
  7429. this.getAveragePointLocal(pointInside);
  7430. for(var i=0; i<N; i++){
  7431. var numVertices = this.faces[i].length;
  7432. var n = normals[i];
  7433. var v = verts[faces[i][0]]; // We only need one point in the face
  7434. // This dot product determines which side of the edge the point is
  7435. var vToP = ConvexPolyhedron_vToP;
  7436. p.vsub(v,vToP);
  7437. var r1 = n.dot(vToP);
  7438. var vToPointInside = ConvexPolyhedron_vToPointInside;
  7439. pointInside.vsub(v,vToPointInside);
  7440. var r2 = n.dot(vToPointInside);
  7441. if((r1<0 && r2>0) || (r1>0 && r2<0)){
  7442. return false; // Encountered some other sign. Exit.
  7443. } else {
  7444. }
  7445. }
  7446. // If we got here, all dot products were of the same sign.
  7447. return positiveResult ? 1 : -1;
  7448. };
  7449. /**
  7450. * Get max and min dot product of a convex hull at position (pos,quat) projected onto an axis. Results are saved in the array maxmin.
  7451. * @static
  7452. * @method project
  7453. * @param {ConvexPolyhedron} hull
  7454. * @param {Vec3} axis
  7455. * @param {Vec3} pos
  7456. * @param {Quaternion} quat
  7457. * @param {array} result result[0] and result[1] will be set to maximum and minimum, respectively.
  7458. */
  7459. var project_worldVertex = new Vec3();
  7460. var project_localAxis = new Vec3();
  7461. var project_localOrigin = new Vec3();
  7462. ConvexPolyhedron.project = function(hull, axis, pos, quat, result){
  7463. var n = hull.vertices.length,
  7464. worldVertex = project_worldVertex,
  7465. localAxis = project_localAxis,
  7466. max = 0,
  7467. min = 0,
  7468. localOrigin = project_localOrigin,
  7469. vs = hull.vertices;
  7470. localOrigin.setZero();
  7471. // Transform the axis to local
  7472. Transform.vectorToLocalFrame(pos, quat, axis, localAxis);
  7473. Transform.pointToLocalFrame(pos, quat, localOrigin, localOrigin);
  7474. var add = localOrigin.dot(localAxis);
  7475. min = max = vs[0].dot(localAxis);
  7476. for(var i = 1; i < n; i++){
  7477. var val = vs[i].dot(localAxis);
  7478. if(val > max){
  7479. max = val;
  7480. }
  7481. if(val < min){
  7482. min = val;
  7483. }
  7484. }
  7485. min -= add;
  7486. max -= add;
  7487. if(min > max){
  7488. // Inconsistent - swap
  7489. var temp = min;
  7490. min = max;
  7491. max = temp;
  7492. }
  7493. // Output
  7494. result[0] = max;
  7495. result[1] = min;
  7496. };
  7497. },{"../math/Quaternion":28,"../math/Transform":29,"../math/Vec3":30,"./Shape":43}],39:[function(_dereq_,module,exports){
  7498. module.exports = Cylinder;
  7499. var Shape = _dereq_('./Shape');
  7500. var Vec3 = _dereq_('../math/Vec3');
  7501. var Quaternion = _dereq_('../math/Quaternion');
  7502. var ConvexPolyhedron = _dereq_('./ConvexPolyhedron');
  7503. /**
  7504. * @class Cylinder
  7505. * @constructor
  7506. * @extends ConvexPolyhedron
  7507. * @author schteppe / https://github.com/schteppe
  7508. * @param {Number} radiusTop
  7509. * @param {Number} radiusBottom
  7510. * @param {Number} height
  7511. * @param {Number} numSegments The number of segments to build the cylinder out of
  7512. */
  7513. function Cylinder( radiusTop, radiusBottom, height , numSegments ) {
  7514. var N = numSegments,
  7515. verts = [],
  7516. axes = [],
  7517. faces = [],
  7518. bottomface = [],
  7519. topface = [],
  7520. cos = Math.cos,
  7521. sin = Math.sin;
  7522. // First bottom point
  7523. verts.push(new Vec3(radiusBottom*cos(0),
  7524. radiusBottom*sin(0),
  7525. -height*0.5));
  7526. bottomface.push(0);
  7527. // First top point
  7528. verts.push(new Vec3(radiusTop*cos(0),
  7529. radiusTop*sin(0),
  7530. height*0.5));
  7531. topface.push(1);
  7532. for(var i=0; i<N; i++){
  7533. var theta = 2*Math.PI/N * (i+1);
  7534. var thetaN = 2*Math.PI/N * (i+0.5);
  7535. if(i<N-1){
  7536. // Bottom
  7537. verts.push(new Vec3(radiusBottom*cos(theta),
  7538. radiusBottom*sin(theta),
  7539. -height*0.5));
  7540. bottomface.push(2*i+2);
  7541. // Top
  7542. verts.push(new Vec3(radiusTop*cos(theta),
  7543. radiusTop*sin(theta),
  7544. height*0.5));
  7545. topface.push(2*i+3);
  7546. // Face
  7547. faces.push([2*i+2, 2*i+3, 2*i+1,2*i]);
  7548. } else {
  7549. faces.push([0,1, 2*i+1, 2*i]); // Connect
  7550. }
  7551. // Axis: we can cut off half of them if we have even number of segments
  7552. if(N % 2 === 1 || i < N / 2){
  7553. axes.push(new Vec3(cos(thetaN), sin(thetaN), 0));
  7554. }
  7555. }
  7556. faces.push(topface);
  7557. axes.push(new Vec3(0,0,1));
  7558. // Reorder bottom face
  7559. var temp = [];
  7560. for(var i=0; i<bottomface.length; i++){
  7561. temp.push(bottomface[bottomface.length - i - 1]);
  7562. }
  7563. faces.push(temp);
  7564. this.type = Shape.types.CONVEXPOLYHEDRON;
  7565. ConvexPolyhedron.call( this, verts, faces, axes );
  7566. }
  7567. Cylinder.prototype = new ConvexPolyhedron();
  7568. },{"../math/Quaternion":28,"../math/Vec3":30,"./ConvexPolyhedron":38,"./Shape":43}],40:[function(_dereq_,module,exports){
  7569. var Shape = _dereq_('./Shape');
  7570. var ConvexPolyhedron = _dereq_('./ConvexPolyhedron');
  7571. var Vec3 = _dereq_('../math/Vec3');
  7572. var Utils = _dereq_('../utils/Utils');
  7573. module.exports = Heightfield;
  7574. /**
  7575. * Heightfield shape class. Height data is given as an array. These data points are spread out evenly with a given distance.
  7576. * @class Heightfield
  7577. * @extends Shape
  7578. * @constructor
  7579. * @param {Array} data An array of Y values that will be used to construct the terrain.
  7580. * @param {object} options
  7581. * @param {Number} [options.minValue] Minimum value of the data points in the data array. Will be computed automatically if not given.
  7582. * @param {Number} [options.maxValue] Maximum value.
  7583. * @param {Number} [options.elementSize=0.1] World spacing between the data points in X direction.
  7584. * @todo Should be possible to use along all axes, not just y
  7585. *
  7586. * @example
  7587. * // Generate some height data (y-values).
  7588. * var data = [];
  7589. * for(var i = 0; i < 1000; i++){
  7590. * var y = 0.5 * Math.cos(0.2 * i);
  7591. * data.push(y);
  7592. * }
  7593. *
  7594. * // Create the heightfield shape
  7595. * var heightfieldShape = new Heightfield(data, {
  7596. * elementSize: 1 // Distance between the data points in X and Y directions
  7597. * });
  7598. * var heightfieldBody = new Body();
  7599. * heightfieldBody.addShape(heightfieldShape);
  7600. * world.addBody(heightfieldBody);
  7601. */
  7602. function Heightfield(data, options){
  7603. options = Utils.defaults(options, {
  7604. maxValue : null,
  7605. minValue : null,
  7606. elementSize : 1
  7607. });
  7608. /**
  7609. * An array of numbers, or height values, that are spread out along the x axis.
  7610. * @property {array} data
  7611. */
  7612. this.data = data;
  7613. /**
  7614. * Max value of the data
  7615. * @property {number} maxValue
  7616. */
  7617. this.maxValue = options.maxValue;
  7618. /**
  7619. * Max value of the data
  7620. * @property {number} minValue
  7621. */
  7622. this.minValue = options.minValue;
  7623. /**
  7624. * The width of each element
  7625. * @property {number} elementSize
  7626. * @todo elementSizeX and Y
  7627. */
  7628. this.elementSize = options.elementSize;
  7629. if(options.minValue === null){
  7630. this.updateMinValue();
  7631. }
  7632. if(options.maxValue === null){
  7633. this.updateMaxValue();
  7634. }
  7635. this.cacheEnabled = true;
  7636. Shape.call(this);
  7637. this.pillarConvex = new ConvexPolyhedron();
  7638. this.pillarOffset = new Vec3();
  7639. this.type = Shape.types.HEIGHTFIELD;
  7640. this.updateBoundingSphereRadius();
  7641. // "i_j_isUpper" => { convex: ..., offset: ... }
  7642. // for example:
  7643. // _cachedPillars["0_2_1"]
  7644. this._cachedPillars = {};
  7645. }
  7646. Heightfield.prototype = new Shape();
  7647. /**
  7648. * Call whenever you change the data array.
  7649. * @method update
  7650. */
  7651. Heightfield.prototype.update = function(){
  7652. this._cachedPillars = {};
  7653. };
  7654. /**
  7655. * Update the .minValue property
  7656. * @method updateMinValue
  7657. */
  7658. Heightfield.prototype.updateMinValue = function(){
  7659. var data = this.data;
  7660. var minValue = data[0][0];
  7661. for(var i=0; i !== data.length; i++){
  7662. for(var j=0; j !== data[i].length; j++){
  7663. var v = data[i][j];
  7664. if(v < minValue){
  7665. minValue = v;
  7666. }
  7667. }
  7668. }
  7669. this.minValue = minValue;
  7670. };
  7671. /**
  7672. * Update the .maxValue property
  7673. * @method updateMaxValue
  7674. */
  7675. Heightfield.prototype.updateMaxValue = function(){
  7676. var data = this.data;
  7677. var maxValue = data[0][0];
  7678. for(var i=0; i !== data.length; i++){
  7679. for(var j=0; j !== data[i].length; j++){
  7680. var v = data[i][j];
  7681. if(v > maxValue){
  7682. maxValue = v;
  7683. }
  7684. }
  7685. }
  7686. this.maxValue = maxValue;
  7687. };
  7688. /**
  7689. * Set the height value at an index. Don't forget to update maxValue and minValue after you're done.
  7690. * @method setHeightValueAtIndex
  7691. * @param {integer} xi
  7692. * @param {integer} yi
  7693. * @param {number} value
  7694. */
  7695. Heightfield.prototype.setHeightValueAtIndex = function(xi, yi, value){
  7696. var data = this.data;
  7697. data[xi][yi] = value;
  7698. // Invalidate cache
  7699. this.clearCachedConvexTrianglePillar(xi, yi, false);
  7700. if(xi > 0){
  7701. this.clearCachedConvexTrianglePillar(xi - 1, yi, true);
  7702. this.clearCachedConvexTrianglePillar(xi - 1, yi, false);
  7703. }
  7704. if(yi > 0){
  7705. this.clearCachedConvexTrianglePillar(xi, yi - 1, true);
  7706. this.clearCachedConvexTrianglePillar(xi, yi - 1, false);
  7707. }
  7708. if(yi > 0 && xi > 0){
  7709. this.clearCachedConvexTrianglePillar(xi - 1, yi - 1, true);
  7710. }
  7711. };
  7712. /**
  7713. * Get max/min in a rectangle in the matrix data
  7714. * @method getRectMinMax
  7715. * @param {integer} iMinX
  7716. * @param {integer} iMinY
  7717. * @param {integer} iMaxX
  7718. * @param {integer} iMaxY
  7719. * @param {array} [result] An array to store the results in.
  7720. * @return {array} The result array, if it was passed in. Minimum will be at position 0 and max at 1.
  7721. */
  7722. Heightfield.prototype.getRectMinMax = function (iMinX, iMinY, iMaxX, iMaxY, result) {
  7723. result = result || [];
  7724. // Get max and min of the data
  7725. var data = this.data,
  7726. max = this.minValue; // Set first value
  7727. for(var i = iMinX; i <= iMaxX; i++){
  7728. for(var j = iMinY; j <= iMaxY; j++){
  7729. var height = data[i][j];
  7730. if(height > max){
  7731. max = height;
  7732. }
  7733. }
  7734. }
  7735. result[0] = this.minValue;
  7736. result[1] = max;
  7737. };
  7738. /**
  7739. * Get the index of a local position on the heightfield. The indexes indicate the rectangles, so if your terrain is made of N x N height data points, you will have rectangle indexes ranging from 0 to N-1.
  7740. * @method getIndexOfPosition
  7741. * @param {number} x
  7742. * @param {number} y
  7743. * @param {array} result Two-element array
  7744. * @param {boolean} clamp If the position should be clamped to the heightfield edge.
  7745. * @return {boolean}
  7746. */
  7747. Heightfield.prototype.getIndexOfPosition = function (x, y, result, clamp) {
  7748. // Get the index of the data points to test against
  7749. var w = this.elementSize;
  7750. var data = this.data;
  7751. var xi = Math.floor(x / w);
  7752. var yi = Math.floor(y / w);
  7753. result[0] = xi;
  7754. result[1] = yi;
  7755. if(clamp){
  7756. // Clamp index to edges
  7757. if(xi < 0){ xi = 0; }
  7758. if(yi < 0){ yi = 0; }
  7759. if(xi >= data.length - 1){ xi = data.length - 1; }
  7760. if(yi >= data[0].length - 1){ yi = data[0].length - 1; }
  7761. }
  7762. // Bail out if we are out of the terrain
  7763. if(xi < 0 || yi < 0 || xi >= data.length-1 || yi >= data[0].length-1){
  7764. return false;
  7765. }
  7766. return true;
  7767. };
  7768. Heightfield.prototype.getHeightAt = function(x, y, edgeClamp){
  7769. var idx = [];
  7770. this.getIndexOfPosition(x, y, idx, edgeClamp);
  7771. // TODO: get upper or lower triangle, then use barycentric interpolation to get the height in the triangle.
  7772. var minmax = [];
  7773. this.getRectMinMax(idx[0], idx[1] + 1, idx[0], idx[1] + 1, minmax);
  7774. return (minmax[0] + minmax[1]) / 2; // average
  7775. };
  7776. Heightfield.prototype.getCacheConvexTrianglePillarKey = function(xi, yi, getUpperTriangle){
  7777. return xi + '_' + yi + '_' + (getUpperTriangle ? 1 : 0);
  7778. };
  7779. Heightfield.prototype.getCachedConvexTrianglePillar = function(xi, yi, getUpperTriangle){
  7780. return this._cachedPillars[this.getCacheConvexTrianglePillarKey(xi, yi, getUpperTriangle)];
  7781. };
  7782. Heightfield.prototype.setCachedConvexTrianglePillar = function(xi, yi, getUpperTriangle, convex, offset){
  7783. this._cachedPillars[this.getCacheConvexTrianglePillarKey(xi, yi, getUpperTriangle)] = {
  7784. convex: convex,
  7785. offset: offset
  7786. };
  7787. };
  7788. Heightfield.prototype.clearCachedConvexTrianglePillar = function(xi, yi, getUpperTriangle){
  7789. delete this._cachedPillars[this.getCacheConvexTrianglePillarKey(xi, yi, getUpperTriangle)];
  7790. };
  7791. /**
  7792. * Get a triangle in the terrain in the form of a triangular convex shape.
  7793. * @method getConvexTrianglePillar
  7794. * @param {integer} i
  7795. * @param {integer} j
  7796. * @param {boolean} getUpperTriangle
  7797. */
  7798. Heightfield.prototype.getConvexTrianglePillar = function(xi, yi, getUpperTriangle){
  7799. var result = this.pillarConvex;
  7800. var offsetResult = this.pillarOffset;
  7801. if(this.cacheEnabled){
  7802. var data = this.getCachedConvexTrianglePillar(xi, yi, getUpperTriangle);
  7803. if(data){
  7804. this.pillarConvex = data.convex;
  7805. this.pillarOffset = data.offset;
  7806. return;
  7807. }
  7808. result = new ConvexPolyhedron();
  7809. offsetResult = new Vec3();
  7810. this.pillarConvex = result;
  7811. this.pillarOffset = offsetResult;
  7812. }
  7813. var data = this.data;
  7814. var elementSize = this.elementSize;
  7815. var faces = result.faces;
  7816. // Reuse verts if possible
  7817. result.vertices.length = 6;
  7818. for (var i = 0; i < 6; i++) {
  7819. if(!result.vertices[i]){
  7820. result.vertices[i] = new Vec3();
  7821. }
  7822. }
  7823. // Reuse faces if possible
  7824. faces.length = 5;
  7825. for (var i = 0; i < 5; i++) {
  7826. if(!faces[i]){
  7827. faces[i] = [];
  7828. }
  7829. }
  7830. var verts = result.vertices;
  7831. var h = (Math.min(
  7832. data[xi][yi],
  7833. data[xi+1][yi],
  7834. data[xi][yi+1],
  7835. data[xi+1][yi+1]
  7836. ) - this.minValue ) / 2 + this.minValue;
  7837. if (!getUpperTriangle) {
  7838. // Center of the triangle pillar - all polygons are given relative to this one
  7839. offsetResult.set(
  7840. (xi + 0.25) * elementSize, // sort of center of a triangle
  7841. (yi + 0.25) * elementSize,
  7842. h // vertical center
  7843. );
  7844. // Top triangle verts
  7845. verts[0].set(
  7846. -0.25 * elementSize,
  7847. -0.25 * elementSize,
  7848. data[xi][yi] - h
  7849. );
  7850. verts[1].set(
  7851. 0.75 * elementSize,
  7852. -0.25 * elementSize,
  7853. data[xi + 1][yi] - h
  7854. );
  7855. verts[2].set(
  7856. -0.25 * elementSize,
  7857. 0.75 * elementSize,
  7858. data[xi][yi + 1] - h
  7859. );
  7860. // bottom triangle verts
  7861. verts[3].set(
  7862. -0.25 * elementSize,
  7863. -0.25 * elementSize,
  7864. -h-1
  7865. );
  7866. verts[4].set(
  7867. 0.75 * elementSize,
  7868. -0.25 * elementSize,
  7869. -h-1
  7870. );
  7871. verts[5].set(
  7872. -0.25 * elementSize,
  7873. 0.75 * elementSize,
  7874. -h-1
  7875. );
  7876. // top triangle
  7877. faces[0][0] = 0;
  7878. faces[0][1] = 1;
  7879. faces[0][2] = 2;
  7880. // bottom triangle
  7881. faces[1][0] = 5;
  7882. faces[1][1] = 4;
  7883. faces[1][2] = 3;
  7884. // -x facing quad
  7885. faces[2][0] = 0;
  7886. faces[2][1] = 2;
  7887. faces[2][2] = 5;
  7888. faces[2][3] = 3;
  7889. // -y facing quad
  7890. faces[3][0] = 1;
  7891. faces[3][1] = 0;
  7892. faces[3][2] = 3;
  7893. faces[3][3] = 4;
  7894. // +xy facing quad
  7895. faces[4][0] = 4;
  7896. faces[4][1] = 5;
  7897. faces[4][2] = 2;
  7898. faces[4][3] = 1;
  7899. } else {
  7900. // Center of the triangle pillar - all polygons are given relative to this one
  7901. offsetResult.set(
  7902. (xi + 0.75) * elementSize, // sort of center of a triangle
  7903. (yi + 0.75) * elementSize,
  7904. h // vertical center
  7905. );
  7906. // Top triangle verts
  7907. verts[0].set(
  7908. 0.25 * elementSize,
  7909. 0.25 * elementSize,
  7910. data[xi + 1][yi + 1] - h
  7911. );
  7912. verts[1].set(
  7913. -0.75 * elementSize,
  7914. 0.25 * elementSize,
  7915. data[xi][yi + 1] - h
  7916. );
  7917. verts[2].set(
  7918. 0.25 * elementSize,
  7919. -0.75 * elementSize,
  7920. data[xi + 1][yi] - h
  7921. );
  7922. // bottom triangle verts
  7923. verts[3].set(
  7924. 0.25 * elementSize,
  7925. 0.25 * elementSize,
  7926. - h-1
  7927. );
  7928. verts[4].set(
  7929. -0.75 * elementSize,
  7930. 0.25 * elementSize,
  7931. - h-1
  7932. );
  7933. verts[5].set(
  7934. 0.25 * elementSize,
  7935. -0.75 * elementSize,
  7936. - h-1
  7937. );
  7938. // Top triangle
  7939. faces[0][0] = 0;
  7940. faces[0][1] = 1;
  7941. faces[0][2] = 2;
  7942. // bottom triangle
  7943. faces[1][0] = 5;
  7944. faces[1][1] = 4;
  7945. faces[1][2] = 3;
  7946. // +x facing quad
  7947. faces[2][0] = 2;
  7948. faces[2][1] = 5;
  7949. faces[2][2] = 3;
  7950. faces[2][3] = 0;
  7951. // +y facing quad
  7952. faces[3][0] = 3;
  7953. faces[3][1] = 4;
  7954. faces[3][2] = 1;
  7955. faces[3][3] = 0;
  7956. // -xy facing quad
  7957. faces[4][0] = 1;
  7958. faces[4][1] = 4;
  7959. faces[4][2] = 5;
  7960. faces[4][3] = 2;
  7961. }
  7962. result.computeNormals();
  7963. result.computeEdges();
  7964. result.updateBoundingSphereRadius();
  7965. this.setCachedConvexTrianglePillar(xi, yi, getUpperTriangle, result, offsetResult);
  7966. };
  7967. Heightfield.prototype.calculateLocalInertia = function(mass, target){
  7968. target = target || new Vec3();
  7969. target.set(0, 0, 0);
  7970. return target;
  7971. };
  7972. Heightfield.prototype.volume = function(){
  7973. return Number.MAX_VALUE; // The terrain is infinite
  7974. };
  7975. Heightfield.prototype.calculateWorldAABB = function(pos, quat, min, max){
  7976. // TODO: do it properly
  7977. min.set(-Number.MAX_VALUE, -Number.MAX_VALUE, -Number.MAX_VALUE);
  7978. max.set(Number.MAX_VALUE, Number.MAX_VALUE, Number.MAX_VALUE);
  7979. };
  7980. Heightfield.prototype.updateBoundingSphereRadius = function(){
  7981. // Use the bounding box of the min/max values
  7982. var data = this.data,
  7983. s = this.elementSize;
  7984. this.boundingSphereRadius = new Vec3(data.length * s, data[0].length * s, Math.max(Math.abs(this.maxValue), Math.abs(this.minValue))).norm();
  7985. };
  7986. },{"../math/Vec3":30,"../utils/Utils":53,"./ConvexPolyhedron":38,"./Shape":43}],41:[function(_dereq_,module,exports){
  7987. module.exports = Particle;
  7988. var Shape = _dereq_('./Shape');
  7989. var Vec3 = _dereq_('../math/Vec3');
  7990. /**
  7991. * Particle shape.
  7992. * @class Particle
  7993. * @constructor
  7994. * @author schteppe
  7995. * @extends Shape
  7996. */
  7997. function Particle(){
  7998. Shape.call(this);
  7999. this.type = Shape.types.PARTICLE;
  8000. }
  8001. Particle.prototype = new Shape();
  8002. Particle.prototype.constructor = Particle;
  8003. /**
  8004. * @method calculateLocalInertia
  8005. * @param {Number} mass
  8006. * @param {Vec3} target
  8007. * @return {Vec3}
  8008. */
  8009. Particle.prototype.calculateLocalInertia = function(mass,target){
  8010. target = target || new Vec3();
  8011. target.set(0, 0, 0);
  8012. return target;
  8013. };
  8014. Particle.prototype.volume = function(){
  8015. return 0;
  8016. };
  8017. Particle.prototype.updateBoundingSphereRadius = function(){
  8018. this.boundingSphereRadius = 0;
  8019. };
  8020. Particle.prototype.calculateWorldAABB = function(pos,quat,min,max){
  8021. // Get each axis max
  8022. min.copy(pos);
  8023. max.copy(pos);
  8024. };
  8025. },{"../math/Vec3":30,"./Shape":43}],42:[function(_dereq_,module,exports){
  8026. module.exports = Plane;
  8027. var Shape = _dereq_('./Shape');
  8028. var Vec3 = _dereq_('../math/Vec3');
  8029. /**
  8030. * A plane, facing in the Z direction. The plane has its surface at z=0 and everything below z=0 is assumed to be solid plane. To make the plane face in some other direction than z, you must put it inside a RigidBody and rotate that body. See the demos.
  8031. * @class Plane
  8032. * @constructor
  8033. * @extends Shape
  8034. * @author schteppe
  8035. */
  8036. function Plane(){
  8037. Shape.call(this);
  8038. this.type = Shape.types.PLANE;
  8039. // World oriented normal
  8040. this.worldNormal = new Vec3();
  8041. this.worldNormalNeedsUpdate = true;
  8042. this.boundingSphereRadius = Number.MAX_VALUE;
  8043. }
  8044. Plane.prototype = new Shape();
  8045. Plane.prototype.constructor = Plane;
  8046. Plane.prototype.computeWorldNormal = function(quat){
  8047. var n = this.worldNormal;
  8048. n.set(0,0,1);
  8049. quat.vmult(n,n);
  8050. this.worldNormalNeedsUpdate = false;
  8051. };
  8052. Plane.prototype.calculateLocalInertia = function(mass,target){
  8053. target = target || new Vec3();
  8054. return target;
  8055. };
  8056. Plane.prototype.volume = function(){
  8057. return Number.MAX_VALUE; // The plane is infinite...
  8058. };
  8059. var tempNormal = new Vec3();
  8060. Plane.prototype.calculateWorldAABB = function(pos, quat, min, max){
  8061. // The plane AABB is infinite, except if the normal is pointing along any axis
  8062. tempNormal.set(0,0,1); // Default plane normal is z
  8063. quat.vmult(tempNormal,tempNormal);
  8064. var maxVal = Number.MAX_VALUE;
  8065. min.set(-maxVal, -maxVal, -maxVal);
  8066. max.set(maxVal, maxVal, maxVal);
  8067. if(tempNormal.x === 1){ max.x = pos.x; }
  8068. if(tempNormal.y === 1){ max.y = pos.y; }
  8069. if(tempNormal.z === 1){ max.z = pos.z; }
  8070. if(tempNormal.x === -1){ min.x = pos.x; }
  8071. if(tempNormal.y === -1){ min.y = pos.y; }
  8072. if(tempNormal.z === -1){ min.z = pos.z; }
  8073. };
  8074. Plane.prototype.updateBoundingSphereRadius = function(){
  8075. this.boundingSphereRadius = Number.MAX_VALUE;
  8076. };
  8077. },{"../math/Vec3":30,"./Shape":43}],43:[function(_dereq_,module,exports){
  8078. module.exports = Shape;
  8079. var Shape = _dereq_('./Shape');
  8080. var Vec3 = _dereq_('../math/Vec3');
  8081. var Quaternion = _dereq_('../math/Quaternion');
  8082. var Material = _dereq_('../material/Material');
  8083. /**
  8084. * Base class for shapes
  8085. * @class Shape
  8086. * @constructor
  8087. * @author schteppe
  8088. * @todo Should have a mechanism for caching bounding sphere radius instead of calculating it each time
  8089. */
  8090. function Shape(){
  8091. /**
  8092. * Identifyer of the Shape.
  8093. * @property {number} id
  8094. */
  8095. this.id = Shape.idCounter++;
  8096. /**
  8097. * The type of this shape. Must be set to an int > 0 by subclasses.
  8098. * @property type
  8099. * @type {Number}
  8100. * @see Shape.types
  8101. */
  8102. this.type = 0;
  8103. /**
  8104. * The local bounding sphere radius of this shape.
  8105. * @property {Number} boundingSphereRadius
  8106. */
  8107. this.boundingSphereRadius = 0;
  8108. /**
  8109. * Whether to produce contact forces when in contact with other bodies. Note that contacts will be generated, but they will be disabled.
  8110. * @property {boolean} collisionResponse
  8111. */
  8112. this.collisionResponse = true;
  8113. /**
  8114. * @property {Material} material
  8115. */
  8116. this.material = null;
  8117. }
  8118. Shape.prototype.constructor = Shape;
  8119. /**
  8120. * Computes the bounding sphere radius. The result is stored in the property .boundingSphereRadius
  8121. * @method updateBoundingSphereRadius
  8122. * @return {Number}
  8123. */
  8124. Shape.prototype.updateBoundingSphereRadius = function(){
  8125. throw "computeBoundingSphereRadius() not implemented for shape type "+this.type;
  8126. };
  8127. /**
  8128. * Get the volume of this shape
  8129. * @method volume
  8130. * @return {Number}
  8131. */
  8132. Shape.prototype.volume = function(){
  8133. throw "volume() not implemented for shape type "+this.type;
  8134. };
  8135. /**
  8136. * Calculates the inertia in the local frame for this shape.
  8137. * @method calculateLocalInertia
  8138. * @return {Vec3}
  8139. * @see http://en.wikipedia.org/wiki/List_of_moments_of_inertia
  8140. */
  8141. Shape.prototype.calculateLocalInertia = function(mass,target){
  8142. throw "calculateLocalInertia() not implemented for shape type "+this.type;
  8143. };
  8144. Shape.idCounter = 0;
  8145. /**
  8146. * The available shape types.
  8147. * @static
  8148. * @property types
  8149. * @type {Object}
  8150. */
  8151. Shape.types = {
  8152. SPHERE:1,
  8153. PLANE:2,
  8154. BOX:4,
  8155. COMPOUND:8,
  8156. CONVEXPOLYHEDRON:16,
  8157. HEIGHTFIELD:32,
  8158. PARTICLE:64,
  8159. CYLINDER:128,
  8160. TRIMESH:256
  8161. };
  8162. },{"../material/Material":25,"../math/Quaternion":28,"../math/Vec3":30,"./Shape":43}],44:[function(_dereq_,module,exports){
  8163. module.exports = Sphere;
  8164. var Shape = _dereq_('./Shape');
  8165. var Vec3 = _dereq_('../math/Vec3');
  8166. /**
  8167. * Spherical shape
  8168. * @class Sphere
  8169. * @constructor
  8170. * @extends Shape
  8171. * @param {Number} radius The radius of the sphere, a non-negative number.
  8172. * @author schteppe / http://github.com/schteppe
  8173. */
  8174. function Sphere(radius){
  8175. Shape.call(this);
  8176. /**
  8177. * @property {Number} radius
  8178. */
  8179. this.radius = radius!==undefined ? Number(radius) : 1.0;
  8180. this.type = Shape.types.SPHERE;
  8181. if(this.radius < 0){
  8182. throw new Error('The sphere radius cannot be negative.');
  8183. }
  8184. this.updateBoundingSphereRadius();
  8185. }
  8186. Sphere.prototype = new Shape();
  8187. Sphere.prototype.constructor = Sphere;
  8188. Sphere.prototype.calculateLocalInertia = function(mass,target){
  8189. target = target || new Vec3();
  8190. var I = 2.0*mass*this.radius*this.radius/5.0;
  8191. target.x = I;
  8192. target.y = I;
  8193. target.z = I;
  8194. return target;
  8195. };
  8196. Sphere.prototype.volume = function(){
  8197. return 4.0 * Math.PI * this.radius / 3.0;
  8198. };
  8199. Sphere.prototype.updateBoundingSphereRadius = function(){
  8200. this.boundingSphereRadius = this.radius;
  8201. };
  8202. Sphere.prototype.calculateWorldAABB = function(pos,quat,min,max){
  8203. var r = this.radius;
  8204. var axes = ['x','y','z'];
  8205. for(var i=0; i<axes.length; i++){
  8206. var ax = axes[i];
  8207. min[ax] = pos[ax] - r;
  8208. max[ax] = pos[ax] + r;
  8209. }
  8210. };
  8211. },{"../math/Vec3":30,"./Shape":43}],45:[function(_dereq_,module,exports){
  8212. module.exports = Trimesh;
  8213. var Shape = _dereq_('./Shape');
  8214. var Vec3 = _dereq_('../math/Vec3');
  8215. var Quaternion = _dereq_('../math/Quaternion');
  8216. var Transform = _dereq_('../math/Transform');
  8217. var AABB = _dereq_('../collision/AABB');
  8218. var Octree = _dereq_('../utils/Octree');
  8219. /**
  8220. * @class Trimesh
  8221. * @constructor
  8222. * @param {array} vertices
  8223. * @param {array} indices
  8224. * @extends Shape
  8225. * @example
  8226. * // How to make a mesh with a single triangle
  8227. * var vertices = [
  8228. * 0, 0, 0, // vertex 0
  8229. * 1, 0, 0, // vertex 1
  8230. * 0, 1, 0 // vertex 2
  8231. * ];
  8232. * var indices = [
  8233. * 0, 1, 2 // triangle 0
  8234. * ];
  8235. * var trimeshShape = new Trimesh(vertices, indices);
  8236. */
  8237. function Trimesh(vertices, indices) {
  8238. Shape.call(this);
  8239. this.type = Shape.types.TRIMESH;
  8240. /**
  8241. * @property vertices
  8242. * @type {Array}
  8243. */
  8244. this.vertices = new Float32Array(vertices);
  8245. /**
  8246. * Array of integers, indicating which vertices each triangle consists of. The length of this array is thus 3 times the number of triangles.
  8247. * @property indices
  8248. * @type {Array}
  8249. */
  8250. this.indices = new Int16Array(indices);
  8251. /**
  8252. * The normals data.
  8253. * @property normals
  8254. * @type {Array}
  8255. */
  8256. this.normals = new Float32Array(indices.length);
  8257. /**
  8258. * The local AABB of the mesh.
  8259. * @property aabb
  8260. * @type {Array}
  8261. */
  8262. this.aabb = new AABB();
  8263. /**
  8264. * References to vertex pairs, making up all unique edges in the trimesh.
  8265. * @property {array} edges
  8266. */
  8267. this.edges = null;
  8268. /**
  8269. * Local scaling of the mesh. Use .setScale() to set it.
  8270. * @property {Vec3} scale
  8271. */
  8272. this.scale = new Vec3(1, 1, 1);
  8273. /**
  8274. * The indexed triangles. Use .updateTree() to update it.
  8275. * @property {Octree} tree
  8276. */
  8277. this.tree = new Octree();
  8278. this.updateEdges();
  8279. this.updateNormals();
  8280. this.updateAABB();
  8281. this.updateBoundingSphereRadius();
  8282. this.updateTree();
  8283. }
  8284. Trimesh.prototype = new Shape();
  8285. Trimesh.prototype.constructor = Trimesh;
  8286. var computeNormals_n = new Vec3();
  8287. /**
  8288. * @method updateTree
  8289. */
  8290. Trimesh.prototype.updateTree = function(){
  8291. var tree = this.tree;
  8292. tree.reset();
  8293. tree.aabb.copy(this.aabb);
  8294. var scale = this.scale; // The local mesh AABB is scaled, but the octree AABB should be unscaled
  8295. tree.aabb.lowerBound.x *= 1 / scale.x;
  8296. tree.aabb.lowerBound.y *= 1 / scale.y;
  8297. tree.aabb.lowerBound.z *= 1 / scale.z;
  8298. tree.aabb.upperBound.x *= 1 / scale.x;
  8299. tree.aabb.upperBound.y *= 1 / scale.y;
  8300. tree.aabb.upperBound.z *= 1 / scale.z;
  8301. // Insert all triangles
  8302. var triangleAABB = new AABB();
  8303. var a = new Vec3();
  8304. var b = new Vec3();
  8305. var c = new Vec3();
  8306. var points = [a, b, c];
  8307. for (var i = 0; i < this.indices.length / 3; i++) {
  8308. //this.getTriangleVertices(i, a, b, c);
  8309. // Get unscaled triangle verts
  8310. var i3 = i * 3;
  8311. this._getUnscaledVertex(this.indices[i3], a);
  8312. this._getUnscaledVertex(this.indices[i3 + 1], b);
  8313. this._getUnscaledVertex(this.indices[i3 + 2], c);
  8314. triangleAABB.setFromPoints(points);
  8315. tree.insert(triangleAABB, i);
  8316. }
  8317. tree.removeEmptyNodes();
  8318. };
  8319. var unscaledAABB = new AABB();
  8320. /**
  8321. * Get triangles in a local AABB from the trimesh.
  8322. * @method getTrianglesInAABB
  8323. * @param {AABB} aabb
  8324. * @param {array} result An array of integers, referencing the queried triangles.
  8325. */
  8326. Trimesh.prototype.getTrianglesInAABB = function(aabb, result){
  8327. unscaledAABB.copy(aabb);
  8328. // Scale it to local
  8329. var scale = this.scale;
  8330. var isx = scale.x;
  8331. var isy = scale.y;
  8332. var isz = scale.z;
  8333. var l = unscaledAABB.lowerBound;
  8334. var u = unscaledAABB.upperBound;
  8335. l.x /= isx;
  8336. l.y /= isy;
  8337. l.z /= isz;
  8338. u.x /= isx;
  8339. u.y /= isy;
  8340. u.z /= isz;
  8341. return this.tree.aabbQuery(unscaledAABB, result);
  8342. };
  8343. /**
  8344. * @method setScale
  8345. * @param {Vec3} scale
  8346. */
  8347. Trimesh.prototype.setScale = function(scale){
  8348. var wasUniform = this.scale.x === this.scale.y === this.scale.z;
  8349. var isUniform = scale.x === scale.y === scale.z;
  8350. if(!(wasUniform && isUniform)){
  8351. // Non-uniform scaling. Need to update normals.
  8352. this.updateNormals();
  8353. }
  8354. this.scale.copy(scale);
  8355. this.updateAABB();
  8356. this.updateBoundingSphereRadius();
  8357. };
  8358. /**
  8359. * Compute the normals of the faces. Will save in the .normals array.
  8360. * @method updateNormals
  8361. */
  8362. Trimesh.prototype.updateNormals = function(){
  8363. var n = computeNormals_n;
  8364. // Generate normals
  8365. var normals = this.normals;
  8366. for(var i=0; i < this.indices.length / 3; i++){
  8367. var i3 = i * 3;
  8368. var a = this.indices[i3],
  8369. b = this.indices[i3 + 1],
  8370. c = this.indices[i3 + 2];
  8371. this.getVertex(a, va);
  8372. this.getVertex(b, vb);
  8373. this.getVertex(c, vc);
  8374. Trimesh.computeNormal(vb, va, vc, n);
  8375. normals[i3] = n.x;
  8376. normals[i3 + 1] = n.y;
  8377. normals[i3 + 2] = n.z;
  8378. }
  8379. };
  8380. /**
  8381. * Update the .edges property
  8382. * @method updateEdges
  8383. */
  8384. Trimesh.prototype.updateEdges = function(){
  8385. var edges = {};
  8386. var add = function(indexA, indexB){
  8387. var key = a < b ? a + '_' + b : b + '_' + a;
  8388. edges[key] = true;
  8389. };
  8390. for(var i=0; i < this.indices.length / 3; i++){
  8391. var i3 = i * 3;
  8392. var a = this.indices[i3],
  8393. b = this.indices[i3 + 1],
  8394. c = this.indices[i3 + 2];
  8395. add(a,b);
  8396. add(b,c);
  8397. add(c,a);
  8398. }
  8399. var keys = Object.keys(edges);
  8400. this.edges = new Int16Array(keys.length * 2);
  8401. for (var i = 0; i < keys.length; i++) {
  8402. var indices = keys[i].split('_');
  8403. this.edges[2 * i] = parseInt(indices[0], 10);
  8404. this.edges[2 * i + 1] = parseInt(indices[1], 10);
  8405. }
  8406. };
  8407. /**
  8408. * Get an edge vertex
  8409. * @method getEdgeVertex
  8410. * @param {number} edgeIndex
  8411. * @param {number} firstOrSecond 0 or 1, depending on which one of the vertices you need.
  8412. * @param {Vec3} vertexStore Where to store the result
  8413. */
  8414. Trimesh.prototype.getEdgeVertex = function(edgeIndex, firstOrSecond, vertexStore){
  8415. var vertexIndex = this.edges[edgeIndex * 2 + (firstOrSecond ? 1 : 0)];
  8416. this.getVertex(vertexIndex, vertexStore);
  8417. };
  8418. var getEdgeVector_va = new Vec3();
  8419. var getEdgeVector_vb = new Vec3();
  8420. /**
  8421. * Get a vector along an edge.
  8422. * @method getEdgeVector
  8423. * @param {number} edgeIndex
  8424. * @param {Vec3} vectorStore
  8425. */
  8426. Trimesh.prototype.getEdgeVector = function(edgeIndex, vectorStore){
  8427. var va = getEdgeVector_va;
  8428. var vb = getEdgeVector_vb;
  8429. this.getEdgeVertex(edgeIndex, 0, va);
  8430. this.getEdgeVertex(edgeIndex, 1, vb);
  8431. vb.vsub(va, vectorStore);
  8432. };
  8433. /**
  8434. * Get face normal given 3 vertices
  8435. * @static
  8436. * @method computeNormal
  8437. * @param {Vec3} va
  8438. * @param {Vec3} vb
  8439. * @param {Vec3} vc
  8440. * @param {Vec3} target
  8441. */
  8442. var cb = new Vec3();
  8443. var ab = new Vec3();
  8444. Trimesh.computeNormal = function ( va, vb, vc, target ) {
  8445. vb.vsub(va,ab);
  8446. vc.vsub(vb,cb);
  8447. cb.cross(ab,target);
  8448. if ( !target.isZero() ) {
  8449. target.normalize();
  8450. }
  8451. };
  8452. var va = new Vec3();
  8453. var vb = new Vec3();
  8454. var vc = new Vec3();
  8455. /**
  8456. * Get vertex i.
  8457. * @method getVertex
  8458. * @param {number} i
  8459. * @param {Vec3} out
  8460. * @return {Vec3} The "out" vector object
  8461. */
  8462. Trimesh.prototype.getVertex = function(i, out){
  8463. var scale = this.scale;
  8464. this._getUnscaledVertex(i, out);
  8465. out.x *= scale.x;
  8466. out.y *= scale.y;
  8467. out.z *= scale.z;
  8468. return out;
  8469. };
  8470. /**
  8471. * Get raw vertex i
  8472. * @private
  8473. * @method _getUnscaledVertex
  8474. * @param {number} i
  8475. * @param {Vec3} out
  8476. * @return {Vec3} The "out" vector object
  8477. */
  8478. Trimesh.prototype._getUnscaledVertex = function(i, out){
  8479. var i3 = i * 3;
  8480. var vertices = this.vertices;
  8481. return out.set(
  8482. vertices[i3],
  8483. vertices[i3 + 1],
  8484. vertices[i3 + 2]
  8485. );
  8486. };
  8487. /**
  8488. * Get a vertex from the trimesh,transformed by the given position and quaternion.
  8489. * @method getWorldVertex
  8490. * @param {number} i
  8491. * @param {Vec3} pos
  8492. * @param {Quaternion} quat
  8493. * @param {Vec3} out
  8494. * @return {Vec3} The "out" vector object
  8495. */
  8496. Trimesh.prototype.getWorldVertex = function(i, pos, quat, out){
  8497. this.getVertex(i, out);
  8498. Transform.pointToWorldFrame(pos, quat, out, out);
  8499. return out;
  8500. };
  8501. /**
  8502. * Get the three vertices for triangle i.
  8503. * @method getTriangleVertices
  8504. * @param {number} i
  8505. * @param {Vec3} a
  8506. * @param {Vec3} b
  8507. * @param {Vec3} c
  8508. */
  8509. Trimesh.prototype.getTriangleVertices = function(i, a, b, c){
  8510. var i3 = i * 3;
  8511. this.getVertex(this.indices[i3], a);
  8512. this.getVertex(this.indices[i3 + 1], b);
  8513. this.getVertex(this.indices[i3 + 2], c);
  8514. };
  8515. /**
  8516. * Compute the normal of triangle i.
  8517. * @method getNormal
  8518. * @param {Number} i
  8519. * @param {Vec3} target
  8520. * @return {Vec3} The "target" vector object
  8521. */
  8522. Trimesh.prototype.getNormal = function(i, target){
  8523. var i3 = i * 3;
  8524. return target.set(
  8525. this.normals[i3],
  8526. this.normals[i3 + 1],
  8527. this.normals[i3 + 2]
  8528. );
  8529. };
  8530. var cli_aabb = new AABB();
  8531. /**
  8532. * @method calculateLocalInertia
  8533. * @param {Number} mass
  8534. * @param {Vec3} target
  8535. * @return {Vec3} The "target" vector object
  8536. */
  8537. Trimesh.prototype.calculateLocalInertia = function(mass,target){
  8538. // Approximate with box inertia
  8539. // Exact inertia calculation is overkill, but see http://geometrictools.com/Documentation/PolyhedralMassProperties.pdf for the correct way to do it
  8540. this.computeLocalAABB(cli_aabb);
  8541. var x = cli_aabb.upperBound.x - cli_aabb.lowerBound.x,
  8542. y = cli_aabb.upperBound.y - cli_aabb.lowerBound.y,
  8543. z = cli_aabb.upperBound.z - cli_aabb.lowerBound.z;
  8544. return target.set(
  8545. 1.0 / 12.0 * mass * ( 2*y*2*y + 2*z*2*z ),
  8546. 1.0 / 12.0 * mass * ( 2*x*2*x + 2*z*2*z ),
  8547. 1.0 / 12.0 * mass * ( 2*y*2*y + 2*x*2*x )
  8548. );
  8549. };
  8550. var computeLocalAABB_worldVert = new Vec3();
  8551. /**
  8552. * Compute the local AABB for the trimesh
  8553. * @method computeLocalAABB
  8554. * @param {AABB} aabb
  8555. */
  8556. Trimesh.prototype.computeLocalAABB = function(aabb){
  8557. var l = aabb.lowerBound,
  8558. u = aabb.upperBound,
  8559. n = this.vertices.length,
  8560. vertices = this.vertices,
  8561. v = computeLocalAABB_worldVert;
  8562. this.getVertex(0, v);
  8563. l.copy(v);
  8564. u.copy(v);
  8565. for(var i=0; i !== n; i++){
  8566. this.getVertex(i, v);
  8567. if(v.x < l.x){
  8568. l.x = v.x;
  8569. } else if(v.x > u.x){
  8570. u.x = v.x;
  8571. }
  8572. if(v.y < l.y){
  8573. l.y = v.y;
  8574. } else if(v.y > u.y){
  8575. u.y = v.y;
  8576. }
  8577. if(v.z < l.z){
  8578. l.z = v.z;
  8579. } else if(v.z > u.z){
  8580. u.z = v.z;
  8581. }
  8582. }
  8583. };
  8584. /**
  8585. * Update the .aabb property
  8586. * @method updateAABB
  8587. */
  8588. Trimesh.prototype.updateAABB = function(){
  8589. this.computeLocalAABB(this.aabb);
  8590. };
  8591. /**
  8592. * Will update the .boundingSphereRadius property
  8593. * @method updateBoundingSphereRadius
  8594. */
  8595. Trimesh.prototype.updateBoundingSphereRadius = function(){
  8596. // Assume points are distributed with local (0,0,0) as center
  8597. var max2 = 0;
  8598. var vertices = this.vertices;
  8599. var v = new Vec3();
  8600. for(var i=0, N=vertices.length / 3; i !== N; i++) {
  8601. this.getVertex(i, v);
  8602. var norm2 = v.norm2();
  8603. if(norm2 > max2){
  8604. max2 = norm2;
  8605. }
  8606. }
  8607. this.boundingSphereRadius = Math.sqrt(max2);
  8608. };
  8609. var tempWorldVertex = new Vec3();
  8610. var calculateWorldAABB_frame = new Transform();
  8611. var calculateWorldAABB_aabb = new AABB();
  8612. /**
  8613. * @method calculateWorldAABB
  8614. * @param {Vec3} pos
  8615. * @param {Quaternion} quat
  8616. * @param {Vec3} min
  8617. * @param {Vec3} max
  8618. */
  8619. Trimesh.prototype.calculateWorldAABB = function(pos,quat,min,max){
  8620. /*
  8621. var n = this.vertices.length / 3,
  8622. verts = this.vertices;
  8623. var minx,miny,minz,maxx,maxy,maxz;
  8624. var v = tempWorldVertex;
  8625. for(var i=0; i<n; i++){
  8626. this.getVertex(i, v);
  8627. quat.vmult(v, v);
  8628. pos.vadd(v, v);
  8629. if (v.x < minx || minx===undefined){
  8630. minx = v.x;
  8631. } else if(v.x > maxx || maxx===undefined){
  8632. maxx = v.x;
  8633. }
  8634. if (v.y < miny || miny===undefined){
  8635. miny = v.y;
  8636. } else if(v.y > maxy || maxy===undefined){
  8637. maxy = v.y;
  8638. }
  8639. if (v.z < minz || minz===undefined){
  8640. minz = v.z;
  8641. } else if(v.z > maxz || maxz===undefined){
  8642. maxz = v.z;
  8643. }
  8644. }
  8645. min.set(minx,miny,minz);
  8646. max.set(maxx,maxy,maxz);
  8647. */
  8648. // Faster approximation using local AABB
  8649. var frame = calculateWorldAABB_frame;
  8650. var result = calculateWorldAABB_aabb;
  8651. frame.position = pos;
  8652. frame.quaternion = quat;
  8653. this.aabb.toWorldFrame(frame, result);
  8654. min.copy(result.lowerBound);
  8655. max.copy(result.upperBound);
  8656. };
  8657. /**
  8658. * Get approximate volume
  8659. * @method volume
  8660. * @return {Number}
  8661. */
  8662. Trimesh.prototype.volume = function(){
  8663. return 4.0 * Math.PI * this.boundingSphereRadius / 3.0;
  8664. };
  8665. /**
  8666. * Create a Trimesh instance, shaped as a torus.
  8667. * @static
  8668. * @method createTorus
  8669. * @param {number} [radius=1]
  8670. * @param {number} [tube=0.5]
  8671. * @param {number} [radialSegments=8]
  8672. * @param {number} [tubularSegments=6]
  8673. * @param {number} [arc=6.283185307179586]
  8674. * @return {Trimesh} A torus
  8675. */
  8676. Trimesh.createTorus = function (radius, tube, radialSegments, tubularSegments, arc) {
  8677. radius = radius || 1;
  8678. tube = tube || 0.5;
  8679. radialSegments = radialSegments || 8;
  8680. tubularSegments = tubularSegments || 6;
  8681. arc = arc || Math.PI * 2;
  8682. var vertices = [];
  8683. var indices = [];
  8684. for ( var j = 0; j <= radialSegments; j ++ ) {
  8685. for ( var i = 0; i <= tubularSegments; i ++ ) {
  8686. var u = i / tubularSegments * arc;
  8687. var v = j / radialSegments * Math.PI * 2;
  8688. var x = ( radius + tube * Math.cos( v ) ) * Math.cos( u );
  8689. var y = ( radius + tube * Math.cos( v ) ) * Math.sin( u );
  8690. var z = tube * Math.sin( v );
  8691. vertices.push( x, y, z );
  8692. }
  8693. }
  8694. for ( var j = 1; j <= radialSegments; j ++ ) {
  8695. for ( var i = 1; i <= tubularSegments; i ++ ) {
  8696. var a = ( tubularSegments + 1 ) * j + i - 1;
  8697. var b = ( tubularSegments + 1 ) * ( j - 1 ) + i - 1;
  8698. var c = ( tubularSegments + 1 ) * ( j - 1 ) + i;
  8699. var d = ( tubularSegments + 1 ) * j + i;
  8700. indices.push(a, b, d);
  8701. indices.push(b, c, d);
  8702. }
  8703. }
  8704. return new Trimesh(vertices, indices);
  8705. };
  8706. },{"../collision/AABB":3,"../math/Quaternion":28,"../math/Transform":29,"../math/Vec3":30,"../utils/Octree":50,"./Shape":43}],46:[function(_dereq_,module,exports){
  8707. module.exports = GSSolver;
  8708. var Vec3 = _dereq_('../math/Vec3');
  8709. var Quaternion = _dereq_('../math/Quaternion');
  8710. var Solver = _dereq_('./Solver');
  8711. /**
  8712. * Constraint equation Gauss-Seidel solver.
  8713. * @class GSSolver
  8714. * @constructor
  8715. * @todo The spook parameters should be specified for each constraint, not globally.
  8716. * @author schteppe / https://github.com/schteppe
  8717. * @see https://www8.cs.umu.se/kurser/5DV058/VT09/lectures/spooknotes.pdf
  8718. * @extends Solver
  8719. */
  8720. function GSSolver(){
  8721. Solver.call(this);
  8722. /**
  8723. * The number of solver iterations determines quality of the constraints in the world. The more iterations, the more correct simulation. More iterations need more computations though. If you have a large gravity force in your world, you will need more iterations.
  8724. * @property iterations
  8725. * @type {Number}
  8726. * @todo write more about solver and iterations in the wiki
  8727. */
  8728. this.iterations = 10;
  8729. /**
  8730. * When tolerance is reached, the system is assumed to be converged.
  8731. * @property tolerance
  8732. * @type {Number}
  8733. */
  8734. this.tolerance = 1e-7;
  8735. }
  8736. GSSolver.prototype = new Solver();
  8737. var GSSolver_solve_lambda = []; // Just temporary number holders that we want to reuse each solve.
  8738. var GSSolver_solve_invCs = [];
  8739. var GSSolver_solve_Bs = [];
  8740. GSSolver.prototype.solve = function(dt,world){
  8741. var iter = 0,
  8742. maxIter = this.iterations,
  8743. tolSquared = this.tolerance*this.tolerance,
  8744. equations = this.equations,
  8745. Neq = equations.length,
  8746. bodies = world.bodies,
  8747. Nbodies = bodies.length,
  8748. h = dt,
  8749. q, B, invC, deltalambda, deltalambdaTot, GWlambda, lambdaj;
  8750. // Update solve mass
  8751. if(Neq !== 0){
  8752. for(var i=0; i!==Nbodies; i++){
  8753. bodies[i].updateSolveMassProperties();
  8754. }
  8755. }
  8756. // Things that does not change during iteration can be computed once
  8757. var invCs = GSSolver_solve_invCs,
  8758. Bs = GSSolver_solve_Bs,
  8759. lambda = GSSolver_solve_lambda;
  8760. invCs.length = Neq;
  8761. Bs.length = Neq;
  8762. lambda.length = Neq;
  8763. for(var i=0; i!==Neq; i++){
  8764. var c = equations[i];
  8765. lambda[i] = 0.0;
  8766. Bs[i] = c.computeB(h);
  8767. invCs[i] = 1.0 / c.computeC();
  8768. }
  8769. if(Neq !== 0){
  8770. // Reset vlambda
  8771. for(var i=0; i!==Nbodies; i++){
  8772. var b=bodies[i],
  8773. vlambda=b.vlambda,
  8774. wlambda=b.wlambda;
  8775. vlambda.set(0,0,0);
  8776. if(wlambda){
  8777. wlambda.set(0,0,0);
  8778. }
  8779. }
  8780. // Iterate over equations
  8781. for(iter=0; iter!==maxIter; iter++){
  8782. // Accumulate the total error for each iteration.
  8783. deltalambdaTot = 0.0;
  8784. for(var j=0; j!==Neq; j++){
  8785. var c = equations[j];
  8786. // Compute iteration
  8787. B = Bs[j];
  8788. invC = invCs[j];
  8789. lambdaj = lambda[j];
  8790. GWlambda = c.computeGWlambda();
  8791. deltalambda = invC * ( B - GWlambda - c.eps * lambdaj );
  8792. // Clamp if we are not within the min/max interval
  8793. if(lambdaj + deltalambda < c.minForce){
  8794. deltalambda = c.minForce - lambdaj;
  8795. } else if(lambdaj + deltalambda > c.maxForce){
  8796. deltalambda = c.maxForce - lambdaj;
  8797. }
  8798. lambda[j] += deltalambda;
  8799. deltalambdaTot += deltalambda > 0.0 ? deltalambda : -deltalambda; // abs(deltalambda)
  8800. c.addToWlambda(deltalambda);
  8801. }
  8802. // If the total error is small enough - stop iterate
  8803. if(deltalambdaTot*deltalambdaTot < tolSquared){
  8804. break;
  8805. }
  8806. }
  8807. // Add result to velocity
  8808. for(var i=0; i!==Nbodies; i++){
  8809. var b=bodies[i],
  8810. v=b.velocity,
  8811. w=b.angularVelocity;
  8812. v.vadd(b.vlambda, v);
  8813. if(w){
  8814. w.vadd(b.wlambda, w);
  8815. }
  8816. }
  8817. }
  8818. return iter;
  8819. };
  8820. },{"../math/Quaternion":28,"../math/Vec3":30,"./Solver":47}],47:[function(_dereq_,module,exports){
  8821. module.exports = Solver;
  8822. /**
  8823. * Constraint equation solver base class.
  8824. * @class Solver
  8825. * @constructor
  8826. * @author schteppe / https://github.com/schteppe
  8827. */
  8828. function Solver(){
  8829. /**
  8830. * All equations to be solved
  8831. * @property {Array} equations
  8832. */
  8833. this.equations = [];
  8834. }
  8835. /**
  8836. * Should be implemented in subclasses!
  8837. * @method solve
  8838. * @param {Number} dt
  8839. * @param {World} world
  8840. */
  8841. Solver.prototype.solve = function(dt,world){
  8842. // Should return the number of iterations done!
  8843. return 0;
  8844. };
  8845. /**
  8846. * Add an equation
  8847. * @method addEquation
  8848. * @param {Equation} eq
  8849. */
  8850. Solver.prototype.addEquation = function(eq){
  8851. if (eq.enabled) {
  8852. this.equations.push(eq);
  8853. }
  8854. };
  8855. /**
  8856. * Remove an equation
  8857. * @method removeEquation
  8858. * @param {Equation} eq
  8859. */
  8860. Solver.prototype.removeEquation = function(eq){
  8861. var eqs = this.equations;
  8862. var i = eqs.indexOf(eq);
  8863. if(i !== -1){
  8864. eqs.splice(i,1);
  8865. }
  8866. };
  8867. /**
  8868. * Add all equations
  8869. * @method removeAllEquations
  8870. */
  8871. Solver.prototype.removeAllEquations = function(){
  8872. this.equations.length = 0;
  8873. };
  8874. },{}],48:[function(_dereq_,module,exports){
  8875. module.exports = SplitSolver;
  8876. var Vec3 = _dereq_('../math/Vec3');
  8877. var Quaternion = _dereq_('../math/Quaternion');
  8878. var Solver = _dereq_('./Solver');
  8879. var Body = _dereq_('../objects/Body');
  8880. /**
  8881. * Splits the equations into islands and solves them independently. Can improve performance.
  8882. * @class SplitSolver
  8883. * @constructor
  8884. * @extends Solver
  8885. * @param {Solver} subsolver
  8886. */
  8887. function SplitSolver(subsolver){
  8888. Solver.call(this);
  8889. this.iterations = 10;
  8890. this.tolerance = 1e-7;
  8891. this.subsolver = subsolver;
  8892. this.nodes = [];
  8893. this.nodePool = [];
  8894. // Create needed nodes, reuse if possible
  8895. while(this.nodePool.length < 128){
  8896. this.nodePool.push(this.createNode());
  8897. }
  8898. }
  8899. SplitSolver.prototype = new Solver();
  8900. // Returns the number of subsystems
  8901. var SplitSolver_solve_nodes = []; // All allocated node objects
  8902. var SplitSolver_solve_nodePool = []; // All allocated node objects
  8903. var SplitSolver_solve_eqs = []; // Temp array
  8904. var SplitSolver_solve_bds = []; // Temp array
  8905. var SplitSolver_solve_dummyWorld = {bodies:[]}; // Temp object
  8906. var STATIC = Body.STATIC;
  8907. function getUnvisitedNode(nodes){
  8908. var Nnodes = nodes.length;
  8909. for(var i=0; i!==Nnodes; i++){
  8910. var node = nodes[i];
  8911. if(!node.visited && !(node.body.type & STATIC)){
  8912. return node;
  8913. }
  8914. }
  8915. return false;
  8916. }
  8917. var queue = [];
  8918. function bfs(root,visitFunc,bds,eqs){
  8919. queue.push(root);
  8920. root.visited = true;
  8921. visitFunc(root,bds,eqs);
  8922. while(queue.length) {
  8923. var node = queue.pop();
  8924. // Loop over unvisited child nodes
  8925. var child;
  8926. while((child = getUnvisitedNode(node.children))) {
  8927. child.visited = true;
  8928. visitFunc(child,bds,eqs);
  8929. queue.push(child);
  8930. }
  8931. }
  8932. }
  8933. function visitFunc(node,bds,eqs){
  8934. bds.push(node.body);
  8935. var Neqs = node.eqs.length;
  8936. for(var i=0; i!==Neqs; i++){
  8937. var eq = node.eqs[i];
  8938. if(eqs.indexOf(eq) === -1){
  8939. eqs.push(eq);
  8940. }
  8941. }
  8942. }
  8943. SplitSolver.prototype.createNode = function(){
  8944. return { body:null, children:[], eqs:[], visited:false };
  8945. };
  8946. /**
  8947. * Solve the subsystems
  8948. * @method solve
  8949. * @param {Number} dt
  8950. * @param {World} world
  8951. */
  8952. SplitSolver.prototype.solve = function(dt,world){
  8953. var nodes=SplitSolver_solve_nodes,
  8954. nodePool=this.nodePool,
  8955. bodies=world.bodies,
  8956. equations=this.equations,
  8957. Neq=equations.length,
  8958. Nbodies=bodies.length,
  8959. subsolver=this.subsolver;
  8960. // Create needed nodes, reuse if possible
  8961. while(nodePool.length < Nbodies){
  8962. nodePool.push(this.createNode());
  8963. }
  8964. nodes.length = Nbodies;
  8965. for (var i = 0; i < Nbodies; i++) {
  8966. nodes[i] = nodePool[i];
  8967. }
  8968. // Reset node values
  8969. for(var i=0; i!==Nbodies; i++){
  8970. var node = nodes[i];
  8971. node.body = bodies[i];
  8972. node.children.length = 0;
  8973. node.eqs.length = 0;
  8974. node.visited = false;
  8975. }
  8976. for(var k=0; k!==Neq; k++){
  8977. var eq=equations[k],
  8978. i=bodies.indexOf(eq.bi),
  8979. j=bodies.indexOf(eq.bj),
  8980. ni=nodes[i],
  8981. nj=nodes[j];
  8982. ni.children.push(nj);
  8983. ni.eqs.push(eq);
  8984. nj.children.push(ni);
  8985. nj.eqs.push(eq);
  8986. }
  8987. var child, n=0, eqs=SplitSolver_solve_eqs;
  8988. subsolver.tolerance = this.tolerance;
  8989. subsolver.iterations = this.iterations;
  8990. var dummyWorld = SplitSolver_solve_dummyWorld;
  8991. while((child = getUnvisitedNode(nodes))){
  8992. eqs.length = 0;
  8993. dummyWorld.bodies.length = 0;
  8994. bfs(child, visitFunc, dummyWorld.bodies, eqs);
  8995. var Neqs = eqs.length;
  8996. eqs = eqs.sort(sortById);
  8997. for(var i=0; i!==Neqs; i++){
  8998. subsolver.addEquation(eqs[i]);
  8999. }
  9000. var iter = subsolver.solve(dt,dummyWorld);
  9001. subsolver.removeAllEquations();
  9002. n++;
  9003. }
  9004. return n;
  9005. };
  9006. function sortById(a, b){
  9007. return b.id - a.id;
  9008. }
  9009. },{"../math/Quaternion":28,"../math/Vec3":30,"../objects/Body":31,"./Solver":47}],49:[function(_dereq_,module,exports){
  9010. /**
  9011. * Base class for objects that dispatches events.
  9012. * @class EventTarget
  9013. * @constructor
  9014. */
  9015. var EventTarget = function () {
  9016. };
  9017. module.exports = EventTarget;
  9018. EventTarget.prototype = {
  9019. constructor: EventTarget,
  9020. /**
  9021. * Add an event listener
  9022. * @method addEventListener
  9023. * @param {String} type
  9024. * @param {Function} listener
  9025. * @return {EventTarget} The self object, for chainability.
  9026. */
  9027. addEventListener: function ( type, listener ) {
  9028. if ( this._listeners === undefined ){ this._listeners = {}; }
  9029. var listeners = this._listeners;
  9030. if ( listeners[ type ] === undefined ) {
  9031. listeners[ type ] = [];
  9032. }
  9033. if ( listeners[ type ].indexOf( listener ) === - 1 ) {
  9034. listeners[ type ].push( listener );
  9035. }
  9036. return this;
  9037. },
  9038. /**
  9039. * Check if an event listener is added
  9040. * @method hasEventListener
  9041. * @param {String} type
  9042. * @param {Function} listener
  9043. * @return {Boolean}
  9044. */
  9045. hasEventListener: function ( type, listener ) {
  9046. if ( this._listeners === undefined ){ return false; }
  9047. var listeners = this._listeners;
  9048. if ( listeners[ type ] !== undefined && listeners[ type ].indexOf( listener ) !== - 1 ) {
  9049. return true;
  9050. }
  9051. return false;
  9052. },
  9053. /**
  9054. * Remove an event listener
  9055. * @method removeEventListener
  9056. * @param {String} type
  9057. * @param {Function} listener
  9058. * @return {EventTarget} The self object, for chainability.
  9059. */
  9060. removeEventListener: function ( type, listener ) {
  9061. if ( this._listeners === undefined ){ return this; }
  9062. var listeners = this._listeners;
  9063. if ( listeners[type] === undefined ){ return this; }
  9064. var index = listeners[ type ].indexOf( listener );
  9065. if ( index !== - 1 ) {
  9066. listeners[ type ].splice( index, 1 );
  9067. }
  9068. return this;
  9069. },
  9070. /**
  9071. * Emit an event.
  9072. * @method dispatchEvent
  9073. * @param {Object} event
  9074. * @param {String} event.type
  9075. * @return {EventTarget} The self object, for chainability.
  9076. */
  9077. dispatchEvent: function ( event ) {
  9078. if ( this._listeners === undefined ){ return this; }
  9079. var listeners = this._listeners;
  9080. var listenerArray = listeners[ event.type ];
  9081. if ( listenerArray !== undefined ) {
  9082. event.target = this;
  9083. for ( var i = 0, l = listenerArray.length; i < l; i ++ ) {
  9084. listenerArray[ i ].call( this, event );
  9085. }
  9086. }
  9087. return this;
  9088. }
  9089. };
  9090. },{}],50:[function(_dereq_,module,exports){
  9091. var AABB = _dereq_('../collision/AABB');
  9092. var Vec3 = _dereq_('../math/Vec3');
  9093. module.exports = Octree;
  9094. /**
  9095. * @class OctreeNode
  9096. * @param {object} [options]
  9097. * @param {Octree} [options.root]
  9098. * @param {AABB} [options.aabb]
  9099. */
  9100. function OctreeNode(options){
  9101. options = options || {};
  9102. /**
  9103. * The root node
  9104. * @property {OctreeNode} root
  9105. */
  9106. this.root = options.root || null;
  9107. /**
  9108. * Boundary of this node
  9109. * @property {AABB} aabb
  9110. */
  9111. this.aabb = options.aabb ? options.aabb.clone() : new AABB();
  9112. /**
  9113. * Contained data at the current node level.
  9114. * @property {Array} data
  9115. */
  9116. this.data = [];
  9117. /**
  9118. * Children to this node
  9119. * @property {Array} children
  9120. */
  9121. this.children = [];
  9122. }
  9123. /**
  9124. * @class Octree
  9125. * @param {AABB} aabb The total AABB of the tree
  9126. * @param {object} [options]
  9127. * @param {number} [options.maxDepth=8]
  9128. * @extends OctreeNode
  9129. */
  9130. function Octree(aabb, options){
  9131. options = options || {};
  9132. options.root = null;
  9133. options.aabb = aabb;
  9134. OctreeNode.call(this, options);
  9135. /**
  9136. * Maximum subdivision depth
  9137. * @property {number} maxDepth
  9138. */
  9139. this.maxDepth = typeof(options.maxDepth) !== 'undefined' ? options.maxDepth : 8;
  9140. }
  9141. Octree.prototype = new OctreeNode();
  9142. OctreeNode.prototype.reset = function(aabb, options){
  9143. this.children.length = this.data.length = 0;
  9144. };
  9145. /**
  9146. * Insert data into this node
  9147. * @method insert
  9148. * @param {AABB} aabb
  9149. * @param {object} elementData
  9150. * @return {boolean} True if successful, otherwise false
  9151. */
  9152. OctreeNode.prototype.insert = function(aabb, elementData, level){
  9153. var nodeData = this.data;
  9154. level = level || 0;
  9155. // Ignore objects that do not belong in this node
  9156. if (!this.aabb.contains(aabb)){
  9157. return false; // object cannot be added
  9158. }
  9159. var children = this.children;
  9160. if(level < (this.maxDepth || this.root.maxDepth)){
  9161. // Subdivide if there are no children yet
  9162. var subdivided = false;
  9163. if (!children.length){
  9164. this.subdivide();
  9165. subdivided = true;
  9166. }
  9167. // add to whichever node will accept it
  9168. for (var i = 0; i !== 8; i++) {
  9169. if (children[i].insert(aabb, elementData, level + 1)){
  9170. return true;
  9171. }
  9172. }
  9173. if(subdivided){
  9174. // No children accepted! Might as well just remove em since they contain none
  9175. children.length = 0;
  9176. }
  9177. }
  9178. // Too deep, or children didnt want it. add it in current node
  9179. nodeData.push(elementData);
  9180. return true;
  9181. };
  9182. var halfDiagonal = new Vec3();
  9183. /**
  9184. * Create 8 equally sized children nodes and put them in the .children array.
  9185. * @method subdivide
  9186. */
  9187. OctreeNode.prototype.subdivide = function() {
  9188. var aabb = this.aabb;
  9189. var l = aabb.lowerBound;
  9190. var u = aabb.upperBound;
  9191. var children = this.children;
  9192. children.push(
  9193. new OctreeNode({ aabb: new AABB({ lowerBound: new Vec3(0,0,0) }) }),
  9194. new OctreeNode({ aabb: new AABB({ lowerBound: new Vec3(1,0,0) }) }),
  9195. new OctreeNode({ aabb: new AABB({ lowerBound: new Vec3(1,1,0) }) }),
  9196. new OctreeNode({ aabb: new AABB({ lowerBound: new Vec3(1,1,1) }) }),
  9197. new OctreeNode({ aabb: new AABB({ lowerBound: new Vec3(0,1,1) }) }),
  9198. new OctreeNode({ aabb: new AABB({ lowerBound: new Vec3(0,0,1) }) }),
  9199. new OctreeNode({ aabb: new AABB({ lowerBound: new Vec3(1,0,1) }) }),
  9200. new OctreeNode({ aabb: new AABB({ lowerBound: new Vec3(0,1,0) }) })
  9201. );
  9202. u.vsub(l, halfDiagonal);
  9203. halfDiagonal.scale(0.5, halfDiagonal);
  9204. var root = this.root || this;
  9205. for (var i = 0; i !== 8; i++) {
  9206. var child = children[i];
  9207. // Set current node as root
  9208. child.root = root;
  9209. // Compute bounds
  9210. var lowerBound = child.aabb.lowerBound;
  9211. lowerBound.x *= halfDiagonal.x;
  9212. lowerBound.y *= halfDiagonal.y;
  9213. lowerBound.z *= halfDiagonal.z;
  9214. lowerBound.vadd(l, lowerBound);
  9215. // Upper bound is always lower bound + halfDiagonal
  9216. lowerBound.vadd(halfDiagonal, child.aabb.upperBound);
  9217. }
  9218. };
  9219. /**
  9220. * Get all data, potentially within an AABB
  9221. * @method aabbQuery
  9222. * @param {AABB} aabb
  9223. * @param {array} result
  9224. * @return {array} The "result" object
  9225. */
  9226. OctreeNode.prototype.aabbQuery = function(aabb, result) {
  9227. var nodeData = this.data;
  9228. // abort if the range does not intersect this node
  9229. // if (!this.aabb.overlaps(aabb)){
  9230. // return result;
  9231. // }
  9232. // Add objects at this level
  9233. // Array.prototype.push.apply(result, nodeData);
  9234. // Add child data
  9235. // @todo unwrap recursion into a queue / loop, that's faster in JS
  9236. var children = this.children;
  9237. // for (var i = 0, N = this.children.length; i !== N; i++) {
  9238. // children[i].aabbQuery(aabb, result);
  9239. // }
  9240. var queue = [this];
  9241. while (queue.length) {
  9242. var node = queue.pop();
  9243. if (node.aabb.overlaps(aabb)){
  9244. Array.prototype.push.apply(result, node.data);
  9245. }
  9246. Array.prototype.push.apply(queue, node.children);
  9247. }
  9248. return result;
  9249. };
  9250. var tmpAABB = new AABB();
  9251. /**
  9252. * Get all data, potentially intersected by a ray.
  9253. * @method rayQuery
  9254. * @param {Ray} ray
  9255. * @param {Transform} treeTransform
  9256. * @param {array} result
  9257. * @return {array} The "result" object
  9258. */
  9259. OctreeNode.prototype.rayQuery = function(ray, treeTransform, result) {
  9260. // Use aabb query for now.
  9261. // @todo implement real ray query which needs less lookups
  9262. ray.getAABB(tmpAABB);
  9263. tmpAABB.toLocalFrame(treeTransform, tmpAABB);
  9264. this.aabbQuery(tmpAABB, result);
  9265. return result;
  9266. };
  9267. /**
  9268. * @method removeEmptyNodes
  9269. */
  9270. OctreeNode.prototype.removeEmptyNodes = function() {
  9271. var queue = [this];
  9272. while (queue.length) {
  9273. var node = queue.pop();
  9274. for (var i = node.children.length - 1; i >= 0; i--) {
  9275. if(!node.children[i].data.length){
  9276. node.children.splice(i, 1);
  9277. }
  9278. }
  9279. Array.prototype.push.apply(queue, node.children);
  9280. }
  9281. };
  9282. },{"../collision/AABB":3,"../math/Vec3":30}],51:[function(_dereq_,module,exports){
  9283. module.exports = Pool;
  9284. /**
  9285. * For pooling objects that can be reused.
  9286. * @class Pool
  9287. * @constructor
  9288. */
  9289. function Pool(){
  9290. /**
  9291. * The pooled objects
  9292. * @property {Array} objects
  9293. */
  9294. this.objects = [];
  9295. /**
  9296. * Constructor of the objects
  9297. * @property {mixed} type
  9298. */
  9299. this.type = Object;
  9300. }
  9301. /**
  9302. * Release an object after use
  9303. * @method release
  9304. * @param {Object} obj
  9305. */
  9306. Pool.prototype.release = function(){
  9307. var Nargs = arguments.length;
  9308. for(var i=0; i!==Nargs; i++){
  9309. this.objects.push(arguments[i]);
  9310. }
  9311. };
  9312. /**
  9313. * Get an object
  9314. * @method get
  9315. * @return {mixed}
  9316. */
  9317. Pool.prototype.get = function(){
  9318. if(this.objects.length===0){
  9319. return this.constructObject();
  9320. } else {
  9321. return this.objects.pop();
  9322. }
  9323. };
  9324. /**
  9325. * Construct an object. Should be implmented in each subclass.
  9326. * @method constructObject
  9327. * @return {mixed}
  9328. */
  9329. Pool.prototype.constructObject = function(){
  9330. throw new Error("constructObject() not implemented in this Pool subclass yet!");
  9331. };
  9332. },{}],52:[function(_dereq_,module,exports){
  9333. module.exports = TupleDictionary;
  9334. /**
  9335. * @class TupleDictionary
  9336. * @constructor
  9337. */
  9338. function TupleDictionary() {
  9339. /**
  9340. * The data storage
  9341. * @property data
  9342. * @type {Object}
  9343. */
  9344. this.data = { keys:[] };
  9345. }
  9346. /**
  9347. * @method get
  9348. * @param {Number} i
  9349. * @param {Number} j
  9350. * @return {Number}
  9351. */
  9352. TupleDictionary.prototype.get = function(i, j) {
  9353. if (i > j) {
  9354. // swap
  9355. var temp = j;
  9356. j = i;
  9357. i = temp;
  9358. }
  9359. return this.data[i+'-'+j];
  9360. };
  9361. /**
  9362. * @method set
  9363. * @param {Number} i
  9364. * @param {Number} j
  9365. * @param {Number} value
  9366. */
  9367. TupleDictionary.prototype.set = function(i, j, value) {
  9368. if (i > j) {
  9369. var temp = j;
  9370. j = i;
  9371. i = temp;
  9372. }
  9373. var key = i+'-'+j;
  9374. // Check if key already exists
  9375. if(!this.get(i,j)){
  9376. this.data.keys.push(key);
  9377. }
  9378. this.data[key] = value;
  9379. };
  9380. /**
  9381. * @method reset
  9382. */
  9383. TupleDictionary.prototype.reset = function() {
  9384. var data = this.data,
  9385. keys = data.keys;
  9386. while(keys.length > 0){
  9387. var key = keys.pop();
  9388. delete data[key];
  9389. }
  9390. };
  9391. },{}],53:[function(_dereq_,module,exports){
  9392. function Utils(){}
  9393. module.exports = Utils;
  9394. /**
  9395. * Extend an options object with default values.
  9396. * @static
  9397. * @method defaults
  9398. * @param {object} options The options object. May be falsy: in this case, a new object is created and returned.
  9399. * @param {object} defaults An object containing default values.
  9400. * @return {object} The modified options object.
  9401. */
  9402. Utils.defaults = function(options, defaults){
  9403. options = options || {};
  9404. for(var key in defaults){
  9405. if(!(key in options)){
  9406. options[key] = defaults[key];
  9407. }
  9408. }
  9409. return options;
  9410. };
  9411. },{}],54:[function(_dereq_,module,exports){
  9412. module.exports = Vec3Pool;
  9413. var Vec3 = _dereq_('../math/Vec3');
  9414. var Pool = _dereq_('./Pool');
  9415. /**
  9416. * @class Vec3Pool
  9417. * @constructor
  9418. * @extends Pool
  9419. */
  9420. function Vec3Pool(){
  9421. Pool.call(this);
  9422. this.type = Vec3;
  9423. }
  9424. Vec3Pool.prototype = new Pool();
  9425. /**
  9426. * Construct a vector
  9427. * @method constructObject
  9428. * @return {Vec3}
  9429. */
  9430. Vec3Pool.prototype.constructObject = function(){
  9431. return new Vec3();
  9432. };
  9433. },{"../math/Vec3":30,"./Pool":51}],55:[function(_dereq_,module,exports){
  9434. module.exports = Narrowphase;
  9435. var AABB = _dereq_('../collision/AABB');
  9436. var Shape = _dereq_('../shapes/Shape');
  9437. var Ray = _dereq_('../collision/Ray');
  9438. var Vec3 = _dereq_('../math/Vec3');
  9439. var Transform = _dereq_('../math/Transform');
  9440. var ConvexPolyhedron = _dereq_('../shapes/ConvexPolyhedron');
  9441. var Quaternion = _dereq_('../math/Quaternion');
  9442. var Solver = _dereq_('../solver/Solver');
  9443. var Vec3Pool = _dereq_('../utils/Vec3Pool');
  9444. var ContactEquation = _dereq_('../equations/ContactEquation');
  9445. var FrictionEquation = _dereq_('../equations/FrictionEquation');
  9446. /**
  9447. * Helper class for the World. Generates ContactEquations.
  9448. * @class Narrowphase
  9449. * @constructor
  9450. * @todo Sphere-ConvexPolyhedron contacts
  9451. * @todo Contact reduction
  9452. * @todo should move methods to prototype
  9453. */
  9454. function Narrowphase(world){
  9455. /**
  9456. * Internal storage of pooled contact points.
  9457. * @property {Array} contactPointPool
  9458. */
  9459. this.contactPointPool = [];
  9460. this.frictionEquationPool = [];
  9461. this.result = [];
  9462. this.frictionResult = [];
  9463. /**
  9464. * Pooled vectors.
  9465. * @property {Vec3Pool} v3pool
  9466. */
  9467. this.v3pool = new Vec3Pool();
  9468. this.world = world;
  9469. this.currentContactMaterial = null;
  9470. /**
  9471. * @property {Boolean} enableFrictionReduction
  9472. */
  9473. this.enableFrictionReduction = false;
  9474. }
  9475. /**
  9476. * Make a contact object, by using the internal pool or creating a new one.
  9477. * @method createContactEquation
  9478. * @return {ContactEquation}
  9479. */
  9480. Narrowphase.prototype.createContactEquation = function(bi, bj, si, sj, rsi, rsj){
  9481. var c;
  9482. if(this.contactPointPool.length){
  9483. c = this.contactPointPool.pop();
  9484. c.bi = bi;
  9485. c.bj = bj;
  9486. } else {
  9487. c = new ContactEquation(bi, bj);
  9488. }
  9489. c.enabled = bi.collisionResponse && bj.collisionResponse && si.collisionResponse && sj.collisionResponse;
  9490. var cm = this.currentContactMaterial;
  9491. c.restitution = cm.restitution;
  9492. c.setSpookParams(
  9493. cm.contactEquationStiffness,
  9494. cm.contactEquationRelaxation,
  9495. this.world.dt
  9496. );
  9497. var matA = si.material || bi.material;
  9498. var matB = sj.material || bj.material;
  9499. if(matA && matB && matA.restitution >= 0 && matB.restitution >= 0){
  9500. c.restitution = matA.restitution * matB.restitution;
  9501. }
  9502. c.si = rsi || si;
  9503. c.sj = rsj || sj;
  9504. return c;
  9505. };
  9506. Narrowphase.prototype.createFrictionEquationsFromContact = function(contactEquation, outArray){
  9507. var bodyA = contactEquation.bi;
  9508. var bodyB = contactEquation.bj;
  9509. var shapeA = contactEquation.si;
  9510. var shapeB = contactEquation.sj;
  9511. var world = this.world;
  9512. var cm = this.currentContactMaterial;
  9513. // If friction or restitution were specified in the material, use them
  9514. var friction = cm.friction;
  9515. var matA = shapeA.material || bodyA.material;
  9516. var matB = shapeB.material || bodyB.material;
  9517. if(matA && matB && matA.friction >= 0 && matB.friction >= 0){
  9518. friction = matA.friction * matB.friction;
  9519. }
  9520. if(friction > 0){
  9521. // Create 2 tangent equations
  9522. var mug = friction * world.gravity.length();
  9523. var reducedMass = (bodyA.invMass + bodyB.invMass);
  9524. if(reducedMass > 0){
  9525. reducedMass = 1/reducedMass;
  9526. }
  9527. var pool = this.frictionEquationPool;
  9528. var c1 = pool.length ? pool.pop() : new FrictionEquation(bodyA,bodyB,mug*reducedMass);
  9529. var c2 = pool.length ? pool.pop() : new FrictionEquation(bodyA,bodyB,mug*reducedMass);
  9530. c1.bi = c2.bi = bodyA;
  9531. c1.bj = c2.bj = bodyB;
  9532. c1.minForce = c2.minForce = -mug*reducedMass;
  9533. c1.maxForce = c2.maxForce = mug*reducedMass;
  9534. // Copy over the relative vectors
  9535. c1.ri.copy(contactEquation.ri);
  9536. c1.rj.copy(contactEquation.rj);
  9537. c2.ri.copy(contactEquation.ri);
  9538. c2.rj.copy(contactEquation.rj);
  9539. // Construct tangents
  9540. contactEquation.ni.tangents(c1.t, c2.t);
  9541. // Set spook params
  9542. c1.setSpookParams(cm.frictionEquationStiffness, cm.frictionEquationRelaxation, world.dt);
  9543. c2.setSpookParams(cm.frictionEquationStiffness, cm.frictionEquationRelaxation, world.dt);
  9544. c1.enabled = c2.enabled = contactEquation.enabled;
  9545. outArray.push(c1, c2);
  9546. return true;
  9547. }
  9548. return false;
  9549. };
  9550. var averageNormal = new Vec3();
  9551. var averageContactPointA = new Vec3();
  9552. var averageContactPointB = new Vec3();
  9553. // Take the average N latest contact point on the plane.
  9554. Narrowphase.prototype.createFrictionFromAverage = function(numContacts){
  9555. // The last contactEquation
  9556. var c = this.result[this.result.length - 1];
  9557. // Create the result: two "average" friction equations
  9558. if (!this.createFrictionEquationsFromContact(c, this.frictionResult) || numContacts === 1) {
  9559. return;
  9560. }
  9561. var f1 = this.frictionResult[this.frictionResult.length - 2];
  9562. var f2 = this.frictionResult[this.frictionResult.length - 1];
  9563. averageNormal.setZero();
  9564. averageContactPointA.setZero();
  9565. averageContactPointB.setZero();
  9566. var bodyA = c.bi;
  9567. var bodyB = c.bj;
  9568. for(var i=0; i!==numContacts; i++){
  9569. c = this.result[this.result.length - 1 - i];
  9570. if(c.bodyA !== bodyA){
  9571. averageNormal.vadd(c.ni, averageNormal); // vec2.add(eq.t, eq.t, c.normalA);
  9572. averageContactPointA.vadd(c.ri, averageContactPointA); // vec2.add(eq.contactPointA, eq.contactPointA, c.contactPointA);
  9573. averageContactPointB.vadd(c.rj, averageContactPointB);
  9574. } else {
  9575. averageNormal.vsub(c.ni, averageNormal); // vec2.sub(eq.t, eq.t, c.normalA);
  9576. averageContactPointA.vadd(c.rj, averageContactPointA); // vec2.add(eq.contactPointA, eq.contactPointA, c.contactPointA);
  9577. averageContactPointB.vadd(c.ri, averageContactPointB);
  9578. }
  9579. }
  9580. var invNumContacts = 1 / numContacts;
  9581. averageContactPointA.scale(invNumContacts, f1.ri); // vec2.scale(eq.contactPointA, eq.contactPointA, invNumContacts);
  9582. averageContactPointB.scale(invNumContacts, f1.rj); // vec2.scale(eq.contactPointB, eq.contactPointB, invNumContacts);
  9583. f2.ri.copy(f1.ri); // Should be the same
  9584. f2.rj.copy(f1.rj);
  9585. averageNormal.normalize();
  9586. averageNormal.tangents(f1.t, f2.t);
  9587. // return eq;
  9588. };
  9589. var tmpVec1 = new Vec3();
  9590. var tmpVec2 = new Vec3();
  9591. var tmpQuat1 = new Quaternion();
  9592. var tmpQuat2 = new Quaternion();
  9593. /**
  9594. * Generate all contacts between a list of body pairs
  9595. * @method getContacts
  9596. * @param {array} p1 Array of body indices
  9597. * @param {array} p2 Array of body indices
  9598. * @param {World} world
  9599. * @param {array} result Array to store generated contacts
  9600. * @param {array} oldcontacts Optional. Array of reusable contact objects
  9601. */
  9602. Narrowphase.prototype.getContacts = function(p1, p2, world, result, oldcontacts, frictionResult, frictionPool){
  9603. // Save old contact objects
  9604. this.contactPointPool = oldcontacts;
  9605. this.frictionEquationPool = frictionPool;
  9606. this.result = result;
  9607. this.frictionResult = frictionResult;
  9608. var qi = tmpQuat1;
  9609. var qj = tmpQuat2;
  9610. var xi = tmpVec1;
  9611. var xj = tmpVec2;
  9612. for(var k=0, N=p1.length; k!==N; k++){
  9613. // Get current collision bodies
  9614. var bi = p1[k],
  9615. bj = p2[k];
  9616. // Get contact material
  9617. var bodyContactMaterial = null;
  9618. if(bi.material && bj.material){
  9619. bodyContactMaterial = world.getContactMaterial(bi.material,bj.material) || null;
  9620. }
  9621. for (var i = 0; i < bi.shapes.length; i++) {
  9622. bi.quaternion.mult(bi.shapeOrientations[i], qi);
  9623. bi.quaternion.vmult(bi.shapeOffsets[i], xi);
  9624. xi.vadd(bi.position, xi);
  9625. var si = bi.shapes[i];
  9626. for (var j = 0; j < bj.shapes.length; j++) {
  9627. // Compute world transform of shapes
  9628. bj.quaternion.mult(bj.shapeOrientations[j], qj);
  9629. bj.quaternion.vmult(bj.shapeOffsets[j], xj);
  9630. xj.vadd(bj.position, xj);
  9631. var sj = bj.shapes[j];
  9632. if(xi.distanceTo(xj) > si.boundingSphereRadius + sj.boundingSphereRadius){
  9633. continue;
  9634. }
  9635. // Get collision material
  9636. var shapeContactMaterial = null;
  9637. if(si.material && sj.material){
  9638. shapeContactMaterial = world.getContactMaterial(si.material,sj.material) || null;
  9639. }
  9640. this.currentContactMaterial = shapeContactMaterial || bodyContactMaterial || world.defaultContactMaterial;
  9641. // Get contacts
  9642. var resolver = this[si.type | sj.type];
  9643. if(resolver){
  9644. if (si.type < sj.type) {
  9645. resolver.call(this, si, sj, xi, xj, qi, qj, bi, bj, si, sj);
  9646. } else {
  9647. resolver.call(this, sj, si, xj, xi, qj, qi, bj, bi, si, sj);
  9648. }
  9649. }
  9650. }
  9651. }
  9652. }
  9653. };
  9654. var numWarnings = 0;
  9655. var maxWarnings = 10;
  9656. function warn(msg){
  9657. if(numWarnings > maxWarnings){
  9658. return;
  9659. }
  9660. numWarnings++;
  9661. console.warn(msg);
  9662. }
  9663. Narrowphase.prototype[Shape.types.BOX | Shape.types.BOX] =
  9664. Narrowphase.prototype.boxBox = function(si,sj,xi,xj,qi,qj,bi,bj){
  9665. si.convexPolyhedronRepresentation.material = si.material;
  9666. sj.convexPolyhedronRepresentation.material = sj.material;
  9667. si.convexPolyhedronRepresentation.collisionResponse = si.collisionResponse;
  9668. sj.convexPolyhedronRepresentation.collisionResponse = sj.collisionResponse;
  9669. this.convexConvex(si.convexPolyhedronRepresentation,sj.convexPolyhedronRepresentation,xi,xj,qi,qj,bi,bj,si,sj);
  9670. };
  9671. Narrowphase.prototype[Shape.types.BOX | Shape.types.CONVEXPOLYHEDRON] =
  9672. Narrowphase.prototype.boxConvex = function(si,sj,xi,xj,qi,qj,bi,bj){
  9673. si.convexPolyhedronRepresentation.material = si.material;
  9674. si.convexPolyhedronRepresentation.collisionResponse = si.collisionResponse;
  9675. this.convexConvex(si.convexPolyhedronRepresentation,sj,xi,xj,qi,qj,bi,bj,si,sj);
  9676. };
  9677. Narrowphase.prototype[Shape.types.BOX | Shape.types.PARTICLE] =
  9678. Narrowphase.prototype.boxParticle = function(si,sj,xi,xj,qi,qj,bi,bj){
  9679. si.convexPolyhedronRepresentation.material = si.material;
  9680. si.convexPolyhedronRepresentation.collisionResponse = si.collisionResponse;
  9681. this.convexParticle(si.convexPolyhedronRepresentation,sj,xi,xj,qi,qj,bi,bj,si,sj);
  9682. };
  9683. /**
  9684. * @method sphereSphere
  9685. * @param {Shape} si
  9686. * @param {Shape} sj
  9687. * @param {Vec3} xi
  9688. * @param {Vec3} xj
  9689. * @param {Quaternion} qi
  9690. * @param {Quaternion} qj
  9691. * @param {Body} bi
  9692. * @param {Body} bj
  9693. */
  9694. Narrowphase.prototype[Shape.types.SPHERE] =
  9695. Narrowphase.prototype.sphereSphere = function(si,sj,xi,xj,qi,qj,bi,bj){
  9696. // We will have only one contact in this case
  9697. var r = this.createContactEquation(bi,bj,si,sj);
  9698. // Contact normal
  9699. xj.vsub(xi, r.ni);
  9700. r.ni.normalize();
  9701. // Contact point locations
  9702. r.ri.copy(r.ni);
  9703. r.rj.copy(r.ni);
  9704. r.ri.mult(si.radius, r.ri);
  9705. r.rj.mult(-sj.radius, r.rj);
  9706. r.ri.vadd(xi, r.ri);
  9707. r.ri.vsub(bi.position, r.ri);
  9708. r.rj.vadd(xj, r.rj);
  9709. r.rj.vsub(bj.position, r.rj);
  9710. this.result.push(r);
  9711. this.createFrictionEquationsFromContact(r, this.frictionResult);
  9712. };
  9713. /**
  9714. * @method planeTrimesh
  9715. * @param {Shape} si
  9716. * @param {Shape} sj
  9717. * @param {Vec3} xi
  9718. * @param {Vec3} xj
  9719. * @param {Quaternion} qi
  9720. * @param {Quaternion} qj
  9721. * @param {Body} bi
  9722. * @param {Body} bj
  9723. */
  9724. var planeTrimesh_normal = new Vec3();
  9725. var planeTrimesh_relpos = new Vec3();
  9726. var planeTrimesh_projected = new Vec3();
  9727. Narrowphase.prototype[Shape.types.PLANE | Shape.types.TRIMESH] =
  9728. Narrowphase.prototype.planeTrimesh = function(
  9729. planeShape,
  9730. trimeshShape,
  9731. planePos,
  9732. trimeshPos,
  9733. planeQuat,
  9734. trimeshQuat,
  9735. planeBody,
  9736. trimeshBody
  9737. ){
  9738. // Make contacts!
  9739. var v = new Vec3();
  9740. var normal = planeTrimesh_normal;
  9741. normal.set(0,0,1);
  9742. planeQuat.vmult(normal,normal); // Turn normal according to plane
  9743. for(var i=0; i<trimeshShape.vertices.length / 3; i++){
  9744. // Get world vertex from trimesh
  9745. trimeshShape.getVertex(i, v);
  9746. // Safe up
  9747. var v2 = new Vec3();
  9748. v2.copy(v);
  9749. Transform.pointToWorldFrame(trimeshPos, trimeshQuat, v2, v);
  9750. // Check plane side
  9751. var relpos = planeTrimesh_relpos;
  9752. v.vsub(planePos, relpos);
  9753. var dot = normal.dot(relpos);
  9754. if(dot <= 0.0){
  9755. var r = this.createContactEquation(planeBody,trimeshBody,planeShape,trimeshShape);
  9756. r.ni.copy(normal); // Contact normal is the plane normal
  9757. // Get vertex position projected on plane
  9758. var projected = planeTrimesh_projected;
  9759. normal.scale(relpos.dot(normal), projected);
  9760. v.vsub(projected,projected);
  9761. // ri is the projected world position minus plane position
  9762. r.ri.copy(projected);
  9763. r.ri.vsub(planeBody.position, r.ri);
  9764. r.rj.copy(v);
  9765. r.rj.vsub(trimeshBody.position, r.rj);
  9766. // Store result
  9767. this.result.push(r);
  9768. this.createFrictionEquationsFromContact(r, this.frictionResult);
  9769. }
  9770. }
  9771. };
  9772. /**
  9773. * @method sphereTrimesh
  9774. * @param {Shape} sphereShape
  9775. * @param {Shape} trimeshShape
  9776. * @param {Vec3} spherePos
  9777. * @param {Vec3} trimeshPos
  9778. * @param {Quaternion} sphereQuat
  9779. * @param {Quaternion} trimeshQuat
  9780. * @param {Body} sphereBody
  9781. * @param {Body} trimeshBody
  9782. */
  9783. var sphereTrimesh_normal = new Vec3();
  9784. var sphereTrimesh_relpos = new Vec3();
  9785. var sphereTrimesh_projected = new Vec3();
  9786. var sphereTrimesh_v = new Vec3();
  9787. var sphereTrimesh_v2 = new Vec3();
  9788. var sphereTrimesh_edgeVertexA = new Vec3();
  9789. var sphereTrimesh_edgeVertexB = new Vec3();
  9790. var sphereTrimesh_edgeVector = new Vec3();
  9791. var sphereTrimesh_edgeVectorUnit = new Vec3();
  9792. var sphereTrimesh_localSpherePos = new Vec3();
  9793. var sphereTrimesh_tmp = new Vec3();
  9794. var sphereTrimesh_va = new Vec3();
  9795. var sphereTrimesh_vb = new Vec3();
  9796. var sphereTrimesh_vc = new Vec3();
  9797. var sphereTrimesh_localSphereAABB = new AABB();
  9798. var sphereTrimesh_triangles = [];
  9799. Narrowphase.prototype[Shape.types.SPHERE | Shape.types.TRIMESH] =
  9800. Narrowphase.prototype.sphereTrimesh = function (
  9801. sphereShape,
  9802. trimeshShape,
  9803. spherePos,
  9804. trimeshPos,
  9805. sphereQuat,
  9806. trimeshQuat,
  9807. sphereBody,
  9808. trimeshBody
  9809. ) {
  9810. var edgeVertexA = sphereTrimesh_edgeVertexA;
  9811. var edgeVertexB = sphereTrimesh_edgeVertexB;
  9812. var edgeVector = sphereTrimesh_edgeVector;
  9813. var edgeVectorUnit = sphereTrimesh_edgeVectorUnit;
  9814. var localSpherePos = sphereTrimesh_localSpherePos;
  9815. var tmp = sphereTrimesh_tmp;
  9816. var localSphereAABB = sphereTrimesh_localSphereAABB;
  9817. var v2 = sphereTrimesh_v2;
  9818. var relpos = sphereTrimesh_relpos;
  9819. var triangles = sphereTrimesh_triangles;
  9820. // Convert sphere position to local in the trimesh
  9821. Transform.pointToLocalFrame(trimeshPos, trimeshQuat, spherePos, localSpherePos);
  9822. // Get the aabb of the sphere locally in the trimesh
  9823. var sphereRadius = sphereShape.radius;
  9824. localSphereAABB.lowerBound.set(
  9825. localSpherePos.x - sphereRadius,
  9826. localSpherePos.y - sphereRadius,
  9827. localSpherePos.z - sphereRadius
  9828. );
  9829. localSphereAABB.upperBound.set(
  9830. localSpherePos.x + sphereRadius,
  9831. localSpherePos.y + sphereRadius,
  9832. localSpherePos.z + sphereRadius
  9833. );
  9834. trimeshShape.getTrianglesInAABB(localSphereAABB, triangles);
  9835. //for (var i = 0; i < trimeshShape.indices.length / 3; i++) triangles.push(i); // All
  9836. // Vertices
  9837. var v = sphereTrimesh_v;
  9838. var radiusSquared = sphereShape.radius * sphereShape.radius;
  9839. for(var i=0; i<triangles.length; i++){
  9840. for (var j = 0; j < 3; j++) {
  9841. trimeshShape.getVertex(trimeshShape.indices[triangles[i] * 3 + j], v);
  9842. // Check vertex overlap in sphere
  9843. v.vsub(localSpherePos, relpos);
  9844. if(relpos.norm2() <= radiusSquared){
  9845. // Safe up
  9846. v2.copy(v);
  9847. Transform.pointToWorldFrame(trimeshPos, trimeshQuat, v2, v);
  9848. v.vsub(spherePos, relpos);
  9849. var r = this.createContactEquation(sphereBody,trimeshBody,sphereShape,trimeshShape);
  9850. r.ni.copy(relpos);
  9851. r.ni.normalize();
  9852. // ri is the vector from sphere center to the sphere surface
  9853. r.ri.copy(r.ni);
  9854. r.ri.scale(sphereShape.radius, r.ri);
  9855. r.ri.vadd(spherePos, r.ri);
  9856. r.ri.vsub(sphereBody.position, r.ri);
  9857. r.rj.copy(v);
  9858. r.rj.vsub(trimeshBody.position, r.rj);
  9859. // Store result
  9860. this.result.push(r);
  9861. this.createFrictionEquationsFromContact(r, this.frictionResult);
  9862. }
  9863. }
  9864. }
  9865. // Check all edges
  9866. for(var i=0; i<triangles.length; i++){
  9867. for (var j = 0; j < 3; j++) {
  9868. trimeshShape.getVertex(trimeshShape.indices[triangles[i] * 3 + j], edgeVertexA);
  9869. trimeshShape.getVertex(trimeshShape.indices[triangles[i] * 3 + ((j+1)%3)], edgeVertexB);
  9870. edgeVertexB.vsub(edgeVertexA, edgeVector);
  9871. // Project sphere position to the edge
  9872. localSpherePos.vsub(edgeVertexB, tmp);
  9873. var positionAlongEdgeB = tmp.dot(edgeVector);
  9874. localSpherePos.vsub(edgeVertexA, tmp);
  9875. var positionAlongEdgeA = tmp.dot(edgeVector);
  9876. if(positionAlongEdgeA > 0 && positionAlongEdgeB < 0){
  9877. // Now check the orthogonal distance from edge to sphere center
  9878. localSpherePos.vsub(edgeVertexA, tmp);
  9879. edgeVectorUnit.copy(edgeVector);
  9880. edgeVectorUnit.normalize();
  9881. positionAlongEdgeA = tmp.dot(edgeVectorUnit);
  9882. edgeVectorUnit.scale(positionAlongEdgeA, tmp);
  9883. tmp.vadd(edgeVertexA, tmp);
  9884. // tmp is now the sphere center position projected to the edge, defined locally in the trimesh frame
  9885. var dist = tmp.distanceTo(localSpherePos);
  9886. if(dist < sphereShape.radius){
  9887. var r = this.createContactEquation(sphereBody, trimeshBody, sphereShape, trimeshShape);
  9888. tmp.vsub(localSpherePos, r.ni);
  9889. r.ni.normalize();
  9890. r.ni.scale(sphereShape.radius, r.ri);
  9891. Transform.pointToWorldFrame(trimeshPos, trimeshQuat, tmp, tmp);
  9892. tmp.vsub(trimeshBody.position, r.rj);
  9893. Transform.vectorToWorldFrame(trimeshQuat, r.ni, r.ni);
  9894. Transform.vectorToWorldFrame(trimeshQuat, r.ri, r.ri);
  9895. this.result.push(r);
  9896. this.createFrictionEquationsFromContact(r, this.frictionResult);
  9897. }
  9898. }
  9899. }
  9900. }
  9901. // Triangle faces
  9902. var va = sphereTrimesh_va;
  9903. var vb = sphereTrimesh_vb;
  9904. var vc = sphereTrimesh_vc;
  9905. var normal = sphereTrimesh_normal;
  9906. for(var i=0, N = triangles.length; i !== N; i++){
  9907. trimeshShape.getTriangleVertices(triangles[i], va, vb, vc);
  9908. trimeshShape.getNormal(triangles[i], normal);
  9909. localSpherePos.vsub(va, tmp);
  9910. var dist = tmp.dot(normal);
  9911. normal.scale(dist, tmp);
  9912. localSpherePos.vsub(tmp, tmp);
  9913. // tmp is now the sphere position projected to the triangle plane
  9914. dist = tmp.distanceTo(localSpherePos);
  9915. if(Ray.pointInTriangle(tmp, va, vb, vc) && dist < sphereShape.radius){
  9916. var r = this.createContactEquation(sphereBody, trimeshBody, sphereShape, trimeshShape);
  9917. tmp.vsub(localSpherePos, r.ni);
  9918. r.ni.normalize();
  9919. r.ni.scale(sphereShape.radius, r.ri);
  9920. Transform.pointToWorldFrame(trimeshPos, trimeshQuat, tmp, tmp);
  9921. tmp.vsub(trimeshBody.position, r.rj);
  9922. Transform.vectorToWorldFrame(trimeshQuat, r.ni, r.ni);
  9923. Transform.vectorToWorldFrame(trimeshQuat, r.ri, r.ri);
  9924. this.result.push(r);
  9925. this.createFrictionEquationsFromContact(r, this.frictionResult);
  9926. }
  9927. }
  9928. triangles.length = 0;
  9929. };
  9930. var point_on_plane_to_sphere = new Vec3();
  9931. var plane_to_sphere_ortho = new Vec3();
  9932. /**
  9933. * @method spherePlane
  9934. * @param {Shape} si
  9935. * @param {Shape} sj
  9936. * @param {Vec3} xi
  9937. * @param {Vec3} xj
  9938. * @param {Quaternion} qi
  9939. * @param {Quaternion} qj
  9940. * @param {Body} bi
  9941. * @param {Body} bj
  9942. */
  9943. Narrowphase.prototype[Shape.types.SPHERE | Shape.types.PLANE] =
  9944. Narrowphase.prototype.spherePlane = function(si,sj,xi,xj,qi,qj,bi,bj){
  9945. // We will have one contact in this case
  9946. var r = this.createContactEquation(bi,bj,si,sj);
  9947. // Contact normal
  9948. r.ni.set(0,0,1);
  9949. qj.vmult(r.ni, r.ni);
  9950. r.ni.negate(r.ni); // body i is the sphere, flip normal
  9951. r.ni.normalize(); // Needed?
  9952. // Vector from sphere center to contact point
  9953. r.ni.mult(si.radius, r.ri);
  9954. // Project down sphere on plane
  9955. xi.vsub(xj, point_on_plane_to_sphere);
  9956. r.ni.mult(r.ni.dot(point_on_plane_to_sphere), plane_to_sphere_ortho);
  9957. point_on_plane_to_sphere.vsub(plane_to_sphere_ortho,r.rj); // The sphere position projected to plane
  9958. if(-point_on_plane_to_sphere.dot(r.ni) <= si.radius){
  9959. // Make it relative to the body
  9960. var ri = r.ri;
  9961. var rj = r.rj;
  9962. ri.vadd(xi, ri);
  9963. ri.vsub(bi.position, ri);
  9964. rj.vadd(xj, rj);
  9965. rj.vsub(bj.position, rj);
  9966. this.result.push(r);
  9967. this.createFrictionEquationsFromContact(r, this.frictionResult);
  9968. }
  9969. };
  9970. // See http://bulletphysics.com/Bullet/BulletFull/SphereTriangleDetector_8cpp_source.html
  9971. var pointInPolygon_edge = new Vec3();
  9972. var pointInPolygon_edge_x_normal = new Vec3();
  9973. var pointInPolygon_vtp = new Vec3();
  9974. function pointInPolygon(verts, normal, p){
  9975. var positiveResult = null;
  9976. var N = verts.length;
  9977. for(var i=0; i!==N; i++){
  9978. var v = verts[i];
  9979. // Get edge to the next vertex
  9980. var edge = pointInPolygon_edge;
  9981. verts[(i+1) % (N)].vsub(v,edge);
  9982. // Get cross product between polygon normal and the edge
  9983. var edge_x_normal = pointInPolygon_edge_x_normal;
  9984. //var edge_x_normal = new Vec3();
  9985. edge.cross(normal,edge_x_normal);
  9986. // Get vector between point and current vertex
  9987. var vertex_to_p = pointInPolygon_vtp;
  9988. p.vsub(v,vertex_to_p);
  9989. // This dot product determines which side of the edge the point is
  9990. var r = edge_x_normal.dot(vertex_to_p);
  9991. // If all such dot products have same sign, we are inside the polygon.
  9992. if(positiveResult===null || (r>0 && positiveResult===true) || (r<=0 && positiveResult===false)){
  9993. if(positiveResult===null){
  9994. positiveResult = r>0;
  9995. }
  9996. continue;
  9997. } else {
  9998. return false; // Encountered some other sign. Exit.
  9999. }
  10000. }
  10001. // If we got here, all dot products were of the same sign.
  10002. return true;
  10003. }
  10004. var box_to_sphere = new Vec3();
  10005. var sphereBox_ns = new Vec3();
  10006. var sphereBox_ns1 = new Vec3();
  10007. var sphereBox_ns2 = new Vec3();
  10008. var sphereBox_sides = [new Vec3(),new Vec3(),new Vec3(),new Vec3(),new Vec3(),new Vec3()];
  10009. var sphereBox_sphere_to_corner = new Vec3();
  10010. var sphereBox_side_ns = new Vec3();
  10011. var sphereBox_side_ns1 = new Vec3();
  10012. var sphereBox_side_ns2 = new Vec3();
  10013. /**
  10014. * @method sphereBox
  10015. * @param {Shape} si
  10016. * @param {Shape} sj
  10017. * @param {Vec3} xi
  10018. * @param {Vec3} xj
  10019. * @param {Quaternion} qi
  10020. * @param {Quaternion} qj
  10021. * @param {Body} bi
  10022. * @param {Body} bj
  10023. */
  10024. Narrowphase.prototype[Shape.types.SPHERE | Shape.types.BOX] =
  10025. Narrowphase.prototype.sphereBox = function(si,sj,xi,xj,qi,qj,bi,bj){
  10026. var v3pool = this.v3pool;
  10027. // we refer to the box as body j
  10028. var sides = sphereBox_sides;
  10029. xi.vsub(xj,box_to_sphere);
  10030. sj.getSideNormals(sides,qj);
  10031. var R = si.radius;
  10032. var penetrating_sides = [];
  10033. // Check side (plane) intersections
  10034. var found = false;
  10035. // Store the resulting side penetration info
  10036. var side_ns = sphereBox_side_ns;
  10037. var side_ns1 = sphereBox_side_ns1;
  10038. var side_ns2 = sphereBox_side_ns2;
  10039. var side_h = null;
  10040. var side_penetrations = 0;
  10041. var side_dot1 = 0;
  10042. var side_dot2 = 0;
  10043. var side_distance = null;
  10044. for(var idx=0,nsides=sides.length; idx!==nsides && found===false; idx++){
  10045. // Get the plane side normal (ns)
  10046. var ns = sphereBox_ns;
  10047. ns.copy(sides[idx]);
  10048. var h = ns.norm();
  10049. ns.normalize();
  10050. // The normal/distance dot product tells which side of the plane we are
  10051. var dot = box_to_sphere.dot(ns);
  10052. if(dot<h+R && dot>0){
  10053. // Intersects plane. Now check the other two dimensions
  10054. var ns1 = sphereBox_ns1;
  10055. var ns2 = sphereBox_ns2;
  10056. ns1.copy(sides[(idx+1)%3]);
  10057. ns2.copy(sides[(idx+2)%3]);
  10058. var h1 = ns1.norm();
  10059. var h2 = ns2.norm();
  10060. ns1.normalize();
  10061. ns2.normalize();
  10062. var dot1 = box_to_sphere.dot(ns1);
  10063. var dot2 = box_to_sphere.dot(ns2);
  10064. if(dot1<h1 && dot1>-h1 && dot2<h2 && dot2>-h2){
  10065. var dist = Math.abs(dot-h-R);
  10066. if(side_distance===null || dist < side_distance){
  10067. side_distance = dist;
  10068. side_dot1 = dot1;
  10069. side_dot2 = dot2;
  10070. side_h = h;
  10071. side_ns.copy(ns);
  10072. side_ns1.copy(ns1);
  10073. side_ns2.copy(ns2);
  10074. side_penetrations++;
  10075. }
  10076. }
  10077. }
  10078. }
  10079. if(side_penetrations){
  10080. found = true;
  10081. var r = this.createContactEquation(bi,bj,si,sj);
  10082. side_ns.mult(-R,r.ri); // Sphere r
  10083. r.ni.copy(side_ns);
  10084. r.ni.negate(r.ni); // Normal should be out of sphere
  10085. side_ns.mult(side_h,side_ns);
  10086. side_ns1.mult(side_dot1,side_ns1);
  10087. side_ns.vadd(side_ns1,side_ns);
  10088. side_ns2.mult(side_dot2,side_ns2);
  10089. side_ns.vadd(side_ns2,r.rj);
  10090. // Make relative to bodies
  10091. r.ri.vadd(xi, r.ri);
  10092. r.ri.vsub(bi.position, r.ri);
  10093. r.rj.vadd(xj, r.rj);
  10094. r.rj.vsub(bj.position, r.rj);
  10095. this.result.push(r);
  10096. this.createFrictionEquationsFromContact(r, this.frictionResult);
  10097. }
  10098. // Check corners
  10099. var rj = v3pool.get();
  10100. var sphere_to_corner = sphereBox_sphere_to_corner;
  10101. for(var j=0; j!==2 && !found; j++){
  10102. for(var k=0; k!==2 && !found; k++){
  10103. for(var l=0; l!==2 && !found; l++){
  10104. rj.set(0,0,0);
  10105. if(j){
  10106. rj.vadd(sides[0],rj);
  10107. } else {
  10108. rj.vsub(sides[0],rj);
  10109. }
  10110. if(k){
  10111. rj.vadd(sides[1],rj);
  10112. } else {
  10113. rj.vsub(sides[1],rj);
  10114. }
  10115. if(l){
  10116. rj.vadd(sides[2],rj);
  10117. } else {
  10118. rj.vsub(sides[2],rj);
  10119. }
  10120. // World position of corner
  10121. xj.vadd(rj,sphere_to_corner);
  10122. sphere_to_corner.vsub(xi,sphere_to_corner);
  10123. if(sphere_to_corner.norm2() < R*R){
  10124. found = true;
  10125. var r = this.createContactEquation(bi,bj,si,sj);
  10126. r.ri.copy(sphere_to_corner);
  10127. r.ri.normalize();
  10128. r.ni.copy(r.ri);
  10129. r.ri.mult(R,r.ri);
  10130. r.rj.copy(rj);
  10131. // Make relative to bodies
  10132. r.ri.vadd(xi, r.ri);
  10133. r.ri.vsub(bi.position, r.ri);
  10134. r.rj.vadd(xj, r.rj);
  10135. r.rj.vsub(bj.position, r.rj);
  10136. this.result.push(r);
  10137. this.createFrictionEquationsFromContact(r, this.frictionResult);
  10138. }
  10139. }
  10140. }
  10141. }
  10142. v3pool.release(rj);
  10143. rj = null;
  10144. // Check edges
  10145. var edgeTangent = v3pool.get();
  10146. var edgeCenter = v3pool.get();
  10147. var r = v3pool.get(); // r = edge center to sphere center
  10148. var orthogonal = v3pool.get();
  10149. var dist = v3pool.get();
  10150. var Nsides = sides.length;
  10151. for(var j=0; j!==Nsides && !found; j++){
  10152. for(var k=0; k!==Nsides && !found; k++){
  10153. if(j%3 !== k%3){
  10154. // Get edge tangent
  10155. sides[k].cross(sides[j],edgeTangent);
  10156. edgeTangent.normalize();
  10157. sides[j].vadd(sides[k], edgeCenter);
  10158. r.copy(xi);
  10159. r.vsub(edgeCenter,r);
  10160. r.vsub(xj,r);
  10161. var orthonorm = r.dot(edgeTangent); // distance from edge center to sphere center in the tangent direction
  10162. edgeTangent.mult(orthonorm,orthogonal); // Vector from edge center to sphere center in the tangent direction
  10163. // Find the third side orthogonal to this one
  10164. var l = 0;
  10165. while(l===j%3 || l===k%3){
  10166. l++;
  10167. }
  10168. // vec from edge center to sphere projected to the plane orthogonal to the edge tangent
  10169. dist.copy(xi);
  10170. dist.vsub(orthogonal,dist);
  10171. dist.vsub(edgeCenter,dist);
  10172. dist.vsub(xj,dist);
  10173. // Distances in tangent direction and distance in the plane orthogonal to it
  10174. var tdist = Math.abs(orthonorm);
  10175. var ndist = dist.norm();
  10176. if(tdist < sides[l].norm() && ndist<R){
  10177. found = true;
  10178. var res = this.createContactEquation(bi,bj,si,sj);
  10179. edgeCenter.vadd(orthogonal,res.rj); // box rj
  10180. res.rj.copy(res.rj);
  10181. dist.negate(res.ni);
  10182. res.ni.normalize();
  10183. res.ri.copy(res.rj);
  10184. res.ri.vadd(xj,res.ri);
  10185. res.ri.vsub(xi,res.ri);
  10186. res.ri.normalize();
  10187. res.ri.mult(R,res.ri);
  10188. // Make relative to bodies
  10189. res.ri.vadd(xi, res.ri);
  10190. res.ri.vsub(bi.position, res.ri);
  10191. res.rj.vadd(xj, res.rj);
  10192. res.rj.vsub(bj.position, res.rj);
  10193. this.result.push(res);
  10194. this.createFrictionEquationsFromContact(res, this.frictionResult);
  10195. }
  10196. }
  10197. }
  10198. }
  10199. v3pool.release(edgeTangent,edgeCenter,r,orthogonal,dist);
  10200. };
  10201. var convex_to_sphere = new Vec3();
  10202. var sphereConvex_edge = new Vec3();
  10203. var sphereConvex_edgeUnit = new Vec3();
  10204. var sphereConvex_sphereToCorner = new Vec3();
  10205. var sphereConvex_worldCorner = new Vec3();
  10206. var sphereConvex_worldNormal = new Vec3();
  10207. var sphereConvex_worldPoint = new Vec3();
  10208. var sphereConvex_worldSpherePointClosestToPlane = new Vec3();
  10209. var sphereConvex_penetrationVec = new Vec3();
  10210. var sphereConvex_sphereToWorldPoint = new Vec3();
  10211. /**
  10212. * @method sphereConvex
  10213. * @param {Shape} si
  10214. * @param {Shape} sj
  10215. * @param {Vec3} xi
  10216. * @param {Vec3} xj
  10217. * @param {Quaternion} qi
  10218. * @param {Quaternion} qj
  10219. * @param {Body} bi
  10220. * @param {Body} bj
  10221. */
  10222. Narrowphase.prototype[Shape.types.SPHERE | Shape.types.CONVEXPOLYHEDRON] =
  10223. Narrowphase.prototype.sphereConvex = function(si,sj,xi,xj,qi,qj,bi,bj){
  10224. var v3pool = this.v3pool;
  10225. xi.vsub(xj,convex_to_sphere);
  10226. var normals = sj.faceNormals;
  10227. var faces = sj.faces;
  10228. var verts = sj.vertices;
  10229. var R = si.radius;
  10230. var penetrating_sides = [];
  10231. // if(convex_to_sphere.norm2() > si.boundingSphereRadius + sj.boundingSphereRadius){
  10232. // return;
  10233. // }
  10234. // Check corners
  10235. for(var i=0; i!==verts.length; i++){
  10236. var v = verts[i];
  10237. // World position of corner
  10238. var worldCorner = sphereConvex_worldCorner;
  10239. qj.vmult(v,worldCorner);
  10240. xj.vadd(worldCorner,worldCorner);
  10241. var sphere_to_corner = sphereConvex_sphereToCorner;
  10242. worldCorner.vsub(xi, sphere_to_corner);
  10243. if(sphere_to_corner.norm2() < R * R){
  10244. found = true;
  10245. var r = this.createContactEquation(bi,bj,si,sj);
  10246. r.ri.copy(sphere_to_corner);
  10247. r.ri.normalize();
  10248. r.ni.copy(r.ri);
  10249. r.ri.mult(R,r.ri);
  10250. worldCorner.vsub(xj,r.rj);
  10251. // Should be relative to the body.
  10252. r.ri.vadd(xi, r.ri);
  10253. r.ri.vsub(bi.position, r.ri);
  10254. // Should be relative to the body.
  10255. r.rj.vadd(xj, r.rj);
  10256. r.rj.vsub(bj.position, r.rj);
  10257. this.result.push(r);
  10258. this.createFrictionEquationsFromContact(r, this.frictionResult);
  10259. return;
  10260. }
  10261. }
  10262. // Check side (plane) intersections
  10263. var found = false;
  10264. for(var i=0, nfaces=faces.length; i!==nfaces && found===false; i++){
  10265. var normal = normals[i];
  10266. var face = faces[i];
  10267. // Get world-transformed normal of the face
  10268. var worldNormal = sphereConvex_worldNormal;
  10269. qj.vmult(normal,worldNormal);
  10270. // Get a world vertex from the face
  10271. var worldPoint = sphereConvex_worldPoint;
  10272. qj.vmult(verts[face[0]],worldPoint);
  10273. worldPoint.vadd(xj,worldPoint);
  10274. // Get a point on the sphere, closest to the face normal
  10275. var worldSpherePointClosestToPlane = sphereConvex_worldSpherePointClosestToPlane;
  10276. worldNormal.mult(-R, worldSpherePointClosestToPlane);
  10277. xi.vadd(worldSpherePointClosestToPlane, worldSpherePointClosestToPlane);
  10278. // Vector from a face point to the closest point on the sphere
  10279. var penetrationVec = sphereConvex_penetrationVec;
  10280. worldSpherePointClosestToPlane.vsub(worldPoint,penetrationVec);
  10281. // The penetration. Negative value means overlap.
  10282. var penetration = penetrationVec.dot(worldNormal);
  10283. var worldPointToSphere = sphereConvex_sphereToWorldPoint;
  10284. xi.vsub(worldPoint, worldPointToSphere);
  10285. if(penetration < 0 && worldPointToSphere.dot(worldNormal)>0){
  10286. // Intersects plane. Now check if the sphere is inside the face polygon
  10287. var faceVerts = []; // Face vertices, in world coords
  10288. for(var j=0, Nverts=face.length; j!==Nverts; j++){
  10289. var worldVertex = v3pool.get();
  10290. qj.vmult(verts[face[j]], worldVertex);
  10291. xj.vadd(worldVertex,worldVertex);
  10292. faceVerts.push(worldVertex);
  10293. }
  10294. if(pointInPolygon(faceVerts,worldNormal,xi)){ // Is the sphere center in the face polygon?
  10295. found = true;
  10296. var r = this.createContactEquation(bi,bj,si,sj);
  10297. worldNormal.mult(-R, r.ri); // Contact offset, from sphere center to contact
  10298. worldNormal.negate(r.ni); // Normal pointing out of sphere
  10299. var penetrationVec2 = v3pool.get();
  10300. worldNormal.mult(-penetration, penetrationVec2);
  10301. var penetrationSpherePoint = v3pool.get();
  10302. worldNormal.mult(-R, penetrationSpherePoint);
  10303. //xi.vsub(xj).vadd(penetrationSpherePoint).vadd(penetrationVec2 , r.rj);
  10304. xi.vsub(xj,r.rj);
  10305. r.rj.vadd(penetrationSpherePoint,r.rj);
  10306. r.rj.vadd(penetrationVec2 , r.rj);
  10307. // Should be relative to the body.
  10308. r.rj.vadd(xj, r.rj);
  10309. r.rj.vsub(bj.position, r.rj);
  10310. // Should be relative to the body.
  10311. r.ri.vadd(xi, r.ri);
  10312. r.ri.vsub(bi.position, r.ri);
  10313. v3pool.release(penetrationVec2);
  10314. v3pool.release(penetrationSpherePoint);
  10315. this.result.push(r);
  10316. this.createFrictionEquationsFromContact(r, this.frictionResult);
  10317. // Release world vertices
  10318. for(var j=0, Nfaceverts=faceVerts.length; j!==Nfaceverts; j++){
  10319. v3pool.release(faceVerts[j]);
  10320. }
  10321. return; // We only expect *one* face contact
  10322. } else {
  10323. // Edge?
  10324. for(var j=0; j!==face.length; j++){
  10325. // Get two world transformed vertices
  10326. var v1 = v3pool.get();
  10327. var v2 = v3pool.get();
  10328. qj.vmult(verts[face[(j+1)%face.length]], v1);
  10329. qj.vmult(verts[face[(j+2)%face.length]], v2);
  10330. xj.vadd(v1, v1);
  10331. xj.vadd(v2, v2);
  10332. // Construct edge vector
  10333. var edge = sphereConvex_edge;
  10334. v2.vsub(v1,edge);
  10335. // Construct the same vector, but normalized
  10336. var edgeUnit = sphereConvex_edgeUnit;
  10337. edge.unit(edgeUnit);
  10338. // p is xi projected onto the edge
  10339. var p = v3pool.get();
  10340. var v1_to_xi = v3pool.get();
  10341. xi.vsub(v1, v1_to_xi);
  10342. var dot = v1_to_xi.dot(edgeUnit);
  10343. edgeUnit.mult(dot, p);
  10344. p.vadd(v1, p);
  10345. // Compute a vector from p to the center of the sphere
  10346. var xi_to_p = v3pool.get();
  10347. p.vsub(xi, xi_to_p);
  10348. // Collision if the edge-sphere distance is less than the radius
  10349. // AND if p is in between v1 and v2
  10350. if(dot > 0 && dot*dot<edge.norm2() && xi_to_p.norm2() < R*R){ // Collision if the edge-sphere distance is less than the radius
  10351. // Edge contact!
  10352. var r = this.createContactEquation(bi,bj,si,sj);
  10353. p.vsub(xj,r.rj);
  10354. p.vsub(xi,r.ni);
  10355. r.ni.normalize();
  10356. r.ni.mult(R,r.ri);
  10357. // Should be relative to the body.
  10358. r.rj.vadd(xj, r.rj);
  10359. r.rj.vsub(bj.position, r.rj);
  10360. // Should be relative to the body.
  10361. r.ri.vadd(xi, r.ri);
  10362. r.ri.vsub(bi.position, r.ri);
  10363. this.result.push(r);
  10364. this.createFrictionEquationsFromContact(r, this.frictionResult);
  10365. // Release world vertices
  10366. for(var j=0, Nfaceverts=faceVerts.length; j!==Nfaceverts; j++){
  10367. v3pool.release(faceVerts[j]);
  10368. }
  10369. v3pool.release(v1);
  10370. v3pool.release(v2);
  10371. v3pool.release(p);
  10372. v3pool.release(xi_to_p);
  10373. v3pool.release(v1_to_xi);
  10374. return;
  10375. }
  10376. v3pool.release(v1);
  10377. v3pool.release(v2);
  10378. v3pool.release(p);
  10379. v3pool.release(xi_to_p);
  10380. v3pool.release(v1_to_xi);
  10381. }
  10382. }
  10383. // Release world vertices
  10384. for(var j=0, Nfaceverts=faceVerts.length; j!==Nfaceverts; j++){
  10385. v3pool.release(faceVerts[j]);
  10386. }
  10387. }
  10388. }
  10389. };
  10390. var planeBox_normal = new Vec3();
  10391. var plane_to_corner = new Vec3();
  10392. /**
  10393. * @method planeBox
  10394. * @param {Array} result
  10395. * @param {Shape} si
  10396. * @param {Shape} sj
  10397. * @param {Vec3} xi
  10398. * @param {Vec3} xj
  10399. * @param {Quaternion} qi
  10400. * @param {Quaternion} qj
  10401. * @param {Body} bi
  10402. * @param {Body} bj
  10403. */
  10404. Narrowphase.prototype[Shape.types.PLANE | Shape.types.BOX] =
  10405. Narrowphase.prototype.planeBox = function(si,sj,xi,xj,qi,qj,bi,bj){
  10406. sj.convexPolyhedronRepresentation.material = sj.material;
  10407. sj.convexPolyhedronRepresentation.collisionResponse = sj.collisionResponse;
  10408. this.planeConvex(si,sj.convexPolyhedronRepresentation,xi,xj,qi,qj,bi,bj);
  10409. };
  10410. var planeConvex_v = new Vec3();
  10411. var planeConvex_normal = new Vec3();
  10412. var planeConvex_relpos = new Vec3();
  10413. var planeConvex_projected = new Vec3();
  10414. /**
  10415. * @method planeConvex
  10416. * @param {Shape} si
  10417. * @param {Shape} sj
  10418. * @param {Vec3} xi
  10419. * @param {Vec3} xj
  10420. * @param {Quaternion} qi
  10421. * @param {Quaternion} qj
  10422. * @param {Body} bi
  10423. * @param {Body} bj
  10424. */
  10425. Narrowphase.prototype[Shape.types.PLANE | Shape.types.CONVEXPOLYHEDRON] =
  10426. Narrowphase.prototype.planeConvex = function(
  10427. planeShape,
  10428. convexShape,
  10429. planePosition,
  10430. convexPosition,
  10431. planeQuat,
  10432. convexQuat,
  10433. planeBody,
  10434. convexBody
  10435. ){
  10436. // Simply return the points behind the plane.
  10437. var worldVertex = planeConvex_v,
  10438. worldNormal = planeConvex_normal;
  10439. worldNormal.set(0,0,1);
  10440. planeQuat.vmult(worldNormal,worldNormal); // Turn normal according to plane orientation
  10441. var numContacts = 0;
  10442. var relpos = planeConvex_relpos;
  10443. for(var i = 0; i !== convexShape.vertices.length; i++){
  10444. // Get world convex vertex
  10445. worldVertex.copy(convexShape.vertices[i]);
  10446. convexQuat.vmult(worldVertex, worldVertex);
  10447. convexPosition.vadd(worldVertex, worldVertex);
  10448. worldVertex.vsub(planePosition, relpos);
  10449. var dot = worldNormal.dot(relpos);
  10450. if(dot <= 0.0){
  10451. var r = this.createContactEquation(planeBody, convexBody, planeShape, convexShape);
  10452. // Get vertex position projected on plane
  10453. var projected = planeConvex_projected;
  10454. worldNormal.mult(worldNormal.dot(relpos),projected);
  10455. worldVertex.vsub(projected, projected);
  10456. projected.vsub(planePosition, r.ri); // From plane to vertex projected on plane
  10457. r.ni.copy(worldNormal); // Contact normal is the plane normal out from plane
  10458. // rj is now just the vector from the convex center to the vertex
  10459. worldVertex.vsub(convexPosition, r.rj);
  10460. // Make it relative to the body
  10461. r.ri.vadd(planePosition, r.ri);
  10462. r.ri.vsub(planeBody.position, r.ri);
  10463. r.rj.vadd(convexPosition, r.rj);
  10464. r.rj.vsub(convexBody.position, r.rj);
  10465. this.result.push(r);
  10466. numContacts++;
  10467. if(!this.enableFrictionReduction){
  10468. this.createFrictionEquationsFromContact(r, this.frictionResult);
  10469. }
  10470. }
  10471. }
  10472. if(this.enableFrictionReduction && numContacts){
  10473. this.createFrictionFromAverage(numContacts);
  10474. }
  10475. };
  10476. var convexConvex_sepAxis = new Vec3();
  10477. var convexConvex_q = new Vec3();
  10478. /**
  10479. * @method convexConvex
  10480. * @param {Shape} si
  10481. * @param {Shape} sj
  10482. * @param {Vec3} xi
  10483. * @param {Vec3} xj
  10484. * @param {Quaternion} qi
  10485. * @param {Quaternion} qj
  10486. * @param {Body} bi
  10487. * @param {Body} bj
  10488. */
  10489. Narrowphase.prototype[Shape.types.CONVEXPOLYHEDRON] =
  10490. Narrowphase.prototype.convexConvex = function(si,sj,xi,xj,qi,qj,bi,bj,rsi,rsj,faceListA,faceListB){
  10491. var sepAxis = convexConvex_sepAxis;
  10492. if(xi.distanceTo(xj) > si.boundingSphereRadius + sj.boundingSphereRadius){
  10493. return;
  10494. }
  10495. if(si.findSeparatingAxis(sj,xi,qi,xj,qj,sepAxis,faceListA,faceListB)){
  10496. var res = [];
  10497. var q = convexConvex_q;
  10498. si.clipAgainstHull(xi,qi,sj,xj,qj,sepAxis,-100,100,res);
  10499. var numContacts = 0;
  10500. for(var j = 0; j !== res.length; j++){
  10501. var r = this.createContactEquation(bi,bj,si,sj,rsi,rsj),
  10502. ri = r.ri,
  10503. rj = r.rj;
  10504. sepAxis.negate(r.ni);
  10505. res[j].normal.negate(q);
  10506. q.mult(res[j].depth, q);
  10507. res[j].point.vadd(q, ri);
  10508. rj.copy(res[j].point);
  10509. // Contact points are in world coordinates. Transform back to relative
  10510. ri.vsub(xi,ri);
  10511. rj.vsub(xj,rj);
  10512. // Make relative to bodies
  10513. ri.vadd(xi, ri);
  10514. ri.vsub(bi.position, ri);
  10515. rj.vadd(xj, rj);
  10516. rj.vsub(bj.position, rj);
  10517. this.result.push(r);
  10518. numContacts++;
  10519. if(!this.enableFrictionReduction){
  10520. this.createFrictionEquationsFromContact(r, this.frictionResult);
  10521. }
  10522. }
  10523. if(this.enableFrictionReduction && numContacts){
  10524. this.createFrictionFromAverage(numContacts);
  10525. }
  10526. }
  10527. };
  10528. /**
  10529. * @method convexTrimesh
  10530. * @param {Array} result
  10531. * @param {Shape} si
  10532. * @param {Shape} sj
  10533. * @param {Vec3} xi
  10534. * @param {Vec3} xj
  10535. * @param {Quaternion} qi
  10536. * @param {Quaternion} qj
  10537. * @param {Body} bi
  10538. * @param {Body} bj
  10539. */
  10540. // Narrowphase.prototype[Shape.types.CONVEXPOLYHEDRON | Shape.types.TRIMESH] =
  10541. // Narrowphase.prototype.convexTrimesh = function(si,sj,xi,xj,qi,qj,bi,bj,rsi,rsj,faceListA,faceListB){
  10542. // var sepAxis = convexConvex_sepAxis;
  10543. // if(xi.distanceTo(xj) > si.boundingSphereRadius + sj.boundingSphereRadius){
  10544. // return;
  10545. // }
  10546. // // Construct a temp hull for each triangle
  10547. // var hullB = new ConvexPolyhedron();
  10548. // hullB.faces = [[0,1,2]];
  10549. // var va = new Vec3();
  10550. // var vb = new Vec3();
  10551. // var vc = new Vec3();
  10552. // hullB.vertices = [
  10553. // va,
  10554. // vb,
  10555. // vc
  10556. // ];
  10557. // for (var i = 0; i < sj.indices.length / 3; i++) {
  10558. // var triangleNormal = new Vec3();
  10559. // sj.getNormal(i, triangleNormal);
  10560. // hullB.faceNormals = [triangleNormal];
  10561. // sj.getTriangleVertices(i, va, vb, vc);
  10562. // var d = si.testSepAxis(triangleNormal, hullB, xi, qi, xj, qj);
  10563. // if(!d){
  10564. // triangleNormal.scale(-1, triangleNormal);
  10565. // d = si.testSepAxis(triangleNormal, hullB, xi, qi, xj, qj);
  10566. // if(!d){
  10567. // continue;
  10568. // }
  10569. // }
  10570. // var res = [];
  10571. // var q = convexConvex_q;
  10572. // si.clipAgainstHull(xi,qi,hullB,xj,qj,triangleNormal,-100,100,res);
  10573. // for(var j = 0; j !== res.length; j++){
  10574. // var r = this.createContactEquation(bi,bj,si,sj,rsi,rsj),
  10575. // ri = r.ri,
  10576. // rj = r.rj;
  10577. // r.ni.copy(triangleNormal);
  10578. // r.ni.negate(r.ni);
  10579. // res[j].normal.negate(q);
  10580. // q.mult(res[j].depth, q);
  10581. // res[j].point.vadd(q, ri);
  10582. // rj.copy(res[j].point);
  10583. // // Contact points are in world coordinates. Transform back to relative
  10584. // ri.vsub(xi,ri);
  10585. // rj.vsub(xj,rj);
  10586. // // Make relative to bodies
  10587. // ri.vadd(xi, ri);
  10588. // ri.vsub(bi.position, ri);
  10589. // rj.vadd(xj, rj);
  10590. // rj.vsub(bj.position, rj);
  10591. // result.push(r);
  10592. // }
  10593. // }
  10594. // };
  10595. var particlePlane_normal = new Vec3();
  10596. var particlePlane_relpos = new Vec3();
  10597. var particlePlane_projected = new Vec3();
  10598. /**
  10599. * @method particlePlane
  10600. * @param {Array} result
  10601. * @param {Shape} si
  10602. * @param {Shape} sj
  10603. * @param {Vec3} xi
  10604. * @param {Vec3} xj
  10605. * @param {Quaternion} qi
  10606. * @param {Quaternion} qj
  10607. * @param {Body} bi
  10608. * @param {Body} bj
  10609. */
  10610. Narrowphase.prototype[Shape.types.PLANE | Shape.types.PARTICLE] =
  10611. Narrowphase.prototype.planeParticle = function(sj,si,xj,xi,qj,qi,bj,bi){
  10612. var normal = particlePlane_normal;
  10613. normal.set(0,0,1);
  10614. bj.quaternion.vmult(normal,normal); // Turn normal according to plane orientation
  10615. var relpos = particlePlane_relpos;
  10616. xi.vsub(bj.position,relpos);
  10617. var dot = normal.dot(relpos);
  10618. if(dot <= 0.0){
  10619. var r = this.createContactEquation(bi,bj,si,sj);
  10620. r.ni.copy(normal); // Contact normal is the plane normal
  10621. r.ni.negate(r.ni);
  10622. r.ri.set(0,0,0); // Center of particle
  10623. // Get particle position projected on plane
  10624. var projected = particlePlane_projected;
  10625. normal.mult(normal.dot(xi),projected);
  10626. xi.vsub(projected,projected);
  10627. //projected.vadd(bj.position,projected);
  10628. // rj is now the projected world position minus plane position
  10629. r.rj.copy(projected);
  10630. this.result.push(r);
  10631. this.createFrictionEquationsFromContact(r, this.frictionResult);
  10632. }
  10633. };
  10634. var particleSphere_normal = new Vec3();
  10635. /**
  10636. * @method particleSphere
  10637. * @param {Array} result
  10638. * @param {Shape} si
  10639. * @param {Shape} sj
  10640. * @param {Vec3} xi
  10641. * @param {Vec3} xj
  10642. * @param {Quaternion} qi
  10643. * @param {Quaternion} qj
  10644. * @param {Body} bi
  10645. * @param {Body} bj
  10646. */
  10647. Narrowphase.prototype[Shape.types.PARTICLE | Shape.types.SPHERE] =
  10648. Narrowphase.prototype.sphereParticle = function(sj,si,xj,xi,qj,qi,bj,bi){
  10649. // The normal is the unit vector from sphere center to particle center
  10650. var normal = particleSphere_normal;
  10651. normal.set(0,0,1);
  10652. xi.vsub(xj,normal);
  10653. var lengthSquared = normal.norm2();
  10654. if(lengthSquared <= sj.radius * sj.radius){
  10655. var r = this.createContactEquation(bi,bj,si,sj);
  10656. normal.normalize();
  10657. r.rj.copy(normal);
  10658. r.rj.mult(sj.radius,r.rj);
  10659. r.ni.copy(normal); // Contact normal
  10660. r.ni.negate(r.ni);
  10661. r.ri.set(0,0,0); // Center of particle
  10662. this.result.push(r);
  10663. this.createFrictionEquationsFromContact(r, this.frictionResult);
  10664. }
  10665. };
  10666. // WIP
  10667. var cqj = new Quaternion();
  10668. var convexParticle_local = new Vec3();
  10669. var convexParticle_normal = new Vec3();
  10670. var convexParticle_penetratedFaceNormal = new Vec3();
  10671. var convexParticle_vertexToParticle = new Vec3();
  10672. var convexParticle_worldPenetrationVec = new Vec3();
  10673. /**
  10674. * @method convexParticle
  10675. * @param {Array} result
  10676. * @param {Shape} si
  10677. * @param {Shape} sj
  10678. * @param {Vec3} xi
  10679. * @param {Vec3} xj
  10680. * @param {Quaternion} qi
  10681. * @param {Quaternion} qj
  10682. * @param {Body} bi
  10683. * @param {Body} bj
  10684. */
  10685. Narrowphase.prototype[Shape.types.PARTICLE | Shape.types.CONVEXPOLYHEDRON] =
  10686. Narrowphase.prototype.convexParticle = function(sj,si,xj,xi,qj,qi,bj,bi){
  10687. var penetratedFaceIndex = -1;
  10688. var penetratedFaceNormal = convexParticle_penetratedFaceNormal;
  10689. var worldPenetrationVec = convexParticle_worldPenetrationVec;
  10690. var minPenetration = null;
  10691. var numDetectedFaces = 0;
  10692. // Convert particle position xi to local coords in the convex
  10693. var local = convexParticle_local;
  10694. local.copy(xi);
  10695. local.vsub(xj,local); // Convert position to relative the convex origin
  10696. qj.conjugate(cqj);
  10697. cqj.vmult(local,local);
  10698. if(sj.pointIsInside(local)){
  10699. if(sj.worldVerticesNeedsUpdate){
  10700. sj.computeWorldVertices(xj,qj);
  10701. }
  10702. if(sj.worldFaceNormalsNeedsUpdate){
  10703. sj.computeWorldFaceNormals(qj);
  10704. }
  10705. // For each world polygon in the polyhedra
  10706. for(var i=0,nfaces=sj.faces.length; i!==nfaces; i++){
  10707. // Construct world face vertices
  10708. var verts = [ sj.worldVertices[ sj.faces[i][0] ] ];
  10709. var normal = sj.worldFaceNormals[i];
  10710. // Check how much the particle penetrates the polygon plane.
  10711. xi.vsub(verts[0],convexParticle_vertexToParticle);
  10712. var penetration = -normal.dot(convexParticle_vertexToParticle);
  10713. if(minPenetration===null || Math.abs(penetration)<Math.abs(minPenetration)){
  10714. minPenetration = penetration;
  10715. penetratedFaceIndex = i;
  10716. penetratedFaceNormal.copy(normal);
  10717. numDetectedFaces++;
  10718. }
  10719. }
  10720. if(penetratedFaceIndex!==-1){
  10721. // Setup contact
  10722. var r = this.createContactEquation(bi,bj,si,sj);
  10723. penetratedFaceNormal.mult(minPenetration, worldPenetrationVec);
  10724. // rj is the particle position projected to the face
  10725. worldPenetrationVec.vadd(xi,worldPenetrationVec);
  10726. worldPenetrationVec.vsub(xj,worldPenetrationVec);
  10727. r.rj.copy(worldPenetrationVec);
  10728. //var projectedToFace = xi.vsub(xj).vadd(worldPenetrationVec);
  10729. //projectedToFace.copy(r.rj);
  10730. //qj.vmult(r.rj,r.rj);
  10731. penetratedFaceNormal.negate( r.ni ); // Contact normal
  10732. r.ri.set(0,0,0); // Center of particle
  10733. var ri = r.ri,
  10734. rj = r.rj;
  10735. // Make relative to bodies
  10736. ri.vadd(xi, ri);
  10737. ri.vsub(bi.position, ri);
  10738. rj.vadd(xj, rj);
  10739. rj.vsub(bj.position, rj);
  10740. this.result.push(r);
  10741. this.createFrictionEquationsFromContact(r, this.frictionResult);
  10742. } else {
  10743. console.warn("Point found inside convex, but did not find penetrating face!");
  10744. }
  10745. }
  10746. };
  10747. Narrowphase.prototype[Shape.types.BOX | Shape.types.HEIGHTFIELD] =
  10748. Narrowphase.prototype.boxHeightfield = function (si,sj,xi,xj,qi,qj,bi,bj){
  10749. si.convexPolyhedronRepresentation.material = si.material;
  10750. si.convexPolyhedronRepresentation.collisionResponse = si.collisionResponse;
  10751. this.convexHeightfield(si.convexPolyhedronRepresentation,sj,xi,xj,qi,qj,bi,bj);
  10752. };
  10753. var convexHeightfield_tmp1 = new Vec3();
  10754. var convexHeightfield_tmp2 = new Vec3();
  10755. var convexHeightfield_faceList = [0];
  10756. /**
  10757. * @method convexHeightfield
  10758. */
  10759. Narrowphase.prototype[Shape.types.CONVEXPOLYHEDRON | Shape.types.HEIGHTFIELD] =
  10760. Narrowphase.prototype.convexHeightfield = function (
  10761. convexShape,
  10762. hfShape,
  10763. convexPos,
  10764. hfPos,
  10765. convexQuat,
  10766. hfQuat,
  10767. convexBody,
  10768. hfBody
  10769. ){
  10770. var data = hfShape.data,
  10771. w = hfShape.elementSize,
  10772. radius = convexShape.boundingSphereRadius,
  10773. worldPillarOffset = convexHeightfield_tmp2,
  10774. faceList = convexHeightfield_faceList;
  10775. // Get sphere position to heightfield local!
  10776. var localConvexPos = convexHeightfield_tmp1;
  10777. Transform.pointToLocalFrame(hfPos, hfQuat, convexPos, localConvexPos);
  10778. // Get the index of the data points to test against
  10779. var iMinX = Math.floor((localConvexPos.x - radius) / w) - 1,
  10780. iMaxX = Math.ceil((localConvexPos.x + radius) / w) + 1,
  10781. iMinY = Math.floor((localConvexPos.y - radius) / w) - 1,
  10782. iMaxY = Math.ceil((localConvexPos.y + radius) / w) + 1;
  10783. // Bail out if we are out of the terrain
  10784. if(iMaxX < 0 || iMaxY < 0 || iMinX > data.length || iMinY > data[0].length){
  10785. return;
  10786. }
  10787. // Clamp index to edges
  10788. if(iMinX < 0){ iMinX = 0; }
  10789. if(iMaxX < 0){ iMaxX = 0; }
  10790. if(iMinY < 0){ iMinY = 0; }
  10791. if(iMaxY < 0){ iMaxY = 0; }
  10792. if(iMinX >= data.length){ iMinX = data.length - 1; }
  10793. if(iMaxX >= data.length){ iMaxX = data.length - 1; }
  10794. if(iMaxY >= data[0].length){ iMaxY = data[0].length - 1; }
  10795. if(iMinY >= data[0].length){ iMinY = data[0].length - 1; }
  10796. var minMax = [];
  10797. hfShape.getRectMinMax(iMinX, iMinY, iMaxX, iMaxY, minMax);
  10798. var min = minMax[0];
  10799. var max = minMax[1];
  10800. // Bail out if we're cant touch the bounding height box
  10801. if(localConvexPos.z - radius > max || localConvexPos.z + radius < min){
  10802. return;
  10803. }
  10804. for(var i = iMinX; i < iMaxX; i++){
  10805. for(var j = iMinY; j < iMaxY; j++){
  10806. // Lower triangle
  10807. hfShape.getConvexTrianglePillar(i, j, false);
  10808. Transform.pointToWorldFrame(hfPos, hfQuat, hfShape.pillarOffset, worldPillarOffset);
  10809. if (convexPos.distanceTo(worldPillarOffset) < hfShape.pillarConvex.boundingSphereRadius + convexShape.boundingSphereRadius) {
  10810. this.convexConvex(convexShape, hfShape.pillarConvex, convexPos, worldPillarOffset, convexQuat, hfQuat, convexBody, hfBody, null, null, faceList, null);
  10811. }
  10812. // Upper triangle
  10813. hfShape.getConvexTrianglePillar(i, j, true);
  10814. Transform.pointToWorldFrame(hfPos, hfQuat, hfShape.pillarOffset, worldPillarOffset);
  10815. if (convexPos.distanceTo(worldPillarOffset) < hfShape.pillarConvex.boundingSphereRadius + convexShape.boundingSphereRadius) {
  10816. this.convexConvex(convexShape, hfShape.pillarConvex, convexPos, worldPillarOffset, convexQuat, hfQuat, convexBody, hfBody, null, null, faceList, null);
  10817. }
  10818. }
  10819. }
  10820. };
  10821. var sphereHeightfield_tmp1 = new Vec3();
  10822. var sphereHeightfield_tmp2 = new Vec3();
  10823. /**
  10824. * @method sphereHeightfield
  10825. */
  10826. Narrowphase.prototype[Shape.types.SPHERE | Shape.types.HEIGHTFIELD] =
  10827. Narrowphase.prototype.sphereHeightfield = function (
  10828. sphereShape,
  10829. hfShape,
  10830. spherePos,
  10831. hfPos,
  10832. sphereQuat,
  10833. hfQuat,
  10834. sphereBody,
  10835. hfBody
  10836. ){
  10837. var data = hfShape.data,
  10838. radius = sphereShape.radius,
  10839. w = hfShape.elementSize,
  10840. worldPillarOffset = sphereHeightfield_tmp2;
  10841. // Get sphere position to heightfield local!
  10842. var localSpherePos = sphereHeightfield_tmp1;
  10843. Transform.pointToLocalFrame(hfPos, hfQuat, spherePos, localSpherePos);
  10844. // Get the index of the data points to test against
  10845. var iMinX = Math.floor((localSpherePos.x - radius) / w) - 1,
  10846. iMaxX = Math.ceil((localSpherePos.x + radius) / w) + 1,
  10847. iMinY = Math.floor((localSpherePos.y - radius) / w) - 1,
  10848. iMaxY = Math.ceil((localSpherePos.y + radius) / w) + 1;
  10849. // Bail out if we are out of the terrain
  10850. if(iMaxX < 0 || iMaxY < 0 || iMinX > data.length || iMaxY > data[0].length){
  10851. return;
  10852. }
  10853. // Clamp index to edges
  10854. if(iMinX < 0){ iMinX = 0; }
  10855. if(iMaxX < 0){ iMaxX = 0; }
  10856. if(iMinY < 0){ iMinY = 0; }
  10857. if(iMaxY < 0){ iMaxY = 0; }
  10858. if(iMinX >= data.length){ iMinX = data.length - 1; }
  10859. if(iMaxX >= data.length){ iMaxX = data.length - 1; }
  10860. if(iMaxY >= data[0].length){ iMaxY = data[0].length - 1; }
  10861. if(iMinY >= data[0].length){ iMinY = data[0].length - 1; }
  10862. var minMax = [];
  10863. hfShape.getRectMinMax(iMinX, iMinY, iMaxX, iMaxY, minMax);
  10864. var min = minMax[0];
  10865. var max = minMax[1];
  10866. // Bail out if we're cant touch the bounding height box
  10867. if(localSpherePos.z - radius > max || localSpherePos.z + radius < min){
  10868. return;
  10869. }
  10870. var result = this.result;
  10871. for(var i = iMinX; i < iMaxX; i++){
  10872. for(var j = iMinY; j < iMaxY; j++){
  10873. var numContactsBefore = result.length;
  10874. // Lower triangle
  10875. hfShape.getConvexTrianglePillar(i, j, false);
  10876. Transform.pointToWorldFrame(hfPos, hfQuat, hfShape.pillarOffset, worldPillarOffset);
  10877. if (spherePos.distanceTo(worldPillarOffset) < hfShape.pillarConvex.boundingSphereRadius + sphereShape.boundingSphereRadius) {
  10878. this.sphereConvex(sphereShape, hfShape.pillarConvex, spherePos, worldPillarOffset, sphereQuat, hfQuat, sphereBody, hfBody);
  10879. }
  10880. // Upper triangle
  10881. hfShape.getConvexTrianglePillar(i, j, true);
  10882. Transform.pointToWorldFrame(hfPos, hfQuat, hfShape.pillarOffset, worldPillarOffset);
  10883. if (spherePos.distanceTo(worldPillarOffset) < hfShape.pillarConvex.boundingSphereRadius + sphereShape.boundingSphereRadius) {
  10884. this.sphereConvex(sphereShape, hfShape.pillarConvex, spherePos, worldPillarOffset, sphereQuat, hfQuat, sphereBody, hfBody);
  10885. }
  10886. var numContacts = result.length - numContactsBefore;
  10887. if(numContacts > 2){
  10888. return;
  10889. }
  10890. /*
  10891. // Skip all but 1
  10892. for (var k = 0; k < numContacts - 1; k++) {
  10893. result.pop();
  10894. }
  10895. */
  10896. }
  10897. }
  10898. };
  10899. },{"../collision/AABB":3,"../collision/Ray":9,"../equations/ContactEquation":19,"../equations/FrictionEquation":21,"../math/Quaternion":28,"../math/Transform":29,"../math/Vec3":30,"../shapes/ConvexPolyhedron":38,"../shapes/Shape":43,"../solver/Solver":47,"../utils/Vec3Pool":54}],56:[function(_dereq_,module,exports){
  10900. /* global performance */
  10901. module.exports = World;
  10902. var Shape = _dereq_('../shapes/Shape');
  10903. var Vec3 = _dereq_('../math/Vec3');
  10904. var Quaternion = _dereq_('../math/Quaternion');
  10905. var GSSolver = _dereq_('../solver/GSSolver');
  10906. var Vec3Pool = _dereq_('../utils/Vec3Pool');
  10907. var ContactEquation = _dereq_('../equations/ContactEquation');
  10908. var FrictionEquation = _dereq_('../equations/FrictionEquation');
  10909. var Narrowphase = _dereq_('./Narrowphase');
  10910. var EventTarget = _dereq_('../utils/EventTarget');
  10911. var ArrayCollisionMatrix = _dereq_('../collision/ArrayCollisionMatrix');
  10912. var Material = _dereq_('../material/Material');
  10913. var ContactMaterial = _dereq_('../material/ContactMaterial');
  10914. var Body = _dereq_('../objects/Body');
  10915. var TupleDictionary = _dereq_('../utils/TupleDictionary');
  10916. var RaycastResult = _dereq_('../collision/RaycastResult');
  10917. var AABB = _dereq_('../collision/AABB');
  10918. var Ray = _dereq_('../collision/Ray');
  10919. var NaiveBroadphase = _dereq_('../collision/NaiveBroadphase');
  10920. /**
  10921. * The physics world
  10922. * @class World
  10923. * @constructor
  10924. * @extends EventTarget
  10925. */
  10926. function World(){
  10927. EventTarget.apply(this);
  10928. /**
  10929. * Currently / last used timestep. Is set to -1 if not available. This value is updated before each internal step, which means that it is "fresh" inside event callbacks.
  10930. * @property {Number} dt
  10931. */
  10932. this.dt = -1;
  10933. /**
  10934. * Makes bodies go to sleep when they've been inactive
  10935. * @property allowSleep
  10936. * @type {Boolean}
  10937. */
  10938. this.allowSleep = false;
  10939. /**
  10940. * All the current contacts (instances of ContactEquation) in the world.
  10941. * @property contacts
  10942. * @type {Array}
  10943. */
  10944. this.contacts = [];
  10945. this.frictionEquations = [];
  10946. /**
  10947. * How often to normalize quaternions. Set to 0 for every step, 1 for every second etc.. A larger value increases performance. If bodies tend to explode, set to a smaller value (zero to be sure nothing can go wrong).
  10948. * @property quatNormalizeSkip
  10949. * @type {Number}
  10950. */
  10951. this.quatNormalizeSkip = 0;
  10952. /**
  10953. * Set to true to use fast quaternion normalization. It is often enough accurate to use. If bodies tend to explode, set to false.
  10954. * @property quatNormalizeFast
  10955. * @type {Boolean}
  10956. * @see Quaternion.normalizeFast
  10957. * @see Quaternion.normalize
  10958. */
  10959. this.quatNormalizeFast = false;
  10960. /**
  10961. * The wall-clock time since simulation start
  10962. * @property time
  10963. * @type {Number}
  10964. */
  10965. this.time = 0.0;
  10966. /**
  10967. * Number of timesteps taken since start
  10968. * @property stepnumber
  10969. * @type {Number}
  10970. */
  10971. this.stepnumber = 0;
  10972. /// Default and last timestep sizes
  10973. this.default_dt = 1/60;
  10974. this.nextId = 0;
  10975. /**
  10976. * @property gravity
  10977. * @type {Vec3}
  10978. */
  10979. this.gravity = new Vec3();
  10980. /**
  10981. * @property broadphase
  10982. * @type {Broadphase}
  10983. */
  10984. this.broadphase = new NaiveBroadphase();
  10985. /**
  10986. * @property bodies
  10987. * @type {Array}
  10988. */
  10989. this.bodies = [];
  10990. /**
  10991. * @property solver
  10992. * @type {Solver}
  10993. */
  10994. this.solver = new GSSolver();
  10995. /**
  10996. * @property constraints
  10997. * @type {Array}
  10998. */
  10999. this.constraints = [];
  11000. /**
  11001. * @property narrowphase
  11002. * @type {Narrowphase}
  11003. */
  11004. this.narrowphase = new Narrowphase(this);
  11005. /**
  11006. * @property {ArrayCollisionMatrix} collisionMatrix
  11007. * @type {ArrayCollisionMatrix}
  11008. */
  11009. this.collisionMatrix = new ArrayCollisionMatrix();
  11010. /**
  11011. * CollisionMatrix from the previous step.
  11012. * @property {ArrayCollisionMatrix} collisionMatrixPrevious
  11013. * @type {ArrayCollisionMatrix}
  11014. */
  11015. this.collisionMatrixPrevious = new ArrayCollisionMatrix();
  11016. /**
  11017. * All added materials
  11018. * @property materials
  11019. * @type {Array}
  11020. */
  11021. this.materials = [];
  11022. /**
  11023. * @property contactmaterials
  11024. * @type {Array}
  11025. */
  11026. this.contactmaterials = [];
  11027. /**
  11028. * Used to look up a ContactMaterial given two instances of Material.
  11029. * @property {TupleDictionary} contactMaterialTable
  11030. */
  11031. this.contactMaterialTable = new TupleDictionary();
  11032. this.defaultMaterial = new Material("default");
  11033. /**
  11034. * This contact material is used if no suitable contactmaterial is found for a contact.
  11035. * @property defaultContactMaterial
  11036. * @type {ContactMaterial}
  11037. */
  11038. this.defaultContactMaterial = new ContactMaterial(this.defaultMaterial, this.defaultMaterial, { friction: 0.3, restitution: 0.0 });
  11039. /**
  11040. * @property doProfiling
  11041. * @type {Boolean}
  11042. */
  11043. this.doProfiling = false;
  11044. /**
  11045. * @property profile
  11046. * @type {Object}
  11047. */
  11048. this.profile = {
  11049. solve:0,
  11050. makeContactConstraints:0,
  11051. broadphase:0,
  11052. integrate:0,
  11053. narrowphase:0,
  11054. };
  11055. /**
  11056. * @property subsystems
  11057. * @type {Array}
  11058. */
  11059. this.subsystems = [];
  11060. this.addBodyEvent = {
  11061. type:"addBody",
  11062. body : null,
  11063. };
  11064. this.removeBodyEvent = {
  11065. type:"removeBody",
  11066. body : null,
  11067. };
  11068. }
  11069. World.prototype = new EventTarget();
  11070. // Temp stuff
  11071. var tmpAABB1 = new AABB();
  11072. var tmpArray1 = [];
  11073. var tmpRay = new Ray();
  11074. /**
  11075. * Get the contact material between materials m1 and m2
  11076. * @method getContactMaterial
  11077. * @param {Material} m1
  11078. * @param {Material} m2
  11079. * @return {ContactMaterial} The contact material if it was found.
  11080. */
  11081. World.prototype.getContactMaterial = function(m1,m2){
  11082. return this.contactMaterialTable.get(m1.id,m2.id); //this.contactmaterials[this.mats2cmat[i+j*this.materials.length]];
  11083. };
  11084. /**
  11085. * Get number of objects in the world.
  11086. * @method numObjects
  11087. * @return {Number}
  11088. * @deprecated
  11089. */
  11090. World.prototype.numObjects = function(){
  11091. return this.bodies.length;
  11092. };
  11093. /**
  11094. * Store old collision state info
  11095. * @method collisionMatrixTick
  11096. */
  11097. World.prototype.collisionMatrixTick = function(){
  11098. var temp = this.collisionMatrixPrevious;
  11099. this.collisionMatrixPrevious = this.collisionMatrix;
  11100. this.collisionMatrix = temp;
  11101. this.collisionMatrix.reset();
  11102. };
  11103. /**
  11104. * Add a rigid body to the simulation.
  11105. * @method add
  11106. * @param {Body} body
  11107. * @todo If the simulation has not yet started, why recrete and copy arrays for each body? Accumulate in dynamic arrays in this case.
  11108. * @todo Adding an array of bodies should be possible. This would save some loops too
  11109. * @deprecated Use .addBody instead
  11110. */
  11111. World.prototype.add = World.prototype.addBody = function(body){
  11112. if(this.bodies.indexOf(body) !== -1){
  11113. return;
  11114. }
  11115. body.index = this.bodies.length;
  11116. this.bodies.push(body);
  11117. body.world = this;
  11118. body.initPosition.copy(body.position);
  11119. body.initVelocity.copy(body.velocity);
  11120. body.timeLastSleepy = this.time;
  11121. if(body instanceof Body){
  11122. body.initAngularVelocity.copy(body.angularVelocity);
  11123. body.initQuaternion.copy(body.quaternion);
  11124. }
  11125. this.collisionMatrix.setNumObjects(this.bodies.length);
  11126. this.addBodyEvent.body = body;
  11127. this.dispatchEvent(this.addBodyEvent);
  11128. };
  11129. /**
  11130. * Add a constraint to the simulation.
  11131. * @method addConstraint
  11132. * @param {Constraint} c
  11133. */
  11134. World.prototype.addConstraint = function(c){
  11135. this.constraints.push(c);
  11136. };
  11137. /**
  11138. * Removes a constraint
  11139. * @method removeConstraint
  11140. * @param {Constraint} c
  11141. */
  11142. World.prototype.removeConstraint = function(c){
  11143. var idx = this.constraints.indexOf(c);
  11144. if(idx!==-1){
  11145. this.constraints.splice(idx,1);
  11146. }
  11147. };
  11148. /**
  11149. * Raycast test
  11150. * @method rayTest
  11151. * @param {Vec3} from
  11152. * @param {Vec3} to
  11153. * @param {Function|RaycastResult} result
  11154. * @deprecated Use .raycastAll, .raycastClosest or .raycastAny instead.
  11155. */
  11156. World.prototype.rayTest = function(from, to, result){
  11157. if(result instanceof RaycastResult){
  11158. // Do raycastclosest
  11159. this.raycastClosest(from, to, {
  11160. skipBackfaces: true
  11161. }, result);
  11162. } else {
  11163. // Do raycastAll
  11164. this.raycastAll(from, to, {
  11165. skipBackfaces: true
  11166. }, result);
  11167. }
  11168. };
  11169. /**
  11170. * Ray cast against all bodies. The provided callback will be executed for each hit with a RaycastResult as single argument.
  11171. * @method raycastAll
  11172. * @param {Vec3} from
  11173. * @param {Vec3} to
  11174. * @param {Object} options
  11175. * @param {number} [options.collisionFilterMask=-1]
  11176. * @param {number} [options.collisionFilterGroup=-1]
  11177. * @param {boolean} [options.skipBackfaces=false]
  11178. * @param {boolean} [options.checkCollisionResponse=true]
  11179. * @param {Function} callback
  11180. * @return {boolean} True if any body was hit.
  11181. */
  11182. World.prototype.raycastAll = function(from, to, options, callback){
  11183. options.mode = Ray.ALL;
  11184. options.from = from;
  11185. options.to = to;
  11186. options.callback = callback;
  11187. return tmpRay.intersectWorld(this, options);
  11188. };
  11189. /**
  11190. * Ray cast, and stop at the first result. Note that the order is random - but the method is fast.
  11191. * @method raycastAny
  11192. * @param {Vec3} from
  11193. * @param {Vec3} to
  11194. * @param {Object} options
  11195. * @param {number} [options.collisionFilterMask=-1]
  11196. * @param {number} [options.collisionFilterGroup=-1]
  11197. * @param {boolean} [options.skipBackfaces=false]
  11198. * @param {boolean} [options.checkCollisionResponse=true]
  11199. * @param {RaycastResult} result
  11200. * @return {boolean} True if any body was hit.
  11201. */
  11202. World.prototype.raycastAny = function(from, to, options, result){
  11203. options.mode = Ray.ANY;
  11204. options.from = from;
  11205. options.to = to;
  11206. options.result = result;
  11207. return tmpRay.intersectWorld(this, options);
  11208. };
  11209. /**
  11210. * Ray cast, and return information of the closest hit.
  11211. * @method raycastClosest
  11212. * @param {Vec3} from
  11213. * @param {Vec3} to
  11214. * @param {Object} options
  11215. * @param {number} [options.collisionFilterMask=-1]
  11216. * @param {number} [options.collisionFilterGroup=-1]
  11217. * @param {boolean} [options.skipBackfaces=false]
  11218. * @param {boolean} [options.checkCollisionResponse=true]
  11219. * @param {RaycastResult} result
  11220. * @return {boolean} True if any body was hit.
  11221. */
  11222. World.prototype.raycastClosest = function(from, to, options, result){
  11223. options.mode = Ray.CLOSEST;
  11224. options.from = from;
  11225. options.to = to;
  11226. options.result = result;
  11227. return tmpRay.intersectWorld(this, options);
  11228. };
  11229. /**
  11230. * Remove a rigid body from the simulation.
  11231. * @method remove
  11232. * @param {Body} body
  11233. * @deprecated Use .removeBody instead
  11234. */
  11235. World.prototype.remove = function(body){
  11236. body.world = null;
  11237. var n = this.bodies.length-1,
  11238. bodies = this.bodies,
  11239. idx = bodies.indexOf(body);
  11240. if(idx !== -1){
  11241. bodies.splice(idx, 1); // Todo: should use a garbage free method
  11242. // Recompute index
  11243. for(var i=0; i!==bodies.length; i++){
  11244. bodies[i].index = i;
  11245. }
  11246. this.collisionMatrix.setNumObjects(n);
  11247. this.removeBodyEvent.body = body;
  11248. this.dispatchEvent(this.removeBodyEvent);
  11249. }
  11250. };
  11251. /**
  11252. * Remove a rigid body from the simulation.
  11253. * @method removeBody
  11254. * @param {Body} body
  11255. */
  11256. World.prototype.removeBody = World.prototype.remove;
  11257. /**
  11258. * Adds a material to the World.
  11259. * @method addMaterial
  11260. * @param {Material} m
  11261. * @todo Necessary?
  11262. */
  11263. World.prototype.addMaterial = function(m){
  11264. this.materials.push(m);
  11265. };
  11266. /**
  11267. * Adds a contact material to the World
  11268. * @method addContactMaterial
  11269. * @param {ContactMaterial} cmat
  11270. */
  11271. World.prototype.addContactMaterial = function(cmat) {
  11272. // Add contact material
  11273. this.contactmaterials.push(cmat);
  11274. // Add current contact material to the material table
  11275. this.contactMaterialTable.set(cmat.materials[0].id,cmat.materials[1].id,cmat);
  11276. };
  11277. // performance.now()
  11278. if(typeof performance === 'undefined'){
  11279. performance = {};
  11280. }
  11281. if(!performance.now){
  11282. var nowOffset = Date.now();
  11283. if (performance.timing && performance.timing.navigationStart){
  11284. nowOffset = performance.timing.navigationStart;
  11285. }
  11286. performance.now = function(){
  11287. return Date.now() - nowOffset;
  11288. };
  11289. }
  11290. var step_tmp1 = new Vec3();
  11291. /**
  11292. * Step the physics world forward in time.
  11293. *
  11294. * There are two modes. The simple mode is fixed timestepping without interpolation. In this case you only use the first argument. The second case uses interpolation. In that you also provide the time since the function was last used, as well as the maximum fixed timesteps to take.
  11295. *
  11296. * @method step
  11297. * @param {Number} dt The fixed time step size to use.
  11298. * @param {Number} [timeSinceLastCalled] The time elapsed since the function was last called.
  11299. * @param {Number} [maxSubSteps=10] Maximum number of fixed steps to take per function call.
  11300. *
  11301. * @example
  11302. * // fixed timestepping without interpolation
  11303. * world.step(1/60);
  11304. *
  11305. * @see http://bulletphysics.org/mediawiki-1.5.8/index.php/Stepping_The_World
  11306. */
  11307. World.prototype.step = function(dt, timeSinceLastCalled, maxSubSteps){
  11308. maxSubSteps = maxSubSteps || 10;
  11309. timeSinceLastCalled = timeSinceLastCalled || 0;
  11310. if(timeSinceLastCalled === 0){ // Fixed, simple stepping
  11311. this.internalStep(dt);
  11312. // Increment time
  11313. this.time += dt;
  11314. } else {
  11315. // Compute the number of fixed steps we should have taken since the last step
  11316. var internalSteps = Math.floor((this.time + timeSinceLastCalled) / dt) - Math.floor(this.time / dt);
  11317. internalSteps = Math.min(internalSteps,maxSubSteps);
  11318. // Do some fixed steps to catch up
  11319. var t0 = performance.now();
  11320. for(var i=0; i!==internalSteps; i++){
  11321. this.internalStep(dt);
  11322. if(performance.now() - t0 > dt * 1000){
  11323. // We are slower than real-time. Better bail out.
  11324. break;
  11325. }
  11326. }
  11327. // Increment internal clock
  11328. this.time += timeSinceLastCalled;
  11329. // Compute "Left over" time step
  11330. var h = this.time % dt;
  11331. var h_div_dt = h / dt;
  11332. var interpvelo = step_tmp1;
  11333. var bodies = this.bodies;
  11334. for(var j=0; j !== bodies.length; j++){
  11335. var b = bodies[j];
  11336. if(b.type !== Body.STATIC && b.sleepState !== Body.SLEEPING){
  11337. // Interpolate
  11338. b.position.vsub(b.previousPosition, interpvelo);
  11339. interpvelo.scale(h_div_dt, interpvelo);
  11340. b.position.vadd(interpvelo, b.interpolatedPosition);
  11341. // TODO: interpolate quaternion
  11342. // b.interpolatedAngle = b.angle + (b.angle - b.previousAngle) * h_div_dt;
  11343. } else {
  11344. // For static bodies, just copy. Who else will do it?
  11345. b.interpolatedPosition.copy(b.position);
  11346. b.interpolatedQuaternion.copy(b.quaternion);
  11347. }
  11348. }
  11349. }
  11350. };
  11351. /**
  11352. * Step the simulation
  11353. * @method step
  11354. * @param {Number} dt
  11355. */
  11356. var World_step_postStepEvent = {type:"postStep"}, // Reusable event objects to save memory
  11357. World_step_preStepEvent = {type:"preStep"},
  11358. World_step_collideEvent = {type:"collide", body:null, contact:null },
  11359. World_step_oldContacts = [], // Pools for unused objects
  11360. World_step_frictionEquationPool = [],
  11361. World_step_p1 = [], // Reusable arrays for collision pairs
  11362. World_step_p2 = [],
  11363. World_step_gvec = new Vec3(), // Temporary vectors and quats
  11364. World_step_vi = new Vec3(),
  11365. World_step_vj = new Vec3(),
  11366. World_step_wi = new Vec3(),
  11367. World_step_wj = new Vec3(),
  11368. World_step_t1 = new Vec3(),
  11369. World_step_t2 = new Vec3(),
  11370. World_step_rixn = new Vec3(),
  11371. World_step_rjxn = new Vec3(),
  11372. World_step_step_q = new Quaternion(),
  11373. World_step_step_w = new Quaternion(),
  11374. World_step_step_wq = new Quaternion(),
  11375. invI_tau_dt = new Vec3();
  11376. World.prototype.internalStep = function(dt){
  11377. this.dt = dt;
  11378. var world = this,
  11379. that = this,
  11380. contacts = this.contacts,
  11381. p1 = World_step_p1,
  11382. p2 = World_step_p2,
  11383. N = this.numObjects(),
  11384. bodies = this.bodies,
  11385. solver = this.solver,
  11386. gravity = this.gravity,
  11387. doProfiling = this.doProfiling,
  11388. profile = this.profile,
  11389. DYNAMIC = Body.DYNAMIC,
  11390. profilingStart,
  11391. constraints = this.constraints,
  11392. frictionEquationPool = World_step_frictionEquationPool,
  11393. gnorm = gravity.norm(),
  11394. gx = gravity.x,
  11395. gy = gravity.y,
  11396. gz = gravity.z,
  11397. i=0;
  11398. if(doProfiling){
  11399. profilingStart = performance.now();
  11400. }
  11401. // Add gravity to all objects
  11402. for(i=0; i!==N; i++){
  11403. var bi = bodies[i];
  11404. if(bi.type & DYNAMIC){ // Only for dynamic bodies
  11405. var f = bi.force, m = bi.mass;
  11406. f.x += m*gx;
  11407. f.y += m*gy;
  11408. f.z += m*gz;
  11409. }
  11410. }
  11411. // Update subsystems
  11412. for(var i=0, Nsubsystems=this.subsystems.length; i!==Nsubsystems; i++){
  11413. this.subsystems[i].update();
  11414. }
  11415. // Collision detection
  11416. if(doProfiling){ profilingStart = performance.now(); }
  11417. p1.length = 0; // Clean up pair arrays from last step
  11418. p2.length = 0;
  11419. this.broadphase.collisionPairs(this,p1,p2);
  11420. if(doProfiling){ profile.broadphase = performance.now() - profilingStart; }
  11421. // Remove constrained pairs with collideConnected == false
  11422. var Nconstraints = constraints.length;
  11423. for(i=0; i!==Nconstraints; i++){
  11424. var c = constraints[i];
  11425. if(!c.collideConnected){
  11426. for(var j = p1.length-1; j>=0; j-=1){
  11427. if( (c.bodyA === p1[j] && c.bodyB === p2[j]) ||
  11428. (c.bodyB === p1[j] && c.bodyA === p2[j])){
  11429. p1.splice(j, 1);
  11430. p2.splice(j, 1);
  11431. }
  11432. }
  11433. }
  11434. }
  11435. this.collisionMatrixTick();
  11436. // Generate contacts
  11437. if(doProfiling){ profilingStart = performance.now(); }
  11438. var oldcontacts = World_step_oldContacts;
  11439. var NoldContacts = contacts.length;
  11440. for(i=0; i!==NoldContacts; i++){
  11441. oldcontacts.push(contacts[i]);
  11442. }
  11443. contacts.length = 0;
  11444. // Transfer FrictionEquation from current list to the pool for reuse
  11445. var NoldFrictionEquations = this.frictionEquations.length;
  11446. for(i=0; i!==NoldFrictionEquations; i++){
  11447. frictionEquationPool.push(this.frictionEquations[i]);
  11448. }
  11449. this.frictionEquations.length = 0;
  11450. this.narrowphase.getContacts(
  11451. p1,
  11452. p2,
  11453. this,
  11454. contacts,
  11455. oldcontacts, // To be reused
  11456. this.frictionEquations,
  11457. frictionEquationPool
  11458. );
  11459. if(doProfiling){
  11460. profile.narrowphase = performance.now() - profilingStart;
  11461. }
  11462. // Loop over all collisions
  11463. if(doProfiling){
  11464. profilingStart = performance.now();
  11465. }
  11466. // Add all friction eqs
  11467. for (var i = 0; i < this.frictionEquations.length; i++) {
  11468. solver.addEquation(this.frictionEquations[i]);
  11469. }
  11470. var ncontacts = contacts.length;
  11471. for(var k=0; k!==ncontacts; k++){
  11472. // Current contact
  11473. var c = contacts[k];
  11474. // Get current collision indeces
  11475. var bi = c.bi,
  11476. bj = c.bj,
  11477. si = c.si,
  11478. sj = c.sj;
  11479. // Get collision properties
  11480. var cm;
  11481. if(bi.material && bj.material){
  11482. cm = this.getContactMaterial(bi.material,bj.material) || this.defaultContactMaterial;
  11483. } else {
  11484. cm = this.defaultContactMaterial;
  11485. }
  11486. // c.enabled = bi.collisionResponse && bj.collisionResponse && si.collisionResponse && sj.collisionResponse;
  11487. var mu = cm.friction;
  11488. // c.restitution = cm.restitution;
  11489. // If friction or restitution were specified in the material, use them
  11490. if(bi.material && bj.material){
  11491. if(bi.material.friction >= 0 && bj.material.friction >= 0){
  11492. mu = bi.material.friction * bj.material.friction;
  11493. }
  11494. if(bi.material.restitution >= 0 && bj.material.restitution >= 0){
  11495. c.restitution = bi.material.restitution * bj.material.restitution;
  11496. }
  11497. }
  11498. // c.setSpookParams(
  11499. // cm.contactEquationStiffness,
  11500. // cm.contactEquationRelaxation,
  11501. // dt
  11502. // );
  11503. solver.addEquation(c);
  11504. // // Add friction constraint equation
  11505. // if(mu > 0){
  11506. // // Create 2 tangent equations
  11507. // var mug = mu * gnorm;
  11508. // var reducedMass = (bi.invMass + bj.invMass);
  11509. // if(reducedMass > 0){
  11510. // reducedMass = 1/reducedMass;
  11511. // }
  11512. // var pool = frictionEquationPool;
  11513. // var c1 = pool.length ? pool.pop() : new FrictionEquation(bi,bj,mug*reducedMass);
  11514. // var c2 = pool.length ? pool.pop() : new FrictionEquation(bi,bj,mug*reducedMass);
  11515. // this.frictionEquations.push(c1, c2);
  11516. // c1.bi = c2.bi = bi;
  11517. // c1.bj = c2.bj = bj;
  11518. // c1.minForce = c2.minForce = -mug*reducedMass;
  11519. // c1.maxForce = c2.maxForce = mug*reducedMass;
  11520. // // Copy over the relative vectors
  11521. // c1.ri.copy(c.ri);
  11522. // c1.rj.copy(c.rj);
  11523. // c2.ri.copy(c.ri);
  11524. // c2.rj.copy(c.rj);
  11525. // // Construct tangents
  11526. // c.ni.tangents(c1.t, c2.t);
  11527. // // Set spook params
  11528. // c1.setSpookParams(cm.frictionEquationStiffness, cm.frictionEquationRelaxation, dt);
  11529. // c2.setSpookParams(cm.frictionEquationStiffness, cm.frictionEquationRelaxation, dt);
  11530. // c1.enabled = c2.enabled = c.enabled;
  11531. // // Add equations to solver
  11532. // solver.addEquation(c1);
  11533. // solver.addEquation(c2);
  11534. // }
  11535. if( bi.allowSleep &&
  11536. bi.type === Body.DYNAMIC &&
  11537. bi.sleepState === Body.SLEEPING &&
  11538. bj.sleepState === Body.AWAKE &&
  11539. bj.type !== Body.STATIC
  11540. ){
  11541. var speedSquaredB = bj.velocity.norm2() + bj.angularVelocity.norm2();
  11542. var speedLimitSquaredB = Math.pow(bj.sleepSpeedLimit,2);
  11543. if(speedSquaredB >= speedLimitSquaredB*2){
  11544. bi._wakeUpAfterNarrowphase = true;
  11545. }
  11546. }
  11547. if( bj.allowSleep &&
  11548. bj.type === Body.DYNAMIC &&
  11549. bj.sleepState === Body.SLEEPING &&
  11550. bi.sleepState === Body.AWAKE &&
  11551. bi.type !== Body.STATIC
  11552. ){
  11553. var speedSquaredA = bi.velocity.norm2() + bi.angularVelocity.norm2();
  11554. var speedLimitSquaredA = Math.pow(bi.sleepSpeedLimit,2);
  11555. if(speedSquaredA >= speedLimitSquaredA*2){
  11556. bj._wakeUpAfterNarrowphase = true;
  11557. }
  11558. }
  11559. // Now we know that i and j are in contact. Set collision matrix state
  11560. this.collisionMatrix.set(bi, bj, true);
  11561. if (!this.collisionMatrixPrevious.get(bi, bj)) {
  11562. // First contact!
  11563. // We reuse the collideEvent object, otherwise we will end up creating new objects for each new contact, even if there's no event listener attached.
  11564. World_step_collideEvent.body = bj;
  11565. World_step_collideEvent.contact = c;
  11566. bi.dispatchEvent(World_step_collideEvent);
  11567. World_step_collideEvent.body = bi;
  11568. bj.dispatchEvent(World_step_collideEvent);
  11569. }
  11570. }
  11571. if(doProfiling){
  11572. profile.makeContactConstraints = performance.now() - profilingStart;
  11573. profilingStart = performance.now();
  11574. }
  11575. // Wake up bodies
  11576. for(i=0; i!==N; i++){
  11577. var bi = bodies[i];
  11578. if(bi._wakeUpAfterNarrowphase){
  11579. bi.wakeUp();
  11580. bi._wakeUpAfterNarrowphase = false;
  11581. }
  11582. }
  11583. // Add user-added constraints
  11584. var Nconstraints = constraints.length;
  11585. for(i=0; i!==Nconstraints; i++){
  11586. var c = constraints[i];
  11587. c.update();
  11588. for(var j=0, Neq=c.equations.length; j!==Neq; j++){
  11589. var eq = c.equations[j];
  11590. solver.addEquation(eq);
  11591. }
  11592. }
  11593. // Solve the constrained system
  11594. solver.solve(dt,this);
  11595. if(doProfiling){
  11596. profile.solve = performance.now() - profilingStart;
  11597. }
  11598. // Remove all contacts from solver
  11599. solver.removeAllEquations();
  11600. // Apply damping, see http://code.google.com/p/bullet/issues/detail?id=74 for details
  11601. var pow = Math.pow;
  11602. for(i=0; i!==N; i++){
  11603. var bi = bodies[i];
  11604. if(bi.type & DYNAMIC){ // Only for dynamic bodies
  11605. var ld = pow(1.0 - bi.linearDamping,dt);
  11606. var v = bi.velocity;
  11607. v.mult(ld,v);
  11608. var av = bi.angularVelocity;
  11609. if(av){
  11610. var ad = pow(1.0 - bi.angularDamping,dt);
  11611. av.mult(ad,av);
  11612. }
  11613. }
  11614. }
  11615. this.dispatchEvent(World_step_preStepEvent);
  11616. // Invoke pre-step callbacks
  11617. for(i=0; i!==N; i++){
  11618. var bi = bodies[i];
  11619. if(bi.preStep){
  11620. bi.preStep.call(bi);
  11621. }
  11622. }
  11623. // Leap frog
  11624. // vnew = v + h*f/m
  11625. // xnew = x + h*vnew
  11626. if(doProfiling){
  11627. profilingStart = performance.now();
  11628. }
  11629. var q = World_step_step_q;
  11630. var w = World_step_step_w;
  11631. var wq = World_step_step_wq;
  11632. var stepnumber = this.stepnumber;
  11633. var DYNAMIC_OR_KINEMATIC = Body.DYNAMIC | Body.KINEMATIC;
  11634. var quatNormalize = stepnumber % (this.quatNormalizeSkip+1) === 0;
  11635. var quatNormalizeFast = this.quatNormalizeFast;
  11636. var half_dt = dt * 0.5;
  11637. var PLANE = Shape.types.PLANE,
  11638. CONVEX = Shape.types.CONVEXPOLYHEDRON;
  11639. for(i=0; i!==N; i++){
  11640. var b = bodies[i],
  11641. force = b.force,
  11642. tau = b.torque;
  11643. if((b.type & DYNAMIC_OR_KINEMATIC) && b.sleepState !== Body.SLEEPING){ // Only for dynamic
  11644. var velo = b.velocity,
  11645. angularVelo = b.angularVelocity,
  11646. pos = b.position,
  11647. quat = b.quaternion,
  11648. invMass = b.invMass,
  11649. invInertia = b.invInertiaWorld;
  11650. velo.x += force.x * invMass * dt;
  11651. velo.y += force.y * invMass * dt;
  11652. velo.z += force.z * invMass * dt;
  11653. if(b.angularVelocity){
  11654. invInertia.vmult(tau,invI_tau_dt);
  11655. invI_tau_dt.mult(dt,invI_tau_dt);
  11656. invI_tau_dt.vadd(angularVelo,angularVelo);
  11657. }
  11658. // Use new velocity - leap frog
  11659. pos.x += velo.x * dt;
  11660. pos.y += velo.y * dt;
  11661. pos.z += velo.z * dt;
  11662. if(b.angularVelocity){
  11663. w.set(angularVelo.x, angularVelo.y, angularVelo.z, 0);
  11664. w.mult(quat,wq);
  11665. quat.x += half_dt * wq.x;
  11666. quat.y += half_dt * wq.y;
  11667. quat.z += half_dt * wq.z;
  11668. quat.w += half_dt * wq.w;
  11669. if(quatNormalize){
  11670. if(quatNormalizeFast){
  11671. quat.normalizeFast();
  11672. } else {
  11673. quat.normalize();
  11674. }
  11675. }
  11676. }
  11677. if(b.aabb){
  11678. b.aabbNeedsUpdate = true;
  11679. }
  11680. // Update world inertia
  11681. if(b.updateInertiaWorld){
  11682. b.updateInertiaWorld();
  11683. }
  11684. }
  11685. }
  11686. this.clearForces();
  11687. this.broadphase.dirty = true;
  11688. if(doProfiling){
  11689. profile.integrate = performance.now() - profilingStart;
  11690. }
  11691. // Update world time
  11692. this.time += dt;
  11693. this.stepnumber += 1;
  11694. this.dispatchEvent(World_step_postStepEvent);
  11695. // Invoke post-step callbacks
  11696. for(i=0; i!==N; i++){
  11697. var bi = bodies[i];
  11698. var postStep = bi.postStep;
  11699. if(postStep){
  11700. postStep.call(bi);
  11701. }
  11702. }
  11703. // Sleeping update
  11704. if(this.allowSleep){
  11705. for(i=0; i!==N; i++){
  11706. bodies[i].sleepTick(this.time);
  11707. }
  11708. }
  11709. };
  11710. /**
  11711. * Sets all body forces in the world to zero.
  11712. * @method clearForces
  11713. */
  11714. World.prototype.clearForces = function(){
  11715. var bodies = this.bodies;
  11716. var N = bodies.length;
  11717. for(var i=0; i !== N; i++){
  11718. var b = bodies[i],
  11719. force = b.force,
  11720. tau = b.torque;
  11721. b.force.set(0,0,0);
  11722. b.torque.set(0,0,0);
  11723. }
  11724. };
  11725. },{"../collision/AABB":3,"../collision/ArrayCollisionMatrix":4,"../collision/NaiveBroadphase":7,"../collision/Ray":9,"../collision/RaycastResult":10,"../equations/ContactEquation":19,"../equations/FrictionEquation":21,"../material/ContactMaterial":24,"../material/Material":25,"../math/Quaternion":28,"../math/Vec3":30,"../objects/Body":31,"../shapes/Shape":43,"../solver/GSSolver":46,"../utils/EventTarget":49,"../utils/TupleDictionary":52,"../utils/Vec3Pool":54,"./Narrowphase":55}]},{},[2])
  11726. (2)
  11727. });