Physics_AF.cpp 205 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007
  1. /*
  2. ===========================================================================
  3. Doom 3 GPL Source Code
  4. Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
  6. Doom 3 Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 Source Code is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. 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.
  17. 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.
  18. ===========================================================================
  19. */
  20. #include "../../idlib/precompiled.h"
  21. #pragma hdrstop
  22. #include "../Game_local.h"
  23. CLASS_DECLARATION( idPhysics_Base, idPhysics_AF )
  24. END_CLASS
  25. const float ERROR_REDUCTION = 0.5f;
  26. const float ERROR_REDUCTION_MAX = 256.0f;
  27. const float LIMIT_ERROR_REDUCTION = 0.3f;
  28. const float LCP_EPSILON = 1e-7f;
  29. const float LIMIT_LCP_EPSILON = 1e-4f;
  30. const float CONTACT_LCP_EPSILON = 1e-6f;
  31. const float CENTER_OF_MASS_EPSILON = 1e-4f;
  32. const float NO_MOVE_TIME = 1.0f;
  33. const float NO_MOVE_TRANSLATION_TOLERANCE = 10.0f;
  34. const float NO_MOVE_ROTATION_TOLERANCE = 10.0f;
  35. const float MIN_MOVE_TIME = -1.0f;
  36. const float MAX_MOVE_TIME = -1.0f;
  37. const float IMPULSE_THRESHOLD = 500.0f;
  38. const float SUSPEND_LINEAR_VELOCITY = 10.0f;
  39. const float SUSPEND_ANGULAR_VELOCITY = 15.0f;
  40. const float SUSPEND_LINEAR_ACCELERATION = 20.0f;
  41. const float SUSPEND_ANGULAR_ACCELERATION = 30.0f;
  42. const idVec6 vec6_lcp_epsilon = idVec6( LCP_EPSILON, LCP_EPSILON, LCP_EPSILON,
  43. LCP_EPSILON, LCP_EPSILON, LCP_EPSILON );
  44. #define AF_TIMINGS
  45. #ifdef AF_TIMINGS
  46. static int lastTimerReset = 0;
  47. static int numArticulatedFigures = 0;
  48. static idTimer timer_total, timer_pc, timer_ac, timer_collision, timer_lcp;
  49. #endif
  50. //===============================================================
  51. //
  52. // idAFConstraint
  53. //
  54. //===============================================================
  55. /*
  56. ================
  57. idAFConstraint::idAFConstraint
  58. ================
  59. */
  60. idAFConstraint::idAFConstraint( void ) {
  61. type = CONSTRAINT_INVALID;
  62. name = "noname";
  63. body1 = NULL;
  64. body2 = NULL;
  65. physics = NULL;
  66. lo.Zero( 6 );
  67. lo.SubVec6(0) = -vec6_infinity;
  68. hi.Zero( 6 );
  69. hi.SubVec6(0) = vec6_infinity;
  70. e.SetSize( 6 );
  71. e.SubVec6(0) = vec6_lcp_epsilon;
  72. boxConstraint = NULL;
  73. boxIndex[0] = -1;
  74. boxIndex[1] = -1;
  75. boxIndex[2] = -1;
  76. boxIndex[3] = -1;
  77. boxIndex[4] = -1;
  78. boxIndex[5] = -1;
  79. firstIndex = 0;
  80. memset( &fl, 0, sizeof( fl ) );
  81. }
  82. /*
  83. ================
  84. idAFConstraint::~idAFConstraint
  85. ================
  86. */
  87. idAFConstraint::~idAFConstraint( void ) {
  88. }
  89. /*
  90. ================
  91. idAFConstraint::SetBody1
  92. ================
  93. */
  94. void idAFConstraint::SetBody1( idAFBody *body ) {
  95. if ( body1 != body) {
  96. body1 = body;
  97. if ( physics ) {
  98. physics->SetChanged();
  99. }
  100. }
  101. }
  102. /*
  103. ================
  104. idAFConstraint::SetBody2
  105. ================
  106. */
  107. void idAFConstraint::SetBody2( idAFBody *body ) {
  108. if ( body2 != body ) {
  109. body2 = body;
  110. if ( physics ) {
  111. physics->SetChanged();
  112. }
  113. }
  114. }
  115. /*
  116. ================
  117. idAFConstraint::GetMultiplier
  118. ================
  119. */
  120. const idVecX &idAFConstraint::GetMultiplier( void ) {
  121. return lm;
  122. }
  123. /*
  124. ================
  125. idAFConstraint::Evaluate
  126. ================
  127. */
  128. void idAFConstraint::Evaluate( float invTimeStep ) {
  129. assert( 0 );
  130. }
  131. /*
  132. ================
  133. idAFConstraint::ApplyFriction
  134. ================
  135. */
  136. void idAFConstraint::ApplyFriction( float invTimeStep ) {
  137. }
  138. /*
  139. ================
  140. idAFConstraint::GetForce
  141. ================
  142. */
  143. void idAFConstraint::GetForce( idAFBody *body, idVec6 &force ) {
  144. idVecX v;
  145. v.SetData( 6, VECX_ALLOCA( 6 ) );
  146. if ( body == body1 ) {
  147. J1.TransposeMultiply( v, lm );
  148. }
  149. else if ( body == body2 ) {
  150. J2.TransposeMultiply( v, lm );
  151. }
  152. else {
  153. v.Zero();
  154. }
  155. force[0] = v[0]; force[1] = v[1]; force[2] = v[2]; force[3] = v[3]; force[4] = v[4]; force[5] = v[5];
  156. }
  157. /*
  158. ================
  159. idAFConstraint::Translate
  160. ================
  161. */
  162. void idAFConstraint::Translate( const idVec3 &translation ) {
  163. assert( 0 );
  164. }
  165. /*
  166. ================
  167. idAFConstraint::Rotate
  168. ================
  169. */
  170. void idAFConstraint::Rotate( const idRotation &rotation ) {
  171. assert( 0 );
  172. }
  173. /*
  174. ================
  175. idAFConstraint::GetCenter
  176. ================
  177. */
  178. void idAFConstraint::GetCenter( idVec3 &center ) {
  179. center.Zero();
  180. }
  181. /*
  182. ================
  183. idAFConstraint::DebugDraw
  184. ================
  185. */
  186. void idAFConstraint::DebugDraw( void ) {
  187. }
  188. /*
  189. ================
  190. idAFConstraint::InitSize
  191. ================
  192. */
  193. void idAFConstraint::InitSize( int size ) {
  194. J1.Zero( size, 6 );
  195. J2.Zero( size, 6 );
  196. c1.Zero( size );
  197. c2.Zero( size );
  198. s.Zero( size );
  199. lm.Zero( size );
  200. }
  201. /*
  202. ================
  203. idAFConstraint::Save
  204. ================
  205. */
  206. void idAFConstraint::Save( idSaveGame *saveFile ) const {
  207. saveFile->WriteInt( type );
  208. }
  209. /*
  210. ================
  211. idAFConstraint::Restore
  212. ================
  213. */
  214. void idAFConstraint::Restore( idRestoreGame *saveFile ) {
  215. constraintType_t t;
  216. saveFile->ReadInt( (int &)t );
  217. assert( t == type );
  218. }
  219. //===============================================================
  220. //
  221. // idAFConstraint_Fixed
  222. //
  223. //===============================================================
  224. /*
  225. ================
  226. idAFConstraint_Fixed::idAFConstraint_Fixed
  227. ================
  228. */
  229. idAFConstraint_Fixed::idAFConstraint_Fixed( const idStr &name, idAFBody *body1, idAFBody *body2 ) {
  230. assert( body1 );
  231. type = CONSTRAINT_FIXED;
  232. this->name = name;
  233. this->body1 = body1;
  234. this->body2 = body2;
  235. InitSize( 6 );
  236. fl.allowPrimary = true;
  237. fl.noCollision = true;
  238. InitOffset();
  239. }
  240. /*
  241. ================
  242. idAFConstraint_Fixed::InitOffset
  243. ================
  244. */
  245. void idAFConstraint_Fixed::InitOffset( void ) {
  246. if ( body2 ) {
  247. offset = ( body1->GetWorldOrigin() - body2->GetWorldOrigin() ) * body2->GetWorldAxis().Transpose();
  248. relAxis = body1->GetWorldAxis() * body2->GetWorldAxis().Transpose();
  249. }
  250. else {
  251. offset = body1->GetWorldOrigin();
  252. relAxis = body1->GetWorldAxis();
  253. }
  254. }
  255. /*
  256. ================
  257. idAFConstraint_Fixed::SetBody1
  258. ================
  259. */
  260. void idAFConstraint_Fixed::SetBody1( idAFBody *body ) {
  261. if ( body1 != body) {
  262. body1 = body;
  263. InitOffset();
  264. if ( physics ) {
  265. physics->SetChanged();
  266. }
  267. }
  268. }
  269. /*
  270. ================
  271. idAFConstraint_Fixed::SetBody2
  272. ================
  273. */
  274. void idAFConstraint_Fixed::SetBody2( idAFBody *body ) {
  275. if ( body2 != body ) {
  276. body2 = body;
  277. InitOffset();
  278. if ( physics ) {
  279. physics->SetChanged();
  280. }
  281. }
  282. }
  283. /*
  284. ================
  285. idAFConstraint_Fixed::Evaluate
  286. ================
  287. */
  288. void idAFConstraint_Fixed::Evaluate( float invTimeStep ) {
  289. idVec3 ofs, a2;
  290. idMat3 ax;
  291. idRotation r;
  292. idAFBody *master;
  293. master = body2 ? body2 : physics->GetMasterBody();
  294. if ( master ) {
  295. a2 = offset * master->GetWorldAxis();
  296. ofs = a2 + master->GetWorldOrigin();
  297. ax = relAxis * master->GetWorldAxis();
  298. }
  299. else {
  300. a2.Zero();
  301. ofs = offset;
  302. ax = relAxis;
  303. }
  304. J1.Set( mat3_identity, mat3_zero,
  305. mat3_zero, mat3_identity );
  306. if ( body2 ) {
  307. J2.Set( -mat3_identity, SkewSymmetric( a2 ),
  308. mat3_zero, -mat3_identity );
  309. }
  310. else {
  311. J2.Zero( 6, 6 );
  312. }
  313. c1.SubVec3(0) = -( invTimeStep * ERROR_REDUCTION ) * ( ofs - body1->GetWorldOrigin() );
  314. r = ( body1->GetWorldAxis().Transpose() * ax ).ToRotation();
  315. c1.SubVec3(1) = -( invTimeStep * ERROR_REDUCTION ) * ( r.GetVec() * -(float) DEG2RAD( r.GetAngle() ) );
  316. c1.Clamp( -ERROR_REDUCTION_MAX, ERROR_REDUCTION_MAX );
  317. }
  318. /*
  319. ================
  320. idAFConstraint_Fixed::ApplyFriction
  321. ================
  322. */
  323. void idAFConstraint_Fixed::ApplyFriction( float invTimeStep ) {
  324. // no friction
  325. }
  326. /*
  327. ================
  328. idAFConstraint_Fixed::Translate
  329. ================
  330. */
  331. void idAFConstraint_Fixed::Translate( const idVec3 &translation ) {
  332. if ( !body2 ) {
  333. offset += translation;
  334. }
  335. }
  336. /*
  337. ================
  338. idAFConstraint_Fixed::Rotate
  339. ================
  340. */
  341. void idAFConstraint_Fixed::Rotate( const idRotation &rotation ) {
  342. if ( !body2 ) {
  343. offset *= rotation;
  344. relAxis *= rotation.ToMat3();
  345. }
  346. }
  347. /*
  348. ================
  349. idAFConstraint_Fixed::GetCenter
  350. ================
  351. */
  352. void idAFConstraint_Fixed::GetCenter( idVec3 &center ) {
  353. center = body1->GetWorldOrigin();
  354. }
  355. /*
  356. ================
  357. idAFConstraint_Fixed::DebugDraw
  358. ================
  359. */
  360. void idAFConstraint_Fixed::DebugDraw( void ) {
  361. idAFBody *master;
  362. master = body2 ? body2 : physics->GetMasterBody();
  363. if ( master ) {
  364. gameRenderWorld->DebugLine( colorRed, body1->GetWorldOrigin(), master->GetWorldOrigin() );
  365. }
  366. else {
  367. gameRenderWorld->DebugLine( colorRed, body1->GetWorldOrigin(), vec3_origin );
  368. }
  369. }
  370. /*
  371. ================
  372. idAFConstraint_Fixed::Save
  373. ================
  374. */
  375. void idAFConstraint_Fixed::Save( idSaveGame *saveFile ) const {
  376. idAFConstraint::Save( saveFile );
  377. saveFile->WriteVec3( offset );
  378. saveFile->WriteMat3( relAxis );
  379. }
  380. /*
  381. ================
  382. idAFConstraint_Fixed::Restore
  383. ================
  384. */
  385. void idAFConstraint_Fixed::Restore( idRestoreGame *saveFile ) {
  386. idAFConstraint::Restore( saveFile );
  387. saveFile->ReadVec3( offset );
  388. saveFile->ReadMat3( relAxis );
  389. }
  390. //===============================================================
  391. //
  392. // idAFConstraint_BallAndSocketJoint
  393. //
  394. //===============================================================
  395. /*
  396. ================
  397. idAFConstraint_BallAndSocketJoint::idAFConstraint_BallAndSocketJoint
  398. ================
  399. */
  400. idAFConstraint_BallAndSocketJoint::idAFConstraint_BallAndSocketJoint( const idStr &name, idAFBody *body1, idAFBody *body2 ) {
  401. assert( body1 );
  402. type = CONSTRAINT_BALLANDSOCKETJOINT;
  403. this->name = name;
  404. this->body1 = body1;
  405. this->body2 = body2;
  406. InitSize( 3 );
  407. coneLimit = NULL;
  408. pyramidLimit = NULL;
  409. friction = 0.0f;
  410. fc = NULL;
  411. fl.allowPrimary = true;
  412. fl.noCollision = true;
  413. }
  414. /*
  415. ================
  416. idAFConstraint_BallAndSocketJoint::~idAFConstraint_BallAndSocketJoint
  417. ================
  418. */
  419. idAFConstraint_BallAndSocketJoint::~idAFConstraint_BallAndSocketJoint( void ) {
  420. if ( coneLimit ) {
  421. delete coneLimit;
  422. }
  423. if ( pyramidLimit ) {
  424. delete pyramidLimit;
  425. }
  426. }
  427. /*
  428. ================
  429. idAFConstraint_BallAndSocketJoint::SetAnchor
  430. ================
  431. */
  432. void idAFConstraint_BallAndSocketJoint::SetAnchor( const idVec3 &worldPosition ) {
  433. // get anchor relative to center of mass of body1
  434. anchor1 = ( worldPosition - body1->GetWorldOrigin() ) * body1->GetWorldAxis().Transpose();
  435. if ( body2 ) {
  436. // get anchor relative to center of mass of body2
  437. anchor2 = ( worldPosition - body2->GetWorldOrigin() ) * body2->GetWorldAxis().Transpose();
  438. }
  439. else {
  440. anchor2 = worldPosition;
  441. }
  442. if ( coneLimit ) {
  443. coneLimit->SetAnchor( anchor2 );
  444. }
  445. if ( pyramidLimit ) {
  446. pyramidLimit->SetAnchor( anchor2 );
  447. }
  448. }
  449. /*
  450. ================
  451. idAFConstraint_BallAndSocketJoint::GetAnchor
  452. ================
  453. */
  454. idVec3 idAFConstraint_BallAndSocketJoint::GetAnchor( void ) const {
  455. if ( body2 ) {
  456. return body2->GetWorldOrigin() + body2->GetWorldAxis() * anchor2;
  457. }
  458. return anchor2;
  459. }
  460. /*
  461. ================
  462. idAFConstraint_BallAndSocketJoint::SetNoLimit
  463. ================
  464. */
  465. void idAFConstraint_BallAndSocketJoint::SetNoLimit( void ) {
  466. if ( coneLimit ) {
  467. delete coneLimit;
  468. coneLimit = NULL;
  469. }
  470. if ( pyramidLimit ) {
  471. delete pyramidLimit;
  472. pyramidLimit = NULL;
  473. }
  474. }
  475. /*
  476. ================
  477. idAFConstraint_BallAndSocketJoint::SetConeLimit
  478. ================
  479. */
  480. void idAFConstraint_BallAndSocketJoint::SetConeLimit( const idVec3 &coneAxis, const float coneAngle, const idVec3 &body1Axis ) {
  481. if ( pyramidLimit ) {
  482. delete pyramidLimit;
  483. pyramidLimit = NULL;
  484. }
  485. if ( !coneLimit ) {
  486. coneLimit = new idAFConstraint_ConeLimit;
  487. coneLimit->SetPhysics( physics );
  488. }
  489. if ( body2 ) {
  490. coneLimit->Setup( body1, body2, anchor2, coneAxis * body2->GetWorldAxis().Transpose(), coneAngle, body1Axis * body1->GetWorldAxis().Transpose() );
  491. }
  492. else {
  493. coneLimit->Setup( body1, body2, anchor2, coneAxis, coneAngle, body1Axis * body1->GetWorldAxis().Transpose() );
  494. }
  495. }
  496. /*
  497. ================
  498. idAFConstraint_BallAndSocketJoint::SetPyramidLimit
  499. ================
  500. */
  501. void idAFConstraint_BallAndSocketJoint::SetPyramidLimit( const idVec3 &pyramidAxis, const idVec3 &baseAxis,
  502. const float angle1, const float angle2, const idVec3 &body1Axis ) {
  503. if ( coneLimit ) {
  504. delete coneLimit;
  505. coneLimit = NULL;
  506. }
  507. if ( !pyramidLimit ) {
  508. pyramidLimit = new idAFConstraint_PyramidLimit;
  509. pyramidLimit->SetPhysics( physics );
  510. }
  511. if ( body2 ) {
  512. pyramidLimit->Setup( body1, body2, anchor2, pyramidAxis * body2->GetWorldAxis().Transpose(),
  513. baseAxis * body2->GetWorldAxis().Transpose(), angle1, angle2,
  514. body1Axis * body1->GetWorldAxis().Transpose() );
  515. }
  516. else {
  517. pyramidLimit->Setup( body1, body2, anchor2, pyramidAxis, baseAxis, angle1, angle2,
  518. body1Axis * body1->GetWorldAxis().Transpose() );
  519. }
  520. }
  521. /*
  522. ================
  523. idAFConstraint_BallAndSocketJoint::SetLimitEpsilon
  524. ================
  525. */
  526. void idAFConstraint_BallAndSocketJoint::SetLimitEpsilon( const float e ) {
  527. if ( coneLimit ) {
  528. coneLimit->SetEpsilon( e );
  529. }
  530. if ( pyramidLimit ) {
  531. pyramidLimit->SetEpsilon( e );
  532. }
  533. }
  534. /*
  535. ================
  536. idAFConstraint_BallAndSocketJoint::GetFriction
  537. ================
  538. */
  539. float idAFConstraint_BallAndSocketJoint::GetFriction( void ) const {
  540. if ( af_forceFriction.GetFloat() > 0.0f ) {
  541. return af_forceFriction.GetFloat();
  542. }
  543. return friction * physics->GetJointFrictionScale();
  544. }
  545. /*
  546. ================
  547. idAFConstraint_BallAndSocketJoint::Evaluate
  548. ================
  549. */
  550. void idAFConstraint_BallAndSocketJoint::Evaluate( float invTimeStep ) {
  551. idVec3 a1, a2;
  552. idAFBody *master;
  553. master = body2 ? body2 : physics->GetMasterBody();
  554. a1 = anchor1 * body1->GetWorldAxis();
  555. if ( master ) {
  556. a2 = anchor2 * master->GetWorldAxis();
  557. c1.SubVec3(0) = -( invTimeStep * ERROR_REDUCTION ) * ( a2 + master->GetWorldOrigin() - ( a1 + body1->GetWorldOrigin() ) );
  558. }
  559. else {
  560. c1.SubVec3(0) = -( invTimeStep * ERROR_REDUCTION ) * ( anchor2 - ( a1 + body1->GetWorldOrigin() ) );
  561. }
  562. c1.Clamp( -ERROR_REDUCTION_MAX, ERROR_REDUCTION_MAX );
  563. J1.Set( mat3_identity, -SkewSymmetric( a1 ) );
  564. if ( body2 ) {
  565. J2.Set( -mat3_identity, SkewSymmetric( a2 ) );
  566. }
  567. else {
  568. J2.Zero( 3, 6 );
  569. }
  570. if ( coneLimit ) {
  571. coneLimit->Add( physics, invTimeStep );
  572. }
  573. else if ( pyramidLimit ) {
  574. pyramidLimit->Add( physics, invTimeStep );
  575. }
  576. }
  577. /*
  578. ================
  579. idAFConstraint_BallAndSocketJoint::ApplyFriction
  580. ================
  581. */
  582. void idAFConstraint_BallAndSocketJoint::ApplyFriction( float invTimeStep ) {
  583. idVec3 angular;
  584. float invMass, currentFriction;
  585. currentFriction = GetFriction();
  586. if ( currentFriction <= 0.0f ) {
  587. return;
  588. }
  589. if ( af_useImpulseFriction.GetBool() || af_useJointImpulseFriction.GetBool() ) {
  590. angular = body1->GetAngularVelocity();
  591. invMass = body1->GetInverseMass();
  592. if ( body2 ) {
  593. angular -= body2->GetAngularVelocity();
  594. invMass += body2->GetInverseMass();
  595. }
  596. angular *= currentFriction / invMass;
  597. body1->SetAngularVelocity( body1->GetAngularVelocity() - angular * body1->GetInverseMass() );
  598. if ( body2 ) {
  599. body2->SetAngularVelocity( body2->GetAngularVelocity() + angular * body2->GetInverseMass() );
  600. }
  601. }
  602. else {
  603. if ( !fc ) {
  604. fc = new idAFConstraint_BallAndSocketJointFriction;
  605. fc->Setup( this );
  606. }
  607. fc->Add( physics, invTimeStep );
  608. }
  609. }
  610. /*
  611. ================
  612. idAFConstraint_BallAndSocketJoint::GetForce
  613. ================
  614. */
  615. void idAFConstraint_BallAndSocketJoint::GetForce( idAFBody *body, idVec6 &force ) {
  616. idAFConstraint::GetForce( body, force );
  617. // FIXME: add limit force
  618. }
  619. /*
  620. ================
  621. idAFConstraint_BallAndSocketJoint::Translate
  622. ================
  623. */
  624. void idAFConstraint_BallAndSocketJoint::Translate( const idVec3 &translation ) {
  625. if ( !body2 ) {
  626. anchor2 += translation;
  627. }
  628. if ( coneLimit ) {
  629. coneLimit->Translate( translation );
  630. }
  631. else if ( pyramidLimit ) {
  632. pyramidLimit->Translate( translation );
  633. }
  634. }
  635. /*
  636. ================
  637. idAFConstraint_BallAndSocketJoint::Rotate
  638. ================
  639. */
  640. void idAFConstraint_BallAndSocketJoint::Rotate( const idRotation &rotation ) {
  641. if ( !body2 ) {
  642. anchor2 *= rotation;
  643. }
  644. if ( coneLimit ) {
  645. coneLimit->Rotate( rotation );
  646. }
  647. else if ( pyramidLimit ) {
  648. pyramidLimit->Rotate( rotation );
  649. }
  650. }
  651. /*
  652. ================
  653. idAFConstraint_BallAndSocketJoint::GetCenter
  654. ================
  655. */
  656. void idAFConstraint_BallAndSocketJoint::GetCenter( idVec3 &center ) {
  657. center = body1->GetWorldOrigin() + anchor1 * body1->GetWorldAxis();
  658. }
  659. /*
  660. ================
  661. idAFConstraint_BallAndSocketJoint::DebugDraw
  662. ================
  663. */
  664. void idAFConstraint_BallAndSocketJoint::DebugDraw( void ) {
  665. idVec3 a1 = body1->GetWorldOrigin() + anchor1 * body1->GetWorldAxis();
  666. gameRenderWorld->DebugLine( colorBlue, a1 - idVec3( 5, 0, 0 ), a1 + idVec3( 5, 0, 0 ) );
  667. gameRenderWorld->DebugLine( colorBlue, a1 - idVec3( 0, 5, 0 ), a1 + idVec3( 0, 5, 0 ) );
  668. gameRenderWorld->DebugLine( colorBlue, a1 - idVec3( 0, 0, 5 ), a1 + idVec3( 0, 0, 5 ) );
  669. if ( af_showLimits.GetBool() ) {
  670. if ( coneLimit ) {
  671. coneLimit->DebugDraw();
  672. }
  673. if ( pyramidLimit ) {
  674. pyramidLimit->DebugDraw();
  675. }
  676. }
  677. }
  678. /*
  679. ================
  680. idAFConstraint_BallAndSocketJoint::Save
  681. ================
  682. */
  683. void idAFConstraint_BallAndSocketJoint::Save( idSaveGame *saveFile ) const {
  684. idAFConstraint::Save( saveFile );
  685. saveFile->WriteVec3( anchor1 );
  686. saveFile->WriteVec3( anchor2 );
  687. saveFile->WriteFloat( friction );
  688. if ( coneLimit ) {
  689. coneLimit->Save( saveFile );
  690. }
  691. if ( pyramidLimit ) {
  692. pyramidLimit->Save( saveFile );
  693. }
  694. }
  695. /*
  696. ================
  697. idAFConstraint_BallAndSocketJoint::Restore
  698. ================
  699. */
  700. void idAFConstraint_BallAndSocketJoint::Restore( idRestoreGame *saveFile ) {
  701. idAFConstraint::Restore( saveFile );
  702. saveFile->ReadVec3( anchor1 );
  703. saveFile->ReadVec3( anchor2 );
  704. saveFile->ReadFloat( friction );
  705. if ( coneLimit ) {
  706. coneLimit->Restore( saveFile );
  707. }
  708. if ( pyramidLimit ) {
  709. pyramidLimit->Restore( saveFile );
  710. }
  711. }
  712. //===============================================================
  713. //
  714. // idAFConstraint_BallAndSocketJointFriction
  715. //
  716. //===============================================================
  717. /*
  718. ================
  719. idAFConstraint_BallAndSocketJointFriction::idAFConstraint_BallAndSocketJointFriction
  720. ================
  721. */
  722. idAFConstraint_BallAndSocketJointFriction::idAFConstraint_BallAndSocketJointFriction( void ) {
  723. type = CONSTRAINT_FRICTION;
  724. name = "ballAndSocketJointFriction";
  725. InitSize( 3 );
  726. joint = NULL;
  727. fl.allowPrimary = false;
  728. fl.frameConstraint = true;
  729. }
  730. /*
  731. ================
  732. idAFConstraint_BallAndSocketJointFriction::Setup
  733. ================
  734. */
  735. void idAFConstraint_BallAndSocketJointFriction::Setup( idAFConstraint_BallAndSocketJoint *bsj ) {
  736. this->joint = bsj;
  737. body1 = bsj->GetBody1();
  738. body2 = bsj->GetBody2();
  739. }
  740. /*
  741. ================
  742. idAFConstraint_BallAndSocketJointFriction::Evaluate
  743. ================
  744. */
  745. void idAFConstraint_BallAndSocketJointFriction::Evaluate( float invTimeStep ) {
  746. // do nothing
  747. }
  748. /*
  749. ================
  750. idAFConstraint_BallAndSocketJointFriction::ApplyFriction
  751. ================
  752. */
  753. void idAFConstraint_BallAndSocketJointFriction::ApplyFriction( float invTimeStep ) {
  754. // do nothing
  755. }
  756. /*
  757. ================
  758. idAFConstraint_BallAndSocketJointFriction::Add
  759. ================
  760. */
  761. bool idAFConstraint_BallAndSocketJointFriction::Add( idPhysics_AF *phys, float invTimeStep ) {
  762. float f;
  763. physics = phys;
  764. f = joint->GetFriction() * joint->GetMultiplier().Length();
  765. if ( f == 0.0f ) {
  766. return false;
  767. }
  768. lo[0] = lo[1] = lo[2] = -f;
  769. hi[0] = hi[1] = hi[2] = f;
  770. J1.Zero( 3, 6 );
  771. J1[0][3] = J1[1][4] = J1[2][5] = 1.0f;
  772. if ( body2 ) {
  773. J2.Zero( 3, 6 );
  774. J2[0][3] = J2[1][4] = J2[2][5] = 1.0f;
  775. }
  776. physics->AddFrameConstraint( this );
  777. return true;
  778. }
  779. /*
  780. ================
  781. idAFConstraint_BallAndSocketJointFriction::Translate
  782. ================
  783. */
  784. void idAFConstraint_BallAndSocketJointFriction::Translate( const idVec3 &translation ) {
  785. }
  786. /*
  787. ================
  788. idAFConstraint_BallAndSocketJointFriction::Rotate
  789. ================
  790. */
  791. void idAFConstraint_BallAndSocketJointFriction::Rotate( const idRotation &rotation ) {
  792. }
  793. //===============================================================
  794. //
  795. // idAFConstraint_UniversalJoint
  796. //
  797. //===============================================================
  798. /*
  799. ================
  800. idAFConstraint_UniversalJoint::idAFConstraint_UniversalJoint
  801. ================
  802. */
  803. idAFConstraint_UniversalJoint::idAFConstraint_UniversalJoint( const idStr &name, idAFBody *body1, idAFBody *body2 ) {
  804. assert( body1 );
  805. type = CONSTRAINT_UNIVERSALJOINT;
  806. this->name = name;
  807. this->body1 = body1;
  808. this->body2 = body2;
  809. InitSize( 4 );
  810. coneLimit = NULL;
  811. pyramidLimit = NULL;
  812. friction = 0.0f;
  813. fc = NULL;
  814. fl.allowPrimary = true;
  815. fl.noCollision = true;
  816. }
  817. /*
  818. ================
  819. idAFConstraint_UniversalJoint::~idAFConstraint_UniversalJoint
  820. ================
  821. */
  822. idAFConstraint_UniversalJoint::~idAFConstraint_UniversalJoint( void ) {
  823. if ( coneLimit ) {
  824. delete coneLimit;
  825. }
  826. if ( pyramidLimit ) {
  827. delete pyramidLimit;
  828. }
  829. if ( fc ) {
  830. delete fc;
  831. }
  832. }
  833. /*
  834. ================
  835. idAFConstraint_UniversalJoint::SetAnchor
  836. ================
  837. */
  838. void idAFConstraint_UniversalJoint::SetAnchor( const idVec3 &worldPosition ) {
  839. // get anchor relative to center of mass of body1
  840. anchor1 = ( worldPosition - body1->GetWorldOrigin() ) * body1->GetWorldAxis().Transpose();
  841. if ( body2 ) {
  842. // get anchor relative to center of mass of body2
  843. anchor2 = ( worldPosition - body2->GetWorldOrigin() ) * body2->GetWorldAxis().Transpose();
  844. }
  845. else {
  846. anchor2 = worldPosition;
  847. }
  848. if ( coneLimit ) {
  849. coneLimit->SetAnchor( anchor2 );
  850. }
  851. if ( pyramidLimit ) {
  852. pyramidLimit->SetAnchor( anchor2 );
  853. }
  854. }
  855. /*
  856. ================
  857. idAFConstraint_UniversalJoint::GetAnchor
  858. ================
  859. */
  860. idVec3 idAFConstraint_UniversalJoint::GetAnchor( void ) const {
  861. if ( body2 ) {
  862. return body2->GetWorldOrigin() + body2->GetWorldAxis() * anchor2;
  863. }
  864. return anchor2;
  865. }
  866. /*
  867. ================
  868. idAFConstraint_UniversalJoint::SetShafts
  869. ================
  870. */
  871. void idAFConstraint_UniversalJoint::SetShafts( const idVec3 &cardanShaft1, const idVec3 &cardanShaft2 ) {
  872. idVec3 cardanAxis;
  873. float l;
  874. shaft1 = cardanShaft1;
  875. l = shaft1.Normalize();
  876. assert( l != 0.0f );
  877. shaft2 = cardanShaft2;
  878. l = shaft2.Normalize();
  879. assert( l != 0.0f );
  880. // the cardan axis is a vector orthogonal to both cardan shafts
  881. cardanAxis = shaft1.Cross( shaft2 );
  882. if ( cardanAxis.Normalize() == 0.0f ) {
  883. idVec3 vecY;
  884. shaft1.OrthogonalBasis( cardanAxis, vecY );
  885. cardanAxis.Normalize();
  886. }
  887. shaft1 *= body1->GetWorldAxis().Transpose();
  888. axis1 = cardanAxis * body1->GetWorldAxis().Transpose();
  889. if ( body2 ) {
  890. shaft2 *= body2->GetWorldAxis().Transpose();
  891. axis2 = cardanAxis * body2->GetWorldAxis().Transpose();
  892. }
  893. else {
  894. axis2 = cardanAxis;
  895. }
  896. if ( coneLimit ) {
  897. coneLimit->SetBody1Axis( shaft1 );
  898. }
  899. if ( pyramidLimit ) {
  900. pyramidLimit->SetBody1Axis( shaft1 );
  901. }
  902. }
  903. /*
  904. ================
  905. idAFConstraint_UniversalJoint::SetNoLimit
  906. ================
  907. */
  908. void idAFConstraint_UniversalJoint::SetNoLimit( void ) {
  909. if ( coneLimit ) {
  910. delete coneLimit;
  911. coneLimit = NULL;
  912. }
  913. if ( pyramidLimit ) {
  914. delete pyramidLimit;
  915. pyramidLimit = NULL;
  916. }
  917. }
  918. /*
  919. ================
  920. idAFConstraint_UniversalJoint::SetConeLimit
  921. ================
  922. */
  923. void idAFConstraint_UniversalJoint::SetConeLimit( const idVec3 &coneAxis, const float coneAngle ) {
  924. if ( pyramidLimit ) {
  925. delete pyramidLimit;
  926. pyramidLimit = NULL;
  927. }
  928. if ( !coneLimit ) {
  929. coneLimit = new idAFConstraint_ConeLimit;
  930. coneLimit->SetPhysics( physics );
  931. }
  932. if ( body2 ) {
  933. coneLimit->Setup( body1, body2, anchor2, coneAxis * body2->GetWorldAxis().Transpose(), coneAngle, shaft1 );
  934. }
  935. else {
  936. coneLimit->Setup( body1, body2, anchor2, coneAxis, coneAngle, shaft1 );
  937. }
  938. }
  939. /*
  940. ================
  941. idAFConstraint_UniversalJoint::SetPyramidLimit
  942. ================
  943. */
  944. void idAFConstraint_UniversalJoint::SetPyramidLimit( const idVec3 &pyramidAxis, const idVec3 &baseAxis,
  945. const float angle1, const float angle2 ) {
  946. if ( coneLimit ) {
  947. delete coneLimit;
  948. coneLimit = NULL;
  949. }
  950. if ( !pyramidLimit ) {
  951. pyramidLimit = new idAFConstraint_PyramidLimit;
  952. pyramidLimit->SetPhysics( physics );
  953. }
  954. if ( body2 ) {
  955. pyramidLimit->Setup( body1, body2, anchor2, pyramidAxis * body2->GetWorldAxis().Transpose(),
  956. baseAxis * body2->GetWorldAxis().Transpose(), angle1, angle2, shaft1 );
  957. }
  958. else {
  959. pyramidLimit->Setup( body1, body2, anchor2, pyramidAxis, baseAxis, angle1, angle2, shaft1 );
  960. }
  961. }
  962. /*
  963. ================
  964. idAFConstraint_UniversalJoint::SetLimitEpsilon
  965. ================
  966. */
  967. void idAFConstraint_UniversalJoint::SetLimitEpsilon( const float e ) {
  968. if ( coneLimit ) {
  969. coneLimit->SetEpsilon( e );
  970. }
  971. if ( pyramidLimit ) {
  972. pyramidLimit->SetEpsilon( e );
  973. }
  974. }
  975. /*
  976. ================
  977. idAFConstraint_UniversalJoint::GetFriction
  978. ================
  979. */
  980. float idAFConstraint_UniversalJoint::GetFriction( void ) const {
  981. if ( af_forceFriction.GetFloat() > 0.0f ) {
  982. return af_forceFriction.GetFloat();
  983. }
  984. return friction * physics->GetJointFrictionScale();
  985. }
  986. /*
  987. ================
  988. idAFConstraint_UniversalJoint::Evaluate
  989. NOTE: this joint is homokinetic
  990. ================
  991. */
  992. void idAFConstraint_UniversalJoint::Evaluate( float invTimeStep ) {
  993. idVec3 a1, a2, s1, s2, d1, d2, v;
  994. idAFBody *master;
  995. master = body2 ? body2 : physics->GetMasterBody();
  996. a1 = anchor1 * body1->GetWorldAxis();
  997. s1 = shaft1 * body1->GetWorldAxis();
  998. d1 = s1.Cross( axis1 * body1->GetWorldAxis() );
  999. if ( master ) {
  1000. a2 = anchor2 * master->GetWorldAxis();
  1001. s2 = shaft2 * master->GetWorldAxis();
  1002. d2 = axis2 * master->GetWorldAxis();
  1003. c1.SubVec3(0) = -( invTimeStep * ERROR_REDUCTION ) * ( a2 + master->GetWorldOrigin() - ( a1 + body1->GetWorldOrigin() ) );
  1004. }
  1005. else {
  1006. a2 = anchor2;
  1007. s2 = shaft2;
  1008. d2 = axis2;
  1009. c1.SubVec3(0) = -( invTimeStep * ERROR_REDUCTION ) * ( a2 - ( a1 + body1->GetWorldOrigin() ) );
  1010. }
  1011. J1.Set( mat3_identity, -SkewSymmetric( a1 ),
  1012. mat3_zero, idMat3( s1[0], s1[1], s1[2],
  1013. 0.0f, 0.0f, 0.0f,
  1014. 0.0f, 0.0f, 0.0f ) );
  1015. J1.SetSize( 4, 6 );
  1016. if ( body2 ) {
  1017. J2.Set( -mat3_identity, SkewSymmetric( a2 ),
  1018. mat3_zero, idMat3( s2[0], s2[1], s2[2],
  1019. 0.0f, 0.0f, 0.0f,
  1020. 0.0f, 0.0f, 0.0f ) );
  1021. J2.SetSize( 4, 6 );
  1022. }
  1023. else {
  1024. J2.Zero( 4, 6 );
  1025. }
  1026. v = s1.Cross( s2 );
  1027. if ( v.Normalize() != 0.0f ) {
  1028. idMat3 m1, m2;
  1029. m1[0] = s1;
  1030. m1[1] = v;
  1031. m1[2] = v.Cross( m1[0] );
  1032. m2[0] = -s2;
  1033. m2[1] = v;
  1034. m2[2] = v.Cross( m2[0] );
  1035. d2 *= m2.Transpose() * m1;
  1036. }
  1037. c1[3] = -( invTimeStep * ERROR_REDUCTION ) * ( d1 * d2 );
  1038. c1.Clamp( -ERROR_REDUCTION_MAX, ERROR_REDUCTION_MAX );
  1039. if ( coneLimit ) {
  1040. coneLimit->Add( physics, invTimeStep );
  1041. }
  1042. else if ( pyramidLimit ) {
  1043. pyramidLimit->Add( physics, invTimeStep );
  1044. }
  1045. }
  1046. /*
  1047. ================
  1048. idAFConstraint_UniversalJoint::ApplyFriction
  1049. ================
  1050. */
  1051. void idAFConstraint_UniversalJoint::ApplyFriction( float invTimeStep ) {
  1052. idVec3 angular;
  1053. float invMass, currentFriction;
  1054. currentFriction = GetFriction();
  1055. if ( currentFriction <= 0.0f ) {
  1056. return;
  1057. }
  1058. if ( af_useImpulseFriction.GetBool() || af_useJointImpulseFriction.GetBool() ) {
  1059. angular = body1->GetAngularVelocity();
  1060. invMass = body1->GetInverseMass();
  1061. if ( body2 ) {
  1062. angular -= body2->GetAngularVelocity();
  1063. invMass += body2->GetInverseMass();
  1064. }
  1065. angular *= currentFriction / invMass;
  1066. body1->SetAngularVelocity( body1->GetAngularVelocity() - angular * body1->GetInverseMass() );
  1067. if ( body2 ) {
  1068. body2->SetAngularVelocity( body2->GetAngularVelocity() + angular * body2->GetInverseMass() );
  1069. }
  1070. }
  1071. else {
  1072. if ( !fc ) {
  1073. fc = new idAFConstraint_UniversalJointFriction;
  1074. fc->Setup( this );
  1075. }
  1076. fc->Add( physics, invTimeStep );
  1077. }
  1078. }
  1079. /*
  1080. ================
  1081. idAFConstraint_UniversalJoint::GetForce
  1082. ================
  1083. */
  1084. void idAFConstraint_UniversalJoint::GetForce( idAFBody *body, idVec6 &force ) {
  1085. idAFConstraint::GetForce( body, force );
  1086. // FIXME: add limit force
  1087. }
  1088. /*
  1089. ================
  1090. idAFConstraint_UniversalJoint::Translate
  1091. ================
  1092. */
  1093. void idAFConstraint_UniversalJoint::Translate( const idVec3 &translation ) {
  1094. if ( !body2 ) {
  1095. anchor2 += translation;
  1096. }
  1097. if ( coneLimit ) {
  1098. coneLimit->Translate( translation );
  1099. }
  1100. else if ( pyramidLimit ) {
  1101. pyramidLimit->Translate( translation );
  1102. }
  1103. }
  1104. /*
  1105. ================
  1106. idAFConstraint_UniversalJoint::Rotate
  1107. ================
  1108. */
  1109. void idAFConstraint_UniversalJoint::Rotate( const idRotation &rotation ) {
  1110. if ( !body2 ) {
  1111. anchor2 *= rotation;
  1112. shaft2 *= rotation.ToMat3();
  1113. axis2 *= rotation.ToMat3();
  1114. }
  1115. if ( coneLimit ) {
  1116. coneLimit->Rotate( rotation );
  1117. }
  1118. else if ( pyramidLimit ) {
  1119. pyramidLimit->Rotate( rotation );
  1120. }
  1121. }
  1122. /*
  1123. ================
  1124. idAFConstraint_UniversalJoint::GetCenter
  1125. ================
  1126. */
  1127. void idAFConstraint_UniversalJoint::GetCenter( idVec3 &center ) {
  1128. center = body1->GetWorldOrigin() + anchor1 * body1->GetWorldAxis();
  1129. }
  1130. /*
  1131. ================
  1132. idAFConstraint_UniversalJoint::DebugDraw
  1133. ================
  1134. */
  1135. void idAFConstraint_UniversalJoint::DebugDraw( void ) {
  1136. idVec3 a1, a2, s1, s2, d1, d2, v;
  1137. idAFBody *master;
  1138. master = body2 ? body2 : physics->GetMasterBody();
  1139. a1 = body1->GetWorldOrigin() + anchor1 * body1->GetWorldAxis();
  1140. s1 = shaft1 * body1->GetWorldAxis();
  1141. d1 = axis1 * body1->GetWorldAxis();
  1142. if ( master ) {
  1143. a2 = master->GetWorldOrigin() + anchor2 * master->GetWorldAxis();
  1144. s2 = shaft2 * master->GetWorldAxis();
  1145. d2 = axis2 * master->GetWorldAxis();
  1146. }
  1147. else {
  1148. a2 = anchor2;
  1149. s2 = shaft2;
  1150. d2 = axis2;
  1151. }
  1152. v = s1.Cross( s2 );
  1153. if ( v.Normalize() != 0.0f ) {
  1154. idMat3 m1, m2;
  1155. m1[0] = s1;
  1156. m1[1] = v;
  1157. m1[2] = v.Cross( m1[0] );
  1158. m2[0] = -s2;
  1159. m2[1] = v;
  1160. m2[2] = v.Cross( m2[0] );
  1161. d2 *= m2.Transpose() * m1;
  1162. }
  1163. gameRenderWorld->DebugArrow( colorCyan, a1, a1 + s1 * 5.0f, 1.0f );
  1164. gameRenderWorld->DebugArrow( colorBlue, a2, a2 + s2 * 5.0f, 1.0f );
  1165. gameRenderWorld->DebugLine( colorGreen, a1, a1 + d1 * 5.0f );
  1166. gameRenderWorld->DebugLine( colorGreen, a2, a2 + d2 * 5.0f );
  1167. if ( af_showLimits.GetBool() ) {
  1168. if ( coneLimit ) {
  1169. coneLimit->DebugDraw();
  1170. }
  1171. if ( pyramidLimit ) {
  1172. pyramidLimit->DebugDraw();
  1173. }
  1174. }
  1175. }
  1176. /*
  1177. ================
  1178. idAFConstraint_UniversalJoint::Save
  1179. ================
  1180. */
  1181. void idAFConstraint_UniversalJoint::Save( idSaveGame *saveFile ) const {
  1182. idAFConstraint::Save( saveFile );
  1183. saveFile->WriteVec3( anchor1 );
  1184. saveFile->WriteVec3( anchor2 );
  1185. saveFile->WriteVec3( shaft1 );
  1186. saveFile->WriteVec3( shaft2 );
  1187. saveFile->WriteVec3( axis1 );
  1188. saveFile->WriteVec3( axis2 );
  1189. saveFile->WriteFloat( friction );
  1190. if ( coneLimit ) {
  1191. coneLimit->Save( saveFile );
  1192. }
  1193. if ( pyramidLimit ) {
  1194. pyramidLimit->Save( saveFile );
  1195. }
  1196. }
  1197. /*
  1198. ================
  1199. idAFConstraint_UniversalJoint::Restore
  1200. ================
  1201. */
  1202. void idAFConstraint_UniversalJoint::Restore( idRestoreGame *saveFile ) {
  1203. idAFConstraint::Restore( saveFile );
  1204. saveFile->ReadVec3( anchor1 );
  1205. saveFile->ReadVec3( anchor2 );
  1206. saveFile->ReadVec3( shaft1 );
  1207. saveFile->ReadVec3( shaft2 );
  1208. saveFile->ReadVec3( axis1 );
  1209. saveFile->ReadVec3( axis2 );
  1210. saveFile->ReadFloat( friction );
  1211. if ( coneLimit ) {
  1212. coneLimit->Restore( saveFile );
  1213. }
  1214. if ( pyramidLimit ) {
  1215. pyramidLimit->Restore( saveFile );
  1216. }
  1217. }
  1218. //===============================================================
  1219. //
  1220. // idAFConstraint_UniversalJointFriction
  1221. //
  1222. //===============================================================
  1223. /*
  1224. ================
  1225. idAFConstraint_UniversalJointFriction::idAFConstraint_UniversalJointFriction
  1226. ================
  1227. */
  1228. idAFConstraint_UniversalJointFriction::idAFConstraint_UniversalJointFriction( void ) {
  1229. type = CONSTRAINT_FRICTION;
  1230. name = "universalJointFriction";
  1231. InitSize( 2 );
  1232. joint = NULL;
  1233. fl.allowPrimary = false;
  1234. fl.frameConstraint = true;
  1235. }
  1236. /*
  1237. ================
  1238. idAFConstraint_UniversalJointFriction::Setup
  1239. ================
  1240. */
  1241. void idAFConstraint_UniversalJointFriction::Setup( idAFConstraint_UniversalJoint *uj ) {
  1242. this->joint = uj;
  1243. body1 = uj->GetBody1();
  1244. body2 = uj->GetBody2();
  1245. }
  1246. /*
  1247. ================
  1248. idAFConstraint_UniversalJointFriction::Evaluate
  1249. ================
  1250. */
  1251. void idAFConstraint_UniversalJointFriction::Evaluate( float invTimeStep ) {
  1252. // do nothing
  1253. }
  1254. /*
  1255. ================
  1256. idAFConstraint_UniversalJointFriction::ApplyFriction
  1257. ================
  1258. */
  1259. void idAFConstraint_UniversalJointFriction::ApplyFriction( float invTimeStep ) {
  1260. // do nothing
  1261. }
  1262. /*
  1263. ================
  1264. idAFConstraint_UniversalJointFriction::Add
  1265. ================
  1266. */
  1267. bool idAFConstraint_UniversalJointFriction::Add( idPhysics_AF *phys, float invTimeStep ) {
  1268. idVec3 s1, s2, dir1, dir2;
  1269. float f;
  1270. physics = phys;
  1271. f = joint->GetFriction() * joint->GetMultiplier().Length();
  1272. if ( f == 0.0f ) {
  1273. return false;
  1274. }
  1275. lo[0] = lo[1] = -f;
  1276. hi[0] = hi[1] = f;
  1277. joint->GetShafts( s1, s2 );
  1278. s1 *= body1->GetWorldAxis();
  1279. s1.NormalVectors( dir1, dir2 );
  1280. J1.SetSize( 2, 6 );
  1281. J1.SubVec6(0).SubVec3(0).Zero();
  1282. J1.SubVec6(0).SubVec3(1) = dir1;
  1283. J1.SubVec6(1).SubVec3(0).Zero();
  1284. J1.SubVec6(1).SubVec3(1) = dir2;
  1285. if ( body2 ) {
  1286. J2.SetSize( 2, 6 );
  1287. J2.SubVec6(0).SubVec3(0).Zero();
  1288. J2.SubVec6(0).SubVec3(1) = -dir1;
  1289. J2.SubVec6(1).SubVec3(0).Zero();
  1290. J2.SubVec6(1).SubVec3(1) = -dir2;
  1291. }
  1292. physics->AddFrameConstraint( this );
  1293. return true;
  1294. }
  1295. /*
  1296. ================
  1297. idAFConstraint_UniversalJointFriction::Translate
  1298. ================
  1299. */
  1300. void idAFConstraint_UniversalJointFriction::Translate( const idVec3 &translation ) {
  1301. }
  1302. /*
  1303. ================
  1304. idAFConstraint_UniversalJointFriction::Rotate
  1305. ================
  1306. */
  1307. void idAFConstraint_UniversalJointFriction::Rotate( const idRotation &rotation ) {
  1308. }
  1309. //===============================================================
  1310. //
  1311. // idAFConstraint_CylindricalJoint
  1312. //
  1313. //===============================================================
  1314. /*
  1315. ================
  1316. idAFConstraint_CylindricalJoint::idAFConstraint_CylindricalJoint
  1317. ================
  1318. */
  1319. idAFConstraint_CylindricalJoint::idAFConstraint_CylindricalJoint( const idStr &name, idAFBody *body1, idAFBody *body2 ) {
  1320. assert( 0 ); // FIXME: implement
  1321. }
  1322. /*
  1323. ================
  1324. idAFConstraint_CylindricalJoint::Evaluate
  1325. ================
  1326. */
  1327. void idAFConstraint_CylindricalJoint::Evaluate( float invTimeStep ) {
  1328. assert( 0 ); // FIXME: implement
  1329. }
  1330. /*
  1331. ================
  1332. idAFConstraint_CylindricalJoint::ApplyFriction
  1333. ================
  1334. */
  1335. void idAFConstraint_CylindricalJoint::ApplyFriction( float invTimeStep ) {
  1336. assert( 0 ); // FIXME: implement
  1337. }
  1338. /*
  1339. ================
  1340. idAFConstraint_CylindricalJoint::Translate
  1341. ================
  1342. */
  1343. void idAFConstraint_CylindricalJoint::Translate( const idVec3 &translation ) {
  1344. assert( 0 ); // FIXME: implement
  1345. }
  1346. /*
  1347. ================
  1348. idAFConstraint_CylindricalJoint::Rotate
  1349. ================
  1350. */
  1351. void idAFConstraint_CylindricalJoint::Rotate( const idRotation &rotation ) {
  1352. assert( 0 ); // FIXME: implement
  1353. }
  1354. /*
  1355. ================
  1356. idAFConstraint_CylindricalJoint::DebugDraw
  1357. ================
  1358. */
  1359. void idAFConstraint_CylindricalJoint::DebugDraw( void ) {
  1360. assert( 0 ); // FIXME: implement
  1361. }
  1362. //===============================================================
  1363. //
  1364. // idAFConstraint_Hinge
  1365. //
  1366. //===============================================================
  1367. /*
  1368. ================
  1369. idAFConstraint_Hinge::idAFConstraint_Hinge
  1370. ================
  1371. */
  1372. idAFConstraint_Hinge::idAFConstraint_Hinge( const idStr &name, idAFBody *body1, idAFBody *body2 ) {
  1373. assert( body1 );
  1374. type = CONSTRAINT_HINGE;
  1375. this->name = name;
  1376. this->body1 = body1;
  1377. this->body2 = body2;
  1378. InitSize( 5 );
  1379. coneLimit = NULL;
  1380. steering = NULL;
  1381. friction = 0.0f;
  1382. fc = NULL;
  1383. fl.allowPrimary = true;
  1384. fl.noCollision = true;
  1385. initialAxis = body1->GetWorldAxis();
  1386. if ( body2 ) {
  1387. initialAxis *= body2->GetWorldAxis().Transpose();
  1388. }
  1389. }
  1390. /*
  1391. ================
  1392. idAFConstraint_Hinge::~idAFConstraint_Hinge
  1393. ================
  1394. */
  1395. idAFConstraint_Hinge::~idAFConstraint_Hinge( void ) {
  1396. if ( coneLimit ) {
  1397. delete coneLimit;
  1398. }
  1399. if ( fc ) {
  1400. delete fc;
  1401. }
  1402. if ( steering ) {
  1403. delete steering;
  1404. }
  1405. }
  1406. /*
  1407. ================
  1408. idAFConstraint_Hinge::SetAnchor
  1409. ================
  1410. */
  1411. void idAFConstraint_Hinge::SetAnchor( const idVec3 &worldPosition ) {
  1412. // get anchor relative to center of mass of body1
  1413. anchor1 = ( worldPosition - body1->GetWorldOrigin() ) * body1->GetWorldAxis().Transpose();
  1414. if ( body2 ) {
  1415. // get anchor relative to center of mass of body2
  1416. anchor2 = ( worldPosition - body2->GetWorldOrigin() ) * body2->GetWorldAxis().Transpose();
  1417. }
  1418. else {
  1419. anchor2 = worldPosition;
  1420. }
  1421. if ( coneLimit ) {
  1422. coneLimit->SetAnchor( anchor2 );
  1423. }
  1424. }
  1425. /*
  1426. ================
  1427. idAFConstraint_Hinge::GetAnchor
  1428. ================
  1429. */
  1430. idVec3 idAFConstraint_Hinge::GetAnchor( void ) const {
  1431. if ( body2 ) {
  1432. return body2->GetWorldOrigin() + body2->GetWorldAxis() * anchor2;
  1433. }
  1434. return anchor2;
  1435. }
  1436. /*
  1437. ================
  1438. idAFConstraint_Hinge::SetAxis
  1439. ================
  1440. */
  1441. void idAFConstraint_Hinge::SetAxis( const idVec3 &axis ) {
  1442. idVec3 normAxis;
  1443. normAxis = axis;
  1444. normAxis.Normalize();
  1445. // get axis relative to body1
  1446. axis1 = normAxis * body1->GetWorldAxis().Transpose();
  1447. if ( body2 ) {
  1448. // get axis relative to body2
  1449. axis2 = normAxis * body2->GetWorldAxis().Transpose();
  1450. }
  1451. else {
  1452. axis2 = normAxis;
  1453. }
  1454. }
  1455. /*
  1456. ================
  1457. idAFConstraint_Hinge::GetAxis
  1458. ================
  1459. */
  1460. idVec3 idAFConstraint_Hinge::GetAxis( void ) const {
  1461. if ( body2 ) {
  1462. return axis2 * body2->GetWorldAxis();
  1463. }
  1464. return axis2;
  1465. }
  1466. /*
  1467. ================
  1468. idAFConstraint_Hinge::SetNoLimit
  1469. ================
  1470. */
  1471. void idAFConstraint_Hinge::SetNoLimit( void ) {
  1472. if ( coneLimit ) {
  1473. delete coneLimit;
  1474. coneLimit = NULL;
  1475. }
  1476. }
  1477. /*
  1478. ================
  1479. idAFConstraint_Hinge::SetLimit
  1480. ================
  1481. */
  1482. void idAFConstraint_Hinge::SetLimit( const idVec3 &axis, const float angle, const idVec3 &body1Axis ) {
  1483. if ( !coneLimit ) {
  1484. coneLimit = new idAFConstraint_ConeLimit;
  1485. coneLimit->SetPhysics( physics );
  1486. }
  1487. if ( body2 ) {
  1488. coneLimit->Setup( body1, body2, anchor2, axis * body2->GetWorldAxis().Transpose(), angle, body1Axis * body1->GetWorldAxis().Transpose() );
  1489. }
  1490. else {
  1491. coneLimit->Setup( body1, body2, anchor2, axis, angle, body1Axis * body1->GetWorldAxis().Transpose() );
  1492. }
  1493. }
  1494. /*
  1495. ================
  1496. idAFConstraint_Hinge::SetLimitEpsilon
  1497. ================
  1498. */
  1499. void idAFConstraint_Hinge::SetLimitEpsilon( const float e ) {
  1500. if ( coneLimit ) {
  1501. coneLimit->SetEpsilon( e );
  1502. }
  1503. }
  1504. /*
  1505. ================
  1506. idAFConstraint_Hinge::GetFriction
  1507. ================
  1508. */
  1509. float idAFConstraint_Hinge::GetFriction( void ) const {
  1510. if ( af_forceFriction.GetFloat() > 0.0f ) {
  1511. return af_forceFriction.GetFloat();
  1512. }
  1513. return friction * physics->GetJointFrictionScale();
  1514. }
  1515. /*
  1516. ================
  1517. idAFConstraint_Hinge::GetAngle
  1518. ================
  1519. */
  1520. float idAFConstraint_Hinge::GetAngle( void ) const {
  1521. idMat3 axis;
  1522. idRotation rotation;
  1523. float angle;
  1524. axis = body1->GetWorldAxis() * body2->GetWorldAxis().Transpose() * initialAxis.Transpose();
  1525. rotation = axis.ToRotation();
  1526. angle = rotation.GetAngle();
  1527. if ( rotation.GetVec() * axis1 < 0.0f ) {
  1528. return -angle;
  1529. }
  1530. return angle;
  1531. }
  1532. /*
  1533. ================
  1534. idAFConstraint_Hinge::SetSteerAngle
  1535. ================
  1536. */
  1537. void idAFConstraint_Hinge::SetSteerAngle( const float degrees ) {
  1538. if ( coneLimit ) {
  1539. delete coneLimit;
  1540. coneLimit = NULL;
  1541. }
  1542. if ( !steering ) {
  1543. steering = new idAFConstraint_HingeSteering();
  1544. steering->Setup( this );
  1545. }
  1546. steering->SetSteerAngle( degrees );
  1547. }
  1548. /*
  1549. ================
  1550. idAFConstraint_Hinge::SetSteerSpeed
  1551. ================
  1552. */
  1553. void idAFConstraint_Hinge::SetSteerSpeed( const float speed ) {
  1554. if ( steering ) {
  1555. steering->SetSteerSpeed( speed );
  1556. }
  1557. }
  1558. /*
  1559. ================
  1560. idAFConstraint_Hinge::Evaluate
  1561. ================
  1562. */
  1563. void idAFConstraint_Hinge::Evaluate( float invTimeStep ) {
  1564. idVec3 a1, a2;
  1565. idVec3 x1, x2, cross;
  1566. idVec3 vecX, vecY;
  1567. idAFBody *master;
  1568. master = body2 ? body2 : physics->GetMasterBody();
  1569. x1 = axis1 * body1->GetWorldAxis(); // axis in body1 space
  1570. x1.OrthogonalBasis( vecX, vecY ); // basis for axis in body1 space
  1571. a1 = anchor1 * body1->GetWorldAxis(); // anchor in body1 space
  1572. if ( master ) {
  1573. a2 = anchor2 * master->GetWorldAxis(); // anchor in master space
  1574. x2 = axis2 * master->GetWorldAxis();
  1575. c1.SubVec3(0) = -( invTimeStep * ERROR_REDUCTION ) * ( a2 + master->GetWorldOrigin() - ( a1 + body1->GetWorldOrigin() ) );
  1576. }
  1577. else {
  1578. a2 = anchor2;
  1579. x2 = axis2;
  1580. c1.SubVec3(0) = -( invTimeStep * ERROR_REDUCTION ) * ( a2 - ( a1 + body1->GetWorldOrigin() ) );
  1581. }
  1582. J1.Set( mat3_identity, -SkewSymmetric( a1 ),
  1583. mat3_zero, idMat3( vecX[0], vecX[1], vecX[2],
  1584. vecY[0], vecY[1], vecY[2],
  1585. 0.0f, 0.0f, 0.0f ) );
  1586. J1.SetSize( 5, 6 );
  1587. if ( body2 ) {
  1588. J2.Set( -mat3_identity, SkewSymmetric( a2 ),
  1589. mat3_zero, idMat3( -vecX[0], -vecX[1], -vecX[2],
  1590. -vecY[0], -vecY[1], -vecY[2],
  1591. 0.0f, 0.0f, 0.0f ) );
  1592. J2.SetSize( 5, 6 );
  1593. }
  1594. else {
  1595. J2.Zero( 5, 6 );
  1596. }
  1597. cross = x1.Cross( x2 );
  1598. c1[3] = -( invTimeStep * ERROR_REDUCTION ) * ( cross * vecX );
  1599. c1[4] = -( invTimeStep * ERROR_REDUCTION ) * ( cross * vecY );
  1600. c1.Clamp( -ERROR_REDUCTION_MAX, ERROR_REDUCTION_MAX );
  1601. if ( steering ) {
  1602. steering->Add( physics, invTimeStep );
  1603. }
  1604. else if ( coneLimit ) {
  1605. coneLimit->Add( physics, invTimeStep );
  1606. }
  1607. }
  1608. /*
  1609. ================
  1610. idAFConstraint_Hinge::ApplyFriction
  1611. ================
  1612. */
  1613. void idAFConstraint_Hinge::ApplyFriction( float invTimeStep ) {
  1614. idVec3 angular;
  1615. float invMass, currentFriction;
  1616. currentFriction = GetFriction();
  1617. if ( currentFriction <= 0.0f ) {
  1618. return;
  1619. }
  1620. if ( af_useImpulseFriction.GetBool() || af_useJointImpulseFriction.GetBool() ) {
  1621. angular = body1->GetAngularVelocity();
  1622. invMass = body1->GetInverseMass();
  1623. if ( body2 ) {
  1624. angular -= body2->GetAngularVelocity();
  1625. invMass += body2->GetInverseMass();
  1626. }
  1627. angular *= currentFriction / invMass;
  1628. body1->SetAngularVelocity( body1->GetAngularVelocity() - angular * body1->GetInverseMass() );
  1629. if ( body2 ) {
  1630. body2->SetAngularVelocity( body2->GetAngularVelocity() + angular * body2->GetInverseMass() );
  1631. }
  1632. }
  1633. else {
  1634. if ( !fc ) {
  1635. fc = new idAFConstraint_HingeFriction;
  1636. fc->Setup( this );
  1637. }
  1638. fc->Add( physics, invTimeStep );
  1639. }
  1640. }
  1641. /*
  1642. ================
  1643. idAFConstraint_Hinge::GetForce
  1644. ================
  1645. */
  1646. void idAFConstraint_Hinge::GetForce( idAFBody *body, idVec6 &force ) {
  1647. idAFConstraint::GetForce( body, force );
  1648. // FIXME: add limit force
  1649. }
  1650. /*
  1651. ================
  1652. idAFConstraint_Hinge::Translate
  1653. ================
  1654. */
  1655. void idAFConstraint_Hinge::Translate( const idVec3 &translation ) {
  1656. if ( !body2 ) {
  1657. anchor2 += translation;
  1658. }
  1659. if ( coneLimit ) {
  1660. coneLimit->Translate( translation );
  1661. }
  1662. }
  1663. /*
  1664. ================
  1665. idAFConstraint_Hinge::Rotate
  1666. ================
  1667. */
  1668. void idAFConstraint_Hinge::Rotate( const idRotation &rotation ) {
  1669. if ( !body2 ) {
  1670. anchor2 *= rotation;
  1671. axis2 *= rotation.ToMat3();
  1672. }
  1673. if ( coneLimit ) {
  1674. coneLimit->Rotate( rotation );
  1675. }
  1676. }
  1677. /*
  1678. ================
  1679. idAFConstraint_Hinge::GetCenter
  1680. ================
  1681. */
  1682. void idAFConstraint_Hinge::GetCenter( idVec3 &center ) {
  1683. center = body1->GetWorldOrigin() + anchor1 * body1->GetWorldAxis();
  1684. }
  1685. /*
  1686. ================
  1687. idAFConstraint_Hinge::DebugDraw
  1688. ================
  1689. */
  1690. void idAFConstraint_Hinge::DebugDraw( void ) {
  1691. idVec3 vecX, vecY;
  1692. idVec3 a1 = body1->GetWorldOrigin() + anchor1 * body1->GetWorldAxis();
  1693. idVec3 x1 = axis1 * body1->GetWorldAxis();
  1694. x1.OrthogonalBasis( vecX, vecY );
  1695. gameRenderWorld->DebugArrow( colorBlue, a1 - 4.0f * x1, a1 + 4.0f * x1, 1 );
  1696. gameRenderWorld->DebugLine( colorBlue, a1 - 2.0f * vecX, a1 + 2.0f * vecX );
  1697. gameRenderWorld->DebugLine( colorBlue, a1 - 2.0f * vecY, a1 + 2.0f * vecY );
  1698. if ( af_showLimits.GetBool() ) {
  1699. if ( coneLimit ) {
  1700. coneLimit->DebugDraw();
  1701. }
  1702. }
  1703. }
  1704. /*
  1705. ================
  1706. idAFConstraint_Hinge::Save
  1707. ================
  1708. */
  1709. void idAFConstraint_Hinge::Save( idSaveGame *saveFile ) const {
  1710. idAFConstraint::Save( saveFile );
  1711. saveFile->WriteVec3( anchor1 );
  1712. saveFile->WriteVec3( anchor2 );
  1713. saveFile->WriteVec3( axis1 );
  1714. saveFile->WriteVec3( axis2 );
  1715. saveFile->WriteMat3( initialAxis );
  1716. saveFile->WriteFloat( friction );
  1717. if ( coneLimit ) {
  1718. saveFile->WriteBool( true );
  1719. coneLimit->Save( saveFile );
  1720. } else {
  1721. saveFile->WriteBool( false );
  1722. }
  1723. if ( steering ) {
  1724. saveFile->WriteBool( true );
  1725. steering->Save( saveFile );
  1726. } else {
  1727. saveFile->WriteBool( false );
  1728. }
  1729. if ( fc ) {
  1730. saveFile->WriteBool( true );
  1731. fc->Save( saveFile );
  1732. } else {
  1733. saveFile->WriteBool( false );
  1734. }
  1735. }
  1736. /*
  1737. ================
  1738. idAFConstraint_Hinge::Restore
  1739. ================
  1740. */
  1741. void idAFConstraint_Hinge::Restore( idRestoreGame *saveFile ) {
  1742. bool b;
  1743. idAFConstraint::Restore( saveFile );
  1744. saveFile->ReadVec3( anchor1 );
  1745. saveFile->ReadVec3( anchor2 );
  1746. saveFile->ReadVec3( axis1 );
  1747. saveFile->ReadVec3( axis2 );
  1748. saveFile->ReadMat3( initialAxis );
  1749. saveFile->ReadFloat( friction );
  1750. saveFile->ReadBool( b );
  1751. if ( b ) {
  1752. if ( !coneLimit ) {
  1753. coneLimit = new idAFConstraint_ConeLimit;
  1754. }
  1755. coneLimit->SetPhysics( physics );
  1756. coneLimit->Restore( saveFile );
  1757. }
  1758. saveFile->ReadBool( b );
  1759. if ( b ) {
  1760. if ( !steering ) {
  1761. steering = new idAFConstraint_HingeSteering;
  1762. }
  1763. steering->Setup( this );
  1764. steering->Restore( saveFile );
  1765. }
  1766. saveFile->ReadBool( b );
  1767. if ( b ) {
  1768. if ( !fc ) {
  1769. fc = new idAFConstraint_HingeFriction;
  1770. }
  1771. fc->Setup( this );
  1772. fc->Restore( saveFile );
  1773. }
  1774. }
  1775. //===============================================================
  1776. //
  1777. // idAFConstraint_HingeFriction
  1778. //
  1779. //===============================================================
  1780. /*
  1781. ================
  1782. idAFConstraint_HingeFriction::idAFConstraint_HingeFriction
  1783. ================
  1784. */
  1785. idAFConstraint_HingeFriction::idAFConstraint_HingeFriction( void ) {
  1786. type = CONSTRAINT_FRICTION;
  1787. name = "hingeFriction";
  1788. InitSize( 1 );
  1789. hinge = NULL;
  1790. fl.allowPrimary = false;
  1791. fl.frameConstraint = true;
  1792. }
  1793. /*
  1794. ================
  1795. idAFConstraint_HingeFriction::Setup
  1796. ================
  1797. */
  1798. void idAFConstraint_HingeFriction::Setup( idAFConstraint_Hinge *h ) {
  1799. this->hinge = h;
  1800. body1 = h->GetBody1();
  1801. body2 = h->GetBody2();
  1802. }
  1803. /*
  1804. ================
  1805. idAFConstraint_HingeFriction::Evaluate
  1806. ================
  1807. */
  1808. void idAFConstraint_HingeFriction::Evaluate( float invTimeStep ) {
  1809. // do nothing
  1810. }
  1811. /*
  1812. ================
  1813. idAFConstraint_HingeFriction::ApplyFriction
  1814. ================
  1815. */
  1816. void idAFConstraint_HingeFriction::ApplyFriction( float invTimeStep ) {
  1817. // do nothing
  1818. }
  1819. /*
  1820. ================
  1821. idAFConstraint_HingeFriction::Add
  1822. ================
  1823. */
  1824. bool idAFConstraint_HingeFriction::Add( idPhysics_AF *phys, float invTimeStep ) {
  1825. idVec3 a1, a2;
  1826. float f;
  1827. physics = phys;
  1828. f = hinge->GetFriction() * hinge->GetMultiplier().Length();
  1829. if ( f == 0.0f ) {
  1830. return false;
  1831. }
  1832. lo[0] = -f;
  1833. hi[0] = f;
  1834. hinge->GetAxis( a1, a2 );
  1835. a1 *= body1->GetWorldAxis();
  1836. J1.SetSize( 1, 6 );
  1837. J1.SubVec6(0).SubVec3(0).Zero();
  1838. J1.SubVec6(0).SubVec3(1) = a1;
  1839. if ( body2 ) {
  1840. a2 *= body2->GetWorldAxis();
  1841. J2.SetSize( 1, 6 );
  1842. J2.SubVec6(0).SubVec3(0).Zero();
  1843. J2.SubVec6(0).SubVec3(1) = -a2;
  1844. }
  1845. physics->AddFrameConstraint( this );
  1846. return true;
  1847. }
  1848. /*
  1849. ================
  1850. idAFConstraint_HingeFriction::Translate
  1851. ================
  1852. */
  1853. void idAFConstraint_HingeFriction::Translate( const idVec3 &translation ) {
  1854. }
  1855. /*
  1856. ================
  1857. idAFConstraint_HingeFriction::Rotate
  1858. ================
  1859. */
  1860. void idAFConstraint_HingeFriction::Rotate( const idRotation &rotation ) {
  1861. }
  1862. //===============================================================
  1863. //
  1864. // idAFConstraint_HingeSteering
  1865. //
  1866. //===============================================================
  1867. /*
  1868. ================
  1869. idAFConstraint_HingeSteering::idAFConstraint_HingeSteering
  1870. ================
  1871. */
  1872. idAFConstraint_HingeSteering::idAFConstraint_HingeSteering( void ) {
  1873. type = CONSTRAINT_HINGESTEERING;
  1874. name = "hingeFriction";
  1875. InitSize( 1 );
  1876. hinge = NULL;
  1877. fl.allowPrimary = false;
  1878. fl.frameConstraint = true;
  1879. steerSpeed = 0.0f;
  1880. epsilon = LCP_EPSILON;
  1881. }
  1882. /*
  1883. ================
  1884. idAFConstraint_HingeSteering::Save
  1885. ================
  1886. */
  1887. void idAFConstraint_HingeSteering::Save( idSaveGame *saveFile ) const {
  1888. saveFile->WriteFloat(steerAngle);
  1889. saveFile->WriteFloat(steerSpeed);
  1890. saveFile->WriteFloat(epsilon);
  1891. }
  1892. /*
  1893. ================
  1894. idAFConstraint_HingeSteering::Restore
  1895. ================
  1896. */
  1897. void idAFConstraint_HingeSteering::Restore( idRestoreGame *saveFile ) {
  1898. saveFile->ReadFloat(steerAngle);
  1899. saveFile->ReadFloat(steerSpeed);
  1900. saveFile->ReadFloat(epsilon);
  1901. }
  1902. /*
  1903. ================
  1904. idAFConstraint_HingeSteering::Setup
  1905. ================
  1906. */
  1907. void idAFConstraint_HingeSteering::Setup( idAFConstraint_Hinge *h ) {
  1908. this->hinge = h;
  1909. body1 = h->GetBody1();
  1910. body2 = h->GetBody2();
  1911. }
  1912. /*
  1913. ================
  1914. idAFConstraint_HingeSteering::Evaluate
  1915. ================
  1916. */
  1917. void idAFConstraint_HingeSteering::Evaluate( float invTimeStep ) {
  1918. // do nothing
  1919. }
  1920. /*
  1921. ================
  1922. idAFConstraint_HingeSteering::ApplyFriction
  1923. ================
  1924. */
  1925. void idAFConstraint_HingeSteering::ApplyFriction( float invTimeStep ) {
  1926. // do nothing
  1927. }
  1928. /*
  1929. ================
  1930. idAFConstraint_HingeSteering::Add
  1931. ================
  1932. */
  1933. bool idAFConstraint_HingeSteering::Add( idPhysics_AF *phys, float invTimeStep ) {
  1934. float angle, speed;
  1935. idVec3 a1, a2;
  1936. physics = phys;
  1937. hinge->GetAxis( a1, a2 );
  1938. angle = hinge->GetAngle();
  1939. a1 *= body1->GetWorldAxis();
  1940. J1.SetSize( 1, 6 );
  1941. J1.SubVec6(0).SubVec3(0).Zero();
  1942. J1.SubVec6(0).SubVec3(1) = a1;
  1943. if ( body2 ) {
  1944. a2 *= body2->GetWorldAxis();
  1945. J2.SetSize( 1, 6 );
  1946. J2.SubVec6(0).SubVec3(0).Zero();
  1947. J2.SubVec6(0).SubVec3(1) = -a2;
  1948. }
  1949. speed = steerAngle - angle;
  1950. if ( steerSpeed != 0.0f ) {
  1951. if ( speed > steerSpeed ) {
  1952. speed = steerSpeed;
  1953. }
  1954. else if ( speed < -steerSpeed ) {
  1955. speed = -steerSpeed;
  1956. }
  1957. }
  1958. c1[0] = DEG2RAD( speed ) * invTimeStep;
  1959. physics->AddFrameConstraint( this );
  1960. return true;
  1961. }
  1962. /*
  1963. ================
  1964. idAFConstraint_HingeSteering::Translate
  1965. ================
  1966. */
  1967. void idAFConstraint_HingeSteering::Translate( const idVec3 &translation ) {
  1968. }
  1969. /*
  1970. ================
  1971. idAFConstraint_HingeSteering::Rotate
  1972. ================
  1973. */
  1974. void idAFConstraint_HingeSteering::Rotate( const idRotation &rotation ) {
  1975. }
  1976. //===============================================================
  1977. //
  1978. // idAFConstraint_Slider
  1979. //
  1980. //===============================================================
  1981. /*
  1982. ================
  1983. idAFConstraint_Slider::idAFConstraint_Slider
  1984. ================
  1985. */
  1986. idAFConstraint_Slider::idAFConstraint_Slider( const idStr &name, idAFBody *body1, idAFBody *body2 ) {
  1987. assert( body1 );
  1988. type = CONSTRAINT_SLIDER;
  1989. this->name = name;
  1990. this->body1 = body1;
  1991. this->body2 = body2;
  1992. InitSize( 5 );
  1993. fl.allowPrimary = true;
  1994. fl.noCollision = true;
  1995. if ( body2 ) {
  1996. offset = ( body1->GetWorldOrigin() - body2->GetWorldOrigin() ) * body1->GetWorldAxis().Transpose();
  1997. relAxis = body1->GetWorldAxis() * body2->GetWorldAxis().Transpose();
  1998. }
  1999. else {
  2000. offset = body1->GetWorldOrigin();
  2001. relAxis = body1->GetWorldAxis();
  2002. }
  2003. }
  2004. /*
  2005. ================
  2006. idAFConstraint_Slider::SetAxis
  2007. ================
  2008. */
  2009. void idAFConstraint_Slider::SetAxis( const idVec3 &ax ) {
  2010. idVec3 normAxis;
  2011. // get normalized axis relative to body1
  2012. normAxis = ax;
  2013. normAxis.Normalize();
  2014. if ( body2 ) {
  2015. axis = normAxis * body2->GetWorldAxis().Transpose();
  2016. }
  2017. else {
  2018. axis = normAxis;
  2019. }
  2020. }
  2021. /*
  2022. ================
  2023. idAFConstraint_Slider::Evaluate
  2024. ================
  2025. */
  2026. void idAFConstraint_Slider::Evaluate( float invTimeStep ) {
  2027. idVec3 vecX, vecY, ofs;
  2028. idRotation r;
  2029. idAFBody *master;
  2030. master = body2 ? body2 : physics->GetMasterBody();
  2031. if ( master ) {
  2032. (axis * master->GetWorldAxis()).OrthogonalBasis( vecX, vecY );
  2033. ofs = master->GetWorldOrigin() + master->GetWorldAxis() * offset - body1->GetWorldOrigin();
  2034. r = ( body1->GetWorldAxis().Transpose() * (relAxis * master->GetWorldAxis()) ).ToRotation();
  2035. }
  2036. else {
  2037. axis.OrthogonalBasis( vecX, vecY );
  2038. ofs = offset - body1->GetWorldOrigin();
  2039. r = ( body1->GetWorldAxis().Transpose() * relAxis ).ToRotation();
  2040. }
  2041. J1.Set( mat3_zero, mat3_identity,
  2042. idMat3( vecX, vecY, vec3_origin ), mat3_zero );
  2043. J1.SetSize( 5, 6 );
  2044. if ( body2 ) {
  2045. J2.Set( mat3_zero, -mat3_identity,
  2046. idMat3( -vecX, -vecY, vec3_origin ), mat3_zero );
  2047. J2.SetSize( 5, 6 );
  2048. }
  2049. else {
  2050. J2.Zero( 5, 6 );
  2051. }
  2052. c1.SubVec3(0) = -( invTimeStep * ERROR_REDUCTION ) * ( r.GetVec() * - (float) DEG2RAD( r.GetAngle() ) );
  2053. c1[3] = -( invTimeStep * ERROR_REDUCTION ) * ( vecX * ofs );
  2054. c1[4] = -( invTimeStep * ERROR_REDUCTION ) * ( vecY * ofs );
  2055. c1.Clamp( -ERROR_REDUCTION_MAX, ERROR_REDUCTION_MAX );
  2056. }
  2057. /*
  2058. ================
  2059. idAFConstraint_Slider::ApplyFriction
  2060. ================
  2061. */
  2062. void idAFConstraint_Slider::ApplyFriction( float invTimeStep ) {
  2063. // no friction
  2064. }
  2065. /*
  2066. ================
  2067. idAFConstraint_Slider::Translate
  2068. ================
  2069. */
  2070. void idAFConstraint_Slider::Translate( const idVec3 &translation ) {
  2071. if ( !body2 ) {
  2072. offset += translation;
  2073. }
  2074. }
  2075. /*
  2076. ================
  2077. idAFConstraint_Slider::Rotate
  2078. ================
  2079. */
  2080. void idAFConstraint_Slider::Rotate( const idRotation &rotation ) {
  2081. if ( !body2 ) {
  2082. offset *= rotation;
  2083. }
  2084. }
  2085. /*
  2086. ================
  2087. idAFConstraint_Slider::GetCenter
  2088. ================
  2089. */
  2090. void idAFConstraint_Slider::GetCenter( idVec3 &center ) {
  2091. idAFBody *master;
  2092. master = body2 ? body2 : physics->GetMasterBody();
  2093. if ( master ) {
  2094. center = master->GetWorldOrigin() + master->GetWorldAxis() * offset - body1->GetWorldOrigin();
  2095. }
  2096. else {
  2097. center = offset - body1->GetWorldOrigin();
  2098. }
  2099. }
  2100. /*
  2101. ================
  2102. idAFConstraint_Slider::DebugDraw
  2103. ================
  2104. */
  2105. void idAFConstraint_Slider::DebugDraw( void ) {
  2106. idVec3 ofs;
  2107. idAFBody *master;
  2108. master = body2 ? body2 : physics->GetMasterBody();
  2109. if ( master ) {
  2110. ofs = master->GetWorldOrigin() + master->GetWorldAxis() * offset - body1->GetWorldOrigin();
  2111. }
  2112. else {
  2113. ofs = offset - body1->GetWorldOrigin();
  2114. }
  2115. gameRenderWorld->DebugLine( colorGreen, ofs, ofs + axis * body1->GetWorldAxis() );
  2116. }
  2117. /*
  2118. ================
  2119. idAFConstraint_Slider::Save
  2120. ================
  2121. */
  2122. void idAFConstraint_Slider::Save( idSaveGame *saveFile ) const {
  2123. idAFConstraint::Save( saveFile );
  2124. saveFile->WriteVec3( axis );
  2125. saveFile->WriteVec3( offset );
  2126. saveFile->WriteMat3( relAxis );
  2127. }
  2128. /*
  2129. ================
  2130. idAFConstraint_Slider::Restore
  2131. ================
  2132. */
  2133. void idAFConstraint_Slider::Restore( idRestoreGame *saveFile ) {
  2134. idAFConstraint::Restore( saveFile );
  2135. saveFile->ReadVec3( axis );
  2136. saveFile->ReadVec3( offset );
  2137. saveFile->ReadMat3( relAxis );
  2138. }
  2139. //===============================================================
  2140. //
  2141. // idAFConstraint_Line
  2142. //
  2143. //===============================================================
  2144. /*
  2145. ================
  2146. idAFConstraint_Line::idAFConstraint_Line
  2147. ================
  2148. */
  2149. idAFConstraint_Line::idAFConstraint_Line( const idStr &name, idAFBody *body1, idAFBody *body2 ) {
  2150. assert( 0 ); // FIXME: implement
  2151. }
  2152. /*
  2153. ================
  2154. idAFConstraint_Line::Evaluate
  2155. ================
  2156. */
  2157. void idAFConstraint_Line::Evaluate( float invTimeStep ) {
  2158. assert( 0 ); // FIXME: implement
  2159. }
  2160. /*
  2161. ================
  2162. idAFConstraint_Line::ApplyFriction
  2163. ================
  2164. */
  2165. void idAFConstraint_Line::ApplyFriction( float invTimeStep ) {
  2166. assert( 0 ); // FIXME: implement
  2167. }
  2168. /*
  2169. ================
  2170. idAFConstraint_Line::Translate
  2171. ================
  2172. */
  2173. void idAFConstraint_Line::Translate( const idVec3 &translation ) {
  2174. assert( 0 ); // FIXME: implement
  2175. }
  2176. /*
  2177. ================
  2178. idAFConstraint_Line::Rotate
  2179. ================
  2180. */
  2181. void idAFConstraint_Line::Rotate( const idRotation &rotation ) {
  2182. assert( 0 ); // FIXME: implement
  2183. }
  2184. /*
  2185. ================
  2186. idAFConstraint_Line::DebugDraw
  2187. ================
  2188. */
  2189. void idAFConstraint_Line::DebugDraw( void ) {
  2190. assert( 0 ); // FIXME: implement
  2191. }
  2192. //===============================================================
  2193. //
  2194. // idAFConstraint_Plane
  2195. //
  2196. //===============================================================
  2197. /*
  2198. ================
  2199. idAFConstraint_Plane::idAFConstraint_Plane
  2200. ================
  2201. */
  2202. idAFConstraint_Plane::idAFConstraint_Plane( const idStr &name, idAFBody *body1, idAFBody *body2 ) {
  2203. assert( body1 );
  2204. type = CONSTRAINT_PLANE;
  2205. this->name = name;
  2206. this->body1 = body1;
  2207. this->body2 = body2;
  2208. InitSize( 1 );
  2209. fl.allowPrimary = true;
  2210. fl.noCollision = true;
  2211. }
  2212. /*
  2213. ================
  2214. idAFConstraint_Plane::SetPlane
  2215. ================
  2216. */
  2217. void idAFConstraint_Plane::SetPlane( const idVec3 &normal, const idVec3 &anchor ) {
  2218. // get anchor relative to center of mass of body1
  2219. anchor1 = ( anchor - body1->GetWorldOrigin() ) * body1->GetWorldAxis().Transpose();
  2220. if ( body2 ) {
  2221. // get anchor relative to center of mass of body2
  2222. anchor2 = ( anchor - body2->GetWorldOrigin() ) * body2->GetWorldAxis().Transpose();
  2223. planeNormal = normal * body2->GetWorldAxis().Transpose();
  2224. }
  2225. else {
  2226. anchor2 = anchor;
  2227. planeNormal = normal;
  2228. }
  2229. }
  2230. /*
  2231. ================
  2232. idAFConstraint_Plane::Evaluate
  2233. ================
  2234. */
  2235. void idAFConstraint_Plane::Evaluate( float invTimeStep ) {
  2236. idVec3 a1, a2, normal, p;
  2237. idVec6 v;
  2238. idAFBody *master;
  2239. master = body2 ? body2 : physics->GetMasterBody();
  2240. a1 = body1->GetWorldOrigin() + anchor1 * body1->GetWorldAxis();
  2241. if ( master ) {
  2242. a2 = master->GetWorldOrigin() + anchor2 * master->GetWorldAxis();
  2243. normal = planeNormal * master->GetWorldAxis();
  2244. }
  2245. else {
  2246. a2 = anchor2;
  2247. normal = planeNormal;
  2248. }
  2249. p = a1 - body1->GetWorldOrigin();
  2250. v.SubVec3(0) = normal;
  2251. v.SubVec3(1) = p.Cross( normal );
  2252. J1.Set( 1, 6, v.ToFloatPtr() );
  2253. if ( body2 ) {
  2254. p = a1 - body2->GetWorldOrigin();
  2255. v.SubVec3(0) = -normal;
  2256. v.SubVec3(1) = p.Cross( -normal );
  2257. J2.Set( 1, 6, v.ToFloatPtr() );
  2258. }
  2259. c1[0] = -( invTimeStep * ERROR_REDUCTION ) * (a1 * normal - a2 * normal);
  2260. c1.Clamp( -ERROR_REDUCTION_MAX, ERROR_REDUCTION_MAX );
  2261. }
  2262. /*
  2263. ================
  2264. idAFConstraint_Plane::ApplyFriction
  2265. ================
  2266. */
  2267. void idAFConstraint_Plane::ApplyFriction( float invTimeStep ) {
  2268. // no friction
  2269. }
  2270. /*
  2271. ================
  2272. idAFConstraint_Plane::Translate
  2273. ================
  2274. */
  2275. void idAFConstraint_Plane::Translate( const idVec3 &translation ) {
  2276. if ( !body2 ) {
  2277. anchor2 += translation;
  2278. }
  2279. }
  2280. /*
  2281. ================
  2282. idAFConstraint_Plane::Rotate
  2283. ================
  2284. */
  2285. void idAFConstraint_Plane::Rotate( const idRotation &rotation ) {
  2286. if ( !body2 ) {
  2287. anchor2 *= rotation;
  2288. planeNormal *= rotation.ToMat3();
  2289. }
  2290. }
  2291. /*
  2292. ================
  2293. idAFConstraint_Plane::DebugDraw
  2294. ================
  2295. */
  2296. void idAFConstraint_Plane::DebugDraw( void ) {
  2297. idVec3 a1, normal, right, up;
  2298. idAFBody *master;
  2299. master = body2 ? body2 : physics->GetMasterBody();
  2300. a1 = body1->GetWorldOrigin() + anchor1 * body1->GetWorldAxis();
  2301. if ( master ) {
  2302. normal = planeNormal * master->GetWorldAxis();
  2303. }
  2304. else {
  2305. normal = planeNormal;
  2306. }
  2307. normal.NormalVectors( right, up );
  2308. normal *= 4.0f;
  2309. right *= 4.0f;
  2310. up *= 4.0f;
  2311. gameRenderWorld->DebugLine( colorCyan, a1 - right, a1 + right );
  2312. gameRenderWorld->DebugLine( colorCyan, a1 - up, a1 + up );
  2313. gameRenderWorld->DebugArrow( colorCyan, a1, a1 + normal, 1 );
  2314. }
  2315. /*
  2316. ================
  2317. idAFConstraint_Plane::Save
  2318. ================
  2319. */
  2320. void idAFConstraint_Plane::Save( idSaveGame *saveFile ) const {
  2321. idAFConstraint::Save( saveFile );
  2322. saveFile->WriteVec3( anchor1 );
  2323. saveFile->WriteVec3( anchor2 );
  2324. saveFile->WriteVec3( planeNormal );
  2325. }
  2326. /*
  2327. ================
  2328. idAFConstraint_Plane::Restore
  2329. ================
  2330. */
  2331. void idAFConstraint_Plane::Restore( idRestoreGame *saveFile ) {
  2332. idAFConstraint::Restore( saveFile );
  2333. saveFile->ReadVec3( anchor1 );
  2334. saveFile->ReadVec3( anchor2 );
  2335. saveFile->ReadVec3( planeNormal );
  2336. }
  2337. //===============================================================
  2338. //
  2339. // idAFConstraint_Spring
  2340. //
  2341. //===============================================================
  2342. /*
  2343. ================
  2344. idAFConstraint_Spring::idAFConstraint_Spring
  2345. ================
  2346. */
  2347. idAFConstraint_Spring::idAFConstraint_Spring( const idStr &name, idAFBody *body1, idAFBody *body2 ) {
  2348. assert( body1 );
  2349. type = CONSTRAINT_SPRING;
  2350. this->name = name;
  2351. this->body1 = body1;
  2352. this->body2 = body2;
  2353. InitSize( 1 );
  2354. fl.allowPrimary = false;
  2355. kstretch = kcompress = damping = 1.0f;
  2356. minLength = maxLength = restLength = 0.0f;
  2357. }
  2358. /*
  2359. ================
  2360. idAFConstraint_Spring::SetAnchor
  2361. ================
  2362. */
  2363. void idAFConstraint_Spring::SetAnchor( const idVec3 &worldAnchor1, const idVec3 &worldAnchor2 ) {
  2364. // get anchor relative to center of mass of body1
  2365. anchor1 = ( worldAnchor1 - body1->GetWorldOrigin() ) * body1->GetWorldAxis().Transpose();
  2366. if ( body2 ) {
  2367. // get anchor relative to center of mass of body2
  2368. anchor2 = ( worldAnchor2 - body2->GetWorldOrigin() ) * body2->GetWorldAxis().Transpose();
  2369. }
  2370. else {
  2371. anchor2 = worldAnchor2;
  2372. }
  2373. }
  2374. /*
  2375. ================
  2376. idAFConstraint_Spring::SetSpring
  2377. ================
  2378. */
  2379. void idAFConstraint_Spring::SetSpring( const float stretch, const float compress, const float damping, const float restLength ) {
  2380. assert( stretch >= 0.0f && compress >= 0.0f && restLength >= 0.0f );
  2381. this->kstretch = stretch;
  2382. this->kcompress = compress;
  2383. this->damping = damping;
  2384. this->restLength = restLength;
  2385. }
  2386. /*
  2387. ================
  2388. idAFConstraint_Spring::SetLimit
  2389. ================
  2390. */
  2391. void idAFConstraint_Spring::SetLimit( const float minLength, const float maxLength ) {
  2392. assert( minLength >= 0.0f && maxLength >= 0.0f && maxLength >= minLength );
  2393. this->minLength = minLength;
  2394. this->maxLength = maxLength;
  2395. }
  2396. /*
  2397. ================
  2398. idAFConstraint_Spring::Evaluate
  2399. ================
  2400. */
  2401. void idAFConstraint_Spring::Evaluate( float invTimeStep ) {
  2402. idVec3 a1, a2, velocity1, velocity2, force;
  2403. idVec6 v1, v2;
  2404. float d, dampingForce, length, error;
  2405. bool limit;
  2406. idAFBody *master;
  2407. master = body2 ? body2 : physics->GetMasterBody();
  2408. a1 = body1->GetWorldOrigin() + anchor1 * body1->GetWorldAxis();
  2409. velocity1 = body1->GetPointVelocity( a1 );
  2410. if ( master ) {
  2411. a2 = master->GetWorldOrigin() + anchor2 * master->GetWorldAxis();
  2412. velocity2 = master->GetPointVelocity( a2 );
  2413. }
  2414. else {
  2415. a2 = anchor2;
  2416. velocity2.Zero();
  2417. }
  2418. force = a2 - a1;
  2419. d = force * force;
  2420. if ( d != 0.0f ) {
  2421. dampingForce = damping * idMath::Fabs( (velocity2 - velocity1) * force ) / d;
  2422. }
  2423. else {
  2424. dampingForce = 0.0f;
  2425. }
  2426. length = force.Normalize();
  2427. if ( length > restLength ) {
  2428. if ( kstretch > 0.0f ) {
  2429. idVec3 springForce = force * ( Square( length - restLength ) * kstretch - dampingForce );
  2430. body1->AddForce( a1, springForce );
  2431. if ( master ) {
  2432. master->AddForce( a2, -springForce );
  2433. }
  2434. }
  2435. }
  2436. else {
  2437. if ( kcompress > 0.0f ) {
  2438. idVec3 springForce = force * -( Square( restLength - length ) * kcompress - dampingForce );
  2439. body1->AddForce( a1, springForce );
  2440. if ( master ) {
  2441. master->AddForce( a2, -springForce );
  2442. }
  2443. }
  2444. }
  2445. // check for spring limits
  2446. if ( length < minLength ) {
  2447. force = -force;
  2448. error = minLength - length;
  2449. limit = true;
  2450. }
  2451. else if ( maxLength > 0.0f && length > maxLength ) {
  2452. error = length - maxLength;
  2453. limit = true;
  2454. }
  2455. else {
  2456. error = 0.0f;
  2457. limit = false;
  2458. }
  2459. if ( limit ) {
  2460. a1 -= body1->GetWorldOrigin();
  2461. v1.SubVec3(0) = force;
  2462. v1.SubVec3(1) = a1.Cross( force );
  2463. J1.Set( 1, 6, v1.ToFloatPtr() );
  2464. if ( body2 ) {
  2465. a2 -= body2->GetWorldOrigin();
  2466. v2.SubVec3(0) = -force;
  2467. v2.SubVec3(1) = a2.Cross( -force );
  2468. J2.Set( 1, 6, v2.ToFloatPtr() );
  2469. }
  2470. c1[0] = -( invTimeStep * ERROR_REDUCTION ) * error;
  2471. lo[0] = 0.0f;
  2472. }
  2473. else {
  2474. J1.Zero( 0, 0 );
  2475. J2.Zero( 0, 0 );
  2476. }
  2477. c1.Clamp( -ERROR_REDUCTION_MAX, ERROR_REDUCTION_MAX );
  2478. }
  2479. /*
  2480. ================
  2481. idAFConstraint_Spring::ApplyFriction
  2482. ================
  2483. */
  2484. void idAFConstraint_Spring::ApplyFriction( float invTimeStep ) {
  2485. // no friction
  2486. }
  2487. /*
  2488. ================
  2489. idAFConstraint_Spring::Translate
  2490. ================
  2491. */
  2492. void idAFConstraint_Spring::Translate( const idVec3 &translation ) {
  2493. if ( !body2 ) {
  2494. anchor2 += translation;
  2495. }
  2496. }
  2497. /*
  2498. ================
  2499. idAFConstraint_Spring::Rotate
  2500. ================
  2501. */
  2502. void idAFConstraint_Spring::Rotate( const idRotation &rotation ) {
  2503. if ( !body2 ) {
  2504. anchor2 *= rotation;
  2505. }
  2506. }
  2507. /*
  2508. ================
  2509. idAFConstraint_Spring::GetCenter
  2510. ================
  2511. */
  2512. void idAFConstraint_Spring::GetCenter( idVec3 &center ) {
  2513. idAFBody *master;
  2514. idVec3 a1, a2;
  2515. master = body2 ? body2 : physics->GetMasterBody();
  2516. a1 = body1->GetWorldOrigin() + anchor1 * body1->GetWorldAxis();
  2517. if ( master ) {
  2518. a2 = master->GetWorldOrigin() + anchor2 * master->GetWorldAxis();
  2519. }
  2520. else {
  2521. a2 = anchor2;
  2522. }
  2523. center = ( a1 + a2 ) * 0.5f;
  2524. }
  2525. /*
  2526. ================
  2527. idAFConstraint_Spring::DebugDraw
  2528. ================
  2529. */
  2530. void idAFConstraint_Spring::DebugDraw( void ) {
  2531. idAFBody *master;
  2532. float length;
  2533. idVec3 a1, a2, dir, mid, p;
  2534. master = body2 ? body2 : physics->GetMasterBody();
  2535. a1 = body1->GetWorldOrigin() + anchor1 * body1->GetWorldAxis();
  2536. if ( master ) {
  2537. a2 = master->GetWorldOrigin() + anchor2 * master->GetWorldAxis();
  2538. }
  2539. else {
  2540. a2 = anchor2;
  2541. }
  2542. dir = a2 - a1;
  2543. mid = a1 + 0.5f * dir;
  2544. length = dir.Normalize();
  2545. // draw spring
  2546. gameRenderWorld->DebugLine( colorGreen, a1, a2 );
  2547. // draw rest length
  2548. p = restLength * 0.5f * dir;
  2549. gameRenderWorld->DebugCircle( colorWhite, mid + p, dir, 1.0f, 10 );
  2550. gameRenderWorld->DebugCircle( colorWhite, mid - p, dir, 1.0f, 10 );
  2551. if ( restLength > length ) {
  2552. gameRenderWorld->DebugLine( colorWhite, a2, mid + p );
  2553. gameRenderWorld->DebugLine( colorWhite, a1, mid - p );
  2554. }
  2555. if ( minLength > 0.0f ) {
  2556. // draw min length
  2557. gameRenderWorld->DebugCircle( colorBlue, mid + minLength * 0.5f * dir, dir, 2.0f, 10 );
  2558. gameRenderWorld->DebugCircle( colorBlue, mid - minLength * 0.5f * dir, dir, 2.0f, 10 );
  2559. }
  2560. if ( maxLength > 0.0f ) {
  2561. // draw max length
  2562. gameRenderWorld->DebugCircle( colorRed, mid + maxLength * 0.5f * dir, dir, 2.0f, 10 );
  2563. gameRenderWorld->DebugCircle( colorRed, mid - maxLength * 0.5f * dir, dir, 2.0f, 10 );
  2564. }
  2565. }
  2566. /*
  2567. ================
  2568. idAFConstraint_Spring::Save
  2569. ================
  2570. */
  2571. void idAFConstraint_Spring::Save( idSaveGame *saveFile ) const {
  2572. idAFConstraint::Save( saveFile );
  2573. saveFile->WriteVec3( anchor1 );
  2574. saveFile->WriteVec3( anchor2 );
  2575. saveFile->WriteFloat( kstretch );
  2576. saveFile->WriteFloat( kcompress );
  2577. saveFile->WriteFloat( damping );
  2578. saveFile->WriteFloat( restLength );
  2579. saveFile->WriteFloat( minLength );
  2580. saveFile->WriteFloat( maxLength );
  2581. }
  2582. /*
  2583. ================
  2584. idAFConstraint_Spring::Restore
  2585. ================
  2586. */
  2587. void idAFConstraint_Spring::Restore( idRestoreGame *saveFile ) {
  2588. idAFConstraint::Restore( saveFile );
  2589. saveFile->ReadVec3( anchor1 );
  2590. saveFile->ReadVec3( anchor2 );
  2591. saveFile->ReadFloat( kstretch );
  2592. saveFile->ReadFloat( kcompress );
  2593. saveFile->ReadFloat( damping );
  2594. saveFile->ReadFloat( restLength );
  2595. saveFile->ReadFloat( minLength );
  2596. saveFile->ReadFloat( maxLength );
  2597. }
  2598. //===============================================================
  2599. //
  2600. // idAFConstraint_Contact
  2601. //
  2602. //===============================================================
  2603. /*
  2604. ================
  2605. idAFConstraint_Contact::idAFConstraint_Contact
  2606. ================
  2607. */
  2608. idAFConstraint_Contact::idAFConstraint_Contact( void ) {
  2609. name = "contact";
  2610. type = CONSTRAINT_CONTACT;
  2611. InitSize( 1 );
  2612. fc = NULL;
  2613. fl.allowPrimary = false;
  2614. fl.frameConstraint = true;
  2615. }
  2616. /*
  2617. ================
  2618. idAFConstraint_Contact::~idAFConstraint_Contact
  2619. ================
  2620. */
  2621. idAFConstraint_Contact::~idAFConstraint_Contact( void ) {
  2622. if ( fc ) {
  2623. delete fc;
  2624. }
  2625. }
  2626. /*
  2627. ================
  2628. idAFConstraint_Contact::Setup
  2629. ================
  2630. */
  2631. void idAFConstraint_Contact::Setup( idAFBody *b1, idAFBody *b2, contactInfo_t &c ) {
  2632. idVec3 p;
  2633. idVec6 v;
  2634. float vel;
  2635. float minBounceVelocity = 2.0f;
  2636. assert( b1 );
  2637. body1 = b1;
  2638. body2 = b2;
  2639. contact = c;
  2640. p = c.point - body1->GetWorldOrigin();
  2641. v.SubVec3(0) = c.normal;
  2642. v.SubVec3(1) = p.Cross( c.normal );
  2643. J1.Set( 1, 6, v.ToFloatPtr() );
  2644. vel = v.SubVec3(0) * body1->GetLinearVelocity() + v.SubVec3(1) * body1->GetAngularVelocity();
  2645. if ( body2 ) {
  2646. p = c.point - body2->GetWorldOrigin();
  2647. v.SubVec3(0) = -c.normal;
  2648. v.SubVec3(1) = p.Cross( -c.normal );
  2649. J2.Set( 1, 6, v.ToFloatPtr() );
  2650. vel += v.SubVec3(0) * body2->GetLinearVelocity() + v.SubVec3(1) * body2->GetAngularVelocity();
  2651. c2[0] = 0.0f;
  2652. }
  2653. if ( body1->GetBouncyness() > 0.0f && -vel > minBounceVelocity ) {
  2654. c1[0] = body1->GetBouncyness() * vel;
  2655. } else {
  2656. c1[0] = 0.0f;
  2657. }
  2658. e[0] = CONTACT_LCP_EPSILON;
  2659. lo[0] = 0.0f;
  2660. hi[0] = idMath::INFINITY;
  2661. boxConstraint = NULL;
  2662. boxIndex[0] = -1;
  2663. }
  2664. /*
  2665. ================
  2666. idAFConstraint_Contact::Evaluate
  2667. ================
  2668. */
  2669. void idAFConstraint_Contact::Evaluate( float invTimeStep ) {
  2670. // do nothing
  2671. }
  2672. /*
  2673. ================
  2674. idAFConstraint_Contact::ApplyFriction
  2675. ================
  2676. */
  2677. void idAFConstraint_Contact::ApplyFriction( float invTimeStep ) {
  2678. idVec3 r, velocity, normal, dir1, dir2;
  2679. float friction, magnitude, forceNumerator, forceDenominator;
  2680. idVecX impulse, dv;
  2681. friction = body1->GetContactFriction();
  2682. if ( body2 && body2->GetContactFriction() < friction ) {
  2683. friction = body2->GetContactFriction();
  2684. }
  2685. friction *= physics->GetContactFrictionScale();
  2686. if ( friction <= 0.0f ) {
  2687. return;
  2688. }
  2689. // seperate friction per contact is silly but it's fast and often looks close enough
  2690. if ( af_useImpulseFriction.GetBool() ) {
  2691. impulse.SetData( 6, VECX_ALLOCA( 6 ) );
  2692. dv.SetData( 6, VECX_ALLOCA( 6 ) );
  2693. // calculate velocity in the contact plane
  2694. r = contact.point - body1->GetWorldOrigin();
  2695. velocity = body1->GetLinearVelocity() + body1->GetAngularVelocity().Cross( r );
  2696. velocity -= contact.normal * velocity * contact.normal;
  2697. // get normalized direction of friction and magnitude of velocity
  2698. normal = -velocity;
  2699. magnitude = normal.Normalize();
  2700. forceNumerator = friction * magnitude;
  2701. forceDenominator = body1->GetInverseMass() + ( ( body1->GetInverseWorldInertia() * r.Cross( normal ) ).Cross( r ) * normal );
  2702. impulse.SubVec3(0) = (forceNumerator / forceDenominator) * normal;
  2703. impulse.SubVec3(1) = r.Cross( impulse.SubVec3(0) );
  2704. body1->InverseWorldSpatialInertiaMultiply( dv, impulse.ToFloatPtr() );
  2705. // modify velocity with friction force
  2706. body1->SetLinearVelocity( body1->GetLinearVelocity() + dv.SubVec3(0) );
  2707. body1->SetAngularVelocity( body1->GetAngularVelocity() + dv.SubVec3(1) );
  2708. }
  2709. else {
  2710. if ( !fc ) {
  2711. fc = new idAFConstraint_ContactFriction;
  2712. }
  2713. // call setup each frame because contact constraints are re-used for different bodies
  2714. fc->Setup( this );
  2715. fc->Add( physics, invTimeStep );
  2716. }
  2717. }
  2718. /*
  2719. ================
  2720. idAFConstraint_Contact::Translate
  2721. ================
  2722. */
  2723. void idAFConstraint_Contact::Translate( const idVec3 &translation ) {
  2724. assert( 0 ); // contact should never be translated
  2725. }
  2726. /*
  2727. ================
  2728. idAFConstraint_Contact::Rotate
  2729. ================
  2730. */
  2731. void idAFConstraint_Contact::Rotate( const idRotation &rotation ) {
  2732. assert( 0 ); // contact should never be rotated
  2733. }
  2734. /*
  2735. ================
  2736. idAFConstraint_Contact::GetCenter
  2737. ================
  2738. */
  2739. void idAFConstraint_Contact::GetCenter( idVec3 &center ) {
  2740. center = contact.point;
  2741. }
  2742. /*
  2743. ================
  2744. idAFConstraint_Contact::DebugDraw
  2745. ================
  2746. */
  2747. void idAFConstraint_Contact::DebugDraw( void ) {
  2748. idVec3 x, y;
  2749. contact.normal.NormalVectors( x, y );
  2750. gameRenderWorld->DebugLine( colorWhite, contact.point, contact.point + 6.0f * contact.normal );
  2751. gameRenderWorld->DebugLine( colorWhite, contact.point - 2.0f * x, contact.point + 2.0f * x );
  2752. gameRenderWorld->DebugLine( colorWhite, contact.point - 2.0f * y, contact.point + 2.0f * y );
  2753. }
  2754. //===============================================================
  2755. //
  2756. // idAFConstraint_ContactFriction
  2757. //
  2758. //===============================================================
  2759. /*
  2760. ================
  2761. idAFConstraint_ContactFriction::idAFConstraint_ContactFriction
  2762. ================
  2763. */
  2764. idAFConstraint_ContactFriction::idAFConstraint_ContactFriction( void ) {
  2765. type = CONSTRAINT_FRICTION;
  2766. name = "contactFriction";
  2767. InitSize( 2 );
  2768. cc = NULL;
  2769. fl.allowPrimary = false;
  2770. fl.frameConstraint = true;
  2771. }
  2772. /*
  2773. ================
  2774. idAFConstraint_ContactFriction::Setup
  2775. ================
  2776. */
  2777. void idAFConstraint_ContactFriction::Setup( idAFConstraint_Contact *cc ) {
  2778. this->cc = cc;
  2779. body1 = cc->GetBody1();
  2780. body2 = cc->GetBody2();
  2781. }
  2782. /*
  2783. ================
  2784. idAFConstraint_ContactFriction::Evaluate
  2785. ================
  2786. */
  2787. void idAFConstraint_ContactFriction::Evaluate( float invTimeStep ) {
  2788. // do nothing
  2789. }
  2790. /*
  2791. ================
  2792. idAFConstraint_ContactFriction::ApplyFriction
  2793. ================
  2794. */
  2795. void idAFConstraint_ContactFriction::ApplyFriction( float invTimeStep ) {
  2796. // do nothing
  2797. }
  2798. /*
  2799. ================
  2800. idAFConstraint_ContactFriction::Add
  2801. ================
  2802. */
  2803. bool idAFConstraint_ContactFriction::Add( idPhysics_AF *phys, float invTimeStep ) {
  2804. idVec3 r, dir1, dir2;
  2805. float friction;
  2806. int newRow;
  2807. physics = phys;
  2808. friction = body1->GetContactFriction() * physics->GetContactFrictionScale();
  2809. // if the body only has friction in one direction
  2810. if ( body1->GetFrictionDirection( dir1 ) ) {
  2811. // project the friction direction into the contact plane
  2812. dir1 -= dir1 * cc->GetContact().normal * dir1;
  2813. dir1.Normalize();
  2814. r = cc->GetContact().point - body1->GetWorldOrigin();
  2815. J1.SetSize( 1, 6 );
  2816. J1.SubVec6(0).SubVec3(0) = dir1;
  2817. J1.SubVec6(0).SubVec3(1) = r.Cross( dir1 );
  2818. c1.SetSize( 1 );
  2819. c1[0] = 0.0f;
  2820. if ( body2 ) {
  2821. r = cc->GetContact().point - body2->GetWorldOrigin();
  2822. J2.SetSize( 1, 6 );
  2823. J2.SubVec6(0).SubVec3(0) = -dir1;
  2824. J2.SubVec6(0).SubVec3(1) = r.Cross( -dir1 );
  2825. c2.SetSize( 1 );
  2826. c2[0] = 0.0f;
  2827. }
  2828. lo[0] = -friction;
  2829. hi[0] = friction;
  2830. boxConstraint = cc;
  2831. boxIndex[0] = 0;
  2832. }
  2833. else {
  2834. // get two friction directions orthogonal to contact normal
  2835. cc->GetContact().normal.NormalVectors( dir1, dir2 );
  2836. r = cc->GetContact().point - body1->GetWorldOrigin();
  2837. J1.SetSize( 2, 6 );
  2838. J1.SubVec6(0).SubVec3(0) = dir1;
  2839. J1.SubVec6(0).SubVec3(1) = r.Cross( dir1 );
  2840. J1.SubVec6(1).SubVec3(0) = dir2;
  2841. J1.SubVec6(1).SubVec3(1) = r.Cross( dir2 );
  2842. c1.SetSize( 2 );
  2843. c1[0] = c1[1] = 0.0f;
  2844. if ( body2 ) {
  2845. r = cc->GetContact().point - body2->GetWorldOrigin();
  2846. J2.SetSize( 2, 6 );
  2847. J2.SubVec6(0).SubVec3(0) = -dir1;
  2848. J2.SubVec6(0).SubVec3(1) = r.Cross( -dir1 );
  2849. J2.SubVec6(1).SubVec3(0) = -dir2;
  2850. J2.SubVec6(1).SubVec3(1) = r.Cross( -dir2 );
  2851. c2.SetSize( 2 );
  2852. c2[0] = c2[1] = 0.0f;
  2853. if ( body2->GetContactFriction() < friction ) {
  2854. friction = body2->GetContactFriction();
  2855. }
  2856. }
  2857. lo[0] = -friction;
  2858. hi[0] = friction;
  2859. boxConstraint = cc;
  2860. boxIndex[0] = 0;
  2861. lo[1] = -friction;
  2862. hi[1] = friction;
  2863. boxIndex[1] = 0;
  2864. }
  2865. if ( body1->GetContactMotorDirection( dir1 ) && body1->GetContactMotorForce() > 0.0f ) {
  2866. // project the motor force direction into the contact plane
  2867. dir1 -= dir1 * cc->GetContact().normal * dir1;
  2868. dir1.Normalize();
  2869. r = cc->GetContact().point - body1->GetWorldOrigin();
  2870. newRow = J1.GetNumRows();
  2871. J1.ChangeSize( newRow+1, J1.GetNumColumns() );
  2872. J1.SubVec6(newRow).SubVec3(0) = -dir1;
  2873. J1.SubVec6(newRow).SubVec3(1) = r.Cross( -dir1 );
  2874. c1.ChangeSize( newRow+1 );
  2875. c1[newRow] = body1->GetContactMotorVelocity();
  2876. if ( body2 ) {
  2877. r = cc->GetContact().point - body2->GetWorldOrigin();
  2878. J2.ChangeSize( newRow+1, J2.GetNumColumns() );
  2879. J2.SubVec6(newRow).SubVec3(0) = -dir1;
  2880. J2.SubVec6(newRow).SubVec3(1) = r.Cross( -dir1 );
  2881. c2.ChangeSize( newRow+1 );
  2882. c2[newRow] = 0.0f;
  2883. }
  2884. lo[newRow] = -body1->GetContactMotorForce();
  2885. hi[newRow] = body1->GetContactMotorForce();
  2886. boxIndex[newRow] = -1;
  2887. }
  2888. physics->AddFrameConstraint( this );
  2889. return true;
  2890. }
  2891. /*
  2892. ================
  2893. idAFConstraint_ContactFriction::Translate
  2894. ================
  2895. */
  2896. void idAFConstraint_ContactFriction::Translate( const idVec3 &translation ) {
  2897. }
  2898. /*
  2899. ================
  2900. idAFConstraint_ContactFriction::Rotate
  2901. ================
  2902. */
  2903. void idAFConstraint_ContactFriction::Rotate( const idRotation &rotation ) {
  2904. }
  2905. /*
  2906. ================
  2907. idAFConstraint_ContactFriction::DebugDraw
  2908. ================
  2909. */
  2910. void idAFConstraint_ContactFriction::DebugDraw( void ) {
  2911. }
  2912. //===============================================================
  2913. //
  2914. // idAFConstraint_ConeLimit
  2915. //
  2916. //===============================================================
  2917. /*
  2918. ================
  2919. idAFConstraint_ConeLimit::idAFConstraint_ConeLimit
  2920. ================
  2921. */
  2922. idAFConstraint_ConeLimit::idAFConstraint_ConeLimit( void ) {
  2923. type = CONSTRAINT_CONELIMIT;
  2924. name = "coneLimit";
  2925. InitSize( 1 );
  2926. fl.allowPrimary = false;
  2927. fl.frameConstraint = true;
  2928. }
  2929. /*
  2930. ================
  2931. idAFConstraint_ConeLimit::Setup
  2932. the coneAnchor is the top of the cone in body2 space
  2933. the coneAxis is the axis of the cone in body2 space
  2934. the coneAngle is the angle the cone hull makes at the top
  2935. the body1Axis is the axis in body1 space that should stay within the cone
  2936. ================
  2937. */
  2938. void idAFConstraint_ConeLimit::Setup( idAFBody *b1, idAFBody *b2, const idVec3 &coneAnchor, const idVec3 &coneAxis, const float coneAngle, const idVec3 &body1Axis ) {
  2939. this->body1 = b1;
  2940. this->body2 = b2;
  2941. this->coneAxis = coneAxis;
  2942. this->coneAxis.Normalize();
  2943. this->coneAnchor = coneAnchor;
  2944. this->body1Axis = body1Axis;
  2945. this->body1Axis.Normalize();
  2946. this->cosAngle = (float) cos( DEG2RAD( coneAngle * 0.5f ) );
  2947. this->sinHalfAngle = (float) sin( DEG2RAD( coneAngle * 0.25f ) );
  2948. this->cosHalfAngle = (float) cos( DEG2RAD( coneAngle * 0.25f ) );
  2949. }
  2950. /*
  2951. ================
  2952. idAFConstraint_ConeLimit::SetAnchor
  2953. ================
  2954. */
  2955. void idAFConstraint_ConeLimit::SetAnchor( const idVec3 &coneAnchor ) {
  2956. this->coneAnchor = coneAnchor;
  2957. }
  2958. /*
  2959. ================
  2960. idAFConstraint_ConeLimit::SetBody1Axis
  2961. ================
  2962. */
  2963. void idAFConstraint_ConeLimit::SetBody1Axis( const idVec3 &body1Axis ) {
  2964. this->body1Axis = body1Axis;
  2965. }
  2966. /*
  2967. ================
  2968. idAFConstraint_ConeLimit::Evaluate
  2969. ================
  2970. */
  2971. void idAFConstraint_ConeLimit::Evaluate( float invTimeStep ) {
  2972. // do nothing
  2973. }
  2974. /*
  2975. ================
  2976. idAFConstraint_ConeLimit::ApplyFriction
  2977. ================
  2978. */
  2979. void idAFConstraint_ConeLimit::ApplyFriction( float invTimeStep ) {
  2980. }
  2981. /*
  2982. ================
  2983. idAFConstraint_ConeLimit::Add
  2984. ================
  2985. */
  2986. bool idAFConstraint_ConeLimit::Add( idPhysics_AF *phys, float invTimeStep ) {
  2987. float a;
  2988. idVec6 J1row, J2row;
  2989. idVec3 ax, anchor, body1ax, normal, coneVector, p1, p2;
  2990. idQuat q;
  2991. idAFBody *master;
  2992. if ( af_skipLimits.GetBool() ) {
  2993. lm.Zero(); // constraint exerts no force
  2994. return false;
  2995. }
  2996. physics = phys;
  2997. master = body2 ? body2 : physics->GetMasterBody();
  2998. if ( master ) {
  2999. ax = coneAxis * master->GetWorldAxis();
  3000. anchor = master->GetWorldOrigin() + coneAnchor * master->GetWorldAxis();
  3001. }
  3002. else {
  3003. ax = coneAxis;
  3004. anchor = coneAnchor;
  3005. }
  3006. body1ax = body1Axis * body1->GetWorldAxis();
  3007. a = ax * body1ax;
  3008. // if the body1 axis is inside the cone
  3009. if ( a > cosAngle ) {
  3010. lm.Zero(); // constraint exerts no force
  3011. return false;
  3012. }
  3013. // calculate the inward cone normal for the position the body1 axis went outside the cone
  3014. normal = body1ax.Cross( ax );
  3015. normal.Normalize();
  3016. q.x = normal.x * sinHalfAngle;
  3017. q.y = normal.y * sinHalfAngle;
  3018. q.z = normal.z * sinHalfAngle;
  3019. q.w = cosHalfAngle;
  3020. coneVector = ax * q.ToMat3();
  3021. normal = coneVector.Cross( ax ).Cross( coneVector );
  3022. normal.Normalize();
  3023. p1 = anchor + 32.0f * coneVector - body1->GetWorldOrigin();
  3024. J1row.SubVec3(0) = normal;
  3025. J1row.SubVec3(1) = p1.Cross( normal );
  3026. J1.Set( 1, 6, J1row.ToFloatPtr() );
  3027. c1[0] = (invTimeStep * LIMIT_ERROR_REDUCTION) * ( normal * (32.0f * body1ax) );
  3028. if ( body2 ) {
  3029. p2 = anchor + 32.0f * coneVector - master->GetWorldOrigin();
  3030. J2row.SubVec3(0) = -normal;
  3031. J2row.SubVec3(1) = p2.Cross( -normal );
  3032. J2.Set( 1, 6, J2row.ToFloatPtr() );
  3033. c2[0] = 0.0f;
  3034. }
  3035. lo[0] = 0.0f;
  3036. e[0] = LIMIT_LCP_EPSILON;
  3037. physics->AddFrameConstraint( this );
  3038. return true;
  3039. }
  3040. /*
  3041. ================
  3042. idAFConstraint_ConeLimit::Translate
  3043. ================
  3044. */
  3045. void idAFConstraint_ConeLimit::Translate( const idVec3 &translation ) {
  3046. if ( !body2 ) {
  3047. coneAnchor += translation;
  3048. }
  3049. }
  3050. /*
  3051. ================
  3052. idAFConstraint_ConeLimit::Rotate
  3053. ================
  3054. */
  3055. void idAFConstraint_ConeLimit::Rotate( const idRotation &rotation ) {
  3056. if ( !body2 ) {
  3057. coneAnchor *= rotation;
  3058. coneAxis *= rotation.ToMat3();
  3059. }
  3060. }
  3061. /*
  3062. ================
  3063. idAFConstraint_ConeLimit::DebugDraw
  3064. ================
  3065. */
  3066. void idAFConstraint_ConeLimit::DebugDraw( void ) {
  3067. idVec3 ax, anchor, x, y, z, start, end;
  3068. float sinAngle, a, size = 10.0f;
  3069. idAFBody *master;
  3070. master = body2 ? body2 : physics->GetMasterBody();
  3071. if ( master ) {
  3072. ax = coneAxis * master->GetWorldAxis();
  3073. anchor = master->GetWorldOrigin() + coneAnchor * master->GetWorldAxis();
  3074. }
  3075. else {
  3076. ax = coneAxis;
  3077. anchor = coneAnchor;
  3078. }
  3079. // draw body1 axis
  3080. gameRenderWorld->DebugLine( colorGreen, anchor, anchor + size * (body1Axis * body1->GetWorldAxis()) );
  3081. // draw cone
  3082. ax.NormalVectors( x, y );
  3083. sinAngle = idMath::Sqrt( 1.0f - cosAngle * cosAngle );
  3084. x *= size * sinAngle;
  3085. y *= size * sinAngle;
  3086. z = anchor + ax * size * cosAngle;
  3087. start = x + z;
  3088. for ( a = 0.0f; a < 360.0f; a += 45.0f ) {
  3089. end = x * (float) cos( DEG2RAD(a + 45.0f) ) + y * (float) sin( DEG2RAD(a + 45.0f) ) + z;
  3090. gameRenderWorld->DebugLine( colorMagenta, anchor, start );
  3091. gameRenderWorld->DebugLine( colorMagenta, start, end );
  3092. start = end;
  3093. }
  3094. }
  3095. /*
  3096. ================
  3097. idAFConstraint_ConeLimit::Save
  3098. ================
  3099. */
  3100. void idAFConstraint_ConeLimit::Save( idSaveGame *saveFile ) const {
  3101. idAFConstraint::Save( saveFile );
  3102. saveFile->WriteVec3( coneAnchor );
  3103. saveFile->WriteVec3( coneAxis );
  3104. saveFile->WriteVec3( body1Axis );
  3105. saveFile->WriteFloat( cosAngle );
  3106. saveFile->WriteFloat( sinHalfAngle );
  3107. saveFile->WriteFloat( cosHalfAngle );
  3108. saveFile->WriteFloat( epsilon );
  3109. }
  3110. /*
  3111. ================
  3112. idAFConstraint_ConeLimit::Restore
  3113. ================
  3114. */
  3115. void idAFConstraint_ConeLimit::Restore( idRestoreGame *saveFile ) {
  3116. idAFConstraint::Restore( saveFile );
  3117. saveFile->ReadVec3( coneAnchor );
  3118. saveFile->ReadVec3( coneAxis );
  3119. saveFile->ReadVec3( body1Axis );
  3120. saveFile->ReadFloat( cosAngle );
  3121. saveFile->ReadFloat( sinHalfAngle );
  3122. saveFile->ReadFloat( cosHalfAngle );
  3123. saveFile->ReadFloat( epsilon );
  3124. }
  3125. //===============================================================
  3126. //
  3127. // idAFConstraint_PyramidLimit
  3128. //
  3129. //===============================================================
  3130. /*
  3131. ================
  3132. idAFConstraint_PyramidLimit::idAFConstraint_PyramidLimit
  3133. ================
  3134. */
  3135. idAFConstraint_PyramidLimit::idAFConstraint_PyramidLimit( void ) {
  3136. type = CONSTRAINT_PYRAMIDLIMIT;
  3137. name = "pyramidLimit";
  3138. InitSize( 1 );
  3139. fl.allowPrimary = false;
  3140. fl.frameConstraint = true;
  3141. }
  3142. /*
  3143. ================
  3144. idAFConstraint_PyramidLimit::Setup
  3145. ================
  3146. */
  3147. void idAFConstraint_PyramidLimit::Setup( idAFBody *b1, idAFBody *b2, const idVec3 &pyramidAnchor,
  3148. const idVec3 &pyramidAxis, const idVec3 &baseAxis,
  3149. const float pyramidAngle1, const float pyramidAngle2, const idVec3 &body1Axis ) {
  3150. body1 = b1;
  3151. body2 = b2;
  3152. // setup the base and make sure the basis is orthonormal
  3153. pyramidBasis[2] = pyramidAxis;
  3154. pyramidBasis[2].Normalize();
  3155. pyramidBasis[0] = baseAxis;
  3156. pyramidBasis[0] -= pyramidBasis[2] * baseAxis * pyramidBasis[2];
  3157. pyramidBasis[0].Normalize();
  3158. pyramidBasis[1] = pyramidBasis[0].Cross( pyramidBasis[2] );
  3159. // pyramid top
  3160. this->pyramidAnchor = pyramidAnchor;
  3161. // angles
  3162. cosAngle[0] = (float) cos( DEG2RAD( pyramidAngle1 * 0.5f ) );
  3163. cosAngle[1] = (float) cos( DEG2RAD( pyramidAngle2 * 0.5f ) );
  3164. sinHalfAngle[0] = (float) sin( DEG2RAD( pyramidAngle1 * 0.25f ) );
  3165. sinHalfAngle[1] = (float) sin( DEG2RAD( pyramidAngle2 * 0.25f ) );
  3166. cosHalfAngle[0] = (float) cos( DEG2RAD( pyramidAngle1 * 0.25f ) );
  3167. cosHalfAngle[1] = (float) cos( DEG2RAD( pyramidAngle2 * 0.25f ) );
  3168. this->body1Axis = body1Axis;
  3169. }
  3170. /*
  3171. ================
  3172. idAFConstraint_PyramidLimit::SetAnchor
  3173. ================
  3174. */
  3175. void idAFConstraint_PyramidLimit::SetAnchor( const idVec3 &pyramidAnchor ) {
  3176. this->pyramidAnchor = pyramidAnchor;
  3177. }
  3178. /*
  3179. ================
  3180. idAFConstraint_PyramidLimit::SetBody1Axis
  3181. ================
  3182. */
  3183. void idAFConstraint_PyramidLimit::SetBody1Axis( const idVec3 &body1Axis ) {
  3184. this->body1Axis = body1Axis;
  3185. }
  3186. /*
  3187. ================
  3188. idAFConstraint_PyramidLimit::Evaluate
  3189. ================
  3190. */
  3191. void idAFConstraint_PyramidLimit::Evaluate( float invTimeStep ) {
  3192. // do nothing
  3193. }
  3194. /*
  3195. ================
  3196. idAFConstraint_PyramidLimit::ApplyFriction
  3197. ================
  3198. */
  3199. void idAFConstraint_PyramidLimit::ApplyFriction( float invTimeStep ) {
  3200. }
  3201. /*
  3202. ================
  3203. idAFConstraint_PyramidLimit::Add
  3204. ================
  3205. */
  3206. bool idAFConstraint_PyramidLimit::Add( idPhysics_AF *phys, float invTimeStep ) {
  3207. int i;
  3208. float a[2];
  3209. idVec6 J1row, J2row;
  3210. idMat3 worldBase;
  3211. idVec3 anchor, body1ax, ax[2], v, normal, pyramidVector, p1, p2;
  3212. idQuat q;
  3213. idAFBody *master;
  3214. if ( af_skipLimits.GetBool() ) {
  3215. lm.Zero(); // constraint exerts no force
  3216. return false;
  3217. }
  3218. physics = phys;
  3219. master = body2 ? body2 : physics->GetMasterBody();
  3220. if ( master ) {
  3221. worldBase[0] = pyramidBasis[0] * master->GetWorldAxis();
  3222. worldBase[1] = pyramidBasis[1] * master->GetWorldAxis();
  3223. worldBase[2] = pyramidBasis[2] * master->GetWorldAxis();
  3224. anchor = master->GetWorldOrigin() + pyramidAnchor * master->GetWorldAxis();
  3225. }
  3226. else {
  3227. worldBase = pyramidBasis;
  3228. anchor = pyramidAnchor;
  3229. }
  3230. body1ax = body1Axis * body1->GetWorldAxis();
  3231. for ( i = 0; i < 2; i++ ) {
  3232. ax[i] = body1ax - worldBase[!i] * body1ax * worldBase[!i];
  3233. ax[i].Normalize();
  3234. a[i] = worldBase[2] * ax[i];
  3235. }
  3236. // if the body1 axis is inside the pyramid
  3237. if ( a[0] > cosAngle[0] && a[1] > cosAngle[1] ) {
  3238. lm.Zero(); // constraint exerts no force
  3239. return false;
  3240. }
  3241. // calculate the inward pyramid normal for the position the body1 axis went outside the pyramid
  3242. pyramidVector = worldBase[2];
  3243. for ( i = 0; i < 2; i++ ) {
  3244. if ( a[i] <= cosAngle[i] ) {
  3245. v = ax[i].Cross( worldBase[2] );
  3246. v.Normalize();
  3247. q.x = v.x * sinHalfAngle[i];
  3248. q.y = v.y * sinHalfAngle[i];
  3249. q.z = v.z * sinHalfAngle[i];
  3250. q.w = cosHalfAngle[i];
  3251. pyramidVector *= q.ToMat3();
  3252. }
  3253. }
  3254. normal = pyramidVector.Cross( worldBase[2] ).Cross( pyramidVector );
  3255. normal.Normalize();
  3256. p1 = anchor + 32.0f * pyramidVector - body1->GetWorldOrigin();
  3257. J1row.SubVec3(0) = normal;
  3258. J1row.SubVec3(1) = p1.Cross( normal );
  3259. J1.Set( 1, 6, J1row.ToFloatPtr() );
  3260. c1[0] = (invTimeStep * LIMIT_ERROR_REDUCTION) * ( normal * (32.0f * body1ax) );
  3261. if ( body2 ) {
  3262. p2 = anchor + 32.0f * pyramidVector - master->GetWorldOrigin();
  3263. J2row.SubVec3(0) = -normal;
  3264. J2row.SubVec3(1) = p2.Cross( -normal );
  3265. J2.Set( 1, 6, J2row.ToFloatPtr() );
  3266. c2[0] = 0.0f;
  3267. }
  3268. lo[0] = 0.0f;
  3269. e[0] = LIMIT_LCP_EPSILON;
  3270. physics->AddFrameConstraint( this );
  3271. return true;
  3272. }
  3273. /*
  3274. ================
  3275. idAFConstraint_PyramidLimit::Translate
  3276. ================
  3277. */
  3278. void idAFConstraint_PyramidLimit::Translate( const idVec3 &translation ) {
  3279. if ( !body2 ) {
  3280. pyramidAnchor += translation;
  3281. }
  3282. }
  3283. /*
  3284. ================
  3285. idAFConstraint_PyramidLimit::Rotate
  3286. ================
  3287. */
  3288. void idAFConstraint_PyramidLimit::Rotate( const idRotation &rotation ) {
  3289. if ( !body2 ) {
  3290. pyramidAnchor *= rotation;
  3291. pyramidBasis[0] *= rotation.ToMat3();
  3292. pyramidBasis[1] *= rotation.ToMat3();
  3293. pyramidBasis[2] *= rotation.ToMat3();
  3294. }
  3295. }
  3296. /*
  3297. ================
  3298. idAFConstraint_PyramidLimit::DebugDraw
  3299. ================
  3300. */
  3301. void idAFConstraint_PyramidLimit::DebugDraw( void ) {
  3302. int i;
  3303. float size = 10.0f;
  3304. idVec3 anchor, dir, p[4];
  3305. idMat3 worldBase, m[2];
  3306. idQuat q;
  3307. idAFBody *master;
  3308. master = body2 ? body2 : physics->GetMasterBody();
  3309. if ( master ) {
  3310. worldBase[0] = pyramidBasis[0] * master->GetWorldAxis();
  3311. worldBase[1] = pyramidBasis[1] * master->GetWorldAxis();
  3312. worldBase[2] = pyramidBasis[2] * master->GetWorldAxis();
  3313. anchor = master->GetWorldOrigin() + pyramidAnchor * master->GetWorldAxis();
  3314. }
  3315. else {
  3316. worldBase = pyramidBasis;
  3317. anchor = pyramidAnchor;
  3318. }
  3319. // draw body1 axis
  3320. gameRenderWorld->DebugLine( colorGreen, anchor, anchor + size * (body1Axis * body1->GetWorldAxis()) );
  3321. // draw the pyramid
  3322. for ( i = 0; i < 2; i++ ) {
  3323. q.x = worldBase[!i].x * sinHalfAngle[i];
  3324. q.y = worldBase[!i].y * sinHalfAngle[i];
  3325. q.z = worldBase[!i].z * sinHalfAngle[i];
  3326. q.w = cosHalfAngle[i];
  3327. m[i] = q.ToMat3();
  3328. }
  3329. dir = worldBase[2] * size;
  3330. p[0] = anchor + m[0] * (m[1] * dir);
  3331. p[1] = anchor + m[0] * (m[1].Transpose() * dir);
  3332. p[2] = anchor + m[0].Transpose() * (m[1].Transpose() * dir);
  3333. p[3] = anchor + m[0].Transpose() * (m[1] * dir);
  3334. for ( i = 0; i < 4; i++ ) {
  3335. gameRenderWorld->DebugLine( colorMagenta, anchor, p[i] );
  3336. gameRenderWorld->DebugLine( colorMagenta, p[i], p[(i+1)&3] );
  3337. }
  3338. }
  3339. /*
  3340. ================
  3341. idAFConstraint_PyramidLimit::Save
  3342. ================
  3343. */
  3344. void idAFConstraint_PyramidLimit::Save( idSaveGame *saveFile ) const {
  3345. idAFConstraint::Save( saveFile );
  3346. saveFile->WriteVec3( pyramidAnchor );
  3347. saveFile->WriteMat3( pyramidBasis );
  3348. saveFile->WriteVec3( body1Axis );
  3349. saveFile->WriteFloat( cosAngle[0] );
  3350. saveFile->WriteFloat( cosAngle[1] );
  3351. saveFile->WriteFloat( sinHalfAngle[0] );
  3352. saveFile->WriteFloat( sinHalfAngle[1] );
  3353. saveFile->WriteFloat( cosHalfAngle[0] );
  3354. saveFile->WriteFloat( cosHalfAngle[1] );
  3355. saveFile->WriteFloat( epsilon );
  3356. }
  3357. /*
  3358. ================
  3359. idAFConstraint_PyramidLimit::Restore
  3360. ================
  3361. */
  3362. void idAFConstraint_PyramidLimit::Restore( idRestoreGame *saveFile ) {
  3363. idAFConstraint::Restore( saveFile );
  3364. saveFile->ReadVec3( pyramidAnchor );
  3365. saveFile->ReadMat3( pyramidBasis );
  3366. saveFile->ReadVec3( body1Axis );
  3367. saveFile->ReadFloat( cosAngle[0] );
  3368. saveFile->ReadFloat( cosAngle[1] );
  3369. saveFile->ReadFloat( sinHalfAngle[0] );
  3370. saveFile->ReadFloat( sinHalfAngle[1] );
  3371. saveFile->ReadFloat( cosHalfAngle[0] );
  3372. saveFile->ReadFloat( cosHalfAngle[1] );
  3373. saveFile->ReadFloat( epsilon );
  3374. }
  3375. //===============================================================
  3376. //
  3377. // idAFConstraint_Suspension
  3378. //
  3379. //===============================================================
  3380. /*
  3381. ================
  3382. idAFConstraint_Suspension::idAFConstraint_Suspension
  3383. ================
  3384. */
  3385. idAFConstraint_Suspension::idAFConstraint_Suspension( void ) {
  3386. type = CONSTRAINT_SUSPENSION;
  3387. name = "suspension";
  3388. InitSize( 3 );
  3389. fl.allowPrimary = false;
  3390. fl.frameConstraint = true;
  3391. localOrigin.Zero();
  3392. localAxis.Identity();
  3393. suspensionUp = 0.0f;
  3394. suspensionDown = 0.0f;
  3395. suspensionKCompress = 0.0f;
  3396. suspensionDamping = 0.0f;
  3397. steerAngle = 0.0f;
  3398. friction = 2.0f;
  3399. motorEnabled = false;
  3400. motorForce = 0.0f;
  3401. motorVelocity = 0.0f;
  3402. wheelModel = NULL;
  3403. memset( &trace, 0, sizeof( trace ) );
  3404. epsilon = LCP_EPSILON;
  3405. }
  3406. /*
  3407. ================
  3408. idAFConstraint_Suspension::Setup
  3409. ================
  3410. */
  3411. void idAFConstraint_Suspension::Setup( const char *name, idAFBody *body, const idVec3 &origin, const idMat3 &axis, idClipModel *clipModel ) {
  3412. this->name = name;
  3413. body1 = body;
  3414. body2 = NULL;
  3415. localOrigin = ( origin - body->GetWorldOrigin() ) * body->GetWorldAxis().Transpose();
  3416. localAxis = axis * body->GetWorldAxis().Transpose();
  3417. wheelModel = clipModel;
  3418. }
  3419. /*
  3420. ================
  3421. idAFConstraint_Suspension::SetSuspension
  3422. ================
  3423. */
  3424. void idAFConstraint_Suspension::SetSuspension( const float up, const float down, const float k, const float d, const float f ) {
  3425. suspensionUp = up;
  3426. suspensionDown = down;
  3427. suspensionKCompress = k;
  3428. suspensionDamping = d;
  3429. friction = f;
  3430. }
  3431. /*
  3432. ================
  3433. idAFConstraint_Suspension::GetWheelOrigin
  3434. ================
  3435. */
  3436. const idVec3 idAFConstraint_Suspension::GetWheelOrigin( void ) const {
  3437. return body1->GetWorldOrigin() + wheelOffset * body1->GetWorldAxis();
  3438. }
  3439. /*
  3440. ================
  3441. idAFConstraint_Suspension::Evaluate
  3442. ================
  3443. */
  3444. void idAFConstraint_Suspension::Evaluate( float invTimeStep ) {
  3445. float velocity, suspensionLength, springLength, compression, dampingForce, springForce;
  3446. idVec3 origin, start, end, vel1, vel2, springDir, r, frictionDir, motorDir;
  3447. idMat3 axis;
  3448. idRotation rotation;
  3449. axis = localAxis * body1->GetWorldAxis();
  3450. origin = body1->GetWorldOrigin() + localOrigin * body1->GetWorldAxis();
  3451. start = origin + suspensionUp * axis[2];
  3452. end = origin - suspensionDown * axis[2];
  3453. rotation.SetVec( axis[2] );
  3454. rotation.SetAngle( steerAngle );
  3455. axis *= rotation.ToMat3();
  3456. gameLocal.clip.Translation( trace, start, end, wheelModel, axis, MASK_SOLID, NULL );
  3457. wheelOffset = ( trace.endpos - body1->GetWorldOrigin() ) * body1->GetWorldAxis().Transpose();
  3458. if ( trace.fraction >= 1.0f ) {
  3459. J1.SetSize( 0, 6 );
  3460. if ( body2 ) {
  3461. J2.SetSize( 0, 6 );
  3462. }
  3463. return;
  3464. }
  3465. // calculate and add spring force
  3466. vel1 = body1->GetPointVelocity( start );
  3467. if ( body2 ) {
  3468. vel2 = body2->GetPointVelocity( trace.c.point );
  3469. } else {
  3470. vel2.Zero();
  3471. }
  3472. suspensionLength = suspensionUp + suspensionDown;
  3473. springDir = trace.endpos - start;
  3474. springLength = trace.fraction * suspensionLength;
  3475. dampingForce = suspensionDamping * idMath::Fabs( ( vel2 - vel1 ) * springDir ) / ( 1.0f + springLength * springLength );
  3476. compression = suspensionLength - springLength;
  3477. springForce = compression * compression * suspensionKCompress - dampingForce;
  3478. r = trace.c.point - body1->GetWorldOrigin();
  3479. J1.SetSize( 2, 6 );
  3480. J1.SubVec6(0).SubVec3(0) = trace.c.normal;
  3481. J1.SubVec6(0).SubVec3(1) = r.Cross( trace.c.normal );
  3482. c1.SetSize( 2 );
  3483. c1[0] = 0.0f;
  3484. velocity = J1.SubVec6(0).SubVec3(0) * body1->GetLinearVelocity() + J1.SubVec6(0).SubVec3(1) * body1->GetAngularVelocity();
  3485. if ( body2 ) {
  3486. r = trace.c.point - body2->GetWorldOrigin();
  3487. J2.SetSize( 2, 6 );
  3488. J2.SubVec6(0).SubVec3(0) = -trace.c.normal;
  3489. J2.SubVec6(0).SubVec3(1) = r.Cross( -trace.c.normal );
  3490. c2.SetSize( 2 );
  3491. c2[0] = 0.0f;
  3492. velocity += J2.SubVec6(0).SubVec3(0) * body2->GetLinearVelocity() + J2.SubVec6(0).SubVec3(1) * body2->GetAngularVelocity();
  3493. }
  3494. c1[0] = -compression; // + 0.5f * -velocity;
  3495. e[0] = 1e-4f;
  3496. lo[0] = 0.0f;
  3497. hi[0] = springForce;
  3498. boxConstraint = NULL;
  3499. boxIndex[0] = -1;
  3500. // project the friction direction into the contact plane
  3501. frictionDir = axis[1] - axis[1] * trace.c.normal * axis[1];
  3502. frictionDir.Normalize();
  3503. r = trace.c.point - body1->GetWorldOrigin();
  3504. J1.SubVec6(1).SubVec3(0) = frictionDir;
  3505. J1.SubVec6(1).SubVec3(1) = r.Cross( frictionDir );
  3506. c1[1] = 0.0f;
  3507. if ( body2 ) {
  3508. r = trace.c.point - body2->GetWorldOrigin();
  3509. J2.SubVec6(1).SubVec3(0) = -frictionDir;
  3510. J2.SubVec6(1).SubVec3(1) = r.Cross( -frictionDir );
  3511. c2[1] = 0.0f;
  3512. }
  3513. lo[1] = -friction * physics->GetContactFrictionScale();
  3514. hi[1] = friction * physics->GetContactFrictionScale();
  3515. boxConstraint = this;
  3516. boxIndex[1] = 0;
  3517. if ( motorEnabled ) {
  3518. // project the motor force direction into the contact plane
  3519. motorDir = axis[0] - axis[0] * trace.c.normal * axis[0];
  3520. motorDir.Normalize();
  3521. r = trace.c.point - body1->GetWorldOrigin();
  3522. J1.ChangeSize( 3, J1.GetNumColumns() );
  3523. J1.SubVec6(2).SubVec3(0) = -motorDir;
  3524. J1.SubVec6(2).SubVec3(1) = r.Cross( -motorDir );
  3525. c1.ChangeSize( 3 );
  3526. c1[2] = motorVelocity;
  3527. if ( body2 ) {
  3528. r = trace.c.point - body2->GetWorldOrigin();
  3529. J2.ChangeSize( 3, J2.GetNumColumns() );
  3530. J2.SubVec6(2).SubVec3(0) = -motorDir;
  3531. J2.SubVec6(2).SubVec3(1) = r.Cross( -motorDir );
  3532. c2.ChangeSize( 3 );
  3533. c2[2] = 0.0f;
  3534. }
  3535. lo[2] = -motorForce;
  3536. hi[2] = motorForce;
  3537. boxIndex[2] = -1;
  3538. }
  3539. }
  3540. /*
  3541. ================
  3542. idAFConstraint_Suspension::ApplyFriction
  3543. ================
  3544. */
  3545. void idAFConstraint_Suspension::ApplyFriction( float invTimeStep ) {
  3546. // do nothing
  3547. }
  3548. /*
  3549. ================
  3550. idAFConstraint_Suspension::Translate
  3551. ================
  3552. */
  3553. void idAFConstraint_Suspension::Translate( const idVec3 &translation ) {
  3554. }
  3555. /*
  3556. ================
  3557. idAFConstraint_Suspension::Rotate
  3558. ================
  3559. */
  3560. void idAFConstraint_Suspension::Rotate( const idRotation &rotation ) {
  3561. }
  3562. /*
  3563. ================
  3564. idAFConstraint_Suspension::DebugDraw
  3565. ================
  3566. */
  3567. void idAFConstraint_Suspension::DebugDraw( void ) {
  3568. idVec3 origin;
  3569. idMat3 axis;
  3570. idRotation rotation;
  3571. axis = localAxis * body1->GetWorldAxis();
  3572. rotation.SetVec( axis[2] );
  3573. rotation.SetAngle( steerAngle );
  3574. axis *= rotation.ToMat3();
  3575. if ( trace.fraction < 1.0f ) {
  3576. origin = trace.c.point;
  3577. gameRenderWorld->DebugLine( colorWhite, origin, origin + 6.0f * axis[2] );
  3578. gameRenderWorld->DebugLine( colorWhite, origin - 4.0f * axis[0], origin + 4.0f * axis[0] );
  3579. gameRenderWorld->DebugLine( colorWhite, origin - 2.0f * axis[1], origin + 2.0f * axis[1] );
  3580. }
  3581. }
  3582. //===============================================================
  3583. //
  3584. // idAFBody
  3585. //
  3586. //===============================================================
  3587. /*
  3588. ================
  3589. idAFBody::idAFBody
  3590. ================
  3591. */
  3592. idAFBody::idAFBody( void ) {
  3593. Init();
  3594. }
  3595. /*
  3596. ================
  3597. idAFBody::idAFBody
  3598. ================
  3599. */
  3600. idAFBody::idAFBody( const idStr &name, idClipModel *clipModel, float density ) {
  3601. assert( clipModel );
  3602. assert( clipModel->IsTraceModel() );
  3603. Init();
  3604. this->name = name;
  3605. this->clipModel = NULL;
  3606. SetClipModel( clipModel );
  3607. SetDensity( density );
  3608. current->worldOrigin = clipModel->GetOrigin();
  3609. current->worldAxis = clipModel->GetAxis();
  3610. *next = *current;
  3611. }
  3612. /*
  3613. ================
  3614. idAFBody::~idAFBody
  3615. ================
  3616. */
  3617. idAFBody::~idAFBody( void ) {
  3618. delete clipModel;
  3619. }
  3620. /*
  3621. ================
  3622. idAFBody::Init
  3623. ================
  3624. */
  3625. void idAFBody::Init( void ) {
  3626. name = "noname";
  3627. parent = NULL;
  3628. clipModel = NULL;
  3629. primaryConstraint = NULL;
  3630. tree = NULL;
  3631. linearFriction = -1.0f;
  3632. angularFriction = -1.0f;
  3633. contactFriction = -1.0f;
  3634. bouncyness = -1.0f;
  3635. clipMask = 0;
  3636. frictionDir = vec3_zero;
  3637. contactMotorDir = vec3_zero;
  3638. contactMotorVelocity = 0.0f;
  3639. contactMotorForce = 0.0f;
  3640. mass = 1.0f;
  3641. invMass = 1.0f;
  3642. centerOfMass = vec3_zero;
  3643. inertiaTensor = mat3_identity;
  3644. inverseInertiaTensor = mat3_identity;
  3645. current = &state[0];
  3646. next = &state[1];
  3647. current->worldOrigin = vec3_zero;
  3648. current->worldAxis = mat3_identity;
  3649. current->spatialVelocity = vec6_zero;
  3650. current->externalForce = vec6_zero;
  3651. *next = *current;
  3652. saved = *current;
  3653. atRestOrigin = vec3_zero;
  3654. atRestAxis = mat3_identity;
  3655. s.Zero( 6 );
  3656. totalForce.Zero( 6 );
  3657. auxForce.Zero( 6 );
  3658. acceleration.Zero( 6 );
  3659. response = NULL;
  3660. responseIndex = NULL;
  3661. numResponses = 0;
  3662. maxAuxiliaryIndex = 0;
  3663. maxSubTreeAuxiliaryIndex = 0;
  3664. memset( &fl, 0, sizeof( fl ) );
  3665. fl.selfCollision = true;
  3666. fl.isZero = true;
  3667. }
  3668. /*
  3669. ================
  3670. idAFBody::SetClipModel
  3671. ================
  3672. */
  3673. void idAFBody::SetClipModel( idClipModel *clipModel ) {
  3674. if ( this->clipModel && this->clipModel != clipModel ) {
  3675. delete this->clipModel;
  3676. }
  3677. this->clipModel = clipModel;
  3678. }
  3679. /*
  3680. ================
  3681. idAFBody::SetFriction
  3682. ================
  3683. */
  3684. void idAFBody::SetFriction( float linear, float angular, float contact ) {
  3685. if ( linear < 0.0f || linear > 1.0f ||
  3686. angular < 0.0f || angular > 1.0f ||
  3687. contact < 0.0f ) {
  3688. gameLocal.Warning( "idAFBody::SetFriction: friction out of range, linear = %.1f, angular = %.1f, contact = %.1f", linear, angular, contact );
  3689. return;
  3690. }
  3691. linearFriction = linear;
  3692. angularFriction = angular;
  3693. contactFriction = contact;
  3694. }
  3695. /*
  3696. ================
  3697. idAFBody::SetBouncyness
  3698. ================
  3699. */
  3700. void idAFBody::SetBouncyness( float bounce ) {
  3701. if ( bounce < 0.0f || bounce > 1.0f ) {
  3702. gameLocal.Warning( "idAFBody::SetBouncyness: bouncyness out of range, bounce = %.1f", bounce );
  3703. return;
  3704. }
  3705. bouncyness = bounce;
  3706. }
  3707. /*
  3708. ================
  3709. idAFBody::SetDensity
  3710. ================
  3711. */
  3712. void idAFBody::SetDensity( float density, const idMat3 &inertiaScale ) {
  3713. // get the body mass properties
  3714. clipModel->GetMassProperties( density, mass, centerOfMass, inertiaTensor );
  3715. // make sure we have a valid mass
  3716. if ( mass <= 0.0f || FLOAT_IS_NAN( mass ) ) {
  3717. gameLocal.Warning( "idAFBody::SetDensity: invalid mass for body '%s'", name.c_str() );
  3718. mass = 1.0f;
  3719. centerOfMass.Zero();
  3720. inertiaTensor.Identity();
  3721. }
  3722. // make sure the center of mass is at the body origin
  3723. if ( !centerOfMass.Compare( vec3_origin, CENTER_OF_MASS_EPSILON ) ) {
  3724. gameLocal.Warning( "idAFBody::SetDentity: center of mass not at origin for body '%s'", name.c_str() );
  3725. }
  3726. centerOfMass.Zero();
  3727. // calculate the inverse mass and inverse inertia tensor
  3728. invMass = 1.0f / mass;
  3729. if ( inertiaScale != mat3_identity ) {
  3730. inertiaTensor *= inertiaScale;
  3731. }
  3732. if ( inertiaTensor.IsDiagonal( 1e-3f ) ) {
  3733. inertiaTensor[0][1] = inertiaTensor[0][2] = 0.0f;
  3734. inertiaTensor[1][0] = inertiaTensor[1][2] = 0.0f;
  3735. inertiaTensor[2][0] = inertiaTensor[2][1] = 0.0f;
  3736. inverseInertiaTensor.Identity();
  3737. inverseInertiaTensor[0][0] = 1.0f / inertiaTensor[0][0];
  3738. inverseInertiaTensor[1][1] = 1.0f / inertiaTensor[1][1];
  3739. inverseInertiaTensor[2][2] = 1.0f / inertiaTensor[2][2];
  3740. }
  3741. else {
  3742. inverseInertiaTensor = inertiaTensor.Inverse();
  3743. }
  3744. }
  3745. /*
  3746. ================
  3747. idAFBody::SetFrictionDirection
  3748. ================
  3749. */
  3750. void idAFBody::SetFrictionDirection( const idVec3 &dir ) {
  3751. frictionDir = dir * current->worldAxis.Transpose();
  3752. fl.useFrictionDir = true;
  3753. }
  3754. /*
  3755. ================
  3756. idAFBody::GetFrictionDirection
  3757. ================
  3758. */
  3759. bool idAFBody::GetFrictionDirection( idVec3 &dir ) const {
  3760. if ( fl.useFrictionDir ) {
  3761. dir = frictionDir * current->worldAxis;
  3762. return true;
  3763. }
  3764. return false;
  3765. }
  3766. /*
  3767. ================
  3768. idAFBody::SetContactMotorDirection
  3769. ================
  3770. */
  3771. void idAFBody::SetContactMotorDirection( const idVec3 &dir ) {
  3772. contactMotorDir = dir * current->worldAxis.Transpose();
  3773. fl.useContactMotorDir = true;
  3774. }
  3775. /*
  3776. ================
  3777. idAFBody::GetContactMotorDirection
  3778. ================
  3779. */
  3780. bool idAFBody::GetContactMotorDirection( idVec3 &dir ) const {
  3781. if ( fl.useContactMotorDir ) {
  3782. dir = contactMotorDir * current->worldAxis;
  3783. return true;
  3784. }
  3785. return false;
  3786. }
  3787. /*
  3788. ================
  3789. idAFBody::GetPointVelocity
  3790. ================
  3791. */
  3792. idVec3 idAFBody::GetPointVelocity( const idVec3 &point ) const {
  3793. idVec3 r = point - current->worldOrigin;
  3794. return current->spatialVelocity.SubVec3(0) + current->spatialVelocity.SubVec3(1).Cross( r );
  3795. }
  3796. /*
  3797. ================
  3798. idAFBody::AddForce
  3799. ================
  3800. */
  3801. void idAFBody::AddForce( const idVec3 &point, const idVec3 &force ) {
  3802. current->externalForce.SubVec3(0) += force;
  3803. current->externalForce.SubVec3(1) += (point - current->worldOrigin).Cross( force );
  3804. }
  3805. /*
  3806. ================
  3807. idAFBody::InverseWorldSpatialInertiaMultiply
  3808. dst = this->inverseWorldSpatialInertia * v;
  3809. ================
  3810. */
  3811. ID_INLINE void idAFBody::InverseWorldSpatialInertiaMultiply( idVecX &dst, const float *v ) const {
  3812. const float *mPtr = inverseWorldSpatialInertia.ToFloatPtr();
  3813. const float *vPtr = v;
  3814. float *dstPtr = dst.ToFloatPtr();
  3815. if ( fl.spatialInertiaSparse ) {
  3816. dstPtr[0] = mPtr[0*6+0] * vPtr[0];
  3817. dstPtr[1] = mPtr[1*6+1] * vPtr[1];
  3818. dstPtr[2] = mPtr[2*6+2] * vPtr[2];
  3819. dstPtr[3] = mPtr[3*6+3] * vPtr[3] + mPtr[3*6+4] * vPtr[4] + mPtr[3*6+5] * vPtr[5];
  3820. dstPtr[4] = mPtr[4*6+3] * vPtr[3] + mPtr[4*6+4] * vPtr[4] + mPtr[4*6+5] * vPtr[5];
  3821. dstPtr[5] = mPtr[5*6+3] * vPtr[3] + mPtr[5*6+4] * vPtr[4] + mPtr[5*6+5] * vPtr[5];
  3822. } else {
  3823. gameLocal.Warning( "spatial inertia is not sparse for body %s", name.c_str() );
  3824. }
  3825. }
  3826. /*
  3827. ================
  3828. idAFBody::Save
  3829. ================
  3830. */
  3831. void idAFBody::Save( idSaveGame *saveFile ) {
  3832. saveFile->WriteFloat( linearFriction );
  3833. saveFile->WriteFloat( angularFriction );
  3834. saveFile->WriteFloat( contactFriction );
  3835. saveFile->WriteFloat( bouncyness );
  3836. saveFile->WriteInt( clipMask );
  3837. saveFile->WriteVec3( frictionDir );
  3838. saveFile->WriteVec3( contactMotorDir );
  3839. saveFile->WriteFloat( contactMotorVelocity );
  3840. saveFile->WriteFloat( contactMotorForce );
  3841. saveFile->WriteFloat( mass );
  3842. saveFile->WriteFloat( invMass );
  3843. saveFile->WriteVec3( centerOfMass );
  3844. saveFile->WriteMat3( inertiaTensor );
  3845. saveFile->WriteMat3( inverseInertiaTensor );
  3846. saveFile->WriteVec3( current->worldOrigin );
  3847. saveFile->WriteMat3( current->worldAxis );
  3848. saveFile->WriteVec6( current->spatialVelocity );
  3849. saveFile->WriteVec6( current->externalForce );
  3850. saveFile->WriteVec3( atRestOrigin );
  3851. saveFile->WriteMat3( atRestAxis );
  3852. }
  3853. /*
  3854. ================
  3855. idAFBody::Restore
  3856. ================
  3857. */
  3858. void idAFBody::Restore( idRestoreGame *saveFile ) {
  3859. saveFile->ReadFloat( linearFriction );
  3860. saveFile->ReadFloat( angularFriction );
  3861. saveFile->ReadFloat( contactFriction );
  3862. saveFile->ReadFloat( bouncyness );
  3863. saveFile->ReadInt( clipMask );
  3864. saveFile->ReadVec3( frictionDir );
  3865. saveFile->ReadVec3( contactMotorDir );
  3866. saveFile->ReadFloat( contactMotorVelocity );
  3867. saveFile->ReadFloat( contactMotorForce );
  3868. saveFile->ReadFloat( mass );
  3869. saveFile->ReadFloat( invMass );
  3870. saveFile->ReadVec3( centerOfMass );
  3871. saveFile->ReadMat3( inertiaTensor );
  3872. saveFile->ReadMat3( inverseInertiaTensor );
  3873. saveFile->ReadVec3( current->worldOrigin );
  3874. saveFile->ReadMat3( current->worldAxis );
  3875. saveFile->ReadVec6( current->spatialVelocity );
  3876. saveFile->ReadVec6( current->externalForce );
  3877. saveFile->ReadVec3( atRestOrigin );
  3878. saveFile->ReadMat3( atRestAxis );
  3879. }
  3880. //===============================================================
  3881. // M
  3882. // idAFTree MrE
  3883. // E
  3884. //===============================================================
  3885. /*
  3886. ================
  3887. idAFTree::Factor
  3888. factor matrix for the primary constraints in the tree
  3889. ================
  3890. */
  3891. void idAFTree::Factor( void ) const {
  3892. int i, j;
  3893. idAFBody *body;
  3894. idAFConstraint *child;
  3895. idMatX childI;
  3896. childI.SetData( 6, 6, MATX_ALLOCA( 6 * 6 ) );
  3897. // from the leaves up towards the root
  3898. for ( i = sortedBodies.Num() - 1; i >= 0; i-- ) {
  3899. body = sortedBodies[i];
  3900. if ( body->children.Num() ) {
  3901. for ( j = 0; j < body->children.Num(); j++ ) {
  3902. child = body->children[j]->primaryConstraint;
  3903. // child->I = - child->body1->J.Transpose() * child->body1->I * child->body1->J;
  3904. childI.SetSize( child->J1.GetNumRows(), child->J1.GetNumRows() );
  3905. child->body1->J.TransposeMultiply( child->body1->I ).Multiply( childI, child->body1->J );
  3906. childI.Negate();
  3907. child->invI = childI;
  3908. if ( !child->invI.InverseFastSelf() ) {
  3909. gameLocal.Warning( "idAFTree::Factor: couldn't invert %dx%d matrix for constraint '%s'",
  3910. child->invI.GetNumRows(), child->invI.GetNumColumns(), child->GetName().c_str() );
  3911. }
  3912. child->J = child->invI * child->J;
  3913. body->I -= child->J.TransposeMultiply( childI ) * child->J;
  3914. }
  3915. body->invI = body->I;
  3916. if ( !body->invI.InverseFastSelf() ) {
  3917. gameLocal.Warning( "idAFTree::Factor: couldn't invert %dx%d matrix for body %s",
  3918. child->invI.GetNumRows(), child->invI.GetNumColumns(), body->GetName().c_str() );
  3919. }
  3920. if ( body->primaryConstraint ) {
  3921. body->J = body->invI * body->J;
  3922. }
  3923. }
  3924. else if ( body->primaryConstraint ) {
  3925. body->J = body->inverseWorldSpatialInertia * body->J;
  3926. }
  3927. }
  3928. }
  3929. /*
  3930. ================
  3931. idAFTree::Solve
  3932. solve for primary constraints in the tree
  3933. ================
  3934. */
  3935. void idAFTree::Solve( int auxiliaryIndex ) const {
  3936. int i, j;
  3937. idAFBody *body, *child;
  3938. idAFConstraint *primaryConstraint;
  3939. // from the leaves up towards the root
  3940. for ( i = sortedBodies.Num() - 1; i >= 0; i-- ) {
  3941. body = sortedBodies[i];
  3942. for ( j = 0; j < body->children.Num(); j++ ) {
  3943. child = body->children[j];
  3944. primaryConstraint = child->primaryConstraint;
  3945. if ( !child->fl.isZero ) {
  3946. child->J.TransposeMultiplySub( primaryConstraint->s, child->s );
  3947. primaryConstraint->fl.isZero = false;
  3948. }
  3949. if ( !primaryConstraint->fl.isZero ) {
  3950. primaryConstraint->J.TransposeMultiplySub( body->s, primaryConstraint->s );
  3951. body->fl.isZero = false;
  3952. }
  3953. }
  3954. }
  3955. bool useSymmetry = af_useSymmetry.GetBool();
  3956. // from the root down towards the leaves
  3957. for ( i = 0; i < sortedBodies.Num(); i++ ) {
  3958. body = sortedBodies[i];
  3959. primaryConstraint = body->primaryConstraint;
  3960. if ( primaryConstraint ) {
  3961. if ( useSymmetry && body->parent->maxSubTreeAuxiliaryIndex < auxiliaryIndex ) {
  3962. continue;
  3963. }
  3964. if ( !primaryConstraint->fl.isZero ) {
  3965. primaryConstraint->s = primaryConstraint->invI * primaryConstraint->s;
  3966. }
  3967. primaryConstraint->J.MultiplySub( primaryConstraint->s, primaryConstraint->body2->s );
  3968. primaryConstraint->lm = primaryConstraint->s;
  3969. if ( useSymmetry && body->maxSubTreeAuxiliaryIndex < auxiliaryIndex ) {
  3970. continue;
  3971. }
  3972. if ( body->children.Num() ) {
  3973. if ( !body->fl.isZero ) {
  3974. body->s = body->invI * body->s;
  3975. }
  3976. body->J.MultiplySub( body->s, primaryConstraint->s );
  3977. }
  3978. } else if ( body->children.Num() ) {
  3979. body->s = body->invI * body->s;
  3980. }
  3981. }
  3982. }
  3983. /*
  3984. ================
  3985. idAFTree::Response
  3986. calculate body forces in the tree in response to a constraint force
  3987. ================
  3988. */
  3989. void idAFTree::Response( const idAFConstraint *constraint, int row, int auxiliaryIndex ) const {
  3990. int i, j;
  3991. idAFBody *body;
  3992. idAFConstraint *child, *primaryConstraint;
  3993. idVecX v;
  3994. // if a single body don't waste time because there aren't any primary constraints
  3995. if ( sortedBodies.Num() == 1 ) {
  3996. body = constraint->body1;
  3997. if ( body->tree == this ) {
  3998. body->GetResponseForce( body->numResponses ) = constraint->J1.SubVec6( row );
  3999. body->responseIndex[body->numResponses++] = auxiliaryIndex;
  4000. }
  4001. else {
  4002. body = constraint->body2;
  4003. body->GetResponseForce( body->numResponses ) = constraint->J2.SubVec6( row );
  4004. body->responseIndex[body->numResponses++] = auxiliaryIndex;
  4005. }
  4006. return;
  4007. }
  4008. v.SetData( 6, VECX_ALLOCA( 6 ) );
  4009. // initialize right hand side to zero
  4010. for ( i = 0; i < sortedBodies.Num(); i++ ) {
  4011. body = sortedBodies[i];
  4012. primaryConstraint = body->primaryConstraint;
  4013. if ( primaryConstraint ) {
  4014. primaryConstraint->s.Zero();
  4015. primaryConstraint->fl.isZero = true;
  4016. }
  4017. body->s.Zero();
  4018. body->fl.isZero = true;
  4019. body->GetResponseForce( body->numResponses ).Zero();
  4020. }
  4021. // set right hand side for first constrained body
  4022. body = constraint->body1;
  4023. if ( body->tree == this ) {
  4024. body->InverseWorldSpatialInertiaMultiply( v, constraint->J1[row] );
  4025. primaryConstraint = body->primaryConstraint;
  4026. if ( primaryConstraint ) {
  4027. primaryConstraint->J1.Multiply( primaryConstraint->s, v );
  4028. primaryConstraint->fl.isZero = false;
  4029. }
  4030. for ( i = 0; i < body->children.Num(); i++ ) {
  4031. child = body->children[i]->primaryConstraint;
  4032. child->J2.Multiply( child->s, v );
  4033. child->fl.isZero = false;
  4034. }
  4035. body->GetResponseForce( body->numResponses ) = constraint->J1.SubVec6( row );
  4036. }
  4037. // set right hand side for second constrained body
  4038. body = constraint->body2;
  4039. if ( body && body->tree == this ) {
  4040. body->InverseWorldSpatialInertiaMultiply( v, constraint->J2[row] );
  4041. primaryConstraint = body->primaryConstraint;
  4042. if ( primaryConstraint ) {
  4043. primaryConstraint->J1.MultiplyAdd( primaryConstraint->s, v );
  4044. primaryConstraint->fl.isZero = false;
  4045. }
  4046. for ( i = 0; i < body->children.Num(); i++ ) {
  4047. child = body->children[i]->primaryConstraint;
  4048. child->J2.MultiplyAdd( child->s, v );
  4049. child->fl.isZero = false;
  4050. }
  4051. body->GetResponseForce( body->numResponses ) = constraint->J2.SubVec6( row );
  4052. }
  4053. // solve for primary constraints
  4054. Solve( auxiliaryIndex );
  4055. bool useSymmetry = af_useSymmetry.GetBool();
  4056. // store body forces in response to the constraint force
  4057. idVecX force;
  4058. for ( i = 0; i < sortedBodies.Num(); i++ ) {
  4059. body = sortedBodies[i];
  4060. if ( useSymmetry && body->maxAuxiliaryIndex < auxiliaryIndex ) {
  4061. continue;
  4062. }
  4063. force.SetData( 6, body->response + body->numResponses * 8 );
  4064. // add forces of all primary constraints acting on this body
  4065. primaryConstraint = body->primaryConstraint;
  4066. if ( primaryConstraint ) {
  4067. primaryConstraint->J1.TransposeMultiplyAdd( force, primaryConstraint->lm );
  4068. }
  4069. for ( j = 0; j < body->children.Num(); j++ ) {
  4070. child = body->children[j]->primaryConstraint;
  4071. child->J2.TransposeMultiplyAdd( force, child->lm );
  4072. }
  4073. body->responseIndex[body->numResponses++] = auxiliaryIndex;
  4074. }
  4075. }
  4076. /*
  4077. ================
  4078. idAFTree::CalculateForces
  4079. calculate forces on the bodies in the tree
  4080. ================
  4081. */
  4082. void idAFTree::CalculateForces( float timeStep ) const {
  4083. int i, j;
  4084. float invStep;
  4085. idAFBody *body;
  4086. idAFConstraint *child, *c, *primaryConstraint;
  4087. // forces on bodies
  4088. for ( i = 0; i < sortedBodies.Num(); i++ ) {
  4089. body = sortedBodies[i];
  4090. body->totalForce.SubVec6(0) = body->current->externalForce + body->auxForce.SubVec6(0);
  4091. }
  4092. // if a single body don't waste time because there aren't any primary constraints
  4093. if ( sortedBodies.Num() == 1 ) {
  4094. return;
  4095. }
  4096. invStep = 1.0f / timeStep;
  4097. // initialize right hand side
  4098. for ( i = 0; i < sortedBodies.Num(); i++ ) {
  4099. body = sortedBodies[i];
  4100. body->InverseWorldSpatialInertiaMultiply( body->acceleration, body->totalForce.ToFloatPtr() );
  4101. body->acceleration.SubVec6(0) += body->current->spatialVelocity * invStep;
  4102. primaryConstraint = body->primaryConstraint;
  4103. if ( primaryConstraint ) {
  4104. // b = ( J * acc + c )
  4105. c = primaryConstraint;
  4106. c->s = c->J1 * c->body1->acceleration + c->J2 * c->body2->acceleration + invStep * ( c->c1 + c->c2 );
  4107. c->fl.isZero = false;
  4108. }
  4109. body->s.Zero();
  4110. body->fl.isZero = true;
  4111. }
  4112. // solve for primary constraints
  4113. Solve();
  4114. // calculate forces on bodies after applying primary constraints
  4115. for ( i = 0; i < sortedBodies.Num(); i++ ) {
  4116. body = sortedBodies[i];
  4117. // add forces of all primary constraints acting on this body
  4118. primaryConstraint = body->primaryConstraint;
  4119. if ( primaryConstraint ) {
  4120. primaryConstraint->J1.TransposeMultiplyAdd( body->totalForce, primaryConstraint->lm );
  4121. }
  4122. for ( j = 0; j < body->children.Num(); j++ ) {
  4123. child = body->children[j]->primaryConstraint;
  4124. child->J2.TransposeMultiplyAdd( body->totalForce, child->lm );
  4125. }
  4126. }
  4127. }
  4128. /*
  4129. ================
  4130. idAFTree::SetMaxSubTreeAuxiliaryIndex
  4131. ================
  4132. */
  4133. void idAFTree::SetMaxSubTreeAuxiliaryIndex( void ) {
  4134. int i, j;
  4135. idAFBody *body, *child;
  4136. // from the leaves up towards the root
  4137. for ( i = sortedBodies.Num() - 1; i >= 0; i-- ) {
  4138. body = sortedBodies[i];
  4139. body->maxSubTreeAuxiliaryIndex = body->maxAuxiliaryIndex;
  4140. for ( j = 0; j < body->children.Num(); j++ ) {
  4141. child = body->children[j];
  4142. if ( child->maxSubTreeAuxiliaryIndex > body->maxSubTreeAuxiliaryIndex ) {
  4143. body->maxSubTreeAuxiliaryIndex = child->maxSubTreeAuxiliaryIndex;
  4144. }
  4145. }
  4146. }
  4147. }
  4148. /*
  4149. ================
  4150. idAFTree::SortBodies_r
  4151. ================
  4152. */
  4153. void idAFTree::SortBodies_r( idList<idAFBody*>&sortedList, idAFBody *body ) {
  4154. int i;
  4155. for ( i = 0; i < body->children.Num(); i++ ) {
  4156. sortedList.Append( body->children[i] );
  4157. }
  4158. for ( i = 0; i < body->children.Num(); i++ ) {
  4159. SortBodies_r( sortedList, body->children[i] );
  4160. }
  4161. }
  4162. /*
  4163. ================
  4164. idAFTree::SortBodies
  4165. sort body list to make sure parents come first
  4166. ================
  4167. */
  4168. void idAFTree::SortBodies( void ) {
  4169. int i;
  4170. idAFBody *body;
  4171. // find the root
  4172. for ( i = 0; i < sortedBodies.Num(); i++ ) {
  4173. if ( !sortedBodies[i]->parent ) {
  4174. break;
  4175. }
  4176. }
  4177. if ( i >= sortedBodies.Num() ) {
  4178. gameLocal.Error( "Articulated figure tree has no root." );
  4179. }
  4180. body = sortedBodies[i];
  4181. sortedBodies.Clear();
  4182. sortedBodies.Append( body );
  4183. SortBodies_r( sortedBodies, body );
  4184. }
  4185. /*
  4186. ================
  4187. idAFTree::DebugDraw
  4188. ================
  4189. */
  4190. void idAFTree::DebugDraw( const idVec4 &color ) const {
  4191. int i;
  4192. idAFBody *body;
  4193. for ( i = 1; i < sortedBodies.Num(); i++ ) {
  4194. body = sortedBodies[i];
  4195. gameRenderWorld->DebugArrow( color, body->parent->current->worldOrigin, body->current->worldOrigin, 1 );
  4196. }
  4197. }
  4198. //===============================================================
  4199. // M
  4200. // idPhysics_AF MrE
  4201. // E
  4202. //===============================================================
  4203. /*
  4204. ================
  4205. idPhysics_AF::EvaluateConstraints
  4206. ================
  4207. */
  4208. void idPhysics_AF::EvaluateConstraints( float timeStep ) {
  4209. int i;
  4210. float invTimeStep;
  4211. idAFBody *body;
  4212. idAFConstraint *c;
  4213. invTimeStep = 1.0f / timeStep;
  4214. // setup the constraint equations for the current position and orientation of the bodies
  4215. for ( i = 0; i < primaryConstraints.Num(); i++ ) {
  4216. c = primaryConstraints[i];
  4217. c->Evaluate( invTimeStep );
  4218. c->J = c->J2;
  4219. }
  4220. for ( i = 0; i < auxiliaryConstraints.Num(); i++ ) {
  4221. auxiliaryConstraints[i]->Evaluate( invTimeStep );
  4222. }
  4223. // add contact constraints to the list with frame constraints
  4224. for ( i = 0; i < contactConstraints.Num(); i++ ) {
  4225. AddFrameConstraint( contactConstraints[i] );
  4226. }
  4227. // setup body primary constraint matrix
  4228. for ( i = 0; i < bodies.Num(); i++ ) {
  4229. body = bodies[i];
  4230. if ( body->primaryConstraint ) {
  4231. body->J = body->primaryConstraint->J1.Transpose();
  4232. }
  4233. }
  4234. }
  4235. /*
  4236. ================
  4237. idPhysics_AF::EvaluateBodies
  4238. ================
  4239. */
  4240. void idPhysics_AF::EvaluateBodies( float timeStep ) {
  4241. int i;
  4242. idAFBody *body;
  4243. idMat3 axis;
  4244. for ( i = 0; i < bodies.Num(); i++ ) {
  4245. body = bodies[i];
  4246. // we transpose the axis before using it because idMat3 is column-major
  4247. axis = body->current->worldAxis.Transpose();
  4248. // if the center of mass is at the body point of reference
  4249. if ( body->centerOfMass.Compare( vec3_origin, CENTER_OF_MASS_EPSILON ) ) {
  4250. // spatial inertia in world space
  4251. body->I.Set( body->mass * mat3_identity, mat3_zero,
  4252. mat3_zero, axis * body->inertiaTensor * axis.Transpose() );
  4253. // inverse spatial inertia in world space
  4254. body->inverseWorldSpatialInertia.Set( body->invMass * mat3_identity, mat3_zero,
  4255. mat3_zero, axis * body->inverseInertiaTensor * axis.Transpose() );
  4256. body->fl.spatialInertiaSparse = true;
  4257. }
  4258. else {
  4259. idMat3 massMoment = body->mass * SkewSymmetric( body->centerOfMass );
  4260. // spatial inertia in world space
  4261. body->I.Set( body->mass * mat3_identity, massMoment,
  4262. massMoment.Transpose(), axis * body->inertiaTensor * axis.Transpose() );
  4263. // inverse spatial inertia in world space
  4264. body->inverseWorldSpatialInertia = body->I.InverseFast();
  4265. body->fl.spatialInertiaSparse = false;
  4266. }
  4267. // initialize auxiliary constraint force to zero
  4268. body->auxForce.Zero();
  4269. }
  4270. }
  4271. /*
  4272. ================
  4273. idPhysics_AF::AddFrameConstraints
  4274. ================
  4275. */
  4276. void idPhysics_AF::AddFrameConstraints( void ) {
  4277. int i;
  4278. // add frame constraints to auxiliary constraints
  4279. for ( i = 0; i < frameConstraints.Num(); i++ ) {
  4280. auxiliaryConstraints.Append( frameConstraints[i] );
  4281. }
  4282. }
  4283. /*
  4284. ================
  4285. idPhysics_AF::RemoveFrameConstraints
  4286. ================
  4287. */
  4288. void idPhysics_AF::RemoveFrameConstraints( void ) {
  4289. // remove all the frame constraints from the auxiliary constraints
  4290. auxiliaryConstraints.SetNum( auxiliaryConstraints.Num() - frameConstraints.Num(), false );
  4291. frameConstraints.SetNum( 0, false );
  4292. }
  4293. /*
  4294. ================
  4295. idPhysics_AF::ApplyFriction
  4296. ================
  4297. */
  4298. void idPhysics_AF::ApplyFriction( float timeStep, float endTimeMSec ) {
  4299. int i;
  4300. float invTimeStep;
  4301. if ( af_skipFriction.GetBool() ) {
  4302. return;
  4303. }
  4304. if ( jointFrictionDentStart < MS2SEC( endTimeMSec ) && jointFrictionDentEnd > MS2SEC( endTimeMSec ) ) {
  4305. float halfTime = ( jointFrictionDentEnd - jointFrictionDentStart ) * 0.5f;
  4306. if ( jointFrictionDentStart + halfTime > MS2SEC( endTimeMSec ) ) {
  4307. jointFrictionDentScale = 1.0f - ( 1.0f - jointFrictionDent ) * ( MS2SEC( endTimeMSec ) - jointFrictionDentStart ) / halfTime;
  4308. } else {
  4309. jointFrictionDentScale = jointFrictionDent + ( 1.0f - jointFrictionDent ) * ( MS2SEC( endTimeMSec ) - jointFrictionDentStart - halfTime ) / halfTime;
  4310. }
  4311. } else {
  4312. jointFrictionDentScale = 0.0f;
  4313. }
  4314. if ( contactFrictionDentStart < MS2SEC( endTimeMSec ) && contactFrictionDentEnd > MS2SEC( endTimeMSec ) ) {
  4315. float halfTime = ( contactFrictionDentEnd - contactFrictionDentStart ) * 0.5f;
  4316. if ( contactFrictionDentStart + halfTime > MS2SEC( endTimeMSec ) ) {
  4317. contactFrictionDentScale = 1.0f - ( 1.0f - contactFrictionDent ) * ( MS2SEC( endTimeMSec ) - contactFrictionDentStart ) / halfTime;
  4318. } else {
  4319. contactFrictionDentScale = contactFrictionDent + ( 1.0f - contactFrictionDent ) * ( MS2SEC( endTimeMSec ) - contactFrictionDentStart - halfTime ) / halfTime;
  4320. }
  4321. } else {
  4322. contactFrictionDentScale = 0.0f;
  4323. }
  4324. invTimeStep = 1.0f / timeStep;
  4325. for ( i = 0; i < primaryConstraints.Num(); i++ ) {
  4326. primaryConstraints[i]->ApplyFriction( invTimeStep );
  4327. }
  4328. for ( i = 0; i < auxiliaryConstraints.Num(); i++ ) {
  4329. auxiliaryConstraints[i]->ApplyFriction( invTimeStep );
  4330. }
  4331. for ( i = 0; i < frameConstraints.Num(); i++ ) {
  4332. frameConstraints[i]->ApplyFriction( invTimeStep );
  4333. }
  4334. }
  4335. /*
  4336. ================
  4337. idPhysics_AF::PrimaryFactor
  4338. ================
  4339. */
  4340. void idPhysics_AF::PrimaryFactor( void ) {
  4341. int i;
  4342. for ( i = 0; i < trees.Num(); i++ ) {
  4343. trees[i]->Factor();
  4344. }
  4345. }
  4346. /*
  4347. ================
  4348. idPhysics_AF::PrimaryForces
  4349. ================
  4350. */
  4351. void idPhysics_AF::PrimaryForces( float timeStep ) {
  4352. int i;
  4353. for ( i = 0; i < trees.Num(); i++ ) {
  4354. trees[i]->CalculateForces( timeStep );
  4355. }
  4356. }
  4357. /*
  4358. ================
  4359. idPhysics_AF::AuxiliaryForces
  4360. ================
  4361. */
  4362. void idPhysics_AF::AuxiliaryForces( float timeStep ) {
  4363. int i, j, k, l, n, m, s, numAuxConstraints, *index, *boxIndex;
  4364. float *ptr, *j1, *j2, *dstPtr, *forcePtr;
  4365. float invStep, u;
  4366. idAFBody *body;
  4367. idAFConstraint *constraint;
  4368. idVecX tmp;
  4369. idMatX jmk;
  4370. idVecX rhs, w, lm, lo, hi;
  4371. // get the number of one dimensional auxiliary constraints
  4372. for ( numAuxConstraints = 0, i = 0; i < auxiliaryConstraints.Num(); i++ ) {
  4373. numAuxConstraints += auxiliaryConstraints[i]->J1.GetNumRows();
  4374. }
  4375. if ( numAuxConstraints == 0 ) {
  4376. return;
  4377. }
  4378. // allocate memory to store the body response to auxiliary constraint forces
  4379. forcePtr = (float *) _alloca16( bodies.Num() * numAuxConstraints * 8 * sizeof( float ) );
  4380. index = (int *) _alloca16( bodies.Num() * numAuxConstraints * sizeof( int ) );
  4381. for ( i = 0; i < bodies.Num(); i++ ) {
  4382. body = bodies[i];
  4383. body->response = forcePtr;
  4384. body->responseIndex = index;
  4385. body->numResponses = 0;
  4386. body->maxAuxiliaryIndex = 0;
  4387. forcePtr += numAuxConstraints * 8;
  4388. index += numAuxConstraints;
  4389. }
  4390. // set on each body the largest index of an auxiliary constraint constraining the body
  4391. if ( af_useSymmetry.GetBool() ) {
  4392. for ( k = 0, i = 0; i < auxiliaryConstraints.Num(); i++ ) {
  4393. constraint = auxiliaryConstraints[i];
  4394. for ( j = 0; j < constraint->J1.GetNumRows(); j++, k++ ) {
  4395. if ( k > constraint->body1->maxAuxiliaryIndex ) {
  4396. constraint->body1->maxAuxiliaryIndex = k;
  4397. }
  4398. if ( constraint->body2 && k > constraint->body2->maxAuxiliaryIndex ) {
  4399. constraint->body2->maxAuxiliaryIndex = k;
  4400. }
  4401. }
  4402. }
  4403. for ( i = 0; i < trees.Num(); i++ ) {
  4404. trees[i]->SetMaxSubTreeAuxiliaryIndex();
  4405. }
  4406. }
  4407. // calculate forces of primary constraints in response to the auxiliary constraint forces
  4408. for ( k = 0, i = 0; i < auxiliaryConstraints.Num(); i++ ) {
  4409. constraint = auxiliaryConstraints[i];
  4410. for ( j = 0; j < constraint->J1.GetNumRows(); j++, k++ ) {
  4411. // calculate body forces in the tree in response to the constraint force
  4412. constraint->body1->tree->Response( constraint, j, k );
  4413. // if there is a second body which is part of a different tree
  4414. if ( constraint->body2 && constraint->body2->tree != constraint->body1->tree ) {
  4415. // calculate body forces in the second tree in response to the constraint force
  4416. constraint->body2->tree->Response( constraint, j, k );
  4417. }
  4418. }
  4419. }
  4420. // NOTE: the rows are 16 byte padded
  4421. jmk.SetData( numAuxConstraints, ((numAuxConstraints+3)&~3), MATX_ALLOCA( numAuxConstraints * ((numAuxConstraints+3)&~3) ) );
  4422. tmp.SetData( 6, VECX_ALLOCA( 6 ) );
  4423. // create constraint matrix for auxiliary constraints using a mass matrix adjusted for the primary constraints
  4424. for ( k = 0, i = 0; i < auxiliaryConstraints.Num(); i++ ) {
  4425. constraint = auxiliaryConstraints[i];
  4426. for ( j = 0; j < constraint->J1.GetNumRows(); j++, k++ ) {
  4427. constraint->body1->InverseWorldSpatialInertiaMultiply( tmp, constraint->J1[j] );
  4428. j1 = tmp.ToFloatPtr();
  4429. ptr = constraint->body1->response;
  4430. index = constraint->body1->responseIndex;
  4431. dstPtr = jmk[k];
  4432. s = af_useSymmetry.GetBool() ? k + 1 : numAuxConstraints;
  4433. for ( l = n = 0, m = index[n]; n < constraint->body1->numResponses && m < s; n++, m = index[n] ) {
  4434. while( l < m ) {
  4435. dstPtr[l++] = 0.0f;
  4436. }
  4437. dstPtr[l++] = j1[0] * ptr[0] + j1[1] * ptr[1] + j1[2] * ptr[2] +
  4438. j1[3] * ptr[3] + j1[4] * ptr[4] + j1[5] * ptr[5];
  4439. ptr += 8;
  4440. }
  4441. while( l < s ) {
  4442. dstPtr[l++] = 0.0f;
  4443. }
  4444. if ( constraint->body2 ) {
  4445. constraint->body2->InverseWorldSpatialInertiaMultiply( tmp, constraint->J2[j] );
  4446. j2 = tmp.ToFloatPtr();
  4447. ptr = constraint->body2->response;
  4448. index = constraint->body2->responseIndex;
  4449. for ( n = 0, m = index[n]; n < constraint->body2->numResponses && m < s; n++, m = index[n] ) {
  4450. dstPtr[m] += j2[0] * ptr[0] + j2[1] * ptr[1] + j2[2] * ptr[2] +
  4451. j2[3] * ptr[3] + j2[4] * ptr[4] + j2[5] * ptr[5];
  4452. ptr += 8;
  4453. }
  4454. }
  4455. }
  4456. }
  4457. if ( af_useSymmetry.GetBool() ) {
  4458. n = jmk.GetNumColumns();
  4459. for ( i = 0; i < numAuxConstraints; i++ ) {
  4460. ptr = jmk.ToFloatPtr() + ( i + 1 ) * n + i;
  4461. dstPtr = jmk.ToFloatPtr() + i * n + i + 1;
  4462. for ( j = i+1; j < numAuxConstraints; j++ ) {
  4463. *dstPtr++ = *ptr;
  4464. ptr += n;
  4465. }
  4466. }
  4467. }
  4468. invStep = 1.0f / timeStep;
  4469. // calculate body acceleration
  4470. for ( i = 0; i < bodies.Num(); i++ ) {
  4471. body = bodies[i];
  4472. body->InverseWorldSpatialInertiaMultiply( body->acceleration, body->totalForce.ToFloatPtr() );
  4473. body->acceleration.SubVec6(0) += body->current->spatialVelocity * invStep;
  4474. }
  4475. rhs.SetData( numAuxConstraints, VECX_ALLOCA( numAuxConstraints ) );
  4476. lo.SetData( numAuxConstraints, VECX_ALLOCA( numAuxConstraints ) );
  4477. hi.SetData( numAuxConstraints, VECX_ALLOCA( numAuxConstraints ) );
  4478. lm.SetData( numAuxConstraints, VECX_ALLOCA( numAuxConstraints ) );
  4479. boxIndex = (int *) _alloca16( numAuxConstraints * sizeof( int ) );
  4480. // set first index for special box constrained variables
  4481. for ( k = 0, i = 0; i < auxiliaryConstraints.Num(); i++ ) {
  4482. auxiliaryConstraints[i]->firstIndex = k;
  4483. k += auxiliaryConstraints[i]->J1.GetNumRows();
  4484. }
  4485. // initialize right hand side and low and high bounds for auxiliary constraints
  4486. for ( k = 0, i = 0; i < auxiliaryConstraints.Num(); i++ ) {
  4487. constraint = auxiliaryConstraints[i];
  4488. n = k;
  4489. for ( j = 0; j < constraint->J1.GetNumRows(); j++, k++ ) {
  4490. j1 = constraint->J1[j];
  4491. ptr = constraint->body1->acceleration.ToFloatPtr();
  4492. 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];
  4493. rhs[k] += constraint->c1[j] * invStep;
  4494. if ( constraint->body2 ) {
  4495. j2 = constraint->J2[j];
  4496. ptr = constraint->body2->acceleration.ToFloatPtr();
  4497. 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];
  4498. rhs[k] += constraint->c2[j] * invStep;
  4499. }
  4500. rhs[k] = -rhs[k];
  4501. lo[k] = constraint->lo[j];
  4502. hi[k] = constraint->hi[j];
  4503. if ( constraint->boxIndex[j] >= 0 ) {
  4504. if ( constraint->boxConstraint->fl.isPrimary ) {
  4505. gameLocal.Error( "cannot reference primary constraints for the box index" );
  4506. }
  4507. boxIndex[k] = constraint->boxConstraint->firstIndex + constraint->boxIndex[j];
  4508. }
  4509. else {
  4510. boxIndex[k] = -1;
  4511. }
  4512. jmk[k][k] += constraint->e[j] * invStep;
  4513. }
  4514. }
  4515. #ifdef AF_TIMINGS
  4516. timer_lcp.Start();
  4517. #endif
  4518. // calculate lagrange multipliers for auxiliary constraints
  4519. if ( !lcp->Solve( jmk, lm, rhs, lo, hi, boxIndex ) ) {
  4520. return; // bad monkey!
  4521. }
  4522. #ifdef AF_TIMINGS
  4523. timer_lcp.Stop();
  4524. #endif
  4525. // calculate auxiliary constraint forces
  4526. for ( k = 0, i = 0; i < auxiliaryConstraints.Num(); i++ ) {
  4527. constraint = auxiliaryConstraints[i];
  4528. for ( j = 0; j < constraint->J1.GetNumRows(); j++, k++ ) {
  4529. constraint->lm[j] = u = lm[k];
  4530. j1 = constraint->J1[j];
  4531. ptr = constraint->body1->auxForce.ToFloatPtr();
  4532. ptr[0] += j1[0] * u; ptr[1] += j1[1] * u; ptr[2] += j1[2] * u;
  4533. ptr[3] += j1[3] * u; ptr[4] += j1[4] * u; ptr[5] += j1[5] * u;
  4534. if ( constraint->body2 ) {
  4535. j2 = constraint->J2[j];
  4536. ptr = constraint->body2->auxForce.ToFloatPtr();
  4537. ptr[0] += j2[0] * u; ptr[1] += j2[1] * u; ptr[2] += j2[2] * u;
  4538. ptr[3] += j2[3] * u; ptr[4] += j2[4] * u; ptr[5] += j2[5] * u;
  4539. }
  4540. }
  4541. }
  4542. // recalculate primary constraint forces in response to auxiliary constraint forces
  4543. PrimaryForces( timeStep );
  4544. // clear pointers pointing to stack space so tools don't get confused
  4545. for ( i = 0; i < bodies.Num(); i++ ) {
  4546. body = bodies[i];
  4547. body->response = NULL;
  4548. body->responseIndex = NULL;
  4549. }
  4550. }
  4551. /*
  4552. ================
  4553. idPhysics_AF::VerifyContactConstraints
  4554. ================
  4555. */
  4556. void idPhysics_AF::VerifyContactConstraints( void ) {
  4557. #if 0
  4558. int i;
  4559. float impulseNumerator, impulseDenominator;
  4560. idVec3 r, velocity, normalVelocity, normal, impulse;
  4561. idAFBody *body;
  4562. for ( i = 0; i < contactConstraints.Num(); i++ ) {
  4563. body = contactConstraints[i]->body1;
  4564. const contactInfo_t &contact = contactConstraints[i]->GetContact();
  4565. r = contact.point - body->GetCenterOfMass();
  4566. // calculate velocity at contact point
  4567. velocity = body->GetLinearVelocity() + body->GetAngularVelocity().Cross( r );
  4568. // velocity along normal vector
  4569. normalVelocity = ( velocity * contact.normal ) * contact.normal;
  4570. // if moving towards the surface at the contact point
  4571. if ( normalVelocity * contact.normal < 0.0f ) {
  4572. // calculate impulse
  4573. normal = -normalVelocity;
  4574. impulseNumerator = normal.Normalize();
  4575. impulseDenominator = body->GetInverseMass() + ( ( body->GetInverseWorldInertia() * r.Cross( normal ) ).Cross( r ) * normal );
  4576. impulse = (impulseNumerator / impulseDenominator) * normal * 1.0001f;
  4577. // apply impulse
  4578. body->SetLinearVelocity( body->GetLinearVelocity() + impulse );
  4579. body->SetAngularVelocity( body->GetAngularVelocity() + r.Cross( impulse ) );
  4580. }
  4581. }
  4582. #else
  4583. int i;
  4584. idAFBody *body;
  4585. idVec3 normal;
  4586. for ( i = 0; i < contactConstraints.Num(); i++ ) {
  4587. body = contactConstraints[i]->body1;
  4588. normal = contactConstraints[i]->GetContact().normal;
  4589. if ( normal * body->next->spatialVelocity.SubVec3(0) <= 0.0f ) {
  4590. body->next->spatialVelocity.SubVec3(0) -= 1.0001f * (normal * body->next->spatialVelocity.SubVec3(0)) * normal;
  4591. }
  4592. body = contactConstraints[i]->body2;
  4593. if ( !body ) {
  4594. continue;
  4595. }
  4596. normal = -normal;
  4597. if ( normal * body->next->spatialVelocity.SubVec3(0) <= 0.0f ) {
  4598. body->next->spatialVelocity.SubVec3(0) -= 1.0001f * (normal * body->next->spatialVelocity.SubVec3(0)) * normal;
  4599. }
  4600. }
  4601. #endif
  4602. }
  4603. /*
  4604. ================
  4605. idPhysics_AF::Evolve
  4606. ================
  4607. */
  4608. void idPhysics_AF::Evolve( float timeStep ) {
  4609. int i;
  4610. float angle;
  4611. idVec3 vec;
  4612. idAFBody *body;
  4613. idVec6 force;
  4614. idRotation rotation;
  4615. float vSqr, maxLinearVelocity, maxAngularVelocity;
  4616. maxLinearVelocity = af_maxLinearVelocity.GetFloat() / timeStep;
  4617. maxAngularVelocity = af_maxAngularVelocity.GetFloat() / timeStep;
  4618. for ( i = 0; i < bodies.Num(); i++ ) {
  4619. body = bodies[i];
  4620. // calculate the spatial velocity for the next physics state
  4621. body->InverseWorldSpatialInertiaMultiply( body->acceleration, body->totalForce.ToFloatPtr() );
  4622. body->next->spatialVelocity = body->current->spatialVelocity + timeStep * body->acceleration.SubVec6(0);
  4623. if ( maxLinearVelocity > 0.0f ) {
  4624. // cap the linear velocity
  4625. vSqr = body->next->spatialVelocity.SubVec3(0).LengthSqr();
  4626. if ( vSqr > Square( maxLinearVelocity ) ) {
  4627. body->next->spatialVelocity.SubVec3(0) *= idMath::InvSqrt( vSqr ) * maxLinearVelocity;
  4628. }
  4629. }
  4630. if ( maxAngularVelocity > 0.0f ) {
  4631. // cap the angular velocity
  4632. vSqr = body->next->spatialVelocity.SubVec3(1).LengthSqr();
  4633. if ( vSqr > Square( maxAngularVelocity ) ) {
  4634. body->next->spatialVelocity.SubVec3(1) *= idMath::InvSqrt( vSqr ) * maxAngularVelocity;
  4635. }
  4636. }
  4637. }
  4638. // make absolutely sure all contact constraints are satisfied
  4639. VerifyContactConstraints();
  4640. // calculate the position of the bodies for the next physics state
  4641. for ( i = 0; i < bodies.Num(); i++ ) {
  4642. body = bodies[i];
  4643. // translate world origin
  4644. body->next->worldOrigin = body->current->worldOrigin + timeStep * body->next->spatialVelocity.SubVec3( 0 );
  4645. // convert angular velocity to a rotation matrix
  4646. vec = body->next->spatialVelocity.SubVec3( 1 );
  4647. angle = -timeStep * (float) RAD2DEG( vec.Normalize() );
  4648. rotation = idRotation( vec3_origin, vec, angle );
  4649. rotation.Normalize180();
  4650. // rotate world axis
  4651. body->next->worldAxis = body->current->worldAxis * rotation.ToMat3();
  4652. body->next->worldAxis.OrthoNormalizeSelf();
  4653. // linear and angular friction
  4654. body->next->spatialVelocity.SubVec3(0) -= body->linearFriction * body->next->spatialVelocity.SubVec3(0);
  4655. body->next->spatialVelocity.SubVec3(1) -= body->angularFriction * body->next->spatialVelocity.SubVec3(1);
  4656. }
  4657. }
  4658. /*
  4659. ================
  4660. idPhysics_AF::CollisionImpulse
  4661. apply impulse to the colliding bodies
  4662. the current state of the body should be set to the moment of impact
  4663. this is silly as it doesn't take the AF structure into account
  4664. ================
  4665. */
  4666. bool idPhysics_AF::CollisionImpulse( float timeStep, idAFBody *body, trace_t &collision ) {
  4667. idVec3 r, velocity, impulse;
  4668. idMat3 inverseWorldInertiaTensor;
  4669. float impulseNumerator, impulseDenominator;
  4670. impactInfo_t info;
  4671. idEntity *ent;
  4672. ent = gameLocal.entities[collision.c.entityNum];
  4673. if ( ent == self ) {
  4674. return false;
  4675. }
  4676. // get info from other entity involved
  4677. ent->GetImpactInfo( self, collision.c.id, collision.c.point, &info );
  4678. // collision point relative to the body center of mass
  4679. r = collision.c.point - (body->current->worldOrigin + body->centerOfMass * body->current->worldAxis);
  4680. // the velocity at the collision point
  4681. velocity = body->current->spatialVelocity.SubVec3(0) + body->current->spatialVelocity.SubVec3(1).Cross(r);
  4682. // subtract velocity of other entity
  4683. velocity -= info.velocity;
  4684. // never stick
  4685. if ( velocity * collision.c.normal > 0.0f ) {
  4686. velocity = collision.c.normal;
  4687. }
  4688. inverseWorldInertiaTensor = body->current->worldAxis.Transpose() * body->inverseInertiaTensor * body->current->worldAxis;
  4689. impulseNumerator = -( 1.0f + body->bouncyness ) * ( velocity * collision.c.normal );
  4690. impulseDenominator = body->invMass + ( ( inverseWorldInertiaTensor * r.Cross( collision.c.normal ) ).Cross( r ) * collision.c.normal );
  4691. if ( info.invMass ) {
  4692. impulseDenominator += info.invMass + ( ( info.invInertiaTensor * info.position.Cross( collision.c.normal ) ).Cross( info.position ) * collision.c.normal );
  4693. }
  4694. impulse = (impulseNumerator / impulseDenominator) * collision.c.normal;
  4695. // apply impact to other entity
  4696. ent->ApplyImpulse( self, collision.c.id, collision.c.point, -impulse );
  4697. // callback to self to let the entity know about the impact
  4698. return self->Collide( collision, velocity );
  4699. }
  4700. /*
  4701. ================
  4702. idPhysics_AF::ApplyCollisions
  4703. ================
  4704. */
  4705. bool idPhysics_AF::ApplyCollisions( float timeStep ) {
  4706. int i;
  4707. for ( i = 0; i < collisions.Num(); i++ ) {
  4708. if ( CollisionImpulse( timeStep, collisions[i].body, collisions[i].trace ) ) {
  4709. return true;
  4710. }
  4711. }
  4712. return false;
  4713. }
  4714. /*
  4715. ================
  4716. idPhysics_AF::SetupCollisionForBody
  4717. ================
  4718. */
  4719. idEntity *idPhysics_AF::SetupCollisionForBody( idAFBody *body ) const {
  4720. int i;
  4721. idAFBody *b;
  4722. idEntity *passEntity;
  4723. passEntity = NULL;
  4724. if ( !selfCollision || !body->fl.selfCollision || af_skipSelfCollision.GetBool() ) {
  4725. // disable all bodies
  4726. for ( i = 0; i < bodies.Num(); i++ ) {
  4727. bodies[i]->clipModel->Disable();
  4728. }
  4729. // don't collide with world collision model if attached to the world
  4730. for ( i = 0; i < body->constraints.Num(); i++ ) {
  4731. if ( !body->constraints[i]->fl.noCollision ) {
  4732. continue;
  4733. }
  4734. // if this constraint attaches the body to the world
  4735. if ( body->constraints[i]->body2 == NULL ) {
  4736. // don't collide with the world collision model
  4737. passEntity = gameLocal.world;
  4738. }
  4739. }
  4740. } else {
  4741. // enable all bodies that have self collision
  4742. for ( i = 0; i < bodies.Num(); i++ ) {
  4743. if ( bodies[i]->fl.selfCollision ) {
  4744. bodies[i]->clipModel->Enable();
  4745. } else {
  4746. bodies[i]->clipModel->Disable();
  4747. }
  4748. }
  4749. // don't let the body collide with itself
  4750. body->clipModel->Disable();
  4751. // disable any bodies attached with constraints
  4752. for ( i = 0; i < body->constraints.Num(); i++ ) {
  4753. if ( !body->constraints[i]->fl.noCollision ) {
  4754. continue;
  4755. }
  4756. // if this constraint attaches the body to the world
  4757. if ( body->constraints[i]->body2 == NULL ) {
  4758. // don't collide with the world collision model
  4759. passEntity = gameLocal.world;
  4760. } else {
  4761. if ( body->constraints[i]->body1 == body ) {
  4762. b = body->constraints[i]->body2;
  4763. } else if ( body->constraints[i]->body2 == body ) {
  4764. b = body->constraints[i]->body1;
  4765. } else {
  4766. continue;
  4767. }
  4768. // don't collide with this body
  4769. b->clipModel->Disable();
  4770. }
  4771. }
  4772. }
  4773. return passEntity;
  4774. }
  4775. /*
  4776. ================
  4777. idPhysics_AF::CheckForCollisions
  4778. check for collisions between the current and next state
  4779. if there is a collision the next state is set to the state at the moment of impact
  4780. assumes all bodies are linked for collision detection and relinks all bodies after moving them
  4781. ================
  4782. */
  4783. void idPhysics_AF::CheckForCollisions( float timeStep ) {
  4784. // #define TEST_COLLISION_DETECTION
  4785. int i, index;
  4786. idAFBody *body;
  4787. idMat3 axis;
  4788. idRotation rotation;
  4789. trace_t collision;
  4790. idEntity *passEntity;
  4791. // clear list with collisions
  4792. collisions.SetNum( 0, false );
  4793. if ( !enableCollision ) {
  4794. return;
  4795. }
  4796. for ( i = 0; i < bodies.Num(); i++ ) {
  4797. body = bodies[i];
  4798. if ( body->clipMask != 0 ) {
  4799. passEntity = SetupCollisionForBody( body );
  4800. #ifdef TEST_COLLISION_DETECTION
  4801. bool startsolid = false;
  4802. if ( gameLocal.clip.Contents( body->current->worldOrigin, body->clipModel,
  4803. body->current->worldAxis, body->clipMask, passEntity ) ) {
  4804. startsolid = true;
  4805. }
  4806. #endif
  4807. TransposeMultiply( body->current->worldAxis, body->next->worldAxis, axis );
  4808. rotation = axis.ToRotation();
  4809. rotation.SetOrigin( body->current->worldOrigin );
  4810. // if there was a collision
  4811. if ( gameLocal.clip.Motion( collision, body->current->worldOrigin, body->next->worldOrigin, rotation,
  4812. body->clipModel, body->current->worldAxis, body->clipMask, passEntity ) ) {
  4813. // set the next state to the state at the moment of impact
  4814. body->next->worldOrigin = collision.endpos;
  4815. body->next->worldAxis = collision.endAxis;
  4816. // add collision to the list
  4817. index = collisions.Num();
  4818. collisions.SetNum( index + 1, false );
  4819. collisions[index].trace = collision;
  4820. collisions[index].body = body;
  4821. }
  4822. #ifdef TEST_COLLISION_DETECTION
  4823. if ( gameLocal.clip.Contents( body->next->worldOrigin, body->clipModel,
  4824. body->next->worldAxis, body->clipMask, passEntity ) ) {
  4825. if ( !startsolid ) {
  4826. int bah = 1;
  4827. }
  4828. }
  4829. #endif
  4830. }
  4831. body->clipModel->Link( gameLocal.clip, self, body->clipModel->GetId(), body->next->worldOrigin, body->next->worldAxis );
  4832. }
  4833. }
  4834. /*
  4835. ================
  4836. idPhysics_AF::EvaluateContacts
  4837. ================
  4838. */
  4839. bool idPhysics_AF::EvaluateContacts( void ) {
  4840. int i, j, k, numContacts, numBodyContacts;
  4841. idAFBody *body;
  4842. contactInfo_t contactInfo[10];
  4843. idEntity *passEntity;
  4844. idVecX dir( 6, VECX_ALLOCA( 6 ) );
  4845. // evaluate bodies
  4846. EvaluateBodies( current.lastTimeStep );
  4847. // remove all existing contacts
  4848. ClearContacts();
  4849. contactBodies.SetNum( 0, false );
  4850. if ( !enableCollision ) {
  4851. return false;
  4852. }
  4853. // find all the contacts
  4854. for ( i = 0; i < bodies.Num(); i++ ) {
  4855. body = bodies[i];
  4856. if ( body->clipMask == 0 ) {
  4857. continue;
  4858. }
  4859. passEntity = SetupCollisionForBody( body );
  4860. body->InverseWorldSpatialInertiaMultiply( dir, body->current->externalForce.ToFloatPtr() );
  4861. dir.SubVec6(0) = body->current->spatialVelocity + current.lastTimeStep * dir.SubVec6(0);
  4862. dir.SubVec3(0).Normalize();
  4863. dir.SubVec3(1).Normalize();
  4864. numContacts = gameLocal.clip.Contacts( contactInfo, 10, body->current->worldOrigin, dir.SubVec6(0), 2.0f, //CONTACT_EPSILON,
  4865. body->clipModel, body->current->worldAxis, body->clipMask, passEntity );
  4866. #if 1
  4867. // merge nearby contacts between the same bodies
  4868. // and assure there are at most three planar contacts between any pair of bodies
  4869. for ( j = 0; j < numContacts; j++ ) {
  4870. numBodyContacts = 0;
  4871. for ( k = 0; k < contacts.Num(); k++ ) {
  4872. if ( contacts[k].entityNum == contactInfo[j].entityNum ) {
  4873. if ( ( contacts[k].id == i && contactInfo[j].id == contactBodies[k] ) ||
  4874. ( contactBodies[k] == i && contacts[k].id == contactInfo[j].id ) ) {
  4875. if ( ( contacts[k].point - contactInfo[j].point ).LengthSqr() < Square( 2.0f ) ) {
  4876. break;
  4877. }
  4878. if ( idMath::Fabs( contacts[k].normal * contactInfo[j].normal ) > 0.9f ) {
  4879. numBodyContacts++;
  4880. }
  4881. }
  4882. }
  4883. }
  4884. if ( k >= contacts.Num() && numBodyContacts < 3 ) {
  4885. contacts.Append( contactInfo[j] );
  4886. contactBodies.Append( i );
  4887. }
  4888. }
  4889. #else
  4890. for ( j = 0; j < numContacts; j++ ) {
  4891. contacts.Append( contactInfo[j] );
  4892. contactBodies.Append( i );
  4893. }
  4894. #endif
  4895. }
  4896. AddContactEntitiesForContacts();
  4897. return ( contacts.Num() != 0 );
  4898. }
  4899. /*
  4900. ================
  4901. idPhysics_AF::SetupContactConstraints
  4902. ================
  4903. */
  4904. void idPhysics_AF::SetupContactConstraints( void ) {
  4905. int i;
  4906. // make sure enough contact constraints are allocated
  4907. contactConstraints.AssureSizeAlloc( contacts.Num(), idListNewElement<idAFConstraint_Contact> );
  4908. contactConstraints.SetNum( contacts.Num(), false );
  4909. // setup contact constraints
  4910. for ( i = 0; i < contacts.Num(); i++ ) {
  4911. // add contact constraint
  4912. contactConstraints[i]->physics = this;
  4913. if ( contacts[i].entityNum == self->entityNumber ) {
  4914. contactConstraints[i]->Setup( bodies[contactBodies[i]], bodies[ contacts[i].id ], contacts[i] );
  4915. }
  4916. else {
  4917. contactConstraints[i]->Setup( bodies[contactBodies[i]], NULL, contacts[i] );
  4918. }
  4919. }
  4920. }
  4921. /*
  4922. ================
  4923. idPhysics_AF::ApplyContactForces
  4924. ================
  4925. */
  4926. void idPhysics_AF::ApplyContactForces( void ) {
  4927. #if 0
  4928. int i;
  4929. idEntity *ent;
  4930. idVec3 force;
  4931. for ( i = 0; i < contactConstraints.Num(); i++ ) {
  4932. if ( contactConstraints[i]->body2 != NULL ) {
  4933. continue;
  4934. }
  4935. const contactInfo_t &contact = contactConstraints[i]->GetContact();
  4936. ent = gameLocal.entities[contact.entityNum];
  4937. if ( !ent ) {
  4938. continue;
  4939. }
  4940. force.Zero();
  4941. ent->AddForce( self, contact.id, contact.point, force );
  4942. }
  4943. #endif
  4944. }
  4945. /*
  4946. ================
  4947. idPhysics_AF::ClearExternalForce
  4948. ================
  4949. */
  4950. void idPhysics_AF::ClearExternalForce( void ) {
  4951. int i;
  4952. idAFBody *body;
  4953. for ( i = 0; i < bodies.Num(); i++ ) {
  4954. body = bodies[i];
  4955. // clear external force
  4956. body->current->externalForce.Zero();
  4957. body->next->externalForce.Zero();
  4958. }
  4959. }
  4960. /*
  4961. ================
  4962. idPhysics_AF::AddGravity
  4963. ================
  4964. */
  4965. void idPhysics_AF::AddGravity( void ) {
  4966. int i;
  4967. idAFBody *body;
  4968. for ( i = 0; i < bodies.Num(); i++ ) {
  4969. body = bodies[i];
  4970. // add gravitational force
  4971. body->current->externalForce.SubVec3( 0 ) += body->mass * gravityVector;
  4972. }
  4973. }
  4974. /*
  4975. ================
  4976. idPhysics_AF::SwapStates
  4977. ================
  4978. */
  4979. void idPhysics_AF::SwapStates( void ) {
  4980. int i;
  4981. idAFBody *body;
  4982. AFBodyPState_t *swap;
  4983. for ( i = 0; i < bodies.Num(); i++ ) {
  4984. body = bodies[i];
  4985. // swap the current and next state for next simulation step
  4986. swap = body->current;
  4987. body->current = body->next;
  4988. body->next = swap;
  4989. }
  4990. }
  4991. /*
  4992. ================
  4993. idPhysics_AF::UpdateClipModels
  4994. ================
  4995. */
  4996. void idPhysics_AF::UpdateClipModels( void ) {
  4997. int i;
  4998. idAFBody *body;
  4999. for ( i = 0; i < bodies.Num(); i++ ) {
  5000. body = bodies[i];
  5001. body->clipModel->Link( gameLocal.clip, self, body->clipModel->GetId(), body->current->worldOrigin, body->current->worldAxis );
  5002. }
  5003. }
  5004. /*
  5005. ================
  5006. idPhysics_AF::SetSuspendSpeed
  5007. ================
  5008. */
  5009. void idPhysics_AF::SetSuspendSpeed( const idVec2 &velocity, const idVec2 &acceleration ) {
  5010. this->suspendVelocity = velocity;
  5011. this->suspendAcceleration = acceleration;
  5012. }
  5013. /*
  5014. ================
  5015. idPhysics_AF::SetSuspendTime
  5016. ================
  5017. */
  5018. void idPhysics_AF::SetSuspendTime( const float minTime, const float maxTime ) {
  5019. this->minMoveTime = minTime;
  5020. this->maxMoveTime = maxTime;
  5021. }
  5022. /*
  5023. ================
  5024. idPhysics_AF::SetSuspendTolerance
  5025. ================
  5026. */
  5027. void idPhysics_AF::SetSuspendTolerance( const float noMoveTime, const float noMoveTranslation, const float noMoveRotation ) {
  5028. this->noMoveTime = noMoveTime;
  5029. this->noMoveTranslation = noMoveTranslation;
  5030. this->noMoveRotation = noMoveRotation;
  5031. }
  5032. /*
  5033. ================
  5034. idPhysics_AF::SetTimeScaleRamp
  5035. ================
  5036. */
  5037. void idPhysics_AF::SetTimeScaleRamp( const float start, const float end ) {
  5038. timeScaleRampStart = start;
  5039. timeScaleRampEnd = end;
  5040. }
  5041. /*
  5042. ================
  5043. idPhysics_AF::SetJointFrictionDent
  5044. ================
  5045. */
  5046. void idPhysics_AF::SetJointFrictionDent( const float dent, const float start, const float end ) {
  5047. jointFrictionDent = dent;
  5048. jointFrictionDentStart = start;
  5049. jointFrictionDentEnd = end;
  5050. }
  5051. /*
  5052. ================
  5053. idPhysics_AF::GetJointFrictionScale
  5054. ================
  5055. */
  5056. float idPhysics_AF::GetJointFrictionScale( void ) const {
  5057. if ( jointFrictionDentScale > 0.0f ) {
  5058. return jointFrictionDentScale;
  5059. } else if ( jointFrictionScale > 0.0f ) {
  5060. return jointFrictionScale;
  5061. } else if ( af_jointFrictionScale.GetFloat() > 0.0f ) {
  5062. return af_jointFrictionScale.GetFloat();
  5063. }
  5064. return 1.0f;
  5065. }
  5066. /*
  5067. ================
  5068. idPhysics_AF::SetContactFrictionDent
  5069. ================
  5070. */
  5071. void idPhysics_AF::SetContactFrictionDent( const float dent, const float start, const float end ) {
  5072. contactFrictionDent = dent;
  5073. contactFrictionDentStart = start;
  5074. contactFrictionDentEnd = end;
  5075. }
  5076. /*
  5077. ================
  5078. idPhysics_AF::GetContactFrictionScale
  5079. ================
  5080. */
  5081. float idPhysics_AF::GetContactFrictionScale( void ) const {
  5082. if ( contactFrictionDentScale > 0.0f ) {
  5083. return contactFrictionDentScale;
  5084. } else if ( contactFrictionScale > 0.0f ) {
  5085. return contactFrictionScale;
  5086. } else if ( af_contactFrictionScale.GetFloat() > 0.0f ) {
  5087. return af_contactFrictionScale.GetFloat();
  5088. }
  5089. return 1.0f;
  5090. }
  5091. /*
  5092. ================
  5093. idPhysics_AF::TestIfAtRest
  5094. ================
  5095. */
  5096. bool idPhysics_AF::TestIfAtRest( float timeStep ) {
  5097. int i;
  5098. float translationSqr, maxTranslationSqr, rotation, maxRotation;
  5099. idAFBody *body;
  5100. if ( current.atRest >= 0 ) {
  5101. return true;
  5102. }
  5103. current.activateTime += timeStep;
  5104. // if the simulation should never be suspended before a certaint amount of time passed
  5105. if ( minMoveTime > 0.0f && current.activateTime < minMoveTime ) {
  5106. return false;
  5107. }
  5108. // if the simulation should always be suspended after a certain amount time passed
  5109. if ( maxMoveTime > 0.0f && current.activateTime > maxMoveTime ) {
  5110. return true;
  5111. }
  5112. // test if all bodies hardly moved over a period of time
  5113. if ( current.noMoveTime == 0.0f ) {
  5114. for ( i = 0; i < bodies.Num(); i++ ) {
  5115. body = bodies[i];
  5116. body->atRestOrigin = body->current->worldOrigin;
  5117. body->atRestAxis = body->current->worldAxis;
  5118. }
  5119. current.noMoveTime += timeStep;
  5120. }
  5121. else if ( current.noMoveTime > noMoveTime ) {
  5122. current.noMoveTime = 0.0f;
  5123. maxTranslationSqr = 0.0f;
  5124. maxRotation = 0.0f;
  5125. for ( i = 0; i < bodies.Num(); i++ ) {
  5126. body = bodies[i];
  5127. translationSqr = ( body->current->worldOrigin - body->atRestOrigin ).LengthSqr();
  5128. if ( translationSqr > maxTranslationSqr ) {
  5129. maxTranslationSqr = translationSqr;
  5130. }
  5131. rotation = ( body->atRestAxis.Transpose() * body->current->worldAxis ).ToRotation().GetAngle();
  5132. if ( rotation > maxRotation ) {
  5133. maxRotation = rotation;
  5134. }
  5135. }
  5136. if ( maxTranslationSqr < Square( noMoveTranslation ) && maxRotation < noMoveRotation ) {
  5137. // hardly moved over a period of time so the articulated figure may come to rest
  5138. return true;
  5139. }
  5140. } else {
  5141. current.noMoveTime += timeStep;
  5142. }
  5143. // test if the velocity or acceleration of any body is still too large to come to rest
  5144. for ( i = 0; i < bodies.Num(); i++ ) {
  5145. body = bodies[i];
  5146. if ( body->current->spatialVelocity.SubVec3(0).LengthSqr() > Square( suspendVelocity[0] ) ) {
  5147. return false;
  5148. }
  5149. if ( body->current->spatialVelocity.SubVec3(1).LengthSqr() > Square( suspendVelocity[1] ) ) {
  5150. return false;
  5151. }
  5152. if ( body->acceleration.SubVec3(0).LengthSqr() > Square( suspendAcceleration[0] ) ) {
  5153. return false;
  5154. }
  5155. if ( body->acceleration.SubVec3(1).LengthSqr() > Square( suspendAcceleration[1] ) ) {
  5156. return false;
  5157. }
  5158. }
  5159. // all bodies have a velocity and acceleration small enough to come to rest
  5160. return true;
  5161. }
  5162. /*
  5163. ================
  5164. idPhysics_AF::Rest
  5165. ================
  5166. */
  5167. void idPhysics_AF::Rest( void ) {
  5168. int i;
  5169. current.atRest = gameLocal.time;
  5170. for ( i = 0; i < bodies.Num(); i++ ) {
  5171. bodies[i]->current->spatialVelocity.Zero();
  5172. bodies[i]->current->externalForce.Zero();
  5173. }
  5174. self->BecomeInactive( TH_PHYSICS );
  5175. }
  5176. /*
  5177. ================
  5178. idPhysics_AF::Activate
  5179. ================
  5180. */
  5181. void idPhysics_AF::Activate( void ) {
  5182. // if the articulated figure was at rest
  5183. if ( current.atRest >= 0 ) {
  5184. // normally gravity is added at the end of a simulation frame
  5185. // if the figure was at rest add gravity here so it is applied this simulation frame
  5186. AddGravity();
  5187. // reset the active time for the max move time
  5188. current.activateTime = 0.0f;
  5189. }
  5190. current.atRest = -1;
  5191. current.noMoveTime = 0.0f;
  5192. self->BecomeActive( TH_PHYSICS );
  5193. }
  5194. /*
  5195. ================
  5196. idPhysics_AF::PutToRest
  5197. put to rest untill something collides with this physics object
  5198. ================
  5199. */
  5200. void idPhysics_AF::PutToRest( void ) {
  5201. Rest();
  5202. }
  5203. /*
  5204. ================
  5205. idPhysics_AF::EnableImpact
  5206. ================
  5207. */
  5208. void idPhysics_AF::EnableImpact( void ) {
  5209. noImpact = false;
  5210. }
  5211. /*
  5212. ================
  5213. idPhysics_AF::DisableImpact
  5214. ================
  5215. */
  5216. void idPhysics_AF::DisableImpact( void ) {
  5217. noImpact = true;
  5218. }
  5219. /*
  5220. ================
  5221. idPhysics_AF::AddPushVelocity
  5222. ================
  5223. */
  5224. void idPhysics_AF::AddPushVelocity( const idVec6 &pushVelocity ) {
  5225. int i;
  5226. if ( pushVelocity != vec6_origin ) {
  5227. for ( i = 0; i < bodies.Num(); i++ ) {
  5228. bodies[i]->current->spatialVelocity += pushVelocity;
  5229. }
  5230. }
  5231. }
  5232. /*
  5233. ================
  5234. idPhysics_AF::SetClipModel
  5235. ================
  5236. */
  5237. void idPhysics_AF::SetClipModel( idClipModel *model, float density, int id, bool freeOld ) {
  5238. }
  5239. /*
  5240. ================
  5241. idPhysics_AF::GetClipModel
  5242. ================
  5243. */
  5244. idClipModel *idPhysics_AF::GetClipModel( int id ) const {
  5245. if ( id >= 0 && id < bodies.Num() ) {
  5246. return bodies[id]->GetClipModel();
  5247. }
  5248. return NULL;
  5249. }
  5250. /*
  5251. ================
  5252. idPhysics_AF::GetNumClipModels
  5253. ================
  5254. */
  5255. int idPhysics_AF::GetNumClipModels( void ) const {
  5256. return bodies.Num();
  5257. }
  5258. /*
  5259. ================
  5260. idPhysics_AF::SetMass
  5261. ================
  5262. */
  5263. void idPhysics_AF::SetMass( float mass, int id ) {
  5264. if ( id >= 0 && id < bodies.Num() ) {
  5265. }
  5266. else {
  5267. forceTotalMass = mass;
  5268. }
  5269. SetChanged();
  5270. }
  5271. /*
  5272. ================
  5273. idPhysics_AF::GetMass
  5274. ================
  5275. */
  5276. float idPhysics_AF::GetMass( int id ) const {
  5277. if ( id >= 0 && id < bodies.Num() ) {
  5278. return bodies[id]->mass;
  5279. }
  5280. return totalMass;
  5281. }
  5282. /*
  5283. ================
  5284. idPhysics_AF::SetContents
  5285. ================
  5286. */
  5287. void idPhysics_AF::SetContents( int contents, int id ) {
  5288. int i;
  5289. if ( id >= 0 && id < bodies.Num() ) {
  5290. bodies[id]->GetClipModel()->SetContents( contents );
  5291. }
  5292. else {
  5293. for ( i = 0; i < bodies.Num(); i++ ) {
  5294. bodies[i]->GetClipModel()->SetContents( contents );
  5295. }
  5296. }
  5297. }
  5298. /*
  5299. ================
  5300. idPhysics_AF::GetContents
  5301. ================
  5302. */
  5303. int idPhysics_AF::GetContents( int id ) const {
  5304. int i, contents;
  5305. if ( id >= 0 && id < bodies.Num() ) {
  5306. return bodies[id]->GetClipModel()->GetContents();
  5307. }
  5308. else {
  5309. contents = 0;
  5310. for ( i = 0; i < bodies.Num(); i++ ) {
  5311. contents |= bodies[i]->GetClipModel()->GetContents();
  5312. }
  5313. return contents;
  5314. }
  5315. }
  5316. /*
  5317. ================
  5318. idPhysics_AF::GetBounds
  5319. ================
  5320. */
  5321. const idBounds &idPhysics_AF::GetBounds( int id ) const {
  5322. int i;
  5323. static idBounds relBounds;
  5324. if ( id >= 0 && id < bodies.Num() ) {
  5325. return bodies[id]->GetClipModel()->GetBounds();
  5326. }
  5327. else if ( !bodies.Num() ) {
  5328. relBounds.Zero();
  5329. return relBounds;
  5330. }
  5331. else {
  5332. relBounds = bodies[0]->GetClipModel()->GetBounds();
  5333. for ( i = 1; i < bodies.Num(); i++ ) {
  5334. idBounds bounds;
  5335. idVec3 origin = ( bodies[i]->GetWorldOrigin() - bodies[0]->GetWorldOrigin() ) * bodies[0]->GetWorldAxis().Transpose();
  5336. idMat3 axis = bodies[i]->GetWorldAxis() * bodies[0]->GetWorldAxis().Transpose();
  5337. bounds.FromTransformedBounds( bodies[i]->GetClipModel()->GetBounds(), origin, axis );
  5338. relBounds += bounds;
  5339. }
  5340. return relBounds;
  5341. }
  5342. }
  5343. /*
  5344. ================
  5345. idPhysics_AF::GetAbsBounds
  5346. ================
  5347. */
  5348. const idBounds &idPhysics_AF::GetAbsBounds( int id ) const {
  5349. int i;
  5350. static idBounds absBounds;
  5351. if ( id >= 0 && id < bodies.Num() ) {
  5352. return bodies[id]->GetClipModel()->GetAbsBounds();
  5353. }
  5354. else if ( !bodies.Num() ) {
  5355. absBounds.Zero();
  5356. return absBounds;
  5357. }
  5358. else {
  5359. absBounds = bodies[0]->GetClipModel()->GetAbsBounds();
  5360. for ( i = 1; i < bodies.Num(); i++ ) {
  5361. absBounds += bodies[i]->GetClipModel()->GetAbsBounds();
  5362. }
  5363. return absBounds;
  5364. }
  5365. }
  5366. /*
  5367. ================
  5368. idPhysics_AF::Evaluate
  5369. ================
  5370. */
  5371. bool idPhysics_AF::Evaluate( int timeStepMSec, int endTimeMSec ) {
  5372. float timeStep;
  5373. if ( timeScaleRampStart < MS2SEC( endTimeMSec ) && timeScaleRampEnd > MS2SEC( endTimeMSec ) ) {
  5374. timeStep = MS2SEC( timeStepMSec ) * ( MS2SEC( endTimeMSec ) - timeScaleRampStart ) / ( timeScaleRampEnd - timeScaleRampStart );
  5375. } else if ( af_timeScale.GetFloat() != 1.0f ) {
  5376. timeStep = MS2SEC( timeStepMSec ) * af_timeScale.GetFloat();
  5377. } else {
  5378. timeStep = MS2SEC( timeStepMSec ) * timeScale;
  5379. }
  5380. current.lastTimeStep = timeStep;
  5381. // if the articulated figure changed
  5382. if ( changedAF || ( linearTime != af_useLinearTime.GetBool() ) ) {
  5383. BuildTrees();
  5384. changedAF = false;
  5385. linearTime = af_useLinearTime.GetBool();
  5386. }
  5387. // get the new master position
  5388. if ( masterBody ) {
  5389. idVec3 masterOrigin;
  5390. idMat3 masterAxis;
  5391. self->GetMasterPosition( masterOrigin, masterAxis );
  5392. if ( current.atRest >= 0 && ( masterBody->current->worldOrigin != masterOrigin || masterBody->current->worldAxis != masterAxis ) ) {
  5393. Activate();
  5394. }
  5395. masterBody->current->worldOrigin = masterOrigin;
  5396. masterBody->current->worldAxis = masterAxis;
  5397. }
  5398. // if the simulation is suspended because the figure is at rest
  5399. if ( current.atRest >= 0 || timeStep <= 0.0f ) {
  5400. DebugDraw();
  5401. return false;
  5402. }
  5403. // move the af velocity into the frame of a pusher
  5404. AddPushVelocity( -current.pushVelocity );
  5405. #ifdef AF_TIMINGS
  5406. timer_total.Start();
  5407. #endif
  5408. #ifdef AF_TIMINGS
  5409. timer_collision.Start();
  5410. #endif
  5411. // evaluate contacts
  5412. EvaluateContacts();
  5413. // setup contact constraints
  5414. SetupContactConstraints();
  5415. #ifdef AF_TIMINGS
  5416. timer_collision.Stop();
  5417. #endif
  5418. // evaluate constraint equations
  5419. EvaluateConstraints( timeStep );
  5420. // apply friction
  5421. ApplyFriction( timeStep, endTimeMSec );
  5422. // add frame constraints
  5423. AddFrameConstraints();
  5424. #ifdef AF_TIMINGS
  5425. int i, numPrimary = 0, numAuxiliary = 0;
  5426. for ( i = 0; i < primaryConstraints.Num(); i++ ) {
  5427. numPrimary += primaryConstraints[i]->J1.GetNumRows();
  5428. }
  5429. for ( i = 0; i < auxiliaryConstraints.Num(); i++ ) {
  5430. numAuxiliary += auxiliaryConstraints[i]->J1.GetNumRows();
  5431. }
  5432. timer_pc.Start();
  5433. #endif
  5434. // factor matrices for primary constraints
  5435. PrimaryFactor();
  5436. // calculate forces on bodies after applying primary constraints
  5437. PrimaryForces( timeStep );
  5438. #ifdef AF_TIMINGS
  5439. timer_pc.Stop();
  5440. timer_ac.Start();
  5441. #endif
  5442. // calculate and apply auxiliary constraint forces
  5443. AuxiliaryForces( timeStep );
  5444. #ifdef AF_TIMINGS
  5445. timer_ac.Stop();
  5446. #endif
  5447. // evolve current state to next state
  5448. Evolve( timeStep );
  5449. // debug graphics
  5450. DebugDraw();
  5451. // clear external forces on all bodies
  5452. ClearExternalForce();
  5453. // apply contact force to other entities
  5454. ApplyContactForces();
  5455. // remove all frame constraints
  5456. RemoveFrameConstraints();
  5457. #ifdef AF_TIMINGS
  5458. timer_collision.Start();
  5459. #endif
  5460. // check for collisions between current and next state
  5461. CheckForCollisions( timeStep );
  5462. #ifdef AF_TIMINGS
  5463. timer_collision.Stop();
  5464. #endif
  5465. // swap the current and next state
  5466. SwapStates();
  5467. // make sure all clip models are disabled in case they were enabled for self collision
  5468. if ( selfCollision && !af_skipSelfCollision.GetBool() ) {
  5469. DisableClip();
  5470. }
  5471. // apply collision impulses
  5472. if ( ApplyCollisions( timeStep ) ) {
  5473. current.atRest = gameLocal.time;
  5474. comeToRest = true;
  5475. }
  5476. // test if the simulation can be suspended because the whole figure is at rest
  5477. if ( comeToRest && TestIfAtRest( timeStep ) ) {
  5478. Rest();
  5479. } else {
  5480. ActivateContactEntities();
  5481. }
  5482. // add gravitational force
  5483. AddGravity();
  5484. // move the af velocity back into the world frame
  5485. AddPushVelocity( current.pushVelocity );
  5486. current.pushVelocity.Zero();
  5487. if ( IsOutsideWorld() ) {
  5488. gameLocal.Warning( "articulated figure moved outside world bounds for entity '%s' type '%s' at (%s)",
  5489. self->name.c_str(), self->GetType()->classname, bodies[0]->current->worldOrigin.ToString(0) );
  5490. Rest();
  5491. }
  5492. #ifdef AF_TIMINGS
  5493. timer_total.Stop();
  5494. if ( af_showTimings.GetInteger() == 1 ) {
  5495. gameLocal.Printf( "%12s: t %1.4f pc %2d, %1.4f ac %2d %1.4f lcp %1.4f cd %1.4f\n",
  5496. self->name.c_str(),
  5497. timer_total.Milliseconds(),
  5498. numPrimary, timer_pc.Milliseconds(),
  5499. numAuxiliary, timer_ac.Milliseconds() - timer_lcp.Milliseconds(),
  5500. timer_lcp.Milliseconds(), timer_collision.Milliseconds() );
  5501. }
  5502. else if ( af_showTimings.GetInteger() == 2 ) {
  5503. numArticulatedFigures++;
  5504. if ( endTimeMSec > lastTimerReset ) {
  5505. gameLocal.Printf( "af %d: t %1.4f pc %2d, %1.4f ac %2d %1.4f lcp %1.4f cd %1.4f\n",
  5506. numArticulatedFigures,
  5507. timer_total.Milliseconds(),
  5508. numPrimary, timer_pc.Milliseconds(),
  5509. numAuxiliary, timer_ac.Milliseconds() - timer_lcp.Milliseconds(),
  5510. timer_lcp.Milliseconds(), timer_collision.Milliseconds() );
  5511. }
  5512. }
  5513. if ( endTimeMSec > lastTimerReset ) {
  5514. lastTimerReset = endTimeMSec;
  5515. numArticulatedFigures = 0;
  5516. timer_total.Clear();
  5517. timer_pc.Clear();
  5518. timer_ac.Clear();
  5519. timer_collision.Clear();
  5520. timer_lcp.Clear();
  5521. }
  5522. #endif
  5523. return true;
  5524. }
  5525. /*
  5526. ================
  5527. idPhysics_AF::UpdateTime
  5528. ================
  5529. */
  5530. void idPhysics_AF::UpdateTime( int endTimeMSec ) {
  5531. }
  5532. /*
  5533. ================
  5534. idPhysics_AF::GetTime
  5535. ================
  5536. */
  5537. int idPhysics_AF::GetTime( void ) const {
  5538. return gameLocal.time;
  5539. }
  5540. /*
  5541. ================
  5542. DrawTraceModelSilhouette
  5543. ================
  5544. */
  5545. void DrawTraceModelSilhouette( const idVec3 &projectionOrigin, const idClipModel *clipModel ) {
  5546. int i, numSilEdges;
  5547. int silEdges[MAX_TRACEMODEL_EDGES];
  5548. idVec3 v1, v2;
  5549. const idTraceModel *trm = clipModel->GetTraceModel();
  5550. const idVec3 &origin = clipModel->GetOrigin();
  5551. const idMat3 &axis = clipModel->GetAxis();
  5552. numSilEdges = trm->GetProjectionSilhouetteEdges( ( projectionOrigin - origin ) * axis.Transpose(), silEdges );
  5553. for ( i = 0; i < numSilEdges; i++ ) {
  5554. v1 = trm->verts[ trm->edges[ abs(silEdges[i]) ].v[ INTSIGNBITSET( silEdges[i] ) ] ];
  5555. v2 = trm->verts[ trm->edges[ abs(silEdges[i]) ].v[ INTSIGNBITNOTSET( silEdges[i] ) ] ];
  5556. gameRenderWorld->DebugArrow( colorRed, origin + v1 * axis, origin + v2 * axis, 1 );
  5557. }
  5558. }
  5559. /*
  5560. ================
  5561. idPhysics_AF::DebugDraw
  5562. ================
  5563. */
  5564. void idPhysics_AF::DebugDraw( void ) {
  5565. int i;
  5566. idAFBody *body, *highlightBody = NULL, *constrainedBody1 = NULL, *constrainedBody2 = NULL;
  5567. idAFConstraint *constraint;
  5568. idVec3 center;
  5569. idMat3 axis;
  5570. if ( af_highlightConstraint.GetString()[0] ) {
  5571. constraint = GetConstraint( af_highlightConstraint.GetString() );
  5572. if ( constraint ) {
  5573. constraint->GetCenter( center );
  5574. axis = gameLocal.GetLocalPlayer()->viewAngles.ToMat3();
  5575. gameRenderWorld->DebugCone( colorYellow, center, (axis[2] - axis[1]) * 4.0f, 0.0f, 1.0f, 0 );
  5576. if ( af_showConstrainedBodies.GetBool() ) {
  5577. cvarSystem->SetCVarString( "cm_drawColor", colorCyan.ToString( 0 ) );
  5578. constrainedBody1 = constraint->body1;
  5579. if ( constrainedBody1 ) {
  5580. collisionModelManager->DrawModel( constrainedBody1->clipModel->Handle(), constrainedBody1->clipModel->GetOrigin(),
  5581. constrainedBody1->clipModel->GetAxis(), vec3_origin, 0.0f );
  5582. }
  5583. cvarSystem->SetCVarString( "cm_drawColor", colorBlue.ToString( 0 ) );
  5584. constrainedBody2 = constraint->body2;
  5585. if ( constrainedBody2 ) {
  5586. collisionModelManager->DrawModel( constrainedBody2->clipModel->Handle(), constrainedBody2->clipModel->GetOrigin(),
  5587. constrainedBody2->clipModel->GetAxis(), vec3_origin, 0.0f );
  5588. }
  5589. cvarSystem->SetCVarString( "cm_drawColor", colorRed.ToString( 0 ) );
  5590. }
  5591. }
  5592. }
  5593. if ( af_highlightBody.GetString()[0] ) {
  5594. highlightBody = GetBody( af_highlightBody.GetString() );
  5595. if ( highlightBody ) {
  5596. cvarSystem->SetCVarString( "cm_drawColor", colorYellow.ToString( 0 ) );
  5597. collisionModelManager->DrawModel( highlightBody->clipModel->Handle(), highlightBody->clipModel->GetOrigin(),
  5598. highlightBody->clipModel->GetAxis(), vec3_origin, 0.0f );
  5599. cvarSystem->SetCVarString( "cm_drawColor", colorRed.ToString( 0 ) );
  5600. }
  5601. }
  5602. if ( af_showBodies.GetBool() ) {
  5603. for ( i = 0; i < bodies.Num(); i++ ) {
  5604. body = bodies[i];
  5605. if ( body == constrainedBody1 || body == constrainedBody2 ) {
  5606. continue;
  5607. }
  5608. if ( body == highlightBody ) {
  5609. continue;
  5610. }
  5611. collisionModelManager->DrawModel( body->clipModel->Handle(), body->clipModel->GetOrigin(),
  5612. body->clipModel->GetAxis(), vec3_origin, 0.0f );
  5613. //DrawTraceModelSilhouette( gameLocal.GetLocalPlayer()->GetEyePosition(), body->clipModel );
  5614. }
  5615. }
  5616. if ( af_showBodyNames.GetBool() ) {
  5617. for ( i = 0; i < bodies.Num(); i++ ) {
  5618. body = bodies[i];
  5619. gameRenderWorld->DrawText( body->GetName().c_str(), body->GetWorldOrigin(), 0.08f, colorCyan, gameLocal.GetLocalPlayer()->viewAngles.ToMat3(), 1 );
  5620. }
  5621. }
  5622. if ( af_showMass.GetBool() ) {
  5623. for ( i = 0; i < bodies.Num(); i++ ) {
  5624. body = bodies[i];
  5625. gameRenderWorld->DrawText( va( "\n%1.2f", 1.0f / body->GetInverseMass() ), body->GetWorldOrigin(), 0.08f, colorCyan, gameLocal.GetLocalPlayer()->viewAngles.ToMat3(), 1 );
  5626. }
  5627. }
  5628. if ( af_showTotalMass.GetBool() ) {
  5629. axis = gameLocal.GetLocalPlayer()->viewAngles.ToMat3();
  5630. gameRenderWorld->DrawText( va( "\n%1.2f", totalMass ), bodies[0]->GetWorldOrigin() + axis[2] * 8.0f, 0.15f, colorCyan, axis, 1 );
  5631. }
  5632. if ( af_showInertia.GetBool() ) {
  5633. for ( i = 0; i < bodies.Num(); i++ ) {
  5634. body = bodies[i];
  5635. idMat3 &I = body->inertiaTensor;
  5636. gameRenderWorld->DrawText( va( "\n\n\n( %.1f %.1f %.1f )\n( %.1f %.1f %.1f )\n( %.1f %.1f %.1f )",
  5637. I[0].x, I[0].y, I[0].z,
  5638. I[1].x, I[1].y, I[1].z,
  5639. I[2].x, I[2].y, I[2].z ),
  5640. body->GetWorldOrigin(), 0.05f, colorCyan, gameLocal.GetLocalPlayer()->viewAngles.ToMat3(), 1 );
  5641. }
  5642. }
  5643. if ( af_showVelocity.GetBool() ) {
  5644. for ( i = 0; i < bodies.Num(); i++ ) {
  5645. DrawVelocity( bodies[i]->clipModel->GetId(), 0.1f, 4.0f );
  5646. }
  5647. }
  5648. if ( af_showConstraints.GetBool() ) {
  5649. for ( i = 0; i < primaryConstraints.Num(); i++ ) {
  5650. constraint = primaryConstraints[i];
  5651. constraint->DebugDraw();
  5652. }
  5653. if ( !af_showPrimaryOnly.GetBool() ) {
  5654. for ( i = 0; i < auxiliaryConstraints.Num(); i++ ) {
  5655. constraint = auxiliaryConstraints[i];
  5656. constraint->DebugDraw();
  5657. }
  5658. }
  5659. }
  5660. if ( af_showConstraintNames.GetBool() ) {
  5661. for ( i = 0; i < primaryConstraints.Num(); i++ ) {
  5662. constraint = primaryConstraints[i];
  5663. constraint->GetCenter( center );
  5664. gameRenderWorld->DrawText( constraint->GetName().c_str(), center, 0.08f, colorCyan, gameLocal.GetLocalPlayer()->viewAngles.ToMat3(), 1 );
  5665. }
  5666. if ( !af_showPrimaryOnly.GetBool() ) {
  5667. for ( i = 0; i < auxiliaryConstraints.Num(); i++ ) {
  5668. constraint = auxiliaryConstraints[i];
  5669. constraint->GetCenter( center );
  5670. gameRenderWorld->DrawText( constraint->GetName().c_str(), center, 0.08f, colorCyan, gameLocal.GetLocalPlayer()->viewAngles.ToMat3(), 1 );
  5671. }
  5672. }
  5673. }
  5674. if ( af_showTrees.GetBool() || ( af_showActive.GetBool() && current.atRest < 0 ) ) {
  5675. for ( i = 0; i < trees.Num(); i++ ) {
  5676. trees[i]->DebugDraw( idStr::ColorForIndex( i+3 ) );
  5677. }
  5678. }
  5679. }
  5680. /*
  5681. ================
  5682. idPhysics_AF::idPhysics_AF
  5683. ================
  5684. */
  5685. idPhysics_AF::idPhysics_AF( void ) {
  5686. trees.Clear();
  5687. bodies.Clear();
  5688. constraints.Clear();
  5689. primaryConstraints.Clear();
  5690. auxiliaryConstraints.Clear();
  5691. frameConstraints.Clear();
  5692. contacts.Clear();
  5693. collisions.Clear();
  5694. changedAF = true;
  5695. masterBody = NULL;
  5696. lcp = idLCP::AllocSymmetric();
  5697. memset( &current, 0, sizeof( current ) );
  5698. current.atRest = -1;
  5699. current.lastTimeStep = USERCMD_MSEC;
  5700. saved = current;
  5701. linearFriction = 0.005f;
  5702. angularFriction = 0.005f;
  5703. contactFriction = 0.8f;
  5704. bouncyness = 0.4f;
  5705. totalMass = 0.0f;
  5706. forceTotalMass = -1.0f;
  5707. suspendVelocity.Set( SUSPEND_LINEAR_VELOCITY, SUSPEND_ANGULAR_VELOCITY );
  5708. suspendAcceleration.Set( SUSPEND_LINEAR_ACCELERATION, SUSPEND_LINEAR_ACCELERATION );
  5709. noMoveTime = NO_MOVE_TIME;
  5710. noMoveTranslation = NO_MOVE_TRANSLATION_TOLERANCE;
  5711. noMoveRotation = NO_MOVE_ROTATION_TOLERANCE;
  5712. minMoveTime = MIN_MOVE_TIME;
  5713. maxMoveTime = MAX_MOVE_TIME;
  5714. impulseThreshold = IMPULSE_THRESHOLD;
  5715. timeScale = 1.0f;
  5716. timeScaleRampStart = 0.0f;
  5717. timeScaleRampEnd = 0.0f;
  5718. jointFrictionScale = 0.0f;
  5719. jointFrictionDent = 0.0f;
  5720. jointFrictionDentStart = 0.0f;
  5721. jointFrictionDentEnd = 0.0f;
  5722. jointFrictionDentScale = 0.0f;
  5723. contactFrictionScale = 0.0f;
  5724. contactFrictionDent = 0.0f;
  5725. contactFrictionDentStart = 0.0f;
  5726. contactFrictionDentEnd = 0.0f;
  5727. contactFrictionDentScale = 0.0f;
  5728. enableCollision = true;
  5729. selfCollision = true;
  5730. comeToRest = true;
  5731. linearTime = true;
  5732. noImpact = false;
  5733. worldConstraintsLocked = false;
  5734. forcePushable = false;
  5735. #ifdef AF_TIMINGS
  5736. lastTimerReset = 0;
  5737. #endif
  5738. }
  5739. /*
  5740. ================
  5741. idPhysics_AF::~idPhysics_AF
  5742. ================
  5743. */
  5744. idPhysics_AF::~idPhysics_AF( void ) {
  5745. int i;
  5746. trees.DeleteContents( true );
  5747. for ( i = 0; i < bodies.Num(); i++ ) {
  5748. delete bodies[i];
  5749. }
  5750. for ( i = 0; i < constraints.Num(); i++ ) {
  5751. delete constraints[i];
  5752. }
  5753. contactConstraints.SetNum( contactConstraints.NumAllocated(), false );
  5754. for ( i = 0; i < contactConstraints.NumAllocated(); i++ ) {
  5755. delete contactConstraints[i];
  5756. }
  5757. delete lcp;
  5758. if ( masterBody ) {
  5759. delete masterBody;
  5760. }
  5761. }
  5762. /*
  5763. ================
  5764. idPhysics_AF_SavePState
  5765. ================
  5766. */
  5767. void idPhysics_AF_SavePState( idSaveGame *saveFile, const AFPState_t &state ) {
  5768. saveFile->WriteInt( state.atRest );
  5769. saveFile->WriteFloat( state.noMoveTime );
  5770. saveFile->WriteFloat( state.activateTime );
  5771. saveFile->WriteFloat( state.lastTimeStep );
  5772. saveFile->WriteVec6( state.pushVelocity );
  5773. }
  5774. /*
  5775. ================
  5776. idPhysics_AF_RestorePState
  5777. ================
  5778. */
  5779. void idPhysics_AF_RestorePState( idRestoreGame *saveFile, AFPState_t &state ) {
  5780. saveFile->ReadInt( state.atRest );
  5781. saveFile->ReadFloat( state.noMoveTime );
  5782. saveFile->ReadFloat( state.activateTime );
  5783. saveFile->ReadFloat( state.lastTimeStep );
  5784. saveFile->ReadVec6( state.pushVelocity );
  5785. }
  5786. /*
  5787. ================
  5788. idPhysics_AF::Save
  5789. ================
  5790. */
  5791. void idPhysics_AF::Save( idSaveGame *saveFile ) const {
  5792. int i;
  5793. // the articulated figure structure is handled by the owner
  5794. idPhysics_AF_SavePState( saveFile, current );
  5795. idPhysics_AF_SavePState( saveFile, saved );
  5796. saveFile->WriteInt( bodies.Num() );
  5797. for ( i = 0; i < bodies.Num(); i++ ) {
  5798. bodies[i]->Save( saveFile );
  5799. }
  5800. if ( masterBody ) {
  5801. saveFile->WriteBool( true );
  5802. masterBody->Save( saveFile );
  5803. } else {
  5804. saveFile->WriteBool( false );
  5805. }
  5806. saveFile->WriteInt( constraints.Num() );
  5807. for ( i = 0; i < constraints.Num(); i++ ) {
  5808. constraints[i]->Save( saveFile );
  5809. }
  5810. saveFile->WriteBool( changedAF );
  5811. saveFile->WriteFloat( linearFriction );
  5812. saveFile->WriteFloat( angularFriction );
  5813. saveFile->WriteFloat( contactFriction );
  5814. saveFile->WriteFloat( bouncyness );
  5815. saveFile->WriteFloat( totalMass );
  5816. saveFile->WriteFloat( forceTotalMass );
  5817. saveFile->WriteVec2( suspendVelocity );
  5818. saveFile->WriteVec2( suspendAcceleration );
  5819. saveFile->WriteFloat( noMoveTime );
  5820. saveFile->WriteFloat( noMoveTranslation );
  5821. saveFile->WriteFloat( noMoveRotation );
  5822. saveFile->WriteFloat( minMoveTime );
  5823. saveFile->WriteFloat( maxMoveTime );
  5824. saveFile->WriteFloat( impulseThreshold );
  5825. saveFile->WriteFloat( timeScale );
  5826. saveFile->WriteFloat( timeScaleRampStart );
  5827. saveFile->WriteFloat( timeScaleRampEnd );
  5828. saveFile->WriteFloat( jointFrictionScale );
  5829. saveFile->WriteFloat( jointFrictionDent );
  5830. saveFile->WriteFloat( jointFrictionDentStart );
  5831. saveFile->WriteFloat( jointFrictionDentEnd );
  5832. saveFile->WriteFloat( jointFrictionDentScale );
  5833. saveFile->WriteFloat( contactFrictionScale );
  5834. saveFile->WriteFloat( contactFrictionDent );
  5835. saveFile->WriteFloat( contactFrictionDentStart );
  5836. saveFile->WriteFloat( contactFrictionDentEnd );
  5837. saveFile->WriteFloat( contactFrictionDentScale );
  5838. saveFile->WriteBool( enableCollision );
  5839. saveFile->WriteBool( selfCollision );
  5840. saveFile->WriteBool( comeToRest );
  5841. saveFile->WriteBool( linearTime );
  5842. saveFile->WriteBool( noImpact );
  5843. saveFile->WriteBool( worldConstraintsLocked );
  5844. saveFile->WriteBool( forcePushable );
  5845. }
  5846. /*
  5847. ================
  5848. idPhysics_AF::Restore
  5849. ================
  5850. */
  5851. void idPhysics_AF::Restore( idRestoreGame *saveFile ) {
  5852. int i, num;
  5853. bool hasMaster;
  5854. // the articulated figure structure should have already been restored
  5855. idPhysics_AF_RestorePState( saveFile, current );
  5856. idPhysics_AF_RestorePState( saveFile, saved );
  5857. saveFile->ReadInt( num );
  5858. assert( num == bodies.Num() );
  5859. for ( i = 0; i < bodies.Num(); i++ ) {
  5860. bodies[i]->Restore( saveFile );
  5861. }
  5862. saveFile->ReadBool( hasMaster );
  5863. if ( hasMaster ) {
  5864. masterBody = new idAFBody();
  5865. masterBody->Restore( saveFile );
  5866. }
  5867. saveFile->ReadInt( num );
  5868. assert( num == constraints.Num() );
  5869. for ( i = 0; i < constraints.Num(); i++ ) {
  5870. constraints[i]->Restore( saveFile );
  5871. }
  5872. saveFile->ReadBool( changedAF );
  5873. saveFile->ReadFloat( linearFriction );
  5874. saveFile->ReadFloat( angularFriction );
  5875. saveFile->ReadFloat( contactFriction );
  5876. saveFile->ReadFloat( bouncyness );
  5877. saveFile->ReadFloat( totalMass );
  5878. saveFile->ReadFloat( forceTotalMass );
  5879. saveFile->ReadVec2( suspendVelocity );
  5880. saveFile->ReadVec2( suspendAcceleration );
  5881. saveFile->ReadFloat( noMoveTime );
  5882. saveFile->ReadFloat( noMoveTranslation );
  5883. saveFile->ReadFloat( noMoveRotation );
  5884. saveFile->ReadFloat( minMoveTime );
  5885. saveFile->ReadFloat( maxMoveTime );
  5886. saveFile->ReadFloat( impulseThreshold );
  5887. saveFile->ReadFloat( timeScale );
  5888. saveFile->ReadFloat( timeScaleRampStart );
  5889. saveFile->ReadFloat( timeScaleRampEnd );
  5890. saveFile->ReadFloat( jointFrictionScale );
  5891. saveFile->ReadFloat( jointFrictionDent );
  5892. saveFile->ReadFloat( jointFrictionDentStart );
  5893. saveFile->ReadFloat( jointFrictionDentEnd );
  5894. saveFile->ReadFloat( jointFrictionDentScale );
  5895. saveFile->ReadFloat( contactFrictionScale );
  5896. saveFile->ReadFloat( contactFrictionDent );
  5897. saveFile->ReadFloat( contactFrictionDentStart );
  5898. saveFile->ReadFloat( contactFrictionDentEnd );
  5899. saveFile->ReadFloat( contactFrictionDentScale );
  5900. saveFile->ReadBool( enableCollision );
  5901. saveFile->ReadBool( selfCollision );
  5902. saveFile->ReadBool( comeToRest );
  5903. saveFile->ReadBool( linearTime );
  5904. saveFile->ReadBool( noImpact );
  5905. saveFile->ReadBool( worldConstraintsLocked );
  5906. saveFile->ReadBool( forcePushable );
  5907. changedAF = true;
  5908. UpdateClipModels();
  5909. }
  5910. /*
  5911. ================
  5912. idPhysics_AF::IsClosedLoop
  5913. ================
  5914. */
  5915. bool idPhysics_AF::IsClosedLoop( const idAFBody *body1, const idAFBody *body2 ) const {
  5916. const idAFBody *b1, *b2;
  5917. for ( b1 = body1; b1->parent; b1 = b1->parent ) {
  5918. }
  5919. for ( b2 = body2; b2->parent; b2 = b2->parent ) {
  5920. }
  5921. return ( b1 == b2 );
  5922. }
  5923. /*
  5924. ================
  5925. idPhysics_AF::BuildTrees
  5926. ================
  5927. */
  5928. void idPhysics_AF::BuildTrees( void ) {
  5929. int i;
  5930. float scale;
  5931. idAFBody *b;
  5932. idAFConstraint *c;
  5933. idAFTree *tree;
  5934. primaryConstraints.Clear();
  5935. auxiliaryConstraints.Clear();
  5936. trees.DeleteContents( true );
  5937. totalMass = 0.0f;
  5938. for ( i = 0; i < bodies.Num(); i++ ) {
  5939. b = bodies[i];
  5940. b->parent = NULL;
  5941. b->primaryConstraint = NULL;
  5942. b->constraints.SetNum( 0, false );
  5943. b->children.Clear();
  5944. b->tree = NULL;
  5945. totalMass += b->mass;
  5946. }
  5947. if ( forceTotalMass > 0.0f ) {
  5948. scale = forceTotalMass / totalMass;
  5949. for ( i = 0; i < bodies.Num(); i++ ) {
  5950. b = bodies[i];
  5951. b->mass *= scale;
  5952. b->invMass = 1.0f / b->mass;
  5953. b->inertiaTensor *= scale;
  5954. b->inverseInertiaTensor = b->inertiaTensor.Inverse();
  5955. }
  5956. totalMass = forceTotalMass;
  5957. }
  5958. if ( af_useLinearTime.GetBool() ) {
  5959. for ( i = 0; i < constraints.Num(); i++ ) {
  5960. c = constraints[i];
  5961. c->body1->constraints.Append( c );
  5962. if ( c->body2 ) {
  5963. c->body2->constraints.Append( c );
  5964. }
  5965. // only bilateral constraints between two non-world bodies that do not
  5966. // create loops can be used as primary constraints
  5967. if ( !c->body1->primaryConstraint && c->fl.allowPrimary && c->body2 != NULL && !IsClosedLoop( c->body1, c->body2 ) ) {
  5968. c->body1->primaryConstraint = c;
  5969. c->body1->parent = c->body2;
  5970. c->body2->children.Append( c->body1 );
  5971. c->fl.isPrimary = true;
  5972. c->firstIndex = 0;
  5973. primaryConstraints.Append( c );
  5974. } else {
  5975. c->fl.isPrimary = false;
  5976. auxiliaryConstraints.Append( c );
  5977. }
  5978. }
  5979. // create trees for all parent bodies
  5980. for ( i = 0; i < bodies.Num(); i++ ) {
  5981. if ( !bodies[i]->parent ) {
  5982. tree = new idAFTree();
  5983. tree->sortedBodies.Clear();
  5984. tree->sortedBodies.Append( bodies[i] );
  5985. bodies[i]->tree = tree;
  5986. trees.Append( tree );
  5987. }
  5988. }
  5989. // add each child body to the appropriate tree
  5990. for ( i = 0; i < bodies.Num(); i++ ) {
  5991. if ( bodies[i]->parent ) {
  5992. for ( b = bodies[i]->parent; !b->tree; b = b->parent ) {
  5993. }
  5994. b->tree->sortedBodies.Append( bodies[i] );
  5995. bodies[i]->tree = b->tree;
  5996. }
  5997. }
  5998. if ( trees.Num() > 1 ) {
  5999. gameLocal.Warning( "Articulated figure has multiple seperate tree structures for entity '%s' type '%s'.",
  6000. self->name.c_str(), self->GetType()->classname );
  6001. }
  6002. // sort bodies in each tree to make sure parents come first
  6003. for ( i = 0; i < trees.Num(); i++ ) {
  6004. trees[i]->SortBodies();
  6005. }
  6006. } else {
  6007. // create a tree for each body
  6008. for ( i = 0; i < bodies.Num(); i++ ) {
  6009. tree = new idAFTree();
  6010. tree->sortedBodies.Clear();
  6011. tree->sortedBodies.Append( bodies[i] );
  6012. bodies[i]->tree = tree;
  6013. trees.Append( tree );
  6014. }
  6015. for ( i = 0; i < constraints.Num(); i++ ) {
  6016. c = constraints[i];
  6017. c->body1->constraints.Append( c );
  6018. if ( c->body2 ) {
  6019. c->body2->constraints.Append( c );
  6020. }
  6021. c->fl.isPrimary = false;
  6022. auxiliaryConstraints.Append( c );
  6023. }
  6024. }
  6025. }
  6026. /*
  6027. ================
  6028. idPhysics_AF::AddBody
  6029. bodies get an id in the order they are added starting at zero
  6030. as such the first body added will get id zero
  6031. ================
  6032. */
  6033. int idPhysics_AF::AddBody( idAFBody *body ) {
  6034. int id = 0;
  6035. if ( !body->clipModel ) {
  6036. gameLocal.Error( "idPhysics_AF::AddBody: body '%s' has no clip model.", body->name.c_str() );
  6037. }
  6038. if ( bodies.Find( body ) ) {
  6039. gameLocal.Error( "idPhysics_AF::AddBody: body '%s' added twice.", body->name.c_str() );
  6040. }
  6041. if ( GetBody( body->name ) ) {
  6042. gameLocal.Error( "idPhysics_AF::AddBody: a body with the name '%s' already exists.", body->name.c_str() );
  6043. }
  6044. id = bodies.Num();
  6045. body->clipModel->SetId( id );
  6046. if ( body->linearFriction < 0.0f ) {
  6047. body->linearFriction = linearFriction;
  6048. body->angularFriction = angularFriction;
  6049. body->contactFriction = contactFriction;
  6050. }
  6051. if ( body->bouncyness < 0.0f ) {
  6052. body->bouncyness = bouncyness;
  6053. }
  6054. if ( !body->fl.clipMaskSet ) {
  6055. body->clipMask = clipMask;
  6056. }
  6057. bodies.Append( body );
  6058. changedAF = true;
  6059. return id;
  6060. }
  6061. /*
  6062. ================
  6063. idPhysics_AF::AddConstraint
  6064. ================
  6065. */
  6066. void idPhysics_AF::AddConstraint( idAFConstraint *constraint ) {
  6067. if ( constraints.Find( constraint ) ) {
  6068. gameLocal.Error( "idPhysics_AF::AddConstraint: constraint '%s' added twice.", constraint->name.c_str() );
  6069. }
  6070. if ( GetConstraint( constraint->name ) ) {
  6071. gameLocal.Error( "idPhysics_AF::AddConstraint: a constraint with the name '%s' already exists.", constraint->name.c_str() );
  6072. }
  6073. if ( !constraint->body1 ) {
  6074. gameLocal.Error( "idPhysics_AF::AddConstraint: body1 == NULL on constraint '%s'.", constraint->name.c_str() );
  6075. }
  6076. if ( !bodies.Find( constraint->body1 ) ) {
  6077. gameLocal.Error( "idPhysics_AF::AddConstraint: body1 of constraint '%s' is not part of the articulated figure.", constraint->name.c_str() );
  6078. }
  6079. if ( constraint->body2 && !bodies.Find( constraint->body2 ) ) {
  6080. gameLocal.Error( "idPhysics_AF::AddConstraint: body2 of constraint '%s' is not part of the articulated figure.", constraint->name.c_str() );
  6081. }
  6082. if ( constraint->body1 == constraint->body2 ) {
  6083. gameLocal.Error( "idPhysics_AF::AddConstraint: body1 and body2 of constraint '%s' are the same.", constraint->name.c_str() );
  6084. }
  6085. constraints.Append( constraint );
  6086. constraint->physics = this;
  6087. changedAF = true;
  6088. }
  6089. /*
  6090. ================
  6091. idPhysics_AF::AddFrameConstraint
  6092. ================
  6093. */
  6094. void idPhysics_AF::AddFrameConstraint( idAFConstraint *constraint ) {
  6095. frameConstraints.Append( constraint );
  6096. constraint->physics = this;
  6097. }
  6098. /*
  6099. ================
  6100. idPhysics_AF::ForceBodyId
  6101. ================
  6102. */
  6103. void idPhysics_AF::ForceBodyId( idAFBody *body, int newId ) {
  6104. int id;
  6105. id = bodies.FindIndex( body );
  6106. if ( id == -1 ) {
  6107. gameLocal.Error( "ForceBodyId: body '%s' is not part of the articulated figure.\n", body->name.c_str() );
  6108. }
  6109. if ( id != newId ) {
  6110. idAFBody *b = bodies[newId];
  6111. bodies[newId] = bodies[id];
  6112. bodies[id] = b;
  6113. changedAF = true;
  6114. }
  6115. }
  6116. /*
  6117. ================
  6118. idPhysics_AF::GetBodyId
  6119. ================
  6120. */
  6121. int idPhysics_AF::GetBodyId( idAFBody *body ) const {
  6122. int id;
  6123. id = bodies.FindIndex( body );
  6124. if ( id == -1 && body ) {
  6125. gameLocal.Error( "GetBodyId: body '%s' is not part of the articulated figure.\n", body->name.c_str() );
  6126. }
  6127. return id;
  6128. }
  6129. /*
  6130. ================
  6131. idPhysics_AF::GetBodyId
  6132. ================
  6133. */
  6134. int idPhysics_AF::GetBodyId( const char *bodyName ) const {
  6135. int i;
  6136. for ( i = 0; i < bodies.Num(); i++ ) {
  6137. if ( !bodies[i]->name.Icmp( bodyName ) ) {
  6138. return i;
  6139. }
  6140. }
  6141. gameLocal.Error( "GetBodyId: no body with the name '%s' is not part of the articulated figure.\n", bodyName );
  6142. return 0;
  6143. }
  6144. /*
  6145. ================
  6146. idPhysics_AF::GetConstraintId
  6147. ================
  6148. */
  6149. int idPhysics_AF::GetConstraintId( idAFConstraint *constraint ) const {
  6150. int id;
  6151. id = constraints.FindIndex( constraint );
  6152. if ( id == -1 && constraint ) {
  6153. gameLocal.Error( "GetConstraintId: constraint '%s' is not part of the articulated figure.\n", constraint->name.c_str() );
  6154. }
  6155. return id;
  6156. }
  6157. /*
  6158. ================
  6159. idPhysics_AF::GetConstraintId
  6160. ================
  6161. */
  6162. int idPhysics_AF::GetConstraintId( const char *constraintName ) const {
  6163. int i;
  6164. for ( i = 0; i < constraints.Num(); i++ ) {
  6165. if ( constraints[i]->name.Icmp( constraintName ) == 0 ) {
  6166. return i;
  6167. }
  6168. }
  6169. gameLocal.Error( "GetConstraintId: no constraint with the name '%s' is not part of the articulated figure.\n", constraintName );
  6170. return 0;
  6171. }
  6172. /*
  6173. ================
  6174. idPhysics_AF::GetNumBodies
  6175. ================
  6176. */
  6177. int idPhysics_AF::GetNumBodies( void ) const {
  6178. return bodies.Num();
  6179. }
  6180. /*
  6181. ================
  6182. idPhysics_AF::GetNumConstraints
  6183. ================
  6184. */
  6185. int idPhysics_AF::GetNumConstraints( void ) const {
  6186. return constraints.Num();
  6187. }
  6188. /*
  6189. ================
  6190. idPhysics_AF::GetBody
  6191. ================
  6192. */
  6193. idAFBody *idPhysics_AF::GetBody( const char *bodyName ) const {
  6194. int i;
  6195. for ( i = 0; i < bodies.Num(); i++ ) {
  6196. if ( !bodies[i]->name.Icmp( bodyName ) ) {
  6197. return bodies[i];
  6198. }
  6199. }
  6200. return NULL;
  6201. }
  6202. /*
  6203. ================
  6204. idPhysics_AF::GetBody
  6205. ================
  6206. */
  6207. idAFBody *idPhysics_AF::GetBody( const int id ) const {
  6208. if ( id < 0 || id >= bodies.Num() ) {
  6209. gameLocal.Error( "GetBody: no body with id %d exists\n", id );
  6210. return NULL;
  6211. }
  6212. return bodies[id];
  6213. }
  6214. /*
  6215. ================
  6216. idPhysics_AF::GetConstraint
  6217. ================
  6218. */
  6219. idAFConstraint *idPhysics_AF::GetConstraint( const char *constraintName ) const {
  6220. int i;
  6221. for ( i = 0; i < constraints.Num(); i++ ) {
  6222. if ( constraints[i]->name.Icmp( constraintName ) == 0 ) {
  6223. return constraints[i];
  6224. }
  6225. }
  6226. return NULL;
  6227. }
  6228. /*
  6229. ================
  6230. idPhysics_AF::GetConstraint
  6231. ================
  6232. */
  6233. idAFConstraint *idPhysics_AF::GetConstraint( const int id ) const {
  6234. if ( id < 0 || id >= constraints.Num() ) {
  6235. gameLocal.Error( "GetConstraint: no constraint with id %d exists\n", id );
  6236. return NULL;
  6237. }
  6238. return constraints[id];
  6239. }
  6240. /*
  6241. ================
  6242. idPhysics_AF::DeleteBody
  6243. ================
  6244. */
  6245. void idPhysics_AF::DeleteBody( const char *bodyName ) {
  6246. int i;
  6247. // find the body with the given name
  6248. for ( i = 0; i < bodies.Num(); i++ ) {
  6249. if ( !bodies[i]->name.Icmp( bodyName ) ) {
  6250. break;
  6251. }
  6252. }
  6253. if ( i >= bodies.Num() ) {
  6254. gameLocal.Warning( "DeleteBody: no body found in the articulated figure with the name '%s' for entity '%s' type '%s'.",
  6255. bodyName, self->name.c_str(), self->GetType()->classname );
  6256. return;
  6257. }
  6258. DeleteBody( i );
  6259. }
  6260. /*
  6261. ================
  6262. idPhysics_AF::DeleteBody
  6263. ================
  6264. */
  6265. void idPhysics_AF::DeleteBody( const int id ) {
  6266. int j;
  6267. if ( id < 0 || id > bodies.Num() ) {
  6268. gameLocal.Error( "DeleteBody: no body with id %d.", id );
  6269. return;
  6270. }
  6271. // remove any constraints attached to this body
  6272. for ( j = 0; j < constraints.Num(); j++ ) {
  6273. if ( constraints[j]->body1 == bodies[id] || constraints[j]->body2 == bodies[id] ) {
  6274. delete constraints[j];
  6275. constraints.RemoveIndex( j );
  6276. j--;
  6277. }
  6278. }
  6279. // remove the body
  6280. delete bodies[id];
  6281. bodies.RemoveIndex( id );
  6282. // set new body ids
  6283. for ( j = 0; j < bodies.Num(); j++ ) {
  6284. bodies[j]->clipModel->SetId( j );
  6285. }
  6286. changedAF = true;
  6287. }
  6288. /*
  6289. ================
  6290. idPhysics_AF::DeleteConstraint
  6291. ================
  6292. */
  6293. void idPhysics_AF::DeleteConstraint( const char *constraintName ) {
  6294. int i;
  6295. // find the constraint with the given name
  6296. for ( i = 0; i < constraints.Num(); i++ ) {
  6297. if ( !constraints[i]->name.Icmp( constraintName ) ) {
  6298. break;
  6299. }
  6300. }
  6301. if ( i >= constraints.Num() ) {
  6302. gameLocal.Warning( "DeleteConstraint: no constriant found in the articulated figure with the name '%s' for entity '%s' type '%s'.",
  6303. constraintName, self->name.c_str(), self->GetType()->classname );
  6304. return;
  6305. }
  6306. DeleteConstraint( i );
  6307. }
  6308. /*
  6309. ================
  6310. idPhysics_AF::DeleteConstraint
  6311. ================
  6312. */
  6313. void idPhysics_AF::DeleteConstraint( const int id ) {
  6314. if ( id < 0 || id >= constraints.Num() ) {
  6315. gameLocal.Error( "DeleteConstraint: no constraint with id %d.", id );
  6316. return;
  6317. }
  6318. // remove the constraint
  6319. delete constraints[id];
  6320. constraints.RemoveIndex( id );
  6321. changedAF = true;
  6322. }
  6323. /*
  6324. ================
  6325. idPhysics_AF::GetBodyContactConstraints
  6326. ================
  6327. */
  6328. int idPhysics_AF::GetBodyContactConstraints( const int id, idAFConstraint_Contact *contacts[], int maxContacts ) const {
  6329. int i, numContacts;
  6330. idAFBody *body;
  6331. idAFConstraint_Contact *contact;
  6332. if ( id < 0 || id >= bodies.Num() || maxContacts <= 0 ) {
  6333. return 0;
  6334. }
  6335. numContacts = 0;
  6336. body = bodies[id];
  6337. for ( i = 0; i < contactConstraints.Num(); i++ ) {
  6338. contact = contactConstraints[i];
  6339. if ( contact->body1 == body || contact->body2 == body ) {
  6340. contacts[numContacts++] = contact;
  6341. if ( numContacts >= maxContacts ) {
  6342. return numContacts;
  6343. }
  6344. }
  6345. }
  6346. return numContacts;
  6347. }
  6348. /*
  6349. ================
  6350. idPhysics_AF::SetDefaultFriction
  6351. ================
  6352. */
  6353. void idPhysics_AF::SetDefaultFriction( float linear, float angular, float contact ) {
  6354. if ( linear < 0.0f || linear > 1.0f ||
  6355. angular < 0.0f || angular > 1.0f ||
  6356. contact < 0.0f || contact > 1.0f ) {
  6357. return;
  6358. }
  6359. linearFriction = linear;
  6360. angularFriction = angular;
  6361. contactFriction = contact;
  6362. }
  6363. /*
  6364. ================
  6365. idPhysics_AF::GetImpactInfo
  6366. ================
  6367. */
  6368. void idPhysics_AF::GetImpactInfo( const int id, const idVec3 &point, impactInfo_t *info ) const {
  6369. if ( id < 0 || id >= bodies.Num() ) {
  6370. memset( info, 0, sizeof( *info ) );
  6371. return;
  6372. }
  6373. info->invMass = 1.0f / bodies[id]->mass;
  6374. info->invInertiaTensor = bodies[id]->current->worldAxis.Transpose() * bodies[id]->inverseInertiaTensor * bodies[id]->current->worldAxis;
  6375. info->position = point - bodies[id]->current->worldOrigin;
  6376. info->velocity = bodies[id]->current->spatialVelocity.SubVec3(0) + bodies[id]->current->spatialVelocity.SubVec3(1).Cross( info->position );
  6377. }
  6378. /*
  6379. ================
  6380. idPhysics_AF::ApplyImpulse
  6381. ================
  6382. */
  6383. void idPhysics_AF::ApplyImpulse( const int id, const idVec3 &point, const idVec3 &impulse ) {
  6384. if ( id < 0 || id >= bodies.Num() ) {
  6385. return;
  6386. }
  6387. if ( noImpact || impulse.LengthSqr() < Square( impulseThreshold ) ) {
  6388. return;
  6389. }
  6390. idMat3 invWorldInertiaTensor = bodies[id]->current->worldAxis.Transpose() * bodies[id]->inverseInertiaTensor * bodies[id]->current->worldAxis;
  6391. bodies[id]->current->spatialVelocity.SubVec3(0) += bodies[id]->invMass * impulse;
  6392. bodies[id]->current->spatialVelocity.SubVec3(1) += invWorldInertiaTensor * (point - bodies[id]->current->worldOrigin).Cross( impulse );
  6393. Activate();
  6394. }
  6395. /*
  6396. ================
  6397. idPhysics_AF::AddForce
  6398. ================
  6399. */
  6400. void idPhysics_AF::AddForce( const int id, const idVec3 &point, const idVec3 &force ) {
  6401. if ( noImpact ) {
  6402. return;
  6403. }
  6404. if ( id < 0 || id >= bodies.Num() ) {
  6405. return;
  6406. }
  6407. bodies[id]->current->externalForce.SubVec3( 0 ) += force;
  6408. bodies[id]->current->externalForce.SubVec3( 1 ) += (point - bodies[id]->current->worldOrigin).Cross( force );
  6409. Activate();
  6410. }
  6411. /*
  6412. ================
  6413. idPhysics_AF::IsAtRest
  6414. ================
  6415. */
  6416. bool idPhysics_AF::IsAtRest( void ) const {
  6417. return current.atRest >= 0;
  6418. }
  6419. /*
  6420. ================
  6421. idPhysics_AF::GetRestStartTime
  6422. ================
  6423. */
  6424. int idPhysics_AF::GetRestStartTime( void ) const {
  6425. return current.atRest;
  6426. }
  6427. /*
  6428. ================
  6429. idPhysics_AF::IsPushable
  6430. ================
  6431. */
  6432. bool idPhysics_AF::IsPushable( void ) const {
  6433. return ( !noImpact && ( masterBody == NULL || forcePushable ) );
  6434. }
  6435. /*
  6436. ================
  6437. idPhysics_AF::SaveState
  6438. ================
  6439. */
  6440. void idPhysics_AF::SaveState( void ) {
  6441. int i;
  6442. saved = current;
  6443. for ( i = 0; i < bodies.Num(); i++ ) {
  6444. memcpy( &bodies[i]->saved, bodies[i]->current, sizeof( AFBodyPState_t ) );
  6445. }
  6446. }
  6447. /*
  6448. ================
  6449. idPhysics_AF::RestoreState
  6450. ================
  6451. */
  6452. void idPhysics_AF::RestoreState( void ) {
  6453. int i;
  6454. current = saved;
  6455. for ( i = 0; i < bodies.Num(); i++ ) {
  6456. *(bodies[i]->current) = bodies[i]->saved;
  6457. }
  6458. EvaluateContacts();
  6459. }
  6460. /*
  6461. ================
  6462. idPhysics_AF::SetOrigin
  6463. ================
  6464. */
  6465. void idPhysics_AF::SetOrigin( const idVec3 &newOrigin, int id ) {
  6466. if ( masterBody ) {
  6467. Translate( masterBody->current->worldOrigin + masterBody->current->worldAxis * newOrigin - bodies[0]->current->worldOrigin );
  6468. } else {
  6469. Translate( newOrigin - bodies[0]->current->worldOrigin );
  6470. }
  6471. }
  6472. /*
  6473. ================
  6474. idPhysics_AF::SetAxis
  6475. ================
  6476. */
  6477. void idPhysics_AF::SetAxis( const idMat3 &newAxis, int id ) {
  6478. idMat3 axis;
  6479. idRotation rotation;
  6480. if ( masterBody ) {
  6481. axis = bodies[0]->current->worldAxis.Transpose() * ( newAxis * masterBody->current->worldAxis );
  6482. } else {
  6483. axis = bodies[0]->current->worldAxis.Transpose() * newAxis;
  6484. }
  6485. rotation = axis.ToRotation();
  6486. rotation.SetOrigin( bodies[0]->current->worldOrigin );
  6487. Rotate( rotation );
  6488. }
  6489. /*
  6490. ================
  6491. idPhysics_AF::Translate
  6492. ================
  6493. */
  6494. void idPhysics_AF::Translate( const idVec3 &translation, int id ) {
  6495. int i;
  6496. idAFBody *body;
  6497. if ( !worldConstraintsLocked ) {
  6498. // translate constraints attached to the world
  6499. for ( i = 0; i < constraints.Num(); i++ ) {
  6500. constraints[i]->Translate( translation );
  6501. }
  6502. }
  6503. // translate all the bodies
  6504. for ( i = 0; i < bodies.Num(); i++ ) {
  6505. body = bodies[i];
  6506. body->current->worldOrigin += translation;
  6507. }
  6508. Activate();
  6509. UpdateClipModels();
  6510. }
  6511. /*
  6512. ================
  6513. idPhysics_AF::Rotate
  6514. ================
  6515. */
  6516. void idPhysics_AF::Rotate( const idRotation &rotation, int id ) {
  6517. int i;
  6518. idAFBody *body;
  6519. if ( !worldConstraintsLocked ) {
  6520. // rotate constraints attached to the world
  6521. for ( i = 0; i < constraints.Num(); i++ ) {
  6522. constraints[i]->Rotate( rotation );
  6523. }
  6524. }
  6525. // rotate all the bodies
  6526. for ( i = 0; i < bodies.Num(); i++ ) {
  6527. body = bodies[i];
  6528. body->current->worldOrigin *= rotation;
  6529. body->current->worldAxis *= rotation.ToMat3();
  6530. }
  6531. Activate();
  6532. UpdateClipModels();
  6533. }
  6534. /*
  6535. ================
  6536. idPhysics_AF::GetOrigin
  6537. ================
  6538. */
  6539. const idVec3 &idPhysics_AF::GetOrigin( int id ) const {
  6540. if ( id < 0 || id >= bodies.Num() ) {
  6541. return vec3_origin;
  6542. }
  6543. else {
  6544. return bodies[id]->current->worldOrigin;
  6545. }
  6546. }
  6547. /*
  6548. ================
  6549. idPhysics_AF::GetAxis
  6550. ================
  6551. */
  6552. const idMat3 &idPhysics_AF::GetAxis( int id ) const {
  6553. if ( id < 0 || id >= bodies.Num() ) {
  6554. return mat3_identity;
  6555. }
  6556. else {
  6557. return bodies[id]->current->worldAxis;
  6558. }
  6559. }
  6560. /*
  6561. ================
  6562. idPhysics_AF::SetLinearVelocity
  6563. ================
  6564. */
  6565. void idPhysics_AF::SetLinearVelocity( const idVec3 &newLinearVelocity, int id ) {
  6566. if ( id < 0 || id >= bodies.Num() ) {
  6567. return;
  6568. }
  6569. bodies[id]->current->spatialVelocity.SubVec3( 0 ) = newLinearVelocity;
  6570. Activate();
  6571. }
  6572. /*
  6573. ================
  6574. idPhysics_AF::SetAngularVelocity
  6575. ================
  6576. */
  6577. void idPhysics_AF::SetAngularVelocity( const idVec3 &newAngularVelocity, int id ) {
  6578. if ( id < 0 || id >= bodies.Num() ) {
  6579. return;
  6580. }
  6581. bodies[id]->current->spatialVelocity.SubVec3( 1 ) = newAngularVelocity;
  6582. Activate();
  6583. }
  6584. /*
  6585. ================
  6586. idPhysics_AF::GetLinearVelocity
  6587. ================
  6588. */
  6589. const idVec3 &idPhysics_AF::GetLinearVelocity( int id ) const {
  6590. if ( id < 0 || id >= bodies.Num() ) {
  6591. return vec3_origin;
  6592. }
  6593. else {
  6594. return bodies[id]->current->spatialVelocity.SubVec3( 0 );
  6595. }
  6596. }
  6597. /*
  6598. ================
  6599. idPhysics_AF::GetAngularVelocity
  6600. ================
  6601. */
  6602. const idVec3 &idPhysics_AF::GetAngularVelocity( int id ) const {
  6603. if ( id < 0 || id >= bodies.Num() ) {
  6604. return vec3_origin;
  6605. }
  6606. else {
  6607. return bodies[id]->current->spatialVelocity.SubVec3( 1 );
  6608. }
  6609. }
  6610. /*
  6611. ================
  6612. idPhysics_AF::ClipTranslation
  6613. ================
  6614. */
  6615. void idPhysics_AF::ClipTranslation( trace_t &results, const idVec3 &translation, const idClipModel *model ) const {
  6616. int i;
  6617. idAFBody *body;
  6618. trace_t bodyResults;
  6619. results.fraction = 1.0f;
  6620. for ( i = 0; i < bodies.Num(); i++ ) {
  6621. body = bodies[i];
  6622. if ( body->clipModel->IsTraceModel() ) {
  6623. if ( model ) {
  6624. gameLocal.clip.TranslationModel( bodyResults, body->current->worldOrigin, body->current->worldOrigin + translation,
  6625. body->clipModel, body->current->worldAxis, body->clipMask,
  6626. model->Handle(), model->GetOrigin(), model->GetAxis() );
  6627. }
  6628. else {
  6629. gameLocal.clip.Translation( bodyResults, body->current->worldOrigin, body->current->worldOrigin + translation,
  6630. body->clipModel, body->current->worldAxis, body->clipMask, self );
  6631. }
  6632. if ( bodyResults.fraction < results.fraction ) {
  6633. results = bodyResults;
  6634. }
  6635. }
  6636. }
  6637. results.endpos = bodies[0]->current->worldOrigin + results.fraction * translation;
  6638. results.endAxis = bodies[0]->current->worldAxis;
  6639. }
  6640. /*
  6641. ================
  6642. idPhysics_AF::ClipRotation
  6643. ================
  6644. */
  6645. void idPhysics_AF::ClipRotation( trace_t &results, const idRotation &rotation, const idClipModel *model ) const {
  6646. int i;
  6647. idAFBody *body;
  6648. trace_t bodyResults;
  6649. idRotation partialRotation;
  6650. results.fraction = 1.0f;
  6651. for ( i = 0; i < bodies.Num(); i++ ) {
  6652. body = bodies[i];
  6653. if ( body->clipModel->IsTraceModel() ) {
  6654. if ( model ) {
  6655. gameLocal.clip.RotationModel( bodyResults, body->current->worldOrigin, rotation,
  6656. body->clipModel, body->current->worldAxis, body->clipMask,
  6657. model->Handle(), model->GetOrigin(), model->GetAxis() );
  6658. }
  6659. else {
  6660. gameLocal.clip.Rotation( bodyResults, body->current->worldOrigin, rotation,
  6661. body->clipModel, body->current->worldAxis, body->clipMask, self );
  6662. }
  6663. if ( bodyResults.fraction < results.fraction ) {
  6664. results = bodyResults;
  6665. }
  6666. }
  6667. }
  6668. partialRotation = rotation * results.fraction;
  6669. results.endpos = bodies[0]->current->worldOrigin * partialRotation;
  6670. results.endAxis = bodies[0]->current->worldAxis * partialRotation.ToMat3();
  6671. }
  6672. /*
  6673. ================
  6674. idPhysics_AF::ClipContents
  6675. ================
  6676. */
  6677. int idPhysics_AF::ClipContents( const idClipModel *model ) const {
  6678. int i, contents;
  6679. idAFBody *body;
  6680. contents = 0;
  6681. for ( i = 0; i < bodies.Num(); i++ ) {
  6682. body = bodies[i];
  6683. if ( body->clipModel->IsTraceModel() ) {
  6684. if ( model ) {
  6685. contents |= gameLocal.clip.ContentsModel( body->current->worldOrigin,
  6686. body->clipModel, body->current->worldAxis, -1,
  6687. model->Handle(), model->GetOrigin(), model->GetAxis() );
  6688. }
  6689. else {
  6690. contents |= gameLocal.clip.Contents( body->current->worldOrigin,
  6691. body->clipModel, body->current->worldAxis, -1, NULL );
  6692. }
  6693. }
  6694. }
  6695. return contents;
  6696. }
  6697. /*
  6698. ================
  6699. idPhysics_AF::DisableClip
  6700. ================
  6701. */
  6702. void idPhysics_AF::DisableClip( void ) {
  6703. int i;
  6704. for ( i = 0; i < bodies.Num(); i++ ) {
  6705. bodies[i]->clipModel->Disable();
  6706. }
  6707. }
  6708. /*
  6709. ================
  6710. idPhysics_AF::EnableClip
  6711. ================
  6712. */
  6713. void idPhysics_AF::EnableClip( void ) {
  6714. int i;
  6715. for ( i = 0; i < bodies.Num(); i++ ) {
  6716. bodies[i]->clipModel->Enable();
  6717. }
  6718. }
  6719. /*
  6720. ================
  6721. idPhysics_AF::UnlinkClip
  6722. ================
  6723. */
  6724. void idPhysics_AF::UnlinkClip( void ) {
  6725. int i;
  6726. for ( i = 0; i < bodies.Num(); i++ ) {
  6727. bodies[i]->clipModel->Unlink();
  6728. }
  6729. }
  6730. /*
  6731. ================
  6732. idPhysics_AF::LinkClip
  6733. ================
  6734. */
  6735. void idPhysics_AF::LinkClip( void ) {
  6736. UpdateClipModels();
  6737. }
  6738. /*
  6739. ================
  6740. idPhysics_AF::SetPushed
  6741. ================
  6742. */
  6743. void idPhysics_AF::SetPushed( int deltaTime ) {
  6744. idAFBody *body;
  6745. idRotation rotation;
  6746. if ( bodies.Num() ) {
  6747. body = bodies[0];
  6748. rotation = ( body->saved.worldAxis.Transpose() * body->current->worldAxis ).ToRotation();
  6749. // velocity with which the af is pushed
  6750. current.pushVelocity.SubVec3(0) += ( body->current->worldOrigin - body->saved.worldOrigin ) / ( deltaTime * idMath::M_MS2SEC );
  6751. current.pushVelocity.SubVec3(1) += rotation.GetVec() * -DEG2RAD( rotation.GetAngle() ) / ( deltaTime * idMath::M_MS2SEC );
  6752. }
  6753. }
  6754. /*
  6755. ================
  6756. idPhysics_AF::GetPushedLinearVelocity
  6757. ================
  6758. */
  6759. const idVec3 &idPhysics_AF::GetPushedLinearVelocity( const int id ) const {
  6760. return current.pushVelocity.SubVec3(0);
  6761. }
  6762. /*
  6763. ================
  6764. idPhysics_AF::GetPushedAngularVelocity
  6765. ================
  6766. */
  6767. const idVec3 &idPhysics_AF::GetPushedAngularVelocity( const int id ) const {
  6768. return current.pushVelocity.SubVec3(1);
  6769. }
  6770. /*
  6771. ================
  6772. idPhysics_AF::SetMaster
  6773. the binding is orientated based on the constraints being used
  6774. ================
  6775. */
  6776. void idPhysics_AF::SetMaster( idEntity *master, const bool orientated ) {
  6777. int i;
  6778. idVec3 masterOrigin;
  6779. idMat3 masterAxis;
  6780. idRotation rotation;
  6781. if ( master ) {
  6782. self->GetMasterPosition( masterOrigin, masterAxis );
  6783. if ( !masterBody ) {
  6784. masterBody = new idAFBody();
  6785. // translate and rotate all the constraints with body2 == NULL from world space to master space
  6786. rotation = masterAxis.Transpose().ToRotation();
  6787. for ( i = 0; i < constraints.Num(); i++ ) {
  6788. if ( constraints[i]->GetBody2() == NULL ) {
  6789. constraints[i]->Translate( -masterOrigin );
  6790. constraints[i]->Rotate( rotation );
  6791. }
  6792. }
  6793. Activate();
  6794. }
  6795. masterBody->current->worldOrigin = masterOrigin;
  6796. masterBody->current->worldAxis = masterAxis;
  6797. }
  6798. else {
  6799. if ( masterBody ) {
  6800. // translate and rotate all the constraints with body2 == NULL from master space to world space
  6801. rotation = masterBody->current->worldAxis.ToRotation();
  6802. for ( i = 0; i < constraints.Num(); i++ ) {
  6803. if ( constraints[i]->GetBody2() == NULL ) {
  6804. constraints[i]->Rotate( rotation );
  6805. constraints[i]->Translate( masterBody->current->worldOrigin );
  6806. }
  6807. }
  6808. delete masterBody;
  6809. masterBody = NULL;
  6810. Activate();
  6811. }
  6812. }
  6813. }
  6814. const float AF_VELOCITY_MAX = 16000;
  6815. const int AF_VELOCITY_TOTAL_BITS = 16;
  6816. const int AF_VELOCITY_EXPONENT_BITS = idMath::BitsForInteger( idMath::BitsForFloat( AF_VELOCITY_MAX ) ) + 1;
  6817. const int AF_VELOCITY_MANTISSA_BITS = AF_VELOCITY_TOTAL_BITS - 1 - AF_VELOCITY_EXPONENT_BITS;
  6818. const float AF_FORCE_MAX = 1e20f;
  6819. const int AF_FORCE_TOTAL_BITS = 16;
  6820. const int AF_FORCE_EXPONENT_BITS = idMath::BitsForInteger( idMath::BitsForFloat( AF_FORCE_MAX ) ) + 1;
  6821. const int AF_FORCE_MANTISSA_BITS = AF_FORCE_TOTAL_BITS - 1 - AF_FORCE_EXPONENT_BITS;
  6822. /*
  6823. ================
  6824. idPhysics_AF::WriteToSnapshot
  6825. ================
  6826. */
  6827. void idPhysics_AF::WriteToSnapshot( idBitMsgDelta &msg ) const {
  6828. int i;
  6829. idCQuat quat;
  6830. msg.WriteLong( current.atRest );
  6831. msg.WriteFloat( current.noMoveTime );
  6832. msg.WriteFloat( current.activateTime );
  6833. msg.WriteDeltaFloat( 0.0f, current.pushVelocity[0], AF_VELOCITY_EXPONENT_BITS, AF_VELOCITY_MANTISSA_BITS );
  6834. msg.WriteDeltaFloat( 0.0f, current.pushVelocity[1], AF_VELOCITY_EXPONENT_BITS, AF_VELOCITY_MANTISSA_BITS );
  6835. msg.WriteDeltaFloat( 0.0f, current.pushVelocity[2], AF_VELOCITY_EXPONENT_BITS, AF_VELOCITY_MANTISSA_BITS );
  6836. msg.WriteDeltaFloat( 0.0f, current.pushVelocity[3], AF_VELOCITY_EXPONENT_BITS, AF_VELOCITY_MANTISSA_BITS );
  6837. msg.WriteDeltaFloat( 0.0f, current.pushVelocity[4], AF_VELOCITY_EXPONENT_BITS, AF_VELOCITY_MANTISSA_BITS );
  6838. msg.WriteDeltaFloat( 0.0f, current.pushVelocity[5], AF_VELOCITY_EXPONENT_BITS, AF_VELOCITY_MANTISSA_BITS );
  6839. msg.WriteByte( bodies.Num() );
  6840. for ( i = 0; i < bodies.Num(); i++ ) {
  6841. AFBodyPState_t *state = bodies[i]->current;
  6842. quat = state->worldAxis.ToCQuat();
  6843. msg.WriteFloat( state->worldOrigin[0] );
  6844. msg.WriteFloat( state->worldOrigin[1] );
  6845. msg.WriteFloat( state->worldOrigin[2] );
  6846. msg.WriteFloat( quat.x );
  6847. msg.WriteFloat( quat.y );
  6848. msg.WriteFloat( quat.z );
  6849. msg.WriteDeltaFloat( 0.0f, state->spatialVelocity[0], AF_VELOCITY_EXPONENT_BITS, AF_VELOCITY_MANTISSA_BITS );
  6850. msg.WriteDeltaFloat( 0.0f, state->spatialVelocity[1], AF_VELOCITY_EXPONENT_BITS, AF_VELOCITY_MANTISSA_BITS );
  6851. msg.WriteDeltaFloat( 0.0f, state->spatialVelocity[2], AF_VELOCITY_EXPONENT_BITS, AF_VELOCITY_MANTISSA_BITS );
  6852. msg.WriteDeltaFloat( 0.0f, state->spatialVelocity[3], AF_VELOCITY_EXPONENT_BITS, AF_VELOCITY_MANTISSA_BITS );
  6853. msg.WriteDeltaFloat( 0.0f, state->spatialVelocity[4], AF_VELOCITY_EXPONENT_BITS, AF_VELOCITY_MANTISSA_BITS );
  6854. msg.WriteDeltaFloat( 0.0f, state->spatialVelocity[5], AF_VELOCITY_EXPONENT_BITS, AF_VELOCITY_MANTISSA_BITS );
  6855. /* msg.WriteDeltaFloat( 0.0f, state->externalForce[0], AF_FORCE_EXPONENT_BITS, AF_FORCE_MANTISSA_BITS );
  6856. msg.WriteDeltaFloat( 0.0f, state->externalForce[1], AF_FORCE_EXPONENT_BITS, AF_FORCE_MANTISSA_BITS );
  6857. msg.WriteDeltaFloat( 0.0f, state->externalForce[2], AF_FORCE_EXPONENT_BITS, AF_FORCE_MANTISSA_BITS );
  6858. msg.WriteDeltaFloat( 0.0f, state->externalForce[3], AF_FORCE_EXPONENT_BITS, AF_FORCE_MANTISSA_BITS );
  6859. msg.WriteDeltaFloat( 0.0f, state->externalForce[4], AF_FORCE_EXPONENT_BITS, AF_FORCE_MANTISSA_BITS );
  6860. msg.WriteDeltaFloat( 0.0f, state->externalForce[5], AF_FORCE_EXPONENT_BITS, AF_FORCE_MANTISSA_BITS );
  6861. */
  6862. }
  6863. }
  6864. /*
  6865. ================
  6866. idPhysics_AF::ReadFromSnapshot
  6867. ================
  6868. */
  6869. void idPhysics_AF::ReadFromSnapshot( const idBitMsgDelta &msg ) {
  6870. int i, num;
  6871. idCQuat quat;
  6872. current.atRest = msg.ReadLong();
  6873. current.noMoveTime = msg.ReadFloat();
  6874. current.activateTime = msg.ReadFloat();
  6875. current.pushVelocity[0] = msg.ReadDeltaFloat( 0.0f, AF_VELOCITY_EXPONENT_BITS, AF_VELOCITY_MANTISSA_BITS );
  6876. current.pushVelocity[1] = msg.ReadDeltaFloat( 0.0f, AF_VELOCITY_EXPONENT_BITS, AF_VELOCITY_MANTISSA_BITS );
  6877. current.pushVelocity[2] = msg.ReadDeltaFloat( 0.0f, AF_VELOCITY_EXPONENT_BITS, AF_VELOCITY_MANTISSA_BITS );
  6878. current.pushVelocity[3] = msg.ReadDeltaFloat( 0.0f, AF_VELOCITY_EXPONENT_BITS, AF_VELOCITY_MANTISSA_BITS );
  6879. current.pushVelocity[4] = msg.ReadDeltaFloat( 0.0f, AF_VELOCITY_EXPONENT_BITS, AF_VELOCITY_MANTISSA_BITS );
  6880. current.pushVelocity[5] = msg.ReadDeltaFloat( 0.0f, AF_VELOCITY_EXPONENT_BITS, AF_VELOCITY_MANTISSA_BITS );
  6881. num = msg.ReadByte();
  6882. assert( num == bodies.Num() );
  6883. for ( i = 0; i < bodies.Num(); i++ ) {
  6884. AFBodyPState_t *state = bodies[i]->current;
  6885. state->worldOrigin[0] = msg.ReadFloat();
  6886. state->worldOrigin[1] = msg.ReadFloat();
  6887. state->worldOrigin[2] = msg.ReadFloat();
  6888. quat.x = msg.ReadFloat();
  6889. quat.y = msg.ReadFloat();
  6890. quat.z = msg.ReadFloat();
  6891. state->spatialVelocity[0] = msg.ReadDeltaFloat( 0.0f, AF_VELOCITY_EXPONENT_BITS, AF_VELOCITY_MANTISSA_BITS );
  6892. state->spatialVelocity[1] = msg.ReadDeltaFloat( 0.0f, AF_VELOCITY_EXPONENT_BITS, AF_VELOCITY_MANTISSA_BITS );
  6893. state->spatialVelocity[2] = msg.ReadDeltaFloat( 0.0f, AF_VELOCITY_EXPONENT_BITS, AF_VELOCITY_MANTISSA_BITS );
  6894. state->spatialVelocity[3] = msg.ReadDeltaFloat( 0.0f, AF_VELOCITY_EXPONENT_BITS, AF_VELOCITY_MANTISSA_BITS );
  6895. state->spatialVelocity[4] = msg.ReadDeltaFloat( 0.0f, AF_VELOCITY_EXPONENT_BITS, AF_VELOCITY_MANTISSA_BITS );
  6896. state->spatialVelocity[5] = msg.ReadDeltaFloat( 0.0f, AF_VELOCITY_EXPONENT_BITS, AF_VELOCITY_MANTISSA_BITS );
  6897. /* state->externalForce[0] = msg.ReadDeltaFloat( 0.0f, AF_FORCE_EXPONENT_BITS, AF_FORCE_MANTISSA_BITS );
  6898. state->externalForce[1] = msg.ReadDeltaFloat( 0.0f, AF_FORCE_EXPONENT_BITS, AF_FORCE_MANTISSA_BITS );
  6899. state->externalForce[2] = msg.ReadDeltaFloat( 0.0f, AF_FORCE_EXPONENT_BITS, AF_FORCE_MANTISSA_BITS );
  6900. state->externalForce[3] = msg.ReadDeltaFloat( 0.0f, AF_FORCE_EXPONENT_BITS, AF_FORCE_MANTISSA_BITS );
  6901. state->externalForce[4] = msg.ReadDeltaFloat( 0.0f, AF_FORCE_EXPONENT_BITS, AF_FORCE_MANTISSA_BITS );
  6902. state->externalForce[5] = msg.ReadDeltaFloat( 0.0f, AF_FORCE_EXPONENT_BITS, AF_FORCE_MANTISSA_BITS );
  6903. */
  6904. state->worldAxis = quat.ToMat3();
  6905. }
  6906. UpdateClipModels();
  6907. }