123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007 |
- /*
- ===========================================================================
- Doom 3 GPL Source Code
- Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
- This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
- Doom 3 Source Code is free software: you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation, either version 3 of the License, or
- (at your option) any later version.
- Doom 3 Source Code is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
- In addition, the Doom 3 Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 Source Code. If not, please request a copy in writing from id Software at the address below.
- If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
- ===========================================================================
- */
- #include "../../idlib/precompiled.h"
- #pragma hdrstop
- #include "../Game_local.h"
- CLASS_DECLARATION( idPhysics_Base, idPhysics_AF )
- END_CLASS
- const float ERROR_REDUCTION = 0.5f;
- const float ERROR_REDUCTION_MAX = 256.0f;
- const float LIMIT_ERROR_REDUCTION = 0.3f;
- const float LCP_EPSILON = 1e-7f;
- const float LIMIT_LCP_EPSILON = 1e-4f;
- const float CONTACT_LCP_EPSILON = 1e-6f;
- const float CENTER_OF_MASS_EPSILON = 1e-4f;
- const float NO_MOVE_TIME = 1.0f;
- const float NO_MOVE_TRANSLATION_TOLERANCE = 10.0f;
- const float NO_MOVE_ROTATION_TOLERANCE = 10.0f;
- const float MIN_MOVE_TIME = -1.0f;
- const float MAX_MOVE_TIME = -1.0f;
- const float IMPULSE_THRESHOLD = 500.0f;
- const float SUSPEND_LINEAR_VELOCITY = 10.0f;
- const float SUSPEND_ANGULAR_VELOCITY = 15.0f;
- const float SUSPEND_LINEAR_ACCELERATION = 20.0f;
- const float SUSPEND_ANGULAR_ACCELERATION = 30.0f;
- const idVec6 vec6_lcp_epsilon = idVec6( LCP_EPSILON, LCP_EPSILON, LCP_EPSILON,
- LCP_EPSILON, LCP_EPSILON, LCP_EPSILON );
- #define AF_TIMINGS
- #ifdef AF_TIMINGS
- static int lastTimerReset = 0;
- static int numArticulatedFigures = 0;
- static idTimer timer_total, timer_pc, timer_ac, timer_collision, timer_lcp;
- #endif
- //===============================================================
- //
- // idAFConstraint
- //
- //===============================================================
- /*
- ================
- idAFConstraint::idAFConstraint
- ================
- */
- idAFConstraint::idAFConstraint( void ) {
- type = CONSTRAINT_INVALID;
- name = "noname";
- body1 = NULL;
- body2 = NULL;
- physics = NULL;
- lo.Zero( 6 );
- lo.SubVec6(0) = -vec6_infinity;
- hi.Zero( 6 );
- hi.SubVec6(0) = vec6_infinity;
- e.SetSize( 6 );
- e.SubVec6(0) = vec6_lcp_epsilon;
- boxConstraint = NULL;
- boxIndex[0] = -1;
- boxIndex[1] = -1;
- boxIndex[2] = -1;
- boxIndex[3] = -1;
- boxIndex[4] = -1;
- boxIndex[5] = -1;
- firstIndex = 0;
- memset( &fl, 0, sizeof( fl ) );
- }
- /*
- ================
- idAFConstraint::~idAFConstraint
- ================
- */
- idAFConstraint::~idAFConstraint( void ) {
- }
- /*
- ================
- idAFConstraint::SetBody1
- ================
- */
- void idAFConstraint::SetBody1( idAFBody *body ) {
- if ( body1 != body) {
- body1 = body;
- if ( physics ) {
- physics->SetChanged();
- }
- }
- }
- /*
- ================
- idAFConstraint::SetBody2
- ================
- */
- void idAFConstraint::SetBody2( idAFBody *body ) {
- if ( body2 != body ) {
- body2 = body;
- if ( physics ) {
- physics->SetChanged();
- }
- }
- }
- /*
- ================
- idAFConstraint::GetMultiplier
- ================
- */
- const idVecX &idAFConstraint::GetMultiplier( void ) {
- return lm;
- }
- /*
- ================
- idAFConstraint::Evaluate
- ================
- */
- void idAFConstraint::Evaluate( float invTimeStep ) {
- assert( 0 );
- }
- /*
- ================
- idAFConstraint::ApplyFriction
- ================
- */
- void idAFConstraint::ApplyFriction( float invTimeStep ) {
- }
- /*
- ================
- idAFConstraint::GetForce
- ================
- */
- void idAFConstraint::GetForce( idAFBody *body, idVec6 &force ) {
- idVecX v;
- v.SetData( 6, VECX_ALLOCA( 6 ) );
- if ( body == body1 ) {
- J1.TransposeMultiply( v, lm );
- }
- else if ( body == body2 ) {
- J2.TransposeMultiply( v, lm );
- }
- else {
- v.Zero();
- }
- force[0] = v[0]; force[1] = v[1]; force[2] = v[2]; force[3] = v[3]; force[4] = v[4]; force[5] = v[5];
- }
- /*
- ================
- idAFConstraint::Translate
- ================
- */
- void idAFConstraint::Translate( const idVec3 &translation ) {
- assert( 0 );
- }
- /*
- ================
- idAFConstraint::Rotate
- ================
- */
- void idAFConstraint::Rotate( const idRotation &rotation ) {
- assert( 0 );
- }
- /*
- ================
- idAFConstraint::GetCenter
- ================
- */
- void idAFConstraint::GetCenter( idVec3 ¢er ) {
- center.Zero();
- }
- /*
- ================
- idAFConstraint::DebugDraw
- ================
- */
- void idAFConstraint::DebugDraw( void ) {
- }
- /*
- ================
- idAFConstraint::InitSize
- ================
- */
- void idAFConstraint::InitSize( int size ) {
- J1.Zero( size, 6 );
- J2.Zero( size, 6 );
- c1.Zero( size );
- c2.Zero( size );
- s.Zero( size );
- lm.Zero( size );
- }
- /*
- ================
- idAFConstraint::Save
- ================
- */
- void idAFConstraint::Save( idSaveGame *saveFile ) const {
- saveFile->WriteInt( type );
- }
- /*
- ================
- idAFConstraint::Restore
- ================
- */
- void idAFConstraint::Restore( idRestoreGame *saveFile ) {
- constraintType_t t;
- saveFile->ReadInt( (int &)t );
- assert( t == type );
- }
- //===============================================================
- //
- // idAFConstraint_Fixed
- //
- //===============================================================
- /*
- ================
- idAFConstraint_Fixed::idAFConstraint_Fixed
- ================
- */
- idAFConstraint_Fixed::idAFConstraint_Fixed( const idStr &name, idAFBody *body1, idAFBody *body2 ) {
- assert( body1 );
- type = CONSTRAINT_FIXED;
- this->name = name;
- this->body1 = body1;
- this->body2 = body2;
- InitSize( 6 );
- fl.allowPrimary = true;
- fl.noCollision = true;
- InitOffset();
- }
- /*
- ================
- idAFConstraint_Fixed::InitOffset
- ================
- */
- void idAFConstraint_Fixed::InitOffset( void ) {
- if ( body2 ) {
- offset = ( body1->GetWorldOrigin() - body2->GetWorldOrigin() ) * body2->GetWorldAxis().Transpose();
- relAxis = body1->GetWorldAxis() * body2->GetWorldAxis().Transpose();
- }
- else {
- offset = body1->GetWorldOrigin();
- relAxis = body1->GetWorldAxis();
- }
- }
- /*
- ================
- idAFConstraint_Fixed::SetBody1
- ================
- */
- void idAFConstraint_Fixed::SetBody1( idAFBody *body ) {
- if ( body1 != body) {
- body1 = body;
- InitOffset();
- if ( physics ) {
- physics->SetChanged();
- }
- }
- }
- /*
- ================
- idAFConstraint_Fixed::SetBody2
- ================
- */
- void idAFConstraint_Fixed::SetBody2( idAFBody *body ) {
- if ( body2 != body ) {
- body2 = body;
- InitOffset();
- if ( physics ) {
- physics->SetChanged();
- }
- }
- }
- /*
- ================
- idAFConstraint_Fixed::Evaluate
- ================
- */
- void idAFConstraint_Fixed::Evaluate( float invTimeStep ) {
- idVec3 ofs, a2;
- idMat3 ax;
- idRotation r;
- idAFBody *master;
- master = body2 ? body2 : physics->GetMasterBody();
- if ( master ) {
- a2 = offset * master->GetWorldAxis();
- ofs = a2 + master->GetWorldOrigin();
- ax = relAxis * master->GetWorldAxis();
- }
- else {
- a2.Zero();
- ofs = offset;
- ax = relAxis;
- }
- J1.Set( mat3_identity, mat3_zero,
- mat3_zero, mat3_identity );
- if ( body2 ) {
- J2.Set( -mat3_identity, SkewSymmetric( a2 ),
- mat3_zero, -mat3_identity );
- }
- else {
- J2.Zero( 6, 6 );
- }
- c1.SubVec3(0) = -( invTimeStep * ERROR_REDUCTION ) * ( ofs - body1->GetWorldOrigin() );
- r = ( body1->GetWorldAxis().Transpose() * ax ).ToRotation();
- c1.SubVec3(1) = -( invTimeStep * ERROR_REDUCTION ) * ( r.GetVec() * -(float) DEG2RAD( r.GetAngle() ) );
- c1.Clamp( -ERROR_REDUCTION_MAX, ERROR_REDUCTION_MAX );
- }
- /*
- ================
- idAFConstraint_Fixed::ApplyFriction
- ================
- */
- void idAFConstraint_Fixed::ApplyFriction( float invTimeStep ) {
- // no friction
- }
- /*
- ================
- idAFConstraint_Fixed::Translate
- ================
- */
- void idAFConstraint_Fixed::Translate( const idVec3 &translation ) {
- if ( !body2 ) {
- offset += translation;
- }
- }
- /*
- ================
- idAFConstraint_Fixed::Rotate
- ================
- */
- void idAFConstraint_Fixed::Rotate( const idRotation &rotation ) {
- if ( !body2 ) {
- offset *= rotation;
- relAxis *= rotation.ToMat3();
- }
- }
- /*
- ================
- idAFConstraint_Fixed::GetCenter
- ================
- */
- void idAFConstraint_Fixed::GetCenter( idVec3 ¢er ) {
- center = body1->GetWorldOrigin();
- }
- /*
- ================
- idAFConstraint_Fixed::DebugDraw
- ================
- */
- void idAFConstraint_Fixed::DebugDraw( void ) {
- idAFBody *master;
- master = body2 ? body2 : physics->GetMasterBody();
- if ( master ) {
- gameRenderWorld->DebugLine( colorRed, body1->GetWorldOrigin(), master->GetWorldOrigin() );
- }
- else {
- gameRenderWorld->DebugLine( colorRed, body1->GetWorldOrigin(), vec3_origin );
- }
- }
- /*
- ================
- idAFConstraint_Fixed::Save
- ================
- */
- void idAFConstraint_Fixed::Save( idSaveGame *saveFile ) const {
- idAFConstraint::Save( saveFile );
- saveFile->WriteVec3( offset );
- saveFile->WriteMat3( relAxis );
- }
- /*
- ================
- idAFConstraint_Fixed::Restore
- ================
- */
- void idAFConstraint_Fixed::Restore( idRestoreGame *saveFile ) {
- idAFConstraint::Restore( saveFile );
- saveFile->ReadVec3( offset );
- saveFile->ReadMat3( relAxis );
- }
- //===============================================================
- //
- // idAFConstraint_BallAndSocketJoint
- //
- //===============================================================
- /*
- ================
- idAFConstraint_BallAndSocketJoint::idAFConstraint_BallAndSocketJoint
- ================
- */
- idAFConstraint_BallAndSocketJoint::idAFConstraint_BallAndSocketJoint( const idStr &name, idAFBody *body1, idAFBody *body2 ) {
- assert( body1 );
- type = CONSTRAINT_BALLANDSOCKETJOINT;
- this->name = name;
- this->body1 = body1;
- this->body2 = body2;
- InitSize( 3 );
- coneLimit = NULL;
- pyramidLimit = NULL;
- friction = 0.0f;
- fc = NULL;
- fl.allowPrimary = true;
- fl.noCollision = true;
- }
- /*
- ================
- idAFConstraint_BallAndSocketJoint::~idAFConstraint_BallAndSocketJoint
- ================
- */
- idAFConstraint_BallAndSocketJoint::~idAFConstraint_BallAndSocketJoint( void ) {
- if ( coneLimit ) {
- delete coneLimit;
- }
- if ( pyramidLimit ) {
- delete pyramidLimit;
- }
- }
- /*
- ================
- idAFConstraint_BallAndSocketJoint::SetAnchor
- ================
- */
- void idAFConstraint_BallAndSocketJoint::SetAnchor( const idVec3 &worldPosition ) {
- // get anchor relative to center of mass of body1
- anchor1 = ( worldPosition - body1->GetWorldOrigin() ) * body1->GetWorldAxis().Transpose();
- if ( body2 ) {
- // get anchor relative to center of mass of body2
- anchor2 = ( worldPosition - body2->GetWorldOrigin() ) * body2->GetWorldAxis().Transpose();
- }
- else {
- anchor2 = worldPosition;
- }
- if ( coneLimit ) {
- coneLimit->SetAnchor( anchor2 );
- }
- if ( pyramidLimit ) {
- pyramidLimit->SetAnchor( anchor2 );
- }
- }
- /*
- ================
- idAFConstraint_BallAndSocketJoint::GetAnchor
- ================
- */
- idVec3 idAFConstraint_BallAndSocketJoint::GetAnchor( void ) const {
- if ( body2 ) {
- return body2->GetWorldOrigin() + body2->GetWorldAxis() * anchor2;
- }
- return anchor2;
- }
- /*
- ================
- idAFConstraint_BallAndSocketJoint::SetNoLimit
- ================
- */
- void idAFConstraint_BallAndSocketJoint::SetNoLimit( void ) {
- if ( coneLimit ) {
- delete coneLimit;
- coneLimit = NULL;
- }
- if ( pyramidLimit ) {
- delete pyramidLimit;
- pyramidLimit = NULL;
- }
- }
- /*
- ================
- idAFConstraint_BallAndSocketJoint::SetConeLimit
- ================
- */
- void idAFConstraint_BallAndSocketJoint::SetConeLimit( const idVec3 &coneAxis, const float coneAngle, const idVec3 &body1Axis ) {
- if ( pyramidLimit ) {
- delete pyramidLimit;
- pyramidLimit = NULL;
- }
- if ( !coneLimit ) {
- coneLimit = new idAFConstraint_ConeLimit;
- coneLimit->SetPhysics( physics );
- }
- if ( body2 ) {
- coneLimit->Setup( body1, body2, anchor2, coneAxis * body2->GetWorldAxis().Transpose(), coneAngle, body1Axis * body1->GetWorldAxis().Transpose() );
- }
- else {
- coneLimit->Setup( body1, body2, anchor2, coneAxis, coneAngle, body1Axis * body1->GetWorldAxis().Transpose() );
- }
- }
- /*
- ================
- idAFConstraint_BallAndSocketJoint::SetPyramidLimit
- ================
- */
- void idAFConstraint_BallAndSocketJoint::SetPyramidLimit( const idVec3 &pyramidAxis, const idVec3 &baseAxis,
- const float angle1, const float angle2, const idVec3 &body1Axis ) {
- if ( coneLimit ) {
- delete coneLimit;
- coneLimit = NULL;
- }
- if ( !pyramidLimit ) {
- pyramidLimit = new idAFConstraint_PyramidLimit;
- pyramidLimit->SetPhysics( physics );
- }
- if ( body2 ) {
- pyramidLimit->Setup( body1, body2, anchor2, pyramidAxis * body2->GetWorldAxis().Transpose(),
- baseAxis * body2->GetWorldAxis().Transpose(), angle1, angle2,
- body1Axis * body1->GetWorldAxis().Transpose() );
- }
- else {
- pyramidLimit->Setup( body1, body2, anchor2, pyramidAxis, baseAxis, angle1, angle2,
- body1Axis * body1->GetWorldAxis().Transpose() );
- }
- }
- /*
- ================
- idAFConstraint_BallAndSocketJoint::SetLimitEpsilon
- ================
- */
- void idAFConstraint_BallAndSocketJoint::SetLimitEpsilon( const float e ) {
- if ( coneLimit ) {
- coneLimit->SetEpsilon( e );
- }
- if ( pyramidLimit ) {
- pyramidLimit->SetEpsilon( e );
- }
- }
- /*
- ================
- idAFConstraint_BallAndSocketJoint::GetFriction
- ================
- */
- float idAFConstraint_BallAndSocketJoint::GetFriction( void ) const {
- if ( af_forceFriction.GetFloat() > 0.0f ) {
- return af_forceFriction.GetFloat();
- }
- return friction * physics->GetJointFrictionScale();
- }
- /*
- ================
- idAFConstraint_BallAndSocketJoint::Evaluate
- ================
- */
- void idAFConstraint_BallAndSocketJoint::Evaluate( float invTimeStep ) {
- idVec3 a1, a2;
- idAFBody *master;
- master = body2 ? body2 : physics->GetMasterBody();
- a1 = anchor1 * body1->GetWorldAxis();
- if ( master ) {
- a2 = anchor2 * master->GetWorldAxis();
- c1.SubVec3(0) = -( invTimeStep * ERROR_REDUCTION ) * ( a2 + master->GetWorldOrigin() - ( a1 + body1->GetWorldOrigin() ) );
- }
- else {
- c1.SubVec3(0) = -( invTimeStep * ERROR_REDUCTION ) * ( anchor2 - ( a1 + body1->GetWorldOrigin() ) );
- }
- c1.Clamp( -ERROR_REDUCTION_MAX, ERROR_REDUCTION_MAX );
- J1.Set( mat3_identity, -SkewSymmetric( a1 ) );
- if ( body2 ) {
- J2.Set( -mat3_identity, SkewSymmetric( a2 ) );
- }
- else {
- J2.Zero( 3, 6 );
- }
- if ( coneLimit ) {
- coneLimit->Add( physics, invTimeStep );
- }
- else if ( pyramidLimit ) {
- pyramidLimit->Add( physics, invTimeStep );
- }
- }
- /*
- ================
- idAFConstraint_BallAndSocketJoint::ApplyFriction
- ================
- */
- void idAFConstraint_BallAndSocketJoint::ApplyFriction( float invTimeStep ) {
- idVec3 angular;
- float invMass, currentFriction;
- currentFriction = GetFriction();
- if ( currentFriction <= 0.0f ) {
- return;
- }
- if ( af_useImpulseFriction.GetBool() || af_useJointImpulseFriction.GetBool() ) {
- angular = body1->GetAngularVelocity();
- invMass = body1->GetInverseMass();
- if ( body2 ) {
- angular -= body2->GetAngularVelocity();
- invMass += body2->GetInverseMass();
- }
- angular *= currentFriction / invMass;
- body1->SetAngularVelocity( body1->GetAngularVelocity() - angular * body1->GetInverseMass() );
- if ( body2 ) {
- body2->SetAngularVelocity( body2->GetAngularVelocity() + angular * body2->GetInverseMass() );
- }
- }
- else {
- if ( !fc ) {
- fc = new idAFConstraint_BallAndSocketJointFriction;
- fc->Setup( this );
- }
- fc->Add( physics, invTimeStep );
- }
- }
- /*
- ================
- idAFConstraint_BallAndSocketJoint::GetForce
- ================
- */
- void idAFConstraint_BallAndSocketJoint::GetForce( idAFBody *body, idVec6 &force ) {
- idAFConstraint::GetForce( body, force );
- // FIXME: add limit force
- }
- /*
- ================
- idAFConstraint_BallAndSocketJoint::Translate
- ================
- */
- void idAFConstraint_BallAndSocketJoint::Translate( const idVec3 &translation ) {
- if ( !body2 ) {
- anchor2 += translation;
- }
- if ( coneLimit ) {
- coneLimit->Translate( translation );
- }
- else if ( pyramidLimit ) {
- pyramidLimit->Translate( translation );
- }
- }
- /*
- ================
- idAFConstraint_BallAndSocketJoint::Rotate
- ================
- */
- void idAFConstraint_BallAndSocketJoint::Rotate( const idRotation &rotation ) {
- if ( !body2 ) {
- anchor2 *= rotation;
- }
- if ( coneLimit ) {
- coneLimit->Rotate( rotation );
- }
- else if ( pyramidLimit ) {
- pyramidLimit->Rotate( rotation );
- }
- }
- /*
- ================
- idAFConstraint_BallAndSocketJoint::GetCenter
- ================
- */
- void idAFConstraint_BallAndSocketJoint::GetCenter( idVec3 ¢er ) {
- center = body1->GetWorldOrigin() + anchor1 * body1->GetWorldAxis();
- }
- /*
- ================
- idAFConstraint_BallAndSocketJoint::DebugDraw
- ================
- */
- void idAFConstraint_BallAndSocketJoint::DebugDraw( void ) {
- idVec3 a1 = body1->GetWorldOrigin() + anchor1 * body1->GetWorldAxis();
- gameRenderWorld->DebugLine( colorBlue, a1 - idVec3( 5, 0, 0 ), a1 + idVec3( 5, 0, 0 ) );
- gameRenderWorld->DebugLine( colorBlue, a1 - idVec3( 0, 5, 0 ), a1 + idVec3( 0, 5, 0 ) );
- gameRenderWorld->DebugLine( colorBlue, a1 - idVec3( 0, 0, 5 ), a1 + idVec3( 0, 0, 5 ) );
- if ( af_showLimits.GetBool() ) {
- if ( coneLimit ) {
- coneLimit->DebugDraw();
- }
- if ( pyramidLimit ) {
- pyramidLimit->DebugDraw();
- }
- }
- }
- /*
- ================
- idAFConstraint_BallAndSocketJoint::Save
- ================
- */
- void idAFConstraint_BallAndSocketJoint::Save( idSaveGame *saveFile ) const {
- idAFConstraint::Save( saveFile );
- saveFile->WriteVec3( anchor1 );
- saveFile->WriteVec3( anchor2 );
- saveFile->WriteFloat( friction );
- if ( coneLimit ) {
- coneLimit->Save( saveFile );
- }
- if ( pyramidLimit ) {
- pyramidLimit->Save( saveFile );
- }
- }
- /*
- ================
- idAFConstraint_BallAndSocketJoint::Restore
- ================
- */
- void idAFConstraint_BallAndSocketJoint::Restore( idRestoreGame *saveFile ) {
- idAFConstraint::Restore( saveFile );
- saveFile->ReadVec3( anchor1 );
- saveFile->ReadVec3( anchor2 );
- saveFile->ReadFloat( friction );
- if ( coneLimit ) {
- coneLimit->Restore( saveFile );
- }
- if ( pyramidLimit ) {
- pyramidLimit->Restore( saveFile );
- }
- }
- //===============================================================
- //
- // idAFConstraint_BallAndSocketJointFriction
- //
- //===============================================================
- /*
- ================
- idAFConstraint_BallAndSocketJointFriction::idAFConstraint_BallAndSocketJointFriction
- ================
- */
- idAFConstraint_BallAndSocketJointFriction::idAFConstraint_BallAndSocketJointFriction( void ) {
- type = CONSTRAINT_FRICTION;
- name = "ballAndSocketJointFriction";
- InitSize( 3 );
- joint = NULL;
- fl.allowPrimary = false;
- fl.frameConstraint = true;
- }
- /*
- ================
- idAFConstraint_BallAndSocketJointFriction::Setup
- ================
- */
- void idAFConstraint_BallAndSocketJointFriction::Setup( idAFConstraint_BallAndSocketJoint *bsj ) {
- this->joint = bsj;
- body1 = bsj->GetBody1();
- body2 = bsj->GetBody2();
- }
- /*
- ================
- idAFConstraint_BallAndSocketJointFriction::Evaluate
- ================
- */
- void idAFConstraint_BallAndSocketJointFriction::Evaluate( float invTimeStep ) {
- // do nothing
- }
- /*
- ================
- idAFConstraint_BallAndSocketJointFriction::ApplyFriction
- ================
- */
- void idAFConstraint_BallAndSocketJointFriction::ApplyFriction( float invTimeStep ) {
- // do nothing
- }
- /*
- ================
- idAFConstraint_BallAndSocketJointFriction::Add
- ================
- */
- bool idAFConstraint_BallAndSocketJointFriction::Add( idPhysics_AF *phys, float invTimeStep ) {
- float f;
- physics = phys;
- f = joint->GetFriction() * joint->GetMultiplier().Length();
- if ( f == 0.0f ) {
- return false;
- }
- lo[0] = lo[1] = lo[2] = -f;
- hi[0] = hi[1] = hi[2] = f;
- J1.Zero( 3, 6 );
- J1[0][3] = J1[1][4] = J1[2][5] = 1.0f;
- if ( body2 ) {
- J2.Zero( 3, 6 );
- J2[0][3] = J2[1][4] = J2[2][5] = 1.0f;
- }
- physics->AddFrameConstraint( this );
- return true;
- }
- /*
- ================
- idAFConstraint_BallAndSocketJointFriction::Translate
- ================
- */
- void idAFConstraint_BallAndSocketJointFriction::Translate( const idVec3 &translation ) {
- }
- /*
- ================
- idAFConstraint_BallAndSocketJointFriction::Rotate
- ================
- */
- void idAFConstraint_BallAndSocketJointFriction::Rotate( const idRotation &rotation ) {
- }
- //===============================================================
- //
- // idAFConstraint_UniversalJoint
- //
- //===============================================================
- /*
- ================
- idAFConstraint_UniversalJoint::idAFConstraint_UniversalJoint
- ================
- */
- idAFConstraint_UniversalJoint::idAFConstraint_UniversalJoint( const idStr &name, idAFBody *body1, idAFBody *body2 ) {
- assert( body1 );
- type = CONSTRAINT_UNIVERSALJOINT;
- this->name = name;
- this->body1 = body1;
- this->body2 = body2;
- InitSize( 4 );
- coneLimit = NULL;
- pyramidLimit = NULL;
- friction = 0.0f;
- fc = NULL;
- fl.allowPrimary = true;
- fl.noCollision = true;
- }
- /*
- ================
- idAFConstraint_UniversalJoint::~idAFConstraint_UniversalJoint
- ================
- */
- idAFConstraint_UniversalJoint::~idAFConstraint_UniversalJoint( void ) {
- if ( coneLimit ) {
- delete coneLimit;
- }
- if ( pyramidLimit ) {
- delete pyramidLimit;
- }
- if ( fc ) {
- delete fc;
- }
- }
- /*
- ================
- idAFConstraint_UniversalJoint::SetAnchor
- ================
- */
- void idAFConstraint_UniversalJoint::SetAnchor( const idVec3 &worldPosition ) {
- // get anchor relative to center of mass of body1
- anchor1 = ( worldPosition - body1->GetWorldOrigin() ) * body1->GetWorldAxis().Transpose();
- if ( body2 ) {
- // get anchor relative to center of mass of body2
- anchor2 = ( worldPosition - body2->GetWorldOrigin() ) * body2->GetWorldAxis().Transpose();
- }
- else {
- anchor2 = worldPosition;
- }
- if ( coneLimit ) {
- coneLimit->SetAnchor( anchor2 );
- }
- if ( pyramidLimit ) {
- pyramidLimit->SetAnchor( anchor2 );
- }
- }
- /*
- ================
- idAFConstraint_UniversalJoint::GetAnchor
- ================
- */
- idVec3 idAFConstraint_UniversalJoint::GetAnchor( void ) const {
- if ( body2 ) {
- return body2->GetWorldOrigin() + body2->GetWorldAxis() * anchor2;
- }
- return anchor2;
- }
- /*
- ================
- idAFConstraint_UniversalJoint::SetShafts
- ================
- */
- void idAFConstraint_UniversalJoint::SetShafts( const idVec3 &cardanShaft1, const idVec3 &cardanShaft2 ) {
- idVec3 cardanAxis;
- float l;
- shaft1 = cardanShaft1;
- l = shaft1.Normalize();
- assert( l != 0.0f );
- shaft2 = cardanShaft2;
- l = shaft2.Normalize();
- assert( l != 0.0f );
- // the cardan axis is a vector orthogonal to both cardan shafts
- cardanAxis = shaft1.Cross( shaft2 );
- if ( cardanAxis.Normalize() == 0.0f ) {
- idVec3 vecY;
- shaft1.OrthogonalBasis( cardanAxis, vecY );
- cardanAxis.Normalize();
- }
- shaft1 *= body1->GetWorldAxis().Transpose();
- axis1 = cardanAxis * body1->GetWorldAxis().Transpose();
- if ( body2 ) {
- shaft2 *= body2->GetWorldAxis().Transpose();
- axis2 = cardanAxis * body2->GetWorldAxis().Transpose();
- }
- else {
- axis2 = cardanAxis;
- }
- if ( coneLimit ) {
- coneLimit->SetBody1Axis( shaft1 );
- }
- if ( pyramidLimit ) {
- pyramidLimit->SetBody1Axis( shaft1 );
- }
- }
- /*
- ================
- idAFConstraint_UniversalJoint::SetNoLimit
- ================
- */
- void idAFConstraint_UniversalJoint::SetNoLimit( void ) {
- if ( coneLimit ) {
- delete coneLimit;
- coneLimit = NULL;
- }
- if ( pyramidLimit ) {
- delete pyramidLimit;
- pyramidLimit = NULL;
- }
- }
- /*
- ================
- idAFConstraint_UniversalJoint::SetConeLimit
- ================
- */
- void idAFConstraint_UniversalJoint::SetConeLimit( const idVec3 &coneAxis, const float coneAngle ) {
- if ( pyramidLimit ) {
- delete pyramidLimit;
- pyramidLimit = NULL;
- }
- if ( !coneLimit ) {
- coneLimit = new idAFConstraint_ConeLimit;
- coneLimit->SetPhysics( physics );
- }
- if ( body2 ) {
- coneLimit->Setup( body1, body2, anchor2, coneAxis * body2->GetWorldAxis().Transpose(), coneAngle, shaft1 );
- }
- else {
- coneLimit->Setup( body1, body2, anchor2, coneAxis, coneAngle, shaft1 );
- }
- }
- /*
- ================
- idAFConstraint_UniversalJoint::SetPyramidLimit
- ================
- */
- void idAFConstraint_UniversalJoint::SetPyramidLimit( const idVec3 &pyramidAxis, const idVec3 &baseAxis,
- const float angle1, const float angle2 ) {
- if ( coneLimit ) {
- delete coneLimit;
- coneLimit = NULL;
- }
- if ( !pyramidLimit ) {
- pyramidLimit = new idAFConstraint_PyramidLimit;
- pyramidLimit->SetPhysics( physics );
- }
- if ( body2 ) {
- pyramidLimit->Setup( body1, body2, anchor2, pyramidAxis * body2->GetWorldAxis().Transpose(),
- baseAxis * body2->GetWorldAxis().Transpose(), angle1, angle2, shaft1 );
- }
- else {
- pyramidLimit->Setup( body1, body2, anchor2, pyramidAxis, baseAxis, angle1, angle2, shaft1 );
- }
- }
- /*
- ================
- idAFConstraint_UniversalJoint::SetLimitEpsilon
- ================
- */
- void idAFConstraint_UniversalJoint::SetLimitEpsilon( const float e ) {
- if ( coneLimit ) {
- coneLimit->SetEpsilon( e );
- }
- if ( pyramidLimit ) {
- pyramidLimit->SetEpsilon( e );
- }
- }
- /*
- ================
- idAFConstraint_UniversalJoint::GetFriction
- ================
- */
- float idAFConstraint_UniversalJoint::GetFriction( void ) const {
- if ( af_forceFriction.GetFloat() > 0.0f ) {
- return af_forceFriction.GetFloat();
- }
- return friction * physics->GetJointFrictionScale();
- }
- /*
- ================
- idAFConstraint_UniversalJoint::Evaluate
- NOTE: this joint is homokinetic
- ================
- */
- void idAFConstraint_UniversalJoint::Evaluate( float invTimeStep ) {
- idVec3 a1, a2, s1, s2, d1, d2, v;
- idAFBody *master;
- master = body2 ? body2 : physics->GetMasterBody();
- a1 = anchor1 * body1->GetWorldAxis();
- s1 = shaft1 * body1->GetWorldAxis();
- d1 = s1.Cross( axis1 * body1->GetWorldAxis() );
- if ( master ) {
- a2 = anchor2 * master->GetWorldAxis();
- s2 = shaft2 * master->GetWorldAxis();
- d2 = axis2 * master->GetWorldAxis();
- c1.SubVec3(0) = -( invTimeStep * ERROR_REDUCTION ) * ( a2 + master->GetWorldOrigin() - ( a1 + body1->GetWorldOrigin() ) );
- }
- else {
- a2 = anchor2;
- s2 = shaft2;
- d2 = axis2;
- c1.SubVec3(0) = -( invTimeStep * ERROR_REDUCTION ) * ( a2 - ( a1 + body1->GetWorldOrigin() ) );
- }
- J1.Set( mat3_identity, -SkewSymmetric( a1 ),
- mat3_zero, idMat3( s1[0], s1[1], s1[2],
- 0.0f, 0.0f, 0.0f,
- 0.0f, 0.0f, 0.0f ) );
- J1.SetSize( 4, 6 );
- if ( body2 ) {
- J2.Set( -mat3_identity, SkewSymmetric( a2 ),
- mat3_zero, idMat3( s2[0], s2[1], s2[2],
- 0.0f, 0.0f, 0.0f,
- 0.0f, 0.0f, 0.0f ) );
- J2.SetSize( 4, 6 );
- }
- else {
- J2.Zero( 4, 6 );
- }
- v = s1.Cross( s2 );
- if ( v.Normalize() != 0.0f ) {
- idMat3 m1, m2;
- m1[0] = s1;
- m1[1] = v;
- m1[2] = v.Cross( m1[0] );
- m2[0] = -s2;
- m2[1] = v;
- m2[2] = v.Cross( m2[0] );
- d2 *= m2.Transpose() * m1;
- }
- c1[3] = -( invTimeStep * ERROR_REDUCTION ) * ( d1 * d2 );
- c1.Clamp( -ERROR_REDUCTION_MAX, ERROR_REDUCTION_MAX );
- if ( coneLimit ) {
- coneLimit->Add( physics, invTimeStep );
- }
- else if ( pyramidLimit ) {
- pyramidLimit->Add( physics, invTimeStep );
- }
- }
- /*
- ================
- idAFConstraint_UniversalJoint::ApplyFriction
- ================
- */
- void idAFConstraint_UniversalJoint::ApplyFriction( float invTimeStep ) {
- idVec3 angular;
- float invMass, currentFriction;
- currentFriction = GetFriction();
- if ( currentFriction <= 0.0f ) {
- return;
- }
- if ( af_useImpulseFriction.GetBool() || af_useJointImpulseFriction.GetBool() ) {
- angular = body1->GetAngularVelocity();
- invMass = body1->GetInverseMass();
- if ( body2 ) {
- angular -= body2->GetAngularVelocity();
- invMass += body2->GetInverseMass();
- }
- angular *= currentFriction / invMass;
- body1->SetAngularVelocity( body1->GetAngularVelocity() - angular * body1->GetInverseMass() );
- if ( body2 ) {
- body2->SetAngularVelocity( body2->GetAngularVelocity() + angular * body2->GetInverseMass() );
- }
- }
- else {
- if ( !fc ) {
- fc = new idAFConstraint_UniversalJointFriction;
- fc->Setup( this );
- }
- fc->Add( physics, invTimeStep );
- }
- }
- /*
- ================
- idAFConstraint_UniversalJoint::GetForce
- ================
- */
- void idAFConstraint_UniversalJoint::GetForce( idAFBody *body, idVec6 &force ) {
- idAFConstraint::GetForce( body, force );
- // FIXME: add limit force
- }
- /*
- ================
- idAFConstraint_UniversalJoint::Translate
- ================
- */
- void idAFConstraint_UniversalJoint::Translate( const idVec3 &translation ) {
- if ( !body2 ) {
- anchor2 += translation;
- }
- if ( coneLimit ) {
- coneLimit->Translate( translation );
- }
- else if ( pyramidLimit ) {
- pyramidLimit->Translate( translation );
- }
- }
- /*
- ================
- idAFConstraint_UniversalJoint::Rotate
- ================
- */
- void idAFConstraint_UniversalJoint::Rotate( const idRotation &rotation ) {
- if ( !body2 ) {
- anchor2 *= rotation;
- shaft2 *= rotation.ToMat3();
- axis2 *= rotation.ToMat3();
- }
- if ( coneLimit ) {
- coneLimit->Rotate( rotation );
- }
- else if ( pyramidLimit ) {
- pyramidLimit->Rotate( rotation );
- }
- }
- /*
- ================
- idAFConstraint_UniversalJoint::GetCenter
- ================
- */
- void idAFConstraint_UniversalJoint::GetCenter( idVec3 ¢er ) {
- center = body1->GetWorldOrigin() + anchor1 * body1->GetWorldAxis();
- }
- /*
- ================
- idAFConstraint_UniversalJoint::DebugDraw
- ================
- */
- void idAFConstraint_UniversalJoint::DebugDraw( void ) {
- idVec3 a1, a2, s1, s2, d1, d2, v;
- idAFBody *master;
- master = body2 ? body2 : physics->GetMasterBody();
- a1 = body1->GetWorldOrigin() + anchor1 * body1->GetWorldAxis();
- s1 = shaft1 * body1->GetWorldAxis();
- d1 = axis1 * body1->GetWorldAxis();
- if ( master ) {
- a2 = master->GetWorldOrigin() + anchor2 * master->GetWorldAxis();
- s2 = shaft2 * master->GetWorldAxis();
- d2 = axis2 * master->GetWorldAxis();
- }
- else {
- a2 = anchor2;
- s2 = shaft2;
- d2 = axis2;
- }
- v = s1.Cross( s2 );
- if ( v.Normalize() != 0.0f ) {
- idMat3 m1, m2;
- m1[0] = s1;
- m1[1] = v;
- m1[2] = v.Cross( m1[0] );
- m2[0] = -s2;
- m2[1] = v;
- m2[2] = v.Cross( m2[0] );
- d2 *= m2.Transpose() * m1;
- }
- gameRenderWorld->DebugArrow( colorCyan, a1, a1 + s1 * 5.0f, 1.0f );
- gameRenderWorld->DebugArrow( colorBlue, a2, a2 + s2 * 5.0f, 1.0f );
- gameRenderWorld->DebugLine( colorGreen, a1, a1 + d1 * 5.0f );
- gameRenderWorld->DebugLine( colorGreen, a2, a2 + d2 * 5.0f );
- if ( af_showLimits.GetBool() ) {
- if ( coneLimit ) {
- coneLimit->DebugDraw();
- }
- if ( pyramidLimit ) {
- pyramidLimit->DebugDraw();
- }
- }
- }
- /*
- ================
- idAFConstraint_UniversalJoint::Save
- ================
- */
- void idAFConstraint_UniversalJoint::Save( idSaveGame *saveFile ) const {
- idAFConstraint::Save( saveFile );
- saveFile->WriteVec3( anchor1 );
- saveFile->WriteVec3( anchor2 );
- saveFile->WriteVec3( shaft1 );
- saveFile->WriteVec3( shaft2 );
- saveFile->WriteVec3( axis1 );
- saveFile->WriteVec3( axis2 );
- saveFile->WriteFloat( friction );
- if ( coneLimit ) {
- coneLimit->Save( saveFile );
- }
- if ( pyramidLimit ) {
- pyramidLimit->Save( saveFile );
- }
- }
- /*
- ================
- idAFConstraint_UniversalJoint::Restore
- ================
- */
- void idAFConstraint_UniversalJoint::Restore( idRestoreGame *saveFile ) {
- idAFConstraint::Restore( saveFile );
- saveFile->ReadVec3( anchor1 );
- saveFile->ReadVec3( anchor2 );
- saveFile->ReadVec3( shaft1 );
- saveFile->ReadVec3( shaft2 );
- saveFile->ReadVec3( axis1 );
- saveFile->ReadVec3( axis2 );
- saveFile->ReadFloat( friction );
- if ( coneLimit ) {
- coneLimit->Restore( saveFile );
- }
- if ( pyramidLimit ) {
- pyramidLimit->Restore( saveFile );
- }
- }
- //===============================================================
- //
- // idAFConstraint_UniversalJointFriction
- //
- //===============================================================
- /*
- ================
- idAFConstraint_UniversalJointFriction::idAFConstraint_UniversalJointFriction
- ================
- */
- idAFConstraint_UniversalJointFriction::idAFConstraint_UniversalJointFriction( void ) {
- type = CONSTRAINT_FRICTION;
- name = "universalJointFriction";
- InitSize( 2 );
- joint = NULL;
- fl.allowPrimary = false;
- fl.frameConstraint = true;
- }
- /*
- ================
- idAFConstraint_UniversalJointFriction::Setup
- ================
- */
- void idAFConstraint_UniversalJointFriction::Setup( idAFConstraint_UniversalJoint *uj ) {
- this->joint = uj;
- body1 = uj->GetBody1();
- body2 = uj->GetBody2();
- }
- /*
- ================
- idAFConstraint_UniversalJointFriction::Evaluate
- ================
- */
- void idAFConstraint_UniversalJointFriction::Evaluate( float invTimeStep ) {
- // do nothing
- }
- /*
- ================
- idAFConstraint_UniversalJointFriction::ApplyFriction
- ================
- */
- void idAFConstraint_UniversalJointFriction::ApplyFriction( float invTimeStep ) {
- // do nothing
- }
- /*
- ================
- idAFConstraint_UniversalJointFriction::Add
- ================
- */
- bool idAFConstraint_UniversalJointFriction::Add( idPhysics_AF *phys, float invTimeStep ) {
- idVec3 s1, s2, dir1, dir2;
- float f;
- physics = phys;
- f = joint->GetFriction() * joint->GetMultiplier().Length();
- if ( f == 0.0f ) {
- return false;
- }
- lo[0] = lo[1] = -f;
- hi[0] = hi[1] = f;
- joint->GetShafts( s1, s2 );
- s1 *= body1->GetWorldAxis();
- s1.NormalVectors( dir1, dir2 );
- J1.SetSize( 2, 6 );
- J1.SubVec6(0).SubVec3(0).Zero();
- J1.SubVec6(0).SubVec3(1) = dir1;
- J1.SubVec6(1).SubVec3(0).Zero();
- J1.SubVec6(1).SubVec3(1) = dir2;
- if ( body2 ) {
- J2.SetSize( 2, 6 );
- J2.SubVec6(0).SubVec3(0).Zero();
- J2.SubVec6(0).SubVec3(1) = -dir1;
- J2.SubVec6(1).SubVec3(0).Zero();
- J2.SubVec6(1).SubVec3(1) = -dir2;
- }
- physics->AddFrameConstraint( this );
- return true;
- }
- /*
- ================
- idAFConstraint_UniversalJointFriction::Translate
- ================
- */
- void idAFConstraint_UniversalJointFriction::Translate( const idVec3 &translation ) {
- }
- /*
- ================
- idAFConstraint_UniversalJointFriction::Rotate
- ================
- */
- void idAFConstraint_UniversalJointFriction::Rotate( const idRotation &rotation ) {
- }
- //===============================================================
- //
- // idAFConstraint_CylindricalJoint
- //
- //===============================================================
- /*
- ================
- idAFConstraint_CylindricalJoint::idAFConstraint_CylindricalJoint
- ================
- */
- idAFConstraint_CylindricalJoint::idAFConstraint_CylindricalJoint( const idStr &name, idAFBody *body1, idAFBody *body2 ) {
- assert( 0 ); // FIXME: implement
- }
- /*
- ================
- idAFConstraint_CylindricalJoint::Evaluate
- ================
- */
- void idAFConstraint_CylindricalJoint::Evaluate( float invTimeStep ) {
- assert( 0 ); // FIXME: implement
- }
- /*
- ================
- idAFConstraint_CylindricalJoint::ApplyFriction
- ================
- */
- void idAFConstraint_CylindricalJoint::ApplyFriction( float invTimeStep ) {
- assert( 0 ); // FIXME: implement
- }
- /*
- ================
- idAFConstraint_CylindricalJoint::Translate
- ================
- */
- void idAFConstraint_CylindricalJoint::Translate( const idVec3 &translation ) {
- assert( 0 ); // FIXME: implement
- }
- /*
- ================
- idAFConstraint_CylindricalJoint::Rotate
- ================
- */
- void idAFConstraint_CylindricalJoint::Rotate( const idRotation &rotation ) {
- assert( 0 ); // FIXME: implement
- }
- /*
- ================
- idAFConstraint_CylindricalJoint::DebugDraw
- ================
- */
- void idAFConstraint_CylindricalJoint::DebugDraw( void ) {
- assert( 0 ); // FIXME: implement
- }
- //===============================================================
- //
- // idAFConstraint_Hinge
- //
- //===============================================================
- /*
- ================
- idAFConstraint_Hinge::idAFConstraint_Hinge
- ================
- */
- idAFConstraint_Hinge::idAFConstraint_Hinge( const idStr &name, idAFBody *body1, idAFBody *body2 ) {
- assert( body1 );
- type = CONSTRAINT_HINGE;
- this->name = name;
- this->body1 = body1;
- this->body2 = body2;
- InitSize( 5 );
- coneLimit = NULL;
- steering = NULL;
- friction = 0.0f;
- fc = NULL;
- fl.allowPrimary = true;
- fl.noCollision = true;
- initialAxis = body1->GetWorldAxis();
- if ( body2 ) {
- initialAxis *= body2->GetWorldAxis().Transpose();
- }
- }
- /*
- ================
- idAFConstraint_Hinge::~idAFConstraint_Hinge
- ================
- */
- idAFConstraint_Hinge::~idAFConstraint_Hinge( void ) {
- if ( coneLimit ) {
- delete coneLimit;
- }
- if ( fc ) {
- delete fc;
- }
- if ( steering ) {
- delete steering;
- }
- }
- /*
- ================
- idAFConstraint_Hinge::SetAnchor
- ================
- */
- void idAFConstraint_Hinge::SetAnchor( const idVec3 &worldPosition ) {
- // get anchor relative to center of mass of body1
- anchor1 = ( worldPosition - body1->GetWorldOrigin() ) * body1->GetWorldAxis().Transpose();
- if ( body2 ) {
- // get anchor relative to center of mass of body2
- anchor2 = ( worldPosition - body2->GetWorldOrigin() ) * body2->GetWorldAxis().Transpose();
- }
- else {
- anchor2 = worldPosition;
- }
- if ( coneLimit ) {
- coneLimit->SetAnchor( anchor2 );
- }
- }
- /*
- ================
- idAFConstraint_Hinge::GetAnchor
- ================
- */
- idVec3 idAFConstraint_Hinge::GetAnchor( void ) const {
- if ( body2 ) {
- return body2->GetWorldOrigin() + body2->GetWorldAxis() * anchor2;
- }
- return anchor2;
- }
- /*
- ================
- idAFConstraint_Hinge::SetAxis
- ================
- */
- void idAFConstraint_Hinge::SetAxis( const idVec3 &axis ) {
- idVec3 normAxis;
- normAxis = axis;
- normAxis.Normalize();
- // get axis relative to body1
- axis1 = normAxis * body1->GetWorldAxis().Transpose();
- if ( body2 ) {
- // get axis relative to body2
- axis2 = normAxis * body2->GetWorldAxis().Transpose();
- }
- else {
- axis2 = normAxis;
- }
- }
- /*
- ================
- idAFConstraint_Hinge::GetAxis
- ================
- */
- idVec3 idAFConstraint_Hinge::GetAxis( void ) const {
- if ( body2 ) {
- return axis2 * body2->GetWorldAxis();
- }
- return axis2;
- }
- /*
- ================
- idAFConstraint_Hinge::SetNoLimit
- ================
- */
- void idAFConstraint_Hinge::SetNoLimit( void ) {
- if ( coneLimit ) {
- delete coneLimit;
- coneLimit = NULL;
- }
- }
- /*
- ================
- idAFConstraint_Hinge::SetLimit
- ================
- */
- void idAFConstraint_Hinge::SetLimit( const idVec3 &axis, const float angle, const idVec3 &body1Axis ) {
- if ( !coneLimit ) {
- coneLimit = new idAFConstraint_ConeLimit;
- coneLimit->SetPhysics( physics );
- }
- if ( body2 ) {
- coneLimit->Setup( body1, body2, anchor2, axis * body2->GetWorldAxis().Transpose(), angle, body1Axis * body1->GetWorldAxis().Transpose() );
- }
- else {
- coneLimit->Setup( body1, body2, anchor2, axis, angle, body1Axis * body1->GetWorldAxis().Transpose() );
- }
- }
- /*
- ================
- idAFConstraint_Hinge::SetLimitEpsilon
- ================
- */
- void idAFConstraint_Hinge::SetLimitEpsilon( const float e ) {
- if ( coneLimit ) {
- coneLimit->SetEpsilon( e );
- }
- }
- /*
- ================
- idAFConstraint_Hinge::GetFriction
- ================
- */
- float idAFConstraint_Hinge::GetFriction( void ) const {
- if ( af_forceFriction.GetFloat() > 0.0f ) {
- return af_forceFriction.GetFloat();
- }
- return friction * physics->GetJointFrictionScale();
- }
- /*
- ================
- idAFConstraint_Hinge::GetAngle
- ================
- */
- float idAFConstraint_Hinge::GetAngle( void ) const {
- idMat3 axis;
- idRotation rotation;
- float angle;
- axis = body1->GetWorldAxis() * body2->GetWorldAxis().Transpose() * initialAxis.Transpose();
- rotation = axis.ToRotation();
- angle = rotation.GetAngle();
- if ( rotation.GetVec() * axis1 < 0.0f ) {
- return -angle;
- }
- return angle;
- }
- /*
- ================
- idAFConstraint_Hinge::SetSteerAngle
- ================
- */
- void idAFConstraint_Hinge::SetSteerAngle( const float degrees ) {
- if ( coneLimit ) {
- delete coneLimit;
- coneLimit = NULL;
- }
- if ( !steering ) {
- steering = new idAFConstraint_HingeSteering();
- steering->Setup( this );
- }
- steering->SetSteerAngle( degrees );
- }
- /*
- ================
- idAFConstraint_Hinge::SetSteerSpeed
- ================
- */
- void idAFConstraint_Hinge::SetSteerSpeed( const float speed ) {
- if ( steering ) {
- steering->SetSteerSpeed( speed );
- }
- }
- /*
- ================
- idAFConstraint_Hinge::Evaluate
- ================
- */
- void idAFConstraint_Hinge::Evaluate( float invTimeStep ) {
- idVec3 a1, a2;
- idVec3 x1, x2, cross;
- idVec3 vecX, vecY;
- idAFBody *master;
- master = body2 ? body2 : physics->GetMasterBody();
- x1 = axis1 * body1->GetWorldAxis(); // axis in body1 space
- x1.OrthogonalBasis( vecX, vecY ); // basis for axis in body1 space
- a1 = anchor1 * body1->GetWorldAxis(); // anchor in body1 space
- if ( master ) {
- a2 = anchor2 * master->GetWorldAxis(); // anchor in master space
- x2 = axis2 * master->GetWorldAxis();
- c1.SubVec3(0) = -( invTimeStep * ERROR_REDUCTION ) * ( a2 + master->GetWorldOrigin() - ( a1 + body1->GetWorldOrigin() ) );
- }
- else {
- a2 = anchor2;
- x2 = axis2;
- c1.SubVec3(0) = -( invTimeStep * ERROR_REDUCTION ) * ( a2 - ( a1 + body1->GetWorldOrigin() ) );
- }
- J1.Set( mat3_identity, -SkewSymmetric( a1 ),
- mat3_zero, idMat3( vecX[0], vecX[1], vecX[2],
- vecY[0], vecY[1], vecY[2],
- 0.0f, 0.0f, 0.0f ) );
- J1.SetSize( 5, 6 );
- if ( body2 ) {
- J2.Set( -mat3_identity, SkewSymmetric( a2 ),
- mat3_zero, idMat3( -vecX[0], -vecX[1], -vecX[2],
- -vecY[0], -vecY[1], -vecY[2],
- 0.0f, 0.0f, 0.0f ) );
- J2.SetSize( 5, 6 );
- }
- else {
- J2.Zero( 5, 6 );
- }
- cross = x1.Cross( x2 );
- c1[3] = -( invTimeStep * ERROR_REDUCTION ) * ( cross * vecX );
- c1[4] = -( invTimeStep * ERROR_REDUCTION ) * ( cross * vecY );
- c1.Clamp( -ERROR_REDUCTION_MAX, ERROR_REDUCTION_MAX );
- if ( steering ) {
- steering->Add( physics, invTimeStep );
- }
- else if ( coneLimit ) {
- coneLimit->Add( physics, invTimeStep );
- }
- }
- /*
- ================
- idAFConstraint_Hinge::ApplyFriction
- ================
- */
- void idAFConstraint_Hinge::ApplyFriction( float invTimeStep ) {
- idVec3 angular;
- float invMass, currentFriction;
- currentFriction = GetFriction();
- if ( currentFriction <= 0.0f ) {
- return;
- }
- if ( af_useImpulseFriction.GetBool() || af_useJointImpulseFriction.GetBool() ) {
- angular = body1->GetAngularVelocity();
- invMass = body1->GetInverseMass();
- if ( body2 ) {
- angular -= body2->GetAngularVelocity();
- invMass += body2->GetInverseMass();
- }
- angular *= currentFriction / invMass;
- body1->SetAngularVelocity( body1->GetAngularVelocity() - angular * body1->GetInverseMass() );
- if ( body2 ) {
- body2->SetAngularVelocity( body2->GetAngularVelocity() + angular * body2->GetInverseMass() );
- }
- }
- else {
- if ( !fc ) {
- fc = new idAFConstraint_HingeFriction;
- fc->Setup( this );
- }
- fc->Add( physics, invTimeStep );
- }
- }
- /*
- ================
- idAFConstraint_Hinge::GetForce
- ================
- */
- void idAFConstraint_Hinge::GetForce( idAFBody *body, idVec6 &force ) {
- idAFConstraint::GetForce( body, force );
- // FIXME: add limit force
- }
- /*
- ================
- idAFConstraint_Hinge::Translate
- ================
- */
- void idAFConstraint_Hinge::Translate( const idVec3 &translation ) {
- if ( !body2 ) {
- anchor2 += translation;
- }
- if ( coneLimit ) {
- coneLimit->Translate( translation );
- }
- }
- /*
- ================
- idAFConstraint_Hinge::Rotate
- ================
- */
- void idAFConstraint_Hinge::Rotate( const idRotation &rotation ) {
- if ( !body2 ) {
- anchor2 *= rotation;
- axis2 *= rotation.ToMat3();
- }
- if ( coneLimit ) {
- coneLimit->Rotate( rotation );
- }
- }
- /*
- ================
- idAFConstraint_Hinge::GetCenter
- ================
- */
- void idAFConstraint_Hinge::GetCenter( idVec3 ¢er ) {
- center = body1->GetWorldOrigin() + anchor1 * body1->GetWorldAxis();
- }
- /*
- ================
- idAFConstraint_Hinge::DebugDraw
- ================
- */
- void idAFConstraint_Hinge::DebugDraw( void ) {
- idVec3 vecX, vecY;
- idVec3 a1 = body1->GetWorldOrigin() + anchor1 * body1->GetWorldAxis();
- idVec3 x1 = axis1 * body1->GetWorldAxis();
- x1.OrthogonalBasis( vecX, vecY );
- gameRenderWorld->DebugArrow( colorBlue, a1 - 4.0f * x1, a1 + 4.0f * x1, 1 );
- gameRenderWorld->DebugLine( colorBlue, a1 - 2.0f * vecX, a1 + 2.0f * vecX );
- gameRenderWorld->DebugLine( colorBlue, a1 - 2.0f * vecY, a1 + 2.0f * vecY );
- if ( af_showLimits.GetBool() ) {
- if ( coneLimit ) {
- coneLimit->DebugDraw();
- }
- }
- }
- /*
- ================
- idAFConstraint_Hinge::Save
- ================
- */
- void idAFConstraint_Hinge::Save( idSaveGame *saveFile ) const {
- idAFConstraint::Save( saveFile );
- saveFile->WriteVec3( anchor1 );
- saveFile->WriteVec3( anchor2 );
- saveFile->WriteVec3( axis1 );
- saveFile->WriteVec3( axis2 );
- saveFile->WriteMat3( initialAxis );
- saveFile->WriteFloat( friction );
- if ( coneLimit ) {
- saveFile->WriteBool( true );
- coneLimit->Save( saveFile );
- } else {
- saveFile->WriteBool( false );
- }
- if ( steering ) {
- saveFile->WriteBool( true );
- steering->Save( saveFile );
- } else {
- saveFile->WriteBool( false );
- }
- if ( fc ) {
- saveFile->WriteBool( true );
- fc->Save( saveFile );
- } else {
- saveFile->WriteBool( false );
- }
- }
- /*
- ================
- idAFConstraint_Hinge::Restore
- ================
- */
- void idAFConstraint_Hinge::Restore( idRestoreGame *saveFile ) {
- bool b;
- idAFConstraint::Restore( saveFile );
- saveFile->ReadVec3( anchor1 );
- saveFile->ReadVec3( anchor2 );
- saveFile->ReadVec3( axis1 );
- saveFile->ReadVec3( axis2 );
- saveFile->ReadMat3( initialAxis );
- saveFile->ReadFloat( friction );
- saveFile->ReadBool( b );
- if ( b ) {
- if ( !coneLimit ) {
- coneLimit = new idAFConstraint_ConeLimit;
- }
- coneLimit->SetPhysics( physics );
- coneLimit->Restore( saveFile );
- }
- saveFile->ReadBool( b );
- if ( b ) {
- if ( !steering ) {
- steering = new idAFConstraint_HingeSteering;
- }
- steering->Setup( this );
- steering->Restore( saveFile );
- }
- saveFile->ReadBool( b );
- if ( b ) {
- if ( !fc ) {
- fc = new idAFConstraint_HingeFriction;
- }
- fc->Setup( this );
- fc->Restore( saveFile );
- }
- }
- //===============================================================
- //
- // idAFConstraint_HingeFriction
- //
- //===============================================================
- /*
- ================
- idAFConstraint_HingeFriction::idAFConstraint_HingeFriction
- ================
- */
- idAFConstraint_HingeFriction::idAFConstraint_HingeFriction( void ) {
- type = CONSTRAINT_FRICTION;
- name = "hingeFriction";
- InitSize( 1 );
- hinge = NULL;
- fl.allowPrimary = false;
- fl.frameConstraint = true;
- }
- /*
- ================
- idAFConstraint_HingeFriction::Setup
- ================
- */
- void idAFConstraint_HingeFriction::Setup( idAFConstraint_Hinge *h ) {
- this->hinge = h;
- body1 = h->GetBody1();
- body2 = h->GetBody2();
- }
- /*
- ================
- idAFConstraint_HingeFriction::Evaluate
- ================
- */
- void idAFConstraint_HingeFriction::Evaluate( float invTimeStep ) {
- // do nothing
- }
- /*
- ================
- idAFConstraint_HingeFriction::ApplyFriction
- ================
- */
- void idAFConstraint_HingeFriction::ApplyFriction( float invTimeStep ) {
- // do nothing
- }
- /*
- ================
- idAFConstraint_HingeFriction::Add
- ================
- */
- bool idAFConstraint_HingeFriction::Add( idPhysics_AF *phys, float invTimeStep ) {
- idVec3 a1, a2;
- float f;
- physics = phys;
- f = hinge->GetFriction() * hinge->GetMultiplier().Length();
- if ( f == 0.0f ) {
- return false;
- }
- lo[0] = -f;
- hi[0] = f;
- hinge->GetAxis( a1, a2 );
- a1 *= body1->GetWorldAxis();
- J1.SetSize( 1, 6 );
- J1.SubVec6(0).SubVec3(0).Zero();
- J1.SubVec6(0).SubVec3(1) = a1;
- if ( body2 ) {
- a2 *= body2->GetWorldAxis();
- J2.SetSize( 1, 6 );
- J2.SubVec6(0).SubVec3(0).Zero();
- J2.SubVec6(0).SubVec3(1) = -a2;
- }
- physics->AddFrameConstraint( this );
- return true;
- }
- /*
- ================
- idAFConstraint_HingeFriction::Translate
- ================
- */
- void idAFConstraint_HingeFriction::Translate( const idVec3 &translation ) {
- }
- /*
- ================
- idAFConstraint_HingeFriction::Rotate
- ================
- */
- void idAFConstraint_HingeFriction::Rotate( const idRotation &rotation ) {
- }
- //===============================================================
- //
- // idAFConstraint_HingeSteering
- //
- //===============================================================
- /*
- ================
- idAFConstraint_HingeSteering::idAFConstraint_HingeSteering
- ================
- */
- idAFConstraint_HingeSteering::idAFConstraint_HingeSteering( void ) {
- type = CONSTRAINT_HINGESTEERING;
- name = "hingeFriction";
- InitSize( 1 );
- hinge = NULL;
- fl.allowPrimary = false;
- fl.frameConstraint = true;
- steerSpeed = 0.0f;
- epsilon = LCP_EPSILON;
- }
- /*
- ================
- idAFConstraint_HingeSteering::Save
- ================
- */
- void idAFConstraint_HingeSteering::Save( idSaveGame *saveFile ) const {
- saveFile->WriteFloat(steerAngle);
- saveFile->WriteFloat(steerSpeed);
- saveFile->WriteFloat(epsilon);
- }
- /*
- ================
- idAFConstraint_HingeSteering::Restore
- ================
- */
- void idAFConstraint_HingeSteering::Restore( idRestoreGame *saveFile ) {
- saveFile->ReadFloat(steerAngle);
- saveFile->ReadFloat(steerSpeed);
- saveFile->ReadFloat(epsilon);
- }
- /*
- ================
- idAFConstraint_HingeSteering::Setup
- ================
- */
- void idAFConstraint_HingeSteering::Setup( idAFConstraint_Hinge *h ) {
- this->hinge = h;
- body1 = h->GetBody1();
- body2 = h->GetBody2();
- }
- /*
- ================
- idAFConstraint_HingeSteering::Evaluate
- ================
- */
- void idAFConstraint_HingeSteering::Evaluate( float invTimeStep ) {
- // do nothing
- }
- /*
- ================
- idAFConstraint_HingeSteering::ApplyFriction
- ================
- */
- void idAFConstraint_HingeSteering::ApplyFriction( float invTimeStep ) {
- // do nothing
- }
- /*
- ================
- idAFConstraint_HingeSteering::Add
- ================
- */
- bool idAFConstraint_HingeSteering::Add( idPhysics_AF *phys, float invTimeStep ) {
- float angle, speed;
- idVec3 a1, a2;
- physics = phys;
- hinge->GetAxis( a1, a2 );
- angle = hinge->GetAngle();
- a1 *= body1->GetWorldAxis();
- J1.SetSize( 1, 6 );
- J1.SubVec6(0).SubVec3(0).Zero();
- J1.SubVec6(0).SubVec3(1) = a1;
- if ( body2 ) {
- a2 *= body2->GetWorldAxis();
- J2.SetSize( 1, 6 );
- J2.SubVec6(0).SubVec3(0).Zero();
- J2.SubVec6(0).SubVec3(1) = -a2;
- }
- speed = steerAngle - angle;
- if ( steerSpeed != 0.0f ) {
- if ( speed > steerSpeed ) {
- speed = steerSpeed;
- }
- else if ( speed < -steerSpeed ) {
- speed = -steerSpeed;
- }
- }
- c1[0] = DEG2RAD( speed ) * invTimeStep;
- physics->AddFrameConstraint( this );
- return true;
- }
- /*
- ================
- idAFConstraint_HingeSteering::Translate
- ================
- */
- void idAFConstraint_HingeSteering::Translate( const idVec3 &translation ) {
- }
- /*
- ================
- idAFConstraint_HingeSteering::Rotate
- ================
- */
- void idAFConstraint_HingeSteering::Rotate( const idRotation &rotation ) {
- }
- //===============================================================
- //
- // idAFConstraint_Slider
- //
- //===============================================================
- /*
- ================
- idAFConstraint_Slider::idAFConstraint_Slider
- ================
- */
- idAFConstraint_Slider::idAFConstraint_Slider( const idStr &name, idAFBody *body1, idAFBody *body2 ) {
- assert( body1 );
- type = CONSTRAINT_SLIDER;
- this->name = name;
- this->body1 = body1;
- this->body2 = body2;
- InitSize( 5 );
- fl.allowPrimary = true;
- fl.noCollision = true;
- if ( body2 ) {
- offset = ( body1->GetWorldOrigin() - body2->GetWorldOrigin() ) * body1->GetWorldAxis().Transpose();
- relAxis = body1->GetWorldAxis() * body2->GetWorldAxis().Transpose();
- }
- else {
- offset = body1->GetWorldOrigin();
- relAxis = body1->GetWorldAxis();
- }
- }
- /*
- ================
- idAFConstraint_Slider::SetAxis
- ================
- */
- void idAFConstraint_Slider::SetAxis( const idVec3 &ax ) {
- idVec3 normAxis;
- // get normalized axis relative to body1
- normAxis = ax;
- normAxis.Normalize();
- if ( body2 ) {
- axis = normAxis * body2->GetWorldAxis().Transpose();
- }
- else {
- axis = normAxis;
- }
- }
- /*
- ================
- idAFConstraint_Slider::Evaluate
- ================
- */
- void idAFConstraint_Slider::Evaluate( float invTimeStep ) {
- idVec3 vecX, vecY, ofs;
- idRotation r;
- idAFBody *master;
- master = body2 ? body2 : physics->GetMasterBody();
- if ( master ) {
- (axis * master->GetWorldAxis()).OrthogonalBasis( vecX, vecY );
- ofs = master->GetWorldOrigin() + master->GetWorldAxis() * offset - body1->GetWorldOrigin();
- r = ( body1->GetWorldAxis().Transpose() * (relAxis * master->GetWorldAxis()) ).ToRotation();
- }
- else {
- axis.OrthogonalBasis( vecX, vecY );
- ofs = offset - body1->GetWorldOrigin();
- r = ( body1->GetWorldAxis().Transpose() * relAxis ).ToRotation();
- }
- J1.Set( mat3_zero, mat3_identity,
- idMat3( vecX, vecY, vec3_origin ), mat3_zero );
- J1.SetSize( 5, 6 );
- if ( body2 ) {
- J2.Set( mat3_zero, -mat3_identity,
- idMat3( -vecX, -vecY, vec3_origin ), mat3_zero );
- J2.SetSize( 5, 6 );
- }
- else {
- J2.Zero( 5, 6 );
- }
- c1.SubVec3(0) = -( invTimeStep * ERROR_REDUCTION ) * ( r.GetVec() * - (float) DEG2RAD( r.GetAngle() ) );
- c1[3] = -( invTimeStep * ERROR_REDUCTION ) * ( vecX * ofs );
- c1[4] = -( invTimeStep * ERROR_REDUCTION ) * ( vecY * ofs );
- c1.Clamp( -ERROR_REDUCTION_MAX, ERROR_REDUCTION_MAX );
- }
- /*
- ================
- idAFConstraint_Slider::ApplyFriction
- ================
- */
- void idAFConstraint_Slider::ApplyFriction( float invTimeStep ) {
- // no friction
- }
- /*
- ================
- idAFConstraint_Slider::Translate
- ================
- */
- void idAFConstraint_Slider::Translate( const idVec3 &translation ) {
- if ( !body2 ) {
- offset += translation;
- }
- }
- /*
- ================
- idAFConstraint_Slider::Rotate
- ================
- */
- void idAFConstraint_Slider::Rotate( const idRotation &rotation ) {
- if ( !body2 ) {
- offset *= rotation;
- }
- }
- /*
- ================
- idAFConstraint_Slider::GetCenter
- ================
- */
- void idAFConstraint_Slider::GetCenter( idVec3 ¢er ) {
- idAFBody *master;
- master = body2 ? body2 : physics->GetMasterBody();
- if ( master ) {
- center = master->GetWorldOrigin() + master->GetWorldAxis() * offset - body1->GetWorldOrigin();
- }
- else {
- center = offset - body1->GetWorldOrigin();
- }
- }
- /*
- ================
- idAFConstraint_Slider::DebugDraw
- ================
- */
- void idAFConstraint_Slider::DebugDraw( void ) {
- idVec3 ofs;
- idAFBody *master;
- master = body2 ? body2 : physics->GetMasterBody();
- if ( master ) {
- ofs = master->GetWorldOrigin() + master->GetWorldAxis() * offset - body1->GetWorldOrigin();
- }
- else {
- ofs = offset - body1->GetWorldOrigin();
- }
- gameRenderWorld->DebugLine( colorGreen, ofs, ofs + axis * body1->GetWorldAxis() );
- }
- /*
- ================
- idAFConstraint_Slider::Save
- ================
- */
- void idAFConstraint_Slider::Save( idSaveGame *saveFile ) const {
- idAFConstraint::Save( saveFile );
- saveFile->WriteVec3( axis );
- saveFile->WriteVec3( offset );
- saveFile->WriteMat3( relAxis );
- }
- /*
- ================
- idAFConstraint_Slider::Restore
- ================
- */
- void idAFConstraint_Slider::Restore( idRestoreGame *saveFile ) {
- idAFConstraint::Restore( saveFile );
- saveFile->ReadVec3( axis );
- saveFile->ReadVec3( offset );
- saveFile->ReadMat3( relAxis );
- }
- //===============================================================
- //
- // idAFConstraint_Line
- //
- //===============================================================
- /*
- ================
- idAFConstraint_Line::idAFConstraint_Line
- ================
- */
- idAFConstraint_Line::idAFConstraint_Line( const idStr &name, idAFBody *body1, idAFBody *body2 ) {
- assert( 0 ); // FIXME: implement
- }
- /*
- ================
- idAFConstraint_Line::Evaluate
- ================
- */
- void idAFConstraint_Line::Evaluate( float invTimeStep ) {
- assert( 0 ); // FIXME: implement
- }
- /*
- ================
- idAFConstraint_Line::ApplyFriction
- ================
- */
- void idAFConstraint_Line::ApplyFriction( float invTimeStep ) {
- assert( 0 ); // FIXME: implement
- }
- /*
- ================
- idAFConstraint_Line::Translate
- ================
- */
- void idAFConstraint_Line::Translate( const idVec3 &translation ) {
- assert( 0 ); // FIXME: implement
- }
- /*
- ================
- idAFConstraint_Line::Rotate
- ================
- */
- void idAFConstraint_Line::Rotate( const idRotation &rotation ) {
- assert( 0 ); // FIXME: implement
- }
- /*
- ================
- idAFConstraint_Line::DebugDraw
- ================
- */
- void idAFConstraint_Line::DebugDraw( void ) {
- assert( 0 ); // FIXME: implement
- }
- //===============================================================
- //
- // idAFConstraint_Plane
- //
- //===============================================================
- /*
- ================
- idAFConstraint_Plane::idAFConstraint_Plane
- ================
- */
- idAFConstraint_Plane::idAFConstraint_Plane( const idStr &name, idAFBody *body1, idAFBody *body2 ) {
- assert( body1 );
- type = CONSTRAINT_PLANE;
- this->name = name;
- this->body1 = body1;
- this->body2 = body2;
- InitSize( 1 );
- fl.allowPrimary = true;
- fl.noCollision = true;
- }
- /*
- ================
- idAFConstraint_Plane::SetPlane
- ================
- */
- void idAFConstraint_Plane::SetPlane( const idVec3 &normal, const idVec3 &anchor ) {
- // get anchor relative to center of mass of body1
- anchor1 = ( anchor - body1->GetWorldOrigin() ) * body1->GetWorldAxis().Transpose();
- if ( body2 ) {
- // get anchor relative to center of mass of body2
- anchor2 = ( anchor - body2->GetWorldOrigin() ) * body2->GetWorldAxis().Transpose();
- planeNormal = normal * body2->GetWorldAxis().Transpose();
- }
- else {
- anchor2 = anchor;
- planeNormal = normal;
- }
- }
- /*
- ================
- idAFConstraint_Plane::Evaluate
- ================
- */
- void idAFConstraint_Plane::Evaluate( float invTimeStep ) {
- idVec3 a1, a2, normal, p;
- idVec6 v;
- idAFBody *master;
- master = body2 ? body2 : physics->GetMasterBody();
- a1 = body1->GetWorldOrigin() + anchor1 * body1->GetWorldAxis();
- if ( master ) {
- a2 = master->GetWorldOrigin() + anchor2 * master->GetWorldAxis();
- normal = planeNormal * master->GetWorldAxis();
- }
- else {
- a2 = anchor2;
- normal = planeNormal;
- }
- p = a1 - body1->GetWorldOrigin();
- v.SubVec3(0) = normal;
- v.SubVec3(1) = p.Cross( normal );
- J1.Set( 1, 6, v.ToFloatPtr() );
- if ( body2 ) {
- p = a1 - body2->GetWorldOrigin();
- v.SubVec3(0) = -normal;
- v.SubVec3(1) = p.Cross( -normal );
- J2.Set( 1, 6, v.ToFloatPtr() );
- }
- c1[0] = -( invTimeStep * ERROR_REDUCTION ) * (a1 * normal - a2 * normal);
- c1.Clamp( -ERROR_REDUCTION_MAX, ERROR_REDUCTION_MAX );
- }
- /*
- ================
- idAFConstraint_Plane::ApplyFriction
- ================
- */
- void idAFConstraint_Plane::ApplyFriction( float invTimeStep ) {
- // no friction
- }
- /*
- ================
- idAFConstraint_Plane::Translate
- ================
- */
- void idAFConstraint_Plane::Translate( const idVec3 &translation ) {
- if ( !body2 ) {
- anchor2 += translation;
- }
- }
- /*
- ================
- idAFConstraint_Plane::Rotate
- ================
- */
- void idAFConstraint_Plane::Rotate( const idRotation &rotation ) {
- if ( !body2 ) {
- anchor2 *= rotation;
- planeNormal *= rotation.ToMat3();
- }
- }
- /*
- ================
- idAFConstraint_Plane::DebugDraw
- ================
- */
- void idAFConstraint_Plane::DebugDraw( void ) {
- idVec3 a1, normal, right, up;
- idAFBody *master;
- master = body2 ? body2 : physics->GetMasterBody();
- a1 = body1->GetWorldOrigin() + anchor1 * body1->GetWorldAxis();
- if ( master ) {
- normal = planeNormal * master->GetWorldAxis();
- }
- else {
- normal = planeNormal;
- }
- normal.NormalVectors( right, up );
- normal *= 4.0f;
- right *= 4.0f;
- up *= 4.0f;
- gameRenderWorld->DebugLine( colorCyan, a1 - right, a1 + right );
- gameRenderWorld->DebugLine( colorCyan, a1 - up, a1 + up );
- gameRenderWorld->DebugArrow( colorCyan, a1, a1 + normal, 1 );
- }
- /*
- ================
- idAFConstraint_Plane::Save
- ================
- */
- void idAFConstraint_Plane::Save( idSaveGame *saveFile ) const {
- idAFConstraint::Save( saveFile );
- saveFile->WriteVec3( anchor1 );
- saveFile->WriteVec3( anchor2 );
- saveFile->WriteVec3( planeNormal );
- }
- /*
- ================
- idAFConstraint_Plane::Restore
- ================
- */
- void idAFConstraint_Plane::Restore( idRestoreGame *saveFile ) {
- idAFConstraint::Restore( saveFile );
- saveFile->ReadVec3( anchor1 );
- saveFile->ReadVec3( anchor2 );
- saveFile->ReadVec3( planeNormal );
- }
- //===============================================================
- //
- // idAFConstraint_Spring
- //
- //===============================================================
- /*
- ================
- idAFConstraint_Spring::idAFConstraint_Spring
- ================
- */
- idAFConstraint_Spring::idAFConstraint_Spring( const idStr &name, idAFBody *body1, idAFBody *body2 ) {
- assert( body1 );
- type = CONSTRAINT_SPRING;
- this->name = name;
- this->body1 = body1;
- this->body2 = body2;
- InitSize( 1 );
- fl.allowPrimary = false;
- kstretch = kcompress = damping = 1.0f;
- minLength = maxLength = restLength = 0.0f;
- }
- /*
- ================
- idAFConstraint_Spring::SetAnchor
- ================
- */
- void idAFConstraint_Spring::SetAnchor( const idVec3 &worldAnchor1, const idVec3 &worldAnchor2 ) {
- // get anchor relative to center of mass of body1
- anchor1 = ( worldAnchor1 - body1->GetWorldOrigin() ) * body1->GetWorldAxis().Transpose();
- if ( body2 ) {
- // get anchor relative to center of mass of body2
- anchor2 = ( worldAnchor2 - body2->GetWorldOrigin() ) * body2->GetWorldAxis().Transpose();
- }
- else {
- anchor2 = worldAnchor2;
- }
- }
- /*
- ================
- idAFConstraint_Spring::SetSpring
- ================
- */
- void idAFConstraint_Spring::SetSpring( const float stretch, const float compress, const float damping, const float restLength ) {
- assert( stretch >= 0.0f && compress >= 0.0f && restLength >= 0.0f );
- this->kstretch = stretch;
- this->kcompress = compress;
- this->damping = damping;
- this->restLength = restLength;
- }
- /*
- ================
- idAFConstraint_Spring::SetLimit
- ================
- */
- void idAFConstraint_Spring::SetLimit( const float minLength, const float maxLength ) {
- assert( minLength >= 0.0f && maxLength >= 0.0f && maxLength >= minLength );
- this->minLength = minLength;
- this->maxLength = maxLength;
- }
- /*
- ================
- idAFConstraint_Spring::Evaluate
- ================
- */
- void idAFConstraint_Spring::Evaluate( float invTimeStep ) {
- idVec3 a1, a2, velocity1, velocity2, force;
- idVec6 v1, v2;
- float d, dampingForce, length, error;
- bool limit;
- idAFBody *master;
- master = body2 ? body2 : physics->GetMasterBody();
- a1 = body1->GetWorldOrigin() + anchor1 * body1->GetWorldAxis();
- velocity1 = body1->GetPointVelocity( a1 );
- if ( master ) {
- a2 = master->GetWorldOrigin() + anchor2 * master->GetWorldAxis();
- velocity2 = master->GetPointVelocity( a2 );
- }
- else {
- a2 = anchor2;
- velocity2.Zero();
- }
- force = a2 - a1;
- d = force * force;
- if ( d != 0.0f ) {
- dampingForce = damping * idMath::Fabs( (velocity2 - velocity1) * force ) / d;
- }
- else {
- dampingForce = 0.0f;
- }
- length = force.Normalize();
- if ( length > restLength ) {
- if ( kstretch > 0.0f ) {
- idVec3 springForce = force * ( Square( length - restLength ) * kstretch - dampingForce );
- body1->AddForce( a1, springForce );
- if ( master ) {
- master->AddForce( a2, -springForce );
- }
- }
- }
- else {
- if ( kcompress > 0.0f ) {
- idVec3 springForce = force * -( Square( restLength - length ) * kcompress - dampingForce );
- body1->AddForce( a1, springForce );
- if ( master ) {
- master->AddForce( a2, -springForce );
- }
- }
- }
- // check for spring limits
- if ( length < minLength ) {
- force = -force;
- error = minLength - length;
- limit = true;
- }
- else if ( maxLength > 0.0f && length > maxLength ) {
- error = length - maxLength;
- limit = true;
- }
- else {
- error = 0.0f;
- limit = false;
- }
- if ( limit ) {
- a1 -= body1->GetWorldOrigin();
- v1.SubVec3(0) = force;
- v1.SubVec3(1) = a1.Cross( force );
- J1.Set( 1, 6, v1.ToFloatPtr() );
- if ( body2 ) {
- a2 -= body2->GetWorldOrigin();
- v2.SubVec3(0) = -force;
- v2.SubVec3(1) = a2.Cross( -force );
- J2.Set( 1, 6, v2.ToFloatPtr() );
- }
- c1[0] = -( invTimeStep * ERROR_REDUCTION ) * error;
- lo[0] = 0.0f;
- }
- else {
- J1.Zero( 0, 0 );
- J2.Zero( 0, 0 );
- }
- c1.Clamp( -ERROR_REDUCTION_MAX, ERROR_REDUCTION_MAX );
- }
- /*
- ================
- idAFConstraint_Spring::ApplyFriction
- ================
- */
- void idAFConstraint_Spring::ApplyFriction( float invTimeStep ) {
- // no friction
- }
- /*
- ================
- idAFConstraint_Spring::Translate
- ================
- */
- void idAFConstraint_Spring::Translate( const idVec3 &translation ) {
- if ( !body2 ) {
- anchor2 += translation;
- }
- }
- /*
- ================
- idAFConstraint_Spring::Rotate
- ================
- */
- void idAFConstraint_Spring::Rotate( const idRotation &rotation ) {
- if ( !body2 ) {
- anchor2 *= rotation;
- }
- }
- /*
- ================
- idAFConstraint_Spring::GetCenter
- ================
- */
- void idAFConstraint_Spring::GetCenter( idVec3 ¢er ) {
- idAFBody *master;
- idVec3 a1, a2;
- master = body2 ? body2 : physics->GetMasterBody();
- a1 = body1->GetWorldOrigin() + anchor1 * body1->GetWorldAxis();
- if ( master ) {
- a2 = master->GetWorldOrigin() + anchor2 * master->GetWorldAxis();
- }
- else {
- a2 = anchor2;
- }
- center = ( a1 + a2 ) * 0.5f;
- }
- /*
- ================
- idAFConstraint_Spring::DebugDraw
- ================
- */
- void idAFConstraint_Spring::DebugDraw( void ) {
- idAFBody *master;
- float length;
- idVec3 a1, a2, dir, mid, p;
- master = body2 ? body2 : physics->GetMasterBody();
- a1 = body1->GetWorldOrigin() + anchor1 * body1->GetWorldAxis();
- if ( master ) {
- a2 = master->GetWorldOrigin() + anchor2 * master->GetWorldAxis();
- }
- else {
- a2 = anchor2;
- }
- dir = a2 - a1;
- mid = a1 + 0.5f * dir;
- length = dir.Normalize();
- // draw spring
- gameRenderWorld->DebugLine( colorGreen, a1, a2 );
- // draw rest length
- p = restLength * 0.5f * dir;
- gameRenderWorld->DebugCircle( colorWhite, mid + p, dir, 1.0f, 10 );
- gameRenderWorld->DebugCircle( colorWhite, mid - p, dir, 1.0f, 10 );
- if ( restLength > length ) {
- gameRenderWorld->DebugLine( colorWhite, a2, mid + p );
- gameRenderWorld->DebugLine( colorWhite, a1, mid - p );
- }
- if ( minLength > 0.0f ) {
- // draw min length
- gameRenderWorld->DebugCircle( colorBlue, mid + minLength * 0.5f * dir, dir, 2.0f, 10 );
- gameRenderWorld->DebugCircle( colorBlue, mid - minLength * 0.5f * dir, dir, 2.0f, 10 );
- }
- if ( maxLength > 0.0f ) {
- // draw max length
- gameRenderWorld->DebugCircle( colorRed, mid + maxLength * 0.5f * dir, dir, 2.0f, 10 );
- gameRenderWorld->DebugCircle( colorRed, mid - maxLength * 0.5f * dir, dir, 2.0f, 10 );
- }
- }
- /*
- ================
- idAFConstraint_Spring::Save
- ================
- */
- void idAFConstraint_Spring::Save( idSaveGame *saveFile ) const {
- idAFConstraint::Save( saveFile );
- saveFile->WriteVec3( anchor1 );
- saveFile->WriteVec3( anchor2 );
- saveFile->WriteFloat( kstretch );
- saveFile->WriteFloat( kcompress );
- saveFile->WriteFloat( damping );
- saveFile->WriteFloat( restLength );
- saveFile->WriteFloat( minLength );
- saveFile->WriteFloat( maxLength );
- }
- /*
- ================
- idAFConstraint_Spring::Restore
- ================
- */
- void idAFConstraint_Spring::Restore( idRestoreGame *saveFile ) {
- idAFConstraint::Restore( saveFile );
- saveFile->ReadVec3( anchor1 );
- saveFile->ReadVec3( anchor2 );
- saveFile->ReadFloat( kstretch );
- saveFile->ReadFloat( kcompress );
- saveFile->ReadFloat( damping );
- saveFile->ReadFloat( restLength );
- saveFile->ReadFloat( minLength );
- saveFile->ReadFloat( maxLength );
- }
- //===============================================================
- //
- // idAFConstraint_Contact
- //
- //===============================================================
- /*
- ================
- idAFConstraint_Contact::idAFConstraint_Contact
- ================
- */
- idAFConstraint_Contact::idAFConstraint_Contact( void ) {
- name = "contact";
- type = CONSTRAINT_CONTACT;
- InitSize( 1 );
- fc = NULL;
- fl.allowPrimary = false;
- fl.frameConstraint = true;
- }
- /*
- ================
- idAFConstraint_Contact::~idAFConstraint_Contact
- ================
- */
- idAFConstraint_Contact::~idAFConstraint_Contact( void ) {
- if ( fc ) {
- delete fc;
- }
- }
- /*
- ================
- idAFConstraint_Contact::Setup
- ================
- */
- void idAFConstraint_Contact::Setup( idAFBody *b1, idAFBody *b2, contactInfo_t &c ) {
- idVec3 p;
- idVec6 v;
- float vel;
- float minBounceVelocity = 2.0f;
- assert( b1 );
- body1 = b1;
- body2 = b2;
- contact = c;
- p = c.point - body1->GetWorldOrigin();
- v.SubVec3(0) = c.normal;
- v.SubVec3(1) = p.Cross( c.normal );
- J1.Set( 1, 6, v.ToFloatPtr() );
- vel = v.SubVec3(0) * body1->GetLinearVelocity() + v.SubVec3(1) * body1->GetAngularVelocity();
- if ( body2 ) {
- p = c.point - body2->GetWorldOrigin();
- v.SubVec3(0) = -c.normal;
- v.SubVec3(1) = p.Cross( -c.normal );
- J2.Set( 1, 6, v.ToFloatPtr() );
- vel += v.SubVec3(0) * body2->GetLinearVelocity() + v.SubVec3(1) * body2->GetAngularVelocity();
- c2[0] = 0.0f;
- }
- if ( body1->GetBouncyness() > 0.0f && -vel > minBounceVelocity ) {
- c1[0] = body1->GetBouncyness() * vel;
- } else {
- c1[0] = 0.0f;
- }
- e[0] = CONTACT_LCP_EPSILON;
- lo[0] = 0.0f;
- hi[0] = idMath::INFINITY;
- boxConstraint = NULL;
- boxIndex[0] = -1;
- }
- /*
- ================
- idAFConstraint_Contact::Evaluate
- ================
- */
- void idAFConstraint_Contact::Evaluate( float invTimeStep ) {
- // do nothing
- }
- /*
- ================
- idAFConstraint_Contact::ApplyFriction
- ================
- */
- void idAFConstraint_Contact::ApplyFriction( float invTimeStep ) {
- idVec3 r, velocity, normal, dir1, dir2;
- float friction, magnitude, forceNumerator, forceDenominator;
- idVecX impulse, dv;
- friction = body1->GetContactFriction();
- if ( body2 && body2->GetContactFriction() < friction ) {
- friction = body2->GetContactFriction();
- }
- friction *= physics->GetContactFrictionScale();
- if ( friction <= 0.0f ) {
- return;
- }
- // seperate friction per contact is silly but it's fast and often looks close enough
- if ( af_useImpulseFriction.GetBool() ) {
- impulse.SetData( 6, VECX_ALLOCA( 6 ) );
- dv.SetData( 6, VECX_ALLOCA( 6 ) );
- // calculate velocity in the contact plane
- r = contact.point - body1->GetWorldOrigin();
- velocity = body1->GetLinearVelocity() + body1->GetAngularVelocity().Cross( r );
- velocity -= contact.normal * velocity * contact.normal;
- // get normalized direction of friction and magnitude of velocity
- normal = -velocity;
- magnitude = normal.Normalize();
- forceNumerator = friction * magnitude;
- forceDenominator = body1->GetInverseMass() + ( ( body1->GetInverseWorldInertia() * r.Cross( normal ) ).Cross( r ) * normal );
- impulse.SubVec3(0) = (forceNumerator / forceDenominator) * normal;
- impulse.SubVec3(1) = r.Cross( impulse.SubVec3(0) );
- body1->InverseWorldSpatialInertiaMultiply( dv, impulse.ToFloatPtr() );
- // modify velocity with friction force
- body1->SetLinearVelocity( body1->GetLinearVelocity() + dv.SubVec3(0) );
- body1->SetAngularVelocity( body1->GetAngularVelocity() + dv.SubVec3(1) );
- }
- else {
- if ( !fc ) {
- fc = new idAFConstraint_ContactFriction;
- }
- // call setup each frame because contact constraints are re-used for different bodies
- fc->Setup( this );
- fc->Add( physics, invTimeStep );
- }
- }
- /*
- ================
- idAFConstraint_Contact::Translate
- ================
- */
- void idAFConstraint_Contact::Translate( const idVec3 &translation ) {
- assert( 0 ); // contact should never be translated
- }
- /*
- ================
- idAFConstraint_Contact::Rotate
- ================
- */
- void idAFConstraint_Contact::Rotate( const idRotation &rotation ) {
- assert( 0 ); // contact should never be rotated
- }
- /*
- ================
- idAFConstraint_Contact::GetCenter
- ================
- */
- void idAFConstraint_Contact::GetCenter( idVec3 ¢er ) {
- center = contact.point;
- }
- /*
- ================
- idAFConstraint_Contact::DebugDraw
- ================
- */
- void idAFConstraint_Contact::DebugDraw( void ) {
- idVec3 x, y;
- contact.normal.NormalVectors( x, y );
- gameRenderWorld->DebugLine( colorWhite, contact.point, contact.point + 6.0f * contact.normal );
- gameRenderWorld->DebugLine( colorWhite, contact.point - 2.0f * x, contact.point + 2.0f * x );
- gameRenderWorld->DebugLine( colorWhite, contact.point - 2.0f * y, contact.point + 2.0f * y );
- }
- //===============================================================
- //
- // idAFConstraint_ContactFriction
- //
- //===============================================================
- /*
- ================
- idAFConstraint_ContactFriction::idAFConstraint_ContactFriction
- ================
- */
- idAFConstraint_ContactFriction::idAFConstraint_ContactFriction( void ) {
- type = CONSTRAINT_FRICTION;
- name = "contactFriction";
- InitSize( 2 );
- cc = NULL;
- fl.allowPrimary = false;
- fl.frameConstraint = true;
- }
- /*
- ================
- idAFConstraint_ContactFriction::Setup
- ================
- */
- void idAFConstraint_ContactFriction::Setup( idAFConstraint_Contact *cc ) {
- this->cc = cc;
- body1 = cc->GetBody1();
- body2 = cc->GetBody2();
- }
- /*
- ================
- idAFConstraint_ContactFriction::Evaluate
- ================
- */
- void idAFConstraint_ContactFriction::Evaluate( float invTimeStep ) {
- // do nothing
- }
- /*
- ================
- idAFConstraint_ContactFriction::ApplyFriction
- ================
- */
- void idAFConstraint_ContactFriction::ApplyFriction( float invTimeStep ) {
- // do nothing
- }
- /*
- ================
- idAFConstraint_ContactFriction::Add
- ================
- */
- bool idAFConstraint_ContactFriction::Add( idPhysics_AF *phys, float invTimeStep ) {
- idVec3 r, dir1, dir2;
- float friction;
- int newRow;
- physics = phys;
- friction = body1->GetContactFriction() * physics->GetContactFrictionScale();
- // if the body only has friction in one direction
- if ( body1->GetFrictionDirection( dir1 ) ) {
- // project the friction direction into the contact plane
- dir1 -= dir1 * cc->GetContact().normal * dir1;
- dir1.Normalize();
- r = cc->GetContact().point - body1->GetWorldOrigin();
- J1.SetSize( 1, 6 );
- J1.SubVec6(0).SubVec3(0) = dir1;
- J1.SubVec6(0).SubVec3(1) = r.Cross( dir1 );
- c1.SetSize( 1 );
- c1[0] = 0.0f;
- if ( body2 ) {
- r = cc->GetContact().point - body2->GetWorldOrigin();
- J2.SetSize( 1, 6 );
- J2.SubVec6(0).SubVec3(0) = -dir1;
- J2.SubVec6(0).SubVec3(1) = r.Cross( -dir1 );
- c2.SetSize( 1 );
- c2[0] = 0.0f;
- }
- lo[0] = -friction;
- hi[0] = friction;
- boxConstraint = cc;
- boxIndex[0] = 0;
- }
- else {
- // get two friction directions orthogonal to contact normal
- cc->GetContact().normal.NormalVectors( dir1, dir2 );
- r = cc->GetContact().point - body1->GetWorldOrigin();
- J1.SetSize( 2, 6 );
- J1.SubVec6(0).SubVec3(0) = dir1;
- J1.SubVec6(0).SubVec3(1) = r.Cross( dir1 );
- J1.SubVec6(1).SubVec3(0) = dir2;
- J1.SubVec6(1).SubVec3(1) = r.Cross( dir2 );
- c1.SetSize( 2 );
- c1[0] = c1[1] = 0.0f;
- if ( body2 ) {
- r = cc->GetContact().point - body2->GetWorldOrigin();
- J2.SetSize( 2, 6 );
- J2.SubVec6(0).SubVec3(0) = -dir1;
- J2.SubVec6(0).SubVec3(1) = r.Cross( -dir1 );
- J2.SubVec6(1).SubVec3(0) = -dir2;
- J2.SubVec6(1).SubVec3(1) = r.Cross( -dir2 );
- c2.SetSize( 2 );
- c2[0] = c2[1] = 0.0f;
- if ( body2->GetContactFriction() < friction ) {
- friction = body2->GetContactFriction();
- }
- }
- lo[0] = -friction;
- hi[0] = friction;
- boxConstraint = cc;
- boxIndex[0] = 0;
- lo[1] = -friction;
- hi[1] = friction;
- boxIndex[1] = 0;
- }
- if ( body1->GetContactMotorDirection( dir1 ) && body1->GetContactMotorForce() > 0.0f ) {
- // project the motor force direction into the contact plane
- dir1 -= dir1 * cc->GetContact().normal * dir1;
- dir1.Normalize();
- r = cc->GetContact().point - body1->GetWorldOrigin();
- newRow = J1.GetNumRows();
- J1.ChangeSize( newRow+1, J1.GetNumColumns() );
- J1.SubVec6(newRow).SubVec3(0) = -dir1;
- J1.SubVec6(newRow).SubVec3(1) = r.Cross( -dir1 );
- c1.ChangeSize( newRow+1 );
- c1[newRow] = body1->GetContactMotorVelocity();
- if ( body2 ) {
- r = cc->GetContact().point - body2->GetWorldOrigin();
- J2.ChangeSize( newRow+1, J2.GetNumColumns() );
- J2.SubVec6(newRow).SubVec3(0) = -dir1;
- J2.SubVec6(newRow).SubVec3(1) = r.Cross( -dir1 );
- c2.ChangeSize( newRow+1 );
- c2[newRow] = 0.0f;
- }
- lo[newRow] = -body1->GetContactMotorForce();
- hi[newRow] = body1->GetContactMotorForce();
- boxIndex[newRow] = -1;
- }
- physics->AddFrameConstraint( this );
- return true;
- }
- /*
- ================
- idAFConstraint_ContactFriction::Translate
- ================
- */
- void idAFConstraint_ContactFriction::Translate( const idVec3 &translation ) {
- }
- /*
- ================
- idAFConstraint_ContactFriction::Rotate
- ================
- */
- void idAFConstraint_ContactFriction::Rotate( const idRotation &rotation ) {
- }
- /*
- ================
- idAFConstraint_ContactFriction::DebugDraw
- ================
- */
- void idAFConstraint_ContactFriction::DebugDraw( void ) {
- }
- //===============================================================
- //
- // idAFConstraint_ConeLimit
- //
- //===============================================================
- /*
- ================
- idAFConstraint_ConeLimit::idAFConstraint_ConeLimit
- ================
- */
- idAFConstraint_ConeLimit::idAFConstraint_ConeLimit( void ) {
- type = CONSTRAINT_CONELIMIT;
- name = "coneLimit";
- InitSize( 1 );
- fl.allowPrimary = false;
- fl.frameConstraint = true;
- }
- /*
- ================
- idAFConstraint_ConeLimit::Setup
- the coneAnchor is the top of the cone in body2 space
- the coneAxis is the axis of the cone in body2 space
- the coneAngle is the angle the cone hull makes at the top
- the body1Axis is the axis in body1 space that should stay within the cone
- ================
- */
- void idAFConstraint_ConeLimit::Setup( idAFBody *b1, idAFBody *b2, const idVec3 &coneAnchor, const idVec3 &coneAxis, const float coneAngle, const idVec3 &body1Axis ) {
- this->body1 = b1;
- this->body2 = b2;
- this->coneAxis = coneAxis;
- this->coneAxis.Normalize();
- this->coneAnchor = coneAnchor;
- this->body1Axis = body1Axis;
- this->body1Axis.Normalize();
- this->cosAngle = (float) cos( DEG2RAD( coneAngle * 0.5f ) );
- this->sinHalfAngle = (float) sin( DEG2RAD( coneAngle * 0.25f ) );
- this->cosHalfAngle = (float) cos( DEG2RAD( coneAngle * 0.25f ) );
- }
- /*
- ================
- idAFConstraint_ConeLimit::SetAnchor
- ================
- */
- void idAFConstraint_ConeLimit::SetAnchor( const idVec3 &coneAnchor ) {
- this->coneAnchor = coneAnchor;
- }
- /*
- ================
- idAFConstraint_ConeLimit::SetBody1Axis
- ================
- */
- void idAFConstraint_ConeLimit::SetBody1Axis( const idVec3 &body1Axis ) {
- this->body1Axis = body1Axis;
- }
- /*
- ================
- idAFConstraint_ConeLimit::Evaluate
- ================
- */
- void idAFConstraint_ConeLimit::Evaluate( float invTimeStep ) {
- // do nothing
- }
- /*
- ================
- idAFConstraint_ConeLimit::ApplyFriction
- ================
- */
- void idAFConstraint_ConeLimit::ApplyFriction( float invTimeStep ) {
- }
- /*
- ================
- idAFConstraint_ConeLimit::Add
- ================
- */
- bool idAFConstraint_ConeLimit::Add( idPhysics_AF *phys, float invTimeStep ) {
- float a;
- idVec6 J1row, J2row;
- idVec3 ax, anchor, body1ax, normal, coneVector, p1, p2;
- idQuat q;
- idAFBody *master;
- if ( af_skipLimits.GetBool() ) {
- lm.Zero(); // constraint exerts no force
- return false;
- }
- physics = phys;
- master = body2 ? body2 : physics->GetMasterBody();
- if ( master ) {
- ax = coneAxis * master->GetWorldAxis();
- anchor = master->GetWorldOrigin() + coneAnchor * master->GetWorldAxis();
- }
- else {
- ax = coneAxis;
- anchor = coneAnchor;
- }
- body1ax = body1Axis * body1->GetWorldAxis();
- a = ax * body1ax;
- // if the body1 axis is inside the cone
- if ( a > cosAngle ) {
- lm.Zero(); // constraint exerts no force
- return false;
- }
- // calculate the inward cone normal for the position the body1 axis went outside the cone
- normal = body1ax.Cross( ax );
- normal.Normalize();
- q.x = normal.x * sinHalfAngle;
- q.y = normal.y * sinHalfAngle;
- q.z = normal.z * sinHalfAngle;
- q.w = cosHalfAngle;
- coneVector = ax * q.ToMat3();
- normal = coneVector.Cross( ax ).Cross( coneVector );
- normal.Normalize();
- p1 = anchor + 32.0f * coneVector - body1->GetWorldOrigin();
- J1row.SubVec3(0) = normal;
- J1row.SubVec3(1) = p1.Cross( normal );
- J1.Set( 1, 6, J1row.ToFloatPtr() );
- c1[0] = (invTimeStep * LIMIT_ERROR_REDUCTION) * ( normal * (32.0f * body1ax) );
- if ( body2 ) {
- p2 = anchor + 32.0f * coneVector - master->GetWorldOrigin();
- J2row.SubVec3(0) = -normal;
- J2row.SubVec3(1) = p2.Cross( -normal );
- J2.Set( 1, 6, J2row.ToFloatPtr() );
- c2[0] = 0.0f;
- }
- lo[0] = 0.0f;
- e[0] = LIMIT_LCP_EPSILON;
- physics->AddFrameConstraint( this );
- return true;
- }
- /*
- ================
- idAFConstraint_ConeLimit::Translate
- ================
- */
- void idAFConstraint_ConeLimit::Translate( const idVec3 &translation ) {
- if ( !body2 ) {
- coneAnchor += translation;
- }
- }
- /*
- ================
- idAFConstraint_ConeLimit::Rotate
- ================
- */
- void idAFConstraint_ConeLimit::Rotate( const idRotation &rotation ) {
- if ( !body2 ) {
- coneAnchor *= rotation;
- coneAxis *= rotation.ToMat3();
- }
- }
- /*
- ================
- idAFConstraint_ConeLimit::DebugDraw
- ================
- */
- void idAFConstraint_ConeLimit::DebugDraw( void ) {
- idVec3 ax, anchor, x, y, z, start, end;
- float sinAngle, a, size = 10.0f;
- idAFBody *master;
- master = body2 ? body2 : physics->GetMasterBody();
- if ( master ) {
- ax = coneAxis * master->GetWorldAxis();
- anchor = master->GetWorldOrigin() + coneAnchor * master->GetWorldAxis();
- }
- else {
- ax = coneAxis;
- anchor = coneAnchor;
- }
- // draw body1 axis
- gameRenderWorld->DebugLine( colorGreen, anchor, anchor + size * (body1Axis * body1->GetWorldAxis()) );
- // draw cone
- ax.NormalVectors( x, y );
- sinAngle = idMath::Sqrt( 1.0f - cosAngle * cosAngle );
- x *= size * sinAngle;
- y *= size * sinAngle;
- z = anchor + ax * size * cosAngle;
- start = x + z;
- for ( a = 0.0f; a < 360.0f; a += 45.0f ) {
- end = x * (float) cos( DEG2RAD(a + 45.0f) ) + y * (float) sin( DEG2RAD(a + 45.0f) ) + z;
- gameRenderWorld->DebugLine( colorMagenta, anchor, start );
- gameRenderWorld->DebugLine( colorMagenta, start, end );
- start = end;
- }
- }
- /*
- ================
- idAFConstraint_ConeLimit::Save
- ================
- */
- void idAFConstraint_ConeLimit::Save( idSaveGame *saveFile ) const {
- idAFConstraint::Save( saveFile );
- saveFile->WriteVec3( coneAnchor );
- saveFile->WriteVec3( coneAxis );
- saveFile->WriteVec3( body1Axis );
- saveFile->WriteFloat( cosAngle );
- saveFile->WriteFloat( sinHalfAngle );
- saveFile->WriteFloat( cosHalfAngle );
- saveFile->WriteFloat( epsilon );
- }
- /*
- ================
- idAFConstraint_ConeLimit::Restore
- ================
- */
- void idAFConstraint_ConeLimit::Restore( idRestoreGame *saveFile ) {
- idAFConstraint::Restore( saveFile );
- saveFile->ReadVec3( coneAnchor );
- saveFile->ReadVec3( coneAxis );
- saveFile->ReadVec3( body1Axis );
- saveFile->ReadFloat( cosAngle );
- saveFile->ReadFloat( sinHalfAngle );
- saveFile->ReadFloat( cosHalfAngle );
- saveFile->ReadFloat( epsilon );
- }
- //===============================================================
- //
- // idAFConstraint_PyramidLimit
- //
- //===============================================================
- /*
- ================
- idAFConstraint_PyramidLimit::idAFConstraint_PyramidLimit
- ================
- */
- idAFConstraint_PyramidLimit::idAFConstraint_PyramidLimit( void ) {
- type = CONSTRAINT_PYRAMIDLIMIT;
- name = "pyramidLimit";
- InitSize( 1 );
- fl.allowPrimary = false;
- fl.frameConstraint = true;
- }
- /*
- ================
- idAFConstraint_PyramidLimit::Setup
- ================
- */
- void idAFConstraint_PyramidLimit::Setup( idAFBody *b1, idAFBody *b2, const idVec3 &pyramidAnchor,
- const idVec3 &pyramidAxis, const idVec3 &baseAxis,
- const float pyramidAngle1, const float pyramidAngle2, const idVec3 &body1Axis ) {
- body1 = b1;
- body2 = b2;
- // setup the base and make sure the basis is orthonormal
- pyramidBasis[2] = pyramidAxis;
- pyramidBasis[2].Normalize();
- pyramidBasis[0] = baseAxis;
- pyramidBasis[0] -= pyramidBasis[2] * baseAxis * pyramidBasis[2];
- pyramidBasis[0].Normalize();
- pyramidBasis[1] = pyramidBasis[0].Cross( pyramidBasis[2] );
- // pyramid top
- this->pyramidAnchor = pyramidAnchor;
- // angles
- cosAngle[0] = (float) cos( DEG2RAD( pyramidAngle1 * 0.5f ) );
- cosAngle[1] = (float) cos( DEG2RAD( pyramidAngle2 * 0.5f ) );
- sinHalfAngle[0] = (float) sin( DEG2RAD( pyramidAngle1 * 0.25f ) );
- sinHalfAngle[1] = (float) sin( DEG2RAD( pyramidAngle2 * 0.25f ) );
- cosHalfAngle[0] = (float) cos( DEG2RAD( pyramidAngle1 * 0.25f ) );
- cosHalfAngle[1] = (float) cos( DEG2RAD( pyramidAngle2 * 0.25f ) );
- this->body1Axis = body1Axis;
- }
- /*
- ================
- idAFConstraint_PyramidLimit::SetAnchor
- ================
- */
- void idAFConstraint_PyramidLimit::SetAnchor( const idVec3 &pyramidAnchor ) {
- this->pyramidAnchor = pyramidAnchor;
- }
- /*
- ================
- idAFConstraint_PyramidLimit::SetBody1Axis
- ================
- */
- void idAFConstraint_PyramidLimit::SetBody1Axis( const idVec3 &body1Axis ) {
- this->body1Axis = body1Axis;
- }
- /*
- ================
- idAFConstraint_PyramidLimit::Evaluate
- ================
- */
- void idAFConstraint_PyramidLimit::Evaluate( float invTimeStep ) {
- // do nothing
- }
- /*
- ================
- idAFConstraint_PyramidLimit::ApplyFriction
- ================
- */
- void idAFConstraint_PyramidLimit::ApplyFriction( float invTimeStep ) {
- }
- /*
- ================
- idAFConstraint_PyramidLimit::Add
- ================
- */
- bool idAFConstraint_PyramidLimit::Add( idPhysics_AF *phys, float invTimeStep ) {
- int i;
- float a[2];
- idVec6 J1row, J2row;
- idMat3 worldBase;
- idVec3 anchor, body1ax, ax[2], v, normal, pyramidVector, p1, p2;
- idQuat q;
- idAFBody *master;
- if ( af_skipLimits.GetBool() ) {
- lm.Zero(); // constraint exerts no force
- return false;
- }
- physics = phys;
- master = body2 ? body2 : physics->GetMasterBody();
- if ( master ) {
- worldBase[0] = pyramidBasis[0] * master->GetWorldAxis();
- worldBase[1] = pyramidBasis[1] * master->GetWorldAxis();
- worldBase[2] = pyramidBasis[2] * master->GetWorldAxis();
- anchor = master->GetWorldOrigin() + pyramidAnchor * master->GetWorldAxis();
- }
- else {
- worldBase = pyramidBasis;
- anchor = pyramidAnchor;
- }
- body1ax = body1Axis * body1->GetWorldAxis();
- for ( i = 0; i < 2; i++ ) {
- ax[i] = body1ax - worldBase[!i] * body1ax * worldBase[!i];
- ax[i].Normalize();
- a[i] = worldBase[2] * ax[i];
- }
- // if the body1 axis is inside the pyramid
- if ( a[0] > cosAngle[0] && a[1] > cosAngle[1] ) {
- lm.Zero(); // constraint exerts no force
- return false;
- }
- // calculate the inward pyramid normal for the position the body1 axis went outside the pyramid
- pyramidVector = worldBase[2];
- for ( i = 0; i < 2; i++ ) {
- if ( a[i] <= cosAngle[i] ) {
- v = ax[i].Cross( worldBase[2] );
- v.Normalize();
- q.x = v.x * sinHalfAngle[i];
- q.y = v.y * sinHalfAngle[i];
- q.z = v.z * sinHalfAngle[i];
- q.w = cosHalfAngle[i];
- pyramidVector *= q.ToMat3();
- }
- }
- normal = pyramidVector.Cross( worldBase[2] ).Cross( pyramidVector );
- normal.Normalize();
- p1 = anchor + 32.0f * pyramidVector - body1->GetWorldOrigin();
- J1row.SubVec3(0) = normal;
- J1row.SubVec3(1) = p1.Cross( normal );
- J1.Set( 1, 6, J1row.ToFloatPtr() );
- c1[0] = (invTimeStep * LIMIT_ERROR_REDUCTION) * ( normal * (32.0f * body1ax) );
- if ( body2 ) {
- p2 = anchor + 32.0f * pyramidVector - master->GetWorldOrigin();
- J2row.SubVec3(0) = -normal;
- J2row.SubVec3(1) = p2.Cross( -normal );
- J2.Set( 1, 6, J2row.ToFloatPtr() );
- c2[0] = 0.0f;
- }
- lo[0] = 0.0f;
- e[0] = LIMIT_LCP_EPSILON;
- physics->AddFrameConstraint( this );
- return true;
- }
- /*
- ================
- idAFConstraint_PyramidLimit::Translate
- ================
- */
- void idAFConstraint_PyramidLimit::Translate( const idVec3 &translation ) {
- if ( !body2 ) {
- pyramidAnchor += translation;
- }
- }
- /*
- ================
- idAFConstraint_PyramidLimit::Rotate
- ================
- */
- void idAFConstraint_PyramidLimit::Rotate( const idRotation &rotation ) {
- if ( !body2 ) {
- pyramidAnchor *= rotation;
- pyramidBasis[0] *= rotation.ToMat3();
- pyramidBasis[1] *= rotation.ToMat3();
- pyramidBasis[2] *= rotation.ToMat3();
- }
- }
- /*
- ================
- idAFConstraint_PyramidLimit::DebugDraw
- ================
- */
- void idAFConstraint_PyramidLimit::DebugDraw( void ) {
- int i;
- float size = 10.0f;
- idVec3 anchor, dir, p[4];
- idMat3 worldBase, m[2];
- idQuat q;
- idAFBody *master;
- master = body2 ? body2 : physics->GetMasterBody();
- if ( master ) {
- worldBase[0] = pyramidBasis[0] * master->GetWorldAxis();
- worldBase[1] = pyramidBasis[1] * master->GetWorldAxis();
- worldBase[2] = pyramidBasis[2] * master->GetWorldAxis();
- anchor = master->GetWorldOrigin() + pyramidAnchor * master->GetWorldAxis();
- }
- else {
- worldBase = pyramidBasis;
- anchor = pyramidAnchor;
- }
- // draw body1 axis
- gameRenderWorld->DebugLine( colorGreen, anchor, anchor + size * (body1Axis * body1->GetWorldAxis()) );
- // draw the pyramid
- for ( i = 0; i < 2; i++ ) {
- q.x = worldBase[!i].x * sinHalfAngle[i];
- q.y = worldBase[!i].y * sinHalfAngle[i];
- q.z = worldBase[!i].z * sinHalfAngle[i];
- q.w = cosHalfAngle[i];
- m[i] = q.ToMat3();
- }
- dir = worldBase[2] * size;
- p[0] = anchor + m[0] * (m[1] * dir);
- p[1] = anchor + m[0] * (m[1].Transpose() * dir);
- p[2] = anchor + m[0].Transpose() * (m[1].Transpose() * dir);
- p[3] = anchor + m[0].Transpose() * (m[1] * dir);
- for ( i = 0; i < 4; i++ ) {
- gameRenderWorld->DebugLine( colorMagenta, anchor, p[i] );
- gameRenderWorld->DebugLine( colorMagenta, p[i], p[(i+1)&3] );
- }
- }
- /*
- ================
- idAFConstraint_PyramidLimit::Save
- ================
- */
- void idAFConstraint_PyramidLimit::Save( idSaveGame *saveFile ) const {
- idAFConstraint::Save( saveFile );
- saveFile->WriteVec3( pyramidAnchor );
- saveFile->WriteMat3( pyramidBasis );
- saveFile->WriteVec3( body1Axis );
- saveFile->WriteFloat( cosAngle[0] );
- saveFile->WriteFloat( cosAngle[1] );
- saveFile->WriteFloat( sinHalfAngle[0] );
- saveFile->WriteFloat( sinHalfAngle[1] );
- saveFile->WriteFloat( cosHalfAngle[0] );
- saveFile->WriteFloat( cosHalfAngle[1] );
- saveFile->WriteFloat( epsilon );
- }
- /*
- ================
- idAFConstraint_PyramidLimit::Restore
- ================
- */
- void idAFConstraint_PyramidLimit::Restore( idRestoreGame *saveFile ) {
- idAFConstraint::Restore( saveFile );
- saveFile->ReadVec3( pyramidAnchor );
- saveFile->ReadMat3( pyramidBasis );
- saveFile->ReadVec3( body1Axis );
- saveFile->ReadFloat( cosAngle[0] );
- saveFile->ReadFloat( cosAngle[1] );
- saveFile->ReadFloat( sinHalfAngle[0] );
- saveFile->ReadFloat( sinHalfAngle[1] );
- saveFile->ReadFloat( cosHalfAngle[0] );
- saveFile->ReadFloat( cosHalfAngle[1] );
- saveFile->ReadFloat( epsilon );
- }
- //===============================================================
- //
- // idAFConstraint_Suspension
- //
- //===============================================================
- /*
- ================
- idAFConstraint_Suspension::idAFConstraint_Suspension
- ================
- */
- idAFConstraint_Suspension::idAFConstraint_Suspension( void ) {
- type = CONSTRAINT_SUSPENSION;
- name = "suspension";
- InitSize( 3 );
- fl.allowPrimary = false;
- fl.frameConstraint = true;
- localOrigin.Zero();
- localAxis.Identity();
- suspensionUp = 0.0f;
- suspensionDown = 0.0f;
- suspensionKCompress = 0.0f;
- suspensionDamping = 0.0f;
- steerAngle = 0.0f;
- friction = 2.0f;
- motorEnabled = false;
- motorForce = 0.0f;
- motorVelocity = 0.0f;
- wheelModel = NULL;
- memset( &trace, 0, sizeof( trace ) );
- epsilon = LCP_EPSILON;
- }
- /*
- ================
- idAFConstraint_Suspension::Setup
- ================
- */
- void idAFConstraint_Suspension::Setup( const char *name, idAFBody *body, const idVec3 &origin, const idMat3 &axis, idClipModel *clipModel ) {
- this->name = name;
- body1 = body;
- body2 = NULL;
- localOrigin = ( origin - body->GetWorldOrigin() ) * body->GetWorldAxis().Transpose();
- localAxis = axis * body->GetWorldAxis().Transpose();
- wheelModel = clipModel;
- }
- /*
- ================
- idAFConstraint_Suspension::SetSuspension
- ================
- */
- void idAFConstraint_Suspension::SetSuspension( const float up, const float down, const float k, const float d, const float f ) {
- suspensionUp = up;
- suspensionDown = down;
- suspensionKCompress = k;
- suspensionDamping = d;
- friction = f;
- }
- /*
- ================
- idAFConstraint_Suspension::GetWheelOrigin
- ================
- */
- const idVec3 idAFConstraint_Suspension::GetWheelOrigin( void ) const {
- return body1->GetWorldOrigin() + wheelOffset * body1->GetWorldAxis();
- }
- /*
- ================
- idAFConstraint_Suspension::Evaluate
- ================
- */
- void idAFConstraint_Suspension::Evaluate( float invTimeStep ) {
- float velocity, suspensionLength, springLength, compression, dampingForce, springForce;
- idVec3 origin, start, end, vel1, vel2, springDir, r, frictionDir, motorDir;
- idMat3 axis;
- idRotation rotation;
- axis = localAxis * body1->GetWorldAxis();
- origin = body1->GetWorldOrigin() + localOrigin * body1->GetWorldAxis();
- start = origin + suspensionUp * axis[2];
- end = origin - suspensionDown * axis[2];
- rotation.SetVec( axis[2] );
- rotation.SetAngle( steerAngle );
- axis *= rotation.ToMat3();
- gameLocal.clip.Translation( trace, start, end, wheelModel, axis, MASK_SOLID, NULL );
- wheelOffset = ( trace.endpos - body1->GetWorldOrigin() ) * body1->GetWorldAxis().Transpose();
- if ( trace.fraction >= 1.0f ) {
- J1.SetSize( 0, 6 );
- if ( body2 ) {
- J2.SetSize( 0, 6 );
- }
- return;
- }
- // calculate and add spring force
- vel1 = body1->GetPointVelocity( start );
- if ( body2 ) {
- vel2 = body2->GetPointVelocity( trace.c.point );
- } else {
- vel2.Zero();
- }
- suspensionLength = suspensionUp + suspensionDown;
- springDir = trace.endpos - start;
- springLength = trace.fraction * suspensionLength;
- dampingForce = suspensionDamping * idMath::Fabs( ( vel2 - vel1 ) * springDir ) / ( 1.0f + springLength * springLength );
- compression = suspensionLength - springLength;
- springForce = compression * compression * suspensionKCompress - dampingForce;
- r = trace.c.point - body1->GetWorldOrigin();
- J1.SetSize( 2, 6 );
- J1.SubVec6(0).SubVec3(0) = trace.c.normal;
- J1.SubVec6(0).SubVec3(1) = r.Cross( trace.c.normal );
- c1.SetSize( 2 );
- c1[0] = 0.0f;
- velocity = J1.SubVec6(0).SubVec3(0) * body1->GetLinearVelocity() + J1.SubVec6(0).SubVec3(1) * body1->GetAngularVelocity();
- if ( body2 ) {
- r = trace.c.point - body2->GetWorldOrigin();
- J2.SetSize( 2, 6 );
- J2.SubVec6(0).SubVec3(0) = -trace.c.normal;
- J2.SubVec6(0).SubVec3(1) = r.Cross( -trace.c.normal );
- c2.SetSize( 2 );
- c2[0] = 0.0f;
- velocity += J2.SubVec6(0).SubVec3(0) * body2->GetLinearVelocity() + J2.SubVec6(0).SubVec3(1) * body2->GetAngularVelocity();
- }
- c1[0] = -compression; // + 0.5f * -velocity;
- e[0] = 1e-4f;
- lo[0] = 0.0f;
- hi[0] = springForce;
- boxConstraint = NULL;
- boxIndex[0] = -1;
- // project the friction direction into the contact plane
- frictionDir = axis[1] - axis[1] * trace.c.normal * axis[1];
- frictionDir.Normalize();
- r = trace.c.point - body1->GetWorldOrigin();
- J1.SubVec6(1).SubVec3(0) = frictionDir;
- J1.SubVec6(1).SubVec3(1) = r.Cross( frictionDir );
- c1[1] = 0.0f;
- if ( body2 ) {
- r = trace.c.point - body2->GetWorldOrigin();
- J2.SubVec6(1).SubVec3(0) = -frictionDir;
- J2.SubVec6(1).SubVec3(1) = r.Cross( -frictionDir );
- c2[1] = 0.0f;
- }
- lo[1] = -friction * physics->GetContactFrictionScale();
- hi[1] = friction * physics->GetContactFrictionScale();
- boxConstraint = this;
- boxIndex[1] = 0;
- if ( motorEnabled ) {
- // project the motor force direction into the contact plane
- motorDir = axis[0] - axis[0] * trace.c.normal * axis[0];
- motorDir.Normalize();
- r = trace.c.point - body1->GetWorldOrigin();
- J1.ChangeSize( 3, J1.GetNumColumns() );
- J1.SubVec6(2).SubVec3(0) = -motorDir;
- J1.SubVec6(2).SubVec3(1) = r.Cross( -motorDir );
- c1.ChangeSize( 3 );
- c1[2] = motorVelocity;
- if ( body2 ) {
- r = trace.c.point - body2->GetWorldOrigin();
- J2.ChangeSize( 3, J2.GetNumColumns() );
- J2.SubVec6(2).SubVec3(0) = -motorDir;
- J2.SubVec6(2).SubVec3(1) = r.Cross( -motorDir );
- c2.ChangeSize( 3 );
- c2[2] = 0.0f;
- }
- lo[2] = -motorForce;
- hi[2] = motorForce;
- boxIndex[2] = -1;
- }
- }
- /*
- ================
- idAFConstraint_Suspension::ApplyFriction
- ================
- */
- void idAFConstraint_Suspension::ApplyFriction( float invTimeStep ) {
- // do nothing
- }
- /*
- ================
- idAFConstraint_Suspension::Translate
- ================
- */
- void idAFConstraint_Suspension::Translate( const idVec3 &translation ) {
- }
- /*
- ================
- idAFConstraint_Suspension::Rotate
- ================
- */
- void idAFConstraint_Suspension::Rotate( const idRotation &rotation ) {
- }
- /*
- ================
- idAFConstraint_Suspension::DebugDraw
- ================
- */
- void idAFConstraint_Suspension::DebugDraw( void ) {
- idVec3 origin;
- idMat3 axis;
- idRotation rotation;
- axis = localAxis * body1->GetWorldAxis();
- rotation.SetVec( axis[2] );
- rotation.SetAngle( steerAngle );
- axis *= rotation.ToMat3();
- if ( trace.fraction < 1.0f ) {
- origin = trace.c.point;
- gameRenderWorld->DebugLine( colorWhite, origin, origin + 6.0f * axis[2] );
- gameRenderWorld->DebugLine( colorWhite, origin - 4.0f * axis[0], origin + 4.0f * axis[0] );
- gameRenderWorld->DebugLine( colorWhite, origin - 2.0f * axis[1], origin + 2.0f * axis[1] );
- }
- }
- //===============================================================
- //
- // idAFBody
- //
- //===============================================================
- /*
- ================
- idAFBody::idAFBody
- ================
- */
- idAFBody::idAFBody( void ) {
- Init();
- }
- /*
- ================
- idAFBody::idAFBody
- ================
- */
- idAFBody::idAFBody( const idStr &name, idClipModel *clipModel, float density ) {
- assert( clipModel );
- assert( clipModel->IsTraceModel() );
- Init();
- this->name = name;
- this->clipModel = NULL;
- SetClipModel( clipModel );
- SetDensity( density );
- current->worldOrigin = clipModel->GetOrigin();
- current->worldAxis = clipModel->GetAxis();
- *next = *current;
- }
- /*
- ================
- idAFBody::~idAFBody
- ================
- */
- idAFBody::~idAFBody( void ) {
- delete clipModel;
- }
- /*
- ================
- idAFBody::Init
- ================
- */
- void idAFBody::Init( void ) {
- name = "noname";
- parent = NULL;
- clipModel = NULL;
- primaryConstraint = NULL;
- tree = NULL;
- linearFriction = -1.0f;
- angularFriction = -1.0f;
- contactFriction = -1.0f;
- bouncyness = -1.0f;
- clipMask = 0;
- frictionDir = vec3_zero;
- contactMotorDir = vec3_zero;
- contactMotorVelocity = 0.0f;
- contactMotorForce = 0.0f;
- mass = 1.0f;
- invMass = 1.0f;
- centerOfMass = vec3_zero;
- inertiaTensor = mat3_identity;
- inverseInertiaTensor = mat3_identity;
- current = &state[0];
- next = &state[1];
- current->worldOrigin = vec3_zero;
- current->worldAxis = mat3_identity;
- current->spatialVelocity = vec6_zero;
- current->externalForce = vec6_zero;
- *next = *current;
- saved = *current;
- atRestOrigin = vec3_zero;
- atRestAxis = mat3_identity;
- s.Zero( 6 );
- totalForce.Zero( 6 );
- auxForce.Zero( 6 );
- acceleration.Zero( 6 );
- response = NULL;
- responseIndex = NULL;
- numResponses = 0;
- maxAuxiliaryIndex = 0;
- maxSubTreeAuxiliaryIndex = 0;
- memset( &fl, 0, sizeof( fl ) );
- fl.selfCollision = true;
- fl.isZero = true;
- }
- /*
- ================
- idAFBody::SetClipModel
- ================
- */
- void idAFBody::SetClipModel( idClipModel *clipModel ) {
- if ( this->clipModel && this->clipModel != clipModel ) {
- delete this->clipModel;
- }
- this->clipModel = clipModel;
- }
- /*
- ================
- idAFBody::SetFriction
- ================
- */
- void idAFBody::SetFriction( float linear, float angular, float contact ) {
- if ( linear < 0.0f || linear > 1.0f ||
- angular < 0.0f || angular > 1.0f ||
- contact < 0.0f ) {
- gameLocal.Warning( "idAFBody::SetFriction: friction out of range, linear = %.1f, angular = %.1f, contact = %.1f", linear, angular, contact );
- return;
- }
- linearFriction = linear;
- angularFriction = angular;
- contactFriction = contact;
- }
- /*
- ================
- idAFBody::SetBouncyness
- ================
- */
- void idAFBody::SetBouncyness( float bounce ) {
- if ( bounce < 0.0f || bounce > 1.0f ) {
- gameLocal.Warning( "idAFBody::SetBouncyness: bouncyness out of range, bounce = %.1f", bounce );
- return;
- }
- bouncyness = bounce;
- }
- /*
- ================
- idAFBody::SetDensity
- ================
- */
- void idAFBody::SetDensity( float density, const idMat3 &inertiaScale ) {
- // get the body mass properties
- clipModel->GetMassProperties( density, mass, centerOfMass, inertiaTensor );
- // make sure we have a valid mass
- if ( mass <= 0.0f || FLOAT_IS_NAN( mass ) ) {
- gameLocal.Warning( "idAFBody::SetDensity: invalid mass for body '%s'", name.c_str() );
- mass = 1.0f;
- centerOfMass.Zero();
- inertiaTensor.Identity();
- }
- // make sure the center of mass is at the body origin
- if ( !centerOfMass.Compare( vec3_origin, CENTER_OF_MASS_EPSILON ) ) {
- gameLocal.Warning( "idAFBody::SetDentity: center of mass not at origin for body '%s'", name.c_str() );
- }
- centerOfMass.Zero();
- // calculate the inverse mass and inverse inertia tensor
- invMass = 1.0f / mass;
- if ( inertiaScale != mat3_identity ) {
- inertiaTensor *= inertiaScale;
- }
- if ( inertiaTensor.IsDiagonal( 1e-3f ) ) {
- inertiaTensor[0][1] = inertiaTensor[0][2] = 0.0f;
- inertiaTensor[1][0] = inertiaTensor[1][2] = 0.0f;
- inertiaTensor[2][0] = inertiaTensor[2][1] = 0.0f;
- inverseInertiaTensor.Identity();
- inverseInertiaTensor[0][0] = 1.0f / inertiaTensor[0][0];
- inverseInertiaTensor[1][1] = 1.0f / inertiaTensor[1][1];
- inverseInertiaTensor[2][2] = 1.0f / inertiaTensor[2][2];
- }
- else {
- inverseInertiaTensor = inertiaTensor.Inverse();
- }
- }
- /*
- ================
- idAFBody::SetFrictionDirection
- ================
- */
- void idAFBody::SetFrictionDirection( const idVec3 &dir ) {
- frictionDir = dir * current->worldAxis.Transpose();
- fl.useFrictionDir = true;
- }
- /*
- ================
- idAFBody::GetFrictionDirection
- ================
- */
- bool idAFBody::GetFrictionDirection( idVec3 &dir ) const {
- if ( fl.useFrictionDir ) {
- dir = frictionDir * current->worldAxis;
- return true;
- }
- return false;
- }
- /*
- ================
- idAFBody::SetContactMotorDirection
- ================
- */
- void idAFBody::SetContactMotorDirection( const idVec3 &dir ) {
- contactMotorDir = dir * current->worldAxis.Transpose();
- fl.useContactMotorDir = true;
- }
- /*
- ================
- idAFBody::GetContactMotorDirection
- ================
- */
- bool idAFBody::GetContactMotorDirection( idVec3 &dir ) const {
- if ( fl.useContactMotorDir ) {
- dir = contactMotorDir * current->worldAxis;
- return true;
- }
- return false;
- }
- /*
- ================
- idAFBody::GetPointVelocity
- ================
- */
- idVec3 idAFBody::GetPointVelocity( const idVec3 &point ) const {
- idVec3 r = point - current->worldOrigin;
- return current->spatialVelocity.SubVec3(0) + current->spatialVelocity.SubVec3(1).Cross( r );
- }
- /*
- ================
- idAFBody::AddForce
- ================
- */
- void idAFBody::AddForce( const idVec3 &point, const idVec3 &force ) {
- current->externalForce.SubVec3(0) += force;
- current->externalForce.SubVec3(1) += (point - current->worldOrigin).Cross( force );
- }
- /*
- ================
- idAFBody::InverseWorldSpatialInertiaMultiply
- dst = this->inverseWorldSpatialInertia * v;
- ================
- */
- ID_INLINE void idAFBody::InverseWorldSpatialInertiaMultiply( idVecX &dst, const float *v ) const {
- const float *mPtr = inverseWorldSpatialInertia.ToFloatPtr();
- const float *vPtr = v;
- float *dstPtr = dst.ToFloatPtr();
- if ( fl.spatialInertiaSparse ) {
- dstPtr[0] = mPtr[0*6+0] * vPtr[0];
- dstPtr[1] = mPtr[1*6+1] * vPtr[1];
- dstPtr[2] = mPtr[2*6+2] * vPtr[2];
- dstPtr[3] = mPtr[3*6+3] * vPtr[3] + mPtr[3*6+4] * vPtr[4] + mPtr[3*6+5] * vPtr[5];
- dstPtr[4] = mPtr[4*6+3] * vPtr[3] + mPtr[4*6+4] * vPtr[4] + mPtr[4*6+5] * vPtr[5];
- dstPtr[5] = mPtr[5*6+3] * vPtr[3] + mPtr[5*6+4] * vPtr[4] + mPtr[5*6+5] * vPtr[5];
- } else {
- gameLocal.Warning( "spatial inertia is not sparse for body %s", name.c_str() );
- }
- }
- /*
- ================
- idAFBody::Save
- ================
- */
- void idAFBody::Save( idSaveGame *saveFile ) {
- saveFile->WriteFloat( linearFriction );
- saveFile->WriteFloat( angularFriction );
- saveFile->WriteFloat( contactFriction );
- saveFile->WriteFloat( bouncyness );
- saveFile->WriteInt( clipMask );
- saveFile->WriteVec3( frictionDir );
- saveFile->WriteVec3( contactMotorDir );
- saveFile->WriteFloat( contactMotorVelocity );
- saveFile->WriteFloat( contactMotorForce );
- saveFile->WriteFloat( mass );
- saveFile->WriteFloat( invMass );
- saveFile->WriteVec3( centerOfMass );
- saveFile->WriteMat3( inertiaTensor );
- saveFile->WriteMat3( inverseInertiaTensor );
- saveFile->WriteVec3( current->worldOrigin );
- saveFile->WriteMat3( current->worldAxis );
- saveFile->WriteVec6( current->spatialVelocity );
- saveFile->WriteVec6( current->externalForce );
- saveFile->WriteVec3( atRestOrigin );
- saveFile->WriteMat3( atRestAxis );
- }
- /*
- ================
- idAFBody::Restore
- ================
- */
- void idAFBody::Restore( idRestoreGame *saveFile ) {
- saveFile->ReadFloat( linearFriction );
- saveFile->ReadFloat( angularFriction );
- saveFile->ReadFloat( contactFriction );
- saveFile->ReadFloat( bouncyness );
- saveFile->ReadInt( clipMask );
- saveFile->ReadVec3( frictionDir );
- saveFile->ReadVec3( contactMotorDir );
- saveFile->ReadFloat( contactMotorVelocity );
- saveFile->ReadFloat( contactMotorForce );
- saveFile->ReadFloat( mass );
- saveFile->ReadFloat( invMass );
- saveFile->ReadVec3( centerOfMass );
- saveFile->ReadMat3( inertiaTensor );
- saveFile->ReadMat3( inverseInertiaTensor );
- saveFile->ReadVec3( current->worldOrigin );
- saveFile->ReadMat3( current->worldAxis );
- saveFile->ReadVec6( current->spatialVelocity );
- saveFile->ReadVec6( current->externalForce );
- saveFile->ReadVec3( atRestOrigin );
- saveFile->ReadMat3( atRestAxis );
- }
- //===============================================================
- // M
- // idAFTree MrE
- // E
- //===============================================================
- /*
- ================
- idAFTree::Factor
- factor matrix for the primary constraints in the tree
- ================
- */
- void idAFTree::Factor( void ) const {
- int i, j;
- idAFBody *body;
- idAFConstraint *child;
- idMatX childI;
- childI.SetData( 6, 6, MATX_ALLOCA( 6 * 6 ) );
- // from the leaves up towards the root
- for ( i = sortedBodies.Num() - 1; i >= 0; i-- ) {
- body = sortedBodies[i];
- if ( body->children.Num() ) {
- for ( j = 0; j < body->children.Num(); j++ ) {
- child = body->children[j]->primaryConstraint;
- // child->I = - child->body1->J.Transpose() * child->body1->I * child->body1->J;
- childI.SetSize( child->J1.GetNumRows(), child->J1.GetNumRows() );
- child->body1->J.TransposeMultiply( child->body1->I ).Multiply( childI, child->body1->J );
- childI.Negate();
- child->invI = childI;
- if ( !child->invI.InverseFastSelf() ) {
- gameLocal.Warning( "idAFTree::Factor: couldn't invert %dx%d matrix for constraint '%s'",
- child->invI.GetNumRows(), child->invI.GetNumColumns(), child->GetName().c_str() );
- }
- child->J = child->invI * child->J;
- body->I -= child->J.TransposeMultiply( childI ) * child->J;
- }
- body->invI = body->I;
- if ( !body->invI.InverseFastSelf() ) {
- gameLocal.Warning( "idAFTree::Factor: couldn't invert %dx%d matrix for body %s",
- child->invI.GetNumRows(), child->invI.GetNumColumns(), body->GetName().c_str() );
- }
- if ( body->primaryConstraint ) {
- body->J = body->invI * body->J;
- }
- }
- else if ( body->primaryConstraint ) {
- body->J = body->inverseWorldSpatialInertia * body->J;
- }
- }
- }
- /*
- ================
- idAFTree::Solve
- solve for primary constraints in the tree
- ================
- */
- void idAFTree::Solve( int auxiliaryIndex ) const {
- int i, j;
- idAFBody *body, *child;
- idAFConstraint *primaryConstraint;
- // from the leaves up towards the root
- for ( i = sortedBodies.Num() - 1; i >= 0; i-- ) {
- body = sortedBodies[i];
- for ( j = 0; j < body->children.Num(); j++ ) {
- child = body->children[j];
- primaryConstraint = child->primaryConstraint;
- if ( !child->fl.isZero ) {
- child->J.TransposeMultiplySub( primaryConstraint->s, child->s );
- primaryConstraint->fl.isZero = false;
- }
- if ( !primaryConstraint->fl.isZero ) {
- primaryConstraint->J.TransposeMultiplySub( body->s, primaryConstraint->s );
- body->fl.isZero = false;
- }
- }
- }
- bool useSymmetry = af_useSymmetry.GetBool();
- // from the root down towards the leaves
- for ( i = 0; i < sortedBodies.Num(); i++ ) {
- body = sortedBodies[i];
- primaryConstraint = body->primaryConstraint;
- if ( primaryConstraint ) {
- if ( useSymmetry && body->parent->maxSubTreeAuxiliaryIndex < auxiliaryIndex ) {
- continue;
- }
- if ( !primaryConstraint->fl.isZero ) {
- primaryConstraint->s = primaryConstraint->invI * primaryConstraint->s;
- }
- primaryConstraint->J.MultiplySub( primaryConstraint->s, primaryConstraint->body2->s );
- primaryConstraint->lm = primaryConstraint->s;
- if ( useSymmetry && body->maxSubTreeAuxiliaryIndex < auxiliaryIndex ) {
- continue;
- }
- if ( body->children.Num() ) {
- if ( !body->fl.isZero ) {
- body->s = body->invI * body->s;
- }
- body->J.MultiplySub( body->s, primaryConstraint->s );
- }
- } else if ( body->children.Num() ) {
- body->s = body->invI * body->s;
- }
- }
- }
- /*
- ================
- idAFTree::Response
- calculate body forces in the tree in response to a constraint force
- ================
- */
- void idAFTree::Response( const idAFConstraint *constraint, int row, int auxiliaryIndex ) const {
- int i, j;
- idAFBody *body;
- idAFConstraint *child, *primaryConstraint;
- idVecX v;
- // if a single body don't waste time because there aren't any primary constraints
- if ( sortedBodies.Num() == 1 ) {
- body = constraint->body1;
- if ( body->tree == this ) {
- body->GetResponseForce( body->numResponses ) = constraint->J1.SubVec6( row );
- body->responseIndex[body->numResponses++] = auxiliaryIndex;
- }
- else {
- body = constraint->body2;
- body->GetResponseForce( body->numResponses ) = constraint->J2.SubVec6( row );
- body->responseIndex[body->numResponses++] = auxiliaryIndex;
- }
- return;
- }
- v.SetData( 6, VECX_ALLOCA( 6 ) );
- // initialize right hand side to zero
- for ( i = 0; i < sortedBodies.Num(); i++ ) {
- body = sortedBodies[i];
- primaryConstraint = body->primaryConstraint;
- if ( primaryConstraint ) {
- primaryConstraint->s.Zero();
- primaryConstraint->fl.isZero = true;
- }
- body->s.Zero();
- body->fl.isZero = true;
- body->GetResponseForce( body->numResponses ).Zero();
- }
- // set right hand side for first constrained body
- body = constraint->body1;
- if ( body->tree == this ) {
- body->InverseWorldSpatialInertiaMultiply( v, constraint->J1[row] );
- primaryConstraint = body->primaryConstraint;
- if ( primaryConstraint ) {
- primaryConstraint->J1.Multiply( primaryConstraint->s, v );
- primaryConstraint->fl.isZero = false;
- }
- for ( i = 0; i < body->children.Num(); i++ ) {
- child = body->children[i]->primaryConstraint;
- child->J2.Multiply( child->s, v );
- child->fl.isZero = false;
- }
- body->GetResponseForce( body->numResponses ) = constraint->J1.SubVec6( row );
- }
- // set right hand side for second constrained body
- body = constraint->body2;
- if ( body && body->tree == this ) {
- body->InverseWorldSpatialInertiaMultiply( v, constraint->J2[row] );
- primaryConstraint = body->primaryConstraint;
- if ( primaryConstraint ) {
- primaryConstraint->J1.MultiplyAdd( primaryConstraint->s, v );
- primaryConstraint->fl.isZero = false;
- }
- for ( i = 0; i < body->children.Num(); i++ ) {
- child = body->children[i]->primaryConstraint;
- child->J2.MultiplyAdd( child->s, v );
- child->fl.isZero = false;
- }
- body->GetResponseForce( body->numResponses ) = constraint->J2.SubVec6( row );
- }
- // solve for primary constraints
- Solve( auxiliaryIndex );
- bool useSymmetry = af_useSymmetry.GetBool();
- // store body forces in response to the constraint force
- idVecX force;
- for ( i = 0; i < sortedBodies.Num(); i++ ) {
- body = sortedBodies[i];
- if ( useSymmetry && body->maxAuxiliaryIndex < auxiliaryIndex ) {
- continue;
- }
- force.SetData( 6, body->response + body->numResponses * 8 );
- // add forces of all primary constraints acting on this body
- primaryConstraint = body->primaryConstraint;
- if ( primaryConstraint ) {
- primaryConstraint->J1.TransposeMultiplyAdd( force, primaryConstraint->lm );
- }
- for ( j = 0; j < body->children.Num(); j++ ) {
- child = body->children[j]->primaryConstraint;
- child->J2.TransposeMultiplyAdd( force, child->lm );
- }
- body->responseIndex[body->numResponses++] = auxiliaryIndex;
- }
- }
- /*
- ================
- idAFTree::CalculateForces
- calculate forces on the bodies in the tree
- ================
- */
- void idAFTree::CalculateForces( float timeStep ) const {
- int i, j;
- float invStep;
- idAFBody *body;
- idAFConstraint *child, *c, *primaryConstraint;
- // forces on bodies
- for ( i = 0; i < sortedBodies.Num(); i++ ) {
- body = sortedBodies[i];
- body->totalForce.SubVec6(0) = body->current->externalForce + body->auxForce.SubVec6(0);
- }
- // if a single body don't waste time because there aren't any primary constraints
- if ( sortedBodies.Num() == 1 ) {
- return;
- }
- invStep = 1.0f / timeStep;
- // initialize right hand side
- for ( i = 0; i < sortedBodies.Num(); i++ ) {
- body = sortedBodies[i];
- body->InverseWorldSpatialInertiaMultiply( body->acceleration, body->totalForce.ToFloatPtr() );
- body->acceleration.SubVec6(0) += body->current->spatialVelocity * invStep;
- primaryConstraint = body->primaryConstraint;
- if ( primaryConstraint ) {
- // b = ( J * acc + c )
- c = primaryConstraint;
- c->s = c->J1 * c->body1->acceleration + c->J2 * c->body2->acceleration + invStep * ( c->c1 + c->c2 );
- c->fl.isZero = false;
- }
- body->s.Zero();
- body->fl.isZero = true;
- }
- // solve for primary constraints
- Solve();
- // calculate forces on bodies after applying primary constraints
- for ( i = 0; i < sortedBodies.Num(); i++ ) {
- body = sortedBodies[i];
- // add forces of all primary constraints acting on this body
- primaryConstraint = body->primaryConstraint;
- if ( primaryConstraint ) {
- primaryConstraint->J1.TransposeMultiplyAdd( body->totalForce, primaryConstraint->lm );
- }
- for ( j = 0; j < body->children.Num(); j++ ) {
- child = body->children[j]->primaryConstraint;
- child->J2.TransposeMultiplyAdd( body->totalForce, child->lm );
- }
- }
- }
- /*
- ================
- idAFTree::SetMaxSubTreeAuxiliaryIndex
- ================
- */
- void idAFTree::SetMaxSubTreeAuxiliaryIndex( void ) {
- int i, j;
- idAFBody *body, *child;
- // from the leaves up towards the root
- for ( i = sortedBodies.Num() - 1; i >= 0; i-- ) {
- body = sortedBodies[i];
- body->maxSubTreeAuxiliaryIndex = body->maxAuxiliaryIndex;
- for ( j = 0; j < body->children.Num(); j++ ) {
- child = body->children[j];
- if ( child->maxSubTreeAuxiliaryIndex > body->maxSubTreeAuxiliaryIndex ) {
- body->maxSubTreeAuxiliaryIndex = child->maxSubTreeAuxiliaryIndex;
- }
- }
- }
- }
- /*
- ================
- idAFTree::SortBodies_r
- ================
- */
- void idAFTree::SortBodies_r( idList<idAFBody*>&sortedList, idAFBody *body ) {
- int i;
- for ( i = 0; i < body->children.Num(); i++ ) {
- sortedList.Append( body->children[i] );
- }
- for ( i = 0; i < body->children.Num(); i++ ) {
- SortBodies_r( sortedList, body->children[i] );
- }
- }
- /*
- ================
- idAFTree::SortBodies
- sort body list to make sure parents come first
- ================
- */
- void idAFTree::SortBodies( void ) {
- int i;
- idAFBody *body;
- // find the root
- for ( i = 0; i < sortedBodies.Num(); i++ ) {
- if ( !sortedBodies[i]->parent ) {
- break;
- }
- }
- if ( i >= sortedBodies.Num() ) {
- gameLocal.Error( "Articulated figure tree has no root." );
- }
- body = sortedBodies[i];
- sortedBodies.Clear();
- sortedBodies.Append( body );
- SortBodies_r( sortedBodies, body );
- }
- /*
- ================
- idAFTree::DebugDraw
- ================
- */
- void idAFTree::DebugDraw( const idVec4 &color ) const {
- int i;
- idAFBody *body;
- for ( i = 1; i < sortedBodies.Num(); i++ ) {
- body = sortedBodies[i];
- gameRenderWorld->DebugArrow( color, body->parent->current->worldOrigin, body->current->worldOrigin, 1 );
- }
- }
- //===============================================================
- // M
- // idPhysics_AF MrE
- // E
- //===============================================================
- /*
- ================
- idPhysics_AF::EvaluateConstraints
- ================
- */
- void idPhysics_AF::EvaluateConstraints( float timeStep ) {
- int i;
- float invTimeStep;
- idAFBody *body;
- idAFConstraint *c;
- invTimeStep = 1.0f / timeStep;
- // setup the constraint equations for the current position and orientation of the bodies
- for ( i = 0; i < primaryConstraints.Num(); i++ ) {
- c = primaryConstraints[i];
- c->Evaluate( invTimeStep );
- c->J = c->J2;
- }
- for ( i = 0; i < auxiliaryConstraints.Num(); i++ ) {
- auxiliaryConstraints[i]->Evaluate( invTimeStep );
- }
- // add contact constraints to the list with frame constraints
- for ( i = 0; i < contactConstraints.Num(); i++ ) {
- AddFrameConstraint( contactConstraints[i] );
- }
- // setup body primary constraint matrix
- for ( i = 0; i < bodies.Num(); i++ ) {
- body = bodies[i];
- if ( body->primaryConstraint ) {
- body->J = body->primaryConstraint->J1.Transpose();
- }
- }
- }
- /*
- ================
- idPhysics_AF::EvaluateBodies
- ================
- */
- void idPhysics_AF::EvaluateBodies( float timeStep ) {
- int i;
- idAFBody *body;
- idMat3 axis;
- for ( i = 0; i < bodies.Num(); i++ ) {
- body = bodies[i];
- // we transpose the axis before using it because idMat3 is column-major
- axis = body->current->worldAxis.Transpose();
- // if the center of mass is at the body point of reference
- if ( body->centerOfMass.Compare( vec3_origin, CENTER_OF_MASS_EPSILON ) ) {
- // spatial inertia in world space
- body->I.Set( body->mass * mat3_identity, mat3_zero,
- mat3_zero, axis * body->inertiaTensor * axis.Transpose() );
- // inverse spatial inertia in world space
- body->inverseWorldSpatialInertia.Set( body->invMass * mat3_identity, mat3_zero,
- mat3_zero, axis * body->inverseInertiaTensor * axis.Transpose() );
- body->fl.spatialInertiaSparse = true;
- }
- else {
- idMat3 massMoment = body->mass * SkewSymmetric( body->centerOfMass );
- // spatial inertia in world space
- body->I.Set( body->mass * mat3_identity, massMoment,
- massMoment.Transpose(), axis * body->inertiaTensor * axis.Transpose() );
- // inverse spatial inertia in world space
- body->inverseWorldSpatialInertia = body->I.InverseFast();
- body->fl.spatialInertiaSparse = false;
- }
- // initialize auxiliary constraint force to zero
- body->auxForce.Zero();
- }
- }
- /*
- ================
- idPhysics_AF::AddFrameConstraints
- ================
- */
- void idPhysics_AF::AddFrameConstraints( void ) {
- int i;
- // add frame constraints to auxiliary constraints
- for ( i = 0; i < frameConstraints.Num(); i++ ) {
- auxiliaryConstraints.Append( frameConstraints[i] );
- }
- }
- /*
- ================
- idPhysics_AF::RemoveFrameConstraints
- ================
- */
- void idPhysics_AF::RemoveFrameConstraints( void ) {
- // remove all the frame constraints from the auxiliary constraints
- auxiliaryConstraints.SetNum( auxiliaryConstraints.Num() - frameConstraints.Num(), false );
- frameConstraints.SetNum( 0, false );
- }
- /*
- ================
- idPhysics_AF::ApplyFriction
- ================
- */
- void idPhysics_AF::ApplyFriction( float timeStep, float endTimeMSec ) {
- int i;
- float invTimeStep;
- if ( af_skipFriction.GetBool() ) {
- return;
- }
- if ( jointFrictionDentStart < MS2SEC( endTimeMSec ) && jointFrictionDentEnd > MS2SEC( endTimeMSec ) ) {
- float halfTime = ( jointFrictionDentEnd - jointFrictionDentStart ) * 0.5f;
- if ( jointFrictionDentStart + halfTime > MS2SEC( endTimeMSec ) ) {
- jointFrictionDentScale = 1.0f - ( 1.0f - jointFrictionDent ) * ( MS2SEC( endTimeMSec ) - jointFrictionDentStart ) / halfTime;
- } else {
- jointFrictionDentScale = jointFrictionDent + ( 1.0f - jointFrictionDent ) * ( MS2SEC( endTimeMSec ) - jointFrictionDentStart - halfTime ) / halfTime;
- }
- } else {
- jointFrictionDentScale = 0.0f;
- }
- if ( contactFrictionDentStart < MS2SEC( endTimeMSec ) && contactFrictionDentEnd > MS2SEC( endTimeMSec ) ) {
- float halfTime = ( contactFrictionDentEnd - contactFrictionDentStart ) * 0.5f;
- if ( contactFrictionDentStart + halfTime > MS2SEC( endTimeMSec ) ) {
- contactFrictionDentScale = 1.0f - ( 1.0f - contactFrictionDent ) * ( MS2SEC( endTimeMSec ) - contactFrictionDentStart ) / halfTime;
- } else {
- contactFrictionDentScale = contactFrictionDent + ( 1.0f - contactFrictionDent ) * ( MS2SEC( endTimeMSec ) - contactFrictionDentStart - halfTime ) / halfTime;
- }
- } else {
- contactFrictionDentScale = 0.0f;
- }
- invTimeStep = 1.0f / timeStep;
- for ( i = 0; i < primaryConstraints.Num(); i++ ) {
- primaryConstraints[i]->ApplyFriction( invTimeStep );
- }
- for ( i = 0; i < auxiliaryConstraints.Num(); i++ ) {
- auxiliaryConstraints[i]->ApplyFriction( invTimeStep );
- }
- for ( i = 0; i < frameConstraints.Num(); i++ ) {
- frameConstraints[i]->ApplyFriction( invTimeStep );
- }
- }
- /*
- ================
- idPhysics_AF::PrimaryFactor
- ================
- */
- void idPhysics_AF::PrimaryFactor( void ) {
- int i;
- for ( i = 0; i < trees.Num(); i++ ) {
- trees[i]->Factor();
- }
- }
- /*
- ================
- idPhysics_AF::PrimaryForces
- ================
- */
- void idPhysics_AF::PrimaryForces( float timeStep ) {
- int i;
- for ( i = 0; i < trees.Num(); i++ ) {
- trees[i]->CalculateForces( timeStep );
- }
- }
- /*
- ================
- idPhysics_AF::AuxiliaryForces
- ================
- */
- void idPhysics_AF::AuxiliaryForces( float timeStep ) {
- int i, j, k, l, n, m, s, numAuxConstraints, *index, *boxIndex;
- float *ptr, *j1, *j2, *dstPtr, *forcePtr;
- float invStep, u;
- idAFBody *body;
- idAFConstraint *constraint;
- idVecX tmp;
- idMatX jmk;
- idVecX rhs, w, lm, lo, hi;
- // get the number of one dimensional auxiliary constraints
- for ( numAuxConstraints = 0, i = 0; i < auxiliaryConstraints.Num(); i++ ) {
- numAuxConstraints += auxiliaryConstraints[i]->J1.GetNumRows();
- }
- if ( numAuxConstraints == 0 ) {
- return;
- }
- // allocate memory to store the body response to auxiliary constraint forces
- forcePtr = (float *) _alloca16( bodies.Num() * numAuxConstraints * 8 * sizeof( float ) );
- index = (int *) _alloca16( bodies.Num() * numAuxConstraints * sizeof( int ) );
- for ( i = 0; i < bodies.Num(); i++ ) {
- body = bodies[i];
- body->response = forcePtr;
- body->responseIndex = index;
- body->numResponses = 0;
- body->maxAuxiliaryIndex = 0;
- forcePtr += numAuxConstraints * 8;
- index += numAuxConstraints;
- }
- // set on each body the largest index of an auxiliary constraint constraining the body
- if ( af_useSymmetry.GetBool() ) {
- for ( k = 0, i = 0; i < auxiliaryConstraints.Num(); i++ ) {
- constraint = auxiliaryConstraints[i];
- for ( j = 0; j < constraint->J1.GetNumRows(); j++, k++ ) {
- if ( k > constraint->body1->maxAuxiliaryIndex ) {
- constraint->body1->maxAuxiliaryIndex = k;
- }
- if ( constraint->body2 && k > constraint->body2->maxAuxiliaryIndex ) {
- constraint->body2->maxAuxiliaryIndex = k;
- }
- }
- }
- for ( i = 0; i < trees.Num(); i++ ) {
- trees[i]->SetMaxSubTreeAuxiliaryIndex();
- }
- }
- // calculate forces of primary constraints in response to the auxiliary constraint forces
- for ( k = 0, i = 0; i < auxiliaryConstraints.Num(); i++ ) {
- constraint = auxiliaryConstraints[i];
- for ( j = 0; j < constraint->J1.GetNumRows(); j++, k++ ) {
- // calculate body forces in the tree in response to the constraint force
- constraint->body1->tree->Response( constraint, j, k );
- // if there is a second body which is part of a different tree
- if ( constraint->body2 && constraint->body2->tree != constraint->body1->tree ) {
- // calculate body forces in the second tree in response to the constraint force
- constraint->body2->tree->Response( constraint, j, k );
- }
- }
- }
- // NOTE: the rows are 16 byte padded
- jmk.SetData( numAuxConstraints, ((numAuxConstraints+3)&~3), MATX_ALLOCA( numAuxConstraints * ((numAuxConstraints+3)&~3) ) );
- tmp.SetData( 6, VECX_ALLOCA( 6 ) );
- // create constraint matrix for auxiliary constraints using a mass matrix adjusted for the primary constraints
- for ( k = 0, i = 0; i < auxiliaryConstraints.Num(); i++ ) {
- constraint = auxiliaryConstraints[i];
- for ( j = 0; j < constraint->J1.GetNumRows(); j++, k++ ) {
- constraint->body1->InverseWorldSpatialInertiaMultiply( tmp, constraint->J1[j] );
- j1 = tmp.ToFloatPtr();
- ptr = constraint->body1->response;
- index = constraint->body1->responseIndex;
- dstPtr = jmk[k];
- s = af_useSymmetry.GetBool() ? k + 1 : numAuxConstraints;
- for ( l = n = 0, m = index[n]; n < constraint->body1->numResponses && m < s; n++, m = index[n] ) {
- while( l < m ) {
- dstPtr[l++] = 0.0f;
- }
- dstPtr[l++] = j1[0] * ptr[0] + j1[1] * ptr[1] + j1[2] * ptr[2] +
- j1[3] * ptr[3] + j1[4] * ptr[4] + j1[5] * ptr[5];
- ptr += 8;
- }
- while( l < s ) {
- dstPtr[l++] = 0.0f;
- }
- if ( constraint->body2 ) {
- constraint->body2->InverseWorldSpatialInertiaMultiply( tmp, constraint->J2[j] );
- j2 = tmp.ToFloatPtr();
- ptr = constraint->body2->response;
- index = constraint->body2->responseIndex;
- for ( n = 0, m = index[n]; n < constraint->body2->numResponses && m < s; n++, m = index[n] ) {
- dstPtr[m] += j2[0] * ptr[0] + j2[1] * ptr[1] + j2[2] * ptr[2] +
- j2[3] * ptr[3] + j2[4] * ptr[4] + j2[5] * ptr[5];
- ptr += 8;
- }
- }
- }
- }
- if ( af_useSymmetry.GetBool() ) {
- n = jmk.GetNumColumns();
- for ( i = 0; i < numAuxConstraints; i++ ) {
- ptr = jmk.ToFloatPtr() + ( i + 1 ) * n + i;
- dstPtr = jmk.ToFloatPtr() + i * n + i + 1;
- for ( j = i+1; j < numAuxConstraints; j++ ) {
- *dstPtr++ = *ptr;
- ptr += n;
- }
- }
- }
- invStep = 1.0f / timeStep;
- // calculate body acceleration
- for ( i = 0; i < bodies.Num(); i++ ) {
- body = bodies[i];
- body->InverseWorldSpatialInertiaMultiply( body->acceleration, body->totalForce.ToFloatPtr() );
- body->acceleration.SubVec6(0) += body->current->spatialVelocity * invStep;
- }
- rhs.SetData( numAuxConstraints, VECX_ALLOCA( numAuxConstraints ) );
- lo.SetData( numAuxConstraints, VECX_ALLOCA( numAuxConstraints ) );
- hi.SetData( numAuxConstraints, VECX_ALLOCA( numAuxConstraints ) );
- lm.SetData( numAuxConstraints, VECX_ALLOCA( numAuxConstraints ) );
- boxIndex = (int *) _alloca16( numAuxConstraints * sizeof( int ) );
- // set first index for special box constrained variables
- for ( k = 0, i = 0; i < auxiliaryConstraints.Num(); i++ ) {
- auxiliaryConstraints[i]->firstIndex = k;
- k += auxiliaryConstraints[i]->J1.GetNumRows();
- }
- // initialize right hand side and low and high bounds for auxiliary constraints
- for ( k = 0, i = 0; i < auxiliaryConstraints.Num(); i++ ) {
- constraint = auxiliaryConstraints[i];
- n = k;
- for ( j = 0; j < constraint->J1.GetNumRows(); j++, k++ ) {
- j1 = constraint->J1[j];
- ptr = constraint->body1->acceleration.ToFloatPtr();
- rhs[k] = j1[0] * ptr[0] + j1[1] * ptr[1] + j1[2] * ptr[2] + j1[3] * ptr[3] + j1[4] * ptr[4] + j1[5] * ptr[5];
- rhs[k] += constraint->c1[j] * invStep;
- if ( constraint->body2 ) {
- j2 = constraint->J2[j];
- ptr = constraint->body2->acceleration.ToFloatPtr();
- rhs[k] += j2[0] * ptr[0] + j2[1] * ptr[1] + j2[2] * ptr[2] + j2[3] * ptr[3] + j2[4] * ptr[4] + j2[5] * ptr[5];
- rhs[k] += constraint->c2[j] * invStep;
- }
- rhs[k] = -rhs[k];
- lo[k] = constraint->lo[j];
- hi[k] = constraint->hi[j];
- if ( constraint->boxIndex[j] >= 0 ) {
- if ( constraint->boxConstraint->fl.isPrimary ) {
- gameLocal.Error( "cannot reference primary constraints for the box index" );
- }
- boxIndex[k] = constraint->boxConstraint->firstIndex + constraint->boxIndex[j];
- }
- else {
- boxIndex[k] = -1;
- }
- jmk[k][k] += constraint->e[j] * invStep;
- }
- }
- #ifdef AF_TIMINGS
- timer_lcp.Start();
- #endif
- // calculate lagrange multipliers for auxiliary constraints
- if ( !lcp->Solve( jmk, lm, rhs, lo, hi, boxIndex ) ) {
- return; // bad monkey!
- }
- #ifdef AF_TIMINGS
- timer_lcp.Stop();
- #endif
- // calculate auxiliary constraint forces
- for ( k = 0, i = 0; i < auxiliaryConstraints.Num(); i++ ) {
- constraint = auxiliaryConstraints[i];
- for ( j = 0; j < constraint->J1.GetNumRows(); j++, k++ ) {
- constraint->lm[j] = u = lm[k];
- j1 = constraint->J1[j];
- ptr = constraint->body1->auxForce.ToFloatPtr();
- ptr[0] += j1[0] * u; ptr[1] += j1[1] * u; ptr[2] += j1[2] * u;
- ptr[3] += j1[3] * u; ptr[4] += j1[4] * u; ptr[5] += j1[5] * u;
- if ( constraint->body2 ) {
- j2 = constraint->J2[j];
- ptr = constraint->body2->auxForce.ToFloatPtr();
- ptr[0] += j2[0] * u; ptr[1] += j2[1] * u; ptr[2] += j2[2] * u;
- ptr[3] += j2[3] * u; ptr[4] += j2[4] * u; ptr[5] += j2[5] * u;
- }
- }
- }
- // recalculate primary constraint forces in response to auxiliary constraint forces
- PrimaryForces( timeStep );
- // clear pointers pointing to stack space so tools don't get confused
- for ( i = 0; i < bodies.Num(); i++ ) {
- body = bodies[i];
- body->response = NULL;
- body->responseIndex = NULL;
- }
- }
- /*
- ================
- idPhysics_AF::VerifyContactConstraints
- ================
- */
- void idPhysics_AF::VerifyContactConstraints( void ) {
- #if 0
- int i;
- float impulseNumerator, impulseDenominator;
- idVec3 r, velocity, normalVelocity, normal, impulse;
- idAFBody *body;
- for ( i = 0; i < contactConstraints.Num(); i++ ) {
- body = contactConstraints[i]->body1;
- const contactInfo_t &contact = contactConstraints[i]->GetContact();
- r = contact.point - body->GetCenterOfMass();
- // calculate velocity at contact point
- velocity = body->GetLinearVelocity() + body->GetAngularVelocity().Cross( r );
- // velocity along normal vector
- normalVelocity = ( velocity * contact.normal ) * contact.normal;
- // if moving towards the surface at the contact point
- if ( normalVelocity * contact.normal < 0.0f ) {
- // calculate impulse
- normal = -normalVelocity;
- impulseNumerator = normal.Normalize();
- impulseDenominator = body->GetInverseMass() + ( ( body->GetInverseWorldInertia() * r.Cross( normal ) ).Cross( r ) * normal );
- impulse = (impulseNumerator / impulseDenominator) * normal * 1.0001f;
- // apply impulse
- body->SetLinearVelocity( body->GetLinearVelocity() + impulse );
- body->SetAngularVelocity( body->GetAngularVelocity() + r.Cross( impulse ) );
- }
- }
- #else
- int i;
- idAFBody *body;
- idVec3 normal;
- for ( i = 0; i < contactConstraints.Num(); i++ ) {
- body = contactConstraints[i]->body1;
- normal = contactConstraints[i]->GetContact().normal;
- if ( normal * body->next->spatialVelocity.SubVec3(0) <= 0.0f ) {
- body->next->spatialVelocity.SubVec3(0) -= 1.0001f * (normal * body->next->spatialVelocity.SubVec3(0)) * normal;
- }
- body = contactConstraints[i]->body2;
- if ( !body ) {
- continue;
- }
- normal = -normal;
- if ( normal * body->next->spatialVelocity.SubVec3(0) <= 0.0f ) {
- body->next->spatialVelocity.SubVec3(0) -= 1.0001f * (normal * body->next->spatialVelocity.SubVec3(0)) * normal;
- }
- }
- #endif
- }
- /*
- ================
- idPhysics_AF::Evolve
- ================
- */
- void idPhysics_AF::Evolve( float timeStep ) {
- int i;
- float angle;
- idVec3 vec;
- idAFBody *body;
- idVec6 force;
- idRotation rotation;
- float vSqr, maxLinearVelocity, maxAngularVelocity;
-
- maxLinearVelocity = af_maxLinearVelocity.GetFloat() / timeStep;
- maxAngularVelocity = af_maxAngularVelocity.GetFloat() / timeStep;
- for ( i = 0; i < bodies.Num(); i++ ) {
- body = bodies[i];
- // calculate the spatial velocity for the next physics state
- body->InverseWorldSpatialInertiaMultiply( body->acceleration, body->totalForce.ToFloatPtr() );
- body->next->spatialVelocity = body->current->spatialVelocity + timeStep * body->acceleration.SubVec6(0);
- if ( maxLinearVelocity > 0.0f ) {
- // cap the linear velocity
- vSqr = body->next->spatialVelocity.SubVec3(0).LengthSqr();
- if ( vSqr > Square( maxLinearVelocity ) ) {
- body->next->spatialVelocity.SubVec3(0) *= idMath::InvSqrt( vSqr ) * maxLinearVelocity;
- }
- }
- if ( maxAngularVelocity > 0.0f ) {
- // cap the angular velocity
- vSqr = body->next->spatialVelocity.SubVec3(1).LengthSqr();
- if ( vSqr > Square( maxAngularVelocity ) ) {
- body->next->spatialVelocity.SubVec3(1) *= idMath::InvSqrt( vSqr ) * maxAngularVelocity;
- }
- }
- }
- // make absolutely sure all contact constraints are satisfied
- VerifyContactConstraints();
- // calculate the position of the bodies for the next physics state
- for ( i = 0; i < bodies.Num(); i++ ) {
- body = bodies[i];
- // translate world origin
- body->next->worldOrigin = body->current->worldOrigin + timeStep * body->next->spatialVelocity.SubVec3( 0 );
- // convert angular velocity to a rotation matrix
- vec = body->next->spatialVelocity.SubVec3( 1 );
- angle = -timeStep * (float) RAD2DEG( vec.Normalize() );
- rotation = idRotation( vec3_origin, vec, angle );
- rotation.Normalize180();
- // rotate world axis
- body->next->worldAxis = body->current->worldAxis * rotation.ToMat3();
- body->next->worldAxis.OrthoNormalizeSelf();
- // linear and angular friction
- body->next->spatialVelocity.SubVec3(0) -= body->linearFriction * body->next->spatialVelocity.SubVec3(0);
- body->next->spatialVelocity.SubVec3(1) -= body->angularFriction * body->next->spatialVelocity.SubVec3(1);
- }
- }
- /*
- ================
- idPhysics_AF::CollisionImpulse
- apply impulse to the colliding bodies
- the current state of the body should be set to the moment of impact
- this is silly as it doesn't take the AF structure into account
- ================
- */
- bool idPhysics_AF::CollisionImpulse( float timeStep, idAFBody *body, trace_t &collision ) {
- idVec3 r, velocity, impulse;
- idMat3 inverseWorldInertiaTensor;
- float impulseNumerator, impulseDenominator;
- impactInfo_t info;
- idEntity *ent;
- ent = gameLocal.entities[collision.c.entityNum];
- if ( ent == self ) {
- return false;
- }
- // get info from other entity involved
- ent->GetImpactInfo( self, collision.c.id, collision.c.point, &info );
- // collision point relative to the body center of mass
- r = collision.c.point - (body->current->worldOrigin + body->centerOfMass * body->current->worldAxis);
- // the velocity at the collision point
- velocity = body->current->spatialVelocity.SubVec3(0) + body->current->spatialVelocity.SubVec3(1).Cross(r);
- // subtract velocity of other entity
- velocity -= info.velocity;
- // never stick
- if ( velocity * collision.c.normal > 0.0f ) {
- velocity = collision.c.normal;
- }
- inverseWorldInertiaTensor = body->current->worldAxis.Transpose() * body->inverseInertiaTensor * body->current->worldAxis;
- impulseNumerator = -( 1.0f + body->bouncyness ) * ( velocity * collision.c.normal );
- impulseDenominator = body->invMass + ( ( inverseWorldInertiaTensor * r.Cross( collision.c.normal ) ).Cross( r ) * collision.c.normal );
- if ( info.invMass ) {
- impulseDenominator += info.invMass + ( ( info.invInertiaTensor * info.position.Cross( collision.c.normal ) ).Cross( info.position ) * collision.c.normal );
- }
- impulse = (impulseNumerator / impulseDenominator) * collision.c.normal;
- // apply impact to other entity
- ent->ApplyImpulse( self, collision.c.id, collision.c.point, -impulse );
- // callback to self to let the entity know about the impact
- return self->Collide( collision, velocity );
- }
- /*
- ================
- idPhysics_AF::ApplyCollisions
- ================
- */
- bool idPhysics_AF::ApplyCollisions( float timeStep ) {
- int i;
- for ( i = 0; i < collisions.Num(); i++ ) {
- if ( CollisionImpulse( timeStep, collisions[i].body, collisions[i].trace ) ) {
- return true;
- }
- }
- return false;
- }
- /*
- ================
- idPhysics_AF::SetupCollisionForBody
- ================
- */
- idEntity *idPhysics_AF::SetupCollisionForBody( idAFBody *body ) const {
- int i;
- idAFBody *b;
- idEntity *passEntity;
- passEntity = NULL;
- if ( !selfCollision || !body->fl.selfCollision || af_skipSelfCollision.GetBool() ) {
- // disable all bodies
- for ( i = 0; i < bodies.Num(); i++ ) {
- bodies[i]->clipModel->Disable();
- }
- // don't collide with world collision model if attached to the world
- for ( i = 0; i < body->constraints.Num(); i++ ) {
- if ( !body->constraints[i]->fl.noCollision ) {
- continue;
- }
- // if this constraint attaches the body to the world
- if ( body->constraints[i]->body2 == NULL ) {
- // don't collide with the world collision model
- passEntity = gameLocal.world;
- }
- }
- } else {
- // enable all bodies that have self collision
- for ( i = 0; i < bodies.Num(); i++ ) {
- if ( bodies[i]->fl.selfCollision ) {
- bodies[i]->clipModel->Enable();
- } else {
- bodies[i]->clipModel->Disable();
- }
- }
- // don't let the body collide with itself
- body->clipModel->Disable();
- // disable any bodies attached with constraints
- for ( i = 0; i < body->constraints.Num(); i++ ) {
- if ( !body->constraints[i]->fl.noCollision ) {
- continue;
- }
- // if this constraint attaches the body to the world
- if ( body->constraints[i]->body2 == NULL ) {
- // don't collide with the world collision model
- passEntity = gameLocal.world;
- } else {
- if ( body->constraints[i]->body1 == body ) {
- b = body->constraints[i]->body2;
- } else if ( body->constraints[i]->body2 == body ) {
- b = body->constraints[i]->body1;
- } else {
- continue;
- }
- // don't collide with this body
- b->clipModel->Disable();
- }
- }
- }
- return passEntity;
- }
- /*
- ================
- idPhysics_AF::CheckForCollisions
- check for collisions between the current and next state
- if there is a collision the next state is set to the state at the moment of impact
- assumes all bodies are linked for collision detection and relinks all bodies after moving them
- ================
- */
- void idPhysics_AF::CheckForCollisions( float timeStep ) {
- // #define TEST_COLLISION_DETECTION
- int i, index;
- idAFBody *body;
- idMat3 axis;
- idRotation rotation;
- trace_t collision;
- idEntity *passEntity;
- // clear list with collisions
- collisions.SetNum( 0, false );
- if ( !enableCollision ) {
- return;
- }
- for ( i = 0; i < bodies.Num(); i++ ) {
- body = bodies[i];
- if ( body->clipMask != 0 ) {
- passEntity = SetupCollisionForBody( body );
- #ifdef TEST_COLLISION_DETECTION
- bool startsolid = false;
- if ( gameLocal.clip.Contents( body->current->worldOrigin, body->clipModel,
- body->current->worldAxis, body->clipMask, passEntity ) ) {
- startsolid = true;
- }
- #endif
- TransposeMultiply( body->current->worldAxis, body->next->worldAxis, axis );
- rotation = axis.ToRotation();
- rotation.SetOrigin( body->current->worldOrigin );
- // if there was a collision
- if ( gameLocal.clip.Motion( collision, body->current->worldOrigin, body->next->worldOrigin, rotation,
- body->clipModel, body->current->worldAxis, body->clipMask, passEntity ) ) {
- // set the next state to the state at the moment of impact
- body->next->worldOrigin = collision.endpos;
- body->next->worldAxis = collision.endAxis;
- // add collision to the list
- index = collisions.Num();
- collisions.SetNum( index + 1, false );
- collisions[index].trace = collision;
- collisions[index].body = body;
- }
- #ifdef TEST_COLLISION_DETECTION
- if ( gameLocal.clip.Contents( body->next->worldOrigin, body->clipModel,
- body->next->worldAxis, body->clipMask, passEntity ) ) {
- if ( !startsolid ) {
- int bah = 1;
- }
- }
- #endif
- }
- body->clipModel->Link( gameLocal.clip, self, body->clipModel->GetId(), body->next->worldOrigin, body->next->worldAxis );
- }
- }
- /*
- ================
- idPhysics_AF::EvaluateContacts
- ================
- */
- bool idPhysics_AF::EvaluateContacts( void ) {
- int i, j, k, numContacts, numBodyContacts;
- idAFBody *body;
- contactInfo_t contactInfo[10];
- idEntity *passEntity;
- idVecX dir( 6, VECX_ALLOCA( 6 ) );
- // evaluate bodies
- EvaluateBodies( current.lastTimeStep );
- // remove all existing contacts
- ClearContacts();
- contactBodies.SetNum( 0, false );
- if ( !enableCollision ) {
- return false;
- }
- // find all the contacts
- for ( i = 0; i < bodies.Num(); i++ ) {
- body = bodies[i];
- if ( body->clipMask == 0 ) {
- continue;
- }
- passEntity = SetupCollisionForBody( body );
- body->InverseWorldSpatialInertiaMultiply( dir, body->current->externalForce.ToFloatPtr() );
- dir.SubVec6(0) = body->current->spatialVelocity + current.lastTimeStep * dir.SubVec6(0);
- dir.SubVec3(0).Normalize();
- dir.SubVec3(1).Normalize();
- numContacts = gameLocal.clip.Contacts( contactInfo, 10, body->current->worldOrigin, dir.SubVec6(0), 2.0f, //CONTACT_EPSILON,
- body->clipModel, body->current->worldAxis, body->clipMask, passEntity );
- #if 1
- // merge nearby contacts between the same bodies
- // and assure there are at most three planar contacts between any pair of bodies
- for ( j = 0; j < numContacts; j++ ) {
- numBodyContacts = 0;
- for ( k = 0; k < contacts.Num(); k++ ) {
- if ( contacts[k].entityNum == contactInfo[j].entityNum ) {
- if ( ( contacts[k].id == i && contactInfo[j].id == contactBodies[k] ) ||
- ( contactBodies[k] == i && contacts[k].id == contactInfo[j].id ) ) {
- if ( ( contacts[k].point - contactInfo[j].point ).LengthSqr() < Square( 2.0f ) ) {
- break;
- }
- if ( idMath::Fabs( contacts[k].normal * contactInfo[j].normal ) > 0.9f ) {
- numBodyContacts++;
- }
- }
- }
- }
- if ( k >= contacts.Num() && numBodyContacts < 3 ) {
- contacts.Append( contactInfo[j] );
- contactBodies.Append( i );
- }
- }
- #else
- for ( j = 0; j < numContacts; j++ ) {
- contacts.Append( contactInfo[j] );
- contactBodies.Append( i );
- }
- #endif
- }
- AddContactEntitiesForContacts();
- return ( contacts.Num() != 0 );
- }
- /*
- ================
- idPhysics_AF::SetupContactConstraints
- ================
- */
- void idPhysics_AF::SetupContactConstraints( void ) {
- int i;
- // make sure enough contact constraints are allocated
- contactConstraints.AssureSizeAlloc( contacts.Num(), idListNewElement<idAFConstraint_Contact> );
- contactConstraints.SetNum( contacts.Num(), false );
- // setup contact constraints
- for ( i = 0; i < contacts.Num(); i++ ) {
- // add contact constraint
- contactConstraints[i]->physics = this;
- if ( contacts[i].entityNum == self->entityNumber ) {
- contactConstraints[i]->Setup( bodies[contactBodies[i]], bodies[ contacts[i].id ], contacts[i] );
- }
- else {
- contactConstraints[i]->Setup( bodies[contactBodies[i]], NULL, contacts[i] );
- }
- }
- }
- /*
- ================
- idPhysics_AF::ApplyContactForces
- ================
- */
- void idPhysics_AF::ApplyContactForces( void ) {
- #if 0
- int i;
- idEntity *ent;
- idVec3 force;
- for ( i = 0; i < contactConstraints.Num(); i++ ) {
- if ( contactConstraints[i]->body2 != NULL ) {
- continue;
- }
- const contactInfo_t &contact = contactConstraints[i]->GetContact();
- ent = gameLocal.entities[contact.entityNum];
- if ( !ent ) {
- continue;
- }
- force.Zero();
- ent->AddForce( self, contact.id, contact.point, force );
- }
- #endif
- }
- /*
- ================
- idPhysics_AF::ClearExternalForce
- ================
- */
- void idPhysics_AF::ClearExternalForce( void ) {
- int i;
- idAFBody *body;
- for ( i = 0; i < bodies.Num(); i++ ) {
- body = bodies[i];
- // clear external force
- body->current->externalForce.Zero();
- body->next->externalForce.Zero();
- }
- }
- /*
- ================
- idPhysics_AF::AddGravity
- ================
- */
- void idPhysics_AF::AddGravity( void ) {
- int i;
- idAFBody *body;
- for ( i = 0; i < bodies.Num(); i++ ) {
- body = bodies[i];
- // add gravitational force
- body->current->externalForce.SubVec3( 0 ) += body->mass * gravityVector;
- }
- }
- /*
- ================
- idPhysics_AF::SwapStates
- ================
- */
- void idPhysics_AF::SwapStates( void ) {
- int i;
- idAFBody *body;
- AFBodyPState_t *swap;
- for ( i = 0; i < bodies.Num(); i++ ) {
- body = bodies[i];
- // swap the current and next state for next simulation step
- swap = body->current;
- body->current = body->next;
- body->next = swap;
- }
- }
- /*
- ================
- idPhysics_AF::UpdateClipModels
- ================
- */
- void idPhysics_AF::UpdateClipModels( void ) {
- int i;
- idAFBody *body;
- for ( i = 0; i < bodies.Num(); i++ ) {
- body = bodies[i];
- body->clipModel->Link( gameLocal.clip, self, body->clipModel->GetId(), body->current->worldOrigin, body->current->worldAxis );
- }
- }
- /*
- ================
- idPhysics_AF::SetSuspendSpeed
- ================
- */
- void idPhysics_AF::SetSuspendSpeed( const idVec2 &velocity, const idVec2 &acceleration ) {
- this->suspendVelocity = velocity;
- this->suspendAcceleration = acceleration;
- }
- /*
- ================
- idPhysics_AF::SetSuspendTime
- ================
- */
- void idPhysics_AF::SetSuspendTime( const float minTime, const float maxTime ) {
- this->minMoveTime = minTime;
- this->maxMoveTime = maxTime;
- }
- /*
- ================
- idPhysics_AF::SetSuspendTolerance
- ================
- */
- void idPhysics_AF::SetSuspendTolerance( const float noMoveTime, const float noMoveTranslation, const float noMoveRotation ) {
- this->noMoveTime = noMoveTime;
- this->noMoveTranslation = noMoveTranslation;
- this->noMoveRotation = noMoveRotation;
- }
- /*
- ================
- idPhysics_AF::SetTimeScaleRamp
- ================
- */
- void idPhysics_AF::SetTimeScaleRamp( const float start, const float end ) {
- timeScaleRampStart = start;
- timeScaleRampEnd = end;
- }
- /*
- ================
- idPhysics_AF::SetJointFrictionDent
- ================
- */
- void idPhysics_AF::SetJointFrictionDent( const float dent, const float start, const float end ) {
- jointFrictionDent = dent;
- jointFrictionDentStart = start;
- jointFrictionDentEnd = end;
- }
- /*
- ================
- idPhysics_AF::GetJointFrictionScale
- ================
- */
- float idPhysics_AF::GetJointFrictionScale( void ) const {
- if ( jointFrictionDentScale > 0.0f ) {
- return jointFrictionDentScale;
- } else if ( jointFrictionScale > 0.0f ) {
- return jointFrictionScale;
- } else if ( af_jointFrictionScale.GetFloat() > 0.0f ) {
- return af_jointFrictionScale.GetFloat();
- }
- return 1.0f;
- }
- /*
- ================
- idPhysics_AF::SetContactFrictionDent
- ================
- */
- void idPhysics_AF::SetContactFrictionDent( const float dent, const float start, const float end ) {
- contactFrictionDent = dent;
- contactFrictionDentStart = start;
- contactFrictionDentEnd = end;
- }
- /*
- ================
- idPhysics_AF::GetContactFrictionScale
- ================
- */
- float idPhysics_AF::GetContactFrictionScale( void ) const {
- if ( contactFrictionDentScale > 0.0f ) {
- return contactFrictionDentScale;
- } else if ( contactFrictionScale > 0.0f ) {
- return contactFrictionScale;
- } else if ( af_contactFrictionScale.GetFloat() > 0.0f ) {
- return af_contactFrictionScale.GetFloat();
- }
- return 1.0f;
- }
- /*
- ================
- idPhysics_AF::TestIfAtRest
- ================
- */
- bool idPhysics_AF::TestIfAtRest( float timeStep ) {
- int i;
- float translationSqr, maxTranslationSqr, rotation, maxRotation;
- idAFBody *body;
- if ( current.atRest >= 0 ) {
- return true;
- }
- current.activateTime += timeStep;
- // if the simulation should never be suspended before a certaint amount of time passed
- if ( minMoveTime > 0.0f && current.activateTime < minMoveTime ) {
- return false;
- }
- // if the simulation should always be suspended after a certain amount time passed
- if ( maxMoveTime > 0.0f && current.activateTime > maxMoveTime ) {
- return true;
- }
- // test if all bodies hardly moved over a period of time
- if ( current.noMoveTime == 0.0f ) {
- for ( i = 0; i < bodies.Num(); i++ ) {
- body = bodies[i];
- body->atRestOrigin = body->current->worldOrigin;
- body->atRestAxis = body->current->worldAxis;
- }
- current.noMoveTime += timeStep;
- }
- else if ( current.noMoveTime > noMoveTime ) {
- current.noMoveTime = 0.0f;
- maxTranslationSqr = 0.0f;
- maxRotation = 0.0f;
- for ( i = 0; i < bodies.Num(); i++ ) {
- body = bodies[i];
- translationSqr = ( body->current->worldOrigin - body->atRestOrigin ).LengthSqr();
- if ( translationSqr > maxTranslationSqr ) {
- maxTranslationSqr = translationSqr;
- }
- rotation = ( body->atRestAxis.Transpose() * body->current->worldAxis ).ToRotation().GetAngle();
- if ( rotation > maxRotation ) {
- maxRotation = rotation;
- }
- }
- if ( maxTranslationSqr < Square( noMoveTranslation ) && maxRotation < noMoveRotation ) {
- // hardly moved over a period of time so the articulated figure may come to rest
- return true;
- }
- } else {
- current.noMoveTime += timeStep;
- }
- // test if the velocity or acceleration of any body is still too large to come to rest
- for ( i = 0; i < bodies.Num(); i++ ) {
- body = bodies[i];
- if ( body->current->spatialVelocity.SubVec3(0).LengthSqr() > Square( suspendVelocity[0] ) ) {
- return false;
- }
- if ( body->current->spatialVelocity.SubVec3(1).LengthSqr() > Square( suspendVelocity[1] ) ) {
- return false;
- }
- if ( body->acceleration.SubVec3(0).LengthSqr() > Square( suspendAcceleration[0] ) ) {
- return false;
- }
- if ( body->acceleration.SubVec3(1).LengthSqr() > Square( suspendAcceleration[1] ) ) {
- return false;
- }
- }
- // all bodies have a velocity and acceleration small enough to come to rest
- return true;
- }
- /*
- ================
- idPhysics_AF::Rest
- ================
- */
- void idPhysics_AF::Rest( void ) {
- int i;
- current.atRest = gameLocal.time;
- for ( i = 0; i < bodies.Num(); i++ ) {
- bodies[i]->current->spatialVelocity.Zero();
- bodies[i]->current->externalForce.Zero();
- }
- self->BecomeInactive( TH_PHYSICS );
- }
- /*
- ================
- idPhysics_AF::Activate
- ================
- */
- void idPhysics_AF::Activate( void ) {
- // if the articulated figure was at rest
- if ( current.atRest >= 0 ) {
- // normally gravity is added at the end of a simulation frame
- // if the figure was at rest add gravity here so it is applied this simulation frame
- AddGravity();
- // reset the active time for the max move time
- current.activateTime = 0.0f;
- }
- current.atRest = -1;
- current.noMoveTime = 0.0f;
- self->BecomeActive( TH_PHYSICS );
- }
- /*
- ================
- idPhysics_AF::PutToRest
- put to rest untill something collides with this physics object
- ================
- */
- void idPhysics_AF::PutToRest( void ) {
- Rest();
- }
- /*
- ================
- idPhysics_AF::EnableImpact
- ================
- */
- void idPhysics_AF::EnableImpact( void ) {
- noImpact = false;
- }
- /*
- ================
- idPhysics_AF::DisableImpact
- ================
- */
- void idPhysics_AF::DisableImpact( void ) {
- noImpact = true;
- }
- /*
- ================
- idPhysics_AF::AddPushVelocity
- ================
- */
- void idPhysics_AF::AddPushVelocity( const idVec6 &pushVelocity ) {
- int i;
- if ( pushVelocity != vec6_origin ) {
- for ( i = 0; i < bodies.Num(); i++ ) {
- bodies[i]->current->spatialVelocity += pushVelocity;
- }
- }
- }
- /*
- ================
- idPhysics_AF::SetClipModel
- ================
- */
- void idPhysics_AF::SetClipModel( idClipModel *model, float density, int id, bool freeOld ) {
- }
- /*
- ================
- idPhysics_AF::GetClipModel
- ================
- */
- idClipModel *idPhysics_AF::GetClipModel( int id ) const {
- if ( id >= 0 && id < bodies.Num() ) {
- return bodies[id]->GetClipModel();
- }
- return NULL;
- }
- /*
- ================
- idPhysics_AF::GetNumClipModels
- ================
- */
- int idPhysics_AF::GetNumClipModels( void ) const {
- return bodies.Num();
- }
- /*
- ================
- idPhysics_AF::SetMass
- ================
- */
- void idPhysics_AF::SetMass( float mass, int id ) {
- if ( id >= 0 && id < bodies.Num() ) {
- }
- else {
- forceTotalMass = mass;
- }
- SetChanged();
- }
- /*
- ================
- idPhysics_AF::GetMass
- ================
- */
- float idPhysics_AF::GetMass( int id ) const {
- if ( id >= 0 && id < bodies.Num() ) {
- return bodies[id]->mass;
- }
- return totalMass;
- }
- /*
- ================
- idPhysics_AF::SetContents
- ================
- */
- void idPhysics_AF::SetContents( int contents, int id ) {
- int i;
- if ( id >= 0 && id < bodies.Num() ) {
- bodies[id]->GetClipModel()->SetContents( contents );
- }
- else {
- for ( i = 0; i < bodies.Num(); i++ ) {
- bodies[i]->GetClipModel()->SetContents( contents );
- }
- }
- }
- /*
- ================
- idPhysics_AF::GetContents
- ================
- */
- int idPhysics_AF::GetContents( int id ) const {
- int i, contents;
- if ( id >= 0 && id < bodies.Num() ) {
- return bodies[id]->GetClipModel()->GetContents();
- }
- else {
- contents = 0;
- for ( i = 0; i < bodies.Num(); i++ ) {
- contents |= bodies[i]->GetClipModel()->GetContents();
- }
- return contents;
- }
- }
- /*
- ================
- idPhysics_AF::GetBounds
- ================
- */
- const idBounds &idPhysics_AF::GetBounds( int id ) const {
- int i;
- static idBounds relBounds;
- if ( id >= 0 && id < bodies.Num() ) {
- return bodies[id]->GetClipModel()->GetBounds();
- }
- else if ( !bodies.Num() ) {
- relBounds.Zero();
- return relBounds;
- }
- else {
- relBounds = bodies[0]->GetClipModel()->GetBounds();
- for ( i = 1; i < bodies.Num(); i++ ) {
- idBounds bounds;
- idVec3 origin = ( bodies[i]->GetWorldOrigin() - bodies[0]->GetWorldOrigin() ) * bodies[0]->GetWorldAxis().Transpose();
- idMat3 axis = bodies[i]->GetWorldAxis() * bodies[0]->GetWorldAxis().Transpose();
- bounds.FromTransformedBounds( bodies[i]->GetClipModel()->GetBounds(), origin, axis );
- relBounds += bounds;
- }
- return relBounds;
- }
- }
- /*
- ================
- idPhysics_AF::GetAbsBounds
- ================
- */
- const idBounds &idPhysics_AF::GetAbsBounds( int id ) const {
- int i;
- static idBounds absBounds;
- if ( id >= 0 && id < bodies.Num() ) {
- return bodies[id]->GetClipModel()->GetAbsBounds();
- }
- else if ( !bodies.Num() ) {
- absBounds.Zero();
- return absBounds;
- }
- else {
- absBounds = bodies[0]->GetClipModel()->GetAbsBounds();
- for ( i = 1; i < bodies.Num(); i++ ) {
- absBounds += bodies[i]->GetClipModel()->GetAbsBounds();
- }
- return absBounds;
- }
- }
- /*
- ================
- idPhysics_AF::Evaluate
- ================
- */
- bool idPhysics_AF::Evaluate( int timeStepMSec, int endTimeMSec ) {
- float timeStep;
- if ( timeScaleRampStart < MS2SEC( endTimeMSec ) && timeScaleRampEnd > MS2SEC( endTimeMSec ) ) {
- timeStep = MS2SEC( timeStepMSec ) * ( MS2SEC( endTimeMSec ) - timeScaleRampStart ) / ( timeScaleRampEnd - timeScaleRampStart );
- } else if ( af_timeScale.GetFloat() != 1.0f ) {
- timeStep = MS2SEC( timeStepMSec ) * af_timeScale.GetFloat();
- } else {
- timeStep = MS2SEC( timeStepMSec ) * timeScale;
- }
- current.lastTimeStep = timeStep;
- // if the articulated figure changed
- if ( changedAF || ( linearTime != af_useLinearTime.GetBool() ) ) {
- BuildTrees();
- changedAF = false;
- linearTime = af_useLinearTime.GetBool();
- }
- // get the new master position
- if ( masterBody ) {
- idVec3 masterOrigin;
- idMat3 masterAxis;
- self->GetMasterPosition( masterOrigin, masterAxis );
- if ( current.atRest >= 0 && ( masterBody->current->worldOrigin != masterOrigin || masterBody->current->worldAxis != masterAxis ) ) {
- Activate();
- }
- masterBody->current->worldOrigin = masterOrigin;
- masterBody->current->worldAxis = masterAxis;
- }
- // if the simulation is suspended because the figure is at rest
- if ( current.atRest >= 0 || timeStep <= 0.0f ) {
- DebugDraw();
- return false;
- }
- // move the af velocity into the frame of a pusher
- AddPushVelocity( -current.pushVelocity );
- #ifdef AF_TIMINGS
- timer_total.Start();
- #endif
- #ifdef AF_TIMINGS
- timer_collision.Start();
- #endif
- // evaluate contacts
- EvaluateContacts();
- // setup contact constraints
- SetupContactConstraints();
- #ifdef AF_TIMINGS
- timer_collision.Stop();
- #endif
- // evaluate constraint equations
- EvaluateConstraints( timeStep );
- // apply friction
- ApplyFriction( timeStep, endTimeMSec );
- // add frame constraints
- AddFrameConstraints();
- #ifdef AF_TIMINGS
- int i, numPrimary = 0, numAuxiliary = 0;
- for ( i = 0; i < primaryConstraints.Num(); i++ ) {
- numPrimary += primaryConstraints[i]->J1.GetNumRows();
- }
- for ( i = 0; i < auxiliaryConstraints.Num(); i++ ) {
- numAuxiliary += auxiliaryConstraints[i]->J1.GetNumRows();
- }
- timer_pc.Start();
- #endif
- // factor matrices for primary constraints
- PrimaryFactor();
- // calculate forces on bodies after applying primary constraints
- PrimaryForces( timeStep );
- #ifdef AF_TIMINGS
- timer_pc.Stop();
- timer_ac.Start();
- #endif
- // calculate and apply auxiliary constraint forces
- AuxiliaryForces( timeStep );
- #ifdef AF_TIMINGS
- timer_ac.Stop();
- #endif
- // evolve current state to next state
- Evolve( timeStep );
- // debug graphics
- DebugDraw();
- // clear external forces on all bodies
- ClearExternalForce();
- // apply contact force to other entities
- ApplyContactForces();
- // remove all frame constraints
- RemoveFrameConstraints();
- #ifdef AF_TIMINGS
- timer_collision.Start();
- #endif
- // check for collisions between current and next state
- CheckForCollisions( timeStep );
- #ifdef AF_TIMINGS
- timer_collision.Stop();
- #endif
- // swap the current and next state
- SwapStates();
- // make sure all clip models are disabled in case they were enabled for self collision
- if ( selfCollision && !af_skipSelfCollision.GetBool() ) {
- DisableClip();
- }
- // apply collision impulses
- if ( ApplyCollisions( timeStep ) ) {
- current.atRest = gameLocal.time;
- comeToRest = true;
- }
- // test if the simulation can be suspended because the whole figure is at rest
- if ( comeToRest && TestIfAtRest( timeStep ) ) {
- Rest();
- } else {
- ActivateContactEntities();
- }
- // add gravitational force
- AddGravity();
- // move the af velocity back into the world frame
- AddPushVelocity( current.pushVelocity );
- current.pushVelocity.Zero();
- if ( IsOutsideWorld() ) {
- gameLocal.Warning( "articulated figure moved outside world bounds for entity '%s' type '%s' at (%s)",
- self->name.c_str(), self->GetType()->classname, bodies[0]->current->worldOrigin.ToString(0) );
- Rest();
- }
- #ifdef AF_TIMINGS
- timer_total.Stop();
- if ( af_showTimings.GetInteger() == 1 ) {
- gameLocal.Printf( "%12s: t %1.4f pc %2d, %1.4f ac %2d %1.4f lcp %1.4f cd %1.4f\n",
- self->name.c_str(),
- timer_total.Milliseconds(),
- numPrimary, timer_pc.Milliseconds(),
- numAuxiliary, timer_ac.Milliseconds() - timer_lcp.Milliseconds(),
- timer_lcp.Milliseconds(), timer_collision.Milliseconds() );
- }
- else if ( af_showTimings.GetInteger() == 2 ) {
- numArticulatedFigures++;
- if ( endTimeMSec > lastTimerReset ) {
- gameLocal.Printf( "af %d: t %1.4f pc %2d, %1.4f ac %2d %1.4f lcp %1.4f cd %1.4f\n",
- numArticulatedFigures,
- timer_total.Milliseconds(),
- numPrimary, timer_pc.Milliseconds(),
- numAuxiliary, timer_ac.Milliseconds() - timer_lcp.Milliseconds(),
- timer_lcp.Milliseconds(), timer_collision.Milliseconds() );
- }
- }
- if ( endTimeMSec > lastTimerReset ) {
- lastTimerReset = endTimeMSec;
- numArticulatedFigures = 0;
- timer_total.Clear();
- timer_pc.Clear();
- timer_ac.Clear();
- timer_collision.Clear();
- timer_lcp.Clear();
- }
- #endif
- return true;
- }
- /*
- ================
- idPhysics_AF::UpdateTime
- ================
- */
- void idPhysics_AF::UpdateTime( int endTimeMSec ) {
- }
- /*
- ================
- idPhysics_AF::GetTime
- ================
- */
- int idPhysics_AF::GetTime( void ) const {
- return gameLocal.time;
- }
- /*
- ================
- DrawTraceModelSilhouette
- ================
- */
- void DrawTraceModelSilhouette( const idVec3 &projectionOrigin, const idClipModel *clipModel ) {
- int i, numSilEdges;
- int silEdges[MAX_TRACEMODEL_EDGES];
- idVec3 v1, v2;
- const idTraceModel *trm = clipModel->GetTraceModel();
- const idVec3 &origin = clipModel->GetOrigin();
- const idMat3 &axis = clipModel->GetAxis();
- numSilEdges = trm->GetProjectionSilhouetteEdges( ( projectionOrigin - origin ) * axis.Transpose(), silEdges );
- for ( i = 0; i < numSilEdges; i++ ) {
- v1 = trm->verts[ trm->edges[ abs(silEdges[i]) ].v[ INTSIGNBITSET( silEdges[i] ) ] ];
- v2 = trm->verts[ trm->edges[ abs(silEdges[i]) ].v[ INTSIGNBITNOTSET( silEdges[i] ) ] ];
- gameRenderWorld->DebugArrow( colorRed, origin + v1 * axis, origin + v2 * axis, 1 );
- }
- }
- /*
- ================
- idPhysics_AF::DebugDraw
- ================
- */
- void idPhysics_AF::DebugDraw( void ) {
- int i;
- idAFBody *body, *highlightBody = NULL, *constrainedBody1 = NULL, *constrainedBody2 = NULL;
- idAFConstraint *constraint;
- idVec3 center;
- idMat3 axis;
- if ( af_highlightConstraint.GetString()[0] ) {
- constraint = GetConstraint( af_highlightConstraint.GetString() );
- if ( constraint ) {
- constraint->GetCenter( center );
- axis = gameLocal.GetLocalPlayer()->viewAngles.ToMat3();
- gameRenderWorld->DebugCone( colorYellow, center, (axis[2] - axis[1]) * 4.0f, 0.0f, 1.0f, 0 );
- if ( af_showConstrainedBodies.GetBool() ) {
- cvarSystem->SetCVarString( "cm_drawColor", colorCyan.ToString( 0 ) );
- constrainedBody1 = constraint->body1;
- if ( constrainedBody1 ) {
- collisionModelManager->DrawModel( constrainedBody1->clipModel->Handle(), constrainedBody1->clipModel->GetOrigin(),
- constrainedBody1->clipModel->GetAxis(), vec3_origin, 0.0f );
- }
- cvarSystem->SetCVarString( "cm_drawColor", colorBlue.ToString( 0 ) );
- constrainedBody2 = constraint->body2;
- if ( constrainedBody2 ) {
- collisionModelManager->DrawModel( constrainedBody2->clipModel->Handle(), constrainedBody2->clipModel->GetOrigin(),
- constrainedBody2->clipModel->GetAxis(), vec3_origin, 0.0f );
- }
- cvarSystem->SetCVarString( "cm_drawColor", colorRed.ToString( 0 ) );
- }
- }
- }
- if ( af_highlightBody.GetString()[0] ) {
- highlightBody = GetBody( af_highlightBody.GetString() );
- if ( highlightBody ) {
- cvarSystem->SetCVarString( "cm_drawColor", colorYellow.ToString( 0 ) );
- collisionModelManager->DrawModel( highlightBody->clipModel->Handle(), highlightBody->clipModel->GetOrigin(),
- highlightBody->clipModel->GetAxis(), vec3_origin, 0.0f );
- cvarSystem->SetCVarString( "cm_drawColor", colorRed.ToString( 0 ) );
- }
- }
- if ( af_showBodies.GetBool() ) {
- for ( i = 0; i < bodies.Num(); i++ ) {
- body = bodies[i];
- if ( body == constrainedBody1 || body == constrainedBody2 ) {
- continue;
- }
- if ( body == highlightBody ) {
- continue;
- }
- collisionModelManager->DrawModel( body->clipModel->Handle(), body->clipModel->GetOrigin(),
- body->clipModel->GetAxis(), vec3_origin, 0.0f );
- //DrawTraceModelSilhouette( gameLocal.GetLocalPlayer()->GetEyePosition(), body->clipModel );
- }
- }
- if ( af_showBodyNames.GetBool() ) {
- for ( i = 0; i < bodies.Num(); i++ ) {
- body = bodies[i];
- gameRenderWorld->DrawText( body->GetName().c_str(), body->GetWorldOrigin(), 0.08f, colorCyan, gameLocal.GetLocalPlayer()->viewAngles.ToMat3(), 1 );
- }
- }
- if ( af_showMass.GetBool() ) {
- for ( i = 0; i < bodies.Num(); i++ ) {
- body = bodies[i];
- gameRenderWorld->DrawText( va( "\n%1.2f", 1.0f / body->GetInverseMass() ), body->GetWorldOrigin(), 0.08f, colorCyan, gameLocal.GetLocalPlayer()->viewAngles.ToMat3(), 1 );
- }
- }
- if ( af_showTotalMass.GetBool() ) {
- axis = gameLocal.GetLocalPlayer()->viewAngles.ToMat3();
- gameRenderWorld->DrawText( va( "\n%1.2f", totalMass ), bodies[0]->GetWorldOrigin() + axis[2] * 8.0f, 0.15f, colorCyan, axis, 1 );
- }
- if ( af_showInertia.GetBool() ) {
- for ( i = 0; i < bodies.Num(); i++ ) {
- body = bodies[i];
- idMat3 &I = body->inertiaTensor;
- gameRenderWorld->DrawText( va( "\n\n\n( %.1f %.1f %.1f )\n( %.1f %.1f %.1f )\n( %.1f %.1f %.1f )",
- I[0].x, I[0].y, I[0].z,
- I[1].x, I[1].y, I[1].z,
- I[2].x, I[2].y, I[2].z ),
- body->GetWorldOrigin(), 0.05f, colorCyan, gameLocal.GetLocalPlayer()->viewAngles.ToMat3(), 1 );
- }
- }
- if ( af_showVelocity.GetBool() ) {
- for ( i = 0; i < bodies.Num(); i++ ) {
- DrawVelocity( bodies[i]->clipModel->GetId(), 0.1f, 4.0f );
- }
- }
- if ( af_showConstraints.GetBool() ) {
- for ( i = 0; i < primaryConstraints.Num(); i++ ) {
- constraint = primaryConstraints[i];
- constraint->DebugDraw();
- }
- if ( !af_showPrimaryOnly.GetBool() ) {
- for ( i = 0; i < auxiliaryConstraints.Num(); i++ ) {
- constraint = auxiliaryConstraints[i];
- constraint->DebugDraw();
- }
- }
- }
- if ( af_showConstraintNames.GetBool() ) {
- for ( i = 0; i < primaryConstraints.Num(); i++ ) {
- constraint = primaryConstraints[i];
- constraint->GetCenter( center );
- gameRenderWorld->DrawText( constraint->GetName().c_str(), center, 0.08f, colorCyan, gameLocal.GetLocalPlayer()->viewAngles.ToMat3(), 1 );
- }
- if ( !af_showPrimaryOnly.GetBool() ) {
- for ( i = 0; i < auxiliaryConstraints.Num(); i++ ) {
- constraint = auxiliaryConstraints[i];
- constraint->GetCenter( center );
- gameRenderWorld->DrawText( constraint->GetName().c_str(), center, 0.08f, colorCyan, gameLocal.GetLocalPlayer()->viewAngles.ToMat3(), 1 );
- }
- }
- }
- if ( af_showTrees.GetBool() || ( af_showActive.GetBool() && current.atRest < 0 ) ) {
- for ( i = 0; i < trees.Num(); i++ ) {
- trees[i]->DebugDraw( idStr::ColorForIndex( i+3 ) );
- }
- }
- }
- /*
- ================
- idPhysics_AF::idPhysics_AF
- ================
- */
- idPhysics_AF::idPhysics_AF( void ) {
- trees.Clear();
- bodies.Clear();
- constraints.Clear();
- primaryConstraints.Clear();
- auxiliaryConstraints.Clear();
- frameConstraints.Clear();
- contacts.Clear();
- collisions.Clear();
- changedAF = true;
- masterBody = NULL;
- lcp = idLCP::AllocSymmetric();
- memset( ¤t, 0, sizeof( current ) );
- current.atRest = -1;
- current.lastTimeStep = USERCMD_MSEC;
- saved = current;
- linearFriction = 0.005f;
- angularFriction = 0.005f;
- contactFriction = 0.8f;
- bouncyness = 0.4f;
- totalMass = 0.0f;
- forceTotalMass = -1.0f;
- suspendVelocity.Set( SUSPEND_LINEAR_VELOCITY, SUSPEND_ANGULAR_VELOCITY );
- suspendAcceleration.Set( SUSPEND_LINEAR_ACCELERATION, SUSPEND_LINEAR_ACCELERATION );
- noMoveTime = NO_MOVE_TIME;
- noMoveTranslation = NO_MOVE_TRANSLATION_TOLERANCE;
- noMoveRotation = NO_MOVE_ROTATION_TOLERANCE;
- minMoveTime = MIN_MOVE_TIME;
- maxMoveTime = MAX_MOVE_TIME;
- impulseThreshold = IMPULSE_THRESHOLD;
- timeScale = 1.0f;
- timeScaleRampStart = 0.0f;
- timeScaleRampEnd = 0.0f;
- jointFrictionScale = 0.0f;
- jointFrictionDent = 0.0f;
- jointFrictionDentStart = 0.0f;
- jointFrictionDentEnd = 0.0f;
- jointFrictionDentScale = 0.0f;
- contactFrictionScale = 0.0f;
- contactFrictionDent = 0.0f;
- contactFrictionDentStart = 0.0f;
- contactFrictionDentEnd = 0.0f;
- contactFrictionDentScale = 0.0f;
- enableCollision = true;
- selfCollision = true;
- comeToRest = true;
- linearTime = true;
- noImpact = false;
- worldConstraintsLocked = false;
- forcePushable = false;
- #ifdef AF_TIMINGS
- lastTimerReset = 0;
- #endif
- }
- /*
- ================
- idPhysics_AF::~idPhysics_AF
- ================
- */
- idPhysics_AF::~idPhysics_AF( void ) {
- int i;
- trees.DeleteContents( true );
- for ( i = 0; i < bodies.Num(); i++ ) {
- delete bodies[i];
- }
- for ( i = 0; i < constraints.Num(); i++ ) {
- delete constraints[i];
- }
- contactConstraints.SetNum( contactConstraints.NumAllocated(), false );
- for ( i = 0; i < contactConstraints.NumAllocated(); i++ ) {
- delete contactConstraints[i];
- }
- delete lcp;
- if ( masterBody ) {
- delete masterBody;
- }
- }
- /*
- ================
- idPhysics_AF_SavePState
- ================
- */
- void idPhysics_AF_SavePState( idSaveGame *saveFile, const AFPState_t &state ) {
- saveFile->WriteInt( state.atRest );
- saveFile->WriteFloat( state.noMoveTime );
- saveFile->WriteFloat( state.activateTime );
- saveFile->WriteFloat( state.lastTimeStep );
- saveFile->WriteVec6( state.pushVelocity );
- }
- /*
- ================
- idPhysics_AF_RestorePState
- ================
- */
- void idPhysics_AF_RestorePState( idRestoreGame *saveFile, AFPState_t &state ) {
- saveFile->ReadInt( state.atRest );
- saveFile->ReadFloat( state.noMoveTime );
- saveFile->ReadFloat( state.activateTime );
- saveFile->ReadFloat( state.lastTimeStep );
- saveFile->ReadVec6( state.pushVelocity );
- }
- /*
- ================
- idPhysics_AF::Save
- ================
- */
- void idPhysics_AF::Save( idSaveGame *saveFile ) const {
- int i;
- // the articulated figure structure is handled by the owner
- idPhysics_AF_SavePState( saveFile, current );
- idPhysics_AF_SavePState( saveFile, saved );
- saveFile->WriteInt( bodies.Num() );
- for ( i = 0; i < bodies.Num(); i++ ) {
- bodies[i]->Save( saveFile );
- }
- if ( masterBody ) {
- saveFile->WriteBool( true );
- masterBody->Save( saveFile );
- } else {
- saveFile->WriteBool( false );
- }
- saveFile->WriteInt( constraints.Num() );
- for ( i = 0; i < constraints.Num(); i++ ) {
- constraints[i]->Save( saveFile );
- }
- saveFile->WriteBool( changedAF );
- saveFile->WriteFloat( linearFriction );
- saveFile->WriteFloat( angularFriction );
- saveFile->WriteFloat( contactFriction );
- saveFile->WriteFloat( bouncyness );
- saveFile->WriteFloat( totalMass );
- saveFile->WriteFloat( forceTotalMass );
- saveFile->WriteVec2( suspendVelocity );
- saveFile->WriteVec2( suspendAcceleration );
- saveFile->WriteFloat( noMoveTime );
- saveFile->WriteFloat( noMoveTranslation );
- saveFile->WriteFloat( noMoveRotation );
- saveFile->WriteFloat( minMoveTime );
- saveFile->WriteFloat( maxMoveTime );
- saveFile->WriteFloat( impulseThreshold );
- saveFile->WriteFloat( timeScale );
- saveFile->WriteFloat( timeScaleRampStart );
- saveFile->WriteFloat( timeScaleRampEnd );
- saveFile->WriteFloat( jointFrictionScale );
- saveFile->WriteFloat( jointFrictionDent );
- saveFile->WriteFloat( jointFrictionDentStart );
- saveFile->WriteFloat( jointFrictionDentEnd );
- saveFile->WriteFloat( jointFrictionDentScale );
- saveFile->WriteFloat( contactFrictionScale );
- saveFile->WriteFloat( contactFrictionDent );
- saveFile->WriteFloat( contactFrictionDentStart );
- saveFile->WriteFloat( contactFrictionDentEnd );
- saveFile->WriteFloat( contactFrictionDentScale );
- saveFile->WriteBool( enableCollision );
- saveFile->WriteBool( selfCollision );
- saveFile->WriteBool( comeToRest );
- saveFile->WriteBool( linearTime );
- saveFile->WriteBool( noImpact );
- saveFile->WriteBool( worldConstraintsLocked );
- saveFile->WriteBool( forcePushable );
- }
- /*
- ================
- idPhysics_AF::Restore
- ================
- */
- void idPhysics_AF::Restore( idRestoreGame *saveFile ) {
- int i, num;
- bool hasMaster;
- // the articulated figure structure should have already been restored
- idPhysics_AF_RestorePState( saveFile, current );
- idPhysics_AF_RestorePState( saveFile, saved );
- saveFile->ReadInt( num );
- assert( num == bodies.Num() );
- for ( i = 0; i < bodies.Num(); i++ ) {
- bodies[i]->Restore( saveFile );
- }
- saveFile->ReadBool( hasMaster );
- if ( hasMaster ) {
- masterBody = new idAFBody();
- masterBody->Restore( saveFile );
- }
- saveFile->ReadInt( num );
- assert( num == constraints.Num() );
- for ( i = 0; i < constraints.Num(); i++ ) {
- constraints[i]->Restore( saveFile );
- }
- saveFile->ReadBool( changedAF );
- saveFile->ReadFloat( linearFriction );
- saveFile->ReadFloat( angularFriction );
- saveFile->ReadFloat( contactFriction );
- saveFile->ReadFloat( bouncyness );
- saveFile->ReadFloat( totalMass );
- saveFile->ReadFloat( forceTotalMass );
- saveFile->ReadVec2( suspendVelocity );
- saveFile->ReadVec2( suspendAcceleration );
- saveFile->ReadFloat( noMoveTime );
- saveFile->ReadFloat( noMoveTranslation );
- saveFile->ReadFloat( noMoveRotation );
- saveFile->ReadFloat( minMoveTime );
- saveFile->ReadFloat( maxMoveTime );
- saveFile->ReadFloat( impulseThreshold );
- saveFile->ReadFloat( timeScale );
- saveFile->ReadFloat( timeScaleRampStart );
- saveFile->ReadFloat( timeScaleRampEnd );
- saveFile->ReadFloat( jointFrictionScale );
- saveFile->ReadFloat( jointFrictionDent );
- saveFile->ReadFloat( jointFrictionDentStart );
- saveFile->ReadFloat( jointFrictionDentEnd );
- saveFile->ReadFloat( jointFrictionDentScale );
- saveFile->ReadFloat( contactFrictionScale );
- saveFile->ReadFloat( contactFrictionDent );
- saveFile->ReadFloat( contactFrictionDentStart );
- saveFile->ReadFloat( contactFrictionDentEnd );
- saveFile->ReadFloat( contactFrictionDentScale );
- saveFile->ReadBool( enableCollision );
- saveFile->ReadBool( selfCollision );
- saveFile->ReadBool( comeToRest );
- saveFile->ReadBool( linearTime );
- saveFile->ReadBool( noImpact );
- saveFile->ReadBool( worldConstraintsLocked );
- saveFile->ReadBool( forcePushable );
- changedAF = true;
- UpdateClipModels();
- }
- /*
- ================
- idPhysics_AF::IsClosedLoop
- ================
- */
- bool idPhysics_AF::IsClosedLoop( const idAFBody *body1, const idAFBody *body2 ) const {
- const idAFBody *b1, *b2;
- for ( b1 = body1; b1->parent; b1 = b1->parent ) {
- }
- for ( b2 = body2; b2->parent; b2 = b2->parent ) {
- }
- return ( b1 == b2 );
- }
- /*
- ================
- idPhysics_AF::BuildTrees
- ================
- */
- void idPhysics_AF::BuildTrees( void ) {
- int i;
- float scale;
- idAFBody *b;
- idAFConstraint *c;
- idAFTree *tree;
- primaryConstraints.Clear();
- auxiliaryConstraints.Clear();
- trees.DeleteContents( true );
- totalMass = 0.0f;
- for ( i = 0; i < bodies.Num(); i++ ) {
- b = bodies[i];
- b->parent = NULL;
- b->primaryConstraint = NULL;
- b->constraints.SetNum( 0, false );
- b->children.Clear();
- b->tree = NULL;
- totalMass += b->mass;
- }
- if ( forceTotalMass > 0.0f ) {
- scale = forceTotalMass / totalMass;
- for ( i = 0; i < bodies.Num(); i++ ) {
- b = bodies[i];
- b->mass *= scale;
- b->invMass = 1.0f / b->mass;
- b->inertiaTensor *= scale;
- b->inverseInertiaTensor = b->inertiaTensor.Inverse();
- }
- totalMass = forceTotalMass;
- }
- if ( af_useLinearTime.GetBool() ) {
- for ( i = 0; i < constraints.Num(); i++ ) {
- c = constraints[i];
- c->body1->constraints.Append( c );
- if ( c->body2 ) {
- c->body2->constraints.Append( c );
- }
- // only bilateral constraints between two non-world bodies that do not
- // create loops can be used as primary constraints
- if ( !c->body1->primaryConstraint && c->fl.allowPrimary && c->body2 != NULL && !IsClosedLoop( c->body1, c->body2 ) ) {
- c->body1->primaryConstraint = c;
- c->body1->parent = c->body2;
- c->body2->children.Append( c->body1 );
- c->fl.isPrimary = true;
- c->firstIndex = 0;
- primaryConstraints.Append( c );
- } else {
- c->fl.isPrimary = false;
- auxiliaryConstraints.Append( c );
- }
- }
- // create trees for all parent bodies
- for ( i = 0; i < bodies.Num(); i++ ) {
- if ( !bodies[i]->parent ) {
- tree = new idAFTree();
- tree->sortedBodies.Clear();
- tree->sortedBodies.Append( bodies[i] );
- bodies[i]->tree = tree;
- trees.Append( tree );
- }
- }
- // add each child body to the appropriate tree
- for ( i = 0; i < bodies.Num(); i++ ) {
- if ( bodies[i]->parent ) {
- for ( b = bodies[i]->parent; !b->tree; b = b->parent ) {
- }
- b->tree->sortedBodies.Append( bodies[i] );
- bodies[i]->tree = b->tree;
- }
- }
- if ( trees.Num() > 1 ) {
- gameLocal.Warning( "Articulated figure has multiple seperate tree structures for entity '%s' type '%s'.",
- self->name.c_str(), self->GetType()->classname );
- }
- // sort bodies in each tree to make sure parents come first
- for ( i = 0; i < trees.Num(); i++ ) {
- trees[i]->SortBodies();
- }
- } else {
- // create a tree for each body
- for ( i = 0; i < bodies.Num(); i++ ) {
- tree = new idAFTree();
- tree->sortedBodies.Clear();
- tree->sortedBodies.Append( bodies[i] );
- bodies[i]->tree = tree;
- trees.Append( tree );
- }
- for ( i = 0; i < constraints.Num(); i++ ) {
- c = constraints[i];
- c->body1->constraints.Append( c );
- if ( c->body2 ) {
- c->body2->constraints.Append( c );
- }
- c->fl.isPrimary = false;
- auxiliaryConstraints.Append( c );
- }
- }
- }
- /*
- ================
- idPhysics_AF::AddBody
- bodies get an id in the order they are added starting at zero
- as such the first body added will get id zero
- ================
- */
- int idPhysics_AF::AddBody( idAFBody *body ) {
- int id = 0;
- if ( !body->clipModel ) {
- gameLocal.Error( "idPhysics_AF::AddBody: body '%s' has no clip model.", body->name.c_str() );
- }
- if ( bodies.Find( body ) ) {
- gameLocal.Error( "idPhysics_AF::AddBody: body '%s' added twice.", body->name.c_str() );
- }
- if ( GetBody( body->name ) ) {
- gameLocal.Error( "idPhysics_AF::AddBody: a body with the name '%s' already exists.", body->name.c_str() );
- }
- id = bodies.Num();
- body->clipModel->SetId( id );
- if ( body->linearFriction < 0.0f ) {
- body->linearFriction = linearFriction;
- body->angularFriction = angularFriction;
- body->contactFriction = contactFriction;
- }
- if ( body->bouncyness < 0.0f ) {
- body->bouncyness = bouncyness;
- }
- if ( !body->fl.clipMaskSet ) {
- body->clipMask = clipMask;
- }
- bodies.Append( body );
- changedAF = true;
- return id;
- }
- /*
- ================
- idPhysics_AF::AddConstraint
- ================
- */
- void idPhysics_AF::AddConstraint( idAFConstraint *constraint ) {
- if ( constraints.Find( constraint ) ) {
- gameLocal.Error( "idPhysics_AF::AddConstraint: constraint '%s' added twice.", constraint->name.c_str() );
- }
- if ( GetConstraint( constraint->name ) ) {
- gameLocal.Error( "idPhysics_AF::AddConstraint: a constraint with the name '%s' already exists.", constraint->name.c_str() );
- }
- if ( !constraint->body1 ) {
- gameLocal.Error( "idPhysics_AF::AddConstraint: body1 == NULL on constraint '%s'.", constraint->name.c_str() );
- }
- if ( !bodies.Find( constraint->body1 ) ) {
- gameLocal.Error( "idPhysics_AF::AddConstraint: body1 of constraint '%s' is not part of the articulated figure.", constraint->name.c_str() );
- }
- if ( constraint->body2 && !bodies.Find( constraint->body2 ) ) {
- gameLocal.Error( "idPhysics_AF::AddConstraint: body2 of constraint '%s' is not part of the articulated figure.", constraint->name.c_str() );
- }
- if ( constraint->body1 == constraint->body2 ) {
- gameLocal.Error( "idPhysics_AF::AddConstraint: body1 and body2 of constraint '%s' are the same.", constraint->name.c_str() );
- }
- constraints.Append( constraint );
- constraint->physics = this;
- changedAF = true;
- }
- /*
- ================
- idPhysics_AF::AddFrameConstraint
- ================
- */
- void idPhysics_AF::AddFrameConstraint( idAFConstraint *constraint ) {
- frameConstraints.Append( constraint );
- constraint->physics = this;
- }
- /*
- ================
- idPhysics_AF::ForceBodyId
- ================
- */
- void idPhysics_AF::ForceBodyId( idAFBody *body, int newId ) {
- int id;
- id = bodies.FindIndex( body );
- if ( id == -1 ) {
- gameLocal.Error( "ForceBodyId: body '%s' is not part of the articulated figure.\n", body->name.c_str() );
- }
- if ( id != newId ) {
- idAFBody *b = bodies[newId];
- bodies[newId] = bodies[id];
- bodies[id] = b;
- changedAF = true;
- }
- }
- /*
- ================
- idPhysics_AF::GetBodyId
- ================
- */
- int idPhysics_AF::GetBodyId( idAFBody *body ) const {
- int id;
- id = bodies.FindIndex( body );
- if ( id == -1 && body ) {
- gameLocal.Error( "GetBodyId: body '%s' is not part of the articulated figure.\n", body->name.c_str() );
- }
- return id;
- }
- /*
- ================
- idPhysics_AF::GetBodyId
- ================
- */
- int idPhysics_AF::GetBodyId( const char *bodyName ) const {
- int i;
- for ( i = 0; i < bodies.Num(); i++ ) {
- if ( !bodies[i]->name.Icmp( bodyName ) ) {
- return i;
- }
- }
- gameLocal.Error( "GetBodyId: no body with the name '%s' is not part of the articulated figure.\n", bodyName );
- return 0;
- }
- /*
- ================
- idPhysics_AF::GetConstraintId
- ================
- */
- int idPhysics_AF::GetConstraintId( idAFConstraint *constraint ) const {
- int id;
- id = constraints.FindIndex( constraint );
- if ( id == -1 && constraint ) {
- gameLocal.Error( "GetConstraintId: constraint '%s' is not part of the articulated figure.\n", constraint->name.c_str() );
- }
- return id;
- }
- /*
- ================
- idPhysics_AF::GetConstraintId
- ================
- */
- int idPhysics_AF::GetConstraintId( const char *constraintName ) const {
- int i;
- for ( i = 0; i < constraints.Num(); i++ ) {
- if ( constraints[i]->name.Icmp( constraintName ) == 0 ) {
- return i;
- }
- }
- gameLocal.Error( "GetConstraintId: no constraint with the name '%s' is not part of the articulated figure.\n", constraintName );
- return 0;
- }
- /*
- ================
- idPhysics_AF::GetNumBodies
- ================
- */
- int idPhysics_AF::GetNumBodies( void ) const {
- return bodies.Num();
- }
- /*
- ================
- idPhysics_AF::GetNumConstraints
- ================
- */
- int idPhysics_AF::GetNumConstraints( void ) const {
- return constraints.Num();
- }
- /*
- ================
- idPhysics_AF::GetBody
- ================
- */
- idAFBody *idPhysics_AF::GetBody( const char *bodyName ) const {
- int i;
- for ( i = 0; i < bodies.Num(); i++ ) {
- if ( !bodies[i]->name.Icmp( bodyName ) ) {
- return bodies[i];
- }
- }
- return NULL;
- }
- /*
- ================
- idPhysics_AF::GetBody
- ================
- */
- idAFBody *idPhysics_AF::GetBody( const int id ) const {
- if ( id < 0 || id >= bodies.Num() ) {
- gameLocal.Error( "GetBody: no body with id %d exists\n", id );
- return NULL;
- }
- return bodies[id];
- }
- /*
- ================
- idPhysics_AF::GetConstraint
- ================
- */
- idAFConstraint *idPhysics_AF::GetConstraint( const char *constraintName ) const {
- int i;
- for ( i = 0; i < constraints.Num(); i++ ) {
- if ( constraints[i]->name.Icmp( constraintName ) == 0 ) {
- return constraints[i];
- }
- }
- return NULL;
- }
- /*
- ================
- idPhysics_AF::GetConstraint
- ================
- */
- idAFConstraint *idPhysics_AF::GetConstraint( const int id ) const {
- if ( id < 0 || id >= constraints.Num() ) {
- gameLocal.Error( "GetConstraint: no constraint with id %d exists\n", id );
- return NULL;
- }
- return constraints[id];
- }
- /*
- ================
- idPhysics_AF::DeleteBody
- ================
- */
- void idPhysics_AF::DeleteBody( const char *bodyName ) {
- int i;
- // find the body with the given name
- for ( i = 0; i < bodies.Num(); i++ ) {
- if ( !bodies[i]->name.Icmp( bodyName ) ) {
- break;
- }
- }
- if ( i >= bodies.Num() ) {
- gameLocal.Warning( "DeleteBody: no body found in the articulated figure with the name '%s' for entity '%s' type '%s'.",
- bodyName, self->name.c_str(), self->GetType()->classname );
- return;
- }
- DeleteBody( i );
- }
- /*
- ================
- idPhysics_AF::DeleteBody
- ================
- */
- void idPhysics_AF::DeleteBody( const int id ) {
- int j;
- if ( id < 0 || id > bodies.Num() ) {
- gameLocal.Error( "DeleteBody: no body with id %d.", id );
- return;
- }
- // remove any constraints attached to this body
- for ( j = 0; j < constraints.Num(); j++ ) {
- if ( constraints[j]->body1 == bodies[id] || constraints[j]->body2 == bodies[id] ) {
- delete constraints[j];
- constraints.RemoveIndex( j );
- j--;
- }
- }
- // remove the body
- delete bodies[id];
- bodies.RemoveIndex( id );
- // set new body ids
- for ( j = 0; j < bodies.Num(); j++ ) {
- bodies[j]->clipModel->SetId( j );
- }
- changedAF = true;
- }
- /*
- ================
- idPhysics_AF::DeleteConstraint
- ================
- */
- void idPhysics_AF::DeleteConstraint( const char *constraintName ) {
- int i;
- // find the constraint with the given name
- for ( i = 0; i < constraints.Num(); i++ ) {
- if ( !constraints[i]->name.Icmp( constraintName ) ) {
- break;
- }
- }
- if ( i >= constraints.Num() ) {
- gameLocal.Warning( "DeleteConstraint: no constriant found in the articulated figure with the name '%s' for entity '%s' type '%s'.",
- constraintName, self->name.c_str(), self->GetType()->classname );
- return;
- }
- DeleteConstraint( i );
- }
- /*
- ================
- idPhysics_AF::DeleteConstraint
- ================
- */
- void idPhysics_AF::DeleteConstraint( const int id ) {
- if ( id < 0 || id >= constraints.Num() ) {
- gameLocal.Error( "DeleteConstraint: no constraint with id %d.", id );
- return;
- }
- // remove the constraint
- delete constraints[id];
- constraints.RemoveIndex( id );
- changedAF = true;
- }
- /*
- ================
- idPhysics_AF::GetBodyContactConstraints
- ================
- */
- int idPhysics_AF::GetBodyContactConstraints( const int id, idAFConstraint_Contact *contacts[], int maxContacts ) const {
- int i, numContacts;
- idAFBody *body;
- idAFConstraint_Contact *contact;
- if ( id < 0 || id >= bodies.Num() || maxContacts <= 0 ) {
- return 0;
- }
- numContacts = 0;
- body = bodies[id];
- for ( i = 0; i < contactConstraints.Num(); i++ ) {
- contact = contactConstraints[i];
- if ( contact->body1 == body || contact->body2 == body ) {
- contacts[numContacts++] = contact;
- if ( numContacts >= maxContacts ) {
- return numContacts;
- }
- }
- }
- return numContacts;
- }
- /*
- ================
- idPhysics_AF::SetDefaultFriction
- ================
- */
- void idPhysics_AF::SetDefaultFriction( float linear, float angular, float contact ) {
- if ( linear < 0.0f || linear > 1.0f ||
- angular < 0.0f || angular > 1.0f ||
- contact < 0.0f || contact > 1.0f ) {
- return;
- }
- linearFriction = linear;
- angularFriction = angular;
- contactFriction = contact;
- }
- /*
- ================
- idPhysics_AF::GetImpactInfo
- ================
- */
- void idPhysics_AF::GetImpactInfo( const int id, const idVec3 &point, impactInfo_t *info ) const {
- if ( id < 0 || id >= bodies.Num() ) {
- memset( info, 0, sizeof( *info ) );
- return;
- }
- info->invMass = 1.0f / bodies[id]->mass;
- info->invInertiaTensor = bodies[id]->current->worldAxis.Transpose() * bodies[id]->inverseInertiaTensor * bodies[id]->current->worldAxis;
- info->position = point - bodies[id]->current->worldOrigin;
- info->velocity = bodies[id]->current->spatialVelocity.SubVec3(0) + bodies[id]->current->spatialVelocity.SubVec3(1).Cross( info->position );
- }
- /*
- ================
- idPhysics_AF::ApplyImpulse
- ================
- */
- void idPhysics_AF::ApplyImpulse( const int id, const idVec3 &point, const idVec3 &impulse ) {
- if ( id < 0 || id >= bodies.Num() ) {
- return;
- }
- if ( noImpact || impulse.LengthSqr() < Square( impulseThreshold ) ) {
- return;
- }
- idMat3 invWorldInertiaTensor = bodies[id]->current->worldAxis.Transpose() * bodies[id]->inverseInertiaTensor * bodies[id]->current->worldAxis;
- bodies[id]->current->spatialVelocity.SubVec3(0) += bodies[id]->invMass * impulse;
- bodies[id]->current->spatialVelocity.SubVec3(1) += invWorldInertiaTensor * (point - bodies[id]->current->worldOrigin).Cross( impulse );
- Activate();
- }
- /*
- ================
- idPhysics_AF::AddForce
- ================
- */
- void idPhysics_AF::AddForce( const int id, const idVec3 &point, const idVec3 &force ) {
- if ( noImpact ) {
- return;
- }
- if ( id < 0 || id >= bodies.Num() ) {
- return;
- }
- bodies[id]->current->externalForce.SubVec3( 0 ) += force;
- bodies[id]->current->externalForce.SubVec3( 1 ) += (point - bodies[id]->current->worldOrigin).Cross( force );
- Activate();
- }
- /*
- ================
- idPhysics_AF::IsAtRest
- ================
- */
- bool idPhysics_AF::IsAtRest( void ) const {
- return current.atRest >= 0;
- }
- /*
- ================
- idPhysics_AF::GetRestStartTime
- ================
- */
- int idPhysics_AF::GetRestStartTime( void ) const {
- return current.atRest;
- }
- /*
- ================
- idPhysics_AF::IsPushable
- ================
- */
- bool idPhysics_AF::IsPushable( void ) const {
- return ( !noImpact && ( masterBody == NULL || forcePushable ) );
- }
- /*
- ================
- idPhysics_AF::SaveState
- ================
- */
- void idPhysics_AF::SaveState( void ) {
- int i;
- saved = current;
- for ( i = 0; i < bodies.Num(); i++ ) {
- memcpy( &bodies[i]->saved, bodies[i]->current, sizeof( AFBodyPState_t ) );
- }
- }
- /*
- ================
- idPhysics_AF::RestoreState
- ================
- */
- void idPhysics_AF::RestoreState( void ) {
- int i;
- current = saved;
- for ( i = 0; i < bodies.Num(); i++ ) {
- *(bodies[i]->current) = bodies[i]->saved;
- }
- EvaluateContacts();
- }
- /*
- ================
- idPhysics_AF::SetOrigin
- ================
- */
- void idPhysics_AF::SetOrigin( const idVec3 &newOrigin, int id ) {
- if ( masterBody ) {
- Translate( masterBody->current->worldOrigin + masterBody->current->worldAxis * newOrigin - bodies[0]->current->worldOrigin );
- } else {
- Translate( newOrigin - bodies[0]->current->worldOrigin );
- }
- }
- /*
- ================
- idPhysics_AF::SetAxis
- ================
- */
- void idPhysics_AF::SetAxis( const idMat3 &newAxis, int id ) {
- idMat3 axis;
- idRotation rotation;
- if ( masterBody ) {
- axis = bodies[0]->current->worldAxis.Transpose() * ( newAxis * masterBody->current->worldAxis );
- } else {
- axis = bodies[0]->current->worldAxis.Transpose() * newAxis;
- }
- rotation = axis.ToRotation();
- rotation.SetOrigin( bodies[0]->current->worldOrigin );
- Rotate( rotation );
- }
- /*
- ================
- idPhysics_AF::Translate
- ================
- */
- void idPhysics_AF::Translate( const idVec3 &translation, int id ) {
- int i;
- idAFBody *body;
- if ( !worldConstraintsLocked ) {
- // translate constraints attached to the world
- for ( i = 0; i < constraints.Num(); i++ ) {
- constraints[i]->Translate( translation );
- }
- }
- // translate all the bodies
- for ( i = 0; i < bodies.Num(); i++ ) {
- body = bodies[i];
- body->current->worldOrigin += translation;
- }
- Activate();
- UpdateClipModels();
- }
- /*
- ================
- idPhysics_AF::Rotate
- ================
- */
- void idPhysics_AF::Rotate( const idRotation &rotation, int id ) {
- int i;
- idAFBody *body;
- if ( !worldConstraintsLocked ) {
- // rotate constraints attached to the world
- for ( i = 0; i < constraints.Num(); i++ ) {
- constraints[i]->Rotate( rotation );
- }
- }
- // rotate all the bodies
- for ( i = 0; i < bodies.Num(); i++ ) {
- body = bodies[i];
- body->current->worldOrigin *= rotation;
- body->current->worldAxis *= rotation.ToMat3();
- }
- Activate();
- UpdateClipModels();
- }
- /*
- ================
- idPhysics_AF::GetOrigin
- ================
- */
- const idVec3 &idPhysics_AF::GetOrigin( int id ) const {
- if ( id < 0 || id >= bodies.Num() ) {
- return vec3_origin;
- }
- else {
- return bodies[id]->current->worldOrigin;
- }
- }
- /*
- ================
- idPhysics_AF::GetAxis
- ================
- */
- const idMat3 &idPhysics_AF::GetAxis( int id ) const {
- if ( id < 0 || id >= bodies.Num() ) {
- return mat3_identity;
- }
- else {
- return bodies[id]->current->worldAxis;
- }
- }
- /*
- ================
- idPhysics_AF::SetLinearVelocity
- ================
- */
- void idPhysics_AF::SetLinearVelocity( const idVec3 &newLinearVelocity, int id ) {
- if ( id < 0 || id >= bodies.Num() ) {
- return;
- }
- bodies[id]->current->spatialVelocity.SubVec3( 0 ) = newLinearVelocity;
- Activate();
- }
- /*
- ================
- idPhysics_AF::SetAngularVelocity
- ================
- */
- void idPhysics_AF::SetAngularVelocity( const idVec3 &newAngularVelocity, int id ) {
- if ( id < 0 || id >= bodies.Num() ) {
- return;
- }
- bodies[id]->current->spatialVelocity.SubVec3( 1 ) = newAngularVelocity;
- Activate();
- }
- /*
- ================
- idPhysics_AF::GetLinearVelocity
- ================
- */
- const idVec3 &idPhysics_AF::GetLinearVelocity( int id ) const {
- if ( id < 0 || id >= bodies.Num() ) {
- return vec3_origin;
- }
- else {
- return bodies[id]->current->spatialVelocity.SubVec3( 0 );
- }
- }
- /*
- ================
- idPhysics_AF::GetAngularVelocity
- ================
- */
- const idVec3 &idPhysics_AF::GetAngularVelocity( int id ) const {
- if ( id < 0 || id >= bodies.Num() ) {
- return vec3_origin;
- }
- else {
- return bodies[id]->current->spatialVelocity.SubVec3( 1 );
- }
- }
- /*
- ================
- idPhysics_AF::ClipTranslation
- ================
- */
- void idPhysics_AF::ClipTranslation( trace_t &results, const idVec3 &translation, const idClipModel *model ) const {
- int i;
- idAFBody *body;
- trace_t bodyResults;
- results.fraction = 1.0f;
- for ( i = 0; i < bodies.Num(); i++ ) {
- body = bodies[i];
- if ( body->clipModel->IsTraceModel() ) {
- if ( model ) {
- gameLocal.clip.TranslationModel( bodyResults, body->current->worldOrigin, body->current->worldOrigin + translation,
- body->clipModel, body->current->worldAxis, body->clipMask,
- model->Handle(), model->GetOrigin(), model->GetAxis() );
- }
- else {
- gameLocal.clip.Translation( bodyResults, body->current->worldOrigin, body->current->worldOrigin + translation,
- body->clipModel, body->current->worldAxis, body->clipMask, self );
- }
- if ( bodyResults.fraction < results.fraction ) {
- results = bodyResults;
- }
- }
- }
- results.endpos = bodies[0]->current->worldOrigin + results.fraction * translation;
- results.endAxis = bodies[0]->current->worldAxis;
- }
- /*
- ================
- idPhysics_AF::ClipRotation
- ================
- */
- void idPhysics_AF::ClipRotation( trace_t &results, const idRotation &rotation, const idClipModel *model ) const {
- int i;
- idAFBody *body;
- trace_t bodyResults;
- idRotation partialRotation;
- results.fraction = 1.0f;
- for ( i = 0; i < bodies.Num(); i++ ) {
- body = bodies[i];
- if ( body->clipModel->IsTraceModel() ) {
- if ( model ) {
- gameLocal.clip.RotationModel( bodyResults, body->current->worldOrigin, rotation,
- body->clipModel, body->current->worldAxis, body->clipMask,
- model->Handle(), model->GetOrigin(), model->GetAxis() );
- }
- else {
- gameLocal.clip.Rotation( bodyResults, body->current->worldOrigin, rotation,
- body->clipModel, body->current->worldAxis, body->clipMask, self );
- }
- if ( bodyResults.fraction < results.fraction ) {
- results = bodyResults;
- }
- }
- }
- partialRotation = rotation * results.fraction;
- results.endpos = bodies[0]->current->worldOrigin * partialRotation;
- results.endAxis = bodies[0]->current->worldAxis * partialRotation.ToMat3();
- }
- /*
- ================
- idPhysics_AF::ClipContents
- ================
- */
- int idPhysics_AF::ClipContents( const idClipModel *model ) const {
- int i, contents;
- idAFBody *body;
- contents = 0;
- for ( i = 0; i < bodies.Num(); i++ ) {
- body = bodies[i];
- if ( body->clipModel->IsTraceModel() ) {
- if ( model ) {
- contents |= gameLocal.clip.ContentsModel( body->current->worldOrigin,
- body->clipModel, body->current->worldAxis, -1,
- model->Handle(), model->GetOrigin(), model->GetAxis() );
- }
- else {
- contents |= gameLocal.clip.Contents( body->current->worldOrigin,
- body->clipModel, body->current->worldAxis, -1, NULL );
- }
- }
- }
- return contents;
- }
- /*
- ================
- idPhysics_AF::DisableClip
- ================
- */
- void idPhysics_AF::DisableClip( void ) {
- int i;
- for ( i = 0; i < bodies.Num(); i++ ) {
- bodies[i]->clipModel->Disable();
- }
- }
- /*
- ================
- idPhysics_AF::EnableClip
- ================
- */
- void idPhysics_AF::EnableClip( void ) {
- int i;
- for ( i = 0; i < bodies.Num(); i++ ) {
- bodies[i]->clipModel->Enable();
- }
- }
- /*
- ================
- idPhysics_AF::UnlinkClip
- ================
- */
- void idPhysics_AF::UnlinkClip( void ) {
- int i;
- for ( i = 0; i < bodies.Num(); i++ ) {
- bodies[i]->clipModel->Unlink();
- }
- }
- /*
- ================
- idPhysics_AF::LinkClip
- ================
- */
- void idPhysics_AF::LinkClip( void ) {
- UpdateClipModels();
- }
- /*
- ================
- idPhysics_AF::SetPushed
- ================
- */
- void idPhysics_AF::SetPushed( int deltaTime ) {
- idAFBody *body;
- idRotation rotation;
- if ( bodies.Num() ) {
- body = bodies[0];
- rotation = ( body->saved.worldAxis.Transpose() * body->current->worldAxis ).ToRotation();
- // velocity with which the af is pushed
- current.pushVelocity.SubVec3(0) += ( body->current->worldOrigin - body->saved.worldOrigin ) / ( deltaTime * idMath::M_MS2SEC );
- current.pushVelocity.SubVec3(1) += rotation.GetVec() * -DEG2RAD( rotation.GetAngle() ) / ( deltaTime * idMath::M_MS2SEC );
- }
- }
- /*
- ================
- idPhysics_AF::GetPushedLinearVelocity
- ================
- */
- const idVec3 &idPhysics_AF::GetPushedLinearVelocity( const int id ) const {
- return current.pushVelocity.SubVec3(0);
- }
- /*
- ================
- idPhysics_AF::GetPushedAngularVelocity
- ================
- */
- const idVec3 &idPhysics_AF::GetPushedAngularVelocity( const int id ) const {
- return current.pushVelocity.SubVec3(1);
- }
- /*
- ================
- idPhysics_AF::SetMaster
- the binding is orientated based on the constraints being used
- ================
- */
- void idPhysics_AF::SetMaster( idEntity *master, const bool orientated ) {
- int i;
- idVec3 masterOrigin;
- idMat3 masterAxis;
- idRotation rotation;
- if ( master ) {
- self->GetMasterPosition( masterOrigin, masterAxis );
- if ( !masterBody ) {
- masterBody = new idAFBody();
- // translate and rotate all the constraints with body2 == NULL from world space to master space
- rotation = masterAxis.Transpose().ToRotation();
- for ( i = 0; i < constraints.Num(); i++ ) {
- if ( constraints[i]->GetBody2() == NULL ) {
- constraints[i]->Translate( -masterOrigin );
- constraints[i]->Rotate( rotation );
- }
- }
- Activate();
- }
- masterBody->current->worldOrigin = masterOrigin;
- masterBody->current->worldAxis = masterAxis;
- }
- else {
- if ( masterBody ) {
- // translate and rotate all the constraints with body2 == NULL from master space to world space
- rotation = masterBody->current->worldAxis.ToRotation();
- for ( i = 0; i < constraints.Num(); i++ ) {
- if ( constraints[i]->GetBody2() == NULL ) {
- constraints[i]->Rotate( rotation );
- constraints[i]->Translate( masterBody->current->worldOrigin );
- }
- }
- delete masterBody;
- masterBody = NULL;
- Activate();
- }
- }
- }
- const float AF_VELOCITY_MAX = 16000;
- const int AF_VELOCITY_TOTAL_BITS = 16;
- const int AF_VELOCITY_EXPONENT_BITS = idMath::BitsForInteger( idMath::BitsForFloat( AF_VELOCITY_MAX ) ) + 1;
- const int AF_VELOCITY_MANTISSA_BITS = AF_VELOCITY_TOTAL_BITS - 1 - AF_VELOCITY_EXPONENT_BITS;
- const float AF_FORCE_MAX = 1e20f;
- const int AF_FORCE_TOTAL_BITS = 16;
- const int AF_FORCE_EXPONENT_BITS = idMath::BitsForInteger( idMath::BitsForFloat( AF_FORCE_MAX ) ) + 1;
- const int AF_FORCE_MANTISSA_BITS = AF_FORCE_TOTAL_BITS - 1 - AF_FORCE_EXPONENT_BITS;
- /*
- ================
- idPhysics_AF::WriteToSnapshot
- ================
- */
- void idPhysics_AF::WriteToSnapshot( idBitMsgDelta &msg ) const {
- int i;
- idCQuat quat;
- msg.WriteLong( current.atRest );
- msg.WriteFloat( current.noMoveTime );
- msg.WriteFloat( current.activateTime );
- msg.WriteDeltaFloat( 0.0f, current.pushVelocity[0], AF_VELOCITY_EXPONENT_BITS, AF_VELOCITY_MANTISSA_BITS );
- msg.WriteDeltaFloat( 0.0f, current.pushVelocity[1], AF_VELOCITY_EXPONENT_BITS, AF_VELOCITY_MANTISSA_BITS );
- msg.WriteDeltaFloat( 0.0f, current.pushVelocity[2], AF_VELOCITY_EXPONENT_BITS, AF_VELOCITY_MANTISSA_BITS );
- msg.WriteDeltaFloat( 0.0f, current.pushVelocity[3], AF_VELOCITY_EXPONENT_BITS, AF_VELOCITY_MANTISSA_BITS );
- msg.WriteDeltaFloat( 0.0f, current.pushVelocity[4], AF_VELOCITY_EXPONENT_BITS, AF_VELOCITY_MANTISSA_BITS );
- msg.WriteDeltaFloat( 0.0f, current.pushVelocity[5], AF_VELOCITY_EXPONENT_BITS, AF_VELOCITY_MANTISSA_BITS );
- msg.WriteByte( bodies.Num() );
- for ( i = 0; i < bodies.Num(); i++ ) {
- AFBodyPState_t *state = bodies[i]->current;
- quat = state->worldAxis.ToCQuat();
- msg.WriteFloat( state->worldOrigin[0] );
- msg.WriteFloat( state->worldOrigin[1] );
- msg.WriteFloat( state->worldOrigin[2] );
- msg.WriteFloat( quat.x );
- msg.WriteFloat( quat.y );
- msg.WriteFloat( quat.z );
- msg.WriteDeltaFloat( 0.0f, state->spatialVelocity[0], AF_VELOCITY_EXPONENT_BITS, AF_VELOCITY_MANTISSA_BITS );
- msg.WriteDeltaFloat( 0.0f, state->spatialVelocity[1], AF_VELOCITY_EXPONENT_BITS, AF_VELOCITY_MANTISSA_BITS );
- msg.WriteDeltaFloat( 0.0f, state->spatialVelocity[2], AF_VELOCITY_EXPONENT_BITS, AF_VELOCITY_MANTISSA_BITS );
- msg.WriteDeltaFloat( 0.0f, state->spatialVelocity[3], AF_VELOCITY_EXPONENT_BITS, AF_VELOCITY_MANTISSA_BITS );
- msg.WriteDeltaFloat( 0.0f, state->spatialVelocity[4], AF_VELOCITY_EXPONENT_BITS, AF_VELOCITY_MANTISSA_BITS );
- msg.WriteDeltaFloat( 0.0f, state->spatialVelocity[5], AF_VELOCITY_EXPONENT_BITS, AF_VELOCITY_MANTISSA_BITS );
- /* msg.WriteDeltaFloat( 0.0f, state->externalForce[0], AF_FORCE_EXPONENT_BITS, AF_FORCE_MANTISSA_BITS );
- msg.WriteDeltaFloat( 0.0f, state->externalForce[1], AF_FORCE_EXPONENT_BITS, AF_FORCE_MANTISSA_BITS );
- msg.WriteDeltaFloat( 0.0f, state->externalForce[2], AF_FORCE_EXPONENT_BITS, AF_FORCE_MANTISSA_BITS );
- msg.WriteDeltaFloat( 0.0f, state->externalForce[3], AF_FORCE_EXPONENT_BITS, AF_FORCE_MANTISSA_BITS );
- msg.WriteDeltaFloat( 0.0f, state->externalForce[4], AF_FORCE_EXPONENT_BITS, AF_FORCE_MANTISSA_BITS );
- msg.WriteDeltaFloat( 0.0f, state->externalForce[5], AF_FORCE_EXPONENT_BITS, AF_FORCE_MANTISSA_BITS );
- */
- }
- }
- /*
- ================
- idPhysics_AF::ReadFromSnapshot
- ================
- */
- void idPhysics_AF::ReadFromSnapshot( const idBitMsgDelta &msg ) {
- int i, num;
- idCQuat quat;
- current.atRest = msg.ReadLong();
- current.noMoveTime = msg.ReadFloat();
- current.activateTime = msg.ReadFloat();
- current.pushVelocity[0] = msg.ReadDeltaFloat( 0.0f, AF_VELOCITY_EXPONENT_BITS, AF_VELOCITY_MANTISSA_BITS );
- current.pushVelocity[1] = msg.ReadDeltaFloat( 0.0f, AF_VELOCITY_EXPONENT_BITS, AF_VELOCITY_MANTISSA_BITS );
- current.pushVelocity[2] = msg.ReadDeltaFloat( 0.0f, AF_VELOCITY_EXPONENT_BITS, AF_VELOCITY_MANTISSA_BITS );
- current.pushVelocity[3] = msg.ReadDeltaFloat( 0.0f, AF_VELOCITY_EXPONENT_BITS, AF_VELOCITY_MANTISSA_BITS );
- current.pushVelocity[4] = msg.ReadDeltaFloat( 0.0f, AF_VELOCITY_EXPONENT_BITS, AF_VELOCITY_MANTISSA_BITS );
- current.pushVelocity[5] = msg.ReadDeltaFloat( 0.0f, AF_VELOCITY_EXPONENT_BITS, AF_VELOCITY_MANTISSA_BITS );
- num = msg.ReadByte();
- assert( num == bodies.Num() );
- for ( i = 0; i < bodies.Num(); i++ ) {
- AFBodyPState_t *state = bodies[i]->current;
- state->worldOrigin[0] = msg.ReadFloat();
- state->worldOrigin[1] = msg.ReadFloat();
- state->worldOrigin[2] = msg.ReadFloat();
- quat.x = msg.ReadFloat();
- quat.y = msg.ReadFloat();
- quat.z = msg.ReadFloat();
- state->spatialVelocity[0] = msg.ReadDeltaFloat( 0.0f, AF_VELOCITY_EXPONENT_BITS, AF_VELOCITY_MANTISSA_BITS );
- state->spatialVelocity[1] = msg.ReadDeltaFloat( 0.0f, AF_VELOCITY_EXPONENT_BITS, AF_VELOCITY_MANTISSA_BITS );
- state->spatialVelocity[2] = msg.ReadDeltaFloat( 0.0f, AF_VELOCITY_EXPONENT_BITS, AF_VELOCITY_MANTISSA_BITS );
- state->spatialVelocity[3] = msg.ReadDeltaFloat( 0.0f, AF_VELOCITY_EXPONENT_BITS, AF_VELOCITY_MANTISSA_BITS );
- state->spatialVelocity[4] = msg.ReadDeltaFloat( 0.0f, AF_VELOCITY_EXPONENT_BITS, AF_VELOCITY_MANTISSA_BITS );
- state->spatialVelocity[5] = msg.ReadDeltaFloat( 0.0f, AF_VELOCITY_EXPONENT_BITS, AF_VELOCITY_MANTISSA_BITS );
- /* state->externalForce[0] = msg.ReadDeltaFloat( 0.0f, AF_FORCE_EXPONENT_BITS, AF_FORCE_MANTISSA_BITS );
- state->externalForce[1] = msg.ReadDeltaFloat( 0.0f, AF_FORCE_EXPONENT_BITS, AF_FORCE_MANTISSA_BITS );
- state->externalForce[2] = msg.ReadDeltaFloat( 0.0f, AF_FORCE_EXPONENT_BITS, AF_FORCE_MANTISSA_BITS );
- state->externalForce[3] = msg.ReadDeltaFloat( 0.0f, AF_FORCE_EXPONENT_BITS, AF_FORCE_MANTISSA_BITS );
- state->externalForce[4] = msg.ReadDeltaFloat( 0.0f, AF_FORCE_EXPONENT_BITS, AF_FORCE_MANTISSA_BITS );
- state->externalForce[5] = msg.ReadDeltaFloat( 0.0f, AF_FORCE_EXPONENT_BITS, AF_FORCE_MANTISSA_BITS );
- */
- state->worldAxis = quat.ToMat3();
- }
- UpdateClipModels();
- }
|