Physics_AF.cpp 197 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008
  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. }
  2656. else {
  2657. c1[0] = 0.0f;
  2658. }
  2659. e[0] = CONTACT_LCP_EPSILON;
  2660. lo[0] = 0.0f;
  2661. hi[0] = idMath::INFINITY;
  2662. boxConstraint = NULL;
  2663. boxIndex[0] = -1;
  2664. }
  2665. /*
  2666. ================
  2667. idAFConstraint_Contact::Evaluate
  2668. ================
  2669. */
  2670. void idAFConstraint_Contact::Evaluate( float invTimeStep ) {
  2671. // do nothing
  2672. }
  2673. /*
  2674. ================
  2675. idAFConstraint_Contact::ApplyFriction
  2676. ================
  2677. */
  2678. void idAFConstraint_Contact::ApplyFriction( float invTimeStep ) {
  2679. idVec3 r, velocity, normal, dir1, dir2;
  2680. float friction, magnitude, forceNumerator, forceDenominator;
  2681. idVecX impulse, dv;
  2682. friction = body1->GetContactFriction();
  2683. if ( body2 && body2->GetContactFriction() < friction ) {
  2684. friction = body2->GetContactFriction();
  2685. }
  2686. friction *= physics->GetContactFrictionScale();
  2687. if ( friction <= 0.0f ) {
  2688. return;
  2689. }
  2690. // seperate friction per contact is silly but it's fast and often looks close enough
  2691. if ( af_useImpulseFriction.GetBool() ) {
  2692. impulse.SetData( 6, VECX_ALLOCA( 6 ) );
  2693. dv.SetData( 6, VECX_ALLOCA( 6 ) );
  2694. // calculate velocity in the contact plane
  2695. r = contact.point - body1->GetWorldOrigin();
  2696. velocity = body1->GetLinearVelocity() + body1->GetAngularVelocity().Cross( r );
  2697. velocity -= contact.normal * velocity * contact.normal;
  2698. // get normalized direction of friction and magnitude of velocity
  2699. normal = -velocity;
  2700. magnitude = normal.Normalize();
  2701. forceNumerator = friction * magnitude;
  2702. forceDenominator = body1->GetInverseMass() + ( ( body1->GetInverseWorldInertia() * r.Cross( normal ) ).Cross( r ) * normal );
  2703. impulse.SubVec3(0) = (forceNumerator / forceDenominator) * normal;
  2704. impulse.SubVec3(1) = r.Cross( impulse.SubVec3(0) );
  2705. body1->InverseWorldSpatialInertiaMultiply( dv, impulse.ToFloatPtr() );
  2706. // modify velocity with friction force
  2707. body1->SetLinearVelocity( body1->GetLinearVelocity() + dv.SubVec3(0) );
  2708. body1->SetAngularVelocity( body1->GetAngularVelocity() + dv.SubVec3(1) );
  2709. }
  2710. else {
  2711. if ( !fc ) {
  2712. fc = new idAFConstraint_ContactFriction;
  2713. }
  2714. // call setup each frame because contact constraints are re-used for different bodies
  2715. fc->Setup( this );
  2716. fc->Add( physics, invTimeStep );
  2717. }
  2718. }
  2719. /*
  2720. ================
  2721. idAFConstraint_Contact::Translate
  2722. ================
  2723. */
  2724. void idAFConstraint_Contact::Translate( const idVec3 &translation ) {
  2725. assert( 0 ); // contact should never be translated
  2726. }
  2727. /*
  2728. ================
  2729. idAFConstraint_Contact::Rotate
  2730. ================
  2731. */
  2732. void idAFConstraint_Contact::Rotate( const idRotation &rotation ) {
  2733. assert( 0 ); // contact should never be rotated
  2734. }
  2735. /*
  2736. ================
  2737. idAFConstraint_Contact::GetCenter
  2738. ================
  2739. */
  2740. void idAFConstraint_Contact::GetCenter( idVec3 &center ) {
  2741. center = contact.point;
  2742. }
  2743. /*
  2744. ================
  2745. idAFConstraint_Contact::DebugDraw
  2746. ================
  2747. */
  2748. void idAFConstraint_Contact::DebugDraw( void ) {
  2749. idVec3 x, y;
  2750. contact.normal.NormalVectors( x, y );
  2751. gameRenderWorld->DebugLine( colorWhite, contact.point, contact.point + 6.0f * contact.normal );
  2752. gameRenderWorld->DebugLine( colorWhite, contact.point - 2.0f * x, contact.point + 2.0f * x );
  2753. gameRenderWorld->DebugLine( colorWhite, contact.point - 2.0f * y, contact.point + 2.0f * y );
  2754. }
  2755. //===============================================================
  2756. //
  2757. // idAFConstraint_ContactFriction
  2758. //
  2759. //===============================================================
  2760. /*
  2761. ================
  2762. idAFConstraint_ContactFriction::idAFConstraint_ContactFriction
  2763. ================
  2764. */
  2765. idAFConstraint_ContactFriction::idAFConstraint_ContactFriction( void ) {
  2766. type = CONSTRAINT_FRICTION;
  2767. name = "contactFriction";
  2768. InitSize( 2 );
  2769. cc = NULL;
  2770. fl.allowPrimary = false;
  2771. fl.frameConstraint = true;
  2772. }
  2773. /*
  2774. ================
  2775. idAFConstraint_ContactFriction::Setup
  2776. ================
  2777. */
  2778. void idAFConstraint_ContactFriction::Setup( idAFConstraint_Contact *cc ) {
  2779. this->cc = cc;
  2780. body1 = cc->GetBody1();
  2781. body2 = cc->GetBody2();
  2782. }
  2783. /*
  2784. ================
  2785. idAFConstraint_ContactFriction::Evaluate
  2786. ================
  2787. */
  2788. void idAFConstraint_ContactFriction::Evaluate( float invTimeStep ) {
  2789. // do nothing
  2790. }
  2791. /*
  2792. ================
  2793. idAFConstraint_ContactFriction::ApplyFriction
  2794. ================
  2795. */
  2796. void idAFConstraint_ContactFriction::ApplyFriction( float invTimeStep ) {
  2797. // do nothing
  2798. }
  2799. /*
  2800. ================
  2801. idAFConstraint_ContactFriction::Add
  2802. ================
  2803. */
  2804. bool idAFConstraint_ContactFriction::Add( idPhysics_AF *phys, float invTimeStep ) {
  2805. idVec3 r, dir1, dir2;
  2806. float friction;
  2807. int newRow;
  2808. physics = phys;
  2809. friction = body1->GetContactFriction() * physics->GetContactFrictionScale();
  2810. // if the body only has friction in one direction
  2811. if ( body1->GetFrictionDirection( dir1 ) ) {
  2812. // project the friction direction into the contact plane
  2813. dir1 -= dir1 * cc->GetContact().normal * dir1;
  2814. dir1.Normalize();
  2815. r = cc->GetContact().point - body1->GetWorldOrigin();
  2816. J1.SetSize( 1, 6 );
  2817. J1.SubVec6(0).SubVec3(0) = dir1;
  2818. J1.SubVec6(0).SubVec3(1) = r.Cross( dir1 );
  2819. c1.SetSize( 1 );
  2820. c1[0] = 0.0f;
  2821. if ( body2 ) {
  2822. r = cc->GetContact().point - body2->GetWorldOrigin();
  2823. J2.SetSize( 1, 6 );
  2824. J2.SubVec6(0).SubVec3(0) = -dir1;
  2825. J2.SubVec6(0).SubVec3(1) = r.Cross( -dir1 );
  2826. c2.SetSize( 1 );
  2827. c2[0] = 0.0f;
  2828. }
  2829. lo[0] = -friction;
  2830. hi[0] = friction;
  2831. boxConstraint = cc;
  2832. boxIndex[0] = 0;
  2833. }
  2834. else {
  2835. // get two friction directions orthogonal to contact normal
  2836. cc->GetContact().normal.NormalVectors( dir1, dir2 );
  2837. r = cc->GetContact().point - body1->GetWorldOrigin();
  2838. J1.SetSize( 2, 6 );
  2839. J1.SubVec6(0).SubVec3(0) = dir1;
  2840. J1.SubVec6(0).SubVec3(1) = r.Cross( dir1 );
  2841. J1.SubVec6(1).SubVec3(0) = dir2;
  2842. J1.SubVec6(1).SubVec3(1) = r.Cross( dir2 );
  2843. c1.SetSize( 2 );
  2844. c1[0] = c1[1] = 0.0f;
  2845. if ( body2 ) {
  2846. r = cc->GetContact().point - body2->GetWorldOrigin();
  2847. J2.SetSize( 2, 6 );
  2848. J2.SubVec6(0).SubVec3(0) = -dir1;
  2849. J2.SubVec6(0).SubVec3(1) = r.Cross( -dir1 );
  2850. J2.SubVec6(1).SubVec3(0) = -dir2;
  2851. J2.SubVec6(1).SubVec3(1) = r.Cross( -dir2 );
  2852. c2.SetSize( 2 );
  2853. c2[0] = c2[1] = 0.0f;
  2854. if ( body2->GetContactFriction() < friction ) {
  2855. friction = body2->GetContactFriction();
  2856. }
  2857. }
  2858. lo[0] = -friction;
  2859. hi[0] = friction;
  2860. boxConstraint = cc;
  2861. boxIndex[0] = 0;
  2862. lo[1] = -friction;
  2863. hi[1] = friction;
  2864. boxIndex[1] = 0;
  2865. }
  2866. if ( body1->GetContactMotorDirection( dir1 ) && body1->GetContactMotorForce() > 0.0f ) {
  2867. // project the motor force direction into the contact plane
  2868. dir1 -= dir1 * cc->GetContact().normal * dir1;
  2869. dir1.Normalize();
  2870. r = cc->GetContact().point - body1->GetWorldOrigin();
  2871. newRow = J1.GetNumRows();
  2872. J1.ChangeSize( newRow+1, J1.GetNumColumns() );
  2873. J1.SubVec6(newRow).SubVec3(0) = -dir1;
  2874. J1.SubVec6(newRow).SubVec3(1) = r.Cross( -dir1 );
  2875. c1.ChangeSize( newRow+1 );
  2876. c1[newRow] = body1->GetContactMotorVelocity();
  2877. if ( body2 ) {
  2878. r = cc->GetContact().point - body2->GetWorldOrigin();
  2879. J2.ChangeSize( newRow+1, J2.GetNumColumns() );
  2880. J2.SubVec6(newRow).SubVec3(0) = -dir1;
  2881. J2.SubVec6(newRow).SubVec3(1) = r.Cross( -dir1 );
  2882. c2.ChangeSize( newRow+1 );
  2883. c2[newRow] = 0.0f;
  2884. }
  2885. lo[newRow] = -body1->GetContactMotorForce();
  2886. hi[newRow] = body1->GetContactMotorForce();
  2887. boxIndex[newRow] = -1;
  2888. }
  2889. physics->AddFrameConstraint( this );
  2890. return true;
  2891. }
  2892. /*
  2893. ================
  2894. idAFConstraint_ContactFriction::Translate
  2895. ================
  2896. */
  2897. void idAFConstraint_ContactFriction::Translate( const idVec3 &translation ) {
  2898. }
  2899. /*
  2900. ================
  2901. idAFConstraint_ContactFriction::Rotate
  2902. ================
  2903. */
  2904. void idAFConstraint_ContactFriction::Rotate( const idRotation &rotation ) {
  2905. }
  2906. /*
  2907. ================
  2908. idAFConstraint_ContactFriction::DebugDraw
  2909. ================
  2910. */
  2911. void idAFConstraint_ContactFriction::DebugDraw( void ) {
  2912. }
  2913. //===============================================================
  2914. //
  2915. // idAFConstraint_ConeLimit
  2916. //
  2917. //===============================================================
  2918. /*
  2919. ================
  2920. idAFConstraint_ConeLimit::idAFConstraint_ConeLimit
  2921. ================
  2922. */
  2923. idAFConstraint_ConeLimit::idAFConstraint_ConeLimit( void ) {
  2924. type = CONSTRAINT_CONELIMIT;
  2925. name = "coneLimit";
  2926. InitSize( 1 );
  2927. fl.allowPrimary = false;
  2928. fl.frameConstraint = true;
  2929. }
  2930. /*
  2931. ================
  2932. idAFConstraint_ConeLimit::Setup
  2933. the coneAnchor is the top of the cone in body2 space
  2934. the coneAxis is the axis of the cone in body2 space
  2935. the coneAngle is the angle the cone hull makes at the top
  2936. the body1Axis is the axis in body1 space that should stay within the cone
  2937. ================
  2938. */
  2939. void idAFConstraint_ConeLimit::Setup( idAFBody *b1, idAFBody *b2, const idVec3 &coneAnchor, const idVec3 &coneAxis, const float coneAngle, const idVec3 &body1Axis ) {
  2940. this->body1 = b1;
  2941. this->body2 = b2;
  2942. this->coneAxis = coneAxis;
  2943. this->coneAxis.Normalize();
  2944. this->coneAnchor = coneAnchor;
  2945. this->body1Axis = body1Axis;
  2946. this->body1Axis.Normalize();
  2947. this->cosAngle = (float) cos( DEG2RAD( coneAngle * 0.5f ) );
  2948. this->sinHalfAngle = (float) sin( DEG2RAD( coneAngle * 0.25f ) );
  2949. this->cosHalfAngle = (float) cos( DEG2RAD( coneAngle * 0.25f ) );
  2950. }
  2951. /*
  2952. ================
  2953. idAFConstraint_ConeLimit::SetAnchor
  2954. ================
  2955. */
  2956. void idAFConstraint_ConeLimit::SetAnchor( const idVec3 &coneAnchor ) {
  2957. this->coneAnchor = coneAnchor;
  2958. }
  2959. /*
  2960. ================
  2961. idAFConstraint_ConeLimit::SetBody1Axis
  2962. ================
  2963. */
  2964. void idAFConstraint_ConeLimit::SetBody1Axis( const idVec3 &body1Axis ) {
  2965. this->body1Axis = body1Axis;
  2966. }
  2967. /*
  2968. ================
  2969. idAFConstraint_ConeLimit::Evaluate
  2970. ================
  2971. */
  2972. void idAFConstraint_ConeLimit::Evaluate( float invTimeStep ) {
  2973. // do nothing
  2974. }
  2975. /*
  2976. ================
  2977. idAFConstraint_ConeLimit::ApplyFriction
  2978. ================
  2979. */
  2980. void idAFConstraint_ConeLimit::ApplyFriction( float invTimeStep ) {
  2981. }
  2982. /*
  2983. ================
  2984. idAFConstraint_ConeLimit::Add
  2985. ================
  2986. */
  2987. bool idAFConstraint_ConeLimit::Add( idPhysics_AF *phys, float invTimeStep ) {
  2988. float a;
  2989. idVec6 J1row, J2row;
  2990. idVec3 ax, anchor, body1ax, normal, coneVector, p1, p2;
  2991. idQuat q;
  2992. idAFBody *master;
  2993. if ( af_skipLimits.GetBool() ) {
  2994. lm.Zero(); // constraint exerts no force
  2995. return false;
  2996. }
  2997. physics = phys;
  2998. master = body2 ? body2 : physics->GetMasterBody();
  2999. if ( master ) {
  3000. ax = coneAxis * master->GetWorldAxis();
  3001. anchor = master->GetWorldOrigin() + coneAnchor * master->GetWorldAxis();
  3002. }
  3003. else {
  3004. ax = coneAxis;
  3005. anchor = coneAnchor;
  3006. }
  3007. body1ax = body1Axis * body1->GetWorldAxis();
  3008. a = ax * body1ax;
  3009. // if the body1 axis is inside the cone
  3010. if ( a > cosAngle ) {
  3011. lm.Zero(); // constraint exerts no force
  3012. return false;
  3013. }
  3014. // calculate the inward cone normal for the position the body1 axis went outside the cone
  3015. normal = body1ax.Cross( ax );
  3016. normal.Normalize();
  3017. q.x = normal.x * sinHalfAngle;
  3018. q.y = normal.y * sinHalfAngle;
  3019. q.z = normal.z * sinHalfAngle;
  3020. q.w = cosHalfAngle;
  3021. coneVector = ax * q.ToMat3();
  3022. normal = coneVector.Cross( ax ).Cross( coneVector );
  3023. normal.Normalize();
  3024. p1 = anchor + 32.0f * coneVector - body1->GetWorldOrigin();
  3025. J1row.SubVec3(0) = normal;
  3026. J1row.SubVec3(1) = p1.Cross( normal );
  3027. J1.Set( 1, 6, J1row.ToFloatPtr() );
  3028. c1[0] = (invTimeStep * LIMIT_ERROR_REDUCTION) * ( normal * (32.0f * body1ax) );
  3029. if ( body2 ) {
  3030. p2 = anchor + 32.0f * coneVector - master->GetWorldOrigin();
  3031. J2row.SubVec3(0) = -normal;
  3032. J2row.SubVec3(1) = p2.Cross( -normal );
  3033. J2.Set( 1, 6, J2row.ToFloatPtr() );
  3034. c2[0] = 0.0f;
  3035. }
  3036. lo[0] = 0.0f;
  3037. e[0] = LIMIT_LCP_EPSILON;
  3038. physics->AddFrameConstraint( this );
  3039. return true;
  3040. }
  3041. /*
  3042. ================
  3043. idAFConstraint_ConeLimit::Translate
  3044. ================
  3045. */
  3046. void idAFConstraint_ConeLimit::Translate( const idVec3 &translation ) {
  3047. if ( !body2 ) {
  3048. coneAnchor += translation;
  3049. }
  3050. }
  3051. /*
  3052. ================
  3053. idAFConstraint_ConeLimit::Rotate
  3054. ================
  3055. */
  3056. void idAFConstraint_ConeLimit::Rotate( const idRotation &rotation ) {
  3057. if ( !body2 ) {
  3058. coneAnchor *= rotation;
  3059. coneAxis *= rotation.ToMat3();
  3060. }
  3061. }
  3062. /*
  3063. ================
  3064. idAFConstraint_ConeLimit::DebugDraw
  3065. ================
  3066. */
  3067. void idAFConstraint_ConeLimit::DebugDraw( void ) {
  3068. idVec3 ax, anchor, x, y, z, start, end;
  3069. float sinAngle, a, size = 10.0f;
  3070. idAFBody *master;
  3071. master = body2 ? body2 : physics->GetMasterBody();
  3072. if ( master ) {
  3073. ax = coneAxis * master->GetWorldAxis();
  3074. anchor = master->GetWorldOrigin() + coneAnchor * master->GetWorldAxis();
  3075. }
  3076. else {
  3077. ax = coneAxis;
  3078. anchor = coneAnchor;
  3079. }
  3080. // draw body1 axis
  3081. gameRenderWorld->DebugLine( colorGreen, anchor, anchor + size * (body1Axis * body1->GetWorldAxis()) );
  3082. // draw cone
  3083. ax.NormalVectors( x, y );
  3084. sinAngle = idMath::Sqrt( 1.0f - cosAngle * cosAngle );
  3085. x *= size * sinAngle;
  3086. y *= size * sinAngle;
  3087. z = anchor + ax * size * cosAngle;
  3088. start = x + z;
  3089. for ( a = 0.0f; a < 360.0f; a += 45.0f ) {
  3090. end = x * (float) cos( DEG2RAD(a + 45.0f) ) + y * (float) sin( DEG2RAD(a + 45.0f) ) + z;
  3091. gameRenderWorld->DebugLine( colorMagenta, anchor, start );
  3092. gameRenderWorld->DebugLine( colorMagenta, start, end );
  3093. start = end;
  3094. }
  3095. }
  3096. /*
  3097. ================
  3098. idAFConstraint_ConeLimit::Save
  3099. ================
  3100. */
  3101. void idAFConstraint_ConeLimit::Save( idSaveGame *saveFile ) const {
  3102. idAFConstraint::Save( saveFile );
  3103. saveFile->WriteVec3( coneAnchor );
  3104. saveFile->WriteVec3( coneAxis );
  3105. saveFile->WriteVec3( body1Axis );
  3106. saveFile->WriteFloat( cosAngle );
  3107. saveFile->WriteFloat( sinHalfAngle );
  3108. saveFile->WriteFloat( cosHalfAngle );
  3109. saveFile->WriteFloat( epsilon );
  3110. }
  3111. /*
  3112. ================
  3113. idAFConstraint_ConeLimit::Restore
  3114. ================
  3115. */
  3116. void idAFConstraint_ConeLimit::Restore( idRestoreGame *saveFile ) {
  3117. idAFConstraint::Restore( saveFile );
  3118. saveFile->ReadVec3( coneAnchor );
  3119. saveFile->ReadVec3( coneAxis );
  3120. saveFile->ReadVec3( body1Axis );
  3121. saveFile->ReadFloat( cosAngle );
  3122. saveFile->ReadFloat( sinHalfAngle );
  3123. saveFile->ReadFloat( cosHalfAngle );
  3124. saveFile->ReadFloat( epsilon );
  3125. }
  3126. //===============================================================
  3127. //
  3128. // idAFConstraint_PyramidLimit
  3129. //
  3130. //===============================================================
  3131. /*
  3132. ================
  3133. idAFConstraint_PyramidLimit::idAFConstraint_PyramidLimit
  3134. ================
  3135. */
  3136. idAFConstraint_PyramidLimit::idAFConstraint_PyramidLimit( void ) {
  3137. type = CONSTRAINT_PYRAMIDLIMIT;
  3138. name = "pyramidLimit";
  3139. InitSize( 1 );
  3140. fl.allowPrimary = false;
  3141. fl.frameConstraint = true;
  3142. }
  3143. /*
  3144. ================
  3145. idAFConstraint_PyramidLimit::Setup
  3146. ================
  3147. */
  3148. void idAFConstraint_PyramidLimit::Setup( idAFBody *b1, idAFBody *b2, const idVec3 &pyramidAnchor,
  3149. const idVec3 &pyramidAxis, const idVec3 &baseAxis,
  3150. const float pyramidAngle1, const float pyramidAngle2, const idVec3 &body1Axis ) {
  3151. body1 = b1;
  3152. body2 = b2;
  3153. // setup the base and make sure the basis is orthonormal
  3154. pyramidBasis[2] = pyramidAxis;
  3155. pyramidBasis[2].Normalize();
  3156. pyramidBasis[0] = baseAxis;
  3157. pyramidBasis[0] -= pyramidBasis[2] * baseAxis * pyramidBasis[2];
  3158. pyramidBasis[0].Normalize();
  3159. pyramidBasis[1] = pyramidBasis[0].Cross( pyramidBasis[2] );
  3160. // pyramid top
  3161. this->pyramidAnchor = pyramidAnchor;
  3162. // angles
  3163. cosAngle[0] = (float) cos( DEG2RAD( pyramidAngle1 * 0.5f ) );
  3164. cosAngle[1] = (float) cos( DEG2RAD( pyramidAngle2 * 0.5f ) );
  3165. sinHalfAngle[0] = (float) sin( DEG2RAD( pyramidAngle1 * 0.25f ) );
  3166. sinHalfAngle[1] = (float) sin( DEG2RAD( pyramidAngle2 * 0.25f ) );
  3167. cosHalfAngle[0] = (float) cos( DEG2RAD( pyramidAngle1 * 0.25f ) );
  3168. cosHalfAngle[1] = (float) cos( DEG2RAD( pyramidAngle2 * 0.25f ) );
  3169. this->body1Axis = body1Axis;
  3170. }
  3171. /*
  3172. ================
  3173. idAFConstraint_PyramidLimit::SetAnchor
  3174. ================
  3175. */
  3176. void idAFConstraint_PyramidLimit::SetAnchor( const idVec3 &pyramidAnchor ) {
  3177. this->pyramidAnchor = pyramidAnchor;
  3178. }
  3179. /*
  3180. ================
  3181. idAFConstraint_PyramidLimit::SetBody1Axis
  3182. ================
  3183. */
  3184. void idAFConstraint_PyramidLimit::SetBody1Axis( const idVec3 &body1Axis ) {
  3185. this->body1Axis = body1Axis;
  3186. }
  3187. /*
  3188. ================
  3189. idAFConstraint_PyramidLimit::Evaluate
  3190. ================
  3191. */
  3192. void idAFConstraint_PyramidLimit::Evaluate( float invTimeStep ) {
  3193. // do nothing
  3194. }
  3195. /*
  3196. ================
  3197. idAFConstraint_PyramidLimit::ApplyFriction
  3198. ================
  3199. */
  3200. void idAFConstraint_PyramidLimit::ApplyFriction( float invTimeStep ) {
  3201. }
  3202. /*
  3203. ================
  3204. idAFConstraint_PyramidLimit::Add
  3205. ================
  3206. */
  3207. bool idAFConstraint_PyramidLimit::Add( idPhysics_AF *phys, float invTimeStep ) {
  3208. int i;
  3209. float a[2];
  3210. idVec6 J1row, J2row;
  3211. idMat3 worldBase;
  3212. idVec3 anchor, body1ax, ax[2], v, normal, pyramidVector, p1, p2;
  3213. idQuat q;
  3214. idAFBody *master;
  3215. if ( af_skipLimits.GetBool() ) {
  3216. lm.Zero(); // constraint exerts no force
  3217. return false;
  3218. }
  3219. physics = phys;
  3220. master = body2 ? body2 : physics->GetMasterBody();
  3221. if ( master ) {
  3222. worldBase[0] = pyramidBasis[0] * master->GetWorldAxis();
  3223. worldBase[1] = pyramidBasis[1] * master->GetWorldAxis();
  3224. worldBase[2] = pyramidBasis[2] * master->GetWorldAxis();
  3225. anchor = master->GetWorldOrigin() + pyramidAnchor * master->GetWorldAxis();
  3226. }
  3227. else {
  3228. worldBase = pyramidBasis;
  3229. anchor = pyramidAnchor;
  3230. }
  3231. body1ax = body1Axis * body1->GetWorldAxis();
  3232. for ( i = 0; i < 2; i++ ) {
  3233. ax[i] = body1ax - worldBase[!i] * body1ax * worldBase[!i];
  3234. ax[i].Normalize();
  3235. a[i] = worldBase[2] * ax[i];
  3236. }
  3237. // if the body1 axis is inside the pyramid
  3238. if ( a[0] > cosAngle[0] && a[1] > cosAngle[1] ) {
  3239. lm.Zero(); // constraint exerts no force
  3240. return false;
  3241. }
  3242. // calculate the inward pyramid normal for the position the body1 axis went outside the pyramid
  3243. pyramidVector = worldBase[2];
  3244. for ( i = 0; i < 2; i++ ) {
  3245. if ( a[i] <= cosAngle[i] ) {
  3246. v = ax[i].Cross( worldBase[2] );
  3247. v.Normalize();
  3248. q.x = v.x * sinHalfAngle[i];
  3249. q.y = v.y * sinHalfAngle[i];
  3250. q.z = v.z * sinHalfAngle[i];
  3251. q.w = cosHalfAngle[i];
  3252. pyramidVector *= q.ToMat3();
  3253. }
  3254. }
  3255. normal = pyramidVector.Cross( worldBase[2] ).Cross( pyramidVector );
  3256. normal.Normalize();
  3257. p1 = anchor + 32.0f * pyramidVector - body1->GetWorldOrigin();
  3258. J1row.SubVec3(0) = normal;
  3259. J1row.SubVec3(1) = p1.Cross( normal );
  3260. J1.Set( 1, 6, J1row.ToFloatPtr() );
  3261. c1[0] = (invTimeStep * LIMIT_ERROR_REDUCTION) * ( normal * (32.0f * body1ax) );
  3262. if ( body2 ) {
  3263. p2 = anchor + 32.0f * pyramidVector - master->GetWorldOrigin();
  3264. J2row.SubVec3(0) = -normal;
  3265. J2row.SubVec3(1) = p2.Cross( -normal );
  3266. J2.Set( 1, 6, J2row.ToFloatPtr() );
  3267. c2[0] = 0.0f;
  3268. }
  3269. lo[0] = 0.0f;
  3270. e[0] = LIMIT_LCP_EPSILON;
  3271. physics->AddFrameConstraint( this );
  3272. return true;
  3273. }
  3274. /*
  3275. ================
  3276. idAFConstraint_PyramidLimit::Translate
  3277. ================
  3278. */
  3279. void idAFConstraint_PyramidLimit::Translate( const idVec3 &translation ) {
  3280. if ( !body2 ) {
  3281. pyramidAnchor += translation;
  3282. }
  3283. }
  3284. /*
  3285. ================
  3286. idAFConstraint_PyramidLimit::Rotate
  3287. ================
  3288. */
  3289. void idAFConstraint_PyramidLimit::Rotate( const idRotation &rotation ) {
  3290. if ( !body2 ) {
  3291. pyramidAnchor *= rotation;
  3292. pyramidBasis[0] *= rotation.ToMat3();
  3293. pyramidBasis[1] *= rotation.ToMat3();
  3294. pyramidBasis[2] *= rotation.ToMat3();
  3295. }
  3296. }
  3297. /*
  3298. ================
  3299. idAFConstraint_PyramidLimit::DebugDraw
  3300. ================
  3301. */
  3302. void idAFConstraint_PyramidLimit::DebugDraw( void ) {
  3303. int i;
  3304. float size = 10.0f;
  3305. idVec3 anchor, dir, p[4];
  3306. idMat3 worldBase, m[2];
  3307. idQuat q;
  3308. idAFBody *master;
  3309. master = body2 ? body2 : physics->GetMasterBody();
  3310. if ( master ) {
  3311. worldBase[0] = pyramidBasis[0] * master->GetWorldAxis();
  3312. worldBase[1] = pyramidBasis[1] * master->GetWorldAxis();
  3313. worldBase[2] = pyramidBasis[2] * master->GetWorldAxis();
  3314. anchor = master->GetWorldOrigin() + pyramidAnchor * master->GetWorldAxis();
  3315. }
  3316. else {
  3317. worldBase = pyramidBasis;
  3318. anchor = pyramidAnchor;
  3319. }
  3320. // draw body1 axis
  3321. gameRenderWorld->DebugLine( colorGreen, anchor, anchor + size * (body1Axis * body1->GetWorldAxis()) );
  3322. // draw the pyramid
  3323. for ( i = 0; i < 2; i++ ) {
  3324. q.x = worldBase[!i].x * sinHalfAngle[i];
  3325. q.y = worldBase[!i].y * sinHalfAngle[i];
  3326. q.z = worldBase[!i].z * sinHalfAngle[i];
  3327. q.w = cosHalfAngle[i];
  3328. m[i] = q.ToMat3();
  3329. }
  3330. dir = worldBase[2] * size;
  3331. p[0] = anchor + m[0] * (m[1] * dir);
  3332. p[1] = anchor + m[0] * (m[1].Transpose() * dir);
  3333. p[2] = anchor + m[0].Transpose() * (m[1].Transpose() * dir);
  3334. p[3] = anchor + m[0].Transpose() * (m[1] * dir);
  3335. for ( i = 0; i < 4; i++ ) {
  3336. gameRenderWorld->DebugLine( colorMagenta, anchor, p[i] );
  3337. gameRenderWorld->DebugLine( colorMagenta, p[i], p[(i+1)&3] );
  3338. }
  3339. }
  3340. /*
  3341. ================
  3342. idAFConstraint_PyramidLimit::Save
  3343. ================
  3344. */
  3345. void idAFConstraint_PyramidLimit::Save( idSaveGame *saveFile ) const {
  3346. idAFConstraint::Save( saveFile );
  3347. saveFile->WriteVec3( pyramidAnchor );
  3348. saveFile->WriteMat3( pyramidBasis );
  3349. saveFile->WriteVec3( body1Axis );
  3350. saveFile->WriteFloat( cosAngle[0] );
  3351. saveFile->WriteFloat( cosAngle[1] );
  3352. saveFile->WriteFloat( sinHalfAngle[0] );
  3353. saveFile->WriteFloat( sinHalfAngle[1] );
  3354. saveFile->WriteFloat( cosHalfAngle[0] );
  3355. saveFile->WriteFloat( cosHalfAngle[1] );
  3356. saveFile->WriteFloat( epsilon );
  3357. }
  3358. /*
  3359. ================
  3360. idAFConstraint_PyramidLimit::Restore
  3361. ================
  3362. */
  3363. void idAFConstraint_PyramidLimit::Restore( idRestoreGame *saveFile ) {
  3364. idAFConstraint::Restore( saveFile );
  3365. saveFile->ReadVec3( pyramidAnchor );
  3366. saveFile->ReadMat3( pyramidBasis );
  3367. saveFile->ReadVec3( body1Axis );
  3368. saveFile->ReadFloat( cosAngle[0] );
  3369. saveFile->ReadFloat( cosAngle[1] );
  3370. saveFile->ReadFloat( sinHalfAngle[0] );
  3371. saveFile->ReadFloat( sinHalfAngle[1] );
  3372. saveFile->ReadFloat( cosHalfAngle[0] );
  3373. saveFile->ReadFloat( cosHalfAngle[1] );
  3374. saveFile->ReadFloat( epsilon );
  3375. }
  3376. //===============================================================
  3377. //
  3378. // idAFConstraint_Suspension
  3379. //
  3380. //===============================================================
  3381. /*
  3382. ================
  3383. idAFConstraint_Suspension::idAFConstraint_Suspension
  3384. ================
  3385. */
  3386. idAFConstraint_Suspension::idAFConstraint_Suspension( void ) {
  3387. type = CONSTRAINT_SUSPENSION;
  3388. name = "suspension";
  3389. InitSize( 3 );
  3390. fl.allowPrimary = false;
  3391. fl.frameConstraint = true;
  3392. localOrigin.Zero();
  3393. localAxis.Identity();
  3394. suspensionUp = 0.0f;
  3395. suspensionDown = 0.0f;
  3396. suspensionKCompress = 0.0f;
  3397. suspensionDamping = 0.0f;
  3398. steerAngle = 0.0f;
  3399. friction = 2.0f;
  3400. motorEnabled = false;
  3401. motorForce = 0.0f;
  3402. motorVelocity = 0.0f;
  3403. wheelModel = NULL;
  3404. memset( &trace, 0, sizeof( trace ) );
  3405. epsilon = LCP_EPSILON;
  3406. }
  3407. /*
  3408. ================
  3409. idAFConstraint_Suspension::Setup
  3410. ================
  3411. */
  3412. void idAFConstraint_Suspension::Setup( const char *name, idAFBody *body, const idVec3 &origin, const idMat3 &axis, idClipModel *clipModel ) {
  3413. this->name = name;
  3414. body1 = body;
  3415. body2 = NULL;
  3416. localOrigin = ( origin - body->GetWorldOrigin() ) * body->GetWorldAxis().Transpose();
  3417. localAxis = axis * body->GetWorldAxis().Transpose();
  3418. wheelModel = clipModel;
  3419. }
  3420. /*
  3421. ================
  3422. idAFConstraint_Suspension::SetSuspension
  3423. ================
  3424. */
  3425. void idAFConstraint_Suspension::SetSuspension( const float up, const float down, const float k, const float d, const float f ) {
  3426. suspensionUp = up;
  3427. suspensionDown = down;
  3428. suspensionKCompress = k;
  3429. suspensionDamping = d;
  3430. friction = f;
  3431. }
  3432. /*
  3433. ================
  3434. idAFConstraint_Suspension::GetWheelOrigin
  3435. ================
  3436. */
  3437. const idVec3 idAFConstraint_Suspension::GetWheelOrigin( void ) const {
  3438. return body1->GetWorldOrigin() + wheelOffset * body1->GetWorldAxis();
  3439. }
  3440. /*
  3441. ================
  3442. idAFConstraint_Suspension::Evaluate
  3443. ================
  3444. */
  3445. void idAFConstraint_Suspension::Evaluate( float invTimeStep ) {
  3446. float velocity, suspensionLength, springLength, compression, dampingForce, springForce;
  3447. idVec3 origin, start, end, vel1, vel2, springDir, r, frictionDir, motorDir;
  3448. idMat3 axis;
  3449. idRotation rotation;
  3450. axis = localAxis * body1->GetWorldAxis();
  3451. origin = body1->GetWorldOrigin() + localOrigin * body1->GetWorldAxis();
  3452. start = origin + suspensionUp * axis[2];
  3453. end = origin - suspensionDown * axis[2];
  3454. rotation.SetVec( axis[2] );
  3455. rotation.SetAngle( steerAngle );
  3456. axis *= rotation.ToMat3();
  3457. gameLocal.clip.Translation( trace, start, end, wheelModel, axis, MASK_SOLID, NULL );
  3458. wheelOffset = ( trace.endpos - body1->GetWorldOrigin() ) * body1->GetWorldAxis().Transpose();
  3459. if ( trace.fraction >= 1.0f ) {
  3460. J1.SetSize( 0, 6 );
  3461. if ( body2 ) {
  3462. J2.SetSize( 0, 6 );
  3463. }
  3464. return;
  3465. }
  3466. // calculate and add spring force
  3467. vel1 = body1->GetPointVelocity( start );
  3468. if ( body2 ) {
  3469. vel2 = body2->GetPointVelocity( trace.c.point );
  3470. } else {
  3471. vel2.Zero();
  3472. }
  3473. suspensionLength = suspensionUp + suspensionDown;
  3474. springDir = trace.endpos - start;
  3475. springLength = trace.fraction * suspensionLength;
  3476. dampingForce = suspensionDamping * idMath::Fabs( ( vel2 - vel1 ) * springDir ) / ( 1.0f + springLength * springLength );
  3477. compression = suspensionLength - springLength;
  3478. springForce = compression * compression * suspensionKCompress - dampingForce;
  3479. r = trace.c.point - body1->GetWorldOrigin();
  3480. J1.SetSize( 2, 6 );
  3481. J1.SubVec6(0).SubVec3(0) = trace.c.normal;
  3482. J1.SubVec6(0).SubVec3(1) = r.Cross( trace.c.normal );
  3483. c1.SetSize( 2 );
  3484. c1[0] = 0.0f;
  3485. velocity = J1.SubVec6(0).SubVec3(0) * body1->GetLinearVelocity() + J1.SubVec6(0).SubVec3(1) * body1->GetAngularVelocity();
  3486. if ( body2 ) {
  3487. r = trace.c.point - body2->GetWorldOrigin();
  3488. J2.SetSize( 2, 6 );
  3489. J2.SubVec6(0).SubVec3(0) = -trace.c.normal;
  3490. J2.SubVec6(0).SubVec3(1) = r.Cross( -trace.c.normal );
  3491. c2.SetSize( 2 );
  3492. c2[0] = 0.0f;
  3493. velocity += J2.SubVec6(0).SubVec3(0) * body2->GetLinearVelocity() + J2.SubVec6(0).SubVec3(1) * body2->GetAngularVelocity();
  3494. }
  3495. c1[0] = -compression; // + 0.5f * -velocity;
  3496. e[0] = 1e-4f;
  3497. lo[0] = 0.0f;
  3498. hi[0] = springForce;
  3499. boxConstraint = NULL;
  3500. boxIndex[0] = -1;
  3501. // project the friction direction into the contact plane
  3502. frictionDir = axis[1] - axis[1] * trace.c.normal * axis[1];
  3503. frictionDir.Normalize();
  3504. r = trace.c.point - body1->GetWorldOrigin();
  3505. J1.SubVec6(1).SubVec3(0) = frictionDir;
  3506. J1.SubVec6(1).SubVec3(1) = r.Cross( frictionDir );
  3507. c1[1] = 0.0f;
  3508. if ( body2 ) {
  3509. r = trace.c.point - body2->GetWorldOrigin();
  3510. J2.SubVec6(1).SubVec3(0) = -frictionDir;
  3511. J2.SubVec6(1).SubVec3(1) = r.Cross( -frictionDir );
  3512. c2[1] = 0.0f;
  3513. }
  3514. lo[1] = -friction * physics->GetContactFrictionScale();
  3515. hi[1] = friction * physics->GetContactFrictionScale();
  3516. boxConstraint = this;
  3517. boxIndex[1] = 0;
  3518. if ( motorEnabled ) {
  3519. // project the motor force direction into the contact plane
  3520. motorDir = axis[0] - axis[0] * trace.c.normal * axis[0];
  3521. motorDir.Normalize();
  3522. r = trace.c.point - body1->GetWorldOrigin();
  3523. J1.ChangeSize( 3, J1.GetNumColumns() );
  3524. J1.SubVec6(2).SubVec3(0) = -motorDir;
  3525. J1.SubVec6(2).SubVec3(1) = r.Cross( -motorDir );
  3526. c1.ChangeSize( 3 );
  3527. c1[2] = motorVelocity;
  3528. if ( body2 ) {
  3529. r = trace.c.point - body2->GetWorldOrigin();
  3530. J2.ChangeSize( 3, J2.GetNumColumns() );
  3531. J2.SubVec6(2).SubVec3(0) = -motorDir;
  3532. J2.SubVec6(2).SubVec3(1) = r.Cross( -motorDir );
  3533. c2.ChangeSize( 3 );
  3534. c2[2] = 0.0f;
  3535. }
  3536. lo[2] = -motorForce;
  3537. hi[2] = motorForce;
  3538. boxIndex[2] = -1;
  3539. }
  3540. }
  3541. /*
  3542. ================
  3543. idAFConstraint_Suspension::ApplyFriction
  3544. ================
  3545. */
  3546. void idAFConstraint_Suspension::ApplyFriction( float invTimeStep ) {
  3547. // do nothing
  3548. }
  3549. /*
  3550. ================
  3551. idAFConstraint_Suspension::Translate
  3552. ================
  3553. */
  3554. void idAFConstraint_Suspension::Translate( const idVec3 &translation ) {
  3555. }
  3556. /*
  3557. ================
  3558. idAFConstraint_Suspension::Rotate
  3559. ================
  3560. */
  3561. void idAFConstraint_Suspension::Rotate( const idRotation &rotation ) {
  3562. }
  3563. /*
  3564. ================
  3565. idAFConstraint_Suspension::DebugDraw
  3566. ================
  3567. */
  3568. void idAFConstraint_Suspension::DebugDraw( void ) {
  3569. idVec3 origin;
  3570. idMat3 axis;
  3571. idRotation rotation;
  3572. axis = localAxis * body1->GetWorldAxis();
  3573. rotation.SetVec( axis[2] );
  3574. rotation.SetAngle( steerAngle );
  3575. axis *= rotation.ToMat3();
  3576. if ( trace.fraction < 1.0f ) {
  3577. origin = trace.c.point;
  3578. gameRenderWorld->DebugLine( colorWhite, origin, origin + 6.0f * axis[2] );
  3579. gameRenderWorld->DebugLine( colorWhite, origin - 4.0f * axis[0], origin + 4.0f * axis[0] );
  3580. gameRenderWorld->DebugLine( colorWhite, origin - 2.0f * axis[1], origin + 2.0f * axis[1] );
  3581. }
  3582. }
  3583. //===============================================================
  3584. //
  3585. // idAFBody
  3586. //
  3587. //===============================================================
  3588. /*
  3589. ================
  3590. idAFBody::idAFBody
  3591. ================
  3592. */
  3593. idAFBody::idAFBody( void ) {
  3594. Init();
  3595. }
  3596. /*
  3597. ================
  3598. idAFBody::idAFBody
  3599. ================
  3600. */
  3601. idAFBody::idAFBody( const idStr &name, idClipModel *clipModel, float density ) {
  3602. assert( clipModel );
  3603. assert( clipModel->IsTraceModel() );
  3604. Init();
  3605. this->name = name;
  3606. this->clipModel = NULL;
  3607. SetClipModel( clipModel );
  3608. SetDensity( density );
  3609. current->worldOrigin = clipModel->GetOrigin();
  3610. current->worldAxis = clipModel->GetAxis();
  3611. *next = *current;
  3612. }
  3613. /*
  3614. ================
  3615. idAFBody::~idAFBody
  3616. ================
  3617. */
  3618. idAFBody::~idAFBody( void ) {
  3619. delete clipModel;
  3620. }
  3621. /*
  3622. ================
  3623. idAFBody::Init
  3624. ================
  3625. */
  3626. void idAFBody::Init( void ) {
  3627. name = "noname";
  3628. parent = NULL;
  3629. clipModel = NULL;
  3630. primaryConstraint = NULL;
  3631. tree = NULL;
  3632. linearFriction = -1.0f;
  3633. angularFriction = -1.0f;
  3634. contactFriction = -1.0f;
  3635. bouncyness = -1.0f;
  3636. clipMask = 0;
  3637. frictionDir = vec3_zero;
  3638. contactMotorDir = vec3_zero;
  3639. contactMotorVelocity = 0.0f;
  3640. contactMotorForce = 0.0f;
  3641. mass = 1.0f;
  3642. invMass = 1.0f;
  3643. centerOfMass = vec3_zero;
  3644. inertiaTensor = mat3_identity;
  3645. inverseInertiaTensor = mat3_identity;
  3646. current = &state[0];
  3647. next = &state[1];
  3648. current->worldOrigin = vec3_zero;
  3649. current->worldAxis = mat3_identity;
  3650. current->spatialVelocity = vec6_zero;
  3651. current->externalForce = vec6_zero;
  3652. *next = *current;
  3653. saved = *current;
  3654. atRestOrigin = vec3_zero;
  3655. atRestAxis = mat3_identity;
  3656. s.Zero( 6 );
  3657. totalForce.Zero( 6 );
  3658. auxForce.Zero( 6 );
  3659. acceleration.Zero( 6 );
  3660. response = NULL;
  3661. responseIndex = NULL;
  3662. numResponses = 0;
  3663. maxAuxiliaryIndex = 0;
  3664. maxSubTreeAuxiliaryIndex = 0;
  3665. memset( &fl, 0, sizeof( fl ) );
  3666. fl.selfCollision = true;
  3667. fl.isZero = true;
  3668. }
  3669. /*
  3670. ================
  3671. idAFBody::SetClipModel
  3672. ================
  3673. */
  3674. void idAFBody::SetClipModel( idClipModel *clipModel ) {
  3675. if ( this->clipModel && this->clipModel != clipModel ) {
  3676. delete this->clipModel;
  3677. }
  3678. this->clipModel = clipModel;
  3679. }
  3680. /*
  3681. ================
  3682. idAFBody::SetFriction
  3683. ================
  3684. */
  3685. void idAFBody::SetFriction( float linear, float angular, float contact ) {
  3686. if ( linear < 0.0f || linear > 1.0f ||
  3687. angular < 0.0f || angular > 1.0f ||
  3688. contact < 0.0f ) {
  3689. gameLocal.Warning( "idAFBody::SetFriction: friction out of range, linear = %.1f, angular = %.1f, contact = %.1f", linear, angular, contact );
  3690. return;
  3691. }
  3692. linearFriction = linear;
  3693. angularFriction = angular;
  3694. contactFriction = contact;
  3695. }
  3696. /*
  3697. ================
  3698. idAFBody::SetBouncyness
  3699. ================
  3700. */
  3701. void idAFBody::SetBouncyness( float bounce ) {
  3702. if ( bounce < 0.0f || bounce > 1.0f ) {
  3703. gameLocal.Warning( "idAFBody::SetBouncyness: bouncyness out of range, bounce = %.1f", bounce );
  3704. return;
  3705. }
  3706. bouncyness = bounce;
  3707. }
  3708. /*
  3709. ================
  3710. idAFBody::SetDensity
  3711. ================
  3712. */
  3713. void idAFBody::SetDensity( float density, const idMat3 &inertiaScale ) {
  3714. // get the body mass properties
  3715. clipModel->GetMassProperties( density, mass, centerOfMass, inertiaTensor );
  3716. // make sure we have a valid mass
  3717. if ( mass <= 0.0f || FLOAT_IS_NAN( mass ) ) {
  3718. gameLocal.Warning( "idAFBody::SetDensity: invalid mass for body '%s'", name.c_str() );
  3719. mass = 1.0f;
  3720. centerOfMass.Zero();
  3721. inertiaTensor.Identity();
  3722. }
  3723. // make sure the center of mass is at the body origin
  3724. if ( !centerOfMass.Compare( vec3_origin, CENTER_OF_MASS_EPSILON ) ) {
  3725. gameLocal.Warning( "idAFBody::SetDentity: center of mass not at origin for body '%s'", name.c_str() );
  3726. }
  3727. centerOfMass.Zero();
  3728. // calculate the inverse mass and inverse inertia tensor
  3729. invMass = 1.0f / mass;
  3730. if ( inertiaScale != mat3_identity ) {
  3731. inertiaTensor *= inertiaScale;
  3732. }
  3733. if ( inertiaTensor.IsDiagonal( 1e-3f ) ) {
  3734. inertiaTensor[0][1] = inertiaTensor[0][2] = 0.0f;
  3735. inertiaTensor[1][0] = inertiaTensor[1][2] = 0.0f;
  3736. inertiaTensor[2][0] = inertiaTensor[2][1] = 0.0f;
  3737. inverseInertiaTensor.Identity();
  3738. inverseInertiaTensor[0][0] = 1.0f / inertiaTensor[0][0];
  3739. inverseInertiaTensor[1][1] = 1.0f / inertiaTensor[1][1];
  3740. inverseInertiaTensor[2][2] = 1.0f / inertiaTensor[2][2];
  3741. }
  3742. else {
  3743. inverseInertiaTensor = inertiaTensor.Inverse();
  3744. }
  3745. }
  3746. /*
  3747. ================
  3748. idAFBody::SetFrictionDirection
  3749. ================
  3750. */
  3751. void idAFBody::SetFrictionDirection( const idVec3 &dir ) {
  3752. frictionDir = dir * current->worldAxis.Transpose();
  3753. fl.useFrictionDir = true;
  3754. }
  3755. /*
  3756. ================
  3757. idAFBody::GetFrictionDirection
  3758. ================
  3759. */
  3760. bool idAFBody::GetFrictionDirection( idVec3 &dir ) const {
  3761. if ( fl.useFrictionDir ) {
  3762. dir = frictionDir * current->worldAxis;
  3763. return true;
  3764. }
  3765. return false;
  3766. }
  3767. /*
  3768. ================
  3769. idAFBody::SetContactMotorDirection
  3770. ================
  3771. */
  3772. void idAFBody::SetContactMotorDirection( const idVec3 &dir ) {
  3773. contactMotorDir = dir * current->worldAxis.Transpose();
  3774. fl.useContactMotorDir = true;
  3775. }
  3776. /*
  3777. ================
  3778. idAFBody::GetContactMotorDirection
  3779. ================
  3780. */
  3781. bool idAFBody::GetContactMotorDirection( idVec3 &dir ) const {
  3782. if ( fl.useContactMotorDir ) {
  3783. dir = contactMotorDir * current->worldAxis;
  3784. return true;
  3785. }
  3786. return false;
  3787. }
  3788. /*
  3789. ================
  3790. idAFBody::GetPointVelocity
  3791. ================
  3792. */
  3793. idVec3 idAFBody::GetPointVelocity( const idVec3 &point ) const {
  3794. idVec3 r = point - current->worldOrigin;
  3795. return current->spatialVelocity.SubVec3(0) + current->spatialVelocity.SubVec3(1).Cross( r );
  3796. }
  3797. /*
  3798. ================
  3799. idAFBody::AddForce
  3800. ================
  3801. */
  3802. void idAFBody::AddForce( const idVec3 &point, const idVec3 &force ) {
  3803. current->externalForce.SubVec3(0) += force;
  3804. current->externalForce.SubVec3(1) += (point - current->worldOrigin).Cross( force );
  3805. }
  3806. /*
  3807. ================
  3808. idAFBody::InverseWorldSpatialInertiaMultiply
  3809. dst = this->inverseWorldSpatialInertia * v;
  3810. ================
  3811. */
  3812. ID_INLINE void idAFBody::InverseWorldSpatialInertiaMultiply( idVecX &dst, const float *v ) const {
  3813. const float *mPtr = inverseWorldSpatialInertia.ToFloatPtr();
  3814. const float *vPtr = v;
  3815. float *dstPtr = dst.ToFloatPtr();
  3816. if ( fl.spatialInertiaSparse ) {
  3817. dstPtr[0] = mPtr[0*6+0] * vPtr[0];
  3818. dstPtr[1] = mPtr[1*6+1] * vPtr[1];
  3819. dstPtr[2] = mPtr[2*6+2] * vPtr[2];
  3820. dstPtr[3] = mPtr[3*6+3] * vPtr[3] + mPtr[3*6+4] * vPtr[4] + mPtr[3*6+5] * vPtr[5];
  3821. dstPtr[4] = mPtr[4*6+3] * vPtr[3] + mPtr[4*6+4] * vPtr[4] + mPtr[4*6+5] * vPtr[5];
  3822. dstPtr[5] = mPtr[5*6+3] * vPtr[3] + mPtr[5*6+4] * vPtr[4] + mPtr[5*6+5] * vPtr[5];
  3823. } else {
  3824. gameLocal.Warning( "spatial inertia is not sparse for body %s", name.c_str() );
  3825. }
  3826. }
  3827. /*
  3828. ================
  3829. idAFBody::Save
  3830. ================
  3831. */
  3832. void idAFBody::Save( idSaveGame *saveFile ) {
  3833. saveFile->WriteFloat( linearFriction );
  3834. saveFile->WriteFloat( angularFriction );
  3835. saveFile->WriteFloat( contactFriction );
  3836. saveFile->WriteFloat( bouncyness );
  3837. saveFile->WriteInt( clipMask );
  3838. saveFile->WriteVec3( frictionDir );
  3839. saveFile->WriteVec3( contactMotorDir );
  3840. saveFile->WriteFloat( contactMotorVelocity );
  3841. saveFile->WriteFloat( contactMotorForce );
  3842. saveFile->WriteFloat( mass );
  3843. saveFile->WriteFloat( invMass );
  3844. saveFile->WriteVec3( centerOfMass );
  3845. saveFile->WriteMat3( inertiaTensor );
  3846. saveFile->WriteMat3( inverseInertiaTensor );
  3847. saveFile->WriteVec3( current->worldOrigin );
  3848. saveFile->WriteMat3( current->worldAxis );
  3849. saveFile->WriteVec6( current->spatialVelocity );
  3850. saveFile->WriteVec6( current->externalForce );
  3851. saveFile->WriteVec3( atRestOrigin );
  3852. saveFile->WriteMat3( atRestAxis );
  3853. }
  3854. /*
  3855. ================
  3856. idAFBody::Restore
  3857. ================
  3858. */
  3859. void idAFBody::Restore( idRestoreGame *saveFile ) {
  3860. saveFile->ReadFloat( linearFriction );
  3861. saveFile->ReadFloat( angularFriction );
  3862. saveFile->ReadFloat( contactFriction );
  3863. saveFile->ReadFloat( bouncyness );
  3864. saveFile->ReadInt( clipMask );
  3865. saveFile->ReadVec3( frictionDir );
  3866. saveFile->ReadVec3( contactMotorDir );
  3867. saveFile->ReadFloat( contactMotorVelocity );
  3868. saveFile->ReadFloat( contactMotorForce );
  3869. saveFile->ReadFloat( mass );
  3870. saveFile->ReadFloat( invMass );
  3871. saveFile->ReadVec3( centerOfMass );
  3872. saveFile->ReadMat3( inertiaTensor );
  3873. saveFile->ReadMat3( inverseInertiaTensor );
  3874. saveFile->ReadVec3( current->worldOrigin );
  3875. saveFile->ReadMat3( current->worldAxis );
  3876. saveFile->ReadVec6( current->spatialVelocity );
  3877. saveFile->ReadVec6( current->externalForce );
  3878. saveFile->ReadVec3( atRestOrigin );
  3879. saveFile->ReadMat3( atRestAxis );
  3880. }
  3881. //===============================================================
  3882. // M
  3883. // idAFTree MrE
  3884. // E
  3885. //===============================================================
  3886. /*
  3887. ================
  3888. idAFTree::Factor
  3889. factor matrix for the primary constraints in the tree
  3890. ================
  3891. */
  3892. void idAFTree::Factor( void ) const {
  3893. int i, j;
  3894. idAFBody *body;
  3895. idAFConstraint *child;
  3896. idMatX childI;
  3897. childI.SetData( 6, 6, MATX_ALLOCA( 6 * 6 ) );
  3898. // from the leaves up towards the root
  3899. for ( i = sortedBodies.Num() - 1; i >= 0; i-- ) {
  3900. body = sortedBodies[i];
  3901. if ( body->children.Num() ) {
  3902. for ( j = 0; j < body->children.Num(); j++ ) {
  3903. child = body->children[j]->primaryConstraint;
  3904. // child->I = - child->body1->J.Transpose() * child->body1->I * child->body1->J;
  3905. childI.SetSize( child->J1.GetNumRows(), child->J1.GetNumRows() );
  3906. child->body1->J.TransposeMultiply( child->body1->I ).Multiply( childI, child->body1->J );
  3907. childI.Negate();
  3908. child->invI = childI;
  3909. if ( !child->invI.InverseFastSelf() ) {
  3910. gameLocal.Warning( "idAFTree::Factor: couldn't invert %dx%d matrix for constraint '%s'",
  3911. child->invI.GetNumRows(), child->invI.GetNumColumns(), child->GetName().c_str() );
  3912. }
  3913. child->J = child->invI * child->J;
  3914. body->I -= child->J.TransposeMultiply( childI ) * child->J;
  3915. }
  3916. body->invI = body->I;
  3917. if ( !body->invI.InverseFastSelf() ) {
  3918. gameLocal.Warning( "idAFTree::Factor: couldn't invert %dx%d matrix for body %s",
  3919. child->invI.GetNumRows(), child->invI.GetNumColumns(), body->GetName().c_str() );
  3920. }
  3921. if ( body->primaryConstraint ) {
  3922. body->J = body->invI * body->J;
  3923. }
  3924. }
  3925. else if ( body->primaryConstraint ) {
  3926. body->J = body->inverseWorldSpatialInertia * body->J;
  3927. }
  3928. }
  3929. }
  3930. /*
  3931. ================
  3932. idAFTree::Solve
  3933. solve for primary constraints in the tree
  3934. ================
  3935. */
  3936. void idAFTree::Solve( int auxiliaryIndex ) const {
  3937. int i, j;
  3938. idAFBody *body, *child;
  3939. idAFConstraint *primaryConstraint;
  3940. // from the leaves up towards the root
  3941. for ( i = sortedBodies.Num() - 1; i >= 0; i-- ) {
  3942. body = sortedBodies[i];
  3943. for ( j = 0; j < body->children.Num(); j++ ) {
  3944. child = body->children[j];
  3945. primaryConstraint = child->primaryConstraint;
  3946. if ( !child->fl.isZero ) {
  3947. child->J.TransposeMultiplySub( primaryConstraint->s, child->s );
  3948. primaryConstraint->fl.isZero = false;
  3949. }
  3950. if ( !primaryConstraint->fl.isZero ) {
  3951. primaryConstraint->J.TransposeMultiplySub( body->s, primaryConstraint->s );
  3952. body->fl.isZero = false;
  3953. }
  3954. }
  3955. }
  3956. bool useSymmetry = af_useSymmetry.GetBool();
  3957. // from the root down towards the leaves
  3958. for ( i = 0; i < sortedBodies.Num(); i++ ) {
  3959. body = sortedBodies[i];
  3960. primaryConstraint = body->primaryConstraint;
  3961. if ( primaryConstraint ) {
  3962. if ( useSymmetry && body->parent->maxSubTreeAuxiliaryIndex < auxiliaryIndex ) {
  3963. continue;
  3964. }
  3965. if ( !primaryConstraint->fl.isZero ) {
  3966. primaryConstraint->s = primaryConstraint->invI * primaryConstraint->s;
  3967. }
  3968. primaryConstraint->J.MultiplySub( primaryConstraint->s, primaryConstraint->body2->s );
  3969. primaryConstraint->lm = primaryConstraint->s;
  3970. if ( useSymmetry && body->maxSubTreeAuxiliaryIndex < auxiliaryIndex ) {
  3971. continue;
  3972. }
  3973. if ( body->children.Num() ) {
  3974. if ( !body->fl.isZero ) {
  3975. body->s = body->invI * body->s;
  3976. }
  3977. body->J.MultiplySub( body->s, primaryConstraint->s );
  3978. }
  3979. } else if ( body->children.Num() ) {
  3980. body->s = body->invI * body->s;
  3981. }
  3982. }
  3983. }
  3984. /*
  3985. ================
  3986. idAFTree::Response
  3987. calculate body forces in the tree in response to a constraint force
  3988. ================
  3989. */
  3990. void idAFTree::Response( const idAFConstraint *constraint, int row, int auxiliaryIndex ) const {
  3991. int i, j;
  3992. idAFBody *body;
  3993. idAFConstraint *child, *primaryConstraint;
  3994. idVecX v;
  3995. // if a single body don't waste time because there aren't any primary constraints
  3996. if ( sortedBodies.Num() == 1 ) {
  3997. body = constraint->body1;
  3998. if ( body->tree == this ) {
  3999. body->GetResponseForce( body->numResponses ) = constraint->J1.SubVec6( row );
  4000. body->responseIndex[body->numResponses++] = auxiliaryIndex;
  4001. }
  4002. else {
  4003. body = constraint->body2;
  4004. body->GetResponseForce( body->numResponses ) = constraint->J2.SubVec6( row );
  4005. body->responseIndex[body->numResponses++] = auxiliaryIndex;
  4006. }
  4007. return;
  4008. }
  4009. v.SetData( 6, VECX_ALLOCA( 6 ) );
  4010. // initialize right hand side to zero
  4011. for ( i = 0; i < sortedBodies.Num(); i++ ) {
  4012. body = sortedBodies[i];
  4013. primaryConstraint = body->primaryConstraint;
  4014. if ( primaryConstraint ) {
  4015. primaryConstraint->s.Zero();
  4016. primaryConstraint->fl.isZero = true;
  4017. }
  4018. body->s.Zero();
  4019. body->fl.isZero = true;
  4020. body->GetResponseForce( body->numResponses ).Zero();
  4021. }
  4022. // set right hand side for first constrained body
  4023. body = constraint->body1;
  4024. if ( body->tree == this ) {
  4025. body->InverseWorldSpatialInertiaMultiply( v, constraint->J1[row] );
  4026. primaryConstraint = body->primaryConstraint;
  4027. if ( primaryConstraint ) {
  4028. primaryConstraint->J1.Multiply( primaryConstraint->s, v );
  4029. primaryConstraint->fl.isZero = false;
  4030. }
  4031. for ( i = 0; i < body->children.Num(); i++ ) {
  4032. child = body->children[i]->primaryConstraint;
  4033. child->J2.Multiply( child->s, v );
  4034. child->fl.isZero = false;
  4035. }
  4036. body->GetResponseForce( body->numResponses ) = constraint->J1.SubVec6( row );
  4037. }
  4038. // set right hand side for second constrained body
  4039. body = constraint->body2;
  4040. if ( body && body->tree == this ) {
  4041. body->InverseWorldSpatialInertiaMultiply( v, constraint->J2[row] );
  4042. primaryConstraint = body->primaryConstraint;
  4043. if ( primaryConstraint ) {
  4044. primaryConstraint->J1.MultiplyAdd( primaryConstraint->s, v );
  4045. primaryConstraint->fl.isZero = false;
  4046. }
  4047. for ( i = 0; i < body->children.Num(); i++ ) {
  4048. child = body->children[i]->primaryConstraint;
  4049. child->J2.MultiplyAdd( child->s, v );
  4050. child->fl.isZero = false;
  4051. }
  4052. body->GetResponseForce( body->numResponses ) = constraint->J2.SubVec6( row );
  4053. }
  4054. // solve for primary constraints
  4055. Solve( auxiliaryIndex );
  4056. bool useSymmetry = af_useSymmetry.GetBool();
  4057. // store body forces in response to the constraint force
  4058. idVecX force;
  4059. for ( i = 0; i < sortedBodies.Num(); i++ ) {
  4060. body = sortedBodies[i];
  4061. if ( useSymmetry && body->maxAuxiliaryIndex < auxiliaryIndex ) {
  4062. continue;
  4063. }
  4064. force.SetData( 6, body->response + body->numResponses * 8 );
  4065. // add forces of all primary constraints acting on this body
  4066. primaryConstraint = body->primaryConstraint;
  4067. if ( primaryConstraint ) {
  4068. primaryConstraint->J1.TransposeMultiplyAdd( force, primaryConstraint->lm );
  4069. }
  4070. for ( j = 0; j < body->children.Num(); j++ ) {
  4071. child = body->children[j]->primaryConstraint;
  4072. child->J2.TransposeMultiplyAdd( force, child->lm );
  4073. }
  4074. body->responseIndex[body->numResponses++] = auxiliaryIndex;
  4075. }
  4076. }
  4077. /*
  4078. ================
  4079. idAFTree::CalculateForces
  4080. calculate forces on the bodies in the tree
  4081. ================
  4082. */
  4083. void idAFTree::CalculateForces( float timeStep ) const {
  4084. int i, j;
  4085. float invStep;
  4086. idAFBody *body;
  4087. idAFConstraint *child, *c, *primaryConstraint;
  4088. // forces on bodies
  4089. for ( i = 0; i < sortedBodies.Num(); i++ ) {
  4090. body = sortedBodies[i];
  4091. body->totalForce.SubVec6(0) = body->current->externalForce + body->auxForce.SubVec6(0);
  4092. }
  4093. // if a single body don't waste time because there aren't any primary constraints
  4094. if ( sortedBodies.Num() == 1 ) {
  4095. return;
  4096. }
  4097. invStep = 1.0f / timeStep;
  4098. // initialize right hand side
  4099. for ( i = 0; i < sortedBodies.Num(); i++ ) {
  4100. body = sortedBodies[i];
  4101. body->InverseWorldSpatialInertiaMultiply( body->acceleration, body->totalForce.ToFloatPtr() );
  4102. body->acceleration.SubVec6(0) += body->current->spatialVelocity * invStep;
  4103. primaryConstraint = body->primaryConstraint;
  4104. if ( primaryConstraint ) {
  4105. // b = ( J * acc + c )
  4106. c = primaryConstraint;
  4107. c->s = c->J1 * c->body1->acceleration + c->J2 * c->body2->acceleration + invStep * ( c->c1 + c->c2 );
  4108. c->fl.isZero = false;
  4109. }
  4110. body->s.Zero();
  4111. body->fl.isZero = true;
  4112. }
  4113. // solve for primary constraints
  4114. Solve();
  4115. // calculate forces on bodies after applying primary constraints
  4116. for ( i = 0; i < sortedBodies.Num(); i++ ) {
  4117. body = sortedBodies[i];
  4118. // add forces of all primary constraints acting on this body
  4119. primaryConstraint = body->primaryConstraint;
  4120. if ( primaryConstraint ) {
  4121. primaryConstraint->J1.TransposeMultiplyAdd( body->totalForce, primaryConstraint->lm );
  4122. }
  4123. for ( j = 0; j < body->children.Num(); j++ ) {
  4124. child = body->children[j]->primaryConstraint;
  4125. child->J2.TransposeMultiplyAdd( body->totalForce, child->lm );
  4126. }
  4127. }
  4128. }
  4129. /*
  4130. ================
  4131. idAFTree::SetMaxSubTreeAuxiliaryIndex
  4132. ================
  4133. */
  4134. void idAFTree::SetMaxSubTreeAuxiliaryIndex( void ) {
  4135. int i, j;
  4136. idAFBody *body, *child;
  4137. // from the leaves up towards the root
  4138. for ( i = sortedBodies.Num() - 1; i >= 0; i-- ) {
  4139. body = sortedBodies[i];
  4140. body->maxSubTreeAuxiliaryIndex = body->maxAuxiliaryIndex;
  4141. for ( j = 0; j < body->children.Num(); j++ ) {
  4142. child = body->children[j];
  4143. if ( child->maxSubTreeAuxiliaryIndex > body->maxSubTreeAuxiliaryIndex ) {
  4144. body->maxSubTreeAuxiliaryIndex = child->maxSubTreeAuxiliaryIndex;
  4145. }
  4146. }
  4147. }
  4148. }
  4149. /*
  4150. ================
  4151. idAFTree::SortBodies_r
  4152. ================
  4153. */
  4154. void idAFTree::SortBodies_r( idList<idAFBody*>&sortedList, idAFBody *body ) {
  4155. int i;
  4156. for ( i = 0; i < body->children.Num(); i++ ) {
  4157. sortedList.Append( body->children[i] );
  4158. }
  4159. for ( i = 0; i < body->children.Num(); i++ ) {
  4160. SortBodies_r( sortedList, body->children[i] );
  4161. }
  4162. }
  4163. /*
  4164. ================
  4165. idAFTree::SortBodies
  4166. sort body list to make sure parents come first
  4167. ================
  4168. */
  4169. void idAFTree::SortBodies( void ) {
  4170. int i;
  4171. idAFBody *body;
  4172. // find the root
  4173. for ( i = 0; i < sortedBodies.Num(); i++ ) {
  4174. if ( !sortedBodies[i]->parent ) {
  4175. break;
  4176. }
  4177. }
  4178. if ( i >= sortedBodies.Num() ) {
  4179. gameLocal.Error( "Articulated figure tree has no root." );
  4180. }
  4181. body = sortedBodies[i];
  4182. sortedBodies.Clear();
  4183. sortedBodies.Append( body );
  4184. SortBodies_r( sortedBodies, body );
  4185. }
  4186. /*
  4187. ================
  4188. idAFTree::DebugDraw
  4189. ================
  4190. */
  4191. void idAFTree::DebugDraw( const idVec4 &color ) const {
  4192. int i;
  4193. idAFBody *body;
  4194. for ( i = 1; i < sortedBodies.Num(); i++ ) {
  4195. body = sortedBodies[i];
  4196. gameRenderWorld->DebugArrow( color, body->parent->current->worldOrigin, body->current->worldOrigin, 1 );
  4197. }
  4198. }
  4199. //===============================================================
  4200. // M
  4201. // idPhysics_AF MrE
  4202. // E
  4203. //===============================================================
  4204. /*
  4205. ================
  4206. idPhysics_AF::EvaluateConstraints
  4207. ================
  4208. */
  4209. void idPhysics_AF::EvaluateConstraints( float timeStep ) {
  4210. int i;
  4211. float invTimeStep;
  4212. idAFBody *body;
  4213. idAFConstraint *c;
  4214. invTimeStep = 1.0f / timeStep;
  4215. // setup the constraint equations for the current position and orientation of the bodies
  4216. for ( i = 0; i < primaryConstraints.Num(); i++ ) {
  4217. c = primaryConstraints[i];
  4218. c->Evaluate( invTimeStep );
  4219. c->J = c->J2;
  4220. }
  4221. for ( i = 0; i < auxiliaryConstraints.Num(); i++ ) {
  4222. auxiliaryConstraints[i]->Evaluate( invTimeStep );
  4223. }
  4224. // add contact constraints to the list with frame constraints
  4225. for ( i = 0; i < contactConstraints.Num(); i++ ) {
  4226. AddFrameConstraint( contactConstraints[i] );
  4227. }
  4228. // setup body primary constraint matrix
  4229. for ( i = 0; i < bodies.Num(); i++ ) {
  4230. body = bodies[i];
  4231. if ( body->primaryConstraint ) {
  4232. body->J = body->primaryConstraint->J1.Transpose();
  4233. }
  4234. }
  4235. }
  4236. /*
  4237. ================
  4238. idPhysics_AF::EvaluateBodies
  4239. ================
  4240. */
  4241. void idPhysics_AF::EvaluateBodies( float timeStep ) {
  4242. int i;
  4243. idAFBody *body;
  4244. idMat3 axis;
  4245. for ( i = 0; i < bodies.Num(); i++ ) {
  4246. body = bodies[i];
  4247. // we transpose the axis before using it because idMat3 is column-major
  4248. axis = body->current->worldAxis.Transpose();
  4249. // if the center of mass is at the body point of reference
  4250. if ( body->centerOfMass.Compare( vec3_origin, CENTER_OF_MASS_EPSILON ) ) {
  4251. // spatial inertia in world space
  4252. body->I.Set( body->mass * mat3_identity, mat3_zero,
  4253. mat3_zero, axis * body->inertiaTensor * axis.Transpose() );
  4254. // inverse spatial inertia in world space
  4255. body->inverseWorldSpatialInertia.Set( body->invMass * mat3_identity, mat3_zero,
  4256. mat3_zero, axis * body->inverseInertiaTensor * axis.Transpose() );
  4257. body->fl.spatialInertiaSparse = true;
  4258. }
  4259. else {
  4260. idMat3 massMoment = body->mass * SkewSymmetric( body->centerOfMass );
  4261. // spatial inertia in world space
  4262. body->I.Set( body->mass * mat3_identity, massMoment,
  4263. massMoment.Transpose(), axis * body->inertiaTensor * axis.Transpose() );
  4264. // inverse spatial inertia in world space
  4265. body->inverseWorldSpatialInertia = body->I.InverseFast();
  4266. body->fl.spatialInertiaSparse = false;
  4267. }
  4268. // initialize auxiliary constraint force to zero
  4269. body->auxForce.Zero();
  4270. }
  4271. }
  4272. /*
  4273. ================
  4274. idPhysics_AF::AddFrameConstraints
  4275. ================
  4276. */
  4277. void idPhysics_AF::AddFrameConstraints( void ) {
  4278. int i;
  4279. // add frame constraints to auxiliary constraints
  4280. for ( i = 0; i < frameConstraints.Num(); i++ ) {
  4281. auxiliaryConstraints.Append( frameConstraints[i] );
  4282. }
  4283. }
  4284. /*
  4285. ================
  4286. idPhysics_AF::RemoveFrameConstraints
  4287. ================
  4288. */
  4289. void idPhysics_AF::RemoveFrameConstraints( void ) {
  4290. // remove all the frame constraints from the auxiliary constraints
  4291. auxiliaryConstraints.SetNum( auxiliaryConstraints.Num() - frameConstraints.Num(), false );
  4292. frameConstraints.SetNum( 0, false );
  4293. }
  4294. /*
  4295. ================
  4296. idPhysics_AF::ApplyFriction
  4297. ================
  4298. */
  4299. void idPhysics_AF::ApplyFriction( float timeStep, float endTimeMSec ) {
  4300. int i;
  4301. float invTimeStep;
  4302. if ( af_skipFriction.GetBool() ) {
  4303. return;
  4304. }
  4305. if ( jointFrictionDentStart < MS2SEC( endTimeMSec ) && jointFrictionDentEnd > MS2SEC( endTimeMSec ) ) {
  4306. float halfTime = ( jointFrictionDentEnd - jointFrictionDentStart ) * 0.5f;
  4307. if ( jointFrictionDentStart + halfTime > MS2SEC( endTimeMSec ) ) {
  4308. jointFrictionDentScale = 1.0f - ( 1.0f - jointFrictionDent ) * ( MS2SEC( endTimeMSec ) - jointFrictionDentStart ) / halfTime;
  4309. } else {
  4310. jointFrictionDentScale = jointFrictionDent + ( 1.0f - jointFrictionDent ) * ( MS2SEC( endTimeMSec ) - jointFrictionDentStart - halfTime ) / halfTime;
  4311. }
  4312. } else {
  4313. jointFrictionDentScale = 0.0f;
  4314. }
  4315. if ( contactFrictionDentStart < MS2SEC( endTimeMSec ) && contactFrictionDentEnd > MS2SEC( endTimeMSec ) ) {
  4316. float halfTime = ( contactFrictionDentEnd - contactFrictionDentStart ) * 0.5f;
  4317. if ( contactFrictionDentStart + halfTime > MS2SEC( endTimeMSec ) ) {
  4318. contactFrictionDentScale = 1.0f - ( 1.0f - contactFrictionDent ) * ( MS2SEC( endTimeMSec ) - contactFrictionDentStart ) / halfTime;
  4319. } else {
  4320. contactFrictionDentScale = contactFrictionDent + ( 1.0f - contactFrictionDent ) * ( MS2SEC( endTimeMSec ) - contactFrictionDentStart - halfTime ) / halfTime;
  4321. }
  4322. } else {
  4323. contactFrictionDentScale = 0.0f;
  4324. }
  4325. invTimeStep = 1.0f / timeStep;
  4326. for ( i = 0; i < primaryConstraints.Num(); i++ ) {
  4327. primaryConstraints[i]->ApplyFriction( invTimeStep );
  4328. }
  4329. for ( i = 0; i < auxiliaryConstraints.Num(); i++ ) {
  4330. auxiliaryConstraints[i]->ApplyFriction( invTimeStep );
  4331. }
  4332. for ( i = 0; i < frameConstraints.Num(); i++ ) {
  4333. frameConstraints[i]->ApplyFriction( invTimeStep );
  4334. }
  4335. }
  4336. /*
  4337. ================
  4338. idPhysics_AF::PrimaryFactor
  4339. ================
  4340. */
  4341. void idPhysics_AF::PrimaryFactor( void ) {
  4342. int i;
  4343. for ( i = 0; i < trees.Num(); i++ ) {
  4344. trees[i]->Factor();
  4345. }
  4346. }
  4347. /*
  4348. ================
  4349. idPhysics_AF::PrimaryForces
  4350. ================
  4351. */
  4352. void idPhysics_AF::PrimaryForces( float timeStep ) {
  4353. int i;
  4354. for ( i = 0; i < trees.Num(); i++ ) {
  4355. trees[i]->CalculateForces( timeStep );
  4356. }
  4357. }
  4358. /*
  4359. ================
  4360. idPhysics_AF::AuxiliaryForces
  4361. ================
  4362. */
  4363. void idPhysics_AF::AuxiliaryForces( float timeStep ) {
  4364. int i, j, k, l, n, m, s, numAuxConstraints, *index, *boxIndex;
  4365. float *ptr, *j1, *j2, *dstPtr, *forcePtr;
  4366. float invStep, u;
  4367. idAFBody *body;
  4368. idAFConstraint *constraint;
  4369. idVecX tmp;
  4370. idMatX jmk;
  4371. idVecX rhs, w, lm, lo, hi;
  4372. // get the number of one dimensional auxiliary constraints
  4373. for ( numAuxConstraints = 0, i = 0; i < auxiliaryConstraints.Num(); i++ ) {
  4374. numAuxConstraints += auxiliaryConstraints[i]->J1.GetNumRows();
  4375. }
  4376. if ( numAuxConstraints == 0 ) {
  4377. return;
  4378. }
  4379. // allocate memory to store the body response to auxiliary constraint forces
  4380. forcePtr = (float *) _alloca16( bodies.Num() * numAuxConstraints * 8 * sizeof( float ) );
  4381. index = (int *) _alloca16( bodies.Num() * numAuxConstraints * sizeof( int ) );
  4382. for ( i = 0; i < bodies.Num(); i++ ) {
  4383. body = bodies[i];
  4384. body->response = forcePtr;
  4385. body->responseIndex = index;
  4386. body->numResponses = 0;
  4387. body->maxAuxiliaryIndex = 0;
  4388. forcePtr += numAuxConstraints * 8;
  4389. index += numAuxConstraints;
  4390. }
  4391. // set on each body the largest index of an auxiliary constraint constraining the body
  4392. if ( af_useSymmetry.GetBool() ) {
  4393. for ( k = 0, i = 0; i < auxiliaryConstraints.Num(); i++ ) {
  4394. constraint = auxiliaryConstraints[i];
  4395. for ( j = 0; j < constraint->J1.GetNumRows(); j++, k++ ) {
  4396. if ( k > constraint->body1->maxAuxiliaryIndex ) {
  4397. constraint->body1->maxAuxiliaryIndex = k;
  4398. }
  4399. if ( constraint->body2 && k > constraint->body2->maxAuxiliaryIndex ) {
  4400. constraint->body2->maxAuxiliaryIndex = k;
  4401. }
  4402. }
  4403. }
  4404. for ( i = 0; i < trees.Num(); i++ ) {
  4405. trees[i]->SetMaxSubTreeAuxiliaryIndex();
  4406. }
  4407. }
  4408. // calculate forces of primary constraints in response to the auxiliary constraint forces
  4409. for ( k = 0, i = 0; i < auxiliaryConstraints.Num(); i++ ) {
  4410. constraint = auxiliaryConstraints[i];
  4411. for ( j = 0; j < constraint->J1.GetNumRows(); j++, k++ ) {
  4412. // calculate body forces in the tree in response to the constraint force
  4413. constraint->body1->tree->Response( constraint, j, k );
  4414. // if there is a second body which is part of a different tree
  4415. if ( constraint->body2 && constraint->body2->tree != constraint->body1->tree ) {
  4416. // calculate body forces in the second tree in response to the constraint force
  4417. constraint->body2->tree->Response( constraint, j, k );
  4418. }
  4419. }
  4420. }
  4421. // NOTE: the rows are 16 byte padded
  4422. jmk.SetData( numAuxConstraints, ((numAuxConstraints+3)&~3), MATX_ALLOCA( numAuxConstraints * ((numAuxConstraints+3)&~3) ) );
  4423. tmp.SetData( 6, VECX_ALLOCA( 6 ) );
  4424. // create constraint matrix for auxiliary constraints using a mass matrix adjusted for the primary constraints
  4425. for ( k = 0, i = 0; i < auxiliaryConstraints.Num(); i++ ) {
  4426. constraint = auxiliaryConstraints[i];
  4427. for ( j = 0; j < constraint->J1.GetNumRows(); j++, k++ ) {
  4428. constraint->body1->InverseWorldSpatialInertiaMultiply( tmp, constraint->J1[j] );
  4429. j1 = tmp.ToFloatPtr();
  4430. ptr = constraint->body1->response;
  4431. index = constraint->body1->responseIndex;
  4432. dstPtr = jmk[k];
  4433. s = af_useSymmetry.GetBool() ? k + 1 : numAuxConstraints;
  4434. for ( l = n = 0, m = index[n]; n < constraint->body1->numResponses && m < s; n++, m = index[n] ) {
  4435. while( l < m ) {
  4436. dstPtr[l++] = 0.0f;
  4437. }
  4438. dstPtr[l++] = j1[0] * ptr[0] + j1[1] * ptr[1] + j1[2] * ptr[2] +
  4439. j1[3] * ptr[3] + j1[4] * ptr[4] + j1[5] * ptr[5];
  4440. ptr += 8;
  4441. }
  4442. while( l < s ) {
  4443. dstPtr[l++] = 0.0f;
  4444. }
  4445. if ( constraint->body2 ) {
  4446. constraint->body2->InverseWorldSpatialInertiaMultiply( tmp, constraint->J2[j] );
  4447. j2 = tmp.ToFloatPtr();
  4448. ptr = constraint->body2->response;
  4449. index = constraint->body2->responseIndex;
  4450. for ( n = 0, m = index[n]; n < constraint->body2->numResponses && m < s; n++, m = index[n] ) {
  4451. dstPtr[m] += j2[0] * ptr[0] + j2[1] * ptr[1] + j2[2] * ptr[2] +
  4452. j2[3] * ptr[3] + j2[4] * ptr[4] + j2[5] * ptr[5];
  4453. ptr += 8;
  4454. }
  4455. }
  4456. }
  4457. }
  4458. if ( af_useSymmetry.GetBool() ) {
  4459. n = jmk.GetNumColumns();
  4460. for ( i = 0; i < numAuxConstraints; i++ ) {
  4461. ptr = jmk.ToFloatPtr() + ( i + 1 ) * n + i;
  4462. dstPtr = jmk.ToFloatPtr() + i * n + i + 1;
  4463. for ( j = i+1; j < numAuxConstraints; j++ ) {
  4464. *dstPtr++ = *ptr;
  4465. ptr += n;
  4466. }
  4467. }
  4468. }
  4469. invStep = 1.0f / timeStep;
  4470. // calculate body acceleration
  4471. for ( i = 0; i < bodies.Num(); i++ ) {
  4472. body = bodies[i];
  4473. body->InverseWorldSpatialInertiaMultiply( body->acceleration, body->totalForce.ToFloatPtr() );
  4474. body->acceleration.SubVec6(0) += body->current->spatialVelocity * invStep;
  4475. }
  4476. rhs.SetData( numAuxConstraints, VECX_ALLOCA( numAuxConstraints ) );
  4477. lo.SetData( numAuxConstraints, VECX_ALLOCA( numAuxConstraints ) );
  4478. hi.SetData( numAuxConstraints, VECX_ALLOCA( numAuxConstraints ) );
  4479. lm.SetData( numAuxConstraints, VECX_ALLOCA( numAuxConstraints ) );
  4480. boxIndex = (int *) _alloca16( numAuxConstraints * sizeof( int ) );
  4481. // set first index for special box constrained variables
  4482. for ( k = 0, i = 0; i < auxiliaryConstraints.Num(); i++ ) {
  4483. auxiliaryConstraints[i]->firstIndex = k;
  4484. k += auxiliaryConstraints[i]->J1.GetNumRows();
  4485. }
  4486. // initialize right hand side and low and high bounds for auxiliary constraints
  4487. for ( k = 0, i = 0; i < auxiliaryConstraints.Num(); i++ ) {
  4488. constraint = auxiliaryConstraints[i];
  4489. n = k;
  4490. for ( j = 0; j < constraint->J1.GetNumRows(); j++, k++ ) {
  4491. j1 = constraint->J1[j];
  4492. ptr = constraint->body1->acceleration.ToFloatPtr();
  4493. 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];
  4494. rhs[k] += constraint->c1[j] * invStep;
  4495. if ( constraint->body2 ) {
  4496. j2 = constraint->J2[j];
  4497. ptr = constraint->body2->acceleration.ToFloatPtr();
  4498. 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];
  4499. rhs[k] += constraint->c2[j] * invStep;
  4500. }
  4501. rhs[k] = -rhs[k];
  4502. lo[k] = constraint->lo[j];
  4503. hi[k] = constraint->hi[j];
  4504. if ( constraint->boxIndex[j] >= 0 ) {
  4505. if ( constraint->boxConstraint->fl.isPrimary ) {
  4506. gameLocal.Error( "cannot reference primary constraints for the box index" );
  4507. }
  4508. boxIndex[k] = constraint->boxConstraint->firstIndex + constraint->boxIndex[j];
  4509. }
  4510. else {
  4511. boxIndex[k] = -1;
  4512. }
  4513. jmk[k][k] += constraint->e[j] * invStep;
  4514. }
  4515. }
  4516. #ifdef AF_TIMINGS
  4517. timer_lcp.Start();
  4518. #endif
  4519. // calculate lagrange multipliers for auxiliary constraints
  4520. if ( !lcp->Solve( jmk, lm, rhs, lo, hi, boxIndex ) ) {
  4521. return; // bad monkey!
  4522. }
  4523. #ifdef AF_TIMINGS
  4524. timer_lcp.Stop();
  4525. #endif
  4526. // calculate auxiliary constraint forces
  4527. for ( k = 0, i = 0; i < auxiliaryConstraints.Num(); i++ ) {
  4528. constraint = auxiliaryConstraints[i];
  4529. for ( j = 0; j < constraint->J1.GetNumRows(); j++, k++ ) {
  4530. constraint->lm[j] = u = lm[k];
  4531. j1 = constraint->J1[j];
  4532. ptr = constraint->body1->auxForce.ToFloatPtr();
  4533. ptr[0] += j1[0] * u; ptr[1] += j1[1] * u; ptr[2] += j1[2] * u;
  4534. ptr[3] += j1[3] * u; ptr[4] += j1[4] * u; ptr[5] += j1[5] * u;
  4535. if ( constraint->body2 ) {
  4536. j2 = constraint->J2[j];
  4537. ptr = constraint->body2->auxForce.ToFloatPtr();
  4538. ptr[0] += j2[0] * u; ptr[1] += j2[1] * u; ptr[2] += j2[2] * u;
  4539. ptr[3] += j2[3] * u; ptr[4] += j2[4] * u; ptr[5] += j2[5] * u;
  4540. }
  4541. }
  4542. }
  4543. // recalculate primary constraint forces in response to auxiliary constraint forces
  4544. PrimaryForces( timeStep );
  4545. // clear pointers pointing to stack space so tools don't get confused
  4546. for ( i = 0; i < bodies.Num(); i++ ) {
  4547. body = bodies[i];
  4548. body->response = NULL;
  4549. body->responseIndex = NULL;
  4550. }
  4551. }
  4552. /*
  4553. ================
  4554. idPhysics_AF::VerifyContactConstraints
  4555. ================
  4556. */
  4557. void idPhysics_AF::VerifyContactConstraints( void ) {
  4558. #if 0
  4559. int i;
  4560. float impulseNumerator, impulseDenominator;
  4561. idVec3 r, velocity, normalVelocity, normal, impulse;
  4562. idAFBody *body;
  4563. for ( i = 0; i < contactConstraints.Num(); i++ ) {
  4564. body = contactConstraints[i]->body1;
  4565. const contactInfo_t &contact = contactConstraints[i]->GetContact();
  4566. r = contact.point - body->GetCenterOfMass();
  4567. // calculate velocity at contact point
  4568. velocity = body->GetLinearVelocity() + body->GetAngularVelocity().Cross( r );
  4569. // velocity along normal vector
  4570. normalVelocity = ( velocity * contact.normal ) * contact.normal;
  4571. // if moving towards the surface at the contact point
  4572. if ( normalVelocity * contact.normal < 0.0f ) {
  4573. // calculate impulse
  4574. normal = -normalVelocity;
  4575. impulseNumerator = normal.Normalize();
  4576. impulseDenominator = body->GetInverseMass() + ( ( body->GetInverseWorldInertia() * r.Cross( normal ) ).Cross( r ) * normal );
  4577. impulse = (impulseNumerator / impulseDenominator) * normal * 1.0001f;
  4578. // apply impulse
  4579. body->SetLinearVelocity( body->GetLinearVelocity() + impulse );
  4580. body->SetAngularVelocity( body->GetAngularVelocity() + r.Cross( impulse ) );
  4581. }
  4582. }
  4583. #else
  4584. int i;
  4585. idAFBody *body;
  4586. idVec3 normal;
  4587. for ( i = 0; i < contactConstraints.Num(); i++ ) {
  4588. body = contactConstraints[i]->body1;
  4589. normal = contactConstraints[i]->GetContact().normal;
  4590. if ( normal * body->next->spatialVelocity.SubVec3(0) <= 0.0f ) {
  4591. body->next->spatialVelocity.SubVec3(0) -= 1.0001f * (normal * body->next->spatialVelocity.SubVec3(0)) * normal;
  4592. }
  4593. body = contactConstraints[i]->body2;
  4594. if ( !body ) {
  4595. continue;
  4596. }
  4597. normal = -normal;
  4598. if ( normal * body->next->spatialVelocity.SubVec3(0) <= 0.0f ) {
  4599. body->next->spatialVelocity.SubVec3(0) -= 1.0001f * (normal * body->next->spatialVelocity.SubVec3(0)) * normal;
  4600. }
  4601. }
  4602. #endif
  4603. }
  4604. /*
  4605. ================
  4606. idPhysics_AF::Evolve
  4607. ================
  4608. */
  4609. void idPhysics_AF::Evolve( float timeStep ) {
  4610. int i;
  4611. float angle;
  4612. idVec3 vec;
  4613. idAFBody *body;
  4614. idVec6 force;
  4615. idRotation rotation;
  4616. float vSqr, maxLinearVelocity, maxAngularVelocity;
  4617. maxLinearVelocity = af_maxLinearVelocity.GetFloat() / timeStep;
  4618. maxAngularVelocity = af_maxAngularVelocity.GetFloat() / timeStep;
  4619. for ( i = 0; i < bodies.Num(); i++ ) {
  4620. body = bodies[i];
  4621. // calculate the spatial velocity for the next physics state
  4622. body->InverseWorldSpatialInertiaMultiply( body->acceleration, body->totalForce.ToFloatPtr() );
  4623. body->next->spatialVelocity = body->current->spatialVelocity + timeStep * body->acceleration.SubVec6(0);
  4624. if ( maxLinearVelocity > 0.0f ) {
  4625. // cap the linear velocity
  4626. vSqr = body->next->spatialVelocity.SubVec3(0).LengthSqr();
  4627. if ( vSqr > Square( maxLinearVelocity ) ) {
  4628. body->next->spatialVelocity.SubVec3(0) *= idMath::InvSqrt( vSqr ) * maxLinearVelocity;
  4629. }
  4630. }
  4631. if ( maxAngularVelocity > 0.0f ) {
  4632. // cap the angular velocity
  4633. vSqr = body->next->spatialVelocity.SubVec3(1).LengthSqr();
  4634. if ( vSqr > Square( maxAngularVelocity ) ) {
  4635. body->next->spatialVelocity.SubVec3(1) *= idMath::InvSqrt( vSqr ) * maxAngularVelocity;
  4636. }
  4637. }
  4638. }
  4639. // make absolutely sure all contact constraints are satisfied
  4640. VerifyContactConstraints();
  4641. // calculate the position of the bodies for the next physics state
  4642. for ( i = 0; i < bodies.Num(); i++ ) {
  4643. body = bodies[i];
  4644. // translate world origin
  4645. body->next->worldOrigin = body->current->worldOrigin + timeStep * body->next->spatialVelocity.SubVec3( 0 );
  4646. // convert angular velocity to a rotation matrix
  4647. vec = body->next->spatialVelocity.SubVec3( 1 );
  4648. angle = -timeStep * (float) RAD2DEG( vec.Normalize() );
  4649. rotation = idRotation( vec3_origin, vec, angle );
  4650. rotation.Normalize180();
  4651. // rotate world axis
  4652. body->next->worldAxis = body->current->worldAxis * rotation.ToMat3();
  4653. body->next->worldAxis.OrthoNormalizeSelf();
  4654. // linear and angular friction
  4655. body->next->spatialVelocity.SubVec3(0) -= body->linearFriction * body->next->spatialVelocity.SubVec3(0);
  4656. body->next->spatialVelocity.SubVec3(1) -= body->angularFriction * body->next->spatialVelocity.SubVec3(1);
  4657. }
  4658. }
  4659. /*
  4660. ================
  4661. idPhysics_AF::CollisionImpulse
  4662. apply impulse to the colliding bodies
  4663. the current state of the body should be set to the moment of impact
  4664. this is silly as it doesn't take the AF structure into account
  4665. ================
  4666. */
  4667. bool idPhysics_AF::CollisionImpulse( float timeStep, idAFBody *body, trace_t &collision ) {
  4668. idVec3 r, velocity, impulse;
  4669. idMat3 inverseWorldInertiaTensor;
  4670. float impulseNumerator, impulseDenominator;
  4671. impactInfo_t info;
  4672. idEntity *ent;
  4673. ent = gameLocal.entities[collision.c.entityNum];
  4674. if ( ent == self ) {
  4675. return false;
  4676. }
  4677. // get info from other entity involved
  4678. ent->GetImpactInfo( self, collision.c.id, collision.c.point, &info );
  4679. // collision point relative to the body center of mass
  4680. r = collision.c.point - (body->current->worldOrigin + body->centerOfMass * body->current->worldAxis);
  4681. // the velocity at the collision point
  4682. velocity = body->current->spatialVelocity.SubVec3(0) + body->current->spatialVelocity.SubVec3(1).Cross(r);
  4683. // subtract velocity of other entity
  4684. velocity -= info.velocity;
  4685. // never stick
  4686. if ( velocity * collision.c.normal > 0.0f ) {
  4687. velocity = collision.c.normal;
  4688. }
  4689. inverseWorldInertiaTensor = body->current->worldAxis.Transpose() * body->inverseInertiaTensor * body->current->worldAxis;
  4690. impulseNumerator = -( 1.0f + body->bouncyness ) * ( velocity * collision.c.normal );
  4691. impulseDenominator = body->invMass + ( ( inverseWorldInertiaTensor * r.Cross( collision.c.normal ) ).Cross( r ) * collision.c.normal );
  4692. if ( info.invMass ) {
  4693. impulseDenominator += info.invMass + ( ( info.invInertiaTensor * info.position.Cross( collision.c.normal ) ).Cross( info.position ) * collision.c.normal );
  4694. }
  4695. impulse = (impulseNumerator / impulseDenominator) * collision.c.normal;
  4696. // apply impact to other entity
  4697. ent->ApplyImpulse( self, collision.c.id, collision.c.point, -impulse );
  4698. // callback to self to let the entity know about the impact
  4699. return self->Collide( collision, velocity );
  4700. }
  4701. /*
  4702. ================
  4703. idPhysics_AF::ApplyCollisions
  4704. ================
  4705. */
  4706. bool idPhysics_AF::ApplyCollisions( float timeStep ) {
  4707. int i;
  4708. for ( i = 0; i < collisions.Num(); i++ ) {
  4709. if ( CollisionImpulse( timeStep, collisions[i].body, collisions[i].trace ) ) {
  4710. return true;
  4711. }
  4712. }
  4713. return false;
  4714. }
  4715. /*
  4716. ================
  4717. idPhysics_AF::SetupCollisionForBody
  4718. ================
  4719. */
  4720. idEntity *idPhysics_AF::SetupCollisionForBody( idAFBody *body ) const {
  4721. int i;
  4722. idAFBody *b;
  4723. idEntity *passEntity;
  4724. passEntity = NULL;
  4725. if ( !selfCollision || !body->fl.selfCollision || af_skipSelfCollision.GetBool() ) {
  4726. // disable all bodies
  4727. for ( i = 0; i < bodies.Num(); i++ ) {
  4728. bodies[i]->clipModel->Disable();
  4729. }
  4730. // don't collide with world collision model if attached to the world
  4731. for ( i = 0; i < body->constraints.Num(); i++ ) {
  4732. if ( !body->constraints[i]->fl.noCollision ) {
  4733. continue;
  4734. }
  4735. // if this constraint attaches the body to the world
  4736. if ( body->constraints[i]->body2 == NULL ) {
  4737. // don't collide with the world collision model
  4738. passEntity = gameLocal.world;
  4739. }
  4740. }
  4741. } else {
  4742. // enable all bodies that have self collision
  4743. for ( i = 0; i < bodies.Num(); i++ ) {
  4744. if ( bodies[i]->fl.selfCollision ) {
  4745. bodies[i]->clipModel->Enable();
  4746. } else {
  4747. bodies[i]->clipModel->Disable();
  4748. }
  4749. }
  4750. // don't let the body collide with itself
  4751. body->clipModel->Disable();
  4752. // disable any bodies attached with constraints
  4753. for ( i = 0; i < body->constraints.Num(); i++ ) {
  4754. if ( !body->constraints[i]->fl.noCollision ) {
  4755. continue;
  4756. }
  4757. // if this constraint attaches the body to the world
  4758. if ( body->constraints[i]->body2 == NULL ) {
  4759. // don't collide with the world collision model
  4760. passEntity = gameLocal.world;
  4761. } else {
  4762. if ( body->constraints[i]->body1 == body ) {
  4763. b = body->constraints[i]->body2;
  4764. } else if ( body->constraints[i]->body2 == body ) {
  4765. b = body->constraints[i]->body1;
  4766. } else {
  4767. continue;
  4768. }
  4769. // don't collide with this body
  4770. b->clipModel->Disable();
  4771. }
  4772. }
  4773. }
  4774. return passEntity;
  4775. }
  4776. /*
  4777. ================
  4778. idPhysics_AF::CheckForCollisions
  4779. check for collisions between the current and next state
  4780. if there is a collision the next state is set to the state at the moment of impact
  4781. assumes all bodies are linked for collision detection and relinks all bodies after moving them
  4782. ================
  4783. */
  4784. void idPhysics_AF::CheckForCollisions( float timeStep ) {
  4785. // #define TEST_COLLISION_DETECTION
  4786. int i, index;
  4787. idAFBody *body;
  4788. idMat3 axis;
  4789. idRotation rotation;
  4790. trace_t collision;
  4791. idEntity *passEntity;
  4792. // clear list with collisions
  4793. collisions.SetNum( 0, false );
  4794. if ( !enableCollision ) {
  4795. return;
  4796. }
  4797. for ( i = 0; i < bodies.Num(); i++ ) {
  4798. body = bodies[i];
  4799. if ( body->clipMask != 0 ) {
  4800. passEntity = SetupCollisionForBody( body );
  4801. #ifdef TEST_COLLISION_DETECTION
  4802. bool startsolid = false;
  4803. if ( gameLocal.clip.Contents( body->current->worldOrigin, body->clipModel,
  4804. body->current->worldAxis, body->clipMask, passEntity ) ) {
  4805. startsolid = true;
  4806. }
  4807. #endif
  4808. TransposeMultiply( body->current->worldAxis, body->next->worldAxis, axis );
  4809. rotation = axis.ToRotation();
  4810. rotation.SetOrigin( body->current->worldOrigin );
  4811. // if there was a collision
  4812. if ( gameLocal.clip.Motion( collision, body->current->worldOrigin, body->next->worldOrigin, rotation,
  4813. body->clipModel, body->current->worldAxis, body->clipMask, passEntity ) ) {
  4814. // set the next state to the state at the moment of impact
  4815. body->next->worldOrigin = collision.endpos;
  4816. body->next->worldAxis = collision.endAxis;
  4817. // add collision to the list
  4818. index = collisions.Num();
  4819. collisions.SetNum( index + 1, false );
  4820. collisions[index].trace = collision;
  4821. collisions[index].body = body;
  4822. }
  4823. #ifdef TEST_COLLISION_DETECTION
  4824. if ( gameLocal.clip.Contents( body->next->worldOrigin, body->clipModel,
  4825. body->next->worldAxis, body->clipMask, passEntity ) ) {
  4826. if ( !startsolid ) {
  4827. int bah = 1;
  4828. }
  4829. }
  4830. #endif
  4831. }
  4832. body->clipModel->Link( gameLocal.clip, self, body->clipModel->GetId(), body->next->worldOrigin, body->next->worldAxis );
  4833. }
  4834. }
  4835. /*
  4836. ================
  4837. idPhysics_AF::EvaluateContacts
  4838. ================
  4839. */
  4840. bool idPhysics_AF::EvaluateContacts( void ) {
  4841. int i, j, k, numContacts, numBodyContacts;
  4842. idAFBody *body;
  4843. contactInfo_t contactInfo[10];
  4844. idEntity *passEntity;
  4845. idVecX dir( 6, VECX_ALLOCA( 6 ) );
  4846. // evaluate bodies
  4847. EvaluateBodies( current.lastTimeStep );
  4848. // remove all existing contacts
  4849. ClearContacts();
  4850. contactBodies.SetNum( 0, false );
  4851. if ( !enableCollision ) {
  4852. return false;
  4853. }
  4854. // find all the contacts
  4855. for ( i = 0; i < bodies.Num(); i++ ) {
  4856. body = bodies[i];
  4857. if ( body->clipMask == 0 ) {
  4858. continue;
  4859. }
  4860. passEntity = SetupCollisionForBody( body );
  4861. body->InverseWorldSpatialInertiaMultiply( dir, body->current->externalForce.ToFloatPtr() );
  4862. dir.SubVec6(0) = body->current->spatialVelocity + current.lastTimeStep * dir.SubVec6(0);
  4863. dir.SubVec3(0).Normalize();
  4864. dir.SubVec3(1).Normalize();
  4865. numContacts = gameLocal.clip.Contacts( contactInfo, 10, body->current->worldOrigin, dir.SubVec6(0), 2.0f, //CONTACT_EPSILON,
  4866. body->clipModel, body->current->worldAxis, body->clipMask, passEntity );
  4867. #if 1
  4868. // merge nearby contacts between the same bodies
  4869. // and assure there are at most three planar contacts between any pair of bodies
  4870. for ( j = 0; j < numContacts; j++ ) {
  4871. numBodyContacts = 0;
  4872. for ( k = 0; k < contacts.Num(); k++ ) {
  4873. if ( contacts[k].entityNum == contactInfo[j].entityNum ) {
  4874. if ( ( contacts[k].id == i && contactInfo[j].id == contactBodies[k] ) ||
  4875. ( contactBodies[k] == i && contacts[k].id == contactInfo[j].id ) ) {
  4876. if ( ( contacts[k].point - contactInfo[j].point ).LengthSqr() < Square( 2.0f ) ) {
  4877. break;
  4878. }
  4879. if ( idMath::Fabs( contacts[k].normal * contactInfo[j].normal ) > 0.9f ) {
  4880. numBodyContacts++;
  4881. }
  4882. }
  4883. }
  4884. }
  4885. if ( k >= contacts.Num() && numBodyContacts < 3 ) {
  4886. contacts.Append( contactInfo[j] );
  4887. contactBodies.Append( i );
  4888. }
  4889. }
  4890. #else
  4891. for ( j = 0; j < numContacts; j++ ) {
  4892. contacts.Append( contactInfo[j] );
  4893. contactBodies.Append( i );
  4894. }
  4895. #endif
  4896. }
  4897. AddContactEntitiesForContacts();
  4898. return ( contacts.Num() != 0 );
  4899. }
  4900. /*
  4901. ================
  4902. idPhysics_AF::SetupContactConstraints
  4903. ================
  4904. */
  4905. void idPhysics_AF::SetupContactConstraints( void ) {
  4906. int i;
  4907. // make sure enough contact constraints are allocated
  4908. contactConstraints.AssureSizeAlloc( contacts.Num(), idListNewElement<idAFConstraint_Contact> );
  4909. contactConstraints.SetNum( contacts.Num(), false );
  4910. // setup contact constraints
  4911. for ( i = 0; i < contacts.Num(); i++ ) {
  4912. // add contact constraint
  4913. contactConstraints[i]->physics = this;
  4914. if ( contacts[i].entityNum == self->entityNumber ) {
  4915. contactConstraints[i]->Setup( bodies[contactBodies[i]], bodies[ contacts[i].id ], contacts[i] );
  4916. }
  4917. else {
  4918. contactConstraints[i]->Setup( bodies[contactBodies[i]], NULL, contacts[i] );
  4919. }
  4920. }
  4921. }
  4922. /*
  4923. ================
  4924. idPhysics_AF::ApplyContactForces
  4925. ================
  4926. */
  4927. void idPhysics_AF::ApplyContactForces( void ) {
  4928. #if 0
  4929. int i;
  4930. idEntity *ent;
  4931. idVec3 force;
  4932. for ( i = 0; i < contactConstraints.Num(); i++ ) {
  4933. if ( contactConstraints[i]->body2 != NULL ) {
  4934. continue;
  4935. }
  4936. const contactInfo_t &contact = contactConstraints[i]->GetContact();
  4937. ent = gameLocal.entities[contact.entityNum];
  4938. if ( !ent ) {
  4939. continue;
  4940. }
  4941. force.Zero();
  4942. ent->AddForce( self, contact.id, contact.point, force );
  4943. }
  4944. #endif
  4945. }
  4946. /*
  4947. ================
  4948. idPhysics_AF::ClearExternalForce
  4949. ================
  4950. */
  4951. void idPhysics_AF::ClearExternalForce( void ) {
  4952. int i;
  4953. idAFBody *body;
  4954. for ( i = 0; i < bodies.Num(); i++ ) {
  4955. body = bodies[i];
  4956. // clear external force
  4957. body->current->externalForce.Zero();
  4958. body->next->externalForce.Zero();
  4959. }
  4960. }
  4961. /*
  4962. ================
  4963. idPhysics_AF::AddGravity
  4964. ================
  4965. */
  4966. void idPhysics_AF::AddGravity( void ) {
  4967. int i;
  4968. idAFBody *body;
  4969. for ( i = 0; i < bodies.Num(); i++ ) {
  4970. body = bodies[i];
  4971. // add gravitational force
  4972. body->current->externalForce.SubVec3( 0 ) += body->mass * gravityVector;
  4973. }
  4974. }
  4975. /*
  4976. ================
  4977. idPhysics_AF::SwapStates
  4978. ================
  4979. */
  4980. void idPhysics_AF::SwapStates( void ) {
  4981. int i;
  4982. idAFBody *body;
  4983. AFBodyPState_t *swap;
  4984. for ( i = 0; i < bodies.Num(); i++ ) {
  4985. body = bodies[i];
  4986. // swap the current and next state for next simulation step
  4987. swap = body->current;
  4988. body->current = body->next;
  4989. body->next = swap;
  4990. }
  4991. }
  4992. /*
  4993. ================
  4994. idPhysics_AF::UpdateClipModels
  4995. ================
  4996. */
  4997. void idPhysics_AF::UpdateClipModels( void ) {
  4998. int i;
  4999. idAFBody *body;
  5000. for ( i = 0; i < bodies.Num(); i++ ) {
  5001. body = bodies[i];
  5002. body->clipModel->Link( gameLocal.clip, self, body->clipModel->GetId(), body->current->worldOrigin, body->current->worldAxis );
  5003. }
  5004. }
  5005. /*
  5006. ================
  5007. idPhysics_AF::SetSuspendSpeed
  5008. ================
  5009. */
  5010. void idPhysics_AF::SetSuspendSpeed( const idVec2 &velocity, const idVec2 &acceleration ) {
  5011. this->suspendVelocity = velocity;
  5012. this->suspendAcceleration = acceleration;
  5013. }
  5014. /*
  5015. ================
  5016. idPhysics_AF::SetSuspendTime
  5017. ================
  5018. */
  5019. void idPhysics_AF::SetSuspendTime( const float minTime, const float maxTime ) {
  5020. this->minMoveTime = minTime;
  5021. this->maxMoveTime = maxTime;
  5022. }
  5023. /*
  5024. ================
  5025. idPhysics_AF::SetSuspendTolerance
  5026. ================
  5027. */
  5028. void idPhysics_AF::SetSuspendTolerance( const float noMoveTime, const float noMoveTranslation, const float noMoveRotation ) {
  5029. this->noMoveTime = noMoveTime;
  5030. this->noMoveTranslation = noMoveTranslation;
  5031. this->noMoveRotation = noMoveRotation;
  5032. }
  5033. /*
  5034. ================
  5035. idPhysics_AF::SetTimeScaleRamp
  5036. ================
  5037. */
  5038. void idPhysics_AF::SetTimeScaleRamp( const float start, const float end ) {
  5039. timeScaleRampStart = start;
  5040. timeScaleRampEnd = end;
  5041. }
  5042. /*
  5043. ================
  5044. idPhysics_AF::SetJointFrictionDent
  5045. ================
  5046. */
  5047. void idPhysics_AF::SetJointFrictionDent( const float dent, const float start, const float end ) {
  5048. jointFrictionDent = dent;
  5049. jointFrictionDentStart = start;
  5050. jointFrictionDentEnd = end;
  5051. }
  5052. /*
  5053. ================
  5054. idPhysics_AF::GetJointFrictionScale
  5055. ================
  5056. */
  5057. float idPhysics_AF::GetJointFrictionScale( void ) const {
  5058. if ( jointFrictionDentScale > 0.0f ) {
  5059. return jointFrictionDentScale;
  5060. } else if ( jointFrictionScale > 0.0f ) {
  5061. return jointFrictionScale;
  5062. } else if ( af_jointFrictionScale.GetFloat() > 0.0f ) {
  5063. return af_jointFrictionScale.GetFloat();
  5064. }
  5065. return 1.0f;
  5066. }
  5067. /*
  5068. ================
  5069. idPhysics_AF::SetContactFrictionDent
  5070. ================
  5071. */
  5072. void idPhysics_AF::SetContactFrictionDent( const float dent, const float start, const float end ) {
  5073. contactFrictionDent = dent;
  5074. contactFrictionDentStart = start;
  5075. contactFrictionDentEnd = end;
  5076. }
  5077. /*
  5078. ================
  5079. idPhysics_AF::GetContactFrictionScale
  5080. ================
  5081. */
  5082. float idPhysics_AF::GetContactFrictionScale( void ) const {
  5083. if ( contactFrictionDentScale > 0.0f ) {
  5084. return contactFrictionDentScale;
  5085. } else if ( contactFrictionScale > 0.0f ) {
  5086. return contactFrictionScale;
  5087. } else if ( af_contactFrictionScale.GetFloat() > 0.0f ) {
  5088. return af_contactFrictionScale.GetFloat();
  5089. }
  5090. return 1.0f;
  5091. }
  5092. /*
  5093. ================
  5094. idPhysics_AF::TestIfAtRest
  5095. ================
  5096. */
  5097. bool idPhysics_AF::TestIfAtRest( float timeStep ) {
  5098. int i;
  5099. float translationSqr, maxTranslationSqr, rotation, maxRotation;
  5100. idAFBody *body;
  5101. if ( current.atRest >= 0 ) {
  5102. return true;
  5103. }
  5104. current.activateTime += timeStep;
  5105. // if the simulation should never be suspended before a certaint amount of time passed
  5106. if ( minMoveTime > 0.0f && current.activateTime < minMoveTime ) {
  5107. return false;
  5108. }
  5109. // if the simulation should always be suspended after a certain amount time passed
  5110. if ( maxMoveTime > 0.0f && current.activateTime > maxMoveTime ) {
  5111. return true;
  5112. }
  5113. // test if all bodies hardly moved over a period of time
  5114. if ( current.noMoveTime == 0.0f ) {
  5115. for ( i = 0; i < bodies.Num(); i++ ) {
  5116. body = bodies[i];
  5117. body->atRestOrigin = body->current->worldOrigin;
  5118. body->atRestAxis = body->current->worldAxis;
  5119. }
  5120. current.noMoveTime += timeStep;
  5121. }
  5122. else if ( current.noMoveTime > noMoveTime ) {
  5123. current.noMoveTime = 0.0f;
  5124. maxTranslationSqr = 0.0f;
  5125. maxRotation = 0.0f;
  5126. for ( i = 0; i < bodies.Num(); i++ ) {
  5127. body = bodies[i];
  5128. translationSqr = ( body->current->worldOrigin - body->atRestOrigin ).LengthSqr();
  5129. if ( translationSqr > maxTranslationSqr ) {
  5130. maxTranslationSqr = translationSqr;
  5131. }
  5132. rotation = ( body->atRestAxis.Transpose() * body->current->worldAxis ).ToRotation().GetAngle();
  5133. if ( rotation > maxRotation ) {
  5134. maxRotation = rotation;
  5135. }
  5136. }
  5137. if ( maxTranslationSqr < Square( noMoveTranslation ) && maxRotation < noMoveRotation ) {
  5138. // hardly moved over a period of time so the articulated figure may come to rest
  5139. return true;
  5140. }
  5141. } else {
  5142. current.noMoveTime += timeStep;
  5143. }
  5144. // test if the velocity or acceleration of any body is still too large to come to rest
  5145. for ( i = 0; i < bodies.Num(); i++ ) {
  5146. body = bodies[i];
  5147. if ( body->current->spatialVelocity.SubVec3(0).LengthSqr() > Square( suspendVelocity[0] ) ) {
  5148. return false;
  5149. }
  5150. if ( body->current->spatialVelocity.SubVec3(1).LengthSqr() > Square( suspendVelocity[1] ) ) {
  5151. return false;
  5152. }
  5153. if ( body->acceleration.SubVec3(0).LengthSqr() > Square( suspendAcceleration[0] ) ) {
  5154. return false;
  5155. }
  5156. if ( body->acceleration.SubVec3(1).LengthSqr() > Square( suspendAcceleration[1] ) ) {
  5157. return false;
  5158. }
  5159. }
  5160. // all bodies have a velocity and acceleration small enough to come to rest
  5161. return true;
  5162. }
  5163. /*
  5164. ================
  5165. idPhysics_AF::Rest
  5166. ================
  5167. */
  5168. void idPhysics_AF::Rest( void ) {
  5169. int i;
  5170. current.atRest = gameLocal.time;
  5171. for ( i = 0; i < bodies.Num(); i++ ) {
  5172. bodies[i]->current->spatialVelocity.Zero();
  5173. bodies[i]->current->externalForce.Zero();
  5174. }
  5175. self->BecomeInactive( TH_PHYSICS );
  5176. }
  5177. /*
  5178. ================
  5179. idPhysics_AF::Activate
  5180. ================
  5181. */
  5182. void idPhysics_AF::Activate( void ) {
  5183. // if the articulated figure was at rest
  5184. if ( current.atRest >= 0 ) {
  5185. // normally gravity is added at the end of a simulation frame
  5186. // if the figure was at rest add gravity here so it is applied this simulation frame
  5187. AddGravity();
  5188. // reset the active time for the max move time
  5189. current.activateTime = 0.0f;
  5190. }
  5191. current.atRest = -1;
  5192. current.noMoveTime = 0.0f;
  5193. self->BecomeActive( TH_PHYSICS );
  5194. }
  5195. /*
  5196. ================
  5197. idPhysics_AF::PutToRest
  5198. put to rest untill something collides with this physics object
  5199. ================
  5200. */
  5201. void idPhysics_AF::PutToRest( void ) {
  5202. Rest();
  5203. }
  5204. /*
  5205. ================
  5206. idPhysics_AF::EnableImpact
  5207. ================
  5208. */
  5209. void idPhysics_AF::EnableImpact( void ) {
  5210. noImpact = false;
  5211. }
  5212. /*
  5213. ================
  5214. idPhysics_AF::DisableImpact
  5215. ================
  5216. */
  5217. void idPhysics_AF::DisableImpact( void ) {
  5218. noImpact = true;
  5219. }
  5220. /*
  5221. ================
  5222. idPhysics_AF::AddPushVelocity
  5223. ================
  5224. */
  5225. void idPhysics_AF::AddPushVelocity( const idVec6 &pushVelocity ) {
  5226. int i;
  5227. if ( pushVelocity != vec6_origin ) {
  5228. for ( i = 0; i < bodies.Num(); i++ ) {
  5229. bodies[i]->current->spatialVelocity += pushVelocity;
  5230. }
  5231. }
  5232. }
  5233. /*
  5234. ================
  5235. idPhysics_AF::SetClipModel
  5236. ================
  5237. */
  5238. void idPhysics_AF::SetClipModel( idClipModel *model, float density, int id, bool freeOld ) {
  5239. }
  5240. /*
  5241. ================
  5242. idPhysics_AF::GetClipModel
  5243. ================
  5244. */
  5245. idClipModel *idPhysics_AF::GetClipModel( int id ) const {
  5246. if ( id >= 0 && id < bodies.Num() ) {
  5247. return bodies[id]->GetClipModel();
  5248. }
  5249. return NULL;
  5250. }
  5251. /*
  5252. ================
  5253. idPhysics_AF::GetNumClipModels
  5254. ================
  5255. */
  5256. int idPhysics_AF::GetNumClipModels( void ) const {
  5257. return bodies.Num();
  5258. }
  5259. /*
  5260. ================
  5261. idPhysics_AF::SetMass
  5262. ================
  5263. */
  5264. void idPhysics_AF::SetMass( float mass, int id ) {
  5265. if ( id >= 0 && id < bodies.Num() ) {
  5266. }
  5267. else {
  5268. forceTotalMass = mass;
  5269. }
  5270. SetChanged();
  5271. }
  5272. /*
  5273. ================
  5274. idPhysics_AF::GetMass
  5275. ================
  5276. */
  5277. float idPhysics_AF::GetMass( int id ) const {
  5278. if ( id >= 0 && id < bodies.Num() ) {
  5279. return bodies[id]->mass;
  5280. }
  5281. return totalMass;
  5282. }
  5283. /*
  5284. ================
  5285. idPhysics_AF::SetContents
  5286. ================
  5287. */
  5288. void idPhysics_AF::SetContents( int contents, int id ) {
  5289. int i;
  5290. if ( id >= 0 && id < bodies.Num() ) {
  5291. bodies[id]->GetClipModel()->SetContents( contents );
  5292. }
  5293. else {
  5294. for ( i = 0; i < bodies.Num(); i++ ) {
  5295. bodies[i]->GetClipModel()->SetContents( contents );
  5296. }
  5297. }
  5298. }
  5299. /*
  5300. ================
  5301. idPhysics_AF::GetContents
  5302. ================
  5303. */
  5304. int idPhysics_AF::GetContents( int id ) const {
  5305. int i, contents;
  5306. if ( id >= 0 && id < bodies.Num() ) {
  5307. return bodies[id]->GetClipModel()->GetContents();
  5308. }
  5309. else {
  5310. contents = 0;
  5311. for ( i = 0; i < bodies.Num(); i++ ) {
  5312. contents |= bodies[i]->GetClipModel()->GetContents();
  5313. }
  5314. return contents;
  5315. }
  5316. }
  5317. /*
  5318. ================
  5319. idPhysics_AF::GetBounds
  5320. ================
  5321. */
  5322. const idBounds &idPhysics_AF::GetBounds( int id ) const {
  5323. int i;
  5324. static idBounds relBounds;
  5325. if ( id >= 0 && id < bodies.Num() ) {
  5326. return bodies[id]->GetClipModel()->GetBounds();
  5327. }
  5328. else if ( !bodies.Num() ) {
  5329. relBounds.Zero();
  5330. return relBounds;
  5331. }
  5332. else {
  5333. relBounds = bodies[0]->GetClipModel()->GetBounds();
  5334. for ( i = 1; i < bodies.Num(); i++ ) {
  5335. idBounds bounds;
  5336. idVec3 origin = ( bodies[i]->GetWorldOrigin() - bodies[0]->GetWorldOrigin() ) * bodies[0]->GetWorldAxis().Transpose();
  5337. idMat3 axis = bodies[i]->GetWorldAxis() * bodies[0]->GetWorldAxis().Transpose();
  5338. bounds.FromTransformedBounds( bodies[i]->GetClipModel()->GetBounds(), origin, axis );
  5339. relBounds += bounds;
  5340. }
  5341. return relBounds;
  5342. }
  5343. }
  5344. /*
  5345. ================
  5346. idPhysics_AF::GetAbsBounds
  5347. ================
  5348. */
  5349. const idBounds &idPhysics_AF::GetAbsBounds( int id ) const {
  5350. int i;
  5351. static idBounds absBounds;
  5352. if ( id >= 0 && id < bodies.Num() ) {
  5353. return bodies[id]->GetClipModel()->GetAbsBounds();
  5354. }
  5355. else if ( !bodies.Num() ) {
  5356. absBounds.Zero();
  5357. return absBounds;
  5358. }
  5359. else {
  5360. absBounds = bodies[0]->GetClipModel()->GetAbsBounds();
  5361. for ( i = 1; i < bodies.Num(); i++ ) {
  5362. absBounds += bodies[i]->GetClipModel()->GetAbsBounds();
  5363. }
  5364. return absBounds;
  5365. }
  5366. }
  5367. /*
  5368. ================
  5369. idPhysics_AF::Evaluate
  5370. ================
  5371. */
  5372. bool idPhysics_AF::Evaluate( int timeStepMSec, int endTimeMSec ) {
  5373. float timeStep;
  5374. if ( timeScaleRampStart < MS2SEC( endTimeMSec ) && timeScaleRampEnd > MS2SEC( endTimeMSec ) ) {
  5375. timeStep = MS2SEC( timeStepMSec ) * ( MS2SEC( endTimeMSec ) - timeScaleRampStart ) / ( timeScaleRampEnd - timeScaleRampStart );
  5376. } else if ( af_timeScale.GetFloat() != 1.0f ) {
  5377. timeStep = MS2SEC( timeStepMSec ) * af_timeScale.GetFloat();
  5378. } else {
  5379. timeStep = MS2SEC( timeStepMSec ) * timeScale;
  5380. }
  5381. current.lastTimeStep = timeStep;
  5382. // if the articulated figure changed
  5383. if ( changedAF || ( linearTime != af_useLinearTime.GetBool() ) ) {
  5384. BuildTrees();
  5385. changedAF = false;
  5386. linearTime = af_useLinearTime.GetBool();
  5387. }
  5388. // get the new master position
  5389. if ( masterBody ) {
  5390. idVec3 masterOrigin;
  5391. idMat3 masterAxis;
  5392. self->GetMasterPosition( masterOrigin, masterAxis );
  5393. if ( current.atRest >= 0 && ( masterBody->current->worldOrigin != masterOrigin || masterBody->current->worldAxis != masterAxis ) ) {
  5394. Activate();
  5395. }
  5396. masterBody->current->worldOrigin = masterOrigin;
  5397. masterBody->current->worldAxis = masterAxis;
  5398. }
  5399. // if the simulation is suspended because the figure is at rest
  5400. if ( current.atRest >= 0 || timeStep <= 0.0f ) {
  5401. DebugDraw();
  5402. return false;
  5403. }
  5404. // move the af velocity into the frame of a pusher
  5405. AddPushVelocity( -current.pushVelocity );
  5406. #ifdef AF_TIMINGS
  5407. timer_total.Start();
  5408. #endif
  5409. #ifdef AF_TIMINGS
  5410. timer_collision.Start();
  5411. #endif
  5412. // evaluate contacts
  5413. EvaluateContacts();
  5414. // setup contact constraints
  5415. SetupContactConstraints();
  5416. #ifdef AF_TIMINGS
  5417. timer_collision.Stop();
  5418. #endif
  5419. // evaluate constraint equations
  5420. EvaluateConstraints( timeStep );
  5421. // apply friction
  5422. ApplyFriction( timeStep, endTimeMSec );
  5423. // add frame constraints
  5424. AddFrameConstraints();
  5425. #ifdef AF_TIMINGS
  5426. int i, numPrimary = 0, numAuxiliary = 0;
  5427. for ( i = 0; i < primaryConstraints.Num(); i++ ) {
  5428. numPrimary += primaryConstraints[i]->J1.GetNumRows();
  5429. }
  5430. for ( i = 0; i < auxiliaryConstraints.Num(); i++ ) {
  5431. numAuxiliary += auxiliaryConstraints[i]->J1.GetNumRows();
  5432. }
  5433. timer_pc.Start();
  5434. #endif
  5435. // factor matrices for primary constraints
  5436. PrimaryFactor();
  5437. // calculate forces on bodies after applying primary constraints
  5438. PrimaryForces( timeStep );
  5439. #ifdef AF_TIMINGS
  5440. timer_pc.Stop();
  5441. timer_ac.Start();
  5442. #endif
  5443. // calculate and apply auxiliary constraint forces
  5444. AuxiliaryForces( timeStep );
  5445. #ifdef AF_TIMINGS
  5446. timer_ac.Stop();
  5447. #endif
  5448. // evolve current state to next state
  5449. Evolve( timeStep );
  5450. // debug graphics
  5451. DebugDraw();
  5452. // clear external forces on all bodies
  5453. ClearExternalForce();
  5454. // apply contact force to other entities
  5455. ApplyContactForces();
  5456. // remove all frame constraints
  5457. RemoveFrameConstraints();
  5458. #ifdef AF_TIMINGS
  5459. timer_collision.Start();
  5460. #endif
  5461. // check for collisions between current and next state
  5462. CheckForCollisions( timeStep );
  5463. #ifdef AF_TIMINGS
  5464. timer_collision.Stop();
  5465. #endif
  5466. // swap the current and next state
  5467. SwapStates();
  5468. // make sure all clip models are disabled in case they were enabled for self collision
  5469. if ( selfCollision && !af_skipSelfCollision.GetBool() ) {
  5470. DisableClip();
  5471. }
  5472. // apply collision impulses
  5473. if ( ApplyCollisions( timeStep ) ) {
  5474. current.atRest = gameLocal.time;
  5475. comeToRest = true;
  5476. }
  5477. // test if the simulation can be suspended because the whole figure is at rest
  5478. if ( comeToRest && TestIfAtRest( timeStep ) ) {
  5479. Rest();
  5480. } else {
  5481. ActivateContactEntities();
  5482. }
  5483. // add gravitational force
  5484. AddGravity();
  5485. // move the af velocity back into the world frame
  5486. AddPushVelocity( current.pushVelocity );
  5487. current.pushVelocity.Zero();
  5488. if ( IsOutsideWorld() ) {
  5489. gameLocal.Warning( "articulated figure moved outside world bounds for entity '%s' type '%s' at (%s)",
  5490. self->name.c_str(), self->GetType()->classname, bodies[0]->current->worldOrigin.ToString(0) );
  5491. Rest();
  5492. }
  5493. #ifdef AF_TIMINGS
  5494. timer_total.Stop();
  5495. if ( af_showTimings.GetInteger() == 1 ) {
  5496. gameLocal.Printf( "%12s: t %1.4f pc %2d, %1.4f ac %2d %1.4f lcp %1.4f cd %1.4f\n",
  5497. self->name.c_str(),
  5498. timer_total.Milliseconds(),
  5499. numPrimary, timer_pc.Milliseconds(),
  5500. numAuxiliary, timer_ac.Milliseconds() - timer_lcp.Milliseconds(),
  5501. timer_lcp.Milliseconds(), timer_collision.Milliseconds() );
  5502. }
  5503. else if ( af_showTimings.GetInteger() == 2 ) {
  5504. numArticulatedFigures++;
  5505. if ( endTimeMSec > lastTimerReset ) {
  5506. gameLocal.Printf( "af %d: t %1.4f pc %2d, %1.4f ac %2d %1.4f lcp %1.4f cd %1.4f\n",
  5507. numArticulatedFigures,
  5508. timer_total.Milliseconds(),
  5509. numPrimary, timer_pc.Milliseconds(),
  5510. numAuxiliary, timer_ac.Milliseconds() - timer_lcp.Milliseconds(),
  5511. timer_lcp.Milliseconds(), timer_collision.Milliseconds() );
  5512. }
  5513. }
  5514. if ( endTimeMSec > lastTimerReset ) {
  5515. lastTimerReset = endTimeMSec;
  5516. numArticulatedFigures = 0;
  5517. timer_total.Clear();
  5518. timer_pc.Clear();
  5519. timer_ac.Clear();
  5520. timer_collision.Clear();
  5521. timer_lcp.Clear();
  5522. }
  5523. #endif
  5524. return true;
  5525. }
  5526. /*
  5527. ================
  5528. idPhysics_AF::UpdateTime
  5529. ================
  5530. */
  5531. void idPhysics_AF::UpdateTime( int endTimeMSec ) {
  5532. }
  5533. /*
  5534. ================
  5535. idPhysics_AF::GetTime
  5536. ================
  5537. */
  5538. int idPhysics_AF::GetTime( void ) const {
  5539. return gameLocal.time;
  5540. }
  5541. /*
  5542. ================
  5543. DrawTraceModelSilhouette
  5544. ================
  5545. */
  5546. void DrawTraceModelSilhouette( const idVec3 &projectionOrigin, const idClipModel *clipModel ) {
  5547. int i, numSilEdges;
  5548. int silEdges[MAX_TRACEMODEL_EDGES];
  5549. idVec3 v1, v2;
  5550. const idTraceModel *trm = clipModel->GetTraceModel();
  5551. const idVec3 &origin = clipModel->GetOrigin();
  5552. const idMat3 &axis = clipModel->GetAxis();
  5553. numSilEdges = trm->GetProjectionSilhouetteEdges( ( projectionOrigin - origin ) * axis.Transpose(), silEdges );
  5554. for ( i = 0; i < numSilEdges; i++ ) {
  5555. v1 = trm->verts[ trm->edges[ abs(silEdges[i]) ].v[ INTSIGNBITSET( silEdges[i] ) ] ];
  5556. v2 = trm->verts[ trm->edges[ abs(silEdges[i]) ].v[ INTSIGNBITNOTSET( silEdges[i] ) ] ];
  5557. gameRenderWorld->DebugArrow( colorRed, origin + v1 * axis, origin + v2 * axis, 1 );
  5558. }
  5559. }
  5560. /*
  5561. ================
  5562. idPhysics_AF::DebugDraw
  5563. ================
  5564. */
  5565. void idPhysics_AF::DebugDraw( void ) {
  5566. int i;
  5567. idAFBody *body, *highlightBody = NULL, *constrainedBody1 = NULL, *constrainedBody2 = NULL;
  5568. idAFConstraint *constraint;
  5569. idVec3 center;
  5570. idMat3 axis;
  5571. if ( af_highlightConstraint.GetString()[0] ) {
  5572. constraint = GetConstraint( af_highlightConstraint.GetString() );
  5573. if ( constraint ) {
  5574. constraint->GetCenter( center );
  5575. axis = gameLocal.GetLocalPlayer()->viewAngles.ToMat3();
  5576. gameRenderWorld->DebugCone( colorYellow, center, (axis[2] - axis[1]) * 4.0f, 0.0f, 1.0f, 0 );
  5577. if ( af_showConstrainedBodies.GetBool() ) {
  5578. cvarSystem->SetCVarString( "cm_drawColor", colorCyan.ToString( 0 ) );
  5579. constrainedBody1 = constraint->body1;
  5580. if ( constrainedBody1 ) {
  5581. collisionModelManager->DrawModel( constrainedBody1->clipModel->Handle(), constrainedBody1->clipModel->GetOrigin(),
  5582. constrainedBody1->clipModel->GetAxis(), vec3_origin, 0.0f );
  5583. }
  5584. cvarSystem->SetCVarString( "cm_drawColor", colorBlue.ToString( 0 ) );
  5585. constrainedBody2 = constraint->body2;
  5586. if ( constrainedBody2 ) {
  5587. collisionModelManager->DrawModel( constrainedBody2->clipModel->Handle(), constrainedBody2->clipModel->GetOrigin(),
  5588. constrainedBody2->clipModel->GetAxis(), vec3_origin, 0.0f );
  5589. }
  5590. cvarSystem->SetCVarString( "cm_drawColor", colorRed.ToString( 0 ) );
  5591. }
  5592. }
  5593. }
  5594. if ( af_highlightBody.GetString()[0] ) {
  5595. highlightBody = GetBody( af_highlightBody.GetString() );
  5596. if ( highlightBody ) {
  5597. cvarSystem->SetCVarString( "cm_drawColor", colorYellow.ToString( 0 ) );
  5598. collisionModelManager->DrawModel( highlightBody->clipModel->Handle(), highlightBody->clipModel->GetOrigin(),
  5599. highlightBody->clipModel->GetAxis(), vec3_origin, 0.0f );
  5600. cvarSystem->SetCVarString( "cm_drawColor", colorRed.ToString( 0 ) );
  5601. }
  5602. }
  5603. if ( af_showBodies.GetBool() ) {
  5604. for ( i = 0; i < bodies.Num(); i++ ) {
  5605. body = bodies[i];
  5606. if ( body == constrainedBody1 || body == constrainedBody2 ) {
  5607. continue;
  5608. }
  5609. if ( body == highlightBody ) {
  5610. continue;
  5611. }
  5612. collisionModelManager->DrawModel( body->clipModel->Handle(), body->clipModel->GetOrigin(),
  5613. body->clipModel->GetAxis(), vec3_origin, 0.0f );
  5614. //DrawTraceModelSilhouette( gameLocal.GetLocalPlayer()->GetEyePosition(), body->clipModel );
  5615. }
  5616. }
  5617. if ( af_showBodyNames.GetBool() ) {
  5618. for ( i = 0; i < bodies.Num(); i++ ) {
  5619. body = bodies[i];
  5620. gameRenderWorld->DrawText( body->GetName().c_str(), body->GetWorldOrigin(), 0.08f, colorCyan, gameLocal.GetLocalPlayer()->viewAngles.ToMat3(), 1 );
  5621. }
  5622. }
  5623. if ( af_showMass.GetBool() ) {
  5624. for ( i = 0; i < bodies.Num(); i++ ) {
  5625. body = bodies[i];
  5626. gameRenderWorld->DrawText( va( "\n%1.2f", 1.0f / body->GetInverseMass() ), body->GetWorldOrigin(), 0.08f, colorCyan, gameLocal.GetLocalPlayer()->viewAngles.ToMat3(), 1 );
  5627. }
  5628. }
  5629. if ( af_showTotalMass.GetBool() ) {
  5630. axis = gameLocal.GetLocalPlayer()->viewAngles.ToMat3();
  5631. gameRenderWorld->DrawText( va( "\n%1.2f", totalMass ), bodies[0]->GetWorldOrigin() + axis[2] * 8.0f, 0.15f, colorCyan, axis, 1 );
  5632. }
  5633. if ( af_showInertia.GetBool() ) {
  5634. for ( i = 0; i < bodies.Num(); i++ ) {
  5635. body = bodies[i];
  5636. idMat3 &I = body->inertiaTensor;
  5637. gameRenderWorld->DrawText( va( "\n\n\n( %.1f %.1f %.1f )\n( %.1f %.1f %.1f )\n( %.1f %.1f %.1f )",
  5638. I[0].x, I[0].y, I[0].z,
  5639. I[1].x, I[1].y, I[1].z,
  5640. I[2].x, I[2].y, I[2].z ),
  5641. body->GetWorldOrigin(), 0.05f, colorCyan, gameLocal.GetLocalPlayer()->viewAngles.ToMat3(), 1 );
  5642. }
  5643. }
  5644. if ( af_showVelocity.GetBool() ) {
  5645. for ( i = 0; i < bodies.Num(); i++ ) {
  5646. DrawVelocity( bodies[i]->clipModel->GetId(), 0.1f, 4.0f );
  5647. }
  5648. }
  5649. if ( af_showConstraints.GetBool() ) {
  5650. for ( i = 0; i < primaryConstraints.Num(); i++ ) {
  5651. constraint = primaryConstraints[i];
  5652. constraint->DebugDraw();
  5653. }
  5654. if ( !af_showPrimaryOnly.GetBool() ) {
  5655. for ( i = 0; i < auxiliaryConstraints.Num(); i++ ) {
  5656. constraint = auxiliaryConstraints[i];
  5657. constraint->DebugDraw();
  5658. }
  5659. }
  5660. }
  5661. if ( af_showConstraintNames.GetBool() ) {
  5662. for ( i = 0; i < primaryConstraints.Num(); i++ ) {
  5663. constraint = primaryConstraints[i];
  5664. constraint->GetCenter( center );
  5665. gameRenderWorld->DrawText( constraint->GetName().c_str(), center, 0.08f, colorCyan, gameLocal.GetLocalPlayer()->viewAngles.ToMat3(), 1 );
  5666. }
  5667. if ( !af_showPrimaryOnly.GetBool() ) {
  5668. for ( i = 0; i < auxiliaryConstraints.Num(); i++ ) {
  5669. constraint = auxiliaryConstraints[i];
  5670. constraint->GetCenter( center );
  5671. gameRenderWorld->DrawText( constraint->GetName().c_str(), center, 0.08f, colorCyan, gameLocal.GetLocalPlayer()->viewAngles.ToMat3(), 1 );
  5672. }
  5673. }
  5674. }
  5675. if ( af_showTrees.GetBool() || ( af_showActive.GetBool() && current.atRest < 0 ) ) {
  5676. for ( i = 0; i < trees.Num(); i++ ) {
  5677. trees[i]->DebugDraw( idStr::ColorForIndex( i+3 ) );
  5678. }
  5679. }
  5680. }
  5681. /*
  5682. ================
  5683. idPhysics_AF::idPhysics_AF
  5684. ================
  5685. */
  5686. idPhysics_AF::idPhysics_AF( void ) {
  5687. trees.Clear();
  5688. bodies.Clear();
  5689. constraints.Clear();
  5690. primaryConstraints.Clear();
  5691. auxiliaryConstraints.Clear();
  5692. frameConstraints.Clear();
  5693. contacts.Clear();
  5694. collisions.Clear();
  5695. changedAF = true;
  5696. masterBody = NULL;
  5697. lcp = idLCP::AllocSymmetric();
  5698. memset( &current, 0, sizeof( current ) );
  5699. current.atRest = -1;
  5700. current.lastTimeStep = USERCMD_MSEC;
  5701. saved = current;
  5702. linearFriction = 0.005f;
  5703. angularFriction = 0.005f;
  5704. contactFriction = 0.8f;
  5705. bouncyness = 0.4f;
  5706. totalMass = 0.0f;
  5707. forceTotalMass = -1.0f;
  5708. suspendVelocity.Set( SUSPEND_LINEAR_VELOCITY, SUSPEND_ANGULAR_VELOCITY );
  5709. suspendAcceleration.Set( SUSPEND_LINEAR_ACCELERATION, SUSPEND_LINEAR_ACCELERATION );
  5710. noMoveTime = NO_MOVE_TIME;
  5711. noMoveTranslation = NO_MOVE_TRANSLATION_TOLERANCE;
  5712. noMoveRotation = NO_MOVE_ROTATION_TOLERANCE;
  5713. minMoveTime = MIN_MOVE_TIME;
  5714. maxMoveTime = MAX_MOVE_TIME;
  5715. impulseThreshold = IMPULSE_THRESHOLD;
  5716. timeScale = 1.0f;
  5717. timeScaleRampStart = 0.0f;
  5718. timeScaleRampEnd = 0.0f;
  5719. jointFrictionScale = 0.0f;
  5720. jointFrictionDent = 0.0f;
  5721. jointFrictionDentStart = 0.0f;
  5722. jointFrictionDentEnd = 0.0f;
  5723. jointFrictionDentScale = 0.0f;
  5724. contactFrictionScale = 0.0f;
  5725. contactFrictionDent = 0.0f;
  5726. contactFrictionDentStart = 0.0f;
  5727. contactFrictionDentEnd = 0.0f;
  5728. contactFrictionDentScale = 0.0f;
  5729. enableCollision = true;
  5730. selfCollision = true;
  5731. comeToRest = true;
  5732. linearTime = true;
  5733. noImpact = false;
  5734. worldConstraintsLocked = false;
  5735. forcePushable = false;
  5736. #ifdef AF_TIMINGS
  5737. lastTimerReset = 0;
  5738. #endif
  5739. }
  5740. /*
  5741. ================
  5742. idPhysics_AF::~idPhysics_AF
  5743. ================
  5744. */
  5745. idPhysics_AF::~idPhysics_AF( void ) {
  5746. int i;
  5747. trees.DeleteContents( true );
  5748. for ( i = 0; i < bodies.Num(); i++ ) {
  5749. delete bodies[i];
  5750. }
  5751. for ( i = 0; i < constraints.Num(); i++ ) {
  5752. delete constraints[i];
  5753. }
  5754. contactConstraints.SetNum( contactConstraints.NumAllocated(), false );
  5755. for ( i = 0; i < contactConstraints.NumAllocated(); i++ ) {
  5756. delete contactConstraints[i];
  5757. }
  5758. delete lcp;
  5759. if ( masterBody ) {
  5760. delete masterBody;
  5761. }
  5762. }
  5763. /*
  5764. ================
  5765. idPhysics_AF_SavePState
  5766. ================
  5767. */
  5768. void idPhysics_AF_SavePState( idSaveGame *saveFile, const AFPState_t &state ) {
  5769. saveFile->WriteInt( state.atRest );
  5770. saveFile->WriteFloat( state.noMoveTime );
  5771. saveFile->WriteFloat( state.activateTime );
  5772. saveFile->WriteFloat( state.lastTimeStep );
  5773. saveFile->WriteVec6( state.pushVelocity );
  5774. }
  5775. /*
  5776. ================
  5777. idPhysics_AF_RestorePState
  5778. ================
  5779. */
  5780. void idPhysics_AF_RestorePState( idRestoreGame *saveFile, AFPState_t &state ) {
  5781. saveFile->ReadInt( state.atRest );
  5782. saveFile->ReadFloat( state.noMoveTime );
  5783. saveFile->ReadFloat( state.activateTime );
  5784. saveFile->ReadFloat( state.lastTimeStep );
  5785. saveFile->ReadVec6( state.pushVelocity );
  5786. }
  5787. /*
  5788. ================
  5789. idPhysics_AF::Save
  5790. ================
  5791. */
  5792. void idPhysics_AF::Save( idSaveGame *saveFile ) const {
  5793. int i;
  5794. // the articulated figure structure is handled by the owner
  5795. idPhysics_AF_SavePState( saveFile, current );
  5796. idPhysics_AF_SavePState( saveFile, saved );
  5797. saveFile->WriteInt( bodies.Num() );
  5798. for ( i = 0; i < bodies.Num(); i++ ) {
  5799. bodies[i]->Save( saveFile );
  5800. }
  5801. if ( masterBody ) {
  5802. saveFile->WriteBool( true );
  5803. masterBody->Save( saveFile );
  5804. } else {
  5805. saveFile->WriteBool( false );
  5806. }
  5807. saveFile->WriteInt( constraints.Num() );
  5808. for ( i = 0; i < constraints.Num(); i++ ) {
  5809. constraints[i]->Save( saveFile );
  5810. }
  5811. saveFile->WriteBool( changedAF );
  5812. saveFile->WriteFloat( linearFriction );
  5813. saveFile->WriteFloat( angularFriction );
  5814. saveFile->WriteFloat( contactFriction );
  5815. saveFile->WriteFloat( bouncyness );
  5816. saveFile->WriteFloat( totalMass );
  5817. saveFile->WriteFloat( forceTotalMass );
  5818. saveFile->WriteVec2( suspendVelocity );
  5819. saveFile->WriteVec2( suspendAcceleration );
  5820. saveFile->WriteFloat( noMoveTime );
  5821. saveFile->WriteFloat( noMoveTranslation );
  5822. saveFile->WriteFloat( noMoveRotation );
  5823. saveFile->WriteFloat( minMoveTime );
  5824. saveFile->WriteFloat( maxMoveTime );
  5825. saveFile->WriteFloat( impulseThreshold );
  5826. saveFile->WriteFloat( timeScale );
  5827. saveFile->WriteFloat( timeScaleRampStart );
  5828. saveFile->WriteFloat( timeScaleRampEnd );
  5829. saveFile->WriteFloat( jointFrictionScale );
  5830. saveFile->WriteFloat( jointFrictionDent );
  5831. saveFile->WriteFloat( jointFrictionDentStart );
  5832. saveFile->WriteFloat( jointFrictionDentEnd );
  5833. saveFile->WriteFloat( jointFrictionDentScale );
  5834. saveFile->WriteFloat( contactFrictionScale );
  5835. saveFile->WriteFloat( contactFrictionDent );
  5836. saveFile->WriteFloat( contactFrictionDentStart );
  5837. saveFile->WriteFloat( contactFrictionDentEnd );
  5838. saveFile->WriteFloat( contactFrictionDentScale );
  5839. saveFile->WriteBool( enableCollision );
  5840. saveFile->WriteBool( selfCollision );
  5841. saveFile->WriteBool( comeToRest );
  5842. saveFile->WriteBool( linearTime );
  5843. saveFile->WriteBool( noImpact );
  5844. saveFile->WriteBool( worldConstraintsLocked );
  5845. saveFile->WriteBool( forcePushable );
  5846. }
  5847. /*
  5848. ================
  5849. idPhysics_AF::Restore
  5850. ================
  5851. */
  5852. void idPhysics_AF::Restore( idRestoreGame *saveFile ) {
  5853. int i, num;
  5854. bool hasMaster;
  5855. // the articulated figure structure should have already been restored
  5856. idPhysics_AF_RestorePState( saveFile, current );
  5857. idPhysics_AF_RestorePState( saveFile, saved );
  5858. saveFile->ReadInt( num );
  5859. assert( num == bodies.Num() );
  5860. for ( i = 0; i < bodies.Num(); i++ ) {
  5861. bodies[i]->Restore( saveFile );
  5862. }
  5863. saveFile->ReadBool( hasMaster );
  5864. if ( hasMaster ) {
  5865. masterBody = new idAFBody();
  5866. masterBody->Restore( saveFile );
  5867. }
  5868. saveFile->ReadInt( num );
  5869. assert( num == constraints.Num() );
  5870. for ( i = 0; i < constraints.Num(); i++ ) {
  5871. constraints[i]->Restore( saveFile );
  5872. }
  5873. saveFile->ReadBool( changedAF );
  5874. saveFile->ReadFloat( linearFriction );
  5875. saveFile->ReadFloat( angularFriction );
  5876. saveFile->ReadFloat( contactFriction );
  5877. saveFile->ReadFloat( bouncyness );
  5878. saveFile->ReadFloat( totalMass );
  5879. saveFile->ReadFloat( forceTotalMass );
  5880. saveFile->ReadVec2( suspendVelocity );
  5881. saveFile->ReadVec2( suspendAcceleration );
  5882. saveFile->ReadFloat( noMoveTime );
  5883. saveFile->ReadFloat( noMoveTranslation );
  5884. saveFile->ReadFloat( noMoveRotation );
  5885. saveFile->ReadFloat( minMoveTime );
  5886. saveFile->ReadFloat( maxMoveTime );
  5887. saveFile->ReadFloat( impulseThreshold );
  5888. saveFile->ReadFloat( timeScale );
  5889. saveFile->ReadFloat( timeScaleRampStart );
  5890. saveFile->ReadFloat( timeScaleRampEnd );
  5891. saveFile->ReadFloat( jointFrictionScale );
  5892. saveFile->ReadFloat( jointFrictionDent );
  5893. saveFile->ReadFloat( jointFrictionDentStart );
  5894. saveFile->ReadFloat( jointFrictionDentEnd );
  5895. saveFile->ReadFloat( jointFrictionDentScale );
  5896. saveFile->ReadFloat( contactFrictionScale );
  5897. saveFile->ReadFloat( contactFrictionDent );
  5898. saveFile->ReadFloat( contactFrictionDentStart );
  5899. saveFile->ReadFloat( contactFrictionDentEnd );
  5900. saveFile->ReadFloat( contactFrictionDentScale );
  5901. saveFile->ReadBool( enableCollision );
  5902. saveFile->ReadBool( selfCollision );
  5903. saveFile->ReadBool( comeToRest );
  5904. saveFile->ReadBool( linearTime );
  5905. saveFile->ReadBool( noImpact );
  5906. saveFile->ReadBool( worldConstraintsLocked );
  5907. saveFile->ReadBool( forcePushable );
  5908. changedAF = true;
  5909. UpdateClipModels();
  5910. }
  5911. /*
  5912. ================
  5913. idPhysics_AF::IsClosedLoop
  5914. ================
  5915. */
  5916. bool idPhysics_AF::IsClosedLoop( const idAFBody *body1, const idAFBody *body2 ) const {
  5917. const idAFBody *b1, *b2;
  5918. for ( b1 = body1; b1->parent; b1 = b1->parent ) {
  5919. }
  5920. for ( b2 = body2; b2->parent; b2 = b2->parent ) {
  5921. }
  5922. return ( b1 == b2 );
  5923. }
  5924. /*
  5925. ================
  5926. idPhysics_AF::BuildTrees
  5927. ================
  5928. */
  5929. void idPhysics_AF::BuildTrees( void ) {
  5930. int i;
  5931. float scale;
  5932. idAFBody *b;
  5933. idAFConstraint *c;
  5934. idAFTree *tree;
  5935. primaryConstraints.Clear();
  5936. auxiliaryConstraints.Clear();
  5937. trees.DeleteContents( true );
  5938. totalMass = 0.0f;
  5939. for ( i = 0; i < bodies.Num(); i++ ) {
  5940. b = bodies[i];
  5941. b->parent = NULL;
  5942. b->primaryConstraint = NULL;
  5943. b->constraints.SetNum( 0, false );
  5944. b->children.Clear();
  5945. b->tree = NULL;
  5946. totalMass += b->mass;
  5947. }
  5948. if ( forceTotalMass > 0.0f ) {
  5949. scale = forceTotalMass / totalMass;
  5950. for ( i = 0; i < bodies.Num(); i++ ) {
  5951. b = bodies[i];
  5952. b->mass *= scale;
  5953. b->invMass = 1.0f / b->mass;
  5954. b->inertiaTensor *= scale;
  5955. b->inverseInertiaTensor = b->inertiaTensor.Inverse();
  5956. }
  5957. totalMass = forceTotalMass;
  5958. }
  5959. if ( af_useLinearTime.GetBool() ) {
  5960. for ( i = 0; i < constraints.Num(); i++ ) {
  5961. c = constraints[i];
  5962. c->body1->constraints.Append( c );
  5963. if ( c->body2 ) {
  5964. c->body2->constraints.Append( c );
  5965. }
  5966. // only bilateral constraints between two non-world bodies that do not
  5967. // create loops can be used as primary constraints
  5968. if ( !c->body1->primaryConstraint && c->fl.allowPrimary && c->body2 != NULL && !IsClosedLoop( c->body1, c->body2 ) ) {
  5969. c->body1->primaryConstraint = c;
  5970. c->body1->parent = c->body2;
  5971. c->body2->children.Append( c->body1 );
  5972. c->fl.isPrimary = true;
  5973. c->firstIndex = 0;
  5974. primaryConstraints.Append( c );
  5975. } else {
  5976. c->fl.isPrimary = false;
  5977. auxiliaryConstraints.Append( c );
  5978. }
  5979. }
  5980. // create trees for all parent bodies
  5981. for ( i = 0; i < bodies.Num(); i++ ) {
  5982. if ( !bodies[i]->parent ) {
  5983. tree = new idAFTree();
  5984. tree->sortedBodies.Clear();
  5985. tree->sortedBodies.Append( bodies[i] );
  5986. bodies[i]->tree = tree;
  5987. trees.Append( tree );
  5988. }
  5989. }
  5990. // add each child body to the appropriate tree
  5991. for ( i = 0; i < bodies.Num(); i++ ) {
  5992. if ( bodies[i]->parent ) {
  5993. for ( b = bodies[i]->parent; !b->tree; b = b->parent ) {
  5994. }
  5995. b->tree->sortedBodies.Append( bodies[i] );
  5996. bodies[i]->tree = b->tree;
  5997. }
  5998. }
  5999. if ( trees.Num() > 1 ) {
  6000. gameLocal.Warning( "Articulated figure has multiple seperate tree structures for entity '%s' type '%s'.",
  6001. self->name.c_str(), self->GetType()->classname );
  6002. }
  6003. // sort bodies in each tree to make sure parents come first
  6004. for ( i = 0; i < trees.Num(); i++ ) {
  6005. trees[i]->SortBodies();
  6006. }
  6007. } else {
  6008. // create a tree for each body
  6009. for ( i = 0; i < bodies.Num(); i++ ) {
  6010. tree = new idAFTree();
  6011. tree->sortedBodies.Clear();
  6012. tree->sortedBodies.Append( bodies[i] );
  6013. bodies[i]->tree = tree;
  6014. trees.Append( tree );
  6015. }
  6016. for ( i = 0; i < constraints.Num(); i++ ) {
  6017. c = constraints[i];
  6018. c->body1->constraints.Append( c );
  6019. if ( c->body2 ) {
  6020. c->body2->constraints.Append( c );
  6021. }
  6022. c->fl.isPrimary = false;
  6023. auxiliaryConstraints.Append( c );
  6024. }
  6025. }
  6026. }
  6027. /*
  6028. ================
  6029. idPhysics_AF::AddBody
  6030. bodies get an id in the order they are added starting at zero
  6031. as such the first body added will get id zero
  6032. ================
  6033. */
  6034. int idPhysics_AF::AddBody( idAFBody *body ) {
  6035. int id = 0;
  6036. if ( !body->clipModel ) {
  6037. gameLocal.Error( "idPhysics_AF::AddBody: body '%s' has no clip model.", body->name.c_str() );
  6038. }
  6039. if ( bodies.Find( body ) ) {
  6040. gameLocal.Error( "idPhysics_AF::AddBody: body '%s' added twice.", body->name.c_str() );
  6041. }
  6042. if ( GetBody( body->name ) ) {
  6043. gameLocal.Error( "idPhysics_AF::AddBody: a body with the name '%s' already exists.", body->name.c_str() );
  6044. }
  6045. id = bodies.Num();
  6046. body->clipModel->SetId( id );
  6047. if ( body->linearFriction < 0.0f ) {
  6048. body->linearFriction = linearFriction;
  6049. body->angularFriction = angularFriction;
  6050. body->contactFriction = contactFriction;
  6051. }
  6052. if ( body->bouncyness < 0.0f ) {
  6053. body->bouncyness = bouncyness;
  6054. }
  6055. if ( !body->fl.clipMaskSet ) {
  6056. body->clipMask = clipMask;
  6057. }
  6058. bodies.Append( body );
  6059. changedAF = true;
  6060. return id;
  6061. }
  6062. /*
  6063. ================
  6064. idPhysics_AF::AddConstraint
  6065. ================
  6066. */
  6067. void idPhysics_AF::AddConstraint( idAFConstraint *constraint ) {
  6068. if ( constraints.Find( constraint ) ) {
  6069. gameLocal.Error( "idPhysics_AF::AddConstraint: constraint '%s' added twice.", constraint->name.c_str() );
  6070. }
  6071. if ( GetConstraint( constraint->name ) ) {
  6072. gameLocal.Error( "idPhysics_AF::AddConstraint: a constraint with the name '%s' already exists.", constraint->name.c_str() );
  6073. }
  6074. if ( !constraint->body1 ) {
  6075. gameLocal.Error( "idPhysics_AF::AddConstraint: body1 == NULL on constraint '%s'.", constraint->name.c_str() );
  6076. }
  6077. if ( !bodies.Find( constraint->body1 ) ) {
  6078. gameLocal.Error( "idPhysics_AF::AddConstraint: body1 of constraint '%s' is not part of the articulated figure.", constraint->name.c_str() );
  6079. }
  6080. if ( constraint->body2 && !bodies.Find( constraint->body2 ) ) {
  6081. gameLocal.Error( "idPhysics_AF::AddConstraint: body2 of constraint '%s' is not part of the articulated figure.", constraint->name.c_str() );
  6082. }
  6083. if ( constraint->body1 == constraint->body2 ) {
  6084. gameLocal.Error( "idPhysics_AF::AddConstraint: body1 and body2 of constraint '%s' are the same.", constraint->name.c_str() );
  6085. }
  6086. constraints.Append( constraint );
  6087. constraint->physics = this;
  6088. changedAF = true;
  6089. }
  6090. /*
  6091. ================
  6092. idPhysics_AF::AddFrameConstraint
  6093. ================
  6094. */
  6095. void idPhysics_AF::AddFrameConstraint( idAFConstraint *constraint ) {
  6096. frameConstraints.Append( constraint );
  6097. constraint->physics = this;
  6098. }
  6099. /*
  6100. ================
  6101. idPhysics_AF::ForceBodyId
  6102. ================
  6103. */
  6104. void idPhysics_AF::ForceBodyId( idAFBody *body, int newId ) {
  6105. int id;
  6106. id = bodies.FindIndex( body );
  6107. if ( id == -1 ) {
  6108. gameLocal.Error( "ForceBodyId: body '%s' is not part of the articulated figure.\n", body->name.c_str() );
  6109. }
  6110. if ( id != newId ) {
  6111. idAFBody *b = bodies[newId];
  6112. bodies[newId] = bodies[id];
  6113. bodies[id] = b;
  6114. changedAF = true;
  6115. }
  6116. }
  6117. /*
  6118. ================
  6119. idPhysics_AF::GetBodyId
  6120. ================
  6121. */
  6122. int idPhysics_AF::GetBodyId( idAFBody *body ) const {
  6123. int id;
  6124. id = bodies.FindIndex( body );
  6125. if ( id == -1 && body ) {
  6126. gameLocal.Error( "GetBodyId: body '%s' is not part of the articulated figure.\n", body->name.c_str() );
  6127. }
  6128. return id;
  6129. }
  6130. /*
  6131. ================
  6132. idPhysics_AF::GetBodyId
  6133. ================
  6134. */
  6135. int idPhysics_AF::GetBodyId( const char *bodyName ) const {
  6136. int i;
  6137. for ( i = 0; i < bodies.Num(); i++ ) {
  6138. if ( !bodies[i]->name.Icmp( bodyName ) ) {
  6139. return i;
  6140. }
  6141. }
  6142. gameLocal.Error( "GetBodyId: no body with the name '%s' is not part of the articulated figure.\n", bodyName );
  6143. return 0;
  6144. }
  6145. /*
  6146. ================
  6147. idPhysics_AF::GetConstraintId
  6148. ================
  6149. */
  6150. int idPhysics_AF::GetConstraintId( idAFConstraint *constraint ) const {
  6151. int id;
  6152. id = constraints.FindIndex( constraint );
  6153. if ( id == -1 && constraint ) {
  6154. gameLocal.Error( "GetConstraintId: constraint '%s' is not part of the articulated figure.\n", constraint->name.c_str() );
  6155. }
  6156. return id;
  6157. }
  6158. /*
  6159. ================
  6160. idPhysics_AF::GetConstraintId
  6161. ================
  6162. */
  6163. int idPhysics_AF::GetConstraintId( const char *constraintName ) const {
  6164. int i;
  6165. for ( i = 0; i < constraints.Num(); i++ ) {
  6166. if ( constraints[i]->name.Icmp( constraintName ) == 0 ) {
  6167. return i;
  6168. }
  6169. }
  6170. gameLocal.Error( "GetConstraintId: no constraint with the name '%s' is not part of the articulated figure.\n", constraintName );
  6171. return 0;
  6172. }
  6173. /*
  6174. ================
  6175. idPhysics_AF::GetNumBodies
  6176. ================
  6177. */
  6178. int idPhysics_AF::GetNumBodies( void ) const {
  6179. return bodies.Num();
  6180. }
  6181. /*
  6182. ================
  6183. idPhysics_AF::GetNumConstraints
  6184. ================
  6185. */
  6186. int idPhysics_AF::GetNumConstraints( void ) const {
  6187. return constraints.Num();
  6188. }
  6189. /*
  6190. ================
  6191. idPhysics_AF::GetBody
  6192. ================
  6193. */
  6194. idAFBody *idPhysics_AF::GetBody( const char *bodyName ) const {
  6195. int i;
  6196. for ( i = 0; i < bodies.Num(); i++ ) {
  6197. if ( !bodies[i]->name.Icmp( bodyName ) ) {
  6198. return bodies[i];
  6199. }
  6200. }
  6201. return NULL;
  6202. }
  6203. /*
  6204. ================
  6205. idPhysics_AF::GetBody
  6206. ================
  6207. */
  6208. idAFBody *idPhysics_AF::GetBody( const int id ) const {
  6209. if ( id < 0 || id >= bodies.Num() ) {
  6210. gameLocal.Error( "GetBody: no body with id %d exists\n", id );
  6211. return NULL;
  6212. }
  6213. return bodies[id];
  6214. }
  6215. /*
  6216. ================
  6217. idPhysics_AF::GetConstraint
  6218. ================
  6219. */
  6220. idAFConstraint *idPhysics_AF::GetConstraint( const char *constraintName ) const {
  6221. int i;
  6222. for ( i = 0; i < constraints.Num(); i++ ) {
  6223. if ( constraints[i]->name.Icmp( constraintName ) == 0 ) {
  6224. return constraints[i];
  6225. }
  6226. }
  6227. return NULL;
  6228. }
  6229. /*
  6230. ================
  6231. idPhysics_AF::GetConstraint
  6232. ================
  6233. */
  6234. idAFConstraint *idPhysics_AF::GetConstraint( const int id ) const {
  6235. if ( id < 0 || id >= constraints.Num() ) {
  6236. gameLocal.Error( "GetConstraint: no constraint with id %d exists\n", id );
  6237. return NULL;
  6238. }
  6239. return constraints[id];
  6240. }
  6241. /*
  6242. ================
  6243. idPhysics_AF::DeleteBody
  6244. ================
  6245. */
  6246. void idPhysics_AF::DeleteBody( const char *bodyName ) {
  6247. int i;
  6248. // find the body with the given name
  6249. for ( i = 0; i < bodies.Num(); i++ ) {
  6250. if ( !bodies[i]->name.Icmp( bodyName ) ) {
  6251. break;
  6252. }
  6253. }
  6254. if ( i >= bodies.Num() ) {
  6255. gameLocal.Warning( "DeleteBody: no body found in the articulated figure with the name '%s' for entity '%s' type '%s'.",
  6256. bodyName, self->name.c_str(), self->GetType()->classname );
  6257. return;
  6258. }
  6259. DeleteBody( i );
  6260. }
  6261. /*
  6262. ================
  6263. idPhysics_AF::DeleteBody
  6264. ================
  6265. */
  6266. void idPhysics_AF::DeleteBody( const int id ) {
  6267. int j;
  6268. if ( id < 0 || id > bodies.Num() ) {
  6269. gameLocal.Error( "DeleteBody: no body with id %d.", id );
  6270. return;
  6271. }
  6272. // remove any constraints attached to this body
  6273. for ( j = 0; j < constraints.Num(); j++ ) {
  6274. if ( constraints[j]->body1 == bodies[id] || constraints[j]->body2 == bodies[id] ) {
  6275. delete constraints[j];
  6276. constraints.RemoveIndex( j );
  6277. j--;
  6278. }
  6279. }
  6280. // remove the body
  6281. delete bodies[id];
  6282. bodies.RemoveIndex( id );
  6283. // set new body ids
  6284. for ( j = 0; j < bodies.Num(); j++ ) {
  6285. bodies[j]->clipModel->SetId( j );
  6286. }
  6287. changedAF = true;
  6288. }
  6289. /*
  6290. ================
  6291. idPhysics_AF::DeleteConstraint
  6292. ================
  6293. */
  6294. void idPhysics_AF::DeleteConstraint( const char *constraintName ) {
  6295. int i;
  6296. // find the constraint with the given name
  6297. for ( i = 0; i < constraints.Num(); i++ ) {
  6298. if ( !constraints[i]->name.Icmp( constraintName ) ) {
  6299. break;
  6300. }
  6301. }
  6302. if ( i >= constraints.Num() ) {
  6303. gameLocal.Warning( "DeleteConstraint: no constriant found in the articulated figure with the name '%s' for entity '%s' type '%s'.",
  6304. constraintName, self->name.c_str(), self->GetType()->classname );
  6305. return;
  6306. }
  6307. DeleteConstraint( i );
  6308. }
  6309. /*
  6310. ================
  6311. idPhysics_AF::DeleteConstraint
  6312. ================
  6313. */
  6314. void idPhysics_AF::DeleteConstraint( const int id ) {
  6315. if ( id < 0 || id >= constraints.Num() ) {
  6316. gameLocal.Error( "DeleteConstraint: no constraint with id %d.", id );
  6317. return;
  6318. }
  6319. // remove the constraint
  6320. delete constraints[id];
  6321. constraints.RemoveIndex( id );
  6322. changedAF = true;
  6323. }
  6324. /*
  6325. ================
  6326. idPhysics_AF::GetBodyContactConstraints
  6327. ================
  6328. */
  6329. int idPhysics_AF::GetBodyContactConstraints( const int id, idAFConstraint_Contact *contacts[], int maxContacts ) const {
  6330. int i, numContacts;
  6331. idAFBody *body;
  6332. idAFConstraint_Contact *contact;
  6333. if ( id < 0 || id >= bodies.Num() || maxContacts <= 0 ) {
  6334. return 0;
  6335. }
  6336. numContacts = 0;
  6337. body = bodies[id];
  6338. for ( i = 0; i < contactConstraints.Num(); i++ ) {
  6339. contact = contactConstraints[i];
  6340. if ( contact->body1 == body || contact->body2 == body ) {
  6341. contacts[numContacts++] = contact;
  6342. if ( numContacts >= maxContacts ) {
  6343. return numContacts;
  6344. }
  6345. }
  6346. }
  6347. return numContacts;
  6348. }
  6349. /*
  6350. ================
  6351. idPhysics_AF::SetDefaultFriction
  6352. ================
  6353. */
  6354. void idPhysics_AF::SetDefaultFriction( float linear, float angular, float contact ) {
  6355. if ( linear < 0.0f || linear > 1.0f ||
  6356. angular < 0.0f || angular > 1.0f ||
  6357. contact < 0.0f || contact > 1.0f ) {
  6358. return;
  6359. }
  6360. linearFriction = linear;
  6361. angularFriction = angular;
  6362. contactFriction = contact;
  6363. }
  6364. /*
  6365. ================
  6366. idPhysics_AF::GetImpactInfo
  6367. ================
  6368. */
  6369. void idPhysics_AF::GetImpactInfo( const int id, const idVec3 &point, impactInfo_t *info ) const {
  6370. if ( id < 0 || id >= bodies.Num() ) {
  6371. memset( info, 0, sizeof( *info ) );
  6372. return;
  6373. }
  6374. info->invMass = 1.0f / bodies[id]->mass;
  6375. info->invInertiaTensor = bodies[id]->current->worldAxis.Transpose() * bodies[id]->inverseInertiaTensor * bodies[id]->current->worldAxis;
  6376. info->position = point - bodies[id]->current->worldOrigin;
  6377. info->velocity = bodies[id]->current->spatialVelocity.SubVec3(0) + bodies[id]->current->spatialVelocity.SubVec3(1).Cross( info->position );
  6378. }
  6379. /*
  6380. ================
  6381. idPhysics_AF::ApplyImpulse
  6382. ================
  6383. */
  6384. void idPhysics_AF::ApplyImpulse( const int id, const idVec3 &point, const idVec3 &impulse ) {
  6385. if ( id < 0 || id >= bodies.Num() ) {
  6386. return;
  6387. }
  6388. if ( noImpact || impulse.LengthSqr() < Square( impulseThreshold ) ) {
  6389. return;
  6390. }
  6391. idMat3 invWorldInertiaTensor = bodies[id]->current->worldAxis.Transpose() * bodies[id]->inverseInertiaTensor * bodies[id]->current->worldAxis;
  6392. bodies[id]->current->spatialVelocity.SubVec3(0) += bodies[id]->invMass * impulse;
  6393. bodies[id]->current->spatialVelocity.SubVec3(1) += invWorldInertiaTensor * (point - bodies[id]->current->worldOrigin).Cross( impulse );
  6394. Activate();
  6395. }
  6396. /*
  6397. ================
  6398. idPhysics_AF::AddForce
  6399. ================
  6400. */
  6401. void idPhysics_AF::AddForce( const int id, const idVec3 &point, const idVec3 &force ) {
  6402. if ( noImpact ) {
  6403. return;
  6404. }
  6405. if ( id < 0 || id >= bodies.Num() ) {
  6406. return;
  6407. }
  6408. bodies[id]->current->externalForce.SubVec3( 0 ) += force;
  6409. bodies[id]->current->externalForce.SubVec3( 1 ) += (point - bodies[id]->current->worldOrigin).Cross( force );
  6410. Activate();
  6411. }
  6412. /*
  6413. ================
  6414. idPhysics_AF::IsAtRest
  6415. ================
  6416. */
  6417. bool idPhysics_AF::IsAtRest( void ) const {
  6418. return current.atRest >= 0;
  6419. }
  6420. /*
  6421. ================
  6422. idPhysics_AF::GetRestStartTime
  6423. ================
  6424. */
  6425. int idPhysics_AF::GetRestStartTime( void ) const {
  6426. return current.atRest;
  6427. }
  6428. /*
  6429. ================
  6430. idPhysics_AF::IsPushable
  6431. ================
  6432. */
  6433. bool idPhysics_AF::IsPushable( void ) const {
  6434. return ( !noImpact && ( masterBody == NULL || forcePushable ) );
  6435. }
  6436. /*
  6437. ================
  6438. idPhysics_AF::SaveState
  6439. ================
  6440. */
  6441. void idPhysics_AF::SaveState( void ) {
  6442. int i;
  6443. saved = current;
  6444. for ( i = 0; i < bodies.Num(); i++ ) {
  6445. memcpy( &bodies[i]->saved, bodies[i]->current, sizeof( AFBodyPState_t ) );
  6446. }
  6447. }
  6448. /*
  6449. ================
  6450. idPhysics_AF::RestoreState
  6451. ================
  6452. */
  6453. void idPhysics_AF::RestoreState( void ) {
  6454. int i;
  6455. current = saved;
  6456. for ( i = 0; i < bodies.Num(); i++ ) {
  6457. *(bodies[i]->current) = bodies[i]->saved;
  6458. }
  6459. EvaluateContacts();
  6460. }
  6461. /*
  6462. ================
  6463. idPhysics_AF::SetOrigin
  6464. ================
  6465. */
  6466. void idPhysics_AF::SetOrigin( const idVec3 &newOrigin, int id ) {
  6467. if ( masterBody ) {
  6468. Translate( masterBody->current->worldOrigin + masterBody->current->worldAxis * newOrigin - bodies[0]->current->worldOrigin );
  6469. } else {
  6470. Translate( newOrigin - bodies[0]->current->worldOrigin );
  6471. }
  6472. }
  6473. /*
  6474. ================
  6475. idPhysics_AF::SetAxis
  6476. ================
  6477. */
  6478. void idPhysics_AF::SetAxis( const idMat3 &newAxis, int id ) {
  6479. idMat3 axis;
  6480. idRotation rotation;
  6481. if ( masterBody ) {
  6482. axis = bodies[0]->current->worldAxis.Transpose() * ( newAxis * masterBody->current->worldAxis );
  6483. } else {
  6484. axis = bodies[0]->current->worldAxis.Transpose() * newAxis;
  6485. }
  6486. rotation = axis.ToRotation();
  6487. rotation.SetOrigin( bodies[0]->current->worldOrigin );
  6488. Rotate( rotation );
  6489. }
  6490. /*
  6491. ================
  6492. idPhysics_AF::Translate
  6493. ================
  6494. */
  6495. void idPhysics_AF::Translate( const idVec3 &translation, int id ) {
  6496. int i;
  6497. idAFBody *body;
  6498. if ( !worldConstraintsLocked ) {
  6499. // translate constraints attached to the world
  6500. for ( i = 0; i < constraints.Num(); i++ ) {
  6501. constraints[i]->Translate( translation );
  6502. }
  6503. }
  6504. // translate all the bodies
  6505. for ( i = 0; i < bodies.Num(); i++ ) {
  6506. body = bodies[i];
  6507. body->current->worldOrigin += translation;
  6508. }
  6509. Activate();
  6510. UpdateClipModels();
  6511. }
  6512. /*
  6513. ================
  6514. idPhysics_AF::Rotate
  6515. ================
  6516. */
  6517. void idPhysics_AF::Rotate( const idRotation &rotation, int id ) {
  6518. int i;
  6519. idAFBody *body;
  6520. if ( !worldConstraintsLocked ) {
  6521. // rotate constraints attached to the world
  6522. for ( i = 0; i < constraints.Num(); i++ ) {
  6523. constraints[i]->Rotate( rotation );
  6524. }
  6525. }
  6526. // rotate all the bodies
  6527. for ( i = 0; i < bodies.Num(); i++ ) {
  6528. body = bodies[i];
  6529. body->current->worldOrigin *= rotation;
  6530. body->current->worldAxis *= rotation.ToMat3();
  6531. }
  6532. Activate();
  6533. UpdateClipModels();
  6534. }
  6535. /*
  6536. ================
  6537. idPhysics_AF::GetOrigin
  6538. ================
  6539. */
  6540. const idVec3 &idPhysics_AF::GetOrigin( int id ) const {
  6541. if ( id < 0 || id >= bodies.Num() ) {
  6542. return vec3_origin;
  6543. }
  6544. else {
  6545. return bodies[id]->current->worldOrigin;
  6546. }
  6547. }
  6548. /*
  6549. ================
  6550. idPhysics_AF::GetAxis
  6551. ================
  6552. */
  6553. const idMat3 &idPhysics_AF::GetAxis( int id ) const {
  6554. if ( id < 0 || id >= bodies.Num() ) {
  6555. return mat3_identity;
  6556. }
  6557. else {
  6558. return bodies[id]->current->worldAxis;
  6559. }
  6560. }
  6561. /*
  6562. ================
  6563. idPhysics_AF::SetLinearVelocity
  6564. ================
  6565. */
  6566. void idPhysics_AF::SetLinearVelocity( const idVec3 &newLinearVelocity, int id ) {
  6567. if ( id < 0 || id >= bodies.Num() ) {
  6568. return;
  6569. }
  6570. bodies[id]->current->spatialVelocity.SubVec3( 0 ) = newLinearVelocity;
  6571. Activate();
  6572. }
  6573. /*
  6574. ================
  6575. idPhysics_AF::SetAngularVelocity
  6576. ================
  6577. */
  6578. void idPhysics_AF::SetAngularVelocity( const idVec3 &newAngularVelocity, int id ) {
  6579. if ( id < 0 || id >= bodies.Num() ) {
  6580. return;
  6581. }
  6582. bodies[id]->current->spatialVelocity.SubVec3( 1 ) = newAngularVelocity;
  6583. Activate();
  6584. }
  6585. /*
  6586. ================
  6587. idPhysics_AF::GetLinearVelocity
  6588. ================
  6589. */
  6590. const idVec3 &idPhysics_AF::GetLinearVelocity( int id ) const {
  6591. if ( id < 0 || id >= bodies.Num() ) {
  6592. return vec3_origin;
  6593. }
  6594. else {
  6595. return bodies[id]->current->spatialVelocity.SubVec3( 0 );
  6596. }
  6597. }
  6598. /*
  6599. ================
  6600. idPhysics_AF::GetAngularVelocity
  6601. ================
  6602. */
  6603. const idVec3 &idPhysics_AF::GetAngularVelocity( int id ) const {
  6604. if ( id < 0 || id >= bodies.Num() ) {
  6605. return vec3_origin;
  6606. }
  6607. else {
  6608. return bodies[id]->current->spatialVelocity.SubVec3( 1 );
  6609. }
  6610. }
  6611. /*
  6612. ================
  6613. idPhysics_AF::ClipTranslation
  6614. ================
  6615. */
  6616. void idPhysics_AF::ClipTranslation( trace_t &results, const idVec3 &translation, const idClipModel *model ) const {
  6617. int i;
  6618. idAFBody *body;
  6619. trace_t bodyResults;
  6620. results.fraction = 1.0f;
  6621. for ( i = 0; i < bodies.Num(); i++ ) {
  6622. body = bodies[i];
  6623. if ( body->clipModel->IsTraceModel() ) {
  6624. if ( model ) {
  6625. gameLocal.clip.TranslationModel( bodyResults, body->current->worldOrigin, body->current->worldOrigin + translation,
  6626. body->clipModel, body->current->worldAxis, body->clipMask,
  6627. model->Handle(), model->GetOrigin(), model->GetAxis() );
  6628. }
  6629. else {
  6630. gameLocal.clip.Translation( bodyResults, body->current->worldOrigin, body->current->worldOrigin + translation,
  6631. body->clipModel, body->current->worldAxis, body->clipMask, self );
  6632. }
  6633. if ( bodyResults.fraction < results.fraction ) {
  6634. results = bodyResults;
  6635. }
  6636. }
  6637. }
  6638. results.endpos = bodies[0]->current->worldOrigin + results.fraction * translation;
  6639. results.endAxis = bodies[0]->current->worldAxis;
  6640. }
  6641. /*
  6642. ================
  6643. idPhysics_AF::ClipRotation
  6644. ================
  6645. */
  6646. void idPhysics_AF::ClipRotation( trace_t &results, const idRotation &rotation, const idClipModel *model ) const {
  6647. int i;
  6648. idAFBody *body;
  6649. trace_t bodyResults;
  6650. idRotation partialRotation;
  6651. results.fraction = 1.0f;
  6652. for ( i = 0; i < bodies.Num(); i++ ) {
  6653. body = bodies[i];
  6654. if ( body->clipModel->IsTraceModel() ) {
  6655. if ( model ) {
  6656. gameLocal.clip.RotationModel( bodyResults, body->current->worldOrigin, rotation,
  6657. body->clipModel, body->current->worldAxis, body->clipMask,
  6658. model->Handle(), model->GetOrigin(), model->GetAxis() );
  6659. }
  6660. else {
  6661. gameLocal.clip.Rotation( bodyResults, body->current->worldOrigin, rotation,
  6662. body->clipModel, body->current->worldAxis, body->clipMask, self );
  6663. }
  6664. if ( bodyResults.fraction < results.fraction ) {
  6665. results = bodyResults;
  6666. }
  6667. }
  6668. }
  6669. partialRotation = rotation * results.fraction;
  6670. results.endpos = bodies[0]->current->worldOrigin * partialRotation;
  6671. results.endAxis = bodies[0]->current->worldAxis * partialRotation.ToMat3();
  6672. }
  6673. /*
  6674. ================
  6675. idPhysics_AF::ClipContents
  6676. ================
  6677. */
  6678. int idPhysics_AF::ClipContents( const idClipModel *model ) const {
  6679. int i, contents;
  6680. idAFBody *body;
  6681. contents = 0;
  6682. for ( i = 0; i < bodies.Num(); i++ ) {
  6683. body = bodies[i];
  6684. if ( body->clipModel->IsTraceModel() ) {
  6685. if ( model ) {
  6686. contents |= gameLocal.clip.ContentsModel( body->current->worldOrigin,
  6687. body->clipModel, body->current->worldAxis, -1,
  6688. model->Handle(), model->GetOrigin(), model->GetAxis() );
  6689. }
  6690. else {
  6691. contents |= gameLocal.clip.Contents( body->current->worldOrigin,
  6692. body->clipModel, body->current->worldAxis, -1, NULL );
  6693. }
  6694. }
  6695. }
  6696. return contents;
  6697. }
  6698. /*
  6699. ================
  6700. idPhysics_AF::DisableClip
  6701. ================
  6702. */
  6703. void idPhysics_AF::DisableClip( void ) {
  6704. int i;
  6705. for ( i = 0; i < bodies.Num(); i++ ) {
  6706. bodies[i]->clipModel->Disable();
  6707. }
  6708. }
  6709. /*
  6710. ================
  6711. idPhysics_AF::EnableClip
  6712. ================
  6713. */
  6714. void idPhysics_AF::EnableClip( void ) {
  6715. int i;
  6716. for ( i = 0; i < bodies.Num(); i++ ) {
  6717. bodies[i]->clipModel->Enable();
  6718. }
  6719. }
  6720. /*
  6721. ================
  6722. idPhysics_AF::UnlinkClip
  6723. ================
  6724. */
  6725. void idPhysics_AF::UnlinkClip( void ) {
  6726. int i;
  6727. for ( i = 0; i < bodies.Num(); i++ ) {
  6728. bodies[i]->clipModel->Unlink();
  6729. }
  6730. }
  6731. /*
  6732. ================
  6733. idPhysics_AF::LinkClip
  6734. ================
  6735. */
  6736. void idPhysics_AF::LinkClip( void ) {
  6737. UpdateClipModels();
  6738. }
  6739. /*
  6740. ================
  6741. idPhysics_AF::SetPushed
  6742. ================
  6743. */
  6744. void idPhysics_AF::SetPushed( int deltaTime ) {
  6745. idAFBody *body;
  6746. idRotation rotation;
  6747. if ( bodies.Num() ) {
  6748. body = bodies[0];
  6749. rotation = ( body->saved.worldAxis.Transpose() * body->current->worldAxis ).ToRotation();
  6750. // velocity with which the af is pushed
  6751. current.pushVelocity.SubVec3(0) += ( body->current->worldOrigin - body->saved.worldOrigin ) / ( deltaTime * idMath::M_MS2SEC );
  6752. current.pushVelocity.SubVec3(1) += rotation.GetVec() * -DEG2RAD( rotation.GetAngle() ) / ( deltaTime * idMath::M_MS2SEC );
  6753. }
  6754. }
  6755. /*
  6756. ================
  6757. idPhysics_AF::GetPushedLinearVelocity
  6758. ================
  6759. */
  6760. const idVec3 &idPhysics_AF::GetPushedLinearVelocity( const int id ) const {
  6761. return current.pushVelocity.SubVec3(0);
  6762. }
  6763. /*
  6764. ================
  6765. idPhysics_AF::GetPushedAngularVelocity
  6766. ================
  6767. */
  6768. const idVec3 &idPhysics_AF::GetPushedAngularVelocity( const int id ) const {
  6769. return current.pushVelocity.SubVec3(1);
  6770. }
  6771. /*
  6772. ================
  6773. idPhysics_AF::SetMaster
  6774. the binding is orientated based on the constraints being used
  6775. ================
  6776. */
  6777. void idPhysics_AF::SetMaster( idEntity *master, const bool orientated ) {
  6778. int i;
  6779. idVec3 masterOrigin;
  6780. idMat3 masterAxis;
  6781. idRotation rotation;
  6782. if ( master ) {
  6783. self->GetMasterPosition( masterOrigin, masterAxis );
  6784. if ( !masterBody ) {
  6785. masterBody = new idAFBody();
  6786. // translate and rotate all the constraints with body2 == NULL from world space to master space
  6787. rotation = masterAxis.Transpose().ToRotation();
  6788. for ( i = 0; i < constraints.Num(); i++ ) {
  6789. if ( constraints[i]->GetBody2() == NULL ) {
  6790. constraints[i]->Translate( -masterOrigin );
  6791. constraints[i]->Rotate( rotation );
  6792. }
  6793. }
  6794. Activate();
  6795. }
  6796. masterBody->current->worldOrigin = masterOrigin;
  6797. masterBody->current->worldAxis = masterAxis;
  6798. }
  6799. else {
  6800. if ( masterBody ) {
  6801. // translate and rotate all the constraints with body2 == NULL from master space to world space
  6802. rotation = masterBody->current->worldAxis.ToRotation();
  6803. for ( i = 0; i < constraints.Num(); i++ ) {
  6804. if ( constraints[i]->GetBody2() == NULL ) {
  6805. constraints[i]->Rotate( rotation );
  6806. constraints[i]->Translate( masterBody->current->worldOrigin );
  6807. }
  6808. }
  6809. delete masterBody;
  6810. masterBody = NULL;
  6811. Activate();
  6812. }
  6813. }
  6814. }
  6815. const float AF_VELOCITY_MAX = 16000;
  6816. const int AF_VELOCITY_TOTAL_BITS = 16;
  6817. const int AF_VELOCITY_EXPONENT_BITS = idMath::BitsForInteger( idMath::BitsForFloat( AF_VELOCITY_MAX ) ) + 1;
  6818. const int AF_VELOCITY_MANTISSA_BITS = AF_VELOCITY_TOTAL_BITS - 1 - AF_VELOCITY_EXPONENT_BITS;
  6819. const float AF_FORCE_MAX = 1e20f;
  6820. const int AF_FORCE_TOTAL_BITS = 16;
  6821. const int AF_FORCE_EXPONENT_BITS = idMath::BitsForInteger( idMath::BitsForFloat( AF_FORCE_MAX ) ) + 1;
  6822. const int AF_FORCE_MANTISSA_BITS = AF_FORCE_TOTAL_BITS - 1 - AF_FORCE_EXPONENT_BITS;
  6823. /*
  6824. ================
  6825. idPhysics_AF::WriteToSnapshot
  6826. ================
  6827. */
  6828. void idPhysics_AF::WriteToSnapshot( idBitMsgDelta &msg ) const {
  6829. int i;
  6830. idCQuat quat;
  6831. msg.WriteLong( current.atRest );
  6832. msg.WriteFloat( current.noMoveTime );
  6833. msg.WriteFloat( current.activateTime );
  6834. msg.WriteDeltaFloat( 0.0f, current.pushVelocity[0], AF_VELOCITY_EXPONENT_BITS, AF_VELOCITY_MANTISSA_BITS );
  6835. msg.WriteDeltaFloat( 0.0f, current.pushVelocity[1], AF_VELOCITY_EXPONENT_BITS, AF_VELOCITY_MANTISSA_BITS );
  6836. msg.WriteDeltaFloat( 0.0f, current.pushVelocity[2], AF_VELOCITY_EXPONENT_BITS, AF_VELOCITY_MANTISSA_BITS );
  6837. msg.WriteDeltaFloat( 0.0f, current.pushVelocity[3], AF_VELOCITY_EXPONENT_BITS, AF_VELOCITY_MANTISSA_BITS );
  6838. msg.WriteDeltaFloat( 0.0f, current.pushVelocity[4], AF_VELOCITY_EXPONENT_BITS, AF_VELOCITY_MANTISSA_BITS );
  6839. msg.WriteDeltaFloat( 0.0f, current.pushVelocity[5], AF_VELOCITY_EXPONENT_BITS, AF_VELOCITY_MANTISSA_BITS );
  6840. msg.WriteByte( bodies.Num() );
  6841. for ( i = 0; i < bodies.Num(); i++ ) {
  6842. AFBodyPState_t *state = bodies[i]->current;
  6843. quat = state->worldAxis.ToCQuat();
  6844. msg.WriteFloat( state->worldOrigin[0] );
  6845. msg.WriteFloat( state->worldOrigin[1] );
  6846. msg.WriteFloat( state->worldOrigin[2] );
  6847. msg.WriteFloat( quat.x );
  6848. msg.WriteFloat( quat.y );
  6849. msg.WriteFloat( quat.z );
  6850. msg.WriteDeltaFloat( 0.0f, state->spatialVelocity[0], AF_VELOCITY_EXPONENT_BITS, AF_VELOCITY_MANTISSA_BITS );
  6851. msg.WriteDeltaFloat( 0.0f, state->spatialVelocity[1], AF_VELOCITY_EXPONENT_BITS, AF_VELOCITY_MANTISSA_BITS );
  6852. msg.WriteDeltaFloat( 0.0f, state->spatialVelocity[2], AF_VELOCITY_EXPONENT_BITS, AF_VELOCITY_MANTISSA_BITS );
  6853. msg.WriteDeltaFloat( 0.0f, state->spatialVelocity[3], AF_VELOCITY_EXPONENT_BITS, AF_VELOCITY_MANTISSA_BITS );
  6854. msg.WriteDeltaFloat( 0.0f, state->spatialVelocity[4], AF_VELOCITY_EXPONENT_BITS, AF_VELOCITY_MANTISSA_BITS );
  6855. msg.WriteDeltaFloat( 0.0f, state->spatialVelocity[5], AF_VELOCITY_EXPONENT_BITS, AF_VELOCITY_MANTISSA_BITS );
  6856. /* msg.WriteDeltaFloat( 0.0f, state->externalForce[0], AF_FORCE_EXPONENT_BITS, AF_FORCE_MANTISSA_BITS );
  6857. msg.WriteDeltaFloat( 0.0f, state->externalForce[1], AF_FORCE_EXPONENT_BITS, AF_FORCE_MANTISSA_BITS );
  6858. msg.WriteDeltaFloat( 0.0f, state->externalForce[2], AF_FORCE_EXPONENT_BITS, AF_FORCE_MANTISSA_BITS );
  6859. msg.WriteDeltaFloat( 0.0f, state->externalForce[3], AF_FORCE_EXPONENT_BITS, AF_FORCE_MANTISSA_BITS );
  6860. msg.WriteDeltaFloat( 0.0f, state->externalForce[4], AF_FORCE_EXPONENT_BITS, AF_FORCE_MANTISSA_BITS );
  6861. msg.WriteDeltaFloat( 0.0f, state->externalForce[5], AF_FORCE_EXPONENT_BITS, AF_FORCE_MANTISSA_BITS );
  6862. */
  6863. }
  6864. }
  6865. /*
  6866. ================
  6867. idPhysics_AF::ReadFromSnapshot
  6868. ================
  6869. */
  6870. void idPhysics_AF::ReadFromSnapshot( const idBitMsgDelta &msg ) {
  6871. int i, num;
  6872. idCQuat quat;
  6873. current.atRest = msg.ReadLong();
  6874. current.noMoveTime = msg.ReadFloat();
  6875. current.activateTime = msg.ReadFloat();
  6876. current.pushVelocity[0] = msg.ReadDeltaFloat( 0.0f, AF_VELOCITY_EXPONENT_BITS, AF_VELOCITY_MANTISSA_BITS );
  6877. current.pushVelocity[1] = msg.ReadDeltaFloat( 0.0f, AF_VELOCITY_EXPONENT_BITS, AF_VELOCITY_MANTISSA_BITS );
  6878. current.pushVelocity[2] = msg.ReadDeltaFloat( 0.0f, AF_VELOCITY_EXPONENT_BITS, AF_VELOCITY_MANTISSA_BITS );
  6879. current.pushVelocity[3] = msg.ReadDeltaFloat( 0.0f, AF_VELOCITY_EXPONENT_BITS, AF_VELOCITY_MANTISSA_BITS );
  6880. current.pushVelocity[4] = msg.ReadDeltaFloat( 0.0f, AF_VELOCITY_EXPONENT_BITS, AF_VELOCITY_MANTISSA_BITS );
  6881. current.pushVelocity[5] = msg.ReadDeltaFloat( 0.0f, AF_VELOCITY_EXPONENT_BITS, AF_VELOCITY_MANTISSA_BITS );
  6882. num = msg.ReadByte();
  6883. assert( num == bodies.Num() );
  6884. for ( i = 0; i < bodies.Num(); i++ ) {
  6885. AFBodyPState_t *state = bodies[i]->current;
  6886. state->worldOrigin[0] = msg.ReadFloat();
  6887. state->worldOrigin[1] = msg.ReadFloat();
  6888. state->worldOrigin[2] = msg.ReadFloat();
  6889. quat.x = msg.ReadFloat();
  6890. quat.y = msg.ReadFloat();
  6891. quat.z = msg.ReadFloat();
  6892. state->spatialVelocity[0] = msg.ReadDeltaFloat( 0.0f, AF_VELOCITY_EXPONENT_BITS, AF_VELOCITY_MANTISSA_BITS );
  6893. state->spatialVelocity[1] = msg.ReadDeltaFloat( 0.0f, AF_VELOCITY_EXPONENT_BITS, AF_VELOCITY_MANTISSA_BITS );
  6894. state->spatialVelocity[2] = msg.ReadDeltaFloat( 0.0f, AF_VELOCITY_EXPONENT_BITS, AF_VELOCITY_MANTISSA_BITS );
  6895. state->spatialVelocity[3] = msg.ReadDeltaFloat( 0.0f, AF_VELOCITY_EXPONENT_BITS, AF_VELOCITY_MANTISSA_BITS );
  6896. state->spatialVelocity[4] = msg.ReadDeltaFloat( 0.0f, AF_VELOCITY_EXPONENT_BITS, AF_VELOCITY_MANTISSA_BITS );
  6897. state->spatialVelocity[5] = msg.ReadDeltaFloat( 0.0f, AF_VELOCITY_EXPONENT_BITS, AF_VELOCITY_MANTISSA_BITS );
  6898. /* state->externalForce[0] = msg.ReadDeltaFloat( 0.0f, AF_FORCE_EXPONENT_BITS, AF_FORCE_MANTISSA_BITS );
  6899. state->externalForce[1] = msg.ReadDeltaFloat( 0.0f, AF_FORCE_EXPONENT_BITS, AF_FORCE_MANTISSA_BITS );
  6900. state->externalForce[2] = msg.ReadDeltaFloat( 0.0f, AF_FORCE_EXPONENT_BITS, AF_FORCE_MANTISSA_BITS );
  6901. state->externalForce[3] = msg.ReadDeltaFloat( 0.0f, AF_FORCE_EXPONENT_BITS, AF_FORCE_MANTISSA_BITS );
  6902. state->externalForce[4] = msg.ReadDeltaFloat( 0.0f, AF_FORCE_EXPONENT_BITS, AF_FORCE_MANTISSA_BITS );
  6903. state->externalForce[5] = msg.ReadDeltaFloat( 0.0f, AF_FORCE_EXPONENT_BITS, AF_FORCE_MANTISSA_BITS );
  6904. */
  6905. state->worldAxis = quat.ToMat3();
  6906. }
  6907. UpdateClipModels();
  6908. }