Scene.cc 202 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _SCENE_H_
  23. #include "Scene.h"
  24. #endif
  25. #ifndef _DGL_H_
  26. #include "graphics/dgl.h"
  27. #endif
  28. #ifndef _CONSOLETYPES_H_
  29. #include "console/consoleTypes.h"
  30. #endif
  31. #ifndef _BITSTREAM_H_
  32. #include "io/bitStream.h"
  33. #endif
  34. #ifndef _SCENE_OBJECT_H_
  35. #include "2d/sceneobject/SceneObject.h"
  36. #endif
  37. #ifndef _CONTACT_FILTER_H_
  38. #include "ContactFilter.h"
  39. #endif
  40. #ifndef _SCENE_RENDER_OBJECT_H_
  41. #include "2d/SceneRenderObject.h"
  42. #endif
  43. #ifndef _SCENE_CONTROLLER_H_
  44. #include "2d/controllers/core/SceneController.h"
  45. #endif
  46. #ifndef _PARTICLE_SYSTEM_H_
  47. #include "2d/core/ParticleSystem.h"
  48. #endif
  49. // Script bindings.
  50. #include "Scene_ScriptBinding.h"
  51. // Debug Profiling.
  52. #include "debug/profiler.h"
  53. //------------------------------------------------------------------------------
  54. SimObjectPtr<Scene> Scene::LoadingScene = NULL;
  55. //------------------------------------------------------------------------------
  56. static ContactFilter mContactFilter;
  57. // Scene counter.
  58. static U32 sSceneCount = 0;
  59. static U32 sSceneMasterIndex = 0;
  60. // Joint custom node names.
  61. static StringTableEntry jointCustomNodeName = StringTable->insert( "Joints" );
  62. static StringTableEntry jointCollideConnectedName = StringTable->insert( "CollideConnected" );
  63. static StringTableEntry jointLocalAnchorAName = StringTable->insert( "LocalAnchorA" );
  64. static StringTableEntry jointLocalAnchorBName = StringTable->insert( "LocalAnchorB" );
  65. static StringTableEntry jointDistanceNodeName = StringTable->insert( "Distance" );
  66. static StringTableEntry jointDistanceLengthName = StringTable->insert( "Length" );
  67. static StringTableEntry jointDistanceFrequencyName = StringTable->insert( "Frequency" );
  68. static StringTableEntry jointDistanceDampingRatioName = StringTable->insert( "DampingRatio" );
  69. static StringTableEntry jointRopeNodeName = StringTable->insert( "Rope" );
  70. static StringTableEntry jointRopeMaxLengthName = StringTable->insert( "MaxLength" );
  71. static StringTableEntry jointRevoluteNodeName = StringTable->insert( "Revolute" );
  72. static StringTableEntry jointRevoluteLimitLowerAngleName = StringTable->insert( "LowerAngle" );
  73. static StringTableEntry jointRevoluteLimitUpperAngleName = StringTable->insert( "UpperAngle" );
  74. static StringTableEntry jointRevoluteMotorSpeedName = StringTable->insert( "MotorSpeed" );
  75. static StringTableEntry jointRevoluteMotorMaxTorqueName = StringTable->insert( "MaxTorque" );
  76. static StringTableEntry jointWeldNodeName = StringTable->insert( "Weld" );
  77. static StringTableEntry jointWeldFrequencyName = jointDistanceFrequencyName;
  78. static StringTableEntry jointWeldDampingRatioName = jointDistanceDampingRatioName;
  79. static StringTableEntry jointWheelNodeName = StringTable->insert( "Wheel" );
  80. static StringTableEntry jointWheelWorldAxisName = StringTable->insert( "WorldAxis" );
  81. static StringTableEntry jointWheelMotorSpeedName = StringTable->insert( "MotorSpeed" );
  82. static StringTableEntry jointWheelMotorMaxTorqueName = jointRevoluteMotorMaxTorqueName;
  83. static StringTableEntry jointWheelFrequencyName = jointDistanceFrequencyName;
  84. static StringTableEntry jointWheelDampingRatioName = jointDistanceDampingRatioName;
  85. static StringTableEntry jointFrictionNodeName = StringTable->insert( "Friction" );
  86. static StringTableEntry jointFrictionMaxForceName = StringTable->insert( "MaxForce" );
  87. static StringTableEntry jointFrictionMaxTorqueName = jointRevoluteMotorMaxTorqueName;
  88. static StringTableEntry jointPrismaticNodeName = StringTable->insert( "Prismatic" );
  89. static StringTableEntry jointPrismaticWorldAxisName = jointWheelWorldAxisName;
  90. static StringTableEntry jointPrismaticLimitLowerTransName = StringTable->insert( "LowerTranslation" );
  91. static StringTableEntry jointPrismaticLimitUpperTransName = StringTable->insert( "UpperTranslation" );
  92. static StringTableEntry jointPrismaticMotorSpeedName = jointRevoluteMotorSpeedName;
  93. static StringTableEntry jointPrismaticMotorMaxForceName = jointFrictionMaxForceName;
  94. static StringTableEntry jointPulleyNodeName = StringTable->insert( "Pulley" );
  95. static StringTableEntry jointPulleyGroundAnchorAName = StringTable->insert( "GroundAnchorA" );
  96. static StringTableEntry jointPulleyGroundAnchorBName = StringTable->insert( "GroundAnchorB" );
  97. static StringTableEntry jointPulleyLengthAName = StringTable->insert( "LengthA" );
  98. static StringTableEntry jointPulleyLengthBName = StringTable->insert( "LengthB" );
  99. static StringTableEntry jointPulleyRatioName = StringTable->insert( "Ratio" );
  100. static StringTableEntry jointTargetNodeName = StringTable->insert( "Target" );
  101. static StringTableEntry jointTargetWorldTargetName = StringTable->insert( "WorldTarget" );
  102. static StringTableEntry jointTargetMaxForceName = StringTable->insert( jointFrictionMaxForceName );
  103. static StringTableEntry jointTargetFrequencyName = jointDistanceFrequencyName;
  104. static StringTableEntry jointTargetDampingRatioName = jointDistanceDampingRatioName;
  105. static StringTableEntry jointMotorNodeName = StringTable->insert( "Motor" );
  106. static StringTableEntry jointMotorLinearOffsetName = StringTable->insert( "LinearOffset" );
  107. static StringTableEntry jointMotorAngularOffsetName = StringTable->insert( "AngularOffset" );
  108. static StringTableEntry jointMotorMaxForceName = jointFrictionMaxForceName;
  109. static StringTableEntry jointMotorMaxTorqueName = jointRevoluteMotorMaxTorqueName;
  110. static StringTableEntry jointMotorCorrectionFactorName = StringTable->insert( "CorrectionFactor" );
  111. // Controller custom node names.
  112. static StringTableEntry controllerCustomNodeName = StringTable->insert( "Controllers" );
  113. // Asset preload custom node names.
  114. static StringTableEntry assetPreloadNodeName = StringTable->insert( "AssetPreloads" );
  115. static StringTableEntry assetNodeName = StringTable->insert( "Asset" );
  116. //-----------------------------------------------------------------------------
  117. Scene::Scene() :
  118. /// World.
  119. mpWorld(NULL),
  120. mWorldGravity(0.0f, 0.0f),
  121. mVelocityIterations(8),
  122. mPositionIterations(3),
  123. /// Joint access.
  124. mJointMasterId(1),
  125. /// Scene time.
  126. mSceneTime(0.0f),
  127. mScenePause(false),
  128. /// Debug and metrics.
  129. mDebugMask(0X00000000),
  130. mpDebugSceneObject(NULL),
  131. /// Window rendering.
  132. mpCurrentRenderWindow(NULL),
  133. /// Miscellaneous.
  134. mIsEditorScene(0),
  135. mUpdateCallback(false),
  136. mRenderCallback(false),
  137. mSceneIndex(0)
  138. {
  139. // Set Vector Associations.
  140. VECTOR_SET_ASSOCIATION( mSceneObjects );
  141. VECTOR_SET_ASSOCIATION( mDeleteRequests );
  142. VECTOR_SET_ASSOCIATION( mDeleteRequestsTemp );
  143. VECTOR_SET_ASSOCIATION( mEndContacts );
  144. VECTOR_SET_ASSOCIATION( mAssetPreloads );
  145. // Initialize layer sort mode.
  146. for ( U32 n = 0; n < MAX_LAYERS_SUPPORTED; ++n )
  147. mLayerSortModes[n] = SceneRenderQueue::RENDER_SORT_NEWEST;
  148. // Set debug stats for batch renderer.
  149. mBatchRenderer.setDebugStats( &mDebugStats );
  150. // Register the scene controllers set.
  151. mControllers = new SimSet();
  152. mControllers->registerObject();
  153. // Assign scene index.
  154. mSceneIndex = ++sSceneMasterIndex;
  155. sSceneCount++;
  156. }
  157. //-----------------------------------------------------------------------------
  158. Scene::~Scene()
  159. {
  160. // Unregister the scene controllers set.
  161. if ( mControllers.notNull() )
  162. mControllers->deleteObject();
  163. // Decrease scene count.
  164. --sSceneCount;
  165. }
  166. //-----------------------------------------------------------------------------
  167. bool Scene::onAdd()
  168. {
  169. // Call Parent.
  170. if(!Parent::onAdd())
  171. return false;
  172. // Create physics world.
  173. mpWorld = new b2World( mWorldGravity );
  174. // Set contact filter.
  175. mpWorld->SetContactFilter( &mContactFilter );
  176. const b2ParticleSystemDef particleSystemDef;
  177. mParticleSystem = mpWorld->CreateParticleSystem(&particleSystemDef);
  178. // Set contact listener.
  179. mpWorld->SetContactListener( this );
  180. // Set destruction listener.
  181. mpWorld->SetDestructionListener( this );
  182. // Create ground body.
  183. b2BodyDef groundBodyDef;
  184. groundBodyDef.userData = static_cast<PhysicsProxy*>(this);
  185. mpGroundBody = mpWorld->CreateBody(&groundBodyDef);
  186. mpGroundBody->SetAwake( false );
  187. // Create world query.
  188. mpWorldQuery = new WorldQuery(this);
  189. // Set loading scene.
  190. Scene::LoadingScene = this;
  191. // Turn-on tick processing.
  192. setProcessTicks( true );
  193. // Return Okay.
  194. return true;
  195. }
  196. //-----------------------------------------------------------------------------
  197. void Scene::onRemove()
  198. {
  199. // Turn-off tick processing.
  200. setProcessTicks( false );
  201. // Clear Scene.
  202. clearScene();
  203. // Process Delete Requests.
  204. processDeleteRequests(true);
  205. // Delete ground body.
  206. mpWorld->DestroyBody( mpGroundBody );
  207. mpGroundBody = NULL;
  208. // Delete physics world and world query.
  209. delete mpWorldQuery;
  210. delete mpWorld;
  211. mpWorldQuery = NULL;
  212. mpWorld = NULL;
  213. // Detach All Scene Windows.
  214. detachAllSceneWindows();
  215. // Call Parent. Clear scene handles all the object removal, so we can skip
  216. // that part and just do the sim-object stuff.
  217. SimObject::onRemove();
  218. }
  219. //-----------------------------------------------------------------------------
  220. void Scene::onDeleteNotify( SimObject* object )
  221. {
  222. // Ignore if we're not monitoring a debug banner scene object.
  223. if ( mpDebugSceneObject == NULL )
  224. return;
  225. // Ignore if it's not the one we're monitoring.
  226. SceneObject* pSceneObject = dynamic_cast<SceneObject*>( object );
  227. if ( pSceneObject != mpDebugSceneObject )
  228. return;
  229. // Reset the monitored scene object.
  230. mpDebugSceneObject = NULL;
  231. }
  232. //-----------------------------------------------------------------------------
  233. void Scene::initPersistFields()
  234. {
  235. // Call Parent.
  236. Parent::initPersistFields();
  237. // Physics.
  238. addProtectedField("Gravity", TypeVector2, Offset(mWorldGravity, Scene), &setGravity, &getGravity, &writeGravity, "" );
  239. addField("VelocityIterations", TypeS32, Offset(mVelocityIterations, Scene), &writeVelocityIterations, "" );
  240. addField("PositionIterations", TypeS32, Offset(mPositionIterations, Scene), &writePositionIterations, "" );
  241. // Layer sort modes.
  242. char buffer[64];
  243. for ( U32 n = 0; n < MAX_LAYERS_SUPPORTED; n++ )
  244. {
  245. dSprintf( buffer, 64, "layerSortMode%d", n );
  246. addField( buffer, TypeEnum, OffsetNonConst(mLayerSortModes[n], Scene), &writeLayerSortMode, 1, &SceneRenderQueue::renderSortTable, "");
  247. }
  248. addProtectedField("Controllers", TypeSimObjectPtr, Offset(mControllers, Scene), &defaultProtectedNotSetFn, &defaultProtectedGetFn, &defaultProtectedNotWriteFn, "The scene controllers to use.");
  249. // Callbacks.
  250. addField("UpdateCallback", TypeBool, Offset(mUpdateCallback, Scene), &writeUpdateCallback, "");
  251. addField("RenderCallback", TypeBool, Offset(mRenderCallback, Scene), &writeRenderCallback, "");
  252. }
  253. //-----------------------------------------------------------------------------
  254. void Scene::BeginContact( b2Contact* pContact )
  255. {
  256. // Ignore contact if it's not a touching contact.
  257. if ( !pContact->IsTouching() )
  258. return;
  259. PROFILE_SCOPE(Scene_BeginContact);
  260. // Fetch fixtures.
  261. b2Fixture* pFixtureA = pContact->GetFixtureA();
  262. b2Fixture* pFixtureB = pContact->GetFixtureB();
  263. // Fetch physics proxies.
  264. PhysicsProxy* pPhysicsProxyA = static_cast<PhysicsProxy*>(pFixtureA->GetBody()->GetUserData());
  265. PhysicsProxy* pPhysicsProxyB = static_cast<PhysicsProxy*>(pFixtureB->GetBody()->GetUserData());
  266. // Ignore stuff that's not a scene object.
  267. if ( pPhysicsProxyA->getPhysicsProxyType() != PhysicsProxy::PHYSIC_PROXY_SCENEOBJECT ||
  268. pPhysicsProxyB->getPhysicsProxyType() != PhysicsProxy::PHYSIC_PROXY_SCENEOBJECT )
  269. {
  270. return;
  271. }
  272. // Fetch scene objects.
  273. SceneObject* pSceneObjectA = static_cast<SceneObject*>(pPhysicsProxyA);
  274. SceneObject* pSceneObjectB = static_cast<SceneObject*>(pPhysicsProxyB);
  275. // Initialize the contact.
  276. TickContact tickContact;
  277. tickContact.initialize( pContact, pSceneObjectA, pSceneObjectB, pFixtureA, pFixtureB );
  278. // Add contact.
  279. mBeginContacts.insert( pContact, tickContact );
  280. }
  281. //-----------------------------------------------------------------------------
  282. void Scene::EndContact( b2Contact* pContact )
  283. {
  284. PROFILE_SCOPE(Scene_EndContact);
  285. // Fetch fixtures.
  286. b2Fixture* pFixtureA = pContact->GetFixtureA();
  287. b2Fixture* pFixtureB = pContact->GetFixtureB();
  288. // Fetch physics proxies.
  289. PhysicsProxy* pPhysicsProxyA = static_cast<PhysicsProxy*>(pFixtureA->GetBody()->GetUserData());
  290. PhysicsProxy* pPhysicsProxyB = static_cast<PhysicsProxy*>(pFixtureB->GetBody()->GetUserData());
  291. // Ignore stuff that's not a scene object.
  292. if ( pPhysicsProxyA->getPhysicsProxyType() != PhysicsProxy::PHYSIC_PROXY_SCENEOBJECT ||
  293. pPhysicsProxyB->getPhysicsProxyType() != PhysicsProxy::PHYSIC_PROXY_SCENEOBJECT )
  294. {
  295. return;
  296. }
  297. // Fetch scene objects.
  298. SceneObject* pSceneObjectA = static_cast<SceneObject*>(pPhysicsProxyA);
  299. SceneObject* pSceneObjectB = static_cast<SceneObject*>(pPhysicsProxyB);
  300. // Initialize the contact.
  301. TickContact tickContact;
  302. tickContact.initialize( pContact, pSceneObjectA, pSceneObjectB, pFixtureA, pFixtureB );
  303. // Add contact.
  304. mEndContacts.push_back( tickContact );
  305. }
  306. //-----------------------------------------------------------------------------
  307. void Scene::PostSolve( b2Contact* pContact, const b2ContactImpulse* pImpulse )
  308. {
  309. // Find contact mapping.
  310. typeContactHash::iterator contactItr = mBeginContacts.find( pContact );
  311. // Finish if we didn't find the contact.
  312. if ( contactItr == mBeginContacts.end() )
  313. return;
  314. // Fetch contact.
  315. TickContact& tickContact = contactItr->value;
  316. // Add the impulse.
  317. for ( U32 index = 0; index < b2_maxManifoldPoints; ++index )
  318. {
  319. tickContact.mNormalImpulses[index] += pImpulse->normalImpulses[index];
  320. tickContact.mTangentImpulses[index] += pImpulse->tangentImpulses[index];
  321. }
  322. }
  323. //-----------------------------------------------------------------------------
  324. void Scene::forwardContacts( void )
  325. {
  326. // Debug Profiling.
  327. PROFILE_SCOPE(Scene_ForwardContacts);
  328. // Iterate end contacts.
  329. for( typeContactVector::iterator contactItr = mEndContacts.begin(); contactItr != mEndContacts.end(); ++contactItr )
  330. {
  331. // Fetch tick contact.
  332. TickContact& tickContact = *contactItr;
  333. // Inform the scene objects.
  334. tickContact.mpSceneObjectA->onEndCollision( tickContact );
  335. tickContact.mpSceneObjectB->onEndCollision( tickContact );
  336. }
  337. // Iterate begin contacts.
  338. for( typeContactHash::iterator contactItr = mBeginContacts.begin(); contactItr != mBeginContacts.end(); ++contactItr )
  339. {
  340. // Fetch tick contact.
  341. TickContact& tickContact = contactItr->value;
  342. // Inform the scene objects.
  343. tickContact.mpSceneObjectA->onBeginCollision( tickContact );
  344. tickContact.mpSceneObjectB->onBeginCollision( tickContact );
  345. }
  346. }
  347. //-----------------------------------------------------------------------------
  348. void Scene::dispatchBeginContactCallbacks( void )
  349. {
  350. // Debug Profiling.
  351. PROFILE_SCOPE(Scene_DispatchBeginContactCallbacks);
  352. // Sanity!
  353. AssertFatal( b2_maxManifoldPoints == 2, "Scene::dispatchBeginContactCallbacks() - Invalid assumption about max manifold points." );
  354. // Fetch contact count.
  355. const U32 contactCount = mBeginContacts.size();
  356. // Finish if no contacts.
  357. if ( contactCount == 0 )
  358. return;
  359. // Iterate all contacts.
  360. for ( typeContactHash::iterator contactItr = mBeginContacts.begin(); contactItr != mBeginContacts.end(); ++contactItr )
  361. {
  362. // Fetch contact.
  363. const TickContact& tickContact = contactItr->value;
  364. // Fetch scene objects.
  365. SceneObject* pSceneObjectA = tickContact.mpSceneObjectA;
  366. SceneObject* pSceneObjectB = tickContact.mpSceneObjectB;
  367. // Skip if either object is being deleted.
  368. if ( pSceneObjectA->isBeingDeleted() || pSceneObjectB->isBeingDeleted() )
  369. continue;
  370. // Skip if both objects don't have collision callback active.
  371. if ( !pSceneObjectA->getCollisionCallback() && !pSceneObjectB->getCollisionCallback() )
  372. continue;
  373. // Fetch normal and contact points.
  374. const U32& pointCount = tickContact.mPointCount;
  375. const b2Vec2& normal = tickContact.mWorldManifold.normal;
  376. const b2Vec2& point1 = tickContact.mWorldManifold.points[0];
  377. const b2Vec2& point2 = tickContact.mWorldManifold.points[1];
  378. const S32 shapeIndexA = pSceneObjectA->getCollisionShapeIndex( tickContact.mpFixtureA );
  379. const S32 shapeIndexB = pSceneObjectB->getCollisionShapeIndex( tickContact.mpFixtureB );
  380. // Sanity!
  381. AssertFatal( shapeIndexA >= 0, "Scene::dispatchBeginContactCallbacks() - Cannot find shape index reported on physics proxy of a fixture." );
  382. AssertFatal( shapeIndexB >= 0, "Scene::dispatchBeginContactCallbacks() - Cannot find shape index reported on physics proxy of a fixture." );
  383. // Fetch collision impulse information
  384. const F32 normalImpulse1 = tickContact.mNormalImpulses[0];
  385. const F32 normalImpulse2 = tickContact.mNormalImpulses[1];
  386. const F32 tangentImpulse1 = tickContact.mTangentImpulses[0];
  387. const F32 tangentImpulse2 = tickContact.mTangentImpulses[1];
  388. // Format objects.
  389. char sceneObjectABuffer[16];
  390. char sceneObjectBBuffer[16];
  391. dSprintf( sceneObjectABuffer, sizeof(sceneObjectABuffer), "%d", pSceneObjectA->getId() );
  392. dSprintf( sceneObjectBBuffer, sizeof(sceneObjectBBuffer), "%d", pSceneObjectB->getId() );
  393. // Format miscellaneous information twice so object b can see things from his point of view.
  394. char miscInfoBufferA[128];
  395. char miscInfoBufferB[128];
  396. if ( pointCount == 2 )
  397. {
  398. dSprintf(miscInfoBufferA, sizeof(miscInfoBufferA),
  399. "%d %d %0.4f %0.4f %0.4f %0.4f %0.4f %0.4f %0.4f %0.4f %0.4f %0.4f",
  400. shapeIndexA, shapeIndexB,
  401. -normal.x, -normal.y,
  402. point1.x, point1.y,
  403. normalImpulse1,
  404. tangentImpulse1,
  405. point2.x, point2.y,
  406. normalImpulse2,
  407. tangentImpulse2 );
  408. dSprintf(miscInfoBufferB, sizeof(miscInfoBufferB),
  409. "%d %d %0.4f %0.4f %0.4f %0.4f %0.4f %0.4f %0.4f %0.4f %0.4f %0.4f",
  410. shapeIndexB, shapeIndexA,
  411. normal.x, normal.y,
  412. point1.x, point1.y,
  413. normalImpulse1,
  414. tangentImpulse1,
  415. point2.x, point2.y,
  416. normalImpulse2,
  417. tangentImpulse2);
  418. }
  419. else if ( pointCount == 1 )
  420. {
  421. dSprintf(miscInfoBufferA, sizeof(miscInfoBufferA),
  422. "%d %d %0.4f %0.4f %0.4f %0.4f %0.4f %0.4f",
  423. shapeIndexA, shapeIndexB,
  424. -normal.x, -normal.y,
  425. point1.x, point1.y,
  426. normalImpulse1,
  427. tangentImpulse1 );
  428. dSprintf(miscInfoBufferB, sizeof(miscInfoBufferB),
  429. "%d %d %0.4f %0.4f %0.4f %0.4f %0.4f %0.4f",
  430. shapeIndexB, shapeIndexA,
  431. normal.x, normal.y,
  432. point1.x, point1.y,
  433. normalImpulse1,
  434. tangentImpulse1);
  435. }
  436. else
  437. {
  438. dSprintf(miscInfoBufferA, sizeof(miscInfoBufferA),
  439. "%d %d",
  440. shapeIndexA, shapeIndexB );
  441. dSprintf(miscInfoBufferB, sizeof(miscInfoBufferB),
  442. "%d %d",
  443. shapeIndexB, shapeIndexA);
  444. }
  445. // Does the scene handle the collision callback?
  446. Namespace* pNamespace = getNamespace();
  447. if ( pNamespace != NULL && pNamespace->lookup( StringTable->insert( "onSceneCollision" ) ) != NULL )
  448. {
  449. // Yes, so perform script callback on the Scene.
  450. Con::executef( this, 4, "onSceneCollision",
  451. sceneObjectABuffer,
  452. sceneObjectBBuffer,
  453. miscInfoBufferA );
  454. }
  455. else
  456. {
  457. // No, so call it on its behaviors.
  458. const char* args[5] = { "onSceneCollision", "", sceneObjectABuffer, sceneObjectBBuffer, miscInfoBufferA };
  459. callOnBehaviors( 5, args );
  460. }
  461. // Is object A allowed to collide with object B?
  462. if ( (pSceneObjectA->mCollisionGroupMask & pSceneObjectB->mSceneGroupMask) != 0 &&
  463. (pSceneObjectA->mCollisionLayerMask & pSceneObjectB->mSceneLayerMask) != 0 )
  464. {
  465. // Yes, so does it handle the collision callback?
  466. if ( pSceneObjectA->isMethod("onCollision") )
  467. {
  468. // Yes, so perform the script callback on it.
  469. Con::executef( pSceneObjectA, 3, "onCollision",
  470. sceneObjectBBuffer,
  471. miscInfoBufferA );
  472. }
  473. else
  474. {
  475. // No, so call it on its behaviors.
  476. const char* args[4] = { "onCollision", "", sceneObjectBBuffer, miscInfoBufferA };
  477. pSceneObjectA->callOnBehaviors( 4, args );
  478. }
  479. }
  480. // Is object B allowed to collide with object A?
  481. if ( (pSceneObjectB->mCollisionGroupMask & pSceneObjectA->mSceneGroupMask) != 0 &&
  482. (pSceneObjectB->mCollisionLayerMask & pSceneObjectA->mSceneLayerMask) != 0 )
  483. {
  484. // Yes, so does it handle the collision callback?
  485. if ( pSceneObjectB->isMethod("onCollision") )
  486. {
  487. // Yes, so perform the script callback on it.
  488. Con::executef( pSceneObjectB, 3, "onCollision",
  489. sceneObjectABuffer,
  490. miscInfoBufferB );
  491. }
  492. else
  493. {
  494. // No, so call it on its behaviors.
  495. const char* args[4] = { "onCollision", "", sceneObjectABuffer, miscInfoBufferB };
  496. pSceneObjectB->callOnBehaviors( 4, args );
  497. }
  498. }
  499. }
  500. }
  501. //-----------------------------------------------------------------------------
  502. void Scene::dispatchEndContactCallbacks( void )
  503. {
  504. // Debug Profiling.
  505. PROFILE_SCOPE(Scene_DispatchEndContactCallbacks);
  506. // Sanity!
  507. AssertFatal( b2_maxManifoldPoints == 2, "Scene::dispatchEndContactCallbacks() - Invalid assumption about max manifold points." );
  508. // Fetch contact count.
  509. const U32 contactCount = mEndContacts.size();
  510. // Finish if no contacts.
  511. if ( contactCount == 0 )
  512. return;
  513. // Iterate all contacts.
  514. for ( typeContactVector::iterator contactItr = mEndContacts.begin(); contactItr != mEndContacts.end(); ++contactItr )
  515. {
  516. // Fetch contact.
  517. const TickContact& tickContact = *contactItr;
  518. // Fetch scene objects.
  519. SceneObject* pSceneObjectA = tickContact.mpSceneObjectA;
  520. SceneObject* pSceneObjectB = tickContact.mpSceneObjectB;
  521. // Skip if either object is being deleted.
  522. if ( pSceneObjectA->isBeingDeleted() || pSceneObjectB->isBeingDeleted() )
  523. continue;
  524. // Skip if both objects don't have collision callback active.
  525. if ( !pSceneObjectA->getCollisionCallback() && !pSceneObjectB->getCollisionCallback() )
  526. continue;
  527. // Fetch shape index.
  528. const S32 shapeIndexA = pSceneObjectA->getCollisionShapeIndex( tickContact.mpFixtureA );
  529. const S32 shapeIndexB = pSceneObjectB->getCollisionShapeIndex( tickContact.mpFixtureB );
  530. // Sanity!
  531. AssertFatal( shapeIndexA >= 0, "Scene::dispatchEndContactCallbacks() - Cannot find shape index reported on physics proxy of a fixture." );
  532. AssertFatal( shapeIndexB >= 0, "Scene::dispatchEndContactCallbacks() - Cannot find shape index reported on physics proxy of a fixture." );
  533. // Format objects.
  534. char sceneObjectABuffer[16];
  535. char sceneObjectBBuffer[16];
  536. dSprintf( sceneObjectABuffer, sizeof(sceneObjectABuffer), "%d", pSceneObjectA->getId() );
  537. dSprintf( sceneObjectBBuffer, sizeof(sceneObjectBBuffer), "%d", pSceneObjectB->getId() );
  538. // Format miscellaneous information.
  539. char miscInfoBuffer[32];
  540. dSprintf(miscInfoBuffer, sizeof(miscInfoBuffer), "%d %d", shapeIndexA, shapeIndexB );
  541. // Does the scene handle the collision callback?
  542. Namespace* pNamespace = getNamespace();
  543. if ( pNamespace != NULL && pNamespace->lookup( StringTable->insert( "onSceneEndCollision" ) ) != NULL )
  544. {
  545. // Yes, so does the scene handle the collision callback?
  546. Con::executef( this, 4, "onSceneEndCollision",
  547. sceneObjectABuffer,
  548. sceneObjectBBuffer,
  549. miscInfoBuffer );
  550. }
  551. else
  552. {
  553. // No, so call it on its behaviors.
  554. const char* args[5] = { "onSceneEndCollision", "", sceneObjectABuffer, sceneObjectBBuffer, miscInfoBuffer };
  555. callOnBehaviors( 5, args );
  556. }
  557. // Is object A allowed to collide with object B?
  558. if ( (pSceneObjectA->mCollisionGroupMask & pSceneObjectB->mSceneGroupMask) != 0 &&
  559. (pSceneObjectA->mCollisionLayerMask & pSceneObjectB->mSceneLayerMask) != 0 )
  560. {
  561. // Yes, so does it handle the collision callback?
  562. if ( pSceneObjectA->isMethod("onEndCollision") )
  563. {
  564. // Yes, so perform the script callback on it.
  565. Con::executef( pSceneObjectA, 3, "onEndCollision",
  566. sceneObjectBBuffer,
  567. miscInfoBuffer );
  568. }
  569. else
  570. {
  571. // No, so call it on its behaviors.
  572. const char* args[4] = { "onEndCollision", "", sceneObjectBBuffer, miscInfoBuffer };
  573. pSceneObjectA->callOnBehaviors( 4, args );
  574. }
  575. }
  576. // Is object B allowed to collide with object A?
  577. if ( (pSceneObjectB->mCollisionGroupMask & pSceneObjectA->mSceneGroupMask) != 0 &&
  578. (pSceneObjectB->mCollisionLayerMask & pSceneObjectA->mSceneLayerMask) != 0 )
  579. {
  580. // Yes, so does it handle the collision callback?
  581. if ( pSceneObjectB->isMethod("onEndCollision") )
  582. {
  583. // Yes, so perform the script callback on it.
  584. Con::executef( pSceneObjectB, 3, "onEndCollision",
  585. sceneObjectABuffer,
  586. miscInfoBuffer );
  587. }
  588. else
  589. {
  590. // No, so call it on its behaviors.
  591. const char* args[4] = { "onEndCollision", "", sceneObjectABuffer, miscInfoBuffer };
  592. pSceneObjectB->callOnBehaviors( 4, args );
  593. }
  594. }
  595. }
  596. }
  597. //-----------------------------------------------------------------------------
  598. void Scene::processTick( void )
  599. {
  600. // Debug Profiling.
  601. PROFILE_SCOPE(Scene_ProcessTick);
  602. // Finish if the Scene is not added to the simulation.
  603. if ( !isProperlyAdded() )
  604. return;
  605. // Process Delete Requests.
  606. processDeleteRequests(false);
  607. // Update debug stats.
  608. mDebugStats.fps = Con::getFloatVariable("fps::framePeriod", 0.0f);
  609. mDebugStats.frameCount = (U32)Con::getIntVariable("fps::frameCount", 0);
  610. mDebugStats.bodyCount = (U32)mpWorld->GetBodyCount();
  611. mDebugStats.jointCount = (U32)mpWorld->GetJointCount();
  612. mDebugStats.contactCount = (U32)mpWorld->GetContactCount();
  613. mDebugStats.proxyCount = (U32)mpWorld->GetProxyCount();
  614. mDebugStats.objectsCount = (U32)mSceneObjects.size();
  615. mDebugStats.worldProfile = mpWorld->GetProfile();
  616. // Set particle stats.
  617. mDebugStats.particlesAlloc = ParticleSystem::Instance->getAllocatedParticleCount();
  618. mDebugStats.particlesUsed = ParticleSystem::Instance->getActiveParticleCount();
  619. mDebugStats.particlesFree = mDebugStats.particlesAlloc - mDebugStats.particlesUsed;
  620. // Finish if scene is paused.
  621. if ( !getScenePause() )
  622. {
  623. // Reset object stats.
  624. U32 objectsEnabled = 0;
  625. U32 objectsVisible = 0;
  626. U32 objectsAwake = 0;
  627. // Fetch if a "normal" i.e. non-editor scene.
  628. const bool isNormalScene = !getIsEditorScene();
  629. // Update scene time.
  630. mSceneTime += Tickable::smTickSec;
  631. // Clear ticked scene objects.
  632. mTickedSceneObjects.clear();
  633. // Iterate scene objects.
  634. for( S32 n = 0; n < mSceneObjects.size(); ++n )
  635. {
  636. // Fetch scene object.
  637. SceneObject* pSceneObject = mSceneObjects[n];
  638. // Update awake/asleep counts.
  639. if ( pSceneObject->getAwake() )
  640. objectsAwake++;
  641. // Update visible.
  642. if ( pSceneObject->getVisible() )
  643. objectsVisible++;
  644. // Push scene object if it's eligible for ticking.
  645. if ( pSceneObject->isEnabled() )
  646. {
  647. // Update enabled.
  648. objectsEnabled++;
  649. // Add to ticked objects if object is not being deleted and this is a "normal" scene or
  650. // the object is marked as allowing editor ticks.
  651. if ( !pSceneObject->isBeingDeleted() && (isNormalScene || pSceneObject->getIsEditorTickAllowed() ) )
  652. mTickedSceneObjects.push_back( pSceneObject );
  653. }
  654. }
  655. // Update object stats.
  656. mDebugStats.objectsEnabled = objectsEnabled;
  657. mDebugStats.objectsVisible = objectsVisible;
  658. mDebugStats.objectsAwake = objectsAwake;
  659. // Debug Status Reference.
  660. DebugStats* pDebugStats = &mDebugStats;
  661. // Fetch ticked scene object count.
  662. const S32 tickedSceneObjectCount = mTickedSceneObjects.size();
  663. // ****************************************************
  664. // Pre-integrate objects.
  665. // ****************************************************
  666. // Iterate ticked scene objects.
  667. for ( S32 i = 0; i < tickedSceneObjectCount; ++i )
  668. {
  669. // Debug Profiling.
  670. PROFILE_SCOPE(Scene_PreIntegrate);
  671. // Pre-integrate.
  672. mTickedSceneObjects[i]->preIntegrate( mSceneTime, Tickable::smTickSec, pDebugStats );
  673. }
  674. // ****************************************************
  675. // Integrate controllers.
  676. // ****************************************************
  677. // Fetch the controller set.
  678. SimSet* pControllerSet = getControllers();
  679. // Do we have any scene controllers?
  680. if ( pControllerSet != NULL )
  681. {
  682. // Debug Profiling.
  683. PROFILE_SCOPE(Scene_IntegrateSceneControllers);
  684. // Yes, so fetch scene controller count.
  685. const S32 sceneControllerCount = (S32)pControllerSet->size();
  686. // Iterate scene controllers.
  687. for( S32 i = 0; i < sceneControllerCount; i++ )
  688. {
  689. // Fetch the scene controller.
  690. SceneController* pController = dynamic_cast<SceneController*>((*pControllerSet)[i]);
  691. // Skip if not a controller.
  692. if ( pController == NULL )
  693. continue;
  694. // Integrate.
  695. pController->integrate( this, mSceneTime, Tickable::smTickSec, pDebugStats );
  696. }
  697. }
  698. // Debug Profiling.
  699. PROFILE_START(Scene_IntegratePhysicsSystem);
  700. // Reset contacts.
  701. mBeginContacts.clear();
  702. mEndContacts.clear();
  703. // Only step the physics if a "normal" scene.
  704. if ( isNormalScene )
  705. {
  706. // Step the physics.
  707. mpWorld->Step( Tickable::smTickSec, mVelocityIterations, mPositionIterations );
  708. }
  709. // Debug Profiling.
  710. PROFILE_END(); // Scene_IntegratePhysicsSystem
  711. // Forward the contacts.
  712. forwardContacts();
  713. // ****************************************************
  714. // Integrate objects.
  715. // ****************************************************
  716. // Iterate ticked scene objects.
  717. for ( S32 i = 0; i < tickedSceneObjectCount; ++i )
  718. {
  719. // Debug Profiling.
  720. PROFILE_SCOPE(Scene_IntegrateObject);
  721. // Integrate.
  722. mTickedSceneObjects[i]->integrateObject( mSceneTime, Tickable::smTickSec, pDebugStats );
  723. }
  724. // ****************************************************
  725. // Post-Integrate Stage.
  726. // ****************************************************
  727. // Iterate ticked scene objects.
  728. for ( S32 i = 0; i < tickedSceneObjectCount; ++i )
  729. {
  730. // Debug Profiling.
  731. PROFILE_SCOPE(Scene_PostIntegrate);
  732. // Post-integrate.
  733. mTickedSceneObjects[i]->postIntegrate( mSceneTime, Tickable::smTickSec, pDebugStats );
  734. }
  735. // Scene update callback.
  736. if( mUpdateCallback )
  737. {
  738. // Debug Profiling.
  739. PROFILE_SCOPE(Scene_OnSceneUpdatetCallback);
  740. Con::executef( this, 1, "onSceneUpdate" );
  741. }
  742. // Only dispatch contacts if a "normal" scene.
  743. if ( isNormalScene )
  744. {
  745. // Dispatch contacts callbacks.
  746. dispatchEndContactCallbacks();
  747. dispatchBeginContactCallbacks();
  748. }
  749. // Clear ticked scene objects.
  750. mTickedSceneObjects.clear();
  751. }
  752. // Update debug stat ranges.
  753. mDebugStats.updateRanges();
  754. }
  755. //-----------------------------------------------------------------------------
  756. void Scene::interpolateTick( F32 timeDelta )
  757. {
  758. // Finish if scene is paused.
  759. if ( getScenePause() ) return;
  760. // Debug Profiling.
  761. PROFILE_SCOPE(Scene_InterpolateTick);
  762. // ****************************************************
  763. // Interpolate scene objects.
  764. // ****************************************************
  765. // Fetch the scene object count.
  766. const S32 sceneObjectCount = mSceneObjects.size();
  767. // Iterate scene objects.
  768. for( S32 n = 0; n < sceneObjectCount; ++n )
  769. {
  770. // Fetch scene object.
  771. SceneObject* pSceneObject = mSceneObjects[n];
  772. // Skip interpolation of scene object if it's not eligible.
  773. if ( !pSceneObject->isEnabled() || pSceneObject->isBeingDeleted() )
  774. continue;
  775. pSceneObject->interpolateObject( timeDelta );
  776. }
  777. }
  778. //-----------------------------------------------------------------------------
  779. void Scene::sceneRender( const SceneRenderState* pSceneRenderState )
  780. {
  781. // Debug Profiling.
  782. PROFILE_SCOPE(Scene_RenderSceneTotal);
  783. // Fetch debug stats.
  784. DebugStats* pDebugStats = pSceneRenderState->mpDebugStats;
  785. // Reset Render Stats.
  786. pDebugStats->renderPicked = 0;
  787. pDebugStats->renderRequests = 0;
  788. pDebugStats->renderFallbacks = 0;
  789. pDebugStats->batchTrianglesSubmitted = 0;
  790. pDebugStats->batchDrawCallsStrict = 0;
  791. pDebugStats->batchDrawCallsSorted = 0;
  792. pDebugStats->batchFlushes = 0;
  793. pDebugStats->batchBlendStateFlush = 0;
  794. pDebugStats->batchColorStateFlush = 0;
  795. pDebugStats->batchAlphaStateFlush = 0;
  796. pDebugStats->batchTextureChangeFlush = 0;
  797. pDebugStats->batchBufferFullFlush = 0;
  798. pDebugStats->batchIsolatedFlush = 0;
  799. pDebugStats->batchLayerFlush = 0;
  800. pDebugStats->batchNoBatchFlush = 0;
  801. pDebugStats->batchAnonymousFlush = 0;
  802. // Set batch renderer wireframe mode.
  803. mBatchRenderer.setWireframeMode( getDebugMask() & SCENE_DEBUG_WIREFRAME_RENDER );
  804. // Debug Profiling.
  805. PROFILE_START(Scene_RenderSceneVisibleQuery);
  806. // Rotate the render AABB by the camera angle.
  807. b2AABB cameraAABB;
  808. CoreMath::mRotateAABB( pSceneRenderState->mRenderAABB, pSceneRenderState->mRenderAngle, cameraAABB );
  809. // Rotate the world matrix by the camera angle.
  810. const Vector2& cameraPosition = pSceneRenderState->mRenderPosition;
  811. glTranslatef( cameraPosition.x, cameraPosition.y, 0.0f );
  812. glRotatef( mRadToDeg(pSceneRenderState->mRenderAngle), 0.0f, 0.0f, 1.0f );
  813. glTranslatef( -cameraPosition.x, -cameraPosition.y, 0.0f );
  814. // Clear world query.
  815. mpWorldQuery->clearQuery();
  816. // Set filter.
  817. WorldQueryFilter queryFilter( pSceneRenderState->mRenderLayerMask, pSceneRenderState->mRenderGroupMask, true, true, false, false );
  818. mpWorldQuery->setQueryFilter( queryFilter );
  819. // Query render AABB.
  820. mpWorldQuery->aabbQueryAABB( cameraAABB );
  821. // Debug Profiling.
  822. PROFILE_END(); //Scene_RenderSceneVisibleQuery
  823. // Are there any query results?
  824. if ( mpWorldQuery->getQueryResultsCount() > 0 )
  825. {
  826. // Debug Profiling.
  827. PROFILE_SCOPE(Scene_RenderSceneCompileRenderRequests);
  828. // Fetch the primary scene render queue.
  829. SceneRenderQueue* pSceneRenderQueue = SceneRenderQueueFactory.createObject();
  830. // Yes so step through layers.
  831. for ( S32 layer = MAX_LAYERS_SUPPORTED-1; layer >= 0 ; layer-- )
  832. {
  833. // Fetch layer.
  834. typeWorldQueryResultVector& layerResults = mpWorldQuery->getLayeredQueryResults( layer );
  835. // Fetch layer object count.
  836. const U32 layerObjectCount = layerResults.size();
  837. // Are there any objects to render in this layer?
  838. if ( layerObjectCount > 0 )
  839. {
  840. // Yes, so increase render picked.
  841. pDebugStats->renderPicked += layerObjectCount;
  842. // Iterate query results.
  843. for( typeWorldQueryResultVector::iterator worldQueryItr = layerResults.begin(); worldQueryItr != layerResults.end(); ++worldQueryItr )
  844. {
  845. // Fetch scene object.
  846. SceneObject* pSceneObject = worldQueryItr->mpSceneObject;
  847. // Skip if the object should not render.
  848. if ( !pSceneObject->shouldRender() )
  849. continue;
  850. // Can the scene object prepare a render?
  851. if ( pSceneObject->canPrepareRender() )
  852. {
  853. // Yes. so is it batch isolated.
  854. if ( pSceneObject->getBatchIsolated() )
  855. {
  856. // Yes, so create a default render request on the primary queue.
  857. SceneRenderRequest* pIsolatedSceneRenderRequest = Scene::createDefaultRenderRequest( pSceneRenderQueue, pSceneObject );
  858. // Create a new isolated render queue.
  859. pIsolatedSceneRenderRequest->mpIsolatedRenderQueue = SceneRenderQueueFactory.createObject();
  860. // Prepare in the isolated queue.
  861. pSceneObject->scenePrepareRender( pSceneRenderState, pIsolatedSceneRenderRequest->mpIsolatedRenderQueue );
  862. // Increase render request count.
  863. pDebugStats->renderRequests += (U32)pIsolatedSceneRenderRequest->mpIsolatedRenderQueue->getRenderRequests().size();
  864. // Adjust for the extra private render request.
  865. pDebugStats->renderRequests -= 1;
  866. }
  867. else
  868. {
  869. // No, so prepare in primary queue.
  870. pSceneObject->scenePrepareRender( pSceneRenderState, pSceneRenderQueue );
  871. }
  872. }
  873. else
  874. {
  875. // No, so create a default render request for it.
  876. Scene::createDefaultRenderRequest( pSceneRenderQueue, pSceneObject );
  877. }
  878. }
  879. // Fetch render requests.
  880. SceneRenderQueue::typeRenderRequestVector& sceneRenderRequests = pSceneRenderQueue->getRenderRequests();
  881. // Fetch render request count.
  882. const U32 renderRequestCount = (U32)sceneRenderRequests.size();
  883. // Increase render request count.
  884. pDebugStats->renderRequests += renderRequestCount;
  885. // Do we have more than a single render request?
  886. if ( renderRequestCount > 1 )
  887. {
  888. // Debug Profiling.
  889. PROFILE_SCOPE(Scene_RenderSceneLayerSorting);
  890. // Yes, so fetch layer sort mode.
  891. SceneRenderQueue::RenderSort& mode = mLayerSortModes[layer];
  892. // Temporarily switch to normal sort if batch sort but batcher disabled.
  893. if ( !mBatchRenderer.getBatchEnabled() && mode == SceneRenderQueue::RENDER_SORT_BATCH )
  894. mode = SceneRenderQueue::RENDER_SORT_NEWEST;
  895. // Set render queue mode.
  896. pSceneRenderQueue->setSortMode( mode );
  897. // Sort the render requests.
  898. pSceneRenderQueue->sort();
  899. }
  900. // Iterate render requests.
  901. for( SceneRenderQueue::typeRenderRequestVector::iterator renderRequestItr = sceneRenderRequests.begin(); renderRequestItr != sceneRenderRequests.end(); ++renderRequestItr )
  902. {
  903. // Debug Profiling.
  904. PROFILE_SCOPE(Scene_RenderSceneRequests);
  905. // Fetch render request.
  906. SceneRenderRequest* pSceneRenderRequest = *renderRequestItr;
  907. // Fetch scene render object.
  908. SceneRenderObject* pSceneRenderObject = pSceneRenderRequest->mpSceneRenderObject;
  909. // Flush if the object is not render batched and we're in strict order mode.
  910. if ( !pSceneRenderObject->isBatchRendered() && mBatchRenderer.getStrictOrderMode() )
  911. {
  912. mBatchRenderer.flush( pDebugStats->batchNoBatchFlush );
  913. }
  914. // Flush if the object is batch isolated.
  915. else if ( pSceneRenderObject->getBatchIsolated() )
  916. {
  917. mBatchRenderer.flush( pDebugStats->batchIsolatedFlush );
  918. }
  919. // Yes, so is the object batch rendered?
  920. if ( pSceneRenderObject->isBatchRendered() )
  921. {
  922. // Yes, so set the blend mode.
  923. mBatchRenderer.setBlendMode( pSceneRenderRequest );
  924. // Set the alpha test mode.
  925. mBatchRenderer.setAlphaTestMode( pSceneRenderRequest );
  926. }
  927. // Set batch strict order mode.
  928. // NOTE: We keep reasserting this because an object is free to change it during rendering.
  929. mBatchRenderer.setStrictOrderMode( pSceneRenderQueue->getStrictOrderMode() );
  930. // Is the object batch isolated?
  931. if ( pSceneRenderObject->getBatchIsolated() )
  932. {
  933. // Yes, so fetch isolated render queue.
  934. SceneRenderQueue* pIsolatedRenderQueue = pSceneRenderRequest->mpIsolatedRenderQueue;
  935. // Sanity!
  936. AssertFatal( pIsolatedRenderQueue != NULL, "Cannot render batch isolated with an isolated render queue." );
  937. // Sort the isolated render requests.
  938. pIsolatedRenderQueue->sort();
  939. // Fetch isolated render requests.
  940. SceneRenderQueue::typeRenderRequestVector& isolatedRenderRequests = pIsolatedRenderQueue->getRenderRequests();
  941. // Can the object render?
  942. if ( pSceneRenderObject->validRender() )
  943. {
  944. // Yes, so iterate isolated render requests.
  945. for( SceneRenderQueue::typeRenderRequestVector::iterator isolatedRenderRequestItr = isolatedRenderRequests.begin(); isolatedRenderRequestItr != isolatedRenderRequests.end(); ++isolatedRenderRequestItr )
  946. {
  947. pSceneRenderObject->sceneRender( pSceneRenderState, *isolatedRenderRequestItr, &mBatchRenderer );
  948. }
  949. }
  950. else
  951. {
  952. // No, so iterate isolated render requests.
  953. for( SceneRenderQueue::typeRenderRequestVector::iterator isolatedRenderRequestItr = isolatedRenderRequests.begin(); isolatedRenderRequestItr != isolatedRenderRequests.end(); ++isolatedRenderRequestItr )
  954. {
  955. pSceneRenderObject->sceneRenderFallback( pSceneRenderState, *isolatedRenderRequestItr, &mBatchRenderer );
  956. }
  957. // Increase render fallbacks.
  958. pDebugStats->renderFallbacks++;
  959. }
  960. // Flush isolated batch.
  961. mBatchRenderer.flush( pDebugStats->batchIsolatedFlush );
  962. }
  963. else
  964. {
  965. // No, so can the object render?
  966. if ( pSceneRenderObject->validRender() )
  967. {
  968. // Yes, so render object.
  969. pSceneRenderObject->sceneRender( pSceneRenderState, pSceneRenderRequest, &mBatchRenderer );
  970. }
  971. else
  972. {
  973. // No, so render using fallback.
  974. pSceneRenderObject->sceneRenderFallback( pSceneRenderState, pSceneRenderRequest, &mBatchRenderer );
  975. // Increase render fallbacks.
  976. pDebugStats->renderFallbacks++;
  977. }
  978. }
  979. }
  980. // Flush.
  981. // NOTE: We cannot batch between layers as we adhere to a strict layer render order.
  982. mBatchRenderer.flush( pDebugStats->batchLayerFlush );
  983. // Iterate query results.
  984. for( typeWorldQueryResultVector::iterator worldQueryItr = layerResults.begin(); worldQueryItr != layerResults.end(); ++worldQueryItr )
  985. {
  986. // Debug Profiling.
  987. PROFILE_SCOPE(Scene_RenderObjectOverlays);
  988. // Fetch scene object.
  989. SceneObject* pSceneObject = worldQueryItr->mpSceneObject;
  990. // Render object overlay.
  991. pSceneObject->sceneRenderOverlay( pSceneRenderState );
  992. }
  993. }
  994. // Reset render queue.
  995. pSceneRenderQueue->resetState();
  996. }
  997. // Cache render queue..
  998. SceneRenderQueueFactory.cacheObject( pSceneRenderQueue );
  999. }
  1000. // Draw controllers.
  1001. if ( getDebugMask() & Scene::SCENE_DEBUG_CONTROLLERS )
  1002. {
  1003. // Fetch the controller set.
  1004. SimSet* pControllerSet = getControllers();
  1005. // Do we have any scene controllers?
  1006. if ( pControllerSet != NULL )
  1007. {
  1008. // Debug Profiling.
  1009. PROFILE_SCOPE(Scene_RenderControllers);
  1010. // Yes, so fetch scene controller count.
  1011. const S32 sceneControllerCount = (S32)pControllerSet->size();
  1012. // Iterate scene controllers.
  1013. for( S32 i = 0; i < sceneControllerCount; i++ )
  1014. {
  1015. // Fetch the scene controller.
  1016. SceneController* pController = dynamic_cast<SceneController*>((*pControllerSet)[i]);
  1017. // Skip if not a controller.
  1018. if ( pController == NULL )
  1019. continue;
  1020. // Render the overlay.
  1021. pController->renderOverlay( this, pSceneRenderState, &mBatchRenderer );
  1022. }
  1023. // Flush isolated batch.
  1024. mBatchRenderer.flush( pDebugStats->batchIsolatedFlush );
  1025. }
  1026. }
  1027. // Draw Joints.
  1028. if ( getDebugMask() & Scene::SCENE_DEBUG_JOINTS )
  1029. {
  1030. // Debug Profiling.
  1031. PROFILE_SCOPE(Scene_RenderSceneJointOverlays);
  1032. mDebugDraw.DrawJoints( mpWorld );
  1033. }
  1034. // Update debug stat ranges.
  1035. mDebugStats.updateRanges();
  1036. // Are we using the render callback?
  1037. if( mRenderCallback )
  1038. {
  1039. // Debug Profiling.
  1040. PROFILE_SCOPE(Scene_OnSceneRendertCallback);
  1041. // Yes, so perform callback.
  1042. Con::executef( this, 1, "onSceneRender" );
  1043. }
  1044. }
  1045. //-----------------------------------------------------------------------------
  1046. void Scene::clearScene( bool deleteObjects )
  1047. {
  1048. while( mSceneObjects.size() > 0 )
  1049. {
  1050. // Fetch first scene object.
  1051. SceneObject* pSceneObject = mSceneObjects[0];
  1052. // Remove Object from Scene.
  1053. removeFromScene( pSceneObject );
  1054. // Queue Object for deletion.
  1055. if ( deleteObjects )
  1056. pSceneObject->safeDelete();
  1057. }
  1058. // Fetch the controller set.
  1059. SimSet* pControllerSet = getControllers();
  1060. // Do we have any scene controllers?
  1061. if ( pControllerSet != NULL )
  1062. {
  1063. // Yes, so delete them all.
  1064. while( pControllerSet->size() > 0 )
  1065. pControllerSet->at(0)->deleteObject();
  1066. }
  1067. // Clear asset preloads.
  1068. clearAssetPreloads();
  1069. }
  1070. //-----------------------------------------------------------------------------
  1071. void Scene::addToScene( SceneObject* pSceneObject )
  1072. {
  1073. if ( pSceneObject == NULL )
  1074. return;
  1075. // Fetch current scene.
  1076. Scene* pCurrentScene = pSceneObject->getScene();
  1077. // Ignore if already in the scene.
  1078. if ( pCurrentScene == this )
  1079. return;
  1080. #if defined(TORQUE_DEBUG)
  1081. // Sanity!
  1082. for ( S32 n = 0; n < mSceneObjects.size(); ++n )
  1083. {
  1084. AssertFatal( mSceneObjects[n] != pSceneObject, "A scene object has become corrupt." );
  1085. }
  1086. #endif
  1087. // Check that object is not already in a scene.
  1088. if ( pCurrentScene )
  1089. {
  1090. // Remove from scene.
  1091. pCurrentScene->removeFromScene( pSceneObject );
  1092. }
  1093. // Add scene object.
  1094. mSceneObjects.push_back( pSceneObject );
  1095. // Register with the scene.
  1096. pSceneObject->OnRegisterScene( this );
  1097. // Perform callback only if properly added to the simulation.
  1098. if ( pSceneObject->isProperlyAdded() )
  1099. {
  1100. Con::executef(pSceneObject, 2, "onAddToScene", getIdString());
  1101. }
  1102. else
  1103. {
  1104. // Warning.
  1105. Con::warnf("Scene object added to scene but the object is not registered with the simulation. No 'onAddToScene' can be performed! Use Target scene.");
  1106. }
  1107. }
  1108. //-----------------------------------------------------------------------------
  1109. void Scene::removeFromScene( SceneObject* pSceneObject )
  1110. {
  1111. if ( pSceneObject == NULL )
  1112. return;
  1113. // Check if object is actually in a scene.
  1114. if ( !pSceneObject->getScene() )
  1115. {
  1116. Con::warnf("Scene::removeFromScene() - Object '%s' is not in a scene!.", pSceneObject->getIdString());
  1117. return;
  1118. }
  1119. // Remove as debug-object if set.
  1120. if ( pSceneObject == getDebugSceneObject() )
  1121. setDebugSceneObject( NULL );
  1122. // Process Destroy Notifications.
  1123. pSceneObject->processDestroyNotifications();
  1124. // Dismount Any Camera.
  1125. pSceneObject->dismountCamera();
  1126. // Remove from the SceneWindow last pickers
  1127. for( U32 i = 0; i < (U32)mAttachedSceneWindows.size(); ++i )
  1128. {
  1129. (dynamic_cast<SceneWindow*>(mAttachedSceneWindows[i]))->removeFromInputEventPick(pSceneObject);
  1130. }
  1131. // Unregister from scene.
  1132. pSceneObject->OnUnregisterScene( this );
  1133. // Find scene object and remove it quickly.
  1134. for ( S32 n = 0; n < mSceneObjects.size(); ++n )
  1135. {
  1136. if ( mSceneObjects[n] == pSceneObject )
  1137. {
  1138. mSceneObjects.erase_fast( n );
  1139. break;
  1140. }
  1141. }
  1142. // Perform callback.
  1143. Con::executef( pSceneObject, 2, "onRemoveFromScene", getIdString() );
  1144. }
  1145. //-----------------------------------------------------------------------------
  1146. SceneObject* Scene::getSceneObject( const U32 objectIndex ) const
  1147. {
  1148. // Sanity!
  1149. AssertFatal( objectIndex < getSceneObjectCount(), "Scene::getSceneObject() - Invalid object index." );
  1150. return mSceneObjects[objectIndex];
  1151. }
  1152. //-----------------------------------------------------------------------------
  1153. U32 Scene::getSceneObjects( typeSceneObjectVector& objects ) const
  1154. {
  1155. // No objects if scene is empty!
  1156. if ( getSceneObjectCount() == 0 )
  1157. return 0;
  1158. // Merge with objects.
  1159. objects.merge( mSceneObjects );
  1160. return getSceneObjectCount();
  1161. }
  1162. //-----------------------------------------------------------------------------
  1163. U32 Scene::getSceneObjects( typeSceneObjectVector& objects, const U32 sceneLayer ) const
  1164. {
  1165. // No objects if scene is empty!
  1166. if ( getSceneObjectCount() == 0 )
  1167. return 0;
  1168. // Reset object count.
  1169. U32 count = 0;
  1170. // Iterate scene objects.
  1171. for( S32 n = 0; n < mSceneObjects.size(); ++n )
  1172. {
  1173. // Fetch scene object.
  1174. SceneObject* pSceneObject = mSceneObjects[n];
  1175. // Skip if not the correct layer.
  1176. if ( pSceneObject->getSceneLayer() != sceneLayer )
  1177. continue;
  1178. // Add to objects.
  1179. objects.push_back( pSceneObject );
  1180. // Increase count.
  1181. count++;
  1182. }
  1183. return count;
  1184. }
  1185. //-----------------------------------------------------------------------------
  1186. const AssetPtr<AssetBase>* Scene::getAssetPreload( const S32 index ) const
  1187. {
  1188. // Is the index valid?
  1189. if ( index < 0 || index >= mAssetPreloads.size() )
  1190. {
  1191. // Yes, so warn.
  1192. Con::warnf( "Scene::getAssetPreload() - Out of range index '%d'. There are only '%d' asset preloads.", index, mAssetPreloads.size() );
  1193. return NULL;
  1194. }
  1195. return mAssetPreloads[index];
  1196. }
  1197. //-----------------------------------------------------------------------------
  1198. void Scene::addAssetPreload( const char* pAssetId )
  1199. {
  1200. // Sanity!
  1201. AssertFatal( pAssetId != NULL, "Scene::addAssetPreload() - Cannot add a NULL asset preload." );
  1202. // Fetch asset Id.
  1203. StringTableEntry assetId = StringTable->insert( pAssetId );
  1204. // Ignore if asset already added.
  1205. const S32 assetPreloadCount = mAssetPreloads.size();
  1206. for( S32 index = 0; index < assetPreloadCount; ++index )
  1207. {
  1208. if ( mAssetPreloads[index]->getAssetId() == assetId )
  1209. return;
  1210. }
  1211. // Create asset pointer.
  1212. AssetPtr<AssetBase>* pAssetPtr = new AssetPtr<AssetBase>( pAssetId );
  1213. // Was the asset acquired?
  1214. if ( pAssetPtr->isNull() )
  1215. {
  1216. // No, so warn.
  1217. Con::warnf( "Scene::addAssetPreload() - Failed to acquire asset '%s' so not added as a preload.", pAssetId );
  1218. // No, so delete the asset pointer.
  1219. delete pAssetPtr;
  1220. return;
  1221. }
  1222. // Add asset.
  1223. mAssetPreloads.push_back( pAssetPtr );
  1224. }
  1225. //-----------------------------------------------------------------------------
  1226. void Scene::removeAssetPreload( const char* pAssetId )
  1227. {
  1228. // Sanity!
  1229. AssertFatal( pAssetId != NULL, "Scene::removeAssetPreload() - Cannot remove a NULL asset preload." );
  1230. // Fetch asset Id.
  1231. StringTableEntry assetId = StringTable->insert( pAssetId );
  1232. // Remove asset Id.
  1233. const S32 assetPreloadCount = mAssetPreloads.size();
  1234. for( S32 index = 0; index < assetPreloadCount; ++index )
  1235. {
  1236. if ( mAssetPreloads[index]->getAssetId() == assetId )
  1237. {
  1238. delete mAssetPreloads[index];
  1239. mAssetPreloads.erase_fast( index );
  1240. return;
  1241. }
  1242. }
  1243. }
  1244. //-----------------------------------------------------------------------------
  1245. void Scene::clearAssetPreloads( void )
  1246. {
  1247. // Delete all the asset preloads.
  1248. while( mAssetPreloads.size() > 0 )
  1249. {
  1250. delete mAssetPreloads.back();
  1251. mAssetPreloads.pop_back();
  1252. }
  1253. }
  1254. //-----------------------------------------------------------------------------
  1255. void Scene::mergeScene( const Scene* pScene )
  1256. {
  1257. // Fetch the scene object count.
  1258. const U32 count = pScene->getSceneObjectCount();
  1259. // Finish if there are not objects to copy.
  1260. if ( count == 0 )
  1261. return;
  1262. for( U32 index = 0; index < count; ++index )
  1263. {
  1264. // Fetch a clone of the scene object.
  1265. SceneObject* pSceneObject = (SceneObject*)pScene->getSceneObject( index )->clone( true );
  1266. // Add the clone.
  1267. addToScene( pSceneObject );
  1268. }
  1269. }
  1270. //-----------------------------------------------------------------------------
  1271. b2Joint* Scene::findJoint( const S32 jointId )
  1272. {
  1273. // Find joint.
  1274. typeJointHash::iterator itr = mJoints.find( jointId );
  1275. return itr == mJoints.end() ? NULL : itr->value;
  1276. }
  1277. //-----------------------------------------------------------------------------
  1278. b2JointType Scene::getJointType( const S32 jointId )
  1279. {
  1280. // Sanity!
  1281. if ( jointId >= mJointMasterId )
  1282. {
  1283. Con::warnf("The joint Id of %d is invalid.", jointId);
  1284. return e_unknownJoint;
  1285. }
  1286. return findJoint( jointId )->GetType();
  1287. }
  1288. //-----------------------------------------------------------------------------
  1289. S32 Scene::findJointId( b2Joint* pJoint )
  1290. {
  1291. // Sanity!
  1292. AssertFatal( pJoint != NULL, "Joint cannot be NULL." );
  1293. // Find joint.
  1294. typeReverseJointHash::iterator itr = mReverseJoints.find( pJoint );
  1295. if ( itr == mReverseJoints.end() )
  1296. {
  1297. Con::warnf("The joint Id could not be found via a joint reference of %x", pJoint);
  1298. return 0;
  1299. }
  1300. return itr->value;
  1301. }
  1302. //-----------------------------------------------------------------------------
  1303. S32 Scene::createJoint( b2JointDef* pJointDef )
  1304. {
  1305. // Sanity!
  1306. AssertFatal( pJointDef != NULL, "Joint definition cannot be NULL." );
  1307. // Create Joint.
  1308. b2Joint* pJoint = mpWorld->CreateJoint( pJointDef );
  1309. // Allocate joint Id.
  1310. const S32 jointId = mJointMasterId++;
  1311. // Insert joint.
  1312. typeJointHash::iterator itr;
  1313. itr = mJoints.insert( jointId, pJoint );
  1314. // Sanity!
  1315. AssertFatal( itr != mJoints.end(), "Joint already in hash table." );
  1316. // Insert reverse joint.
  1317. mReverseJoints.insert( pJoint, jointId );
  1318. return jointId;
  1319. }
  1320. //-----------------------------------------------------------------------------
  1321. bool Scene::deleteJoint( const U32 jointId )
  1322. {
  1323. // Fetch joint.
  1324. b2Joint* pJoint = findJoint( jointId );
  1325. // Finish if no joint.
  1326. if ( pJoint == NULL )
  1327. return false;
  1328. // Destroy joint.
  1329. // This should result in the joint references also being destroyed
  1330. // as the scene is a destruction listener.
  1331. mpWorld->DestroyJoint( pJoint );
  1332. return true;
  1333. }
  1334. //-----------------------------------------------------------------------------
  1335. bool Scene::hasJoints( SceneObject* pSceneObject )
  1336. {
  1337. // Sanity!
  1338. AssertFatal( pSceneObject != NULL, "Scene object cannot be NULL!" );
  1339. AssertFatal( pSceneObject->getScene() != this, "Scene object is not in this scene" );
  1340. // Fetch body.
  1341. b2Body* pBody = pSceneObject->getBody();
  1342. // Fetch joint edge.
  1343. b2JointEdge* pJointEdge = pBody->GetJointList();
  1344. if ( pJointEdge == NULL || pJointEdge->joint == NULL )
  1345. return false;
  1346. // Found at least one joint.
  1347. return true;
  1348. }
  1349. //-----------------------------------------------------------------------------
  1350. S32 Scene::createDistanceJoint(
  1351. const SceneObject* pSceneObjectA, const SceneObject* pSceneObjectB,
  1352. const b2Vec2& localAnchorA, const b2Vec2& localAnchorB,
  1353. const F32 length,
  1354. const F32 frequency,
  1355. const F32 dampingRatio,
  1356. const bool collideConnected )
  1357. {
  1358. // Sanity!
  1359. if ( (pSceneObjectA != NULL && pSceneObjectA->getScene() == NULL) ||
  1360. (pSceneObjectB != NULL && pSceneObjectB->getScene() == NULL) )
  1361. {
  1362. // Warn.
  1363. Con::printf( "Cannot add a joint to a scene object that is not in a scene." );
  1364. return -1;
  1365. }
  1366. // Check for two invalid objects.
  1367. if ( pSceneObjectA == NULL && pSceneObjectB == NULL )
  1368. {
  1369. Con::warnf("Scene::createDistanceJoint() - Cannot create joint without at least a single scene object." );
  1370. return -1;
  1371. }
  1372. // Fetch bodies.
  1373. b2Body* pBodyA = pSceneObjectA != NULL ? pSceneObjectA->getBody() : getGroundBody();
  1374. b2Body* pBodyB = pSceneObjectB != NULL ? pSceneObjectB->getBody() : getGroundBody();
  1375. // Populate definition.
  1376. b2DistanceJointDef jointDef;
  1377. jointDef.userData = static_cast<PhysicsProxy*>(this);
  1378. jointDef.collideConnected = collideConnected;
  1379. jointDef.bodyA = pBodyA;
  1380. jointDef.bodyB = pBodyB;
  1381. jointDef.localAnchorA = localAnchorA;
  1382. jointDef.localAnchorB = localAnchorB;
  1383. jointDef.length = length < 0.0f ? (pBodyB->GetWorldPoint( localAnchorB ) - pBodyA->GetWorldPoint( localAnchorA )).Length() : length;
  1384. jointDef.frequencyHz = frequency;
  1385. jointDef.dampingRatio = dampingRatio;
  1386. // Create joint.
  1387. return createJoint( &jointDef );
  1388. }
  1389. //-----------------------------------------------------------------------------
  1390. void Scene::setDistanceJointLength(
  1391. const U32 jointId,
  1392. const F32 length )
  1393. {
  1394. // Fetch joint.
  1395. b2Joint* pJoint = findJoint( jointId );
  1396. // Ignore invalid joint.
  1397. if ( !pJoint )
  1398. return;
  1399. // Fetch joint type.
  1400. const b2JointType jointType = pJoint->GetType();
  1401. if ( jointType != e_distanceJoint )
  1402. {
  1403. Con::warnf( "Invalid joint type of %s.", getJointTypeDescription(jointType) );
  1404. return;
  1405. }
  1406. // Cast joint.
  1407. b2DistanceJoint* pRealJoint = static_cast<b2DistanceJoint*>( pJoint );
  1408. // Access joint.
  1409. pRealJoint->SetLength( length );
  1410. }
  1411. //-----------------------------------------------------------------------------
  1412. F32 Scene::getDistanceJointLength( const U32 jointId )
  1413. {
  1414. // Fetch joint.
  1415. b2Joint* pJoint = findJoint( jointId );
  1416. // Ignore invalid joint.
  1417. if ( !pJoint )
  1418. return -1.0f;
  1419. // Fetch joint type.
  1420. const b2JointType jointType = pJoint->GetType();
  1421. if ( jointType != e_distanceJoint )
  1422. {
  1423. Con::warnf( "Invalid joint type of %s.", getJointTypeDescription(jointType) );
  1424. return -1.0f;
  1425. }
  1426. // Cast joint.
  1427. b2DistanceJoint* pRealJoint = static_cast<b2DistanceJoint*>( pJoint );
  1428. // Access joint.
  1429. return pRealJoint->GetLength();
  1430. }
  1431. //-----------------------------------------------------------------------------
  1432. void Scene::setDistanceJointFrequency(
  1433. const U32 jointId,
  1434. const F32 frequency )
  1435. {
  1436. // Fetch joint.
  1437. b2Joint* pJoint = findJoint( jointId );
  1438. // Ignore invalid joint.
  1439. if ( !pJoint )
  1440. return;
  1441. // Fetch joint type.
  1442. const b2JointType jointType = pJoint->GetType();
  1443. if ( jointType != e_distanceJoint )
  1444. {
  1445. Con::warnf( "Invalid joint type of %s.", getJointTypeDescription(jointType) );
  1446. return;
  1447. }
  1448. // Cast joint.
  1449. b2DistanceJoint* pRealJoint = static_cast<b2DistanceJoint*>( pJoint );
  1450. // Access joint.
  1451. pRealJoint->SetFrequency( frequency );
  1452. }
  1453. //-----------------------------------------------------------------------------
  1454. F32 Scene::getDistanceJointFrequency( const U32 jointId )
  1455. {
  1456. // Fetch joint.
  1457. b2Joint* pJoint = findJoint( jointId );
  1458. // Ignore invalid joint.
  1459. if ( !pJoint )
  1460. return -1.0f;
  1461. // Fetch joint type.
  1462. const b2JointType jointType = pJoint->GetType();
  1463. if ( jointType != e_distanceJoint )
  1464. {
  1465. Con::warnf( "Invalid joint type of %s.", getJointTypeDescription(jointType) );
  1466. return -1.0f;
  1467. }
  1468. // Cast joint.
  1469. b2DistanceJoint* pRealJoint = static_cast<b2DistanceJoint*>( pJoint );
  1470. // Access joint.
  1471. return pRealJoint->GetFrequency();
  1472. }
  1473. //-----------------------------------------------------------------------------
  1474. void Scene::setDistanceJointDampingRatio(
  1475. const U32 jointId,
  1476. const F32 dampingRatio )
  1477. {
  1478. // Fetch joint.
  1479. b2Joint* pJoint = findJoint( jointId );
  1480. // Ignore invalid joint.
  1481. if ( !pJoint )
  1482. return;
  1483. // Fetch joint type.
  1484. const b2JointType jointType = pJoint->GetType();
  1485. if ( jointType != e_distanceJoint )
  1486. {
  1487. Con::warnf( "Invalid joint type of %s.", getJointTypeDescription(jointType) );
  1488. return;
  1489. }
  1490. // Cast joint.
  1491. b2DistanceJoint* pRealJoint = static_cast<b2DistanceJoint*>( pJoint );
  1492. // Access joint.
  1493. pRealJoint->SetDampingRatio( dampingRatio );
  1494. }
  1495. //-----------------------------------------------------------------------------
  1496. F32 Scene::getDistanceJointDampingRatio( const U32 jointId )
  1497. {
  1498. // Fetch joint.
  1499. b2Joint* pJoint = findJoint( jointId );
  1500. // Ignore invalid joint.
  1501. if ( !pJoint )
  1502. return -1.0f;
  1503. // Fetch joint type.
  1504. const b2JointType jointType = pJoint->GetType();
  1505. if ( jointType != e_distanceJoint )
  1506. {
  1507. Con::warnf( "Invalid joint type of %s.", getJointTypeDescription(jointType) );
  1508. return -1.0f;
  1509. }
  1510. // Cast joint.
  1511. b2DistanceJoint* pRealJoint = static_cast<b2DistanceJoint*>( pJoint );
  1512. // Access joint.
  1513. return pRealJoint->GetDampingRatio();
  1514. }
  1515. //-----------------------------------------------------------------------------
  1516. S32 Scene::createRopeJoint(
  1517. const SceneObject* pSceneObjectA, const SceneObject* pSceneObjectB,
  1518. const b2Vec2& localAnchorA, const b2Vec2& localAnchorB,
  1519. const F32 maxLength,
  1520. const bool collideConnected )
  1521. {
  1522. // Sanity!
  1523. if ( (pSceneObjectA != NULL && pSceneObjectA->getScene() == NULL) ||
  1524. (pSceneObjectB != NULL && pSceneObjectB->getScene() == NULL) )
  1525. {
  1526. // Warn.
  1527. Con::printf( "Cannot add a joint to a scene object that is not in a scene." );
  1528. return -1;
  1529. }
  1530. // Check for two invalid objects.
  1531. if ( pSceneObjectA == NULL && pSceneObjectB == NULL )
  1532. {
  1533. Con::warnf("Scene::createRopeJoint() - Cannot create joint without at least a single scene object." );
  1534. return -1;
  1535. }
  1536. // Fetch bodies.
  1537. b2Body* pBodyA = pSceneObjectA != NULL ? pSceneObjectA->getBody() : getGroundBody();
  1538. b2Body* pBodyB = pSceneObjectB != NULL ? pSceneObjectB->getBody() : getGroundBody();
  1539. // Populate definition.
  1540. b2RopeJointDef jointDef;
  1541. jointDef.userData = static_cast<PhysicsProxy*>(this);
  1542. jointDef.collideConnected = collideConnected;
  1543. jointDef.bodyA = pBodyA;
  1544. jointDef.bodyB = pBodyB;
  1545. jointDef.localAnchorA = localAnchorA;
  1546. jointDef.localAnchorB = localAnchorB;
  1547. jointDef.maxLength = maxLength < 0.0f ? (pBodyB->GetWorldPoint( localAnchorB ) - pBodyA->GetWorldPoint( localAnchorA )).Length() : maxLength;
  1548. // Create joint.
  1549. return createJoint( &jointDef );
  1550. }
  1551. //-----------------------------------------------------------------------------
  1552. void Scene::setRopeJointMaxLength(
  1553. const U32 jointId,
  1554. const F32 maxLength )
  1555. {
  1556. // Fetch joint.
  1557. b2Joint* pJoint = findJoint( jointId );
  1558. // Ignore invalid joint.
  1559. if ( !pJoint )
  1560. return;
  1561. // Fetch joint type.
  1562. const b2JointType jointType = pJoint->GetType();
  1563. if ( jointType != e_ropeJoint )
  1564. {
  1565. Con::warnf( "Invalid joint type of %s.", getJointTypeDescription(jointType) );
  1566. return;
  1567. }
  1568. // Cast joint.
  1569. b2RopeJoint* pRealJoint = static_cast<b2RopeJoint*>( pJoint );
  1570. // Access joint.
  1571. pRealJoint->SetMaxLength( maxLength );
  1572. }
  1573. //-----------------------------------------------------------------------------
  1574. F32 Scene::getRopeJointMaxLength( const U32 jointId )
  1575. {
  1576. // Fetch joint.
  1577. b2Joint* pJoint = findJoint( jointId );
  1578. // Ignore invalid joint.
  1579. if ( !pJoint )
  1580. return -1.0f;
  1581. // Fetch joint type.
  1582. const b2JointType jointType = pJoint->GetType();
  1583. if ( jointType != e_ropeJoint )
  1584. {
  1585. Con::warnf( "Invalid joint type of %s.", getJointTypeDescription(jointType) );
  1586. return -1.0f;
  1587. }
  1588. // Cast joint.
  1589. b2RopeJoint* pRealJoint = static_cast<b2RopeJoint*>( pJoint );
  1590. // Access joint.
  1591. return pRealJoint->GetMaxLength();
  1592. }
  1593. //-----------------------------------------------------------------------------
  1594. S32 Scene::createRevoluteJoint(
  1595. const SceneObject* pSceneObjectA, const SceneObject* pSceneObjectB,
  1596. const b2Vec2& localAnchorA, const b2Vec2& localAnchorB,
  1597. const bool collideConnected )
  1598. {
  1599. // Sanity!
  1600. if ( (pSceneObjectA != NULL && pSceneObjectA->getScene() == NULL) ||
  1601. (pSceneObjectB != NULL && pSceneObjectB->getScene() == NULL) )
  1602. {
  1603. // Warn.
  1604. Con::printf( "Cannot add a joint to a scene object that is not in a scene." );
  1605. return -1;
  1606. }
  1607. // Check for two invalid objects.
  1608. if ( pSceneObjectA == NULL && pSceneObjectB == NULL )
  1609. {
  1610. Con::warnf("Scene::createRevoluteJoint() - Cannot create joint without at least a single scene object." );
  1611. return -1;
  1612. }
  1613. // Fetch bodies.
  1614. b2Body* pBodyA = pSceneObjectA != NULL ? pSceneObjectA->getBody() : getGroundBody();
  1615. b2Body* pBodyB = pSceneObjectB != NULL ? pSceneObjectB->getBody() : getGroundBody();
  1616. // Populate definition.
  1617. b2RevoluteJointDef jointDef;
  1618. jointDef.userData = static_cast<PhysicsProxy*>(this);
  1619. jointDef.collideConnected = collideConnected;
  1620. jointDef.bodyA = pBodyA;
  1621. jointDef.bodyB = pBodyB;
  1622. jointDef.referenceAngle = pBodyB->GetAngle() - pBodyA->GetAngle();
  1623. jointDef.localAnchorA = localAnchorA;
  1624. jointDef.localAnchorB = localAnchorB;
  1625. // Create joint.
  1626. return createJoint( &jointDef );
  1627. }
  1628. //-----------------------------------------------------------------------------
  1629. void Scene::setRevoluteJointLimit(
  1630. const U32 jointId,
  1631. const bool enableLimit,
  1632. const F32 lowerAngle, const F32 upperAngle )
  1633. {
  1634. // Fetch joint.
  1635. b2Joint* pJoint = findJoint( jointId );
  1636. // Ignore invalid joint.
  1637. if ( !pJoint )
  1638. return;
  1639. // Fetch joint type.
  1640. const b2JointType jointType = pJoint->GetType();
  1641. if ( jointType != e_revoluteJoint )
  1642. {
  1643. Con::warnf( "Invalid joint type of %s.", getJointTypeDescription(jointType) );
  1644. return;
  1645. }
  1646. // Cast joint.
  1647. b2RevoluteJoint* pRealJoint = static_cast<b2RevoluteJoint*>( pJoint );
  1648. // Access joint.
  1649. pRealJoint->SetLimits( lowerAngle, upperAngle );
  1650. pRealJoint->EnableLimit( enableLimit );
  1651. }
  1652. //-----------------------------------------------------------------------------
  1653. bool Scene::getRevoluteJointLimit(
  1654. const U32 jointId,
  1655. bool& enableLimit,
  1656. F32& lowerAngle, F32& upperAngle )
  1657. {
  1658. // Fetch joint.
  1659. b2Joint* pJoint = findJoint( jointId );
  1660. // Ignore invalid joint.
  1661. if ( !pJoint )
  1662. return false;
  1663. // Fetch joint type.
  1664. const b2JointType jointType = pJoint->GetType();
  1665. if ( jointType != e_revoluteJoint )
  1666. {
  1667. Con::warnf( "Invalid joint type of %s.", getJointTypeDescription(jointType) );
  1668. return false;
  1669. }
  1670. // Cast joint.
  1671. b2RevoluteJoint* pRealJoint = static_cast<b2RevoluteJoint*>( pJoint );
  1672. // Access joint.
  1673. enableLimit = pRealJoint->IsLimitEnabled();
  1674. lowerAngle = pRealJoint->GetLowerLimit();
  1675. upperAngle = pRealJoint->GetUpperLimit();
  1676. return true;
  1677. }
  1678. //-----------------------------------------------------------------------------
  1679. void Scene::setRevoluteJointMotor(
  1680. const U32 jointId,
  1681. const bool enableMotor,
  1682. const F32 motorSpeed,
  1683. const F32 maxMotorTorque )
  1684. {
  1685. // Fetch joint.
  1686. b2Joint* pJoint = findJoint( jointId );
  1687. // Ignore invalid joint.
  1688. if ( !pJoint )
  1689. return;
  1690. // Fetch joint type.
  1691. const b2JointType jointType = pJoint->GetType();
  1692. if ( jointType != e_revoluteJoint )
  1693. {
  1694. Con::warnf( "Invalid joint type of %s.", getJointTypeDescription(jointType) );
  1695. return;
  1696. }
  1697. // Cast joint.
  1698. b2RevoluteJoint* pRealJoint = static_cast<b2RevoluteJoint*>( pJoint );
  1699. // Access joint.
  1700. pRealJoint->SetMotorSpeed( motorSpeed );
  1701. pRealJoint->SetMaxMotorTorque( maxMotorTorque );
  1702. pRealJoint->EnableMotor( enableMotor );
  1703. }
  1704. //-----------------------------------------------------------------------------
  1705. bool Scene::getRevoluteJointMotor(
  1706. const U32 jointId,
  1707. bool& enableMotor,
  1708. F32& motorSpeed,
  1709. F32& maxMotorTorque )
  1710. {
  1711. // Fetch joint.
  1712. b2Joint* pJoint = findJoint( jointId );
  1713. // Ignore invalid joint.
  1714. if ( !pJoint )
  1715. return false;
  1716. // Fetch joint type.
  1717. const b2JointType jointType = pJoint->GetType();
  1718. if ( jointType != e_revoluteJoint )
  1719. {
  1720. Con::warnf( "Invalid joint type of %s.", getJointTypeDescription(jointType) );
  1721. return false;
  1722. }
  1723. // Cast joint.
  1724. b2RevoluteJoint* pRealJoint = static_cast<b2RevoluteJoint*>( pJoint );
  1725. // Access joint.
  1726. enableMotor = pRealJoint->IsMotorEnabled();
  1727. motorSpeed = pRealJoint->GetMotorSpeed();
  1728. maxMotorTorque = pRealJoint->GetMaxMotorTorque();
  1729. return true;
  1730. }
  1731. //-----------------------------------------------------------------------------
  1732. F32 Scene::getRevoluteJointAngle( const U32 jointId )
  1733. {
  1734. // Fetch joint.
  1735. b2Joint* pJoint = findJoint( jointId );
  1736. // Ignore invalid joint.
  1737. if ( !pJoint )
  1738. return 0.0f;
  1739. // Fetch joint type.
  1740. const b2JointType jointType = pJoint->GetType();
  1741. if ( jointType != e_revoluteJoint )
  1742. {
  1743. Con::warnf( "Invalid joint type of %s.", getJointTypeDescription(jointType) );
  1744. return 0.0f;
  1745. }
  1746. // Cast joint.
  1747. b2RevoluteJoint* pRealJoint = static_cast<b2RevoluteJoint*>( pJoint );
  1748. // Access joint.
  1749. return pRealJoint->GetJointAngle();
  1750. }
  1751. //-----------------------------------------------------------------------------
  1752. F32 Scene::getRevoluteJointSpeed( const U32 jointId )
  1753. {
  1754. // Fetch joint.
  1755. b2Joint* pJoint = findJoint( jointId );
  1756. // Ignore invalid joint.
  1757. if ( !pJoint )
  1758. return 0.0f;
  1759. // Fetch joint type.
  1760. const b2JointType jointType = pJoint->GetType();
  1761. if ( jointType != e_revoluteJoint )
  1762. {
  1763. Con::warnf( "Invalid joint type of %s.", getJointTypeDescription(jointType) );
  1764. return 0.0f;
  1765. }
  1766. // Cast joint.
  1767. b2RevoluteJoint* pRealJoint = static_cast<b2RevoluteJoint*>( pJoint );
  1768. // Access joint.
  1769. return pRealJoint->GetJointSpeed();
  1770. }
  1771. //-----------------------------------------------------------------------------
  1772. S32 Scene::createWeldJoint(
  1773. const SceneObject* pSceneObjectA, const SceneObject* pSceneObjectB,
  1774. const b2Vec2& localAnchorA, const b2Vec2& localAnchorB,
  1775. const F32 frequency,
  1776. const F32 dampingRatio,
  1777. const bool collideConnected )
  1778. {
  1779. // Sanity!
  1780. if ( (pSceneObjectA != NULL && pSceneObjectA->getScene() == NULL) ||
  1781. (pSceneObjectB != NULL && pSceneObjectB->getScene() == NULL) )
  1782. {
  1783. // Warn.
  1784. Con::printf( "Cannot add a joint to a scene object that is not in a scene." );
  1785. return -1;
  1786. }
  1787. // Check for two invalid objects.
  1788. if ( pSceneObjectA == NULL && pSceneObjectB == NULL )
  1789. {
  1790. Con::warnf("Scene::createWeldJoint() - Cannot create joint without at least a single scene object." );
  1791. return -1;
  1792. }
  1793. // Fetch bodies.
  1794. b2Body* pBodyA = pSceneObjectA != NULL ? pSceneObjectA->getBody() : getGroundBody();
  1795. b2Body* pBodyB = pSceneObjectB != NULL ? pSceneObjectB->getBody() : getGroundBody();
  1796. // Populate definition.
  1797. b2WeldJointDef jointDef;
  1798. jointDef.userData = static_cast<PhysicsProxy*>(this);
  1799. jointDef.collideConnected = collideConnected;
  1800. jointDef.bodyA = pBodyA;
  1801. jointDef.bodyB = pBodyB;
  1802. jointDef.referenceAngle = pBodyB->GetAngle() - pBodyA->GetAngle();
  1803. jointDef.localAnchorA = localAnchorA;
  1804. jointDef.localAnchorB = localAnchorB;
  1805. jointDef.frequencyHz = frequency;
  1806. jointDef.dampingRatio = dampingRatio;
  1807. // Create joint.
  1808. return createJoint( &jointDef );
  1809. }
  1810. //-----------------------------------------------------------------------------
  1811. void Scene::setWeldJointFrequency(
  1812. const U32 jointId,
  1813. const F32 frequency )
  1814. {
  1815. // Fetch joint.
  1816. b2Joint* pJoint = findJoint( jointId );
  1817. // Ignore invalid joint.
  1818. if ( !pJoint )
  1819. return;
  1820. // Fetch joint type.
  1821. const b2JointType jointType = pJoint->GetType();
  1822. if ( jointType != e_weldJoint )
  1823. {
  1824. Con::warnf( "Invalid joint type of %s.", getJointTypeDescription(jointType) );
  1825. return;
  1826. }
  1827. // Cast joint.
  1828. b2WeldJoint* pRealJoint = static_cast<b2WeldJoint*>( pJoint );
  1829. // Access joint.
  1830. pRealJoint->SetFrequency( frequency );
  1831. }
  1832. //-----------------------------------------------------------------------------
  1833. F32 Scene::getWeldJointFrequency( const U32 jointId )
  1834. {
  1835. // Fetch joint.
  1836. b2Joint* pJoint = findJoint( jointId );
  1837. // Ignore invalid joint.
  1838. if ( !pJoint )
  1839. return -1.0f;
  1840. // Fetch joint type.
  1841. const b2JointType jointType = pJoint->GetType();
  1842. if ( jointType != e_weldJoint )
  1843. {
  1844. Con::warnf( "Invalid joint type of %s.", getJointTypeDescription(jointType) );
  1845. return -1.0f;
  1846. }
  1847. // Cast joint.
  1848. b2WeldJoint* pRealJoint = static_cast<b2WeldJoint*>( pJoint );
  1849. // Access joint.
  1850. return pRealJoint->GetFrequency();
  1851. }
  1852. //-----------------------------------------------------------------------------
  1853. void Scene::setWeldJointDampingRatio(
  1854. const U32 jointId,
  1855. const F32 dampingRatio )
  1856. {
  1857. // Fetch joint.
  1858. b2Joint* pJoint = findJoint( jointId );
  1859. // Ignore invalid joint.
  1860. if ( !pJoint )
  1861. return;
  1862. // Fetch joint type.
  1863. const b2JointType jointType = pJoint->GetType();
  1864. if ( jointType != e_weldJoint )
  1865. {
  1866. Con::warnf( "Invalid joint type of %s.", getJointTypeDescription(jointType) );
  1867. return;
  1868. }
  1869. // Cast joint.
  1870. b2WeldJoint* pRealJoint = static_cast<b2WeldJoint*>( pJoint );
  1871. // Access joint.
  1872. pRealJoint->SetDampingRatio( dampingRatio );
  1873. }
  1874. //-----------------------------------------------------------------------------
  1875. F32 Scene::getWeldJointDampingRatio( const U32 jointId )
  1876. {
  1877. // Fetch joint.
  1878. b2Joint* pJoint = findJoint( jointId );
  1879. // Ignore invalid joint.
  1880. if ( !pJoint )
  1881. return -1.0f;
  1882. // Fetch joint type.
  1883. const b2JointType jointType = pJoint->GetType();
  1884. if ( jointType != e_weldJoint )
  1885. {
  1886. Con::warnf( "Invalid joint type of %s.", getJointTypeDescription(jointType) );
  1887. return -1.0f;
  1888. }
  1889. // Cast joint.
  1890. b2WeldJoint* pRealJoint = static_cast<b2WeldJoint*>( pJoint );
  1891. // Access joint.
  1892. return pRealJoint->GetDampingRatio();
  1893. }
  1894. //-----------------------------------------------------------------------------
  1895. S32 Scene::createWheelJoint(
  1896. const SceneObject* pSceneObjectA, const SceneObject* pSceneObjectB,
  1897. const b2Vec2& localAnchorA, const b2Vec2& localAnchorB,
  1898. const b2Vec2& worldAxis,
  1899. const bool collideConnected )
  1900. {
  1901. // Sanity!
  1902. if ( (pSceneObjectA != NULL && pSceneObjectA->getScene() == NULL) ||
  1903. (pSceneObjectB != NULL && pSceneObjectB->getScene() == NULL) )
  1904. {
  1905. // Warn.
  1906. Con::printf( "Cannot add a joint to a scene object that is not in a scene." );
  1907. return -1;
  1908. }
  1909. // Check for two invalid objects.
  1910. if ( pSceneObjectA == NULL && pSceneObjectB == NULL )
  1911. {
  1912. Con::warnf("Scene::createWheelJoint() - Cannot create joint without at least a single scene object." );
  1913. return -1;
  1914. }
  1915. // Fetch bodies.
  1916. b2Body* pBodyA = pSceneObjectA != NULL ? pSceneObjectA->getBody() : getGroundBody();
  1917. b2Body* pBodyB = pSceneObjectB != NULL ? pSceneObjectB->getBody() : getGroundBody();
  1918. // Populate definition.
  1919. b2WheelJointDef jointDef;
  1920. jointDef.userData = static_cast<PhysicsProxy*>(this);
  1921. jointDef.collideConnected = collideConnected;
  1922. jointDef.bodyA = pBodyA;
  1923. jointDef.bodyB = pBodyB;
  1924. jointDef.localAnchorA = localAnchorA;
  1925. jointDef.localAnchorB = localAnchorB;
  1926. jointDef.localAxisA = pBodyA->GetLocalVector( worldAxis );
  1927. // Create joint.
  1928. return createJoint( &jointDef );
  1929. }
  1930. //-----------------------------------------------------------------------------
  1931. void Scene::setWheelJointMotor(
  1932. const U32 jointId,
  1933. const bool enableMotor,
  1934. const F32 motorSpeed,
  1935. const F32 maxMotorTorque )
  1936. {
  1937. // Fetch joint.
  1938. b2Joint* pJoint = findJoint( jointId );
  1939. // Ignore invalid joint.
  1940. if ( !pJoint )
  1941. return;
  1942. // Fetch joint type.
  1943. const b2JointType jointType = pJoint->GetType();
  1944. if ( jointType != e_wheelJoint )
  1945. {
  1946. Con::warnf( "Invalid joint type of %s.", getJointTypeDescription(jointType) );
  1947. return;
  1948. }
  1949. // Cast joint.
  1950. b2WheelJoint* pRealJoint = static_cast<b2WheelJoint*>( pJoint );
  1951. // Access joint.
  1952. pRealJoint->SetMotorSpeed( motorSpeed );
  1953. pRealJoint->SetMaxMotorTorque( maxMotorTorque );
  1954. pRealJoint->EnableMotor( enableMotor );
  1955. }
  1956. //-----------------------------------------------------------------------------
  1957. bool Scene::getWheelJointMotor(
  1958. const U32 jointId,
  1959. bool& enableMotor,
  1960. F32& motorSpeed,
  1961. F32& maxMotorTorque )
  1962. {
  1963. // Fetch joint.
  1964. b2Joint* pJoint = findJoint( jointId );
  1965. // Ignore invalid joint.
  1966. if ( !pJoint )
  1967. return false;
  1968. // Fetch joint type.
  1969. const b2JointType jointType = pJoint->GetType();
  1970. if ( jointType != e_wheelJoint )
  1971. {
  1972. Con::warnf( "Invalid joint type of %s.", getJointTypeDescription(jointType) );
  1973. return false;
  1974. }
  1975. // Cast joint.
  1976. b2WheelJoint* pRealJoint = static_cast<b2WheelJoint*>( pJoint );
  1977. // Access joint.
  1978. enableMotor = pRealJoint->IsMotorEnabled();
  1979. motorSpeed = pRealJoint->GetMotorSpeed();
  1980. maxMotorTorque = pRealJoint->GetMaxMotorTorque();
  1981. return true;
  1982. }
  1983. //-----------------------------------------------------------------------------
  1984. void Scene::setWheelJointFrequency(
  1985. const U32 jointId,
  1986. const F32 frequency )
  1987. {
  1988. // Fetch joint.
  1989. b2Joint* pJoint = findJoint( jointId );
  1990. // Ignore invalid joint.
  1991. if ( !pJoint )
  1992. return;
  1993. // Fetch joint type.
  1994. const b2JointType jointType = pJoint->GetType();
  1995. if ( jointType != e_wheelJoint )
  1996. {
  1997. Con::warnf( "Invalid joint type of %s.", getJointTypeDescription(jointType) );
  1998. return;
  1999. }
  2000. // Cast joint.
  2001. b2WheelJoint* pRealJoint = static_cast<b2WheelJoint*>( pJoint );
  2002. // Access joint.
  2003. pRealJoint->SetSpringFrequencyHz( frequency );
  2004. }
  2005. //-----------------------------------------------------------------------------
  2006. F32 Scene::getWheelJointFrequency( const U32 jointId )
  2007. {
  2008. // Fetch joint.
  2009. b2Joint* pJoint = findJoint( jointId );
  2010. // Ignore invalid joint.
  2011. if ( !pJoint )
  2012. return -1.0f;
  2013. // Fetch joint type.
  2014. const b2JointType jointType = pJoint->GetType();
  2015. if ( jointType != e_wheelJoint )
  2016. {
  2017. Con::warnf( "Invalid joint type of %s.", getJointTypeDescription(jointType) );
  2018. return -1.0f;
  2019. }
  2020. // Cast joint.
  2021. b2WheelJoint* pRealJoint = static_cast<b2WheelJoint*>( pJoint );
  2022. // Access joint.
  2023. return pRealJoint->GetSpringFrequencyHz();
  2024. }
  2025. //-----------------------------------------------------------------------------
  2026. void Scene::setWheelJointDampingRatio(
  2027. const U32 jointId,
  2028. const F32 dampingRatio )
  2029. {
  2030. // Fetch joint.
  2031. b2Joint* pJoint = findJoint( jointId );
  2032. // Ignore invalid joint.
  2033. if ( !pJoint )
  2034. return;
  2035. // Fetch joint type.
  2036. const b2JointType jointType = pJoint->GetType();
  2037. if ( jointType != e_wheelJoint )
  2038. {
  2039. Con::warnf( "Invalid joint type of %s.", getJointTypeDescription(jointType) );
  2040. return;
  2041. }
  2042. // Cast joint.
  2043. b2WheelJoint* pRealJoint = static_cast<b2WheelJoint*>( pJoint );
  2044. // Access joint.
  2045. pRealJoint->SetSpringDampingRatio( dampingRatio );
  2046. }
  2047. //-----------------------------------------------------------------------------
  2048. F32 Scene::getWheelJointDampingRatio( const U32 jointId )
  2049. {
  2050. // Fetch joint.
  2051. b2Joint* pJoint = findJoint( jointId );
  2052. // Ignore invalid joint.
  2053. if ( !pJoint )
  2054. return -1.0f;
  2055. // Fetch joint type.
  2056. const b2JointType jointType = pJoint->GetType();
  2057. if ( jointType != e_wheelJoint )
  2058. {
  2059. Con::warnf( "Invalid joint type of %s.", getJointTypeDescription(jointType) );
  2060. return -1.0f;
  2061. }
  2062. // Cast joint.
  2063. b2WheelJoint* pRealJoint = static_cast<b2WheelJoint*>( pJoint );
  2064. // Access joint.
  2065. return pRealJoint->GetSpringDampingRatio();
  2066. }
  2067. //-----------------------------------------------------------------------------
  2068. S32 Scene::createFrictionJoint(
  2069. const SceneObject* pSceneObjectA, const SceneObject* pSceneObjectB,
  2070. const b2Vec2& localAnchorA, const b2Vec2& localAnchorB,
  2071. const F32 maxForce,
  2072. const F32 maxTorque,
  2073. const bool collideConnected )
  2074. {
  2075. // Sanity!
  2076. if ( (pSceneObjectA != NULL && pSceneObjectA->getScene() == NULL) ||
  2077. (pSceneObjectB != NULL && pSceneObjectB->getScene() == NULL) )
  2078. {
  2079. // Warn.
  2080. Con::printf( "Cannot add a joint to a scene object that is not in a scene." );
  2081. return -1;
  2082. }
  2083. // Check for two invalid objects.
  2084. if ( pSceneObjectA == NULL && pSceneObjectB == NULL )
  2085. {
  2086. Con::warnf("Scene::createFrictionJoint() - Cannot create joint without at least a single scene object." );
  2087. return -1;
  2088. }
  2089. // Fetch bodies.
  2090. b2Body* pBodyA = pSceneObjectA != NULL ? pSceneObjectA->getBody() : getGroundBody();
  2091. b2Body* pBodyB = pSceneObjectB != NULL ? pSceneObjectB->getBody() : getGroundBody();
  2092. // Populate definition.
  2093. b2FrictionJointDef jointDef;
  2094. jointDef.userData = static_cast<PhysicsProxy*>(this);
  2095. jointDef.collideConnected = collideConnected;
  2096. jointDef.bodyA = pBodyA;
  2097. jointDef.bodyB = pBodyB;
  2098. jointDef.localAnchorA = localAnchorA;
  2099. jointDef.localAnchorB = localAnchorB;
  2100. jointDef.maxForce = maxForce;
  2101. jointDef.maxTorque = maxTorque;
  2102. // Create joint.
  2103. return createJoint( &jointDef );
  2104. }
  2105. //-----------------------------------------------------------------------------
  2106. void Scene::setFrictionJointMaxForce(
  2107. const U32 jointId,
  2108. const F32 maxForce )
  2109. {
  2110. // Fetch joint.
  2111. b2Joint* pJoint = findJoint( jointId );
  2112. // Ignore invalid joint.
  2113. if ( !pJoint )
  2114. return;
  2115. // Fetch joint type.
  2116. const b2JointType jointType = pJoint->GetType();
  2117. if ( jointType != e_frictionJoint )
  2118. {
  2119. Con::warnf( "Invalid joint type of %s.", getJointTypeDescription(jointType) );
  2120. return;
  2121. }
  2122. // Cast joint.
  2123. b2FrictionJoint* pRealJoint = static_cast<b2FrictionJoint*>( pJoint );
  2124. // Access joint.
  2125. pRealJoint->SetMaxForce( maxForce );
  2126. }
  2127. //-----------------------------------------------------------------------------
  2128. F32 Scene::getFrictionJointMaxForce( const U32 jointId )
  2129. {
  2130. // Fetch joint.
  2131. b2Joint* pJoint = findJoint( jointId );
  2132. // Ignore invalid joint.
  2133. if ( !pJoint )
  2134. return -1.0f;
  2135. // Fetch joint type.
  2136. const b2JointType jointType = pJoint->GetType();
  2137. if ( jointType != e_frictionJoint )
  2138. {
  2139. Con::warnf( "Invalid joint type of %s.", getJointTypeDescription(jointType) );
  2140. return -1.0f;
  2141. }
  2142. // Cast joint.
  2143. b2FrictionJoint* pRealJoint = static_cast<b2FrictionJoint*>( pJoint );
  2144. // Access joint.
  2145. return pRealJoint->GetMaxForce();
  2146. }
  2147. //-----------------------------------------------------------------------------
  2148. void Scene::setFrictionJointMaxTorque(
  2149. const U32 jointId,
  2150. const F32 maxTorque )
  2151. {
  2152. // Fetch joint.
  2153. b2Joint* pJoint = findJoint( jointId );
  2154. // Ignore invalid joint.
  2155. if ( !pJoint )
  2156. return;
  2157. // Fetch joint type.
  2158. const b2JointType jointType = pJoint->GetType();
  2159. if ( jointType != e_frictionJoint )
  2160. {
  2161. Con::warnf( "Invalid joint type of %s.", getJointTypeDescription(jointType) );
  2162. return;
  2163. }
  2164. // Cast joint.
  2165. b2FrictionJoint* pRealJoint = static_cast<b2FrictionJoint*>( pJoint );
  2166. // Access joint.
  2167. pRealJoint->SetMaxTorque( maxTorque );
  2168. }
  2169. //-----------------------------------------------------------------------------
  2170. F32 Scene::getFrictionJointMaxTorque( const U32 jointId )
  2171. {
  2172. // Fetch joint.
  2173. b2Joint* pJoint = findJoint( jointId );
  2174. // Ignore invalid joint.
  2175. if ( !pJoint )
  2176. return -1.0f;
  2177. // Fetch joint type.
  2178. const b2JointType jointType = pJoint->GetType();
  2179. if ( jointType != e_frictionJoint )
  2180. {
  2181. Con::warnf( "Invalid joint type of %s.", getJointTypeDescription(jointType) );
  2182. return -1.0f;
  2183. }
  2184. // Cast joint.
  2185. b2FrictionJoint* pRealJoint = static_cast<b2FrictionJoint*>( pJoint );
  2186. // Access joint.
  2187. return pRealJoint->GetMaxTorque();
  2188. }
  2189. //-----------------------------------------------------------------------------
  2190. S32 Scene::createPrismaticJoint(
  2191. const SceneObject* pSceneObjectA, const SceneObject* pSceneObjectB,
  2192. const b2Vec2& localAnchorA, const b2Vec2& localAnchorB,
  2193. const b2Vec2& worldAxis,
  2194. const bool collideConnected )
  2195. {
  2196. // Sanity!
  2197. if ( (pSceneObjectA != NULL && pSceneObjectA->getScene() == NULL) ||
  2198. (pSceneObjectB != NULL && pSceneObjectB->getScene() == NULL) )
  2199. {
  2200. // Warn.
  2201. Con::printf( "Cannot add a joint to a scene object that is not in a scene." );
  2202. return -1;
  2203. }
  2204. // Check for two invalid objects.
  2205. if ( pSceneObjectA == NULL && pSceneObjectB == NULL )
  2206. {
  2207. Con::warnf("Scene::createPrismaticJoint() - Cannot create joint without at least a single scene object." );
  2208. return -1;
  2209. }
  2210. // Fetch bodies.
  2211. b2Body* pBodyA = pSceneObjectA != NULL ? pSceneObjectA->getBody() : getGroundBody();
  2212. b2Body* pBodyB = pSceneObjectB != NULL ? pSceneObjectB->getBody() : getGroundBody();
  2213. // Populate definition.
  2214. b2PrismaticJointDef jointDef;
  2215. jointDef.userData = static_cast<PhysicsProxy*>(this);
  2216. jointDef.collideConnected = collideConnected;
  2217. jointDef.bodyA = pBodyA;
  2218. jointDef.bodyB = pBodyB;
  2219. jointDef.referenceAngle = pBodyB->GetAngle() - pBodyA->GetAngle();
  2220. jointDef.localAnchorA = localAnchorA;
  2221. jointDef.localAnchorB = localAnchorB;
  2222. jointDef.localAxisA = pBodyA->GetLocalVector( worldAxis );
  2223. // Create joint.
  2224. return createJoint( &jointDef );
  2225. }
  2226. //-----------------------------------------------------------------------------
  2227. void Scene::setPrismaticJointLimit(
  2228. const U32 jointId,
  2229. const bool enableLimit,
  2230. const F32 lowerTranslation, const F32 upperTranslation )
  2231. {
  2232. // Fetch joint.
  2233. b2Joint* pJoint = findJoint( jointId );
  2234. // Ignore invalid joint.
  2235. if ( !pJoint )
  2236. return;
  2237. // Fetch joint type.
  2238. const b2JointType jointType = pJoint->GetType();
  2239. if ( jointType != e_prismaticJoint )
  2240. {
  2241. Con::warnf( "Invalid joint type of %s.", getJointTypeDescription(jointType) );
  2242. return;
  2243. }
  2244. // Cast joint.
  2245. b2PrismaticJoint* pRealJoint = static_cast<b2PrismaticJoint*>( pJoint );
  2246. // Access joint.
  2247. pRealJoint->SetLimits( lowerTranslation, upperTranslation );
  2248. pRealJoint->EnableLimit( enableLimit );
  2249. }
  2250. //-----------------------------------------------------------------------------
  2251. bool Scene::getPrismaticJointLimit(
  2252. const U32 jointId,
  2253. bool& enableLimit,
  2254. F32& lowerTranslation, F32& upperTranslation )
  2255. {
  2256. // Fetch joint.
  2257. b2Joint* pJoint = findJoint( jointId );
  2258. // Ignore invalid joint.
  2259. if ( !pJoint )
  2260. return false;
  2261. // Fetch joint type.
  2262. const b2JointType jointType = pJoint->GetType();
  2263. if ( jointType != e_prismaticJoint )
  2264. {
  2265. Con::warnf( "Invalid joint type of %s.", getJointTypeDescription(jointType) );
  2266. return false;
  2267. }
  2268. // Cast joint.
  2269. b2PrismaticJoint* pRealJoint = static_cast<b2PrismaticJoint*>( pJoint );
  2270. // Access joint.
  2271. enableLimit = pRealJoint->IsLimitEnabled();
  2272. lowerTranslation = pRealJoint->GetLowerLimit();
  2273. upperTranslation = pRealJoint->GetUpperLimit();
  2274. return true;
  2275. }
  2276. //-----------------------------------------------------------------------------
  2277. void Scene::setPrismaticJointMotor(
  2278. const U32 jointId,
  2279. const bool enableMotor,
  2280. const F32 motorSpeed,
  2281. const F32 maxMotorForce )
  2282. {
  2283. // Fetch joint.
  2284. b2Joint* pJoint = findJoint( jointId );
  2285. // Ignore invalid joint.
  2286. if ( !pJoint )
  2287. return;
  2288. // Fetch joint type.
  2289. const b2JointType jointType = pJoint->GetType();
  2290. if ( jointType != e_prismaticJoint )
  2291. {
  2292. Con::warnf( "Invalid joint type of %s.", getJointTypeDescription(jointType) );
  2293. return;
  2294. }
  2295. // Cast joint.
  2296. b2PrismaticJoint* pRealJoint = static_cast<b2PrismaticJoint*>( pJoint );
  2297. // Access joint.
  2298. pRealJoint->SetMotorSpeed( motorSpeed );
  2299. pRealJoint->SetMaxMotorForce( maxMotorForce );
  2300. pRealJoint->EnableMotor( enableMotor );
  2301. }
  2302. //-----------------------------------------------------------------------------
  2303. bool Scene::getPrismaticJointMotor(
  2304. const U32 jointId,
  2305. bool& enableMotor,
  2306. F32& motorSpeed,
  2307. F32& maxMotorForce )
  2308. {
  2309. // Fetch joint.
  2310. b2Joint* pJoint = findJoint( jointId );
  2311. // Ignore invalid joint.
  2312. if ( !pJoint )
  2313. return false;
  2314. // Fetch joint type.
  2315. const b2JointType jointType = pJoint->GetType();
  2316. if ( jointType != e_prismaticJoint )
  2317. {
  2318. Con::warnf( "Invalid joint type of %s.", getJointTypeDescription(jointType) );
  2319. return false;
  2320. }
  2321. // Cast joint.
  2322. b2PrismaticJoint* pRealJoint = static_cast<b2PrismaticJoint*>( pJoint );
  2323. // Access joint.
  2324. enableMotor = pRealJoint->IsMotorEnabled();
  2325. motorSpeed = pRealJoint->GetMotorSpeed();
  2326. maxMotorForce = pRealJoint->GetMaxMotorForce();
  2327. return true;
  2328. }
  2329. //-----------------------------------------------------------------------------
  2330. S32 Scene::createPulleyJoint(
  2331. const SceneObject* pSceneObjectA, const SceneObject* pSceneObjectB,
  2332. const b2Vec2& localAnchorA, const b2Vec2& localAnchorB,
  2333. const b2Vec2& worldGroundAnchorA, const b2Vec2& worldGroundAnchorB,
  2334. const F32 ratio,
  2335. const F32 lengthA, const F32 lengthB,
  2336. const bool collideConnected )
  2337. {
  2338. // Sanity!
  2339. if ( (pSceneObjectA != NULL && pSceneObjectA->getScene() == NULL) ||
  2340. (pSceneObjectB != NULL && pSceneObjectB->getScene() == NULL) )
  2341. {
  2342. // Warn.
  2343. Con::printf( "Cannot add a joint to a scene object that is not in a scene." );
  2344. return -1;
  2345. }
  2346. // Check for two invalid objects.
  2347. if ( pSceneObjectA == NULL && pSceneObjectB == NULL )
  2348. {
  2349. Con::warnf("Scene::createPulleyJoint() - Cannot create joint without at least a single scene object." );
  2350. return -1;
  2351. }
  2352. // Fetch bodies.
  2353. b2Body* pBodyA = pSceneObjectA != NULL ? pSceneObjectA->getBody() : getGroundBody();
  2354. b2Body* pBodyB = pSceneObjectB != NULL ? pSceneObjectB->getBody() : getGroundBody();
  2355. // Populate definition.
  2356. b2PulleyJointDef jointDef;
  2357. jointDef.userData = static_cast<PhysicsProxy*>(this);
  2358. jointDef.collideConnected = collideConnected;
  2359. jointDef.bodyA = pBodyA;
  2360. jointDef.bodyB = pBodyB;
  2361. jointDef.groundAnchorA = worldGroundAnchorA;
  2362. jointDef.groundAnchorB = worldGroundAnchorB;
  2363. jointDef.localAnchorA = localAnchorA;
  2364. jointDef.localAnchorB = localAnchorB;
  2365. jointDef.lengthA = lengthA < 0.0f ? (pBodyA->GetWorldPoint( localAnchorA ) - worldGroundAnchorA).Length() : lengthA;
  2366. jointDef.lengthB = lengthB < 0.0f ? (pBodyA->GetWorldPoint( localAnchorB ) - worldGroundAnchorB).Length() : lengthB;
  2367. jointDef.ratio = ratio > b2_epsilon ? ratio : b2_epsilon + b2_epsilon;
  2368. // Create joint.
  2369. return createJoint( &jointDef );
  2370. }
  2371. //-----------------------------------------------------------------------------
  2372. S32 Scene::createTargetJoint(
  2373. const SceneObject* pSceneObject,
  2374. const b2Vec2& worldTarget,
  2375. const F32 maxForce,
  2376. const bool useCenterOfMass,
  2377. const F32 frequency,
  2378. const F32 dampingRatio,
  2379. const bool collideConnected )
  2380. {
  2381. // Sanity!
  2382. if ( pSceneObject != NULL && pSceneObject->getScene() == NULL )
  2383. {
  2384. // Warn.
  2385. Con::printf( "Cannot add a joint to a scene object that is not in a scene." );
  2386. return -1;
  2387. }
  2388. // Check for invalid object.
  2389. if ( pSceneObject == NULL )
  2390. {
  2391. Con::warnf("Scene::createPulleyJoint() - Cannot create joint without a scene object." );
  2392. return -1;
  2393. }
  2394. // Fetch bodies.
  2395. b2Body* pBody = pSceneObject->getBody();
  2396. // Populate definition.
  2397. b2MouseJointDef jointDef;
  2398. jointDef.userData = static_cast<PhysicsProxy*>(this);
  2399. jointDef.collideConnected = collideConnected;
  2400. jointDef.bodyA = getGroundBody();
  2401. jointDef.bodyB = pBody;
  2402. jointDef.target = useCenterOfMass ? pBody->GetWorldCenter() : worldTarget;
  2403. jointDef.maxForce = maxForce;
  2404. jointDef.frequencyHz = frequency;
  2405. jointDef.dampingRatio = dampingRatio;
  2406. // Create joint.
  2407. const U32 jointId = createJoint( &jointDef );
  2408. // Cast joint.
  2409. b2MouseJoint* pRealJoint = static_cast<b2MouseJoint*>( findJoint( jointId ) );
  2410. // Are we using the center of mass?
  2411. if ( !useCenterOfMass )
  2412. {
  2413. // No, so set the target as the world target.
  2414. // NOTE:- This is done because initially the target (mouse) joint assumes the target
  2415. // coincides with the body anchor.
  2416. pRealJoint->SetTarget( worldTarget );
  2417. }
  2418. return jointId;
  2419. }
  2420. //-----------------------------------------------------------------------------
  2421. void Scene::setTargetJointTarget(
  2422. const U32 jointId,
  2423. const b2Vec2& worldTarget )
  2424. {
  2425. // Fetch joint.
  2426. b2Joint* pJoint = findJoint( jointId );
  2427. // Ignore invalid joint.
  2428. if ( !pJoint )
  2429. return;
  2430. // Fetch joint type.
  2431. const b2JointType jointType = pJoint->GetType();
  2432. if ( jointType != e_mouseJoint )
  2433. {
  2434. Con::warnf( "Invalid joint type of %s.", getJointTypeDescription(jointType) );
  2435. return;
  2436. }
  2437. // Cast joint.
  2438. b2MouseJoint* pRealJoint = static_cast<b2MouseJoint*>( pJoint );
  2439. // Access joint.
  2440. pRealJoint->SetTarget( worldTarget );
  2441. }
  2442. //-----------------------------------------------------------------------------
  2443. b2Vec2 Scene::getTargetJointTarget( const U32 jointId )
  2444. {
  2445. // Fetch joint.
  2446. b2Joint* pJoint = findJoint( jointId );
  2447. // Ignore invalid joint.
  2448. if ( !pJoint )
  2449. return b2Vec2_zero;
  2450. // Fetch joint type.
  2451. const b2JointType jointType = pJoint->GetType();
  2452. if ( jointType != e_mouseJoint )
  2453. {
  2454. Con::warnf( "Invalid joint type of %s.", getJointTypeDescription(jointType) );
  2455. return b2Vec2_zero;
  2456. }
  2457. // Cast joint.
  2458. b2MouseJoint* pRealJoint = static_cast<b2MouseJoint*>( pJoint );
  2459. // Access joint.
  2460. return pRealJoint->GetTarget();
  2461. }
  2462. //-----------------------------------------------------------------------------
  2463. void Scene::setTargetJointMaxForce(
  2464. const U32 jointId,
  2465. const F32 maxForce )
  2466. {
  2467. // Fetch joint.
  2468. b2Joint* pJoint = findJoint( jointId );
  2469. // Ignore invalid joint.
  2470. if ( !pJoint )
  2471. return;
  2472. // Fetch joint type.
  2473. const b2JointType jointType = pJoint->GetType();
  2474. if ( jointType != e_mouseJoint )
  2475. {
  2476. Con::warnf( "Invalid joint type of %s.", getJointTypeDescription(jointType) );
  2477. return;
  2478. }
  2479. // Cast joint.
  2480. b2MouseJoint* pRealJoint = static_cast<b2MouseJoint*>( pJoint );
  2481. // Access joint.
  2482. pRealJoint->SetMaxForce( maxForce );
  2483. }
  2484. //-----------------------------------------------------------------------------
  2485. F32 Scene::getTargetJointMaxForce( const U32 jointId )
  2486. {
  2487. // Fetch joint.
  2488. b2Joint* pJoint = findJoint( jointId );
  2489. // Ignore invalid joint.
  2490. if ( !pJoint )
  2491. return -1.0f;
  2492. // Fetch joint type.
  2493. const b2JointType jointType = pJoint->GetType();
  2494. if ( jointType != e_mouseJoint )
  2495. {
  2496. Con::warnf( "Invalid joint type of %s.", getJointTypeDescription(jointType) );
  2497. return -1.0f;
  2498. }
  2499. // Cast joint.
  2500. b2MouseJoint* pRealJoint = static_cast<b2MouseJoint*>( pJoint );
  2501. // Access joint.
  2502. return pRealJoint->GetMaxForce();
  2503. }
  2504. //-----------------------------------------------------------------------------
  2505. void Scene::setTargetJointFrequency(
  2506. const U32 jointId,
  2507. const F32 frequency )
  2508. {
  2509. // Fetch joint.
  2510. b2Joint* pJoint = findJoint( jointId );
  2511. // Ignore invalid joint.
  2512. if ( !pJoint )
  2513. return;
  2514. // Fetch joint type.
  2515. const b2JointType jointType = pJoint->GetType();
  2516. if ( jointType != e_mouseJoint )
  2517. {
  2518. Con::warnf( "Invalid joint type of %s.", getJointTypeDescription(jointType) );
  2519. return;
  2520. }
  2521. // Cast joint.
  2522. b2MouseJoint* pRealJoint = static_cast<b2MouseJoint*>( pJoint );
  2523. // Access joint.
  2524. pRealJoint->SetFrequency( frequency );
  2525. }
  2526. //-----------------------------------------------------------------------------
  2527. F32 Scene::getTargetJointFrequency( const U32 jointId )
  2528. {
  2529. // Fetch joint.
  2530. b2Joint* pJoint = findJoint( jointId );
  2531. // Ignore invalid joint.
  2532. if ( !pJoint )
  2533. return -1.0f;
  2534. // Fetch joint type.
  2535. const b2JointType jointType = pJoint->GetType();
  2536. if ( jointType != e_mouseJoint )
  2537. {
  2538. Con::warnf( "Invalid joint type of %s.", getJointTypeDescription(jointType) );
  2539. return -1.0f;
  2540. }
  2541. // Cast joint.
  2542. b2MouseJoint* pRealJoint = static_cast<b2MouseJoint*>( pJoint );
  2543. // Access joint.
  2544. return pRealJoint->GetFrequency();
  2545. }
  2546. //-----------------------------------------------------------------------------
  2547. void Scene::setTargetJointDampingRatio(
  2548. const U32 jointId,
  2549. const F32 dampingRatio )
  2550. {
  2551. // Fetch joint.
  2552. b2Joint* pJoint = findJoint( jointId );
  2553. // Ignore invalid joint.
  2554. if ( !pJoint )
  2555. return;
  2556. // Fetch joint type.
  2557. const b2JointType jointType = pJoint->GetType();
  2558. if ( jointType != e_mouseJoint )
  2559. {
  2560. Con::warnf( "Invalid joint type of %s.", getJointTypeDescription(jointType) );
  2561. return;
  2562. }
  2563. // Cast joint.
  2564. b2MouseJoint* pRealJoint = static_cast<b2MouseJoint*>( pJoint );
  2565. // Access joint.
  2566. pRealJoint->SetDampingRatio( dampingRatio );
  2567. }
  2568. //-----------------------------------------------------------------------------
  2569. F32 Scene::getTargetJointDampingRatio( const U32 jointId )
  2570. {
  2571. // Fetch joint.
  2572. b2Joint* pJoint = findJoint( jointId );
  2573. // Ignore invalid joint.
  2574. if ( !pJoint )
  2575. return -1.0f;
  2576. // Fetch joint type.
  2577. const b2JointType jointType = pJoint->GetType();
  2578. if ( jointType != e_mouseJoint )
  2579. {
  2580. Con::warnf( "Invalid joint type of %s.", getJointTypeDescription(jointType) );
  2581. return -1.0f;
  2582. }
  2583. // Cast joint.
  2584. b2MouseJoint* pRealJoint = static_cast<b2MouseJoint*>( pJoint );
  2585. // Access joint.
  2586. return pRealJoint->GetDampingRatio();
  2587. }
  2588. //-----------------------------------------------------------------------------
  2589. S32 Scene::createMotorJoint(
  2590. const SceneObject* pSceneObjectA, const SceneObject* pSceneObjectB,
  2591. const b2Vec2 linearOffset,
  2592. const F32 angularOffset,
  2593. const F32 maxForce,
  2594. const F32 maxTorque,
  2595. const F32 correctionFactor,
  2596. const bool collideConnected )
  2597. {
  2598. // Sanity!
  2599. if ( (pSceneObjectA != NULL && pSceneObjectA->getScene() == NULL) ||
  2600. (pSceneObjectB != NULL && pSceneObjectB->getScene() == NULL) )
  2601. {
  2602. // Warn.
  2603. Con::printf( "Cannot add a joint to a scene object that is not in a scene." );
  2604. return -1;
  2605. }
  2606. // Check for two invalid objects.
  2607. if ( pSceneObjectA == NULL && pSceneObjectB == NULL )
  2608. {
  2609. Con::warnf("Scene::createMotorJoint() - Cannot create joint without at least a single scene object." );
  2610. return -1;
  2611. }
  2612. // Fetch bodies.
  2613. b2Body* pBodyA = pSceneObjectA != NULL ? pSceneObjectA->getBody() : getGroundBody();
  2614. b2Body* pBodyB = pSceneObjectB != NULL ? pSceneObjectB->getBody() : getGroundBody();
  2615. // Populate definition.
  2616. b2MotorJointDef jointDef;
  2617. jointDef.userData = static_cast<PhysicsProxy*>(this);
  2618. jointDef.collideConnected = collideConnected;
  2619. jointDef.bodyA = pBodyA;
  2620. jointDef.bodyB = pBodyB;
  2621. jointDef.linearOffset = linearOffset;
  2622. jointDef.angularOffset = angularOffset;
  2623. jointDef.correctionFactor = correctionFactor;
  2624. jointDef.maxForce = maxForce;
  2625. jointDef.maxTorque = maxTorque;
  2626. // Create joint.
  2627. return createJoint( &jointDef );
  2628. }
  2629. //-----------------------------------------------------------------------------
  2630. void Scene::setMotorJointLinearOffset(
  2631. const U32 jointId,
  2632. const b2Vec2& linearOffset )
  2633. {
  2634. // Fetch joint.
  2635. b2Joint* pJoint = findJoint( jointId );
  2636. // Ignore invalid joint.
  2637. if ( !pJoint )
  2638. return;
  2639. // Fetch joint type.
  2640. const b2JointType jointType = pJoint->GetType();
  2641. if ( jointType != e_motorJoint )
  2642. {
  2643. Con::warnf( "Invalid joint type of %s.", getJointTypeDescription(jointType) );
  2644. return;
  2645. }
  2646. // Cast joint.
  2647. b2MotorJoint* pRealJoint = static_cast<b2MotorJoint*>( pJoint );
  2648. // Access joint.
  2649. pRealJoint->SetLinearOffset( linearOffset );
  2650. }
  2651. //-----------------------------------------------------------------------------
  2652. b2Vec2 Scene::getMotorJointLinearOffset( const U32 jointId )
  2653. {
  2654. // Fetch joint.
  2655. b2Joint* pJoint = findJoint( jointId );
  2656. // Ignore invalid joint.
  2657. if ( !pJoint )
  2658. return b2Vec2_zero;
  2659. // Fetch joint type.
  2660. const b2JointType jointType = pJoint->GetType();
  2661. if ( jointType != e_motorJoint )
  2662. {
  2663. Con::warnf( "Invalid joint type of %s.", getJointTypeDescription(jointType) );
  2664. return b2Vec2_zero;
  2665. }
  2666. // Cast joint.
  2667. b2MotorJoint* pRealJoint = static_cast<b2MotorJoint*>( pJoint );
  2668. // Access joint.
  2669. return pRealJoint->GetLinearOffset();
  2670. }
  2671. //-----------------------------------------------------------------------------
  2672. void Scene::setMotorJointAngularOffset(
  2673. const U32 jointId,
  2674. const F32 angularOffset )
  2675. {
  2676. // Fetch joint.
  2677. b2Joint* pJoint = findJoint( jointId );
  2678. // Ignore invalid joint.
  2679. if ( !pJoint )
  2680. return;
  2681. // Fetch joint type.
  2682. const b2JointType jointType = pJoint->GetType();
  2683. if ( jointType != e_motorJoint )
  2684. {
  2685. Con::warnf( "Invalid joint type of %s.", getJointTypeDescription(jointType) );
  2686. return;
  2687. }
  2688. // Cast joint.
  2689. b2MotorJoint* pRealJoint = static_cast<b2MotorJoint*>( pJoint );
  2690. // Access joint.
  2691. pRealJoint->SetAngularOffset( angularOffset );
  2692. }
  2693. //-----------------------------------------------------------------------------
  2694. F32 Scene::getMotorJointAngularOffset( const U32 jointId )
  2695. {
  2696. // Fetch joint.
  2697. b2Joint* pJoint = findJoint( jointId );
  2698. // Ignore invalid joint.
  2699. if ( !pJoint )
  2700. return -1.0f;
  2701. // Fetch joint type.
  2702. const b2JointType jointType = pJoint->GetType();
  2703. if ( jointType != e_motorJoint )
  2704. {
  2705. Con::warnf( "Invalid joint type of %s.", getJointTypeDescription(jointType) );
  2706. return -1.0f;
  2707. }
  2708. // Cast joint.
  2709. b2MotorJoint* pRealJoint = static_cast<b2MotorJoint*>( pJoint );
  2710. // Access joint.
  2711. return pRealJoint->GetAngularOffset();
  2712. }
  2713. //-----------------------------------------------------------------------------
  2714. void Scene::setMotorJointMaxForce(
  2715. const U32 jointId,
  2716. const F32 maxForce )
  2717. {
  2718. // Fetch joint.
  2719. b2Joint* pJoint = findJoint( jointId );
  2720. // Ignore invalid joint.
  2721. if ( !pJoint )
  2722. return;
  2723. // Fetch joint type.
  2724. const b2JointType jointType = pJoint->GetType();
  2725. if ( jointType != e_motorJoint )
  2726. {
  2727. Con::warnf( "Invalid joint type of %s.", getJointTypeDescription(jointType) );
  2728. return;
  2729. }
  2730. // Cast joint.
  2731. b2MotorJoint* pRealJoint = static_cast<b2MotorJoint*>( pJoint );
  2732. // Access joint.
  2733. pRealJoint->SetMaxForce( maxForce );
  2734. }
  2735. //-----------------------------------------------------------------------------
  2736. F32 Scene::getMotorJointMaxForce( const U32 jointId )
  2737. {
  2738. // Fetch joint.
  2739. b2Joint* pJoint = findJoint( jointId );
  2740. // Ignore invalid joint.
  2741. if ( !pJoint )
  2742. return -1.0f;
  2743. // Fetch joint type.
  2744. const b2JointType jointType = pJoint->GetType();
  2745. if ( jointType != e_motorJoint )
  2746. {
  2747. Con::warnf( "Invalid joint type of %s.", getJointTypeDescription(jointType) );
  2748. return -1.0f;
  2749. }
  2750. // Cast joint.
  2751. b2MotorJoint* pRealJoint = static_cast<b2MotorJoint*>( pJoint );
  2752. // Access joint.
  2753. return pRealJoint->GetMaxForce();
  2754. }
  2755. //-----------------------------------------------------------------------------
  2756. void Scene::setMotorJointMaxTorque(
  2757. const U32 jointId,
  2758. const F32 maxTorque )
  2759. {
  2760. // Fetch joint.
  2761. b2Joint* pJoint = findJoint( jointId );
  2762. // Ignore invalid joint.
  2763. if ( !pJoint )
  2764. return;
  2765. // Fetch joint type.
  2766. const b2JointType jointType = pJoint->GetType();
  2767. if ( jointType != e_motorJoint )
  2768. {
  2769. Con::warnf( "Invalid joint type of %s.", getJointTypeDescription(jointType) );
  2770. return;
  2771. }
  2772. // Cast joint.
  2773. b2MotorJoint* pRealJoint = static_cast<b2MotorJoint*>( pJoint );
  2774. // Access joint.
  2775. pRealJoint->SetMaxTorque( maxTorque );
  2776. }
  2777. //-----------------------------------------------------------------------------
  2778. F32 Scene::getMotorJointMaxTorque( const U32 jointId )
  2779. {
  2780. // Fetch joint.
  2781. b2Joint* pJoint = findJoint( jointId );
  2782. // Ignore invalid joint.
  2783. if ( !pJoint )
  2784. return -1.0f;
  2785. // Fetch joint type.
  2786. const b2JointType jointType = pJoint->GetType();
  2787. if ( jointType != e_motorJoint )
  2788. {
  2789. Con::warnf( "Invalid joint type of %s.", getJointTypeDescription(jointType) );
  2790. return -1.0f;
  2791. }
  2792. // Cast joint.
  2793. b2MotorJoint* pRealJoint = static_cast<b2MotorJoint*>( pJoint );
  2794. // Access joint.
  2795. return pRealJoint->GetMaxTorque();
  2796. }
  2797. //-----------------------------------------------------------------------------
  2798. void Scene::setDebugSceneObject( SceneObject* pSceneObject )
  2799. {
  2800. // Ignore no change.
  2801. if ( mpDebugSceneObject == pSceneObject )
  2802. return;
  2803. // Remove delete notification for existing monitored object.
  2804. if ( mpDebugSceneObject != NULL )
  2805. clearNotify( mpDebugSceneObject );
  2806. // Set monitored scene object.
  2807. mpDebugSceneObject = pSceneObject;
  2808. // Finish if resetting monitored object.
  2809. if ( pSceneObject == NULL )
  2810. return;
  2811. // Add delete notification for new monitored object.
  2812. deleteNotify( pSceneObject );
  2813. }
  2814. //-----------------------------------------------------------------------------
  2815. void Scene::setLayerSortMode( const U32 layer, const SceneRenderQueue::RenderSort sortMode )
  2816. {
  2817. // Is the layer valid?
  2818. if ( layer >= MAX_LAYERS_SUPPORTED )
  2819. {
  2820. // No, so warn.
  2821. Con::warnf( "Scene::setLayerSortMode() - Layer '%d' is out of range.", layer );
  2822. return;
  2823. }
  2824. // Is the sort mode valid?
  2825. if ( sortMode == SceneRenderQueue::RENDER_SORT_INVALID )
  2826. {
  2827. // No, so warn.
  2828. Con::warnf( "Scene::setLayerSortMode() - Sort mode is invalid for layer '%d'.", layer );
  2829. return;
  2830. }
  2831. mLayerSortModes[layer] = sortMode;
  2832. }
  2833. //-----------------------------------------------------------------------------
  2834. SceneRenderQueue::RenderSort Scene::getLayerSortMode( const U32 layer )
  2835. {
  2836. // Is the layer valid?
  2837. if ( layer >= MAX_LAYERS_SUPPORTED )
  2838. {
  2839. // No, so warn.
  2840. Con::warnf( "Scene::getLayerSortMode() - Layer '%d' is out of range.", layer );
  2841. return SceneRenderQueue::RENDER_SORT_INVALID;
  2842. }
  2843. return mLayerSortModes[layer];
  2844. }
  2845. //-----------------------------------------------------------------------------
  2846. void Scene::attachSceneWindow( SceneWindow* pSceneWindow2D )
  2847. {
  2848. // Ignore if already attached.
  2849. if ( isSceneWindowAttached( pSceneWindow2D ) )
  2850. return;
  2851. // Add to Attached List.
  2852. mAttachedSceneWindows.addObject( pSceneWindow2D );
  2853. }
  2854. //-----------------------------------------------------------------------------
  2855. void Scene::detachSceneWindow( SceneWindow* pSceneWindow2D )
  2856. {
  2857. // Ignore if not attached.
  2858. if ( !isSceneWindowAttached( pSceneWindow2D ) )
  2859. return;
  2860. // Add to Attached List.
  2861. mAttachedSceneWindows.removeObject( pSceneWindow2D );
  2862. }
  2863. //-----------------------------------------------------------------------------
  2864. void Scene::detachAllSceneWindows( void )
  2865. {
  2866. // Detach All Scene Windows.
  2867. while( mAttachedSceneWindows.size() > 0 )
  2868. dynamic_cast<SceneWindow*>(mAttachedSceneWindows[mAttachedSceneWindows.size()-1])->resetScene();
  2869. }
  2870. //-----------------------------------------------------------------------------
  2871. bool Scene::isSceneWindowAttached( SceneWindow* pSceneWindow2D )
  2872. {
  2873. for( SimSet::iterator itr = mAttachedSceneWindows.begin(); itr != mAttachedSceneWindows.end(); itr++ )
  2874. if ( pSceneWindow2D == dynamic_cast<SceneWindow*>(*itr) )
  2875. // Found.
  2876. return true;
  2877. // Not Found.
  2878. return false;
  2879. }
  2880. //-----------------------------------------------------------------------------
  2881. void Scene::addDeleteRequest( SceneObject* pSceneObject )
  2882. {
  2883. // Ignore if it's already being safe-deleted.
  2884. if ( pSceneObject->isBeingDeleted() )
  2885. return;
  2886. // Populate Delete Request.
  2887. tDeleteRequest deleteRequest = { pSceneObject->getId(), NULL, false };
  2888. // Push Delete Request.
  2889. mDeleteRequests.push_back( deleteRequest );
  2890. // Flag Delete in Progress.
  2891. pSceneObject->mBeingSafeDeleted = true;
  2892. }
  2893. //-----------------------------------------------------------------------------
  2894. void Scene::processDeleteRequests( const bool forceImmediate )
  2895. {
  2896. // Ignore if there's no delete requests!
  2897. if ( mDeleteRequests.size() == 0 )
  2898. return;
  2899. // Validate All Delete Requests.
  2900. U32 safeDeleteReadyCount = 0;
  2901. for ( U32 requestIndex = 0; requestIndex < (U32)mDeleteRequests.size(); )
  2902. {
  2903. // Fetch Reference to Delete Request.
  2904. tDeleteRequest& deleteRequest = mDeleteRequests[requestIndex];
  2905. // Fetch Object.
  2906. // NOTE:- Let's be safer and check that it's definitely a scene-object.
  2907. SceneObject* pSceneObject = dynamic_cast<SceneObject*>( Sim::findObject( deleteRequest.mObjectId ) );
  2908. // Does this object exist?
  2909. if ( pSceneObject )
  2910. {
  2911. // Yes, so write object.
  2912. deleteRequest.mpSceneObject = pSceneObject;
  2913. // Calculate Safe-Ready Flag.
  2914. deleteRequest.mSafeDeleteReady = forceImmediate || pSceneObject->getSafeDelete();
  2915. // Is it ready to safe-delete?
  2916. if ( deleteRequest.mSafeDeleteReady )
  2917. {
  2918. // Yes, so increase safe-ready count.
  2919. ++safeDeleteReadyCount;
  2920. }
  2921. }
  2922. else
  2923. {
  2924. // No, so it looks like the object got deleted prematurely; let's just remove
  2925. // the request instead.
  2926. mDeleteRequests.erase_fast( requestIndex );
  2927. // Repeat this item.
  2928. continue;
  2929. }
  2930. // Skip to next request index.
  2931. ++requestIndex;
  2932. }
  2933. // Stop if there's no delete requests!
  2934. if ( mDeleteRequests.size() == 0 )
  2935. return;
  2936. // Transfer Delete-Requests to Temporary version.
  2937. // NOTE:- We do this because we may delete objects which have dependencies. This would
  2938. // cause objects to be added to the safe-delete list. We don't want to work on
  2939. // the list whilst this is happening so we'll transfer it to a temporary list.
  2940. mDeleteRequestsTemp = mDeleteRequests;
  2941. // Can we process all remaining delete-requests?
  2942. if ( safeDeleteReadyCount == (U32)mDeleteRequestsTemp.size() )
  2943. {
  2944. // Yes, so process ALL safe-ready delete-requests.
  2945. for ( U32 requestIndex = 0; requestIndex < (U32)mDeleteRequestsTemp.size(); ++requestIndex )
  2946. {
  2947. // Yes, so fetch object.
  2948. SceneObject* pSceneObject = mDeleteRequestsTemp[requestIndex].mpSceneObject;
  2949. // Do script callback.
  2950. Con::executef(this, 2, "onSafeDelete", pSceneObject->getIdString() );
  2951. // Destroy the object.
  2952. pSceneObject->deleteObject();
  2953. }
  2954. // Remove All delete-requests.
  2955. mDeleteRequestsTemp.clear();
  2956. }
  2957. else
  2958. {
  2959. // No, so process only safe-ready delete-requests.
  2960. for ( U32 requestIndex = 0; requestIndex <(U32) mDeleteRequestsTemp.size(); )
  2961. {
  2962. // Fetch Reference to Delete Request.
  2963. tDeleteRequest& deleteRequest = mDeleteRequestsTemp[requestIndex];
  2964. // Is the Object Safe-Ready?
  2965. if ( deleteRequest.mSafeDeleteReady )
  2966. {
  2967. // Yes, so fetch object.
  2968. SceneObject* pSceneObject = deleteRequest.mpSceneObject;
  2969. // Do script callback.
  2970. Con::executef(this, 2, "onSafeDelete", pSceneObject->getIdString() );
  2971. // Destroy the object.
  2972. pSceneObject->deleteObject();
  2973. // Quickly remove delete-request.
  2974. mDeleteRequestsTemp.erase_fast( requestIndex );
  2975. // Repeat this item.
  2976. continue;
  2977. }
  2978. // Skip to next request index.
  2979. ++requestIndex;
  2980. }
  2981. }
  2982. }
  2983. //-----------------------------------------------------------------------------
  2984. void Scene::SayGoodbye( b2Joint* pJoint )
  2985. {
  2986. // Find the joint id.
  2987. const U32 jointId = findJointId( pJoint );
  2988. // Ignore a bad joint.
  2989. if ( jointId == 0 )
  2990. return;
  2991. // Remove joint references.
  2992. mJoints.erase( jointId );
  2993. mReverseJoints.erase( pJoint );
  2994. }
  2995. //-----------------------------------------------------------------------------
  2996. SceneObject* Scene::create( const char* pType )
  2997. {
  2998. // Sanity!
  2999. AssertFatal( pType != NULL, "Scene::create() - Cannot create a NULL type." );
  3000. // Find the class rep.
  3001. AbstractClassRep* pClassRep = AbstractClassRep::findClassRep( pType );
  3002. // Did we find the type?
  3003. if ( pClassRep == NULL )
  3004. {
  3005. // No, so warn.
  3006. Con::warnf( "Scene::create() - Could not find type '%s' to create.", pType );
  3007. return NULL;
  3008. }
  3009. // Find the scene object rep.
  3010. AbstractClassRep* pSceneObjectRep = AbstractClassRep::findClassRep( "SceneObject" );
  3011. // Sanity!
  3012. AssertFatal( pSceneObjectRep != NULL, "Scene::create() - Could not find SceneObject class rep." );
  3013. // Is the type derived from scene object?
  3014. if ( !pClassRep->isClass( pSceneObjectRep ) )
  3015. {
  3016. // No, so warn.
  3017. Con::warnf( "Scene::create() - Type '%s' is not derived from SceneObject.", pType );
  3018. return NULL;
  3019. }
  3020. // Create the type.
  3021. SceneObject* pSceneObject = dynamic_cast<SceneObject*>( pClassRep->create() );
  3022. // Sanity!
  3023. AssertFatal( pSceneObject != NULL, "Scene::create() - Failed to create type via class rep." );
  3024. // Attempt to register the object.
  3025. if ( !pSceneObject->registerObject() )
  3026. {
  3027. // No, so warn.
  3028. Con::warnf( "Scene::create() - Failed to register type '%s'.", pType );
  3029. delete pSceneObject;
  3030. return NULL;
  3031. }
  3032. // Add to the scene.
  3033. addToScene( pSceneObject );
  3034. return pSceneObject;
  3035. }
  3036. //-----------------------------------------------------------------------------
  3037. void Scene::onTamlPreRead( void )
  3038. {
  3039. // Call parent.
  3040. Parent::onTamlPreRead();
  3041. }
  3042. //-----------------------------------------------------------------------------
  3043. void Scene::onTamlPostRead( const TamlCustomNodes& customNodes )
  3044. {
  3045. // Call parent.
  3046. Parent::onTamlPostRead( customNodes );
  3047. // Reset the loading scene.
  3048. Scene::LoadingScene = NULL;
  3049. // Find joint custom node.
  3050. const TamlCustomNode* pJointNode = customNodes.findNode( jointCustomNodeName );
  3051. // Do we have any joints?
  3052. if ( pJointNode != NULL )
  3053. {
  3054. // Yes, so fetch children joint nodes.
  3055. const TamlCustomNodeVector& jointChildren = pJointNode->getChildren();
  3056. // Iterate joints.
  3057. for( TamlCustomNodeVector::const_iterator jointNodeItr = jointChildren.begin(); jointNodeItr != jointChildren.end(); ++jointNodeItr )
  3058. {
  3059. // Fetch joint node,
  3060. TamlCustomNode* pJointNode = *jointNodeItr;
  3061. // Fetch node name.
  3062. StringTableEntry nodeName = pJointNode->getNodeName();
  3063. // Is this a distance joint?
  3064. if ( nodeName == jointDistanceNodeName )
  3065. {
  3066. // Fetch joint children.
  3067. const TamlCustomNodeVector& jointChildren = pJointNode->getChildren();
  3068. // Fetch joint objects.
  3069. SceneObject* pSceneObjectA = jointChildren.size() > 0 ? jointChildren[0]->getProxyObject<SceneObject>(true) : NULL;
  3070. SceneObject* pSceneObjectB = jointChildren.size() == 2 ? jointChildren[1]->getProxyObject<SceneObject>(true) : NULL;
  3071. // Did we get any connected objects?
  3072. if( pSceneObjectA == NULL && pSceneObjectB == NULL )
  3073. {
  3074. // No, so warn.
  3075. Con::warnf( "Scene::onTamlPostRead() - Encountered a joint '%s' but it has invalid connected objects.", nodeName );
  3076. continue;
  3077. }
  3078. b2Vec2 localAnchorA = b2Vec2_zero;
  3079. b2Vec2 localAnchorB = b2Vec2_zero;
  3080. bool collideConnected = false;
  3081. F32 length = -1.0f;
  3082. F32 frequency = 0.0f;
  3083. F32 dampingRatio = 0.0f;
  3084. // Fetch joint fields.
  3085. const TamlCustomFieldVector& jointFields = pJointNode->getFields();
  3086. // Iterate property fields.
  3087. for ( TamlCustomFieldVector::const_iterator jointFieldItr = jointFields.begin(); jointFieldItr != jointFields.end(); ++jointFieldItr )
  3088. {
  3089. // Fetch field node.
  3090. TamlCustomField* pField = *jointFieldItr;
  3091. // Fetch property field name.
  3092. StringTableEntry fieldName = pField->getFieldName();
  3093. // Fetch fields.
  3094. if ( fieldName == jointLocalAnchorAName )
  3095. {
  3096. pField->getFieldValue( localAnchorA );
  3097. }
  3098. else if ( fieldName == jointLocalAnchorBName )
  3099. {
  3100. pField->getFieldValue( localAnchorB );
  3101. }
  3102. else if ( fieldName == jointCollideConnectedName )
  3103. {
  3104. pField->getFieldValue( collideConnected );
  3105. }
  3106. else if ( fieldName == jointDistanceLengthName )
  3107. {
  3108. pField->getFieldValue( length );
  3109. }
  3110. else if ( fieldName == jointDistanceFrequencyName )
  3111. {
  3112. pField->getFieldValue( frequency );
  3113. }
  3114. else if ( fieldName == jointDistanceDampingRatioName )
  3115. {
  3116. pField->getFieldValue( dampingRatio );
  3117. }
  3118. }
  3119. // Create joint.
  3120. createDistanceJoint( pSceneObjectA, pSceneObjectB, localAnchorA, localAnchorB, length, frequency, dampingRatio, collideConnected );
  3121. }
  3122. // is this a rope joint?
  3123. else if ( nodeName == jointRopeNodeName )
  3124. {
  3125. // Fetch joint children.
  3126. const TamlCustomNodeVector& jointChildren = pJointNode->getChildren();
  3127. // Fetch joint objects.
  3128. SceneObject* pSceneObjectA = jointChildren.size() > 0 ? jointChildren[0]->getProxyObject<SceneObject>(true) : NULL;
  3129. SceneObject* pSceneObjectB = jointChildren.size() == 2 ? jointChildren[1]->getProxyObject<SceneObject>(true) : NULL;
  3130. // Did we get any connected objects?
  3131. if( pSceneObjectA == NULL && pSceneObjectB == NULL )
  3132. {
  3133. // No, so warn.
  3134. Con::warnf( "Scene::onTamlPostRead() - Encountered a joint '%s' but it has invalid connected objects.", nodeName );
  3135. continue;
  3136. }
  3137. b2Vec2 localAnchorA = b2Vec2_zero;
  3138. b2Vec2 localAnchorB = b2Vec2_zero;
  3139. bool collideConnected = false;
  3140. F32 maxLength = -1.0f;
  3141. // Fetch joint fields.
  3142. const TamlCustomFieldVector& jointFields = pJointNode->getFields();
  3143. // Iterate property fields.
  3144. for ( TamlCustomFieldVector::const_iterator jointFieldItr = jointFields.begin(); jointFieldItr != jointFields.end(); ++jointFieldItr )
  3145. {
  3146. // Fetch field node.
  3147. TamlCustomField* pField = *jointFieldItr;
  3148. // Fetch property field name.
  3149. StringTableEntry fieldName = pField->getFieldName();
  3150. // Fetch fields.
  3151. if ( fieldName == jointLocalAnchorAName )
  3152. {
  3153. pField->getFieldValue( localAnchorA );
  3154. }
  3155. else if ( fieldName == jointLocalAnchorBName )
  3156. {
  3157. pField->getFieldValue( localAnchorB );
  3158. }
  3159. else if ( fieldName == jointCollideConnectedName )
  3160. {
  3161. pField->getFieldValue( collideConnected );
  3162. }
  3163. else if ( fieldName == jointRopeMaxLengthName )
  3164. {
  3165. pField->getFieldValue( maxLength );
  3166. }
  3167. }
  3168. // Create joint.
  3169. createRopeJoint( pSceneObjectA, pSceneObjectB, localAnchorA, localAnchorB, maxLength, collideConnected );
  3170. }
  3171. // Is this a revolute joint?
  3172. else if ( nodeName == jointRevoluteNodeName )
  3173. {
  3174. // Fetch joint children.
  3175. const TamlCustomNodeVector& jointChildren = pJointNode->getChildren();
  3176. // Fetch joint objects.
  3177. SceneObject* pSceneObjectA = jointChildren.size() > 0 ? jointChildren[0]->getProxyObject<SceneObject>(true) : NULL;
  3178. SceneObject* pSceneObjectB = jointChildren.size() == 2 ? jointChildren[1]->getProxyObject<SceneObject>(true) : NULL;
  3179. // Did we get any connected objects?
  3180. if( pSceneObjectA == NULL && pSceneObjectB == NULL )
  3181. {
  3182. // No, so warn.
  3183. Con::warnf( "Scene::onTamlPostRead() - Encountered a joint '%s' but it has invalid connected objects.", nodeName );
  3184. continue;
  3185. }
  3186. b2Vec2 localAnchorA = b2Vec2_zero;
  3187. b2Vec2 localAnchorB = b2Vec2_zero;
  3188. bool collideConnected = false;
  3189. bool enableLimit = false;
  3190. F32 lowerAngle = 0.0f;
  3191. F32 upperAngle = 0.0f;
  3192. bool enableMotor = false;
  3193. F32 motorSpeed = b2_pi;
  3194. F32 maxMotorTorque = 0.0f;
  3195. // Fetch joint fields.
  3196. const TamlCustomFieldVector& jointFields = pJointNode->getFields();
  3197. // Iterate property fields.
  3198. for ( TamlCustomFieldVector::const_iterator jointFieldItr = jointFields.begin(); jointFieldItr != jointFields.end(); ++jointFieldItr )
  3199. {
  3200. // Fetch field node.
  3201. TamlCustomField* pField = *jointFieldItr;
  3202. // Fetch property field name.
  3203. StringTableEntry fieldName = pField->getFieldName();
  3204. // Fetch fields.
  3205. if ( fieldName == jointLocalAnchorAName )
  3206. {
  3207. pField->getFieldValue( localAnchorA );
  3208. }
  3209. else if ( fieldName == jointLocalAnchorBName )
  3210. {
  3211. pField->getFieldValue( localAnchorB );
  3212. }
  3213. else if ( fieldName == jointCollideConnectedName )
  3214. {
  3215. pField->getFieldValue( collideConnected );
  3216. }
  3217. else if ( fieldName == jointRevoluteLimitLowerAngleName )
  3218. {
  3219. pField->getFieldValue( lowerAngle );
  3220. lowerAngle = mDegToRad( lowerAngle );
  3221. enableLimit = true;
  3222. }
  3223. else if ( fieldName == jointRevoluteLimitUpperAngleName )
  3224. {
  3225. pField->getFieldValue( upperAngle );
  3226. upperAngle = mDegToRad( upperAngle );
  3227. enableLimit = true;
  3228. }
  3229. else if ( fieldName == jointRevoluteMotorSpeedName )
  3230. {
  3231. pField->getFieldValue( motorSpeed );
  3232. motorSpeed = mDegToRad( motorSpeed );
  3233. enableMotor = true;
  3234. }
  3235. else if ( fieldName == jointRevoluteMotorMaxTorqueName )
  3236. {
  3237. pField->getFieldValue( maxMotorTorque );
  3238. enableMotor = true;
  3239. }
  3240. }
  3241. // Create joint.
  3242. const U32 jointId = createRevoluteJoint( pSceneObjectA, pSceneObjectB, localAnchorA, localAnchorB, collideConnected );
  3243. if ( enableLimit )
  3244. setRevoluteJointLimit( jointId, true, lowerAngle, upperAngle );
  3245. if ( enableMotor )
  3246. setRevoluteJointMotor( jointId, true, motorSpeed, maxMotorTorque );
  3247. }
  3248. // is this a weld joint?
  3249. else if ( nodeName == jointWeldNodeName )
  3250. {
  3251. // Fetch joint children.
  3252. const TamlCustomNodeVector& jointChildren = pJointNode->getChildren();
  3253. // Fetch joint objects.
  3254. SceneObject* pSceneObjectA = jointChildren.size() > 0 ? jointChildren[0]->getProxyObject<SceneObject>(true) : NULL;
  3255. SceneObject* pSceneObjectB = jointChildren.size() == 2 ? jointChildren[1]->getProxyObject<SceneObject>(true) : NULL;
  3256. // Did we get any connected objects?
  3257. if( pSceneObjectA == NULL && pSceneObjectB == NULL )
  3258. {
  3259. // No, so warn.
  3260. Con::warnf( "Scene::onTamlPostRead() - Encountered a joint '%s' but it has invalid connected objects.", nodeName );
  3261. continue;
  3262. }
  3263. b2Vec2 localAnchorA = b2Vec2_zero;
  3264. b2Vec2 localAnchorB = b2Vec2_zero;
  3265. bool collideConnected = false;
  3266. F32 frequency = 0.0f;
  3267. F32 dampingRatio = 0.0f;
  3268. // Fetch joint fields.
  3269. const TamlCustomFieldVector& jointFields = pJointNode->getFields();
  3270. // Iterate property fields.
  3271. for ( TamlCustomFieldVector::const_iterator jointFieldItr = jointFields.begin(); jointFieldItr != jointFields.end(); ++jointFieldItr )
  3272. {
  3273. // Fetch field node.
  3274. TamlCustomField* pField = *jointFieldItr;
  3275. // Fetch property field name.
  3276. StringTableEntry fieldName = pField->getFieldName();
  3277. // Fetch fields.
  3278. if ( fieldName == jointLocalAnchorAName )
  3279. {
  3280. pField->getFieldValue( localAnchorA );
  3281. }
  3282. else if ( fieldName == jointLocalAnchorBName )
  3283. {
  3284. pField->getFieldValue( localAnchorB );
  3285. }
  3286. else if ( fieldName == jointCollideConnectedName )
  3287. {
  3288. pField->getFieldValue( collideConnected );
  3289. }
  3290. else if ( fieldName == jointWeldFrequencyName )
  3291. {
  3292. pField->getFieldValue( frequency );
  3293. }
  3294. else if ( fieldName == jointWeldDampingRatioName )
  3295. {
  3296. pField->getFieldValue( dampingRatio );
  3297. }
  3298. }
  3299. // Create joint.
  3300. createWeldJoint( pSceneObjectA, pSceneObjectB, localAnchorA, localAnchorB, frequency, dampingRatio, collideConnected );
  3301. }
  3302. // Is this a wheel joint?
  3303. else if ( nodeName == jointWheelNodeName )
  3304. {
  3305. // Fetch joint children.
  3306. const TamlCustomNodeVector& jointChildren = pJointNode->getChildren();
  3307. // Fetch joint objects.
  3308. SceneObject* pSceneObjectA = jointChildren.size() > 0 ? jointChildren[0]->getProxyObject<SceneObject>(true) : NULL;
  3309. SceneObject* pSceneObjectB = jointChildren.size() == 2 ? jointChildren[1]->getProxyObject<SceneObject>(true) : NULL;
  3310. // Did we get any connected objects?
  3311. if( pSceneObjectA == NULL && pSceneObjectB == NULL )
  3312. {
  3313. // No, so warn.
  3314. Con::warnf( "Scene::onTamlPostRead() - Encountered a joint '%s' but it has invalid connected objects.", nodeName );
  3315. continue;
  3316. }
  3317. b2Vec2 localAnchorA = b2Vec2_zero;
  3318. b2Vec2 localAnchorB = b2Vec2_zero;
  3319. bool collideConnected = false;
  3320. bool enableMotor = false;
  3321. F32 motorSpeed = b2_pi;
  3322. F32 maxMotorTorque = 0.0f;
  3323. F32 frequency = 0.0f;
  3324. F32 dampingRatio = 0.0f;
  3325. b2Vec2 worldAxis( 0.0f, 1.0f );
  3326. // Fetch joint fields.
  3327. const TamlCustomFieldVector& jointFields = pJointNode->getFields();
  3328. // Iterate property fields.
  3329. for ( TamlCustomFieldVector::const_iterator jointFieldItr = jointFields.begin(); jointFieldItr != jointFields.end(); ++jointFieldItr )
  3330. {
  3331. // Fetch field node.
  3332. TamlCustomField* pField = *jointFieldItr;
  3333. // Fetch property field name.
  3334. StringTableEntry fieldName = pField->getFieldName();
  3335. // Fetch fields.
  3336. if ( fieldName == jointLocalAnchorAName )
  3337. {
  3338. pField->getFieldValue( localAnchorA );
  3339. }
  3340. else if ( fieldName == jointLocalAnchorBName )
  3341. {
  3342. pField->getFieldValue( localAnchorB );
  3343. }
  3344. else if ( fieldName == jointCollideConnectedName )
  3345. {
  3346. pField->getFieldValue( collideConnected );
  3347. }
  3348. else if ( fieldName == jointWheelMotorSpeedName )
  3349. {
  3350. pField->getFieldValue( motorSpeed );
  3351. motorSpeed = mDegToRad( motorSpeed );
  3352. enableMotor = true;
  3353. }
  3354. else if ( fieldName == jointWheelMotorMaxTorqueName )
  3355. {
  3356. pField->getFieldValue( maxMotorTorque );
  3357. enableMotor = true;
  3358. }
  3359. else if ( fieldName == jointWheelFrequencyName )
  3360. {
  3361. pField->getFieldValue( frequency );
  3362. }
  3363. else if ( fieldName == jointWheelDampingRatioName )
  3364. {
  3365. pField->getFieldValue( dampingRatio );
  3366. }
  3367. else if ( fieldName == jointWheelWorldAxisName )
  3368. {
  3369. pField->getFieldValue( worldAxis );
  3370. }
  3371. }
  3372. // Create joint.
  3373. const U32 jointId = createWheelJoint( pSceneObjectA, pSceneObjectB, localAnchorA, localAnchorB, worldAxis, collideConnected );
  3374. if ( enableMotor )
  3375. setWheelJointMotor( jointId, true, motorSpeed, maxMotorTorque );
  3376. setWheelJointFrequency( jointId, frequency );
  3377. setWheelJointDampingRatio( jointId, dampingRatio );
  3378. }
  3379. // Is this a friction joint?
  3380. else if ( nodeName == jointFrictionNodeName )
  3381. {
  3382. // Fetch joint children.
  3383. const TamlCustomNodeVector& jointChildren = pJointNode->getChildren();
  3384. // Fetch joint objects.
  3385. SceneObject* pSceneObjectA = jointChildren.size() > 0 ? jointChildren[0]->getProxyObject<SceneObject>(true) : NULL;
  3386. SceneObject* pSceneObjectB = jointChildren.size() == 2 ? jointChildren[1]->getProxyObject<SceneObject>(true) : NULL;
  3387. // Did we get any connected objects?
  3388. if( pSceneObjectA == NULL && pSceneObjectB == NULL )
  3389. {
  3390. // No, so warn.
  3391. Con::warnf( "Scene::onTamlPostRead() - Encountered a joint '%s' but it has invalid connected objects.", nodeName );
  3392. continue;
  3393. }
  3394. b2Vec2 localAnchorA = b2Vec2_zero;
  3395. b2Vec2 localAnchorB = b2Vec2_zero;
  3396. bool collideConnected = false;
  3397. F32 maxForce = 0.0f;
  3398. F32 maxTorque = 0.0f;
  3399. // Fetch joint fields.
  3400. const TamlCustomFieldVector& jointFields = pJointNode->getFields();
  3401. // Iterate property fields.
  3402. for ( TamlCustomFieldVector::const_iterator jointFieldItr = jointFields.begin(); jointFieldItr != jointFields.end(); ++jointFieldItr )
  3403. {
  3404. // Fetch field node.
  3405. TamlCustomField* pField = *jointFieldItr;
  3406. // Fetch property field name.
  3407. StringTableEntry fieldName = pField->getFieldName();
  3408. // Fetch fields.
  3409. if ( fieldName == jointLocalAnchorAName )
  3410. {
  3411. pField->getFieldValue( localAnchorA );
  3412. }
  3413. else if ( fieldName == jointLocalAnchorBName )
  3414. {
  3415. pField->getFieldValue( localAnchorB );
  3416. }
  3417. else if ( fieldName == jointCollideConnectedName )
  3418. {
  3419. pField->getFieldValue( collideConnected );
  3420. }
  3421. else if ( fieldName == jointFrictionMaxForceName )
  3422. {
  3423. pField->getFieldValue( maxForce );
  3424. }
  3425. else if ( fieldName == jointFrictionMaxTorqueName )
  3426. {
  3427. pField->getFieldValue( maxTorque );
  3428. }
  3429. }
  3430. // Create joint.
  3431. createFrictionJoint( pSceneObjectA, pSceneObjectB, localAnchorA, localAnchorB, maxForce, maxTorque, collideConnected );
  3432. }
  3433. // Is this a prismatic joint?
  3434. else if ( nodeName == jointPrismaticNodeName )
  3435. {
  3436. // Fetch joint children.
  3437. const TamlCustomNodeVector& jointChildren = pJointNode->getChildren();
  3438. // Fetch joint objects.
  3439. SceneObject* pSceneObjectA = jointChildren.size() > 0 ? jointChildren[0]->getProxyObject<SceneObject>(true) : NULL;
  3440. SceneObject* pSceneObjectB = jointChildren.size() == 2 ? jointChildren[1]->getProxyObject<SceneObject>(true) : NULL;
  3441. // Did we get any connected objects?
  3442. if( pSceneObjectA == NULL && pSceneObjectB == NULL )
  3443. {
  3444. // No, so warn.
  3445. Con::warnf( "Scene::onTamlPostRead() - Encountered a joint '%s' but it has invalid connected objects.", nodeName );
  3446. continue;
  3447. }
  3448. b2Vec2 localAnchorA = b2Vec2_zero;
  3449. b2Vec2 localAnchorB = b2Vec2_zero;
  3450. bool collideConnected = false;
  3451. bool enableLimit;
  3452. F32 lowerTransLimit = 0.0f;
  3453. F32 upperTransLimit = 1.0f;
  3454. bool enableMotor = false;
  3455. F32 motorSpeed = b2_pi;
  3456. F32 maxMotorForce = 0.0f;
  3457. b2Vec2 worldAxis( 0.0f, 1.0f );
  3458. // Fetch joint fields.
  3459. const TamlCustomFieldVector& jointFields = pJointNode->getFields();
  3460. // Iterate property fields.
  3461. for ( TamlCustomFieldVector::const_iterator jointFieldItr = jointFields.begin(); jointFieldItr != jointFields.end(); ++jointFieldItr )
  3462. {
  3463. // Fetch field node.
  3464. TamlCustomField* pField = *jointFieldItr;
  3465. // Fetch property field name.
  3466. StringTableEntry fieldName = pField->getFieldName();
  3467. // Fetch fields.
  3468. if ( fieldName == jointLocalAnchorAName )
  3469. {
  3470. pField->getFieldValue( localAnchorA );
  3471. }
  3472. else if ( fieldName == jointLocalAnchorBName )
  3473. {
  3474. pField->getFieldValue( localAnchorB );
  3475. }
  3476. else if ( fieldName == jointCollideConnectedName )
  3477. {
  3478. pField->getFieldValue( collideConnected );
  3479. }
  3480. else if ( fieldName == jointPrismaticLimitLowerTransName )
  3481. {
  3482. pField->getFieldValue( lowerTransLimit );
  3483. enableLimit = true;
  3484. }
  3485. else if ( fieldName == jointPrismaticLimitUpperTransName )
  3486. {
  3487. pField->getFieldValue( upperTransLimit );
  3488. enableLimit = true;
  3489. }
  3490. else if ( fieldName == jointPrismaticMotorSpeedName )
  3491. {
  3492. pField->getFieldValue( motorSpeed );
  3493. motorSpeed = mDegToRad( motorSpeed );
  3494. enableMotor = true;
  3495. }
  3496. else if ( fieldName == jointPrismaticMotorMaxForceName )
  3497. {
  3498. pField->getFieldValue( maxMotorForce );
  3499. enableMotor = true;
  3500. }
  3501. else if ( fieldName == jointPrismaticWorldAxisName )
  3502. {
  3503. pField->getFieldValue( worldAxis );
  3504. }
  3505. }
  3506. // Create joint.
  3507. const U32 jointId = createPrismaticJoint( pSceneObjectA, pSceneObjectB, localAnchorA, localAnchorB, worldAxis, collideConnected );
  3508. if ( enableLimit )
  3509. setPrismaticJointLimit( jointId, true, lowerTransLimit, upperTransLimit );
  3510. if ( enableMotor )
  3511. setPrismaticJointMotor( jointId, true, motorSpeed, maxMotorForce );
  3512. }
  3513. // Is this a pulley joint?
  3514. else if ( nodeName == jointPulleyNodeName )
  3515. {
  3516. // Fetch joint children.
  3517. const TamlCustomNodeVector& jointChildren = pJointNode->getChildren();
  3518. // Fetch joint objects.
  3519. SceneObject* pSceneObjectA = jointChildren.size() > 0 ? jointChildren[0]->getProxyObject<SceneObject>(true) : NULL;
  3520. SceneObject* pSceneObjectB = jointChildren.size() == 2 ? jointChildren[1]->getProxyObject<SceneObject>(true) : NULL;
  3521. // Did we get any connected objects?
  3522. if( pSceneObjectA == NULL && pSceneObjectB == NULL )
  3523. {
  3524. // No, so warn.
  3525. Con::warnf( "Scene::onTamlPostRead() - Encountered a joint '%s' but it has invalid connected objects.", nodeName );
  3526. continue;
  3527. }
  3528. b2Vec2 localAnchorA = b2Vec2_zero;
  3529. b2Vec2 localAnchorB = b2Vec2_zero;
  3530. bool collideConnected = false;
  3531. F32 lengthA = -1.0f;
  3532. F32 lengthB = -1.0f;
  3533. F32 ratio = 0.5f;
  3534. b2Vec2 worldGroundAnchorA = b2Vec2_zero;
  3535. b2Vec2 worldGroundAnchorB = b2Vec2_zero;
  3536. // Fetch joint fields.
  3537. const TamlCustomFieldVector& jointFields = pJointNode->getFields();
  3538. // Iterate property fields.
  3539. for ( TamlCustomFieldVector::const_iterator jointFieldItr = jointFields.begin(); jointFieldItr != jointFields.end(); ++jointFieldItr )
  3540. {
  3541. // Fetch field node.
  3542. TamlCustomField* pField = *jointFieldItr;
  3543. // Fetch property field name.
  3544. StringTableEntry fieldName = pField->getFieldName();
  3545. // Fetch fields.
  3546. if ( fieldName == jointLocalAnchorAName )
  3547. {
  3548. pField->getFieldValue( localAnchorA );
  3549. }
  3550. else if ( fieldName == jointLocalAnchorBName )
  3551. {
  3552. pField->getFieldValue( localAnchorB );
  3553. }
  3554. else if ( fieldName == jointCollideConnectedName )
  3555. {
  3556. pField->getFieldValue( collideConnected );
  3557. }
  3558. else if ( fieldName == jointPulleyLengthAName )
  3559. {
  3560. pField->getFieldValue( lengthA );
  3561. }
  3562. else if ( fieldName == jointPulleyLengthBName )
  3563. {
  3564. pField->getFieldValue( lengthB );
  3565. }
  3566. else if ( fieldName == jointPulleyRatioName )
  3567. {
  3568. pField->getFieldValue( ratio );
  3569. }
  3570. else if ( fieldName == jointPulleyGroundAnchorAName )
  3571. {
  3572. pField->getFieldValue( worldGroundAnchorA );
  3573. }
  3574. else if ( fieldName == jointPulleyGroundAnchorBName )
  3575. {
  3576. pField->getFieldValue( worldGroundAnchorB );
  3577. }
  3578. }
  3579. // Create joint.
  3580. createPulleyJoint( pSceneObjectA, pSceneObjectB, localAnchorA, localAnchorB, worldGroundAnchorA, worldGroundAnchorB, ratio, lengthA, lengthB, collideConnected );
  3581. }
  3582. // Is this a target joint?
  3583. else if ( nodeName == jointTargetNodeName )
  3584. {
  3585. // Fetch joint children.
  3586. const TamlCustomNodeVector& jointChildren = pJointNode->getChildren();
  3587. // Fetch joint objects.
  3588. SceneObject* pSceneObject = jointChildren.size() == 1 ? jointChildren[0]->getProxyObject<SceneObject>(true) : NULL;
  3589. // Did we get any connected objects?
  3590. if( pSceneObject == NULL )
  3591. {
  3592. // No, so warn.
  3593. Con::warnf( "Scene::onTamlPostRead() - Encountered a joint '%s' but it has an invalid connected object.", nodeName );
  3594. continue;
  3595. }
  3596. bool collideConnected = false;
  3597. b2Vec2 worldTarget = b2Vec2_zero;
  3598. F32 maxForce = 1.0f;
  3599. F32 frequency = 5.0f;
  3600. F32 dampingRatio = 0.7f;
  3601. // Fetch joint fields.
  3602. const TamlCustomFieldVector& jointFields = pJointNode->getFields();
  3603. // Iterate property fields.
  3604. for ( TamlCustomFieldVector::const_iterator jointFieldItr = jointFields.begin(); jointFieldItr != jointFields.end(); ++jointFieldItr )
  3605. {
  3606. // Fetch field node.
  3607. TamlCustomField* pField = *jointFieldItr;
  3608. // Fetch property field name.
  3609. StringTableEntry fieldName = pField->getFieldName();
  3610. // Fetch fields.
  3611. if ( fieldName == jointCollideConnectedName )
  3612. {
  3613. pField->getFieldValue( collideConnected );
  3614. }
  3615. else if ( fieldName == jointTargetWorldTargetName )
  3616. {
  3617. pField->getFieldValue( worldTarget );
  3618. }
  3619. else if ( fieldName == jointTargetMaxForceName )
  3620. {
  3621. pField->getFieldValue( maxForce );
  3622. }
  3623. else if ( fieldName == jointTargetFrequencyName )
  3624. {
  3625. pField->getFieldValue( frequency );
  3626. }
  3627. else if ( fieldName == jointTargetDampingRatioName )
  3628. {
  3629. pField->getFieldValue( dampingRatio );
  3630. }
  3631. }
  3632. // Create joint.
  3633. createTargetJoint( pSceneObject, worldTarget, maxForce, frequency, dampingRatio, collideConnected );
  3634. }
  3635. // Is this a motor joint?
  3636. else if ( nodeName == jointMotorNodeName )
  3637. {
  3638. // Fetch joint children.
  3639. const TamlCustomNodeVector& jointChildren = pJointNode->getChildren();
  3640. // Fetch joint objects.
  3641. SceneObject* pSceneObjectA = jointChildren.size() > 0 ? jointChildren[0]->getProxyObject<SceneObject>(true) : NULL;
  3642. SceneObject* pSceneObjectB = jointChildren.size() == 2 ? jointChildren[1]->getProxyObject<SceneObject>(true) : NULL;
  3643. // Did we get any connected objects?
  3644. if( pSceneObjectA == NULL && pSceneObjectB == NULL )
  3645. {
  3646. // No, so warn.
  3647. Con::warnf( "Scene::onTamlPostRead() - Encountered a joint '%s' but it has invalid connected objects.", nodeName );
  3648. continue;
  3649. }
  3650. bool collideConnected = false;
  3651. b2Vec2 linearOffset = b2Vec2_zero;
  3652. F32 angularOffset = 0.0f;
  3653. F32 maxForce = 1.0f;
  3654. F32 maxTorque = 1.0f;
  3655. F32 correctionFactor = 0.3f;
  3656. // Fetch joint fields.
  3657. const TamlCustomFieldVector& jointFields = pJointNode->getFields();
  3658. // Iterate property fields.
  3659. for ( TamlCustomFieldVector::const_iterator jointFieldItr = jointFields.begin(); jointFieldItr != jointFields.end(); ++jointFieldItr )
  3660. {
  3661. // Fetch field node.
  3662. TamlCustomField* pField = *jointFieldItr;
  3663. // Fetch property field name.
  3664. StringTableEntry fieldName = pField->getFieldName();
  3665. // Fetch fields.
  3666. if ( fieldName == jointCollideConnectedName )
  3667. {
  3668. pField->getFieldValue( collideConnected );
  3669. }
  3670. else if ( fieldName == jointMotorLinearOffsetName )
  3671. {
  3672. pField->getFieldValue( linearOffset );
  3673. }
  3674. else if ( fieldName == jointMotorAngularOffsetName )
  3675. {
  3676. pField->getFieldValue( angularOffset );
  3677. angularOffset = mDegToRad( angularOffset );
  3678. }
  3679. else if ( fieldName == jointMotorMaxForceName )
  3680. {
  3681. pField->getFieldValue( maxForce );
  3682. }
  3683. else if ( fieldName == jointMotorMaxTorqueName )
  3684. {
  3685. pField->getFieldValue( maxTorque );
  3686. }
  3687. else if ( fieldName == jointMotorCorrectionFactorName )
  3688. {
  3689. pField->getFieldValue( correctionFactor );
  3690. }
  3691. }
  3692. // Create joint.
  3693. createMotorJoint( pSceneObjectA, pSceneObjectB, linearOffset, angularOffset, maxForce, maxTorque, correctionFactor, collideConnected );
  3694. }
  3695. // Unknown joint type!
  3696. else
  3697. {
  3698. // Warn.
  3699. Con::warnf( "Unknown joint type of '%s' encountered.", nodeName );
  3700. // Sanity!
  3701. AssertFatal( false, "Scene::onTamlCustomRead() - Unknown joint type detected." );
  3702. continue;
  3703. }
  3704. }
  3705. }
  3706. // Find controller custom node.
  3707. const TamlCustomNode* pControllerCustomNode = customNodes.findNode( controllerCustomNodeName );
  3708. // Do we have any controllers?
  3709. if ( pControllerCustomNode != NULL )
  3710. {
  3711. // Yes, so fetch the scene controllers.
  3712. SimSet* pControllerSet = getControllers();
  3713. // Fetch children controller nodes.
  3714. const TamlCustomNodeVector& controllerChildren = pControllerCustomNode->getChildren();
  3715. // Iterate controllers.
  3716. for( TamlCustomNodeVector::const_iterator controllerNodeItr = controllerChildren.begin(); controllerNodeItr != controllerChildren.end(); ++controllerNodeItr )
  3717. {
  3718. // Fetch controller node.
  3719. TamlCustomNode* pControllerNode = *controllerNodeItr;
  3720. // Is the node a proxy object?
  3721. if ( !pControllerNode->isProxyObject() )
  3722. {
  3723. // No, so warn.
  3724. Con::warnf("Scene::onTamlPostRead() - Reading scene controllers but node '%s'is not an object.", pControllerNode->getNodeName() );
  3725. continue;
  3726. }
  3727. // Add the proxy object.
  3728. SimObject* pProxyObject = pControllerNode->getProxyObject<SimObject>(false);
  3729. // Is it a scene controller?
  3730. if ( dynamic_cast<SceneController*>( pProxyObject ) == NULL )
  3731. {
  3732. // No, so warn.
  3733. Con::warnf("Scene::onTamlPostRead() - Reading scene controllers but node '%s'is not a scene controller.", pControllerNode->getNodeName() );
  3734. // Delete the object.
  3735. pProxyObject->deleteObject();
  3736. continue;
  3737. }
  3738. // Add to scene controllers.
  3739. pControllerSet->addObject( pProxyObject );
  3740. }
  3741. }
  3742. // Find asset preload custom node.
  3743. const TamlCustomNode* pAssetPreloadNode = customNodes.findNode( assetPreloadNodeName );
  3744. // Do we have any asset preloads?
  3745. if ( pAssetPreloadNode != NULL )
  3746. {
  3747. // Yes, so clear any existing asset preloads.
  3748. clearAssetPreloads();
  3749. // Yes, so fetch asset Id type prefix.
  3750. StringTableEntry assetIdTypePrefix = ConsoleBaseType::getType( TypeAssetId )->getTypePrefix();
  3751. // Fetch the prefix length.
  3752. const S32 assetIdPrefixLength = dStrlen( assetIdTypePrefix );
  3753. // Fetch the preload children nodes.
  3754. const TamlCustomNodeVector& preloadChildren = pAssetPreloadNode->getChildren();
  3755. // Iterate asset preloads.
  3756. for( TamlCustomNodeVector::const_iterator assetPreloadNodeItr = preloadChildren.begin(); assetPreloadNodeItr != preloadChildren.end(); ++assetPreloadNodeItr )
  3757. {
  3758. // Fetch asset node.
  3759. const TamlCustomNode* pAssetNode = *assetPreloadNodeItr;
  3760. // Ignore non-asset nodes.
  3761. if ( pAssetNode->getNodeName() != assetNodeName )
  3762. continue;
  3763. // Find the asset Id field.
  3764. const TamlCustomField* pAssetIdField = pAssetNode->findField( "Id" );
  3765. // Did we find the field?
  3766. if ( pAssetIdField == NULL )
  3767. {
  3768. // No, so warn.
  3769. Con::warnf("Scene::onTamlPostRead() - Found asset preload but failed to find asset Id field." );
  3770. continue;
  3771. }
  3772. // Fetch field value.
  3773. const char* pFieldValue = pAssetIdField->getFieldValue();
  3774. // Calculate the field value start (skip any type prefix).
  3775. const S32 prefixOffset = dStrnicmp( pFieldValue, assetIdTypePrefix, assetIdPrefixLength ) == 0 ? assetIdPrefixLength : 0;
  3776. // Add asset preload.
  3777. addAssetPreload( pFieldValue + prefixOffset );
  3778. }
  3779. }
  3780. }
  3781. //-----------------------------------------------------------------------------
  3782. void Scene::onTamlCustomWrite( TamlCustomNodes& customNodes )
  3783. {
  3784. // Call parent.
  3785. Parent::onTamlCustomWrite( customNodes );
  3786. // Fetch joint count.
  3787. const U32 jointCount = getJointCount();
  3788. // Do we have any joints?
  3789. if ( jointCount > 0 )
  3790. {
  3791. // Yes, so add joint custom node.
  3792. TamlCustomNode* pJointCustomNode = customNodes.addNode( jointCustomNodeName );
  3793. // Iterate joints.
  3794. for( typeJointHash::iterator jointItr = mJoints.begin(); jointItr != mJoints.end(); ++jointItr )
  3795. {
  3796. // Fetch base joint.
  3797. b2Joint* pBaseJoint = jointItr->value;
  3798. // Add joint node.
  3799. // NOTE: The name of the node will get updated shortly.
  3800. TamlCustomNode* pJointNode = pJointCustomNode->addNode( StringTable->EmptyString );
  3801. // Fetch common details.
  3802. b2Body* pBodyA = pBaseJoint->GetBodyA();
  3803. b2Body* pBodyB = pBaseJoint->GetBodyB();
  3804. // Fetch physics proxies.
  3805. PhysicsProxy* pPhysicsProxyA = static_cast<PhysicsProxy*>(pBodyA->GetUserData());
  3806. PhysicsProxy* pPhysicsProxyB = static_cast<PhysicsProxy*>(pBodyB->GetUserData());
  3807. // Fetch physics proxy type.
  3808. PhysicsProxy::ePhysicsProxyType proxyTypeA = static_cast<PhysicsProxy*>(pBodyA->GetUserData())->getPhysicsProxyType();
  3809. PhysicsProxy::ePhysicsProxyType proxyTypeB = static_cast<PhysicsProxy*>(pBodyB->GetUserData())->getPhysicsProxyType();
  3810. // Fetch scene objects.
  3811. SceneObject* pSceneObjectA = proxyTypeA == PhysicsProxy::PHYSIC_PROXY_SCENEOBJECT ? static_cast<SceneObject*>(pPhysicsProxyA) : NULL;
  3812. SceneObject* pSceneObjectB = proxyTypeB == PhysicsProxy::PHYSIC_PROXY_SCENEOBJECT ? static_cast<SceneObject*>(pPhysicsProxyB) : NULL;
  3813. // Populate joint appropriately.
  3814. switch( pBaseJoint->GetType() )
  3815. {
  3816. case e_distanceJoint:
  3817. {
  3818. // Set joint name.
  3819. pJointNode->setNodeName( jointDistanceNodeName );
  3820. // Fetch joint.
  3821. const b2DistanceJoint* pJoint = dynamic_cast<const b2DistanceJoint*>( pBaseJoint );
  3822. // Sanity!
  3823. AssertFatal( pJoint != NULL, "Scene::onTamlCustomWrite() - Invalid distance joint type returned." );
  3824. // Add length.
  3825. pJointNode->addField( jointDistanceLengthName, pJoint->GetLength() );
  3826. // Add frequency.
  3827. if ( mNotZero( pJoint->GetFrequency() ) )
  3828. pJointNode->addField( jointDistanceFrequencyName, pJoint->GetFrequency() );
  3829. // Add damping ratio.
  3830. if ( mNotZero( pJoint->GetDampingRatio() ) )
  3831. pJointNode->addField( jointDistanceDampingRatioName, pJoint->GetDampingRatio() );
  3832. // Add local anchors.
  3833. if ( mNotZero( pJoint->GetLocalAnchorA().LengthSquared() ) )
  3834. pJointNode->addField( jointLocalAnchorAName, pJoint->GetLocalAnchorA() );
  3835. if ( mNotZero( pJoint->GetLocalAnchorB().LengthSquared() ) )
  3836. pJointNode->addField( jointLocalAnchorBName, pJoint->GetLocalAnchorB() );
  3837. // Add scene object bodies.
  3838. if ( pSceneObjectA != NULL )
  3839. pJointNode->addNode( pSceneObjectA );
  3840. if ( pSceneObjectB != NULL )
  3841. pJointNode->addNode( pSceneObjectB );
  3842. }
  3843. break;
  3844. case e_ropeJoint:
  3845. {
  3846. // Set joint name.
  3847. pJointNode->setNodeName( jointRopeNodeName );
  3848. // Fetch joint.
  3849. const b2RopeJoint* pJoint = dynamic_cast<const b2RopeJoint*>( pBaseJoint );
  3850. // Sanity!
  3851. AssertFatal( pJoint != NULL, "Scene::onTamlCustomWrite() - Invalid rope joint type returned." );
  3852. // Add max length.
  3853. if ( mNotZero( pJoint->GetMaxLength() ) )
  3854. pJointNode->addField( jointRopeMaxLengthName, pJoint->GetMaxLength() );
  3855. // Add local anchors.
  3856. if ( mNotZero( pJoint->GetLocalAnchorA().LengthSquared() ) )
  3857. pJointNode->addField( jointLocalAnchorAName, pJoint->GetLocalAnchorA() );
  3858. if ( mNotZero( pJoint->GetLocalAnchorB().LengthSquared() ) )
  3859. pJointNode->addField( jointLocalAnchorBName, pJoint->GetLocalAnchorB() );
  3860. // Add scene object bodies.
  3861. if ( pSceneObjectA != NULL )
  3862. pJointNode->addNode( pSceneObjectA );
  3863. if ( pSceneObjectB != NULL )
  3864. pJointNode->addNode( pSceneObjectB );
  3865. }
  3866. break;
  3867. case e_revoluteJoint:
  3868. {
  3869. // Set join name.
  3870. pJointNode->setNodeName( jointRevoluteNodeName );
  3871. // Fetch joint.
  3872. const b2RevoluteJoint* pJoint = dynamic_cast<const b2RevoluteJoint*>( pBaseJoint );
  3873. // Sanity!
  3874. AssertFatal( pJoint != NULL, "Scene::onTamlCustomWrite() - Invalid revolute joint type returned." );
  3875. // Add limit.
  3876. if ( pJoint->IsLimitEnabled() )
  3877. {
  3878. // Add limits.
  3879. pJointNode->addField( jointRevoluteLimitLowerAngleName, mRadToDeg(pJoint->GetLowerLimit()) );
  3880. pJointNode->addField( jointRevoluteLimitUpperAngleName, mRadToDeg(pJoint->GetUpperLimit()) );
  3881. }
  3882. // Add motor.
  3883. if ( pJoint->IsMotorEnabled() )
  3884. {
  3885. // Add motor.
  3886. pJointNode->addField( jointRevoluteMotorSpeedName, mRadToDeg(pJoint->GetMotorSpeed()) );
  3887. pJointNode->addField( jointRevoluteMotorMaxTorqueName, pJoint->GetMaxMotorTorque() );
  3888. }
  3889. // Add local anchors.
  3890. if ( mNotZero( pJoint->GetLocalAnchorA().LengthSquared() ) )
  3891. pJointNode->addField( jointLocalAnchorAName, pJoint->GetLocalAnchorA() );
  3892. if ( mNotZero( pJoint->GetLocalAnchorB().LengthSquared() ) )
  3893. pJointNode->addField( jointLocalAnchorBName, pJoint->GetLocalAnchorB() );
  3894. // Add scene object bodies.
  3895. if ( pSceneObjectA != NULL )
  3896. pJointNode->addNode( pSceneObjectA );
  3897. if ( pSceneObjectB != NULL )
  3898. pJointNode->addNode( pSceneObjectB );
  3899. }
  3900. break;
  3901. case e_weldJoint:
  3902. {
  3903. // Set joint name.
  3904. pJointNode->setNodeName( jointWeldNodeName );
  3905. // Fetch joint.
  3906. const b2WeldJoint* pJoint = dynamic_cast<const b2WeldJoint*>( pBaseJoint );
  3907. // Sanity!
  3908. AssertFatal( pJoint != NULL, "Scene::onTamlCustomWrite() - Invalid weld joint type returned." );
  3909. // Add frequency.
  3910. if ( mNotZero( pJoint->GetFrequency() ) )
  3911. pJointNode->addField( jointWeldFrequencyName, pJoint->GetFrequency() );
  3912. // Add damping ratio.
  3913. if ( mNotZero( pJoint->GetDampingRatio() ) )
  3914. pJointNode->addField( jointWeldDampingRatioName, pJoint->GetDampingRatio() );
  3915. // Add local anchors.
  3916. if ( mNotZero( pJoint->GetLocalAnchorA().LengthSquared() ) )
  3917. pJointNode->addField( jointLocalAnchorAName, pJoint->GetLocalAnchorA() );
  3918. if ( mNotZero( pJoint->GetLocalAnchorB().LengthSquared() ) )
  3919. pJointNode->addField( jointLocalAnchorBName, pJoint->GetLocalAnchorB() );
  3920. // Add scene object bodies.
  3921. if ( pSceneObjectA != NULL )
  3922. pJointNode->addNode( pSceneObjectA );
  3923. if ( pSceneObjectB != NULL )
  3924. pJointNode->addNode( pSceneObjectB );
  3925. }
  3926. break;
  3927. case e_wheelJoint:
  3928. {
  3929. // Set joint name.
  3930. pJointNode->setNodeName( jointWheelNodeName );
  3931. // Fetch joint.
  3932. b2WheelJoint* pJoint = dynamic_cast<b2WheelJoint*>( pBaseJoint );
  3933. // Sanity!
  3934. AssertFatal( pJoint != NULL, "Scene::onTamlCustomWrite() - Invalid wheel joint type returned." );
  3935. // Add motor.
  3936. if ( pJoint->IsMotorEnabled() )
  3937. {
  3938. // Add motor.
  3939. pJointNode->addField( jointWheelMotorSpeedName, mRadToDeg(pJoint->GetMotorSpeed()) );
  3940. pJointNode->addField( jointWheelMotorMaxTorqueName, pJoint->GetMaxMotorTorque() );
  3941. }
  3942. // Add frequency.
  3943. if ( mNotZero( pJoint->GetSpringFrequencyHz() ) )
  3944. pJointNode->addField( jointWheelFrequencyName, pJoint->GetSpringFrequencyHz() );
  3945. // Add damping ratio.
  3946. if ( mNotZero( pJoint->GetSpringDampingRatio() ) )
  3947. pJointNode->addField( jointWheelDampingRatioName, pJoint->GetSpringDampingRatio() );
  3948. // Add world axis.
  3949. pJointNode->addField( jointWheelWorldAxisName, pJoint->GetBodyA()->GetWorldVector( pJoint->GetLocalAxisA() ) );
  3950. // Add local anchors.
  3951. pJointNode->addField( jointLocalAnchorAName, pJoint->GetLocalAnchorA() );
  3952. pJointNode->addField( jointLocalAnchorBName, pJoint->GetLocalAnchorB() );
  3953. // Add scene object bodies.
  3954. if ( pSceneObjectA != NULL )
  3955. pJointNode->addNode( pSceneObjectA );
  3956. if ( pSceneObjectB != NULL )
  3957. pJointNode->addNode( pSceneObjectB );
  3958. }
  3959. break;
  3960. case e_frictionJoint:
  3961. {
  3962. // Set joint name.
  3963. pJointNode->setNodeName( jointFrictionNodeName );
  3964. // Fetch joint.
  3965. const b2FrictionJoint* pJoint = dynamic_cast<const b2FrictionJoint*>( pBaseJoint );
  3966. // Add max force.
  3967. if ( mNotZero( pJoint->GetMaxForce() ) )
  3968. pJointNode->addField( jointFrictionMaxForceName, pJoint->GetMaxForce() );
  3969. // Add max torque.
  3970. if ( mNotZero( pJoint->GetMaxTorque() ) )
  3971. pJointNode->addField( jointFrictionMaxTorqueName, pJoint->GetMaxTorque() );
  3972. // Sanity!
  3973. AssertFatal( pJoint != NULL, "Scene::onTamlCustomWrite() - Invalid friction joint type returned." );
  3974. // Add local anchors.
  3975. if ( mNotZero( pJoint->GetLocalAnchorA().LengthSquared() ) )
  3976. pJointNode->addField( jointLocalAnchorAName, pJoint->GetLocalAnchorA() );
  3977. if ( mNotZero( pJoint->GetLocalAnchorB().LengthSquared() ) )
  3978. pJointNode->addField( jointLocalAnchorBName, pJoint->GetLocalAnchorB() );
  3979. // Add scene object bodies.
  3980. if ( pSceneObjectA != NULL )
  3981. pJointNode->addNode( pSceneObjectA );
  3982. if ( pSceneObjectB != NULL )
  3983. pJointNode->addNode( pSceneObjectB );
  3984. }
  3985. break;
  3986. case e_prismaticJoint:
  3987. {
  3988. // Set joint name.
  3989. pJointNode->setNodeName( jointPrismaticNodeName );
  3990. // Fetch joint.
  3991. b2PrismaticJoint* pJoint = dynamic_cast<b2PrismaticJoint*>( pBaseJoint );
  3992. // Sanity!
  3993. AssertFatal( pJoint != NULL, "Scene::onTamlCustomWrite() - Invalid prismatic joint type returned." );
  3994. // Add limit.
  3995. if ( pJoint->IsLimitEnabled() )
  3996. {
  3997. // Add limits.
  3998. pJointNode->addField( jointPrismaticLimitLowerTransName, pJoint->GetLowerLimit() );
  3999. pJointNode->addField( jointPrismaticLimitUpperTransName, pJoint->GetUpperLimit() );
  4000. }
  4001. // Add motor.
  4002. if ( pJoint->IsMotorEnabled() )
  4003. {
  4004. // Add motor.
  4005. pJointNode->addField( jointPrismaticMotorSpeedName, mRadToDeg(pJoint->GetMotorSpeed()) );
  4006. pJointNode->addField( jointPrismaticMotorMaxForceName, pJoint->GetMaxMotorForce() );
  4007. }
  4008. // Add world axis.
  4009. pJointNode->addField( jointPrismaticWorldAxisName, pJoint->GetBodyA()->GetWorldVector( pJoint->GetLocalAxisA() ) );
  4010. // Add local anchors.
  4011. pJointNode->addField( jointLocalAnchorAName, pJoint->GetLocalAnchorA() );
  4012. pJointNode->addField( jointLocalAnchorBName, pJoint->GetLocalAnchorB() );
  4013. // Add scene object bodies.
  4014. if ( pSceneObjectA != NULL )
  4015. pJointNode->addNode( pSceneObjectA );
  4016. if ( pSceneObjectB != NULL )
  4017. pJointNode->addNode( pSceneObjectB );
  4018. }
  4019. break;
  4020. case e_pulleyJoint:
  4021. {
  4022. // Set joint name.
  4023. pJointNode->setNodeName( jointPulleyNodeName );
  4024. // Fetch joint.
  4025. b2PulleyJoint* pJoint = dynamic_cast<b2PulleyJoint*>( pBaseJoint );
  4026. // Sanity!
  4027. AssertFatal( pJoint != NULL, "Scene::onTamlCustomWrite() - Invalid pulley joint type returned." );
  4028. // Add lengths.
  4029. pJointNode->addField( jointPulleyLengthAName, pJoint->GetLengthA() );
  4030. pJointNode->addField( jointPulleyLengthBName, pJoint->GetLengthB() );
  4031. // Add ratio,
  4032. pJointNode->addField( jointPulleyRatioName, pJoint->GetRatio() );
  4033. // Add ground anchors.
  4034. pJointNode->addField( jointPulleyGroundAnchorAName, pJoint->GetGroundAnchorA() );
  4035. pJointNode->addField( jointPulleyGroundAnchorBName, pJoint->GetGroundAnchorB() );
  4036. // Add local anchors.
  4037. pJointNode->addField( jointLocalAnchorAName, pJoint->GetBodyA()->GetLocalPoint( pJoint->GetAnchorA() ) );
  4038. pJointNode->addField( jointLocalAnchorBName, pJoint->GetBodyB()->GetLocalPoint( pJoint->GetAnchorB() ) );
  4039. // Add scene object bodies.
  4040. if ( pSceneObjectA != NULL )
  4041. pJointNode->addNode( pSceneObjectA );
  4042. if ( pSceneObjectB != NULL )
  4043. pJointNode->addNode( pSceneObjectB );
  4044. }
  4045. break;
  4046. case e_mouseJoint:
  4047. {
  4048. // Set joint name.
  4049. pJointNode->setNodeName( jointTargetNodeName );
  4050. // Fetch joint.
  4051. const b2MouseJoint* pJoint = dynamic_cast<const b2MouseJoint*>( pBaseJoint );
  4052. // Sanity!
  4053. AssertFatal( pJoint != NULL, "Scene::onTamlCustomWrite() - Invalid target joint type returned." );
  4054. // Add target.
  4055. pJointNode->addField( jointTargetWorldTargetName, pJoint->GetTarget() );
  4056. // Add max force.
  4057. pJointNode->addField( jointTargetMaxForceName, pJoint->GetMaxForce() );
  4058. // Add frequency
  4059. pJointNode->addField( jointTargetFrequencyName, pJoint->GetFrequency() );
  4060. // Add damping ratio.
  4061. pJointNode->addField( jointTargetDampingRatioName, pJoint->GetDampingRatio() );
  4062. // Add body.
  4063. // NOTE: This joint uses BODYB as the object, BODYA is the ground-body however for easy of use
  4064. // we'll refer to this as OBJECTA in the persisted format.
  4065. if ( pSceneObjectB != NULL )
  4066. pJointNode->addNode( pSceneObjectB );
  4067. }
  4068. break;
  4069. case e_motorJoint:
  4070. {
  4071. // Set joint name.
  4072. pJointNode->setNodeName( jointMotorNodeName );
  4073. // Fetch joint.
  4074. const b2MotorJoint* pJoint = dynamic_cast<const b2MotorJoint*>( pBaseJoint );
  4075. // Sanity!
  4076. AssertFatal( pJoint != NULL, "Scene::onTamlCustomWrite() - Invalid motor joint type returned." );
  4077. // Add linear offset.
  4078. if ( mNotZero( pJoint->GetLinearOffset().LengthSquared() ) )
  4079. pJointNode->addField( jointMotorLinearOffsetName, pJoint->GetLinearOffset() );
  4080. // Add angular offset.
  4081. if ( mNotZero( pJoint->GetAngularOffset() ) )
  4082. pJointNode->addField( jointMotorAngularOffsetName, mRadToDeg( pJoint->GetAngularOffset() ) );
  4083. // Add max force.
  4084. pJointNode->addField( jointMotorMaxForceName, pJoint->GetMaxForce() );
  4085. // Add max torque.
  4086. pJointNode->addField( jointMotorMaxTorqueName, pJoint->GetMaxTorque() );
  4087. // Add correction factor.
  4088. pJointNode->addField( jointMotorCorrectionFactorName, pJoint->GetCorrectionFactor() );
  4089. // Add scene object bodies.
  4090. if ( pSceneObjectA != NULL )
  4091. pJointNode->addNode( pSceneObjectA );
  4092. if ( pSceneObjectB != NULL )
  4093. pJointNode->addNode( pSceneObjectB );
  4094. }
  4095. break;
  4096. default:
  4097. // Sanity!
  4098. AssertFatal( false, "Scene::onTamlCustomWrite() - Unknown joint type detected." );
  4099. }
  4100. // Add collide connected flag.
  4101. if ( pBaseJoint->GetCollideConnected() )
  4102. pJointNode->addField( jointCollideConnectedName, pBaseJoint->GetCollideConnected() );
  4103. }
  4104. }
  4105. // Fetch controller count.
  4106. const S32 sceneControllerCount = getControllers() ? getControllers()->size() : 0;
  4107. // Do we have any scene controllers?
  4108. if ( sceneControllerCount > 0 )
  4109. {
  4110. // Yes, so add controller node.
  4111. TamlCustomNode* pControllerCustomNode = customNodes.addNode( controllerCustomNodeName );
  4112. // Fetch the scene controllers.
  4113. SimSet* pControllerSet = getControllers();
  4114. // Iterate scene controllers.
  4115. for( S32 i = 0; i < sceneControllerCount; i++ )
  4116. {
  4117. // Fetch the set object.
  4118. SimObject* pSetObject = pControllerSet->at(i);
  4119. // Skip if not a controller.
  4120. if ( !pSetObject->isType<SceneController*>() )
  4121. continue;
  4122. // Add controller node.
  4123. pControllerCustomNode->addNode( pSetObject );
  4124. }
  4125. }
  4126. // Fetch asset preload count.
  4127. const S32 assetPreloadCount = getAssetPreloadCount();
  4128. // Do we have any asset preloads?
  4129. if ( assetPreloadCount > 0 )
  4130. {
  4131. // Yes, so fetch asset Id type prefix.
  4132. StringTableEntry assetIdTypePrefix = ConsoleBaseType::getType( TypeAssetId )->getTypePrefix();
  4133. // Add asset preload node.
  4134. TamlCustomNode* pAssetPreloadCustomNode = customNodes.addNode( assetPreloadNodeName );
  4135. // Iterate asset preloads.
  4136. for( typeAssetPtrVector::const_iterator assetItr = mAssetPreloads.begin(); assetItr != mAssetPreloads.end(); ++assetItr )
  4137. {
  4138. // Add node.
  4139. TamlCustomNode* pAssetNode = pAssetPreloadCustomNode->addNode( assetNodeName );
  4140. char valueBuffer[1024];
  4141. dSprintf( valueBuffer, sizeof(valueBuffer), "%s%s", assetIdTypePrefix, (*assetItr)->getAssetId() );
  4142. // Add asset Id.
  4143. pAssetNode->addField( "Id", valueBuffer );
  4144. }
  4145. }
  4146. }
  4147. //-----------------------------------------------------------------------------
  4148. void Scene::onTamlCustomRead( const TamlCustomNodes& customNodes )
  4149. {
  4150. // Call parent.
  4151. Parent::onTamlCustomRead( customNodes );
  4152. }
  4153. //-----------------------------------------------------------------------------
  4154. U32 Scene::getGlobalSceneCount( void )
  4155. {
  4156. return sSceneCount;
  4157. }
  4158. //-----------------------------------------------------------------------------
  4159. SceneRenderRequest* Scene::createDefaultRenderRequest( SceneRenderQueue* pSceneRenderQueue, SceneObject* pSceneObject )
  4160. {
  4161. // Create a render request and populate it with the default details.
  4162. SceneRenderRequest* pSceneRenderRequest = pSceneRenderQueue->createRenderRequest()->set(
  4163. pSceneObject,
  4164. pSceneObject->getRenderPosition(),
  4165. pSceneObject->getSceneLayerDepth(),
  4166. pSceneObject->getSortPoint(),
  4167. pSceneObject->getSerialId(),
  4168. pSceneObject->getRenderGroup() );
  4169. // Prepare the blending for it.
  4170. pSceneRenderRequest->mBlendMode = pSceneObject->getBlendMode();
  4171. pSceneRenderRequest->mBlendColor = pSceneObject->getBlendColor();
  4172. pSceneRenderRequest->mSrcBlendFactor = pSceneObject->getSrcBlendFactor();
  4173. pSceneRenderRequest->mDstBlendFactor = pSceneObject->getDstBlendFactor();
  4174. pSceneRenderRequest->mAlphaTest = pSceneObject->getAlphaTest();
  4175. return pSceneRenderRequest;
  4176. }
  4177. //-----------------------------------------------------------------------------
  4178. SimObject* Scene::getTamlChild( const U32 childIndex ) const
  4179. {
  4180. // Sanity!
  4181. AssertFatal( childIndex < (U32)mSceneObjects.size(), "Scene::getTamlChild() - Child index is out of range." );
  4182. // For when the assert is not used.
  4183. if ( childIndex >= (U32)mSceneObjects.size() )
  4184. return NULL;
  4185. return mSceneObjects[ childIndex ];
  4186. }
  4187. //-----------------------------------------------------------------------------
  4188. void Scene::addTamlChild( SimObject* pSimObject )
  4189. {
  4190. // Sanity!
  4191. AssertFatal( pSimObject != NULL, "Scene::addTamlChild() - Cannot add a NULL child object." );
  4192. // Fetch as a scene object.
  4193. SceneObject* pSceneObject = dynamic_cast<SceneObject*>( pSimObject );
  4194. // Is it a scene object?
  4195. if ( pSceneObject == NULL )
  4196. {
  4197. // No, so warn.
  4198. Con::warnf( "Scene::addTamlChild() - Cannot add a child object that isn't a scene object." );
  4199. return;
  4200. }
  4201. // Add the scene object.
  4202. addToScene( pSceneObject );
  4203. }
  4204. //-----------------------------------------------------------------------------
  4205. static EnumTable::Enums DebugOptionsLookup[] =
  4206. {
  4207. { Scene::SCENE_DEBUG_METRICS, "metrics" },
  4208. { Scene::SCENE_DEBUG_FPS_METRICS, "fps" },
  4209. { Scene::SCENE_DEBUG_CONTROLLERS, "controllers" },
  4210. { Scene::SCENE_DEBUG_JOINTS, "joints" },
  4211. { Scene::SCENE_DEBUG_WIREFRAME_RENDER, "wireframe" },
  4212. { Scene::SCENE_DEBUG_AUDIO_SOURCES, "audio"},
  4213. ///
  4214. { Scene::SCENE_DEBUG_AABB, "aabb" },
  4215. { Scene::SCENE_DEBUG_OOBB, "oobb" },
  4216. { Scene::SCENE_DEBUG_SLEEP, "sleep" },
  4217. { Scene::SCENE_DEBUG_COLLISION_SHAPES, "collision" },
  4218. { Scene::SCENE_DEBUG_POSITION_AND_COM, "position" },
  4219. { Scene::SCENE_DEBUG_SORT_POINTS, "sort" },
  4220. };
  4221. //-----------------------------------------------------------------------------
  4222. Scene::DebugOption Scene::getDebugOptionEnum(const char* label)
  4223. {
  4224. // Search for Mnemonic.
  4225. for(U32 i = 0; i < (sizeof(DebugOptionsLookup) / sizeof(EnumTable::Enums)); i++)
  4226. if( dStricmp(DebugOptionsLookup[i].label, label) == 0)
  4227. return((Scene::DebugOption)DebugOptionsLookup[i].index);
  4228. // Warn.
  4229. Con::warnf( "Scene::getDebugOptionEnum() - Invalid debug option '%s'.", label );
  4230. return Scene::SCENE_DEBUG_INVALID;
  4231. }
  4232. //-----------------------------------------------------------------------------
  4233. const char* Scene::getDebugOptionDescription( Scene::DebugOption debugOption )
  4234. {
  4235. // Search for Mnemonic.
  4236. for (U32 i = 0; i < (sizeof(DebugOptionsLookup) / sizeof(EnumTable::Enums)); i++)
  4237. {
  4238. if( DebugOptionsLookup[i].index == debugOption )
  4239. return DebugOptionsLookup[i].label;
  4240. }
  4241. // Warn.
  4242. Con::warnf( "Scene::getDebugOptionDescription() - Invalid debug option." );
  4243. return StringTable->EmptyString;
  4244. }
  4245. //-----------------------------------------------------------------------------
  4246. static EnumTable::Enums jointTypeLookup[] =
  4247. {
  4248. { e_distanceJoint, "distance" },
  4249. { e_ropeJoint, "rope" },
  4250. { e_revoluteJoint, "revolute" },
  4251. { e_weldJoint, "weld" },
  4252. { e_wheelJoint, "wheel" },
  4253. { e_frictionJoint, "friction" },
  4254. { e_prismaticJoint, "prismatic" },
  4255. { e_pulleyJoint, "pulley" },
  4256. { e_mouseJoint, "target" },
  4257. { e_motorJoint, "motor" },
  4258. };
  4259. EnumTable jointTypeTable(sizeof(jointTypeLookup) / sizeof(EnumTable::Enums), &jointTypeLookup[0]);
  4260. //-----------------------------------------------------------------------------
  4261. const char* Scene::getJointTypeDescription( b2JointType jointType )
  4262. {
  4263. // Search for Mnemonic.
  4264. for (U32 i = 0; i < (sizeof(jointTypeLookup) / sizeof(EnumTable::Enums)); i++)
  4265. {
  4266. if( jointTypeLookup[i].index == jointType )
  4267. return jointTypeLookup[i].label;
  4268. }
  4269. // Warn.
  4270. Con::warnf( "Scene::getJointTypeDescription() - Invalid joint type." );
  4271. return StringTable->EmptyString;
  4272. }
  4273. //-----------------------------------------------------------------------------
  4274. b2JointType Scene::getJointTypeEnum(const char* label)
  4275. {
  4276. // Search for Mnemonic.
  4277. for (U32 i = 0; i < (sizeof(jointTypeLookup) / sizeof(EnumTable::Enums)); i++)
  4278. {
  4279. if( dStricmp(jointTypeLookup[i].label, label) == 0)
  4280. return (b2JointType)jointTypeLookup[i].index;
  4281. }
  4282. // Warn.
  4283. Con::warnf( "Scene::getJointTypeEnum() - Invalid joint of '%s'", label );
  4284. return e_unknownJoint;
  4285. }
  4286. //-----------------------------------------------------------------------------
  4287. static EnumTable::Enums pickModeLookup[] =
  4288. {
  4289. { Scene::PICK_ANY, "Any" },
  4290. { Scene::PICK_AABB, "AABB" },
  4291. { Scene::PICK_OOBB, "OOBB" },
  4292. { Scene::PICK_COLLISION, "Collision" },
  4293. };
  4294. //-----------------------------------------------------------------------------
  4295. Scene::PickMode Scene::getPickModeEnum(const char* label)
  4296. {
  4297. // Search for Mnemonic.
  4298. for(U32 i = 0; i < (sizeof(pickModeLookup) / sizeof(EnumTable::Enums)); i++)
  4299. if( dStricmp(pickModeLookup[i].label, label) == 0)
  4300. return((Scene::PickMode)pickModeLookup[i].index);
  4301. // Warn.
  4302. Con::warnf( "Scene::getPickModeEnum() - Invalid pick mode '%s'.", label );
  4303. return Scene::PICK_INVALID;
  4304. }
  4305. //-----------------------------------------------------------------------------
  4306. const char* Scene::getPickModeDescription( Scene::PickMode pickMode )
  4307. {
  4308. // Search for Mnemonic.
  4309. for (U32 i = 0; i < (sizeof(pickModeLookup) / sizeof(EnumTable::Enums)); i++)
  4310. {
  4311. if( pickModeLookup[i].index == pickMode )
  4312. return pickModeLookup[i].label;
  4313. }
  4314. // Warn.
  4315. Con::warnf( "Scene::getPickModeDescription() - Invalid pick mode.");
  4316. return StringTable->EmptyString;
  4317. }
  4318. //-----------------------------------------------------------------------------
  4319. static void WriteJointsCustomTamlScehema( const AbstractClassRep* pClassRep, TiXmlElement* pParentElement )
  4320. {
  4321. // Sanity!
  4322. AssertFatal( pClassRep != NULL, "Taml::WriteJointsCustomTamlScehema() - ClassRep cannot be NULL." );
  4323. AssertFatal( pParentElement != NULL, "Taml::WriteJointsCustomTamlScehema() - Parent Element cannot be NULL." );
  4324. char buffer[1024];
  4325. // Create joints node element.
  4326. TiXmlElement* pJointsNodeElement = new TiXmlElement( "xs:element" );
  4327. dSprintf( buffer, sizeof(buffer), "%s.%s", pClassRep->getClassName(), jointCustomNodeName );
  4328. pJointsNodeElement->SetAttribute( "name", buffer );
  4329. pJointsNodeElement->SetAttribute( "minOccurs", 0 );
  4330. pJointsNodeElement->SetAttribute( "maxOccurs", 1 );
  4331. pParentElement->LinkEndChild( pJointsNodeElement );
  4332. // Create complex type.
  4333. TiXmlElement* pJointsNodeComplexTypeElement = new TiXmlElement( "xs:complexType" );
  4334. pJointsNodeElement->LinkEndChild( pJointsNodeComplexTypeElement );
  4335. // Create choice element.
  4336. TiXmlElement* pJointsNodeChoiceElement = new TiXmlElement( "xs:choice" );
  4337. pJointsNodeChoiceElement->SetAttribute( "minOccurs", 0 );
  4338. pJointsNodeChoiceElement->SetAttribute( "maxOccurs", "unbounded" );
  4339. pJointsNodeComplexTypeElement->LinkEndChild( pJointsNodeChoiceElement );
  4340. // ********************************************************************************
  4341. // Create Distance Joint Element.
  4342. // ********************************************************************************
  4343. TiXmlElement* pDistanceJointElement = new TiXmlElement( "xs:element" );
  4344. pDistanceJointElement->SetAttribute( "name", jointDistanceNodeName );
  4345. pDistanceJointElement->SetAttribute( "minOccurs", 0 );
  4346. pDistanceJointElement->SetAttribute( "maxOccurs", 1 );
  4347. pJointsNodeChoiceElement->LinkEndChild( pDistanceJointElement );
  4348. // Create complex type Element.
  4349. TiXmlElement* pDistanceJointComplexTypeElement = new TiXmlElement( "xs:complexType" );
  4350. pDistanceJointElement->LinkEndChild( pDistanceJointComplexTypeElement );
  4351. // Create "Length" attribute.
  4352. TiXmlElement* pDistanceJointElementA = new TiXmlElement( "xs:attribute" );
  4353. pDistanceJointElementA->SetAttribute( "name", jointDistanceLengthName );
  4354. pDistanceJointComplexTypeElement->LinkEndChild( pDistanceJointElementA );
  4355. TiXmlElement* pDistanceJointElementB = new TiXmlElement( "xs:simpleType" );
  4356. pDistanceJointElementA->LinkEndChild( pDistanceJointElementB );
  4357. TiXmlElement* pDistanceJointElementC = new TiXmlElement( "xs:restriction" );
  4358. pDistanceJointElementC->SetAttribute( "base", "xs:float" );
  4359. pDistanceJointElementB->LinkEndChild( pDistanceJointElementC );
  4360. TiXmlElement* pDistanceJointElementD = new TiXmlElement( "xs:minInclusive" );
  4361. pDistanceJointElementD->SetAttribute( "value", "0" );
  4362. pDistanceJointElementC->LinkEndChild( pDistanceJointElementD );
  4363. // Create "Frequency" attribute.
  4364. pDistanceJointElementA = new TiXmlElement( "xs:attribute" );
  4365. pDistanceJointElementA->SetAttribute( "name", jointDistanceFrequencyName );
  4366. pDistanceJointComplexTypeElement->LinkEndChild( pDistanceJointElementA );
  4367. pDistanceJointElementB = new TiXmlElement( "xs:simpleType" );
  4368. pDistanceJointElementA->LinkEndChild( pDistanceJointElementB );
  4369. pDistanceJointElementC = new TiXmlElement( "xs:restriction" );
  4370. pDistanceJointElementC->SetAttribute( "base", "xs:float" );
  4371. pDistanceJointElementB->LinkEndChild( pDistanceJointElementC );
  4372. pDistanceJointElementD = new TiXmlElement( "xs:minInclusive" );
  4373. pDistanceJointElementD->SetAttribute( "value", "0" );
  4374. pDistanceJointElementC->LinkEndChild( pDistanceJointElementD );
  4375. // Create "Damping Ratio" attribute.
  4376. pDistanceJointElementA = new TiXmlElement( "xs:attribute" );
  4377. pDistanceJointElementA->SetAttribute( "name", jointDistanceDampingRatioName );
  4378. pDistanceJointComplexTypeElement->LinkEndChild( pDistanceJointElementA );
  4379. pDistanceJointElementB = new TiXmlElement( "xs:simpleType" );
  4380. pDistanceJointElementA->LinkEndChild( pDistanceJointElementB );
  4381. pDistanceJointElementC = new TiXmlElement( "xs:restriction" );
  4382. pDistanceJointElementC->SetAttribute( "base", "xs:float" );
  4383. pDistanceJointElementB->LinkEndChild( pDistanceJointElementC );
  4384. pDistanceJointElementD = new TiXmlElement( "xs:minInclusive" );
  4385. pDistanceJointElementD->SetAttribute( "value", "0" );
  4386. pDistanceJointElementC->LinkEndChild( pDistanceJointElementD );
  4387. }
  4388. //-----------------------------------------------------------------------------
  4389. static void WriteControllersCustomTamlScehema( const AbstractClassRep* pClassRep, TiXmlElement* pParentElement )
  4390. {
  4391. // Sanity!
  4392. AssertFatal( pClassRep != NULL, "Taml::WriteControllersCustomTamlScehema() - ClassRep cannot be NULL." );
  4393. AssertFatal( pParentElement != NULL, "Taml::WriteControllersCustomTamlScehema() - Parent Element cannot be NULL." );
  4394. char buffer[1024];
  4395. // Create controllers node element.
  4396. TiXmlElement* pControllersNodeElement = new TiXmlElement( "xs:element" );
  4397. dSprintf( buffer, sizeof(buffer), "%s.%s", pClassRep->getClassName(), controllerCustomNodeName );
  4398. pControllersNodeElement->SetAttribute( "name", buffer );
  4399. pControllersNodeElement->SetAttribute( "minOccurs", 0 );
  4400. pControllersNodeElement->SetAttribute( "maxOccurs", 1 );
  4401. pParentElement->LinkEndChild( pControllersNodeElement );
  4402. // Create complex type.
  4403. TiXmlElement* pControllersNodeComplexTypeElement = new TiXmlElement( "xs:complexType" );
  4404. pControllersNodeElement->LinkEndChild( pControllersNodeComplexTypeElement );
  4405. // Create choice element.
  4406. TiXmlElement* pControllersNodeChoiceElement = new TiXmlElement( "xs:choice" );
  4407. pControllersNodeChoiceElement->SetAttribute( "minOccurs", 0 );
  4408. pControllersNodeChoiceElement->SetAttribute( "maxOccurs", "unbounded" );
  4409. pControllersNodeComplexTypeElement->LinkEndChild( pControllersNodeChoiceElement );
  4410. // Fetch the scene controller type.
  4411. AbstractClassRep* pPickingSceneControllerType = AbstractClassRep::findClassRep( "PickingSceneController" );
  4412. AbstractClassRep* pGroupedSceneControllerType = AbstractClassRep::findClassRep( "GroupedSceneController" );
  4413. // Sanity!
  4414. AssertFatal( pPickingSceneControllerType != NULL, "Scene::WriteControllersCustomTamlScehema() - Cannot find the PickingSceneController type." );
  4415. AssertFatal( pGroupedSceneControllerType != NULL, "Scene::WriteControllersCustomTamlScehema() - Cannot find the GroupedSceneController type." );
  4416. // Add choice members.
  4417. for ( AbstractClassRep* pChoiceType = AbstractClassRep::getClassList(); pChoiceType != NULL; pChoiceType = pChoiceType->getNextClass() )
  4418. {
  4419. // Skip if not derived from either of the scene controllers.
  4420. if ( !pChoiceType->isClass( pPickingSceneControllerType ) && !pChoiceType->isClass( pGroupedSceneControllerType ) )
  4421. continue;
  4422. // Add choice member.
  4423. TiXmlElement* pChoiceMemberElement = new TiXmlElement( "xs:element" );
  4424. pChoiceMemberElement->SetAttribute( "name", pChoiceType->getClassName() );
  4425. dSprintf( buffer, sizeof(buffer), "%s_Type", pChoiceType->getClassName() );
  4426. pChoiceMemberElement->SetAttribute( "type", buffer );
  4427. pControllersNodeChoiceElement->LinkEndChild( pChoiceMemberElement );
  4428. }
  4429. }
  4430. //-----------------------------------------------------------------------------
  4431. static void WritePreloadsCustomTamlScehema( const AbstractClassRep* pClassRep, TiXmlElement* pParentElement )
  4432. {
  4433. // Sanity!
  4434. AssertFatal( pClassRep != NULL, "Taml::WritePreloadsCustomTamlScehema() - ClassRep cannot be NULL." );
  4435. AssertFatal( pParentElement != NULL, "Taml::WritePreloadsCustomTamlScehema() - Parent Element cannot be NULL." );
  4436. char buffer[1024];
  4437. // Create preloads node element.
  4438. TiXmlElement* pPreloadsNodeElement = new TiXmlElement( "xs:element" );
  4439. dSprintf( buffer, sizeof(buffer), "%s.%s", pClassRep->getClassName(), assetPreloadNodeName );
  4440. pPreloadsNodeElement->SetAttribute( "name", buffer );
  4441. pPreloadsNodeElement->SetAttribute( "minOccurs", 0 );
  4442. pPreloadsNodeElement->SetAttribute( "maxOccurs", 1 );
  4443. pParentElement->LinkEndChild( pPreloadsNodeElement );
  4444. // Create complex type.
  4445. TiXmlElement* pPreloadsNodeComplexTypeElement = new TiXmlElement( "xs:complexType" );
  4446. pPreloadsNodeElement->LinkEndChild( pPreloadsNodeComplexTypeElement );
  4447. // Create choice element.
  4448. TiXmlElement* pPreloadsNodeChoiceElement = new TiXmlElement( "xs:choice" );
  4449. pPreloadsNodeChoiceElement->SetAttribute( "minOccurs", 0 );
  4450. pPreloadsNodeChoiceElement->SetAttribute( "maxOccurs", "unbounded" );
  4451. pPreloadsNodeComplexTypeElement->LinkEndChild( pPreloadsNodeChoiceElement );
  4452. // Add choice member element.
  4453. TiXmlElement* pChoiceMemberElement = new TiXmlElement( "xs:element" );
  4454. pChoiceMemberElement->SetAttribute( "name", assetNodeName );
  4455. pPreloadsNodeChoiceElement->LinkEndChild( pChoiceMemberElement );
  4456. // Add choice member complex type element.
  4457. TiXmlElement* pChoiceMemberComplexTypeElement = new TiXmlElement( "xs:complexType" );
  4458. pChoiceMemberElement->LinkEndChild( pChoiceMemberComplexTypeElement );
  4459. // Add choice member attribute element.
  4460. TiXmlElement* pChoiceAttributeElement = new TiXmlElement( "xs:attribute" );
  4461. pChoiceAttributeElement->SetAttribute( "name", "Id" );
  4462. dSprintf( buffer, sizeof(buffer), "%s", "AssetId_ConsoleType" );
  4463. pChoiceAttributeElement->SetAttribute( "type", buffer );
  4464. pChoiceMemberComplexTypeElement->LinkEndChild( pChoiceAttributeElement );
  4465. }
  4466. //-----------------------------------------------------------------------------
  4467. static void WriteCustomTamlSchema( const AbstractClassRep* pClassRep, TiXmlElement* pParentElement )
  4468. {
  4469. // Sanity!
  4470. AssertFatal( pClassRep != NULL, "Taml::WriteCustomTamlSchema() - ClassRep cannot be NULL." );
  4471. AssertFatal( pParentElement != NULL, "Taml::WriteCustomTamlSchema() - Parent Element cannot be NULL." );
  4472. WriteJointsCustomTamlScehema( pClassRep, pParentElement );
  4473. WriteControllersCustomTamlScehema( pClassRep, pParentElement );
  4474. WritePreloadsCustomTamlScehema( pClassRep, pParentElement );
  4475. }
  4476. //------------------------------------------------------------------------------
  4477. IMPLEMENT_CONOBJECT_CHILDREN_SCHEMA(Scene, WriteCustomTamlSchema);