Scene.cc 201 KB

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