Scene.cc 201 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599
  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( (U32)pJoint );
  1272. if ( itr == mReverseJoints.end() )
  1273. {
  1274. Con::warnf("The joint Id could not be found via a joint reference of %x", (U32)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 = mJoints.insert( jointId, pJoint );
  1290. // Sanity!
  1291. AssertFatal( itr != mJoints.end(), "Joint already in hash table." );
  1292. // Insert reverse joint.
  1293. mReverseJoints.insert( (U32)pJoint, jointId );
  1294. return jointId;
  1295. }
  1296. //-----------------------------------------------------------------------------
  1297. bool Scene::deleteJoint( const U32 jointId )
  1298. {
  1299. // Fetch joint.
  1300. b2Joint* pJoint = findJoint( jointId );
  1301. // Finish if no joint.
  1302. if ( pJoint == NULL )
  1303. return false;
  1304. // Destroy joint.
  1305. // This should result in the joint references also being destroyed
  1306. // as the scene is a destruction listener.
  1307. mpWorld->DestroyJoint( pJoint );
  1308. return true;
  1309. }
  1310. //-----------------------------------------------------------------------------
  1311. bool Scene::hasJoints( SceneObject* pSceneObject )
  1312. {
  1313. // Sanity!
  1314. AssertFatal( pSceneObject != NULL, "Scene object cannot be NULL!" );
  1315. AssertFatal( pSceneObject->getScene() != this, "Scene object is not in this scene" );
  1316. // Fetch body.
  1317. b2Body* pBody = pSceneObject->getBody();
  1318. // Fetch joint edge.
  1319. b2JointEdge* pJointEdge = pBody->GetJointList();
  1320. if ( pJointEdge == NULL || pJointEdge->joint == NULL )
  1321. return false;
  1322. // Found at least one joint.
  1323. return true;
  1324. }
  1325. //-----------------------------------------------------------------------------
  1326. S32 Scene::createDistanceJoint(
  1327. const SceneObject* pSceneObjectA, const SceneObject* pSceneObjectB,
  1328. const b2Vec2& localAnchorA, const b2Vec2& localAnchorB,
  1329. const F32 length,
  1330. const F32 frequency,
  1331. const F32 dampingRatio,
  1332. const bool collideConnected )
  1333. {
  1334. // Sanity!
  1335. if ( (pSceneObjectA != NULL && pSceneObjectA->getScene() == NULL) ||
  1336. (pSceneObjectB != NULL && pSceneObjectB->getScene() == NULL) )
  1337. {
  1338. // Warn.
  1339. Con::printf( "Cannot add a joint to a scene object that is not in a scene." );
  1340. return -1;
  1341. }
  1342. // Check for two invalid objects.
  1343. if ( pSceneObjectA == NULL && pSceneObjectB == NULL )
  1344. {
  1345. Con::warnf("Scene::createDistanceJoint() - Cannot create joint without at least a single scene object." );
  1346. return -1;
  1347. }
  1348. // Fetch bodies.
  1349. b2Body* pBodyA = pSceneObjectA != NULL ? pSceneObjectA->getBody() : getGroundBody();
  1350. b2Body* pBodyB = pSceneObjectB != NULL ? pSceneObjectB->getBody() : getGroundBody();
  1351. // Populate definition.
  1352. b2DistanceJointDef jointDef;
  1353. jointDef.userData = static_cast<PhysicsProxy*>(this);
  1354. jointDef.collideConnected = collideConnected;
  1355. jointDef.bodyA = pBodyA;
  1356. jointDef.bodyB = pBodyB;
  1357. jointDef.localAnchorA = localAnchorA;
  1358. jointDef.localAnchorB = localAnchorB;
  1359. jointDef.length = length < 0.0f ? (pBodyB->GetWorldPoint( localAnchorB ) - pBodyA->GetWorldPoint( localAnchorA )).Length() : length;
  1360. jointDef.frequencyHz = frequency;
  1361. jointDef.dampingRatio = dampingRatio;
  1362. // Create joint.
  1363. return createJoint( &jointDef );
  1364. }
  1365. //-----------------------------------------------------------------------------
  1366. void Scene::setDistanceJointLength(
  1367. const U32 jointId,
  1368. const F32 length )
  1369. {
  1370. // Fetch joint.
  1371. b2Joint* pJoint = findJoint( jointId );
  1372. // Ignore invalid joint.
  1373. if ( !pJoint )
  1374. return;
  1375. // Fetch joint type.
  1376. const b2JointType jointType = pJoint->GetType();
  1377. if ( jointType != e_distanceJoint )
  1378. {
  1379. Con::warnf( "Invalid joint type of %s.", getJointTypeDescription(jointType) );
  1380. return;
  1381. }
  1382. // Cast joint.
  1383. b2DistanceJoint* pRealJoint = static_cast<b2DistanceJoint*>( pJoint );
  1384. // Access joint.
  1385. pRealJoint->SetLength( length );
  1386. }
  1387. //-----------------------------------------------------------------------------
  1388. F32 Scene::getDistanceJointLength( const U32 jointId )
  1389. {
  1390. // Fetch joint.
  1391. b2Joint* pJoint = findJoint( jointId );
  1392. // Ignore invalid joint.
  1393. if ( !pJoint )
  1394. return -1.0f;
  1395. // Fetch joint type.
  1396. const b2JointType jointType = pJoint->GetType();
  1397. if ( jointType != e_distanceJoint )
  1398. {
  1399. Con::warnf( "Invalid joint type of %s.", getJointTypeDescription(jointType) );
  1400. return -1.0f;
  1401. }
  1402. // Cast joint.
  1403. b2DistanceJoint* pRealJoint = static_cast<b2DistanceJoint*>( pJoint );
  1404. // Access joint.
  1405. return pRealJoint->GetLength();
  1406. }
  1407. //-----------------------------------------------------------------------------
  1408. void Scene::setDistanceJointFrequency(
  1409. const U32 jointId,
  1410. const F32 frequency )
  1411. {
  1412. // Fetch joint.
  1413. b2Joint* pJoint = findJoint( jointId );
  1414. // Ignore invalid joint.
  1415. if ( !pJoint )
  1416. return;
  1417. // Fetch joint type.
  1418. const b2JointType jointType = pJoint->GetType();
  1419. if ( jointType != e_distanceJoint )
  1420. {
  1421. Con::warnf( "Invalid joint type of %s.", getJointTypeDescription(jointType) );
  1422. return;
  1423. }
  1424. // Cast joint.
  1425. b2DistanceJoint* pRealJoint = static_cast<b2DistanceJoint*>( pJoint );
  1426. // Access joint.
  1427. pRealJoint->SetFrequency( frequency );
  1428. }
  1429. //-----------------------------------------------------------------------------
  1430. F32 Scene::getDistanceJointFrequency( const U32 jointId )
  1431. {
  1432. // Fetch joint.
  1433. b2Joint* pJoint = findJoint( jointId );
  1434. // Ignore invalid joint.
  1435. if ( !pJoint )
  1436. return -1.0f;
  1437. // Fetch joint type.
  1438. const b2JointType jointType = pJoint->GetType();
  1439. if ( jointType != e_distanceJoint )
  1440. {
  1441. Con::warnf( "Invalid joint type of %s.", getJointTypeDescription(jointType) );
  1442. return -1.0f;
  1443. }
  1444. // Cast joint.
  1445. b2DistanceJoint* pRealJoint = static_cast<b2DistanceJoint*>( pJoint );
  1446. // Access joint.
  1447. return pRealJoint->GetFrequency();
  1448. }
  1449. //-----------------------------------------------------------------------------
  1450. void Scene::setDistanceJointDampingRatio(
  1451. const U32 jointId,
  1452. const F32 dampingRatio )
  1453. {
  1454. // Fetch joint.
  1455. b2Joint* pJoint = findJoint( jointId );
  1456. // Ignore invalid joint.
  1457. if ( !pJoint )
  1458. return;
  1459. // Fetch joint type.
  1460. const b2JointType jointType = pJoint->GetType();
  1461. if ( jointType != e_distanceJoint )
  1462. {
  1463. Con::warnf( "Invalid joint type of %s.", getJointTypeDescription(jointType) );
  1464. return;
  1465. }
  1466. // Cast joint.
  1467. b2DistanceJoint* pRealJoint = static_cast<b2DistanceJoint*>( pJoint );
  1468. // Access joint.
  1469. pRealJoint->SetDampingRatio( dampingRatio );
  1470. }
  1471. //-----------------------------------------------------------------------------
  1472. F32 Scene::getDistanceJointDampingRatio( const U32 jointId )
  1473. {
  1474. // Fetch joint.
  1475. b2Joint* pJoint = findJoint( jointId );
  1476. // Ignore invalid joint.
  1477. if ( !pJoint )
  1478. return -1.0f;
  1479. // Fetch joint type.
  1480. const b2JointType jointType = pJoint->GetType();
  1481. if ( jointType != e_distanceJoint )
  1482. {
  1483. Con::warnf( "Invalid joint type of %s.", getJointTypeDescription(jointType) );
  1484. return -1.0f;
  1485. }
  1486. // Cast joint.
  1487. b2DistanceJoint* pRealJoint = static_cast<b2DistanceJoint*>( pJoint );
  1488. // Access joint.
  1489. return pRealJoint->GetDampingRatio();
  1490. }
  1491. //-----------------------------------------------------------------------------
  1492. S32 Scene::createRopeJoint(
  1493. const SceneObject* pSceneObjectA, const SceneObject* pSceneObjectB,
  1494. const b2Vec2& localAnchorA, const b2Vec2& localAnchorB,
  1495. const F32 maxLength,
  1496. const bool collideConnected )
  1497. {
  1498. // Sanity!
  1499. if ( (pSceneObjectA != NULL && pSceneObjectA->getScene() == NULL) ||
  1500. (pSceneObjectB != NULL && pSceneObjectB->getScene() == NULL) )
  1501. {
  1502. // Warn.
  1503. Con::printf( "Cannot add a joint to a scene object that is not in a scene." );
  1504. return -1;
  1505. }
  1506. // Check for two invalid objects.
  1507. if ( pSceneObjectA == NULL && pSceneObjectB == NULL )
  1508. {
  1509. Con::warnf("Scene::createRopeJoint() - Cannot create joint without at least a single scene object." );
  1510. return -1;
  1511. }
  1512. // Fetch bodies.
  1513. b2Body* pBodyA = pSceneObjectA != NULL ? pSceneObjectA->getBody() : getGroundBody();
  1514. b2Body* pBodyB = pSceneObjectB != NULL ? pSceneObjectB->getBody() : getGroundBody();
  1515. // Populate definition.
  1516. b2RopeJointDef jointDef;
  1517. jointDef.userData = static_cast<PhysicsProxy*>(this);
  1518. jointDef.collideConnected = collideConnected;
  1519. jointDef.bodyA = pBodyA;
  1520. jointDef.bodyB = pBodyB;
  1521. jointDef.localAnchorA = localAnchorA;
  1522. jointDef.localAnchorB = localAnchorB;
  1523. jointDef.maxLength = maxLength < 0.0f ? (pBodyB->GetWorldPoint( localAnchorB ) - pBodyA->GetWorldPoint( localAnchorA )).Length() : maxLength;
  1524. // Create joint.
  1525. return createJoint( &jointDef );
  1526. }
  1527. //-----------------------------------------------------------------------------
  1528. void Scene::setRopeJointMaxLength(
  1529. const U32 jointId,
  1530. const F32 maxLength )
  1531. {
  1532. // Fetch joint.
  1533. b2Joint* pJoint = findJoint( jointId );
  1534. // Ignore invalid joint.
  1535. if ( !pJoint )
  1536. return;
  1537. // Fetch joint type.
  1538. const b2JointType jointType = pJoint->GetType();
  1539. if ( jointType != e_ropeJoint )
  1540. {
  1541. Con::warnf( "Invalid joint type of %s.", getJointTypeDescription(jointType) );
  1542. return;
  1543. }
  1544. // Cast joint.
  1545. b2RopeJoint* pRealJoint = static_cast<b2RopeJoint*>( pJoint );
  1546. // Access joint.
  1547. pRealJoint->SetMaxLength( maxLength );
  1548. }
  1549. //-----------------------------------------------------------------------------
  1550. F32 Scene::getRopeJointMaxLength( const U32 jointId )
  1551. {
  1552. // Fetch joint.
  1553. b2Joint* pJoint = findJoint( jointId );
  1554. // Ignore invalid joint.
  1555. if ( !pJoint )
  1556. return -1.0f;
  1557. // Fetch joint type.
  1558. const b2JointType jointType = pJoint->GetType();
  1559. if ( jointType != e_ropeJoint )
  1560. {
  1561. Con::warnf( "Invalid joint type of %s.", getJointTypeDescription(jointType) );
  1562. return -1.0f;
  1563. }
  1564. // Cast joint.
  1565. b2RopeJoint* pRealJoint = static_cast<b2RopeJoint*>( pJoint );
  1566. // Access joint.
  1567. return pRealJoint->GetMaxLength();
  1568. }
  1569. //-----------------------------------------------------------------------------
  1570. S32 Scene::createRevoluteJoint(
  1571. const SceneObject* pSceneObjectA, const SceneObject* pSceneObjectB,
  1572. const b2Vec2& localAnchorA, const b2Vec2& localAnchorB,
  1573. const bool collideConnected )
  1574. {
  1575. // Sanity!
  1576. if ( (pSceneObjectA != NULL && pSceneObjectA->getScene() == NULL) ||
  1577. (pSceneObjectB != NULL && pSceneObjectB->getScene() == NULL) )
  1578. {
  1579. // Warn.
  1580. Con::printf( "Cannot add a joint to a scene object that is not in a scene." );
  1581. return -1;
  1582. }
  1583. // Check for two invalid objects.
  1584. if ( pSceneObjectA == NULL && pSceneObjectB == NULL )
  1585. {
  1586. Con::warnf("Scene::createRevoluteJoint() - Cannot create joint without at least a single scene object." );
  1587. return -1;
  1588. }
  1589. // Fetch bodies.
  1590. b2Body* pBodyA = pSceneObjectA != NULL ? pSceneObjectA->getBody() : getGroundBody();
  1591. b2Body* pBodyB = pSceneObjectB != NULL ? pSceneObjectB->getBody() : getGroundBody();
  1592. // Populate definition.
  1593. b2RevoluteJointDef jointDef;
  1594. jointDef.userData = static_cast<PhysicsProxy*>(this);
  1595. jointDef.collideConnected = collideConnected;
  1596. jointDef.bodyA = pBodyA;
  1597. jointDef.bodyB = pBodyB;
  1598. jointDef.referenceAngle = pBodyB->GetAngle() - pBodyA->GetAngle();
  1599. jointDef.localAnchorA = localAnchorA;
  1600. jointDef.localAnchorB = localAnchorB;
  1601. // Create joint.
  1602. return createJoint( &jointDef );
  1603. }
  1604. //-----------------------------------------------------------------------------
  1605. void Scene::setRevoluteJointLimit(
  1606. const U32 jointId,
  1607. const bool enableLimit,
  1608. const F32 lowerAngle, const F32 upperAngle )
  1609. {
  1610. // Fetch joint.
  1611. b2Joint* pJoint = findJoint( jointId );
  1612. // Ignore invalid joint.
  1613. if ( !pJoint )
  1614. return;
  1615. // Fetch joint type.
  1616. const b2JointType jointType = pJoint->GetType();
  1617. if ( jointType != e_revoluteJoint )
  1618. {
  1619. Con::warnf( "Invalid joint type of %s.", getJointTypeDescription(jointType) );
  1620. return;
  1621. }
  1622. // Cast joint.
  1623. b2RevoluteJoint* pRealJoint = static_cast<b2RevoluteJoint*>( pJoint );
  1624. // Access joint.
  1625. pRealJoint->SetLimits( lowerAngle, upperAngle );
  1626. pRealJoint->EnableLimit( enableLimit );
  1627. }
  1628. //-----------------------------------------------------------------------------
  1629. bool Scene::getRevoluteJointLimit(
  1630. const U32 jointId,
  1631. bool& enableLimit,
  1632. F32& lowerAngle, F32& upperAngle )
  1633. {
  1634. // Fetch joint.
  1635. b2Joint* pJoint = findJoint( jointId );
  1636. // Ignore invalid joint.
  1637. if ( !pJoint )
  1638. return false;
  1639. // Fetch joint type.
  1640. const b2JointType jointType = pJoint->GetType();
  1641. if ( jointType != e_revoluteJoint )
  1642. {
  1643. Con::warnf( "Invalid joint type of %s.", getJointTypeDescription(jointType) );
  1644. return false;
  1645. }
  1646. // Cast joint.
  1647. b2RevoluteJoint* pRealJoint = static_cast<b2RevoluteJoint*>( pJoint );
  1648. // Access joint.
  1649. enableLimit = pRealJoint->IsLimitEnabled();
  1650. lowerAngle = pRealJoint->GetLowerLimit();
  1651. upperAngle = pRealJoint->GetUpperLimit();
  1652. return true;
  1653. }
  1654. //-----------------------------------------------------------------------------
  1655. void Scene::setRevoluteJointMotor(
  1656. const U32 jointId,
  1657. const bool enableMotor,
  1658. const F32 motorSpeed,
  1659. const F32 maxMotorTorque )
  1660. {
  1661. // Fetch joint.
  1662. b2Joint* pJoint = findJoint( jointId );
  1663. // Ignore invalid joint.
  1664. if ( !pJoint )
  1665. return;
  1666. // Fetch joint type.
  1667. const b2JointType jointType = pJoint->GetType();
  1668. if ( jointType != e_revoluteJoint )
  1669. {
  1670. Con::warnf( "Invalid joint type of %s.", getJointTypeDescription(jointType) );
  1671. return;
  1672. }
  1673. // Cast joint.
  1674. b2RevoluteJoint* pRealJoint = static_cast<b2RevoluteJoint*>( pJoint );
  1675. // Access joint.
  1676. pRealJoint->SetMotorSpeed( motorSpeed );
  1677. pRealJoint->SetMaxMotorTorque( maxMotorTorque );
  1678. pRealJoint->EnableMotor( enableMotor );
  1679. }
  1680. //-----------------------------------------------------------------------------
  1681. bool Scene::getRevoluteJointMotor(
  1682. const U32 jointId,
  1683. bool& enableMotor,
  1684. F32& motorSpeed,
  1685. F32& maxMotorTorque )
  1686. {
  1687. // Fetch joint.
  1688. b2Joint* pJoint = findJoint( jointId );
  1689. // Ignore invalid joint.
  1690. if ( !pJoint )
  1691. return false;
  1692. // Fetch joint type.
  1693. const b2JointType jointType = pJoint->GetType();
  1694. if ( jointType != e_revoluteJoint )
  1695. {
  1696. Con::warnf( "Invalid joint type of %s.", getJointTypeDescription(jointType) );
  1697. return false;
  1698. }
  1699. // Cast joint.
  1700. b2RevoluteJoint* pRealJoint = static_cast<b2RevoluteJoint*>( pJoint );
  1701. // Access joint.
  1702. enableMotor = pRealJoint->IsMotorEnabled();
  1703. motorSpeed = pRealJoint->GetMotorSpeed();
  1704. maxMotorTorque = pRealJoint->GetMaxMotorTorque();
  1705. return true;
  1706. }
  1707. //-----------------------------------------------------------------------------
  1708. F32 Scene::getRevoluteJointAngle( const U32 jointId )
  1709. {
  1710. // Fetch joint.
  1711. b2Joint* pJoint = findJoint( jointId );
  1712. // Ignore invalid joint.
  1713. if ( !pJoint )
  1714. return 0.0f;
  1715. // Fetch joint type.
  1716. const b2JointType jointType = pJoint->GetType();
  1717. if ( jointType != e_revoluteJoint )
  1718. {
  1719. Con::warnf( "Invalid joint type of %s.", getJointTypeDescription(jointType) );
  1720. return 0.0f;
  1721. }
  1722. // Cast joint.
  1723. b2RevoluteJoint* pRealJoint = static_cast<b2RevoluteJoint*>( pJoint );
  1724. // Access joint.
  1725. return pRealJoint->GetJointAngle();
  1726. }
  1727. //-----------------------------------------------------------------------------
  1728. F32 Scene::getRevoluteJointSpeed( const U32 jointId )
  1729. {
  1730. // Fetch joint.
  1731. b2Joint* pJoint = findJoint( jointId );
  1732. // Ignore invalid joint.
  1733. if ( !pJoint )
  1734. return 0.0f;
  1735. // Fetch joint type.
  1736. const b2JointType jointType = pJoint->GetType();
  1737. if ( jointType != e_revoluteJoint )
  1738. {
  1739. Con::warnf( "Invalid joint type of %s.", getJointTypeDescription(jointType) );
  1740. return 0.0f;
  1741. }
  1742. // Cast joint.
  1743. b2RevoluteJoint* pRealJoint = static_cast<b2RevoluteJoint*>( pJoint );
  1744. // Access joint.
  1745. return pRealJoint->GetJointSpeed();
  1746. }
  1747. //-----------------------------------------------------------------------------
  1748. S32 Scene::createWeldJoint(
  1749. const SceneObject* pSceneObjectA, const SceneObject* pSceneObjectB,
  1750. const b2Vec2& localAnchorA, const b2Vec2& localAnchorB,
  1751. const F32 frequency,
  1752. const F32 dampingRatio,
  1753. const bool collideConnected )
  1754. {
  1755. // Sanity!
  1756. if ( (pSceneObjectA != NULL && pSceneObjectA->getScene() == NULL) ||
  1757. (pSceneObjectB != NULL && pSceneObjectB->getScene() == NULL) )
  1758. {
  1759. // Warn.
  1760. Con::printf( "Cannot add a joint to a scene object that is not in a scene." );
  1761. return -1;
  1762. }
  1763. // Check for two invalid objects.
  1764. if ( pSceneObjectA == NULL && pSceneObjectB == NULL )
  1765. {
  1766. Con::warnf("Scene::createWeldJoint() - Cannot create joint without at least a single scene object." );
  1767. return -1;
  1768. }
  1769. // Fetch bodies.
  1770. b2Body* pBodyA = pSceneObjectA != NULL ? pSceneObjectA->getBody() : getGroundBody();
  1771. b2Body* pBodyB = pSceneObjectB != NULL ? pSceneObjectB->getBody() : getGroundBody();
  1772. // Populate definition.
  1773. b2WeldJointDef jointDef;
  1774. jointDef.userData = static_cast<PhysicsProxy*>(this);
  1775. jointDef.collideConnected = collideConnected;
  1776. jointDef.bodyA = pBodyA;
  1777. jointDef.bodyB = pBodyB;
  1778. jointDef.referenceAngle = pBodyB->GetAngle() - pBodyA->GetAngle();
  1779. jointDef.localAnchorA = localAnchorA;
  1780. jointDef.localAnchorB = localAnchorB;
  1781. jointDef.frequencyHz = frequency;
  1782. jointDef.dampingRatio = dampingRatio;
  1783. // Create joint.
  1784. return createJoint( &jointDef );
  1785. }
  1786. //-----------------------------------------------------------------------------
  1787. void Scene::setWeldJointFrequency(
  1788. const U32 jointId,
  1789. const F32 frequency )
  1790. {
  1791. // Fetch joint.
  1792. b2Joint* pJoint = findJoint( jointId );
  1793. // Ignore invalid joint.
  1794. if ( !pJoint )
  1795. return;
  1796. // Fetch joint type.
  1797. const b2JointType jointType = pJoint->GetType();
  1798. if ( jointType != e_weldJoint )
  1799. {
  1800. Con::warnf( "Invalid joint type of %s.", getJointTypeDescription(jointType) );
  1801. return;
  1802. }
  1803. // Cast joint.
  1804. b2WeldJoint* pRealJoint = static_cast<b2WeldJoint*>( pJoint );
  1805. // Access joint.
  1806. pRealJoint->SetFrequency( frequency );
  1807. }
  1808. //-----------------------------------------------------------------------------
  1809. F32 Scene::getWeldJointFrequency( const U32 jointId )
  1810. {
  1811. // Fetch joint.
  1812. b2Joint* pJoint = findJoint( jointId );
  1813. // Ignore invalid joint.
  1814. if ( !pJoint )
  1815. return -1.0f;
  1816. // Fetch joint type.
  1817. const b2JointType jointType = pJoint->GetType();
  1818. if ( jointType != e_weldJoint )
  1819. {
  1820. Con::warnf( "Invalid joint type of %s.", getJointTypeDescription(jointType) );
  1821. return -1.0f;
  1822. }
  1823. // Cast joint.
  1824. b2WeldJoint* pRealJoint = static_cast<b2WeldJoint*>( pJoint );
  1825. // Access joint.
  1826. return pRealJoint->GetFrequency();
  1827. }
  1828. //-----------------------------------------------------------------------------
  1829. void Scene::setWeldJointDampingRatio(
  1830. const U32 jointId,
  1831. const F32 dampingRatio )
  1832. {
  1833. // Fetch joint.
  1834. b2Joint* pJoint = findJoint( jointId );
  1835. // Ignore invalid joint.
  1836. if ( !pJoint )
  1837. return;
  1838. // Fetch joint type.
  1839. const b2JointType jointType = pJoint->GetType();
  1840. if ( jointType != e_weldJoint )
  1841. {
  1842. Con::warnf( "Invalid joint type of %s.", getJointTypeDescription(jointType) );
  1843. return;
  1844. }
  1845. // Cast joint.
  1846. b2WeldJoint* pRealJoint = static_cast<b2WeldJoint*>( pJoint );
  1847. // Access joint.
  1848. pRealJoint->SetDampingRatio( dampingRatio );
  1849. }
  1850. //-----------------------------------------------------------------------------
  1851. F32 Scene::getWeldJointDampingRatio( const U32 jointId )
  1852. {
  1853. // Fetch joint.
  1854. b2Joint* pJoint = findJoint( jointId );
  1855. // Ignore invalid joint.
  1856. if ( !pJoint )
  1857. return -1.0f;
  1858. // Fetch joint type.
  1859. const b2JointType jointType = pJoint->GetType();
  1860. if ( jointType != e_weldJoint )
  1861. {
  1862. Con::warnf( "Invalid joint type of %s.", getJointTypeDescription(jointType) );
  1863. return -1.0f;
  1864. }
  1865. // Cast joint.
  1866. b2WeldJoint* pRealJoint = static_cast<b2WeldJoint*>( pJoint );
  1867. // Access joint.
  1868. return pRealJoint->GetDampingRatio();
  1869. }
  1870. //-----------------------------------------------------------------------------
  1871. S32 Scene::createWheelJoint(
  1872. const SceneObject* pSceneObjectA, const SceneObject* pSceneObjectB,
  1873. const b2Vec2& localAnchorA, const b2Vec2& localAnchorB,
  1874. const b2Vec2& worldAxis,
  1875. const bool collideConnected )
  1876. {
  1877. // Sanity!
  1878. if ( (pSceneObjectA != NULL && pSceneObjectA->getScene() == NULL) ||
  1879. (pSceneObjectB != NULL && pSceneObjectB->getScene() == NULL) )
  1880. {
  1881. // Warn.
  1882. Con::printf( "Cannot add a joint to a scene object that is not in a scene." );
  1883. return -1;
  1884. }
  1885. // Check for two invalid objects.
  1886. if ( pSceneObjectA == NULL && pSceneObjectB == NULL )
  1887. {
  1888. Con::warnf("Scene::createWheelJoint() - Cannot create joint without at least a single scene object." );
  1889. return -1;
  1890. }
  1891. // Fetch bodies.
  1892. b2Body* pBodyA = pSceneObjectA != NULL ? pSceneObjectA->getBody() : getGroundBody();
  1893. b2Body* pBodyB = pSceneObjectB != NULL ? pSceneObjectB->getBody() : getGroundBody();
  1894. // Populate definition.
  1895. b2WheelJointDef jointDef;
  1896. jointDef.userData = static_cast<PhysicsProxy*>(this);
  1897. jointDef.collideConnected = collideConnected;
  1898. jointDef.bodyA = pBodyA;
  1899. jointDef.bodyB = pBodyB;
  1900. jointDef.localAnchorA = localAnchorA;
  1901. jointDef.localAnchorB = localAnchorB;
  1902. jointDef.localAxisA = pBodyA->GetLocalVector( worldAxis );
  1903. // Create joint.
  1904. return createJoint( &jointDef );
  1905. }
  1906. //-----------------------------------------------------------------------------
  1907. void Scene::setWheelJointMotor(
  1908. const U32 jointId,
  1909. const bool enableMotor,
  1910. const F32 motorSpeed,
  1911. const F32 maxMotorTorque )
  1912. {
  1913. // Fetch joint.
  1914. b2Joint* pJoint = findJoint( jointId );
  1915. // Ignore invalid joint.
  1916. if ( !pJoint )
  1917. return;
  1918. // Fetch joint type.
  1919. const b2JointType jointType = pJoint->GetType();
  1920. if ( jointType != e_wheelJoint )
  1921. {
  1922. Con::warnf( "Invalid joint type of %s.", getJointTypeDescription(jointType) );
  1923. return;
  1924. }
  1925. // Cast joint.
  1926. b2WheelJoint* pRealJoint = static_cast<b2WheelJoint*>( pJoint );
  1927. // Access joint.
  1928. pRealJoint->SetMotorSpeed( motorSpeed );
  1929. pRealJoint->SetMaxMotorTorque( maxMotorTorque );
  1930. pRealJoint->EnableMotor( enableMotor );
  1931. }
  1932. //-----------------------------------------------------------------------------
  1933. bool Scene::getWheelJointMotor(
  1934. const U32 jointId,
  1935. bool& enableMotor,
  1936. F32& motorSpeed,
  1937. F32& maxMotorTorque )
  1938. {
  1939. // Fetch joint.
  1940. b2Joint* pJoint = findJoint( jointId );
  1941. // Ignore invalid joint.
  1942. if ( !pJoint )
  1943. return false;
  1944. // Fetch joint type.
  1945. const b2JointType jointType = pJoint->GetType();
  1946. if ( jointType != e_wheelJoint )
  1947. {
  1948. Con::warnf( "Invalid joint type of %s.", getJointTypeDescription(jointType) );
  1949. return false;
  1950. }
  1951. // Cast joint.
  1952. b2WheelJoint* pRealJoint = static_cast<b2WheelJoint*>( pJoint );
  1953. // Access joint.
  1954. enableMotor = pRealJoint->IsMotorEnabled();
  1955. motorSpeed = pRealJoint->GetMotorSpeed();
  1956. maxMotorTorque = pRealJoint->GetMaxMotorTorque();
  1957. return true;
  1958. }
  1959. //-----------------------------------------------------------------------------
  1960. void Scene::setWheelJointFrequency(
  1961. const U32 jointId,
  1962. const F32 frequency )
  1963. {
  1964. // Fetch joint.
  1965. b2Joint* pJoint = findJoint( jointId );
  1966. // Ignore invalid joint.
  1967. if ( !pJoint )
  1968. return;
  1969. // Fetch joint type.
  1970. const b2JointType jointType = pJoint->GetType();
  1971. if ( jointType != e_wheelJoint )
  1972. {
  1973. Con::warnf( "Invalid joint type of %s.", getJointTypeDescription(jointType) );
  1974. return;
  1975. }
  1976. // Cast joint.
  1977. b2WheelJoint* pRealJoint = static_cast<b2WheelJoint*>( pJoint );
  1978. // Access joint.
  1979. pRealJoint->SetSpringFrequencyHz( frequency );
  1980. }
  1981. //-----------------------------------------------------------------------------
  1982. F32 Scene::getWheelJointFrequency( const U32 jointId )
  1983. {
  1984. // Fetch joint.
  1985. b2Joint* pJoint = findJoint( jointId );
  1986. // Ignore invalid joint.
  1987. if ( !pJoint )
  1988. return -1.0f;
  1989. // Fetch joint type.
  1990. const b2JointType jointType = pJoint->GetType();
  1991. if ( jointType != e_wheelJoint )
  1992. {
  1993. Con::warnf( "Invalid joint type of %s.", getJointTypeDescription(jointType) );
  1994. return -1.0f;
  1995. }
  1996. // Cast joint.
  1997. b2WheelJoint* pRealJoint = static_cast<b2WheelJoint*>( pJoint );
  1998. // Access joint.
  1999. return pRealJoint->GetSpringFrequencyHz();
  2000. }
  2001. //-----------------------------------------------------------------------------
  2002. void Scene::setWheelJointDampingRatio(
  2003. const U32 jointId,
  2004. const F32 dampingRatio )
  2005. {
  2006. // Fetch joint.
  2007. b2Joint* pJoint = findJoint( jointId );
  2008. // Ignore invalid joint.
  2009. if ( !pJoint )
  2010. return;
  2011. // Fetch joint type.
  2012. const b2JointType jointType = pJoint->GetType();
  2013. if ( jointType != e_wheelJoint )
  2014. {
  2015. Con::warnf( "Invalid joint type of %s.", getJointTypeDescription(jointType) );
  2016. return;
  2017. }
  2018. // Cast joint.
  2019. b2WheelJoint* pRealJoint = static_cast<b2WheelJoint*>( pJoint );
  2020. // Access joint.
  2021. pRealJoint->SetSpringDampingRatio( dampingRatio );
  2022. }
  2023. //-----------------------------------------------------------------------------
  2024. F32 Scene::getWheelJointDampingRatio( const U32 jointId )
  2025. {
  2026. // Fetch joint.
  2027. b2Joint* pJoint = findJoint( jointId );
  2028. // Ignore invalid joint.
  2029. if ( !pJoint )
  2030. return -1.0f;
  2031. // Fetch joint type.
  2032. const b2JointType jointType = pJoint->GetType();
  2033. if ( jointType != e_wheelJoint )
  2034. {
  2035. Con::warnf( "Invalid joint type of %s.", getJointTypeDescription(jointType) );
  2036. return -1.0f;
  2037. }
  2038. // Cast joint.
  2039. b2WheelJoint* pRealJoint = static_cast<b2WheelJoint*>( pJoint );
  2040. // Access joint.
  2041. return pRealJoint->GetSpringDampingRatio();
  2042. }
  2043. //-----------------------------------------------------------------------------
  2044. S32 Scene::createFrictionJoint(
  2045. const SceneObject* pSceneObjectA, const SceneObject* pSceneObjectB,
  2046. const b2Vec2& localAnchorA, const b2Vec2& localAnchorB,
  2047. const F32 maxForce,
  2048. const F32 maxTorque,
  2049. const bool collideConnected )
  2050. {
  2051. // Sanity!
  2052. if ( (pSceneObjectA != NULL && pSceneObjectA->getScene() == NULL) ||
  2053. (pSceneObjectB != NULL && pSceneObjectB->getScene() == NULL) )
  2054. {
  2055. // Warn.
  2056. Con::printf( "Cannot add a joint to a scene object that is not in a scene." );
  2057. return -1;
  2058. }
  2059. // Check for two invalid objects.
  2060. if ( pSceneObjectA == NULL && pSceneObjectB == NULL )
  2061. {
  2062. Con::warnf("Scene::createFrictionJoint() - Cannot create joint without at least a single scene object." );
  2063. return -1;
  2064. }
  2065. // Fetch bodies.
  2066. b2Body* pBodyA = pSceneObjectA != NULL ? pSceneObjectA->getBody() : getGroundBody();
  2067. b2Body* pBodyB = pSceneObjectB != NULL ? pSceneObjectB->getBody() : getGroundBody();
  2068. // Populate definition.
  2069. b2FrictionJointDef jointDef;
  2070. jointDef.userData = static_cast<PhysicsProxy*>(this);
  2071. jointDef.collideConnected = collideConnected;
  2072. jointDef.bodyA = pBodyA;
  2073. jointDef.bodyB = pBodyB;
  2074. jointDef.localAnchorA = localAnchorA;
  2075. jointDef.localAnchorB = localAnchorB;
  2076. jointDef.maxForce = maxForce;
  2077. jointDef.maxTorque = maxTorque;
  2078. // Create joint.
  2079. return createJoint( &jointDef );
  2080. }
  2081. //-----------------------------------------------------------------------------
  2082. void Scene::setFrictionJointMaxForce(
  2083. const U32 jointId,
  2084. const F32 maxForce )
  2085. {
  2086. // Fetch joint.
  2087. b2Joint* pJoint = findJoint( jointId );
  2088. // Ignore invalid joint.
  2089. if ( !pJoint )
  2090. return;
  2091. // Fetch joint type.
  2092. const b2JointType jointType = pJoint->GetType();
  2093. if ( jointType != e_frictionJoint )
  2094. {
  2095. Con::warnf( "Invalid joint type of %s.", getJointTypeDescription(jointType) );
  2096. return;
  2097. }
  2098. // Cast joint.
  2099. b2FrictionJoint* pRealJoint = static_cast<b2FrictionJoint*>( pJoint );
  2100. // Access joint.
  2101. pRealJoint->SetMaxForce( maxForce );
  2102. }
  2103. //-----------------------------------------------------------------------------
  2104. F32 Scene::getFrictionJointMaxForce( const U32 jointId )
  2105. {
  2106. // Fetch joint.
  2107. b2Joint* pJoint = findJoint( jointId );
  2108. // Ignore invalid joint.
  2109. if ( !pJoint )
  2110. return -1.0f;
  2111. // Fetch joint type.
  2112. const b2JointType jointType = pJoint->GetType();
  2113. if ( jointType != e_frictionJoint )
  2114. {
  2115. Con::warnf( "Invalid joint type of %s.", getJointTypeDescription(jointType) );
  2116. return -1.0f;
  2117. }
  2118. // Cast joint.
  2119. b2FrictionJoint* pRealJoint = static_cast<b2FrictionJoint*>( pJoint );
  2120. // Access joint.
  2121. return pRealJoint->GetMaxForce();
  2122. }
  2123. //-----------------------------------------------------------------------------
  2124. void Scene::setFrictionJointMaxTorque(
  2125. const U32 jointId,
  2126. const F32 maxTorque )
  2127. {
  2128. // Fetch joint.
  2129. b2Joint* pJoint = findJoint( jointId );
  2130. // Ignore invalid joint.
  2131. if ( !pJoint )
  2132. return;
  2133. // Fetch joint type.
  2134. const b2JointType jointType = pJoint->GetType();
  2135. if ( jointType != e_frictionJoint )
  2136. {
  2137. Con::warnf( "Invalid joint type of %s.", getJointTypeDescription(jointType) );
  2138. return;
  2139. }
  2140. // Cast joint.
  2141. b2FrictionJoint* pRealJoint = static_cast<b2FrictionJoint*>( pJoint );
  2142. // Access joint.
  2143. pRealJoint->SetMaxTorque( maxTorque );
  2144. }
  2145. //-----------------------------------------------------------------------------
  2146. F32 Scene::getFrictionJointMaxTorque( const U32 jointId )
  2147. {
  2148. // Fetch joint.
  2149. b2Joint* pJoint = findJoint( jointId );
  2150. // Ignore invalid joint.
  2151. if ( !pJoint )
  2152. return -1.0f;
  2153. // Fetch joint type.
  2154. const b2JointType jointType = pJoint->GetType();
  2155. if ( jointType != e_frictionJoint )
  2156. {
  2157. Con::warnf( "Invalid joint type of %s.", getJointTypeDescription(jointType) );
  2158. return -1.0f;
  2159. }
  2160. // Cast joint.
  2161. b2FrictionJoint* pRealJoint = static_cast<b2FrictionJoint*>( pJoint );
  2162. // Access joint.
  2163. return pRealJoint->GetMaxTorque();
  2164. }
  2165. //-----------------------------------------------------------------------------
  2166. S32 Scene::createPrismaticJoint(
  2167. const SceneObject* pSceneObjectA, const SceneObject* pSceneObjectB,
  2168. const b2Vec2& localAnchorA, const b2Vec2& localAnchorB,
  2169. const b2Vec2& worldAxis,
  2170. const bool collideConnected )
  2171. {
  2172. // Sanity!
  2173. if ( (pSceneObjectA != NULL && pSceneObjectA->getScene() == NULL) ||
  2174. (pSceneObjectB != NULL && pSceneObjectB->getScene() == NULL) )
  2175. {
  2176. // Warn.
  2177. Con::printf( "Cannot add a joint to a scene object that is not in a scene." );
  2178. return -1;
  2179. }
  2180. // Check for two invalid objects.
  2181. if ( pSceneObjectA == NULL && pSceneObjectB == NULL )
  2182. {
  2183. Con::warnf("Scene::createPrismaticJoint() - Cannot create joint without at least a single scene object." );
  2184. return -1;
  2185. }
  2186. // Fetch bodies.
  2187. b2Body* pBodyA = pSceneObjectA != NULL ? pSceneObjectA->getBody() : getGroundBody();
  2188. b2Body* pBodyB = pSceneObjectB != NULL ? pSceneObjectB->getBody() : getGroundBody();
  2189. // Populate definition.
  2190. b2PrismaticJointDef jointDef;
  2191. jointDef.userData = static_cast<PhysicsProxy*>(this);
  2192. jointDef.collideConnected = collideConnected;
  2193. jointDef.bodyA = pBodyA;
  2194. jointDef.bodyB = pBodyB;
  2195. jointDef.referenceAngle = pBodyB->GetAngle() - pBodyA->GetAngle();
  2196. jointDef.localAnchorA = localAnchorA;
  2197. jointDef.localAnchorB = localAnchorB;
  2198. jointDef.localAxisA = pBodyA->GetLocalVector( worldAxis );
  2199. // Create joint.
  2200. return createJoint( &jointDef );
  2201. }
  2202. //-----------------------------------------------------------------------------
  2203. void Scene::setPrismaticJointLimit(
  2204. const U32 jointId,
  2205. const bool enableLimit,
  2206. const F32 lowerTranslation, const F32 upperTranslation )
  2207. {
  2208. // Fetch joint.
  2209. b2Joint* pJoint = findJoint( jointId );
  2210. // Ignore invalid joint.
  2211. if ( !pJoint )
  2212. return;
  2213. // Fetch joint type.
  2214. const b2JointType jointType = pJoint->GetType();
  2215. if ( jointType != e_prismaticJoint )
  2216. {
  2217. Con::warnf( "Invalid joint type of %s.", getJointTypeDescription(jointType) );
  2218. return;
  2219. }
  2220. // Cast joint.
  2221. b2PrismaticJoint* pRealJoint = static_cast<b2PrismaticJoint*>( pJoint );
  2222. // Access joint.
  2223. pRealJoint->SetLimits( lowerTranslation, upperTranslation );
  2224. pRealJoint->EnableLimit( enableLimit );
  2225. }
  2226. //-----------------------------------------------------------------------------
  2227. bool Scene::getPrismaticJointLimit(
  2228. const U32 jointId,
  2229. bool& enableLimit,
  2230. F32& lowerTranslation, F32& upperTranslation )
  2231. {
  2232. // Fetch joint.
  2233. b2Joint* pJoint = findJoint( jointId );
  2234. // Ignore invalid joint.
  2235. if ( !pJoint )
  2236. return false;
  2237. // Fetch joint type.
  2238. const b2JointType jointType = pJoint->GetType();
  2239. if ( jointType != e_prismaticJoint )
  2240. {
  2241. Con::warnf( "Invalid joint type of %s.", getJointTypeDescription(jointType) );
  2242. return false;
  2243. }
  2244. // Cast joint.
  2245. b2PrismaticJoint* pRealJoint = static_cast<b2PrismaticJoint*>( pJoint );
  2246. // Access joint.
  2247. enableLimit = pRealJoint->IsLimitEnabled();
  2248. lowerTranslation = pRealJoint->GetLowerLimit();
  2249. upperTranslation = pRealJoint->GetUpperLimit();
  2250. return true;
  2251. }
  2252. //-----------------------------------------------------------------------------
  2253. void Scene::setPrismaticJointMotor(
  2254. const U32 jointId,
  2255. const bool enableMotor,
  2256. const F32 motorSpeed,
  2257. const F32 maxMotorForce )
  2258. {
  2259. // Fetch joint.
  2260. b2Joint* pJoint = findJoint( jointId );
  2261. // Ignore invalid joint.
  2262. if ( !pJoint )
  2263. return;
  2264. // Fetch joint type.
  2265. const b2JointType jointType = pJoint->GetType();
  2266. if ( jointType != e_prismaticJoint )
  2267. {
  2268. Con::warnf( "Invalid joint type of %s.", getJointTypeDescription(jointType) );
  2269. return;
  2270. }
  2271. // Cast joint.
  2272. b2PrismaticJoint* pRealJoint = static_cast<b2PrismaticJoint*>( pJoint );
  2273. // Access joint.
  2274. pRealJoint->SetMotorSpeed( motorSpeed );
  2275. pRealJoint->SetMaxMotorForce( maxMotorForce );
  2276. pRealJoint->EnableMotor( enableMotor );
  2277. }
  2278. //-----------------------------------------------------------------------------
  2279. bool Scene::getPrismaticJointMotor(
  2280. const U32 jointId,
  2281. bool& enableMotor,
  2282. F32& motorSpeed,
  2283. F32& maxMotorForce )
  2284. {
  2285. // Fetch joint.
  2286. b2Joint* pJoint = findJoint( jointId );
  2287. // Ignore invalid joint.
  2288. if ( !pJoint )
  2289. return false;
  2290. // Fetch joint type.
  2291. const b2JointType jointType = pJoint->GetType();
  2292. if ( jointType != e_prismaticJoint )
  2293. {
  2294. Con::warnf( "Invalid joint type of %s.", getJointTypeDescription(jointType) );
  2295. return false;
  2296. }
  2297. // Cast joint.
  2298. b2PrismaticJoint* pRealJoint = static_cast<b2PrismaticJoint*>( pJoint );
  2299. // Access joint.
  2300. enableMotor = pRealJoint->IsMotorEnabled();
  2301. motorSpeed = pRealJoint->GetMotorSpeed();
  2302. maxMotorForce = pRealJoint->GetMaxMotorForce();
  2303. return true;
  2304. }
  2305. //-----------------------------------------------------------------------------
  2306. S32 Scene::createPulleyJoint(
  2307. const SceneObject* pSceneObjectA, const SceneObject* pSceneObjectB,
  2308. const b2Vec2& localAnchorA, const b2Vec2& localAnchorB,
  2309. const b2Vec2& worldGroundAnchorA, const b2Vec2& worldGroundAnchorB,
  2310. const F32 ratio,
  2311. const F32 lengthA, const F32 lengthB,
  2312. const bool collideConnected )
  2313. {
  2314. // Sanity!
  2315. if ( (pSceneObjectA != NULL && pSceneObjectA->getScene() == NULL) ||
  2316. (pSceneObjectB != NULL && pSceneObjectB->getScene() == NULL) )
  2317. {
  2318. // Warn.
  2319. Con::printf( "Cannot add a joint to a scene object that is not in a scene." );
  2320. return -1;
  2321. }
  2322. // Check for two invalid objects.
  2323. if ( pSceneObjectA == NULL && pSceneObjectB == NULL )
  2324. {
  2325. Con::warnf("Scene::createPulleyJoint() - Cannot create joint without at least a single scene object." );
  2326. return -1;
  2327. }
  2328. // Fetch bodies.
  2329. b2Body* pBodyA = pSceneObjectA != NULL ? pSceneObjectA->getBody() : getGroundBody();
  2330. b2Body* pBodyB = pSceneObjectB != NULL ? pSceneObjectB->getBody() : getGroundBody();
  2331. // Populate definition.
  2332. b2PulleyJointDef jointDef;
  2333. jointDef.userData = static_cast<PhysicsProxy*>(this);
  2334. jointDef.collideConnected = collideConnected;
  2335. jointDef.bodyA = pBodyA;
  2336. jointDef.bodyB = pBodyB;
  2337. jointDef.groundAnchorA = worldGroundAnchorA;
  2338. jointDef.groundAnchorB = worldGroundAnchorB;
  2339. jointDef.localAnchorA = localAnchorA;
  2340. jointDef.localAnchorB = localAnchorB;
  2341. jointDef.lengthA = lengthA < 0.0f ? (pBodyA->GetWorldPoint( localAnchorA ) - worldGroundAnchorA).Length() : lengthA;
  2342. jointDef.lengthB = lengthB < 0.0f ? (pBodyA->GetWorldPoint( localAnchorB ) - worldGroundAnchorB).Length() : lengthB;
  2343. jointDef.ratio = ratio > b2_epsilon ? ratio : b2_epsilon + b2_epsilon;
  2344. // Create joint.
  2345. return createJoint( &jointDef );
  2346. }
  2347. //-----------------------------------------------------------------------------
  2348. S32 Scene::createTargetJoint(
  2349. const SceneObject* pSceneObject,
  2350. const b2Vec2& worldTarget,
  2351. const F32 maxForce,
  2352. const bool useCenterOfMass,
  2353. const F32 frequency,
  2354. const F32 dampingRatio,
  2355. const bool collideConnected )
  2356. {
  2357. // Sanity!
  2358. if ( pSceneObject != NULL && pSceneObject->getScene() == NULL )
  2359. {
  2360. // Warn.
  2361. Con::printf( "Cannot add a joint to a scene object that is not in a scene." );
  2362. return -1;
  2363. }
  2364. // Check for invalid object.
  2365. if ( pSceneObject == NULL )
  2366. {
  2367. Con::warnf("Scene::createPulleyJoint() - Cannot create joint without a scene object." );
  2368. return -1;
  2369. }
  2370. // Fetch bodies.
  2371. b2Body* pBody = pSceneObject->getBody();
  2372. // Populate definition.
  2373. b2MouseJointDef jointDef;
  2374. jointDef.userData = static_cast<PhysicsProxy*>(this);
  2375. jointDef.collideConnected = collideConnected;
  2376. jointDef.bodyA = getGroundBody();
  2377. jointDef.bodyB = pBody;
  2378. jointDef.target = useCenterOfMass ? pBody->GetWorldCenter() : worldTarget;
  2379. jointDef.maxForce = maxForce;
  2380. jointDef.frequencyHz = frequency;
  2381. jointDef.dampingRatio = dampingRatio;
  2382. // Create joint.
  2383. const U32 jointId = createJoint( &jointDef );
  2384. // Cast joint.
  2385. b2MouseJoint* pRealJoint = static_cast<b2MouseJoint*>( findJoint( jointId ) );
  2386. // Are we using the center of mass?
  2387. if ( !useCenterOfMass )
  2388. {
  2389. // No, so set the target as the world target.
  2390. // NOTE:- This is done because initially the target (mouse) joint assumes the target
  2391. // coincides with the body anchor.
  2392. pRealJoint->SetTarget( worldTarget );
  2393. }
  2394. return jointId;
  2395. }
  2396. //-----------------------------------------------------------------------------
  2397. void Scene::setTargetJointTarget(
  2398. const U32 jointId,
  2399. const b2Vec2& worldTarget )
  2400. {
  2401. // Fetch joint.
  2402. b2Joint* pJoint = findJoint( jointId );
  2403. // Ignore invalid joint.
  2404. if ( !pJoint )
  2405. return;
  2406. // Fetch joint type.
  2407. const b2JointType jointType = pJoint->GetType();
  2408. if ( jointType != e_mouseJoint )
  2409. {
  2410. Con::warnf( "Invalid joint type of %s.", getJointTypeDescription(jointType) );
  2411. return;
  2412. }
  2413. // Cast joint.
  2414. b2MouseJoint* pRealJoint = static_cast<b2MouseJoint*>( pJoint );
  2415. // Access joint.
  2416. pRealJoint->SetTarget( worldTarget );
  2417. }
  2418. //-----------------------------------------------------------------------------
  2419. b2Vec2 Scene::getTargetJointTarget( const U32 jointId )
  2420. {
  2421. // Fetch joint.
  2422. b2Joint* pJoint = findJoint( jointId );
  2423. // Ignore invalid joint.
  2424. if ( !pJoint )
  2425. return b2Vec2_zero;
  2426. // Fetch joint type.
  2427. const b2JointType jointType = pJoint->GetType();
  2428. if ( jointType != e_mouseJoint )
  2429. {
  2430. Con::warnf( "Invalid joint type of %s.", getJointTypeDescription(jointType) );
  2431. return b2Vec2_zero;
  2432. }
  2433. // Cast joint.
  2434. b2MouseJoint* pRealJoint = static_cast<b2MouseJoint*>( pJoint );
  2435. // Access joint.
  2436. return pRealJoint->GetTarget();
  2437. }
  2438. //-----------------------------------------------------------------------------
  2439. void Scene::setTargetJointMaxForce(
  2440. const U32 jointId,
  2441. const F32 maxForce )
  2442. {
  2443. // Fetch joint.
  2444. b2Joint* pJoint = findJoint( jointId );
  2445. // Ignore invalid joint.
  2446. if ( !pJoint )
  2447. return;
  2448. // Fetch joint type.
  2449. const b2JointType jointType = pJoint->GetType();
  2450. if ( jointType != e_mouseJoint )
  2451. {
  2452. Con::warnf( "Invalid joint type of %s.", getJointTypeDescription(jointType) );
  2453. return;
  2454. }
  2455. // Cast joint.
  2456. b2MouseJoint* pRealJoint = static_cast<b2MouseJoint*>( pJoint );
  2457. // Access joint.
  2458. pRealJoint->SetMaxForce( maxForce );
  2459. }
  2460. //-----------------------------------------------------------------------------
  2461. F32 Scene::getTargetJointMaxForce( const U32 jointId )
  2462. {
  2463. // Fetch joint.
  2464. b2Joint* pJoint = findJoint( jointId );
  2465. // Ignore invalid joint.
  2466. if ( !pJoint )
  2467. return -1.0f;
  2468. // Fetch joint type.
  2469. const b2JointType jointType = pJoint->GetType();
  2470. if ( jointType != e_mouseJoint )
  2471. {
  2472. Con::warnf( "Invalid joint type of %s.", getJointTypeDescription(jointType) );
  2473. return -1.0f;
  2474. }
  2475. // Cast joint.
  2476. b2MouseJoint* pRealJoint = static_cast<b2MouseJoint*>( pJoint );
  2477. // Access joint.
  2478. return pRealJoint->GetMaxForce();
  2479. }
  2480. //-----------------------------------------------------------------------------
  2481. void Scene::setTargetJointFrequency(
  2482. const U32 jointId,
  2483. const F32 frequency )
  2484. {
  2485. // Fetch joint.
  2486. b2Joint* pJoint = findJoint( jointId );
  2487. // Ignore invalid joint.
  2488. if ( !pJoint )
  2489. return;
  2490. // Fetch joint type.
  2491. const b2JointType jointType = pJoint->GetType();
  2492. if ( jointType != e_mouseJoint )
  2493. {
  2494. Con::warnf( "Invalid joint type of %s.", getJointTypeDescription(jointType) );
  2495. return;
  2496. }
  2497. // Cast joint.
  2498. b2MouseJoint* pRealJoint = static_cast<b2MouseJoint*>( pJoint );
  2499. // Access joint.
  2500. pRealJoint->SetFrequency( frequency );
  2501. }
  2502. //-----------------------------------------------------------------------------
  2503. F32 Scene::getTargetJointFrequency( const U32 jointId )
  2504. {
  2505. // Fetch joint.
  2506. b2Joint* pJoint = findJoint( jointId );
  2507. // Ignore invalid joint.
  2508. if ( !pJoint )
  2509. return -1.0f;
  2510. // Fetch joint type.
  2511. const b2JointType jointType = pJoint->GetType();
  2512. if ( jointType != e_mouseJoint )
  2513. {
  2514. Con::warnf( "Invalid joint type of %s.", getJointTypeDescription(jointType) );
  2515. return -1.0f;
  2516. }
  2517. // Cast joint.
  2518. b2MouseJoint* pRealJoint = static_cast<b2MouseJoint*>( pJoint );
  2519. // Access joint.
  2520. return pRealJoint->GetFrequency();
  2521. }
  2522. //-----------------------------------------------------------------------------
  2523. void Scene::setTargetJointDampingRatio(
  2524. const U32 jointId,
  2525. const F32 dampingRatio )
  2526. {
  2527. // Fetch joint.
  2528. b2Joint* pJoint = findJoint( jointId );
  2529. // Ignore invalid joint.
  2530. if ( !pJoint )
  2531. return;
  2532. // Fetch joint type.
  2533. const b2JointType jointType = pJoint->GetType();
  2534. if ( jointType != e_mouseJoint )
  2535. {
  2536. Con::warnf( "Invalid joint type of %s.", getJointTypeDescription(jointType) );
  2537. return;
  2538. }
  2539. // Cast joint.
  2540. b2MouseJoint* pRealJoint = static_cast<b2MouseJoint*>( pJoint );
  2541. // Access joint.
  2542. pRealJoint->SetDampingRatio( dampingRatio );
  2543. }
  2544. //-----------------------------------------------------------------------------
  2545. F32 Scene::getTargetJointDampingRatio( const U32 jointId )
  2546. {
  2547. // Fetch joint.
  2548. b2Joint* pJoint = findJoint( jointId );
  2549. // Ignore invalid joint.
  2550. if ( !pJoint )
  2551. return -1.0f;
  2552. // Fetch joint type.
  2553. const b2JointType jointType = pJoint->GetType();
  2554. if ( jointType != e_mouseJoint )
  2555. {
  2556. Con::warnf( "Invalid joint type of %s.", getJointTypeDescription(jointType) );
  2557. return -1.0f;
  2558. }
  2559. // Cast joint.
  2560. b2MouseJoint* pRealJoint = static_cast<b2MouseJoint*>( pJoint );
  2561. // Access joint.
  2562. return pRealJoint->GetDampingRatio();
  2563. }
  2564. //-----------------------------------------------------------------------------
  2565. S32 Scene::createMotorJoint(
  2566. const SceneObject* pSceneObjectA, const SceneObject* pSceneObjectB,
  2567. const b2Vec2 linearOffset,
  2568. const F32 angularOffset,
  2569. const F32 maxForce,
  2570. const F32 maxTorque,
  2571. const F32 correctionFactor,
  2572. const bool collideConnected )
  2573. {
  2574. // Sanity!
  2575. if ( (pSceneObjectA != NULL && pSceneObjectA->getScene() == NULL) ||
  2576. (pSceneObjectB != NULL && pSceneObjectB->getScene() == NULL) )
  2577. {
  2578. // Warn.
  2579. Con::printf( "Cannot add a joint to a scene object that is not in a scene." );
  2580. return -1;
  2581. }
  2582. // Check for two invalid objects.
  2583. if ( pSceneObjectA == NULL && pSceneObjectB == NULL )
  2584. {
  2585. Con::warnf("Scene::createMotorJoint() - Cannot create joint without at least a single scene object." );
  2586. return -1;
  2587. }
  2588. // Fetch bodies.
  2589. b2Body* pBodyA = pSceneObjectA != NULL ? pSceneObjectA->getBody() : getGroundBody();
  2590. b2Body* pBodyB = pSceneObjectB != NULL ? pSceneObjectB->getBody() : getGroundBody();
  2591. // Populate definition.
  2592. b2MotorJointDef jointDef;
  2593. jointDef.userData = static_cast<PhysicsProxy*>(this);
  2594. jointDef.collideConnected = collideConnected;
  2595. jointDef.bodyA = pBodyA;
  2596. jointDef.bodyB = pBodyB;
  2597. jointDef.linearOffset = linearOffset;
  2598. jointDef.angularOffset = angularOffset;
  2599. jointDef.correctionFactor = correctionFactor;
  2600. jointDef.maxForce = maxForce;
  2601. jointDef.maxTorque = maxTorque;
  2602. // Create joint.
  2603. return createJoint( &jointDef );
  2604. }
  2605. //-----------------------------------------------------------------------------
  2606. void Scene::setMotorJointLinearOffset(
  2607. const U32 jointId,
  2608. const b2Vec2& linearOffset )
  2609. {
  2610. // Fetch joint.
  2611. b2Joint* pJoint = findJoint( jointId );
  2612. // Ignore invalid joint.
  2613. if ( !pJoint )
  2614. return;
  2615. // Fetch joint type.
  2616. const b2JointType jointType = pJoint->GetType();
  2617. if ( jointType != e_motorJoint )
  2618. {
  2619. Con::warnf( "Invalid joint type of %s.", getJointTypeDescription(jointType) );
  2620. return;
  2621. }
  2622. // Cast joint.
  2623. b2MotorJoint* pRealJoint = static_cast<b2MotorJoint*>( pJoint );
  2624. // Access joint.
  2625. pRealJoint->SetLinearOffset( linearOffset );
  2626. }
  2627. //-----------------------------------------------------------------------------
  2628. b2Vec2 Scene::getMotorJointLinearOffset( const U32 jointId )
  2629. {
  2630. // Fetch joint.
  2631. b2Joint* pJoint = findJoint( jointId );
  2632. // Ignore invalid joint.
  2633. if ( !pJoint )
  2634. return b2Vec2_zero;
  2635. // Fetch joint type.
  2636. const b2JointType jointType = pJoint->GetType();
  2637. if ( jointType != e_motorJoint )
  2638. {
  2639. Con::warnf( "Invalid joint type of %s.", getJointTypeDescription(jointType) );
  2640. return b2Vec2_zero;
  2641. }
  2642. // Cast joint.
  2643. b2MotorJoint* pRealJoint = static_cast<b2MotorJoint*>( pJoint );
  2644. // Access joint.
  2645. return pRealJoint->GetLinearOffset();
  2646. }
  2647. //-----------------------------------------------------------------------------
  2648. void Scene::setMotorJointAngularOffset(
  2649. const U32 jointId,
  2650. const F32 angularOffset )
  2651. {
  2652. // Fetch joint.
  2653. b2Joint* pJoint = findJoint( jointId );
  2654. // Ignore invalid joint.
  2655. if ( !pJoint )
  2656. return;
  2657. // Fetch joint type.
  2658. const b2JointType jointType = pJoint->GetType();
  2659. if ( jointType != e_motorJoint )
  2660. {
  2661. Con::warnf( "Invalid joint type of %s.", getJointTypeDescription(jointType) );
  2662. return;
  2663. }
  2664. // Cast joint.
  2665. b2MotorJoint* pRealJoint = static_cast<b2MotorJoint*>( pJoint );
  2666. // Access joint.
  2667. pRealJoint->SetAngularOffset( angularOffset );
  2668. }
  2669. //-----------------------------------------------------------------------------
  2670. F32 Scene::getMotorJointAngularOffset( const U32 jointId )
  2671. {
  2672. // Fetch joint.
  2673. b2Joint* pJoint = findJoint( jointId );
  2674. // Ignore invalid joint.
  2675. if ( !pJoint )
  2676. return -1.0f;
  2677. // Fetch joint type.
  2678. const b2JointType jointType = pJoint->GetType();
  2679. if ( jointType != e_motorJoint )
  2680. {
  2681. Con::warnf( "Invalid joint type of %s.", getJointTypeDescription(jointType) );
  2682. return -1.0f;
  2683. }
  2684. // Cast joint.
  2685. b2MotorJoint* pRealJoint = static_cast<b2MotorJoint*>( pJoint );
  2686. // Access joint.
  2687. return pRealJoint->GetAngularOffset();
  2688. }
  2689. //-----------------------------------------------------------------------------
  2690. void Scene::setMotorJointMaxForce(
  2691. const U32 jointId,
  2692. const F32 maxForce )
  2693. {
  2694. // Fetch joint.
  2695. b2Joint* pJoint = findJoint( jointId );
  2696. // Ignore invalid joint.
  2697. if ( !pJoint )
  2698. return;
  2699. // Fetch joint type.
  2700. const b2JointType jointType = pJoint->GetType();
  2701. if ( jointType != e_motorJoint )
  2702. {
  2703. Con::warnf( "Invalid joint type of %s.", getJointTypeDescription(jointType) );
  2704. return;
  2705. }
  2706. // Cast joint.
  2707. b2MotorJoint* pRealJoint = static_cast<b2MotorJoint*>( pJoint );
  2708. // Access joint.
  2709. pRealJoint->SetMaxForce( maxForce );
  2710. }
  2711. //-----------------------------------------------------------------------------
  2712. F32 Scene::getMotorJointMaxForce( const U32 jointId )
  2713. {
  2714. // Fetch joint.
  2715. b2Joint* pJoint = findJoint( jointId );
  2716. // Ignore invalid joint.
  2717. if ( !pJoint )
  2718. return -1.0f;
  2719. // Fetch joint type.
  2720. const b2JointType jointType = pJoint->GetType();
  2721. if ( jointType != e_motorJoint )
  2722. {
  2723. Con::warnf( "Invalid joint type of %s.", getJointTypeDescription(jointType) );
  2724. return -1.0f;
  2725. }
  2726. // Cast joint.
  2727. b2MotorJoint* pRealJoint = static_cast<b2MotorJoint*>( pJoint );
  2728. // Access joint.
  2729. return pRealJoint->GetMaxForce();
  2730. }
  2731. //-----------------------------------------------------------------------------
  2732. void Scene::setMotorJointMaxTorque(
  2733. const U32 jointId,
  2734. const F32 maxTorque )
  2735. {
  2736. // Fetch joint.
  2737. b2Joint* pJoint = findJoint( jointId );
  2738. // Ignore invalid joint.
  2739. if ( !pJoint )
  2740. return;
  2741. // Fetch joint type.
  2742. const b2JointType jointType = pJoint->GetType();
  2743. if ( jointType != e_motorJoint )
  2744. {
  2745. Con::warnf( "Invalid joint type of %s.", getJointTypeDescription(jointType) );
  2746. return;
  2747. }
  2748. // Cast joint.
  2749. b2MotorJoint* pRealJoint = static_cast<b2MotorJoint*>( pJoint );
  2750. // Access joint.
  2751. pRealJoint->SetMaxTorque( maxTorque );
  2752. }
  2753. //-----------------------------------------------------------------------------
  2754. F32 Scene::getMotorJointMaxTorque( const U32 jointId )
  2755. {
  2756. // Fetch joint.
  2757. b2Joint* pJoint = findJoint( jointId );
  2758. // Ignore invalid joint.
  2759. if ( !pJoint )
  2760. return -1.0f;
  2761. // Fetch joint type.
  2762. const b2JointType jointType = pJoint->GetType();
  2763. if ( jointType != e_motorJoint )
  2764. {
  2765. Con::warnf( "Invalid joint type of %s.", getJointTypeDescription(jointType) );
  2766. return -1.0f;
  2767. }
  2768. // Cast joint.
  2769. b2MotorJoint* pRealJoint = static_cast<b2MotorJoint*>( pJoint );
  2770. // Access joint.
  2771. return pRealJoint->GetMaxTorque();
  2772. }
  2773. //-----------------------------------------------------------------------------
  2774. void Scene::setDebugSceneObject( SceneObject* pSceneObject )
  2775. {
  2776. // Ignore no change.
  2777. if ( mpDebugSceneObject == pSceneObject )
  2778. return;
  2779. // Remove delete notification for existing monitored object.
  2780. if ( mpDebugSceneObject != NULL )
  2781. clearNotify( mpDebugSceneObject );
  2782. // Set monitored scene object.
  2783. mpDebugSceneObject = pSceneObject;
  2784. // Finish if resetting monitored object.
  2785. if ( pSceneObject == NULL )
  2786. return;
  2787. // Add delete notification for new monitored object.
  2788. deleteNotify( pSceneObject );
  2789. }
  2790. //-----------------------------------------------------------------------------
  2791. void Scene::setLayerSortMode( const U32 layer, const SceneRenderQueue::RenderSort sortMode )
  2792. {
  2793. // Is the layer valid?
  2794. if ( layer > MAX_LAYERS_SUPPORTED )
  2795. {
  2796. // No, so warn.
  2797. Con::warnf( "Scene::setLayerSortMode() - Layer '%d' is out of range.", layer );
  2798. return;
  2799. }
  2800. // Is the sort mode valid?
  2801. if ( sortMode == SceneRenderQueue::RENDER_SORT_INVALID )
  2802. {
  2803. // No, so warn.
  2804. Con::warnf( "Scene::setLayerSortMode() - Sort mode is invalid for layer '%d'.", layer );
  2805. return;
  2806. }
  2807. mLayerSortModes[layer] = sortMode;
  2808. }
  2809. //-----------------------------------------------------------------------------
  2810. SceneRenderQueue::RenderSort Scene::getLayerSortMode( const U32 layer )
  2811. {
  2812. // Is the layer valid?
  2813. if ( layer > MAX_LAYERS_SUPPORTED )
  2814. {
  2815. // No, so warn.
  2816. Con::warnf( "Scene::getLayerSortMode() - Layer '%d' is out of range.", layer );
  2817. return SceneRenderQueue::RENDER_SORT_INVALID;
  2818. }
  2819. return mLayerSortModes[layer];
  2820. }
  2821. //-----------------------------------------------------------------------------
  2822. void Scene::attachSceneWindow( SceneWindow* pSceneWindow2D )
  2823. {
  2824. // Ignore if already attached.
  2825. if ( isSceneWindowAttached( pSceneWindow2D ) )
  2826. return;
  2827. // Add to Attached List.
  2828. mAttachedSceneWindows.addObject( pSceneWindow2D );
  2829. }
  2830. //-----------------------------------------------------------------------------
  2831. void Scene::detachSceneWindow( SceneWindow* pSceneWindow2D )
  2832. {
  2833. // Ignore if not attached.
  2834. if ( !isSceneWindowAttached( pSceneWindow2D ) )
  2835. return;
  2836. // Add to Attached List.
  2837. mAttachedSceneWindows.removeObject( pSceneWindow2D );
  2838. }
  2839. //-----------------------------------------------------------------------------
  2840. void Scene::detachAllSceneWindows( void )
  2841. {
  2842. // Detach All Scene Windows.
  2843. while( mAttachedSceneWindows.size() > 0 )
  2844. dynamic_cast<SceneWindow*>(mAttachedSceneWindows[mAttachedSceneWindows.size()-1])->resetScene();
  2845. }
  2846. //-----------------------------------------------------------------------------
  2847. bool Scene::isSceneWindowAttached( SceneWindow* pSceneWindow2D )
  2848. {
  2849. for( SimSet::iterator itr = mAttachedSceneWindows.begin(); itr != mAttachedSceneWindows.end(); itr++ )
  2850. if ( pSceneWindow2D == dynamic_cast<SceneWindow*>(*itr) )
  2851. // Found.
  2852. return true;
  2853. // Not Found.
  2854. return false;
  2855. }
  2856. //-----------------------------------------------------------------------------
  2857. void Scene::addDeleteRequest( SceneObject* pSceneObject )
  2858. {
  2859. // Ignore if it's already being safe-deleted.
  2860. if ( pSceneObject->isBeingDeleted() )
  2861. return;
  2862. // Populate Delete Request.
  2863. tDeleteRequest deleteRequest = { pSceneObject->getId(), NULL, false };
  2864. // Push Delete Request.
  2865. mDeleteRequests.push_back( deleteRequest );
  2866. // Flag Delete in Progress.
  2867. pSceneObject->mBeingSafeDeleted = true;
  2868. }
  2869. //-----------------------------------------------------------------------------
  2870. void Scene::processDeleteRequests( const bool forceImmediate )
  2871. {
  2872. // Ignore if there's no delete requests!
  2873. if ( mDeleteRequests.size() == 0 )
  2874. return;
  2875. // Validate All Delete Requests.
  2876. U32 safeDeleteReadyCount = 0;
  2877. for ( U32 requestIndex = 0; requestIndex < (U32)mDeleteRequests.size(); )
  2878. {
  2879. // Fetch Reference to Delete Request.
  2880. tDeleteRequest& deleteRequest = mDeleteRequests[requestIndex];
  2881. // Fetch Object.
  2882. // NOTE:- Let's be safer and check that it's definitely a scene-object.
  2883. SceneObject* pSceneObject = dynamic_cast<SceneObject*>( Sim::findObject( deleteRequest.mObjectId ) );
  2884. // Does this object exist?
  2885. if ( pSceneObject )
  2886. {
  2887. // Yes, so write object.
  2888. deleteRequest.mpSceneObject = pSceneObject;
  2889. // Calculate Safe-Ready Flag.
  2890. deleteRequest.mSafeDeleteReady = forceImmediate || pSceneObject->getSafeDelete();
  2891. // Is it ready to safe-delete?
  2892. if ( deleteRequest.mSafeDeleteReady )
  2893. {
  2894. // Yes, so increase safe-ready count.
  2895. ++safeDeleteReadyCount;
  2896. }
  2897. }
  2898. else
  2899. {
  2900. // No, so it looks like the object got deleted prematurely; let's just remove
  2901. // the request instead.
  2902. mDeleteRequests.erase_fast( requestIndex );
  2903. // Repeat this item.
  2904. continue;
  2905. }
  2906. // Skip to next request index.
  2907. ++requestIndex;
  2908. }
  2909. // Stop if there's no delete requests!
  2910. if ( mDeleteRequests.size() == 0 )
  2911. return;
  2912. // Transfer Delete-Requests to Temporary version.
  2913. // NOTE:- We do this because we may delete objects which have dependencies. This would
  2914. // cause objects to be added to the safe-delete list. We don't want to work on
  2915. // the list whilst this is happening so we'll transfer it to a temporary list.
  2916. mDeleteRequestsTemp = mDeleteRequests;
  2917. // Can we process all remaining delete-requests?
  2918. if ( safeDeleteReadyCount == (U32)mDeleteRequestsTemp.size() )
  2919. {
  2920. // Yes, so process ALL safe-ready delete-requests.
  2921. for ( U32 requestIndex = 0; requestIndex < (U32)mDeleteRequestsTemp.size(); ++requestIndex )
  2922. {
  2923. // Yes, so fetch object.
  2924. SceneObject* pSceneObject = mDeleteRequestsTemp[requestIndex].mpSceneObject;
  2925. // Do script callback.
  2926. Con::executef(this, 2, "onSafeDelete", pSceneObject->getIdString() );
  2927. // Destroy the object.
  2928. pSceneObject->deleteObject();
  2929. }
  2930. // Remove All delete-requests.
  2931. mDeleteRequestsTemp.clear();
  2932. }
  2933. else
  2934. {
  2935. // No, so process only safe-ready delete-requests.
  2936. for ( U32 requestIndex = 0; requestIndex <(U32) mDeleteRequestsTemp.size(); )
  2937. {
  2938. // Fetch Reference to Delete Request.
  2939. tDeleteRequest& deleteRequest = mDeleteRequestsTemp[requestIndex];
  2940. // Is the Object Safe-Ready?
  2941. if ( deleteRequest.mSafeDeleteReady )
  2942. {
  2943. // Yes, so fetch object.
  2944. SceneObject* pSceneObject = deleteRequest.mpSceneObject;
  2945. // Do script callback.
  2946. Con::executef(this, 2, "onSafeDelete", pSceneObject->getIdString() );
  2947. // Destroy the object.
  2948. pSceneObject->deleteObject();
  2949. // Quickly remove delete-request.
  2950. mDeleteRequestsTemp.erase_fast( requestIndex );
  2951. // Repeat this item.
  2952. continue;
  2953. }
  2954. // Skip to next request index.
  2955. ++requestIndex;
  2956. }
  2957. }
  2958. }
  2959. //-----------------------------------------------------------------------------
  2960. void Scene::SayGoodbye( b2Joint* pJoint )
  2961. {
  2962. // Find the joint id.
  2963. const U32 jointId = findJointId( pJoint );
  2964. // Ignore a bad joint.
  2965. if ( jointId == 0 )
  2966. return;
  2967. // Remove joint references.
  2968. mJoints.erase( jointId );
  2969. mReverseJoints.erase( (U32)pJoint );
  2970. }
  2971. //-----------------------------------------------------------------------------
  2972. SceneObject* Scene::create( const char* pType )
  2973. {
  2974. // Sanity!
  2975. AssertFatal( pType != NULL, "Scene::create() - Cannot create a NULL type." );
  2976. // Find the class rep.
  2977. AbstractClassRep* pClassRep = AbstractClassRep::findClassRep( pType );
  2978. // Did we find the type?
  2979. if ( pClassRep == NULL )
  2980. {
  2981. // No, so warn.
  2982. Con::warnf( "Scene::create() - Could not find type '%s' to create.", pType );
  2983. return NULL;
  2984. }
  2985. // Find the scene object rep.
  2986. AbstractClassRep* pSceneObjectRep = AbstractClassRep::findClassRep( "SceneObject" );
  2987. // Sanity!
  2988. AssertFatal( pSceneObjectRep != NULL, "Scene::create() - Could not find SceneObject class rep." );
  2989. // Is the type derived from scene object?
  2990. if ( !pClassRep->isClass( pSceneObjectRep ) )
  2991. {
  2992. // No, so warn.
  2993. Con::warnf( "Scene::create() - Type '%s' is not derived from SceneObject.", pType );
  2994. return NULL;
  2995. }
  2996. // Create the type.
  2997. SceneObject* pSceneObject = dynamic_cast<SceneObject*>( pClassRep->create() );
  2998. // Sanity!
  2999. AssertFatal( pSceneObject != NULL, "Scene::create() - Failed to create type via class rep." );
  3000. // Attempt to register the object.
  3001. if ( !pSceneObject->registerObject() )
  3002. {
  3003. // No, so warn.
  3004. Con::warnf( "Scene::create() - Failed to register type '%s'.", pType );
  3005. delete pSceneObject;
  3006. return NULL;
  3007. }
  3008. // Add to the scene.
  3009. addToScene( pSceneObject );
  3010. return pSceneObject;
  3011. }
  3012. //-----------------------------------------------------------------------------
  3013. void Scene::onTamlPreRead( void )
  3014. {
  3015. // Call parent.
  3016. Parent::onTamlPreRead();
  3017. }
  3018. //-----------------------------------------------------------------------------
  3019. void Scene::onTamlPostRead( const TamlCustomNodes& customNodes )
  3020. {
  3021. // Call parent.
  3022. Parent::onTamlPostRead( customNodes );
  3023. // Reset the loading scene.
  3024. Scene::LoadingScene = NULL;
  3025. // Find joint custom node.
  3026. const TamlCustomNode* pJointNode = customNodes.findNode( jointCustomNodeName );
  3027. // Do we have any joints?
  3028. if ( pJointNode != NULL )
  3029. {
  3030. // Yes, so fetch children joint nodes.
  3031. const TamlCustomNodeVector& jointChildren = pJointNode->getChildren();
  3032. // Iterate joints.
  3033. for( TamlCustomNodeVector::const_iterator jointNodeItr = jointChildren.begin(); jointNodeItr != jointChildren.end(); ++jointNodeItr )
  3034. {
  3035. // Fetch joint node,
  3036. TamlCustomNode* pJointNode = *jointNodeItr;
  3037. // Fetch node name.
  3038. StringTableEntry nodeName = pJointNode->getNodeName();
  3039. // Is this a distance joint?
  3040. if ( nodeName == jointDistanceNodeName )
  3041. {
  3042. // Fetch joint children.
  3043. const TamlCustomNodeVector& jointChildren = pJointNode->getChildren();
  3044. // Fetch joint objects.
  3045. SceneObject* pSceneObjectA = jointChildren.size() > 0 ? jointChildren[0]->getProxyObject<SceneObject>(true) : NULL;
  3046. SceneObject* pSceneObjectB = jointChildren.size() == 2 ? jointChildren[1]->getProxyObject<SceneObject>(true) : NULL;
  3047. // Did we get any connected objects?
  3048. if( pSceneObjectA == NULL && pSceneObjectB == NULL )
  3049. {
  3050. // No, so warn.
  3051. Con::warnf( "Scene::onTamlPostRead() - Encountered a joint '%s' but it has invalid connected objects.", nodeName );
  3052. continue;
  3053. }
  3054. b2Vec2 localAnchorA = b2Vec2_zero;
  3055. b2Vec2 localAnchorB = b2Vec2_zero;
  3056. bool collideConnected = false;
  3057. F32 length = -1.0f;
  3058. F32 frequency = 0.0f;
  3059. F32 dampingRatio = 0.0f;
  3060. // Fetch joint fields.
  3061. const TamlCustomFieldVector& jointFields = pJointNode->getFields();
  3062. // Iterate property fields.
  3063. for ( TamlCustomFieldVector::const_iterator jointFieldItr = jointFields.begin(); jointFieldItr != jointFields.end(); ++jointFieldItr )
  3064. {
  3065. // Fetch field node.
  3066. TamlCustomField* pField = *jointFieldItr;
  3067. // Fetch property field name.
  3068. StringTableEntry fieldName = pField->getFieldName();
  3069. // Fetch fields.
  3070. if ( fieldName == jointLocalAnchorAName )
  3071. {
  3072. pField->getFieldValue( localAnchorA );
  3073. }
  3074. else if ( fieldName == jointLocalAnchorBName )
  3075. {
  3076. pField->getFieldValue( localAnchorB );
  3077. }
  3078. else if ( fieldName == jointCollideConnectedName )
  3079. {
  3080. pField->getFieldValue( collideConnected );
  3081. }
  3082. else if ( fieldName == jointDistanceLengthName )
  3083. {
  3084. pField->getFieldValue( length );
  3085. }
  3086. else if ( fieldName == jointDistanceFrequencyName )
  3087. {
  3088. pField->getFieldValue( frequency );
  3089. }
  3090. else if ( fieldName == jointDistanceDampingRatioName )
  3091. {
  3092. pField->getFieldValue( dampingRatio );
  3093. }
  3094. }
  3095. // Create joint.
  3096. createDistanceJoint( pSceneObjectA, pSceneObjectB, localAnchorA, localAnchorB, length, frequency, dampingRatio, collideConnected );
  3097. }
  3098. // is this a rope joint?
  3099. else if ( nodeName == jointRopeNodeName )
  3100. {
  3101. // Fetch joint children.
  3102. const TamlCustomNodeVector& jointChildren = pJointNode->getChildren();
  3103. // Fetch joint objects.
  3104. SceneObject* pSceneObjectA = jointChildren.size() > 0 ? jointChildren[0]->getProxyObject<SceneObject>(true) : NULL;
  3105. SceneObject* pSceneObjectB = jointChildren.size() == 2 ? jointChildren[1]->getProxyObject<SceneObject>(true) : NULL;
  3106. // Did we get any connected objects?
  3107. if( pSceneObjectA == NULL && pSceneObjectB == NULL )
  3108. {
  3109. // No, so warn.
  3110. Con::warnf( "Scene::onTamlPostRead() - Encountered a joint '%s' but it has invalid connected objects.", nodeName );
  3111. continue;
  3112. }
  3113. b2Vec2 localAnchorA = b2Vec2_zero;
  3114. b2Vec2 localAnchorB = b2Vec2_zero;
  3115. bool collideConnected = false;
  3116. F32 maxLength = -1.0f;
  3117. // Fetch joint fields.
  3118. const TamlCustomFieldVector& jointFields = pJointNode->getFields();
  3119. // Iterate property fields.
  3120. for ( TamlCustomFieldVector::const_iterator jointFieldItr = jointFields.begin(); jointFieldItr != jointFields.end(); ++jointFieldItr )
  3121. {
  3122. // Fetch field node.
  3123. TamlCustomField* pField = *jointFieldItr;
  3124. // Fetch property field name.
  3125. StringTableEntry fieldName = pField->getFieldName();
  3126. // Fetch fields.
  3127. if ( fieldName == jointLocalAnchorAName )
  3128. {
  3129. pField->getFieldValue( localAnchorA );
  3130. }
  3131. else if ( fieldName == jointLocalAnchorBName )
  3132. {
  3133. pField->getFieldValue( localAnchorB );
  3134. }
  3135. else if ( fieldName == jointCollideConnectedName )
  3136. {
  3137. pField->getFieldValue( collideConnected );
  3138. }
  3139. else if ( fieldName == jointRopeMaxLengthName )
  3140. {
  3141. pField->getFieldValue( maxLength );
  3142. }
  3143. }
  3144. // Create joint.
  3145. createRopeJoint( pSceneObjectA, pSceneObjectB, localAnchorA, localAnchorB, maxLength, collideConnected );
  3146. }
  3147. // Is this a revolute joint?
  3148. else if ( nodeName == jointRevoluteNodeName )
  3149. {
  3150. // Fetch joint children.
  3151. const TamlCustomNodeVector& jointChildren = pJointNode->getChildren();
  3152. // Fetch joint objects.
  3153. SceneObject* pSceneObjectA = jointChildren.size() > 0 ? jointChildren[0]->getProxyObject<SceneObject>(true) : NULL;
  3154. SceneObject* pSceneObjectB = jointChildren.size() == 2 ? jointChildren[1]->getProxyObject<SceneObject>(true) : NULL;
  3155. // Did we get any connected objects?
  3156. if( pSceneObjectA == NULL && pSceneObjectB == NULL )
  3157. {
  3158. // No, so warn.
  3159. Con::warnf( "Scene::onTamlPostRead() - Encountered a joint '%s' but it has invalid connected objects.", nodeName );
  3160. continue;
  3161. }
  3162. b2Vec2 localAnchorA = b2Vec2_zero;
  3163. b2Vec2 localAnchorB = b2Vec2_zero;
  3164. bool collideConnected = false;
  3165. bool enableLimit = false;
  3166. F32 lowerAngle = 0.0f;
  3167. F32 upperAngle = 0.0f;
  3168. bool enableMotor = false;
  3169. F32 motorSpeed = b2_pi;
  3170. F32 maxMotorTorque = 0.0f;
  3171. // Fetch joint fields.
  3172. const TamlCustomFieldVector& jointFields = pJointNode->getFields();
  3173. // Iterate property fields.
  3174. for ( TamlCustomFieldVector::const_iterator jointFieldItr = jointFields.begin(); jointFieldItr != jointFields.end(); ++jointFieldItr )
  3175. {
  3176. // Fetch field node.
  3177. TamlCustomField* pField = *jointFieldItr;
  3178. // Fetch property field name.
  3179. StringTableEntry fieldName = pField->getFieldName();
  3180. // Fetch fields.
  3181. if ( fieldName == jointLocalAnchorAName )
  3182. {
  3183. pField->getFieldValue( localAnchorA );
  3184. }
  3185. else if ( fieldName == jointLocalAnchorBName )
  3186. {
  3187. pField->getFieldValue( localAnchorB );
  3188. }
  3189. else if ( fieldName == jointCollideConnectedName )
  3190. {
  3191. pField->getFieldValue( collideConnected );
  3192. }
  3193. else if ( fieldName == jointRevoluteLimitLowerAngleName )
  3194. {
  3195. pField->getFieldValue( lowerAngle );
  3196. lowerAngle = mDegToRad( lowerAngle );
  3197. enableLimit = true;
  3198. }
  3199. else if ( fieldName == jointRevoluteLimitUpperAngleName )
  3200. {
  3201. pField->getFieldValue( upperAngle );
  3202. upperAngle = mDegToRad( upperAngle );
  3203. enableLimit = true;
  3204. }
  3205. else if ( fieldName == jointRevoluteMotorSpeedName )
  3206. {
  3207. pField->getFieldValue( motorSpeed );
  3208. motorSpeed = mDegToRad( motorSpeed );
  3209. enableMotor = true;
  3210. }
  3211. else if ( fieldName == jointRevoluteMotorMaxTorqueName )
  3212. {
  3213. pField->getFieldValue( maxMotorTorque );
  3214. enableMotor = true;
  3215. }
  3216. }
  3217. // Create joint.
  3218. const U32 jointId = createRevoluteJoint( pSceneObjectA, pSceneObjectB, localAnchorA, localAnchorB, collideConnected );
  3219. if ( enableLimit )
  3220. setRevoluteJointLimit( jointId, true, lowerAngle, upperAngle );
  3221. if ( enableMotor )
  3222. setRevoluteJointMotor( jointId, true, motorSpeed, maxMotorTorque );
  3223. }
  3224. // is this a weld joint?
  3225. else if ( nodeName == jointWeldNodeName )
  3226. {
  3227. // Fetch joint children.
  3228. const TamlCustomNodeVector& jointChildren = pJointNode->getChildren();
  3229. // Fetch joint objects.
  3230. SceneObject* pSceneObjectA = jointChildren.size() > 0 ? jointChildren[0]->getProxyObject<SceneObject>(true) : NULL;
  3231. SceneObject* pSceneObjectB = jointChildren.size() == 2 ? jointChildren[1]->getProxyObject<SceneObject>(true) : NULL;
  3232. // Did we get any connected objects?
  3233. if( pSceneObjectA == NULL && pSceneObjectB == NULL )
  3234. {
  3235. // No, so warn.
  3236. Con::warnf( "Scene::onTamlPostRead() - Encountered a joint '%s' but it has invalid connected objects.", nodeName );
  3237. continue;
  3238. }
  3239. b2Vec2 localAnchorA = b2Vec2_zero;
  3240. b2Vec2 localAnchorB = b2Vec2_zero;
  3241. bool collideConnected = false;
  3242. F32 frequency = 0.0f;
  3243. F32 dampingRatio = 0.0f;
  3244. // Fetch joint fields.
  3245. const TamlCustomFieldVector& jointFields = pJointNode->getFields();
  3246. // Iterate property fields.
  3247. for ( TamlCustomFieldVector::const_iterator jointFieldItr = jointFields.begin(); jointFieldItr != jointFields.end(); ++jointFieldItr )
  3248. {
  3249. // Fetch field node.
  3250. TamlCustomField* pField = *jointFieldItr;
  3251. // Fetch property field name.
  3252. StringTableEntry fieldName = pField->getFieldName();
  3253. // Fetch fields.
  3254. if ( fieldName == jointLocalAnchorAName )
  3255. {
  3256. pField->getFieldValue( localAnchorA );
  3257. }
  3258. else if ( fieldName == jointLocalAnchorBName )
  3259. {
  3260. pField->getFieldValue( localAnchorB );
  3261. }
  3262. else if ( fieldName == jointCollideConnectedName )
  3263. {
  3264. pField->getFieldValue( collideConnected );
  3265. }
  3266. else if ( fieldName == jointWeldFrequencyName )
  3267. {
  3268. pField->getFieldValue( frequency );
  3269. }
  3270. else if ( fieldName == jointWeldDampingRatioName )
  3271. {
  3272. pField->getFieldValue( dampingRatio );
  3273. }
  3274. }
  3275. // Create joint.
  3276. createWeldJoint( pSceneObjectA, pSceneObjectB, localAnchorA, localAnchorB, frequency, dampingRatio, collideConnected );
  3277. }
  3278. // Is this a wheel joint?
  3279. else if ( nodeName == jointWheelNodeName )
  3280. {
  3281. // Fetch joint children.
  3282. const TamlCustomNodeVector& jointChildren = pJointNode->getChildren();
  3283. // Fetch joint objects.
  3284. SceneObject* pSceneObjectA = jointChildren.size() > 0 ? jointChildren[0]->getProxyObject<SceneObject>(true) : NULL;
  3285. SceneObject* pSceneObjectB = jointChildren.size() == 2 ? jointChildren[1]->getProxyObject<SceneObject>(true) : NULL;
  3286. // Did we get any connected objects?
  3287. if( pSceneObjectA == NULL && pSceneObjectB == NULL )
  3288. {
  3289. // No, so warn.
  3290. Con::warnf( "Scene::onTamlPostRead() - Encountered a joint '%s' but it has invalid connected objects.", nodeName );
  3291. continue;
  3292. }
  3293. b2Vec2 localAnchorA = b2Vec2_zero;
  3294. b2Vec2 localAnchorB = b2Vec2_zero;
  3295. bool collideConnected = false;
  3296. bool enableMotor = false;
  3297. F32 motorSpeed = b2_pi;
  3298. F32 maxMotorTorque = 0.0f;
  3299. F32 frequency = 0.0f;
  3300. F32 dampingRatio = 0.0f;
  3301. b2Vec2 worldAxis( 0.0f, 1.0f );
  3302. // Fetch joint fields.
  3303. const TamlCustomFieldVector& jointFields = pJointNode->getFields();
  3304. // Iterate property fields.
  3305. for ( TamlCustomFieldVector::const_iterator jointFieldItr = jointFields.begin(); jointFieldItr != jointFields.end(); ++jointFieldItr )
  3306. {
  3307. // Fetch field node.
  3308. TamlCustomField* pField = *jointFieldItr;
  3309. // Fetch property field name.
  3310. StringTableEntry fieldName = pField->getFieldName();
  3311. // Fetch fields.
  3312. if ( fieldName == jointLocalAnchorAName )
  3313. {
  3314. pField->getFieldValue( localAnchorA );
  3315. }
  3316. else if ( fieldName == jointLocalAnchorBName )
  3317. {
  3318. pField->getFieldValue( localAnchorB );
  3319. }
  3320. else if ( fieldName == jointCollideConnectedName )
  3321. {
  3322. pField->getFieldValue( collideConnected );
  3323. }
  3324. else if ( fieldName == jointWheelMotorSpeedName )
  3325. {
  3326. pField->getFieldValue( motorSpeed );
  3327. motorSpeed = mDegToRad( motorSpeed );
  3328. enableMotor = true;
  3329. }
  3330. else if ( fieldName == jointWheelMotorMaxTorqueName )
  3331. {
  3332. pField->getFieldValue( maxMotorTorque );
  3333. enableMotor = true;
  3334. }
  3335. else if ( fieldName == jointWheelFrequencyName )
  3336. {
  3337. pField->getFieldValue( frequency );
  3338. }
  3339. else if ( fieldName == jointWheelDampingRatioName )
  3340. {
  3341. pField->getFieldValue( dampingRatio );
  3342. }
  3343. else if ( fieldName == jointWheelWorldAxisName )
  3344. {
  3345. pField->getFieldValue( worldAxis );
  3346. }
  3347. }
  3348. // Create joint.
  3349. const U32 jointId = createWheelJoint( pSceneObjectA, pSceneObjectB, localAnchorA, localAnchorB, worldAxis, collideConnected );
  3350. if ( enableMotor )
  3351. setWheelJointMotor( jointId, true, motorSpeed, maxMotorTorque );
  3352. setWheelJointFrequency( jointId, frequency );
  3353. setWheelJointDampingRatio( jointId, dampingRatio );
  3354. }
  3355. // Is this a friction joint?
  3356. else if ( nodeName == jointFrictionNodeName )
  3357. {
  3358. // Fetch joint children.
  3359. const TamlCustomNodeVector& jointChildren = pJointNode->getChildren();
  3360. // Fetch joint objects.
  3361. SceneObject* pSceneObjectA = jointChildren.size() > 0 ? jointChildren[0]->getProxyObject<SceneObject>(true) : NULL;
  3362. SceneObject* pSceneObjectB = jointChildren.size() == 2 ? jointChildren[1]->getProxyObject<SceneObject>(true) : NULL;
  3363. // Did we get any connected objects?
  3364. if( pSceneObjectA == NULL && pSceneObjectB == NULL )
  3365. {
  3366. // No, so warn.
  3367. Con::warnf( "Scene::onTamlPostRead() - Encountered a joint '%s' but it has invalid connected objects.", nodeName );
  3368. continue;
  3369. }
  3370. b2Vec2 localAnchorA = b2Vec2_zero;
  3371. b2Vec2 localAnchorB = b2Vec2_zero;
  3372. bool collideConnected = false;
  3373. F32 maxForce = 0.0f;
  3374. F32 maxTorque = 0.0f;
  3375. // Fetch joint fields.
  3376. const TamlCustomFieldVector& jointFields = pJointNode->getFields();
  3377. // Iterate property fields.
  3378. for ( TamlCustomFieldVector::const_iterator jointFieldItr = jointFields.begin(); jointFieldItr != jointFields.end(); ++jointFieldItr )
  3379. {
  3380. // Fetch field node.
  3381. TamlCustomField* pField = *jointFieldItr;
  3382. // Fetch property field name.
  3383. StringTableEntry fieldName = pField->getFieldName();
  3384. // Fetch fields.
  3385. if ( fieldName == jointLocalAnchorAName )
  3386. {
  3387. pField->getFieldValue( localAnchorA );
  3388. }
  3389. else if ( fieldName == jointLocalAnchorBName )
  3390. {
  3391. pField->getFieldValue( localAnchorB );
  3392. }
  3393. else if ( fieldName == jointCollideConnectedName )
  3394. {
  3395. pField->getFieldValue( collideConnected );
  3396. }
  3397. else if ( fieldName == jointFrictionMaxForceName )
  3398. {
  3399. pField->getFieldValue( maxForce );
  3400. }
  3401. else if ( fieldName == jointFrictionMaxTorqueName )
  3402. {
  3403. pField->getFieldValue( maxTorque );
  3404. }
  3405. }
  3406. // Create joint.
  3407. createFrictionJoint( pSceneObjectA, pSceneObjectB, localAnchorA, localAnchorB, maxForce, maxTorque, collideConnected );
  3408. }
  3409. // Is this a prismatic joint?
  3410. else if ( nodeName == jointPrismaticNodeName )
  3411. {
  3412. // Fetch joint children.
  3413. const TamlCustomNodeVector& jointChildren = pJointNode->getChildren();
  3414. // Fetch joint objects.
  3415. SceneObject* pSceneObjectA = jointChildren.size() > 0 ? jointChildren[0]->getProxyObject<SceneObject>(true) : NULL;
  3416. SceneObject* pSceneObjectB = jointChildren.size() == 2 ? jointChildren[1]->getProxyObject<SceneObject>(true) : NULL;
  3417. // Did we get any connected objects?
  3418. if( pSceneObjectA == NULL && pSceneObjectB == NULL )
  3419. {
  3420. // No, so warn.
  3421. Con::warnf( "Scene::onTamlPostRead() - Encountered a joint '%s' but it has invalid connected objects.", nodeName );
  3422. continue;
  3423. }
  3424. b2Vec2 localAnchorA = b2Vec2_zero;
  3425. b2Vec2 localAnchorB = b2Vec2_zero;
  3426. bool collideConnected = false;
  3427. bool enableLimit;
  3428. F32 lowerTransLimit = 0.0f;
  3429. F32 upperTransLimit = 1.0f;
  3430. bool enableMotor = false;
  3431. F32 motorSpeed = b2_pi;
  3432. F32 maxMotorForce = 0.0f;
  3433. b2Vec2 worldAxis( 0.0f, 1.0f );
  3434. // Fetch joint fields.
  3435. const TamlCustomFieldVector& jointFields = pJointNode->getFields();
  3436. // Iterate property fields.
  3437. for ( TamlCustomFieldVector::const_iterator jointFieldItr = jointFields.begin(); jointFieldItr != jointFields.end(); ++jointFieldItr )
  3438. {
  3439. // Fetch field node.
  3440. TamlCustomField* pField = *jointFieldItr;
  3441. // Fetch property field name.
  3442. StringTableEntry fieldName = pField->getFieldName();
  3443. // Fetch fields.
  3444. if ( fieldName == jointLocalAnchorAName )
  3445. {
  3446. pField->getFieldValue( localAnchorA );
  3447. }
  3448. else if ( fieldName == jointLocalAnchorBName )
  3449. {
  3450. pField->getFieldValue( localAnchorB );
  3451. }
  3452. else if ( fieldName == jointCollideConnectedName )
  3453. {
  3454. pField->getFieldValue( collideConnected );
  3455. }
  3456. else if ( fieldName == jointPrismaticLimitLowerTransName )
  3457. {
  3458. pField->getFieldValue( lowerTransLimit );
  3459. enableLimit = true;
  3460. }
  3461. else if ( fieldName == jointPrismaticLimitUpperTransName )
  3462. {
  3463. pField->getFieldValue( upperTransLimit );
  3464. enableLimit = true;
  3465. }
  3466. else if ( fieldName == jointPrismaticMotorSpeedName )
  3467. {
  3468. pField->getFieldValue( motorSpeed );
  3469. motorSpeed = mDegToRad( motorSpeed );
  3470. enableMotor = true;
  3471. }
  3472. else if ( fieldName == jointPrismaticMotorMaxForceName )
  3473. {
  3474. pField->getFieldValue( maxMotorForce );
  3475. enableMotor = true;
  3476. }
  3477. else if ( fieldName == jointPrismaticWorldAxisName )
  3478. {
  3479. pField->getFieldValue( worldAxis );
  3480. }
  3481. }
  3482. // Create joint.
  3483. const U32 jointId = createPrismaticJoint( pSceneObjectA, pSceneObjectB, localAnchorA, localAnchorB, worldAxis, collideConnected );
  3484. if ( enableLimit )
  3485. setPrismaticJointLimit( jointId, true, lowerTransLimit, upperTransLimit );
  3486. if ( enableMotor )
  3487. setPrismaticJointMotor( jointId, true, motorSpeed, maxMotorForce );
  3488. }
  3489. // Is this a pulley joint?
  3490. else if ( nodeName == jointPulleyNodeName )
  3491. {
  3492. // Fetch joint children.
  3493. const TamlCustomNodeVector& jointChildren = pJointNode->getChildren();
  3494. // Fetch joint objects.
  3495. SceneObject* pSceneObjectA = jointChildren.size() > 0 ? jointChildren[0]->getProxyObject<SceneObject>(true) : NULL;
  3496. SceneObject* pSceneObjectB = jointChildren.size() == 2 ? jointChildren[1]->getProxyObject<SceneObject>(true) : NULL;
  3497. // Did we get any connected objects?
  3498. if( pSceneObjectA == NULL && pSceneObjectB == NULL )
  3499. {
  3500. // No, so warn.
  3501. Con::warnf( "Scene::onTamlPostRead() - Encountered a joint '%s' but it has invalid connected objects.", nodeName );
  3502. continue;
  3503. }
  3504. b2Vec2 localAnchorA = b2Vec2_zero;
  3505. b2Vec2 localAnchorB = b2Vec2_zero;
  3506. bool collideConnected = false;
  3507. F32 lengthA = -1.0f;
  3508. F32 lengthB = -1.0f;
  3509. F32 ratio = 0.5f;
  3510. b2Vec2 worldGroundAnchorA = b2Vec2_zero;
  3511. b2Vec2 worldGroundAnchorB = b2Vec2_zero;
  3512. // Fetch joint fields.
  3513. const TamlCustomFieldVector& jointFields = pJointNode->getFields();
  3514. // Iterate property fields.
  3515. for ( TamlCustomFieldVector::const_iterator jointFieldItr = jointFields.begin(); jointFieldItr != jointFields.end(); ++jointFieldItr )
  3516. {
  3517. // Fetch field node.
  3518. TamlCustomField* pField = *jointFieldItr;
  3519. // Fetch property field name.
  3520. StringTableEntry fieldName = pField->getFieldName();
  3521. // Fetch fields.
  3522. if ( fieldName == jointLocalAnchorAName )
  3523. {
  3524. pField->getFieldValue( localAnchorA );
  3525. }
  3526. else if ( fieldName == jointLocalAnchorBName )
  3527. {
  3528. pField->getFieldValue( localAnchorB );
  3529. }
  3530. else if ( fieldName == jointCollideConnectedName )
  3531. {
  3532. pField->getFieldValue( collideConnected );
  3533. }
  3534. else if ( fieldName == jointPulleyLengthAName )
  3535. {
  3536. pField->getFieldValue( lengthA );
  3537. }
  3538. else if ( fieldName == jointPulleyLengthBName )
  3539. {
  3540. pField->getFieldValue( lengthB );
  3541. }
  3542. else if ( fieldName == jointPulleyRatioName )
  3543. {
  3544. pField->getFieldValue( ratio );
  3545. }
  3546. else if ( fieldName == jointPulleyGroundAnchorAName )
  3547. {
  3548. pField->getFieldValue( worldGroundAnchorA );
  3549. }
  3550. else if ( fieldName == jointPulleyGroundAnchorBName )
  3551. {
  3552. pField->getFieldValue( worldGroundAnchorB );
  3553. }
  3554. }
  3555. // Create joint.
  3556. createPulleyJoint( pSceneObjectA, pSceneObjectB, localAnchorA, localAnchorB, worldGroundAnchorA, worldGroundAnchorB, ratio, lengthA, lengthB, collideConnected );
  3557. }
  3558. // Is this a target joint?
  3559. else if ( nodeName == jointTargetNodeName )
  3560. {
  3561. // Fetch joint children.
  3562. const TamlCustomNodeVector& jointChildren = pJointNode->getChildren();
  3563. // Fetch joint objects.
  3564. SceneObject* pSceneObject = jointChildren.size() == 1 ? jointChildren[0]->getProxyObject<SceneObject>(true) : NULL;
  3565. // Did we get any connected objects?
  3566. if( pSceneObject == NULL )
  3567. {
  3568. // No, so warn.
  3569. Con::warnf( "Scene::onTamlPostRead() - Encountered a joint '%s' but it has an invalid connected object.", nodeName );
  3570. continue;
  3571. }
  3572. bool collideConnected = false;
  3573. b2Vec2 worldTarget = b2Vec2_zero;
  3574. F32 maxForce = 1.0f;
  3575. F32 frequency = 5.0f;
  3576. F32 dampingRatio = 0.7f;
  3577. // Fetch joint fields.
  3578. const TamlCustomFieldVector& jointFields = pJointNode->getFields();
  3579. // Iterate property fields.
  3580. for ( TamlCustomFieldVector::const_iterator jointFieldItr = jointFields.begin(); jointFieldItr != jointFields.end(); ++jointFieldItr )
  3581. {
  3582. // Fetch field node.
  3583. TamlCustomField* pField = *jointFieldItr;
  3584. // Fetch property field name.
  3585. StringTableEntry fieldName = pField->getFieldName();
  3586. // Fetch fields.
  3587. if ( fieldName == jointCollideConnectedName )
  3588. {
  3589. pField->getFieldValue( collideConnected );
  3590. }
  3591. else if ( fieldName == jointTargetWorldTargetName )
  3592. {
  3593. pField->getFieldValue( worldTarget );
  3594. }
  3595. else if ( fieldName == jointTargetMaxForceName )
  3596. {
  3597. pField->getFieldValue( maxForce );
  3598. }
  3599. else if ( fieldName == jointTargetFrequencyName )
  3600. {
  3601. pField->getFieldValue( frequency );
  3602. }
  3603. else if ( fieldName == jointTargetDampingRatioName )
  3604. {
  3605. pField->getFieldValue( dampingRatio );
  3606. }
  3607. }
  3608. // Create joint.
  3609. createTargetJoint( pSceneObject, worldTarget, maxForce, frequency, dampingRatio, collideConnected );
  3610. }
  3611. // Is this a motor joint?
  3612. else if ( nodeName == jointMotorNodeName )
  3613. {
  3614. // Fetch joint children.
  3615. const TamlCustomNodeVector& jointChildren = pJointNode->getChildren();
  3616. // Fetch joint objects.
  3617. SceneObject* pSceneObjectA = jointChildren.size() > 0 ? jointChildren[0]->getProxyObject<SceneObject>(true) : NULL;
  3618. SceneObject* pSceneObjectB = jointChildren.size() == 2 ? jointChildren[1]->getProxyObject<SceneObject>(true) : NULL;
  3619. // Did we get any connected objects?
  3620. if( pSceneObjectA == NULL && pSceneObjectB == NULL )
  3621. {
  3622. // No, so warn.
  3623. Con::warnf( "Scene::onTamlPostRead() - Encountered a joint '%s' but it has invalid connected objects.", nodeName );
  3624. continue;
  3625. }
  3626. bool collideConnected = false;
  3627. b2Vec2 linearOffset = b2Vec2_zero;
  3628. F32 angularOffset = 0.0f;
  3629. F32 maxForce = 1.0f;
  3630. F32 maxTorque = 1.0f;
  3631. F32 correctionFactor = 0.3f;
  3632. // Fetch joint fields.
  3633. const TamlCustomFieldVector& jointFields = pJointNode->getFields();
  3634. // Iterate property fields.
  3635. for ( TamlCustomFieldVector::const_iterator jointFieldItr = jointFields.begin(); jointFieldItr != jointFields.end(); ++jointFieldItr )
  3636. {
  3637. // Fetch field node.
  3638. TamlCustomField* pField = *jointFieldItr;
  3639. // Fetch property field name.
  3640. StringTableEntry fieldName = pField->getFieldName();
  3641. // Fetch fields.
  3642. if ( fieldName == jointCollideConnectedName )
  3643. {
  3644. pField->getFieldValue( collideConnected );
  3645. }
  3646. else if ( fieldName == jointMotorLinearOffsetName )
  3647. {
  3648. pField->getFieldValue( linearOffset );
  3649. }
  3650. else if ( fieldName == jointMotorAngularOffsetName )
  3651. {
  3652. pField->getFieldValue( angularOffset );
  3653. angularOffset = mDegToRad( angularOffset );
  3654. }
  3655. else if ( fieldName == jointMotorMaxForceName )
  3656. {
  3657. pField->getFieldValue( maxForce );
  3658. }
  3659. else if ( fieldName == jointMotorMaxTorqueName )
  3660. {
  3661. pField->getFieldValue( maxTorque );
  3662. }
  3663. else if ( fieldName == jointMotorCorrectionFactorName )
  3664. {
  3665. pField->getFieldValue( correctionFactor );
  3666. }
  3667. }
  3668. // Create joint.
  3669. createMotorJoint( pSceneObjectA, pSceneObjectB, linearOffset, angularOffset, maxForce, maxTorque, correctionFactor, collideConnected );
  3670. }
  3671. // Unknown joint type!
  3672. else
  3673. {
  3674. // Warn.
  3675. Con::warnf( "Unknown joint type of '%s' encountered.", nodeName );
  3676. // Sanity!
  3677. AssertFatal( false, "Scene::onTamlCustomRead() - Unknown joint type detected." );
  3678. continue;
  3679. }
  3680. }
  3681. }
  3682. // Find controller custom node.
  3683. const TamlCustomNode* pControllerNode = customNodes.findNode( controllerCustomNodeName );
  3684. // Do we have any controllers?
  3685. if ( pControllerNode != NULL )
  3686. {
  3687. // Yes, so fetch the scene controllers.
  3688. SimSet* pControllerSet = getControllers();
  3689. // Fetch children controller nodes.
  3690. const TamlCustomNodeVector& controllerChildren = pControllerNode->getChildren();
  3691. // Iterate controllers.
  3692. for( TamlCustomNodeVector::const_iterator controllerNodeItr = controllerChildren.begin(); controllerNodeItr != controllerChildren.end(); ++controllerNodeItr )
  3693. {
  3694. // Is the node a proxy object?
  3695. if ( !pControllerNode->isProxyObject() )
  3696. {
  3697. // No, so warn.
  3698. Con::warnf("Scene::onTamlPostRead() - Reading scene controllers but node '%s'is not an object.", pControllerNode->getNodeName() );
  3699. continue;
  3700. }
  3701. // Add the proxy object.
  3702. SimObject* pProxyObject = pControllerNode->getProxyObject<SimObject>(false);
  3703. // Is it a scene controller?
  3704. if ( dynamic_cast<SceneController*>( pProxyObject ) == NULL )
  3705. {
  3706. // No, so warn.
  3707. Con::warnf("Scene::onTamlPostRead() - Reading scene controllers but node '%s'is not a scene controller.", pControllerNode->getNodeName() );
  3708. // Delete the object.
  3709. pProxyObject->deleteObject();
  3710. continue;
  3711. }
  3712. // Add to scene controllers.
  3713. pControllerSet->addObject( pProxyObject );
  3714. }
  3715. }
  3716. // Find asset preload custom node.
  3717. const TamlCustomNode* pAssetPreloadNode = customNodes.findNode( assetPreloadNodeName );
  3718. // Do we have any asset preloads?
  3719. if ( pAssetPreloadNode != NULL )
  3720. {
  3721. // Yes, so clear any existing asset preloads.
  3722. clearAssetPreloads();
  3723. // Yes, so fetch asset Id type prefix.
  3724. StringTableEntry assetIdTypePrefix = ConsoleBaseType::getType( TypeAssetId )->getTypePrefix();
  3725. // Fetch the prefix length.
  3726. const S32 assetIdPrefixLength = dStrlen( assetIdTypePrefix );
  3727. // Fetch the preload children nodes.
  3728. const TamlCustomNodeVector& preloadChildren = pAssetPreloadNode->getChildren();
  3729. // Iterate asset preloads.
  3730. for( TamlCustomNodeVector::const_iterator assetPreloadNodeItr = preloadChildren.begin(); assetPreloadNodeItr != preloadChildren.end(); ++assetPreloadNodeItr )
  3731. {
  3732. // Fetch asset node.
  3733. const TamlCustomNode* pAssetNode = *assetPreloadNodeItr;
  3734. // Ignore non-asset nodes.
  3735. if ( pAssetNode->getNodeName() != assetNodeName )
  3736. continue;
  3737. // Find the asset Id field.
  3738. const TamlCustomField* pAssetIdField = pAssetNode->findField( "Id" );
  3739. // Did we find the field?
  3740. if ( pAssetIdField == NULL )
  3741. {
  3742. // No, so warn.
  3743. Con::warnf("Scene::onTamlPostRead() - Found asset preload but failed to find asset Id field." );
  3744. continue;
  3745. }
  3746. // Fetch field value.
  3747. const char* pFieldValue = pAssetIdField->getFieldValue();
  3748. // Calculate the field value start (skip any type prefix).
  3749. const S32 prefixOffset = dStrnicmp( pFieldValue, assetIdTypePrefix, assetIdPrefixLength ) == 0 ? assetIdPrefixLength : 0;
  3750. // Add asset preload.
  3751. addAssetPreload( pFieldValue + prefixOffset );
  3752. }
  3753. }
  3754. }
  3755. //-----------------------------------------------------------------------------
  3756. void Scene::onTamlCustomWrite( TamlCustomNodes& customNodes )
  3757. {
  3758. // Call parent.
  3759. Parent::onTamlCustomWrite( customNodes );
  3760. // Fetch joint count.
  3761. const U32 jointCount = getJointCount();
  3762. // Do we have any joints?
  3763. if ( jointCount > 0 )
  3764. {
  3765. // Yes, so add joint custom node.
  3766. TamlCustomNode* pJointCustomNode = customNodes.addNode( jointCustomNodeName );
  3767. // Iterate joints.
  3768. for( typeJointHash::iterator jointItr = mJoints.begin(); jointItr != mJoints.end(); ++jointItr )
  3769. {
  3770. // Fetch base joint.
  3771. b2Joint* pBaseJoint = jointItr->value;
  3772. // Add joint node.
  3773. // NOTE: The name of the node will get updated shortly.
  3774. TamlCustomNode* pJointNode = pJointCustomNode->addNode( StringTable->EmptyString );
  3775. // Fetch common details.
  3776. b2Body* pBodyA = pBaseJoint->GetBodyA();
  3777. b2Body* pBodyB = pBaseJoint->GetBodyB();
  3778. // Fetch physics proxies.
  3779. PhysicsProxy* pPhysicsProxyA = static_cast<PhysicsProxy*>(pBodyA->GetUserData());
  3780. PhysicsProxy* pPhysicsProxyB = static_cast<PhysicsProxy*>(pBodyB->GetUserData());
  3781. // Fetch physics proxy type.
  3782. PhysicsProxy::ePhysicsProxyType proxyTypeA = static_cast<PhysicsProxy*>(pBodyA->GetUserData())->getPhysicsProxyType();
  3783. PhysicsProxy::ePhysicsProxyType proxyTypeB = static_cast<PhysicsProxy*>(pBodyB->GetUserData())->getPhysicsProxyType();
  3784. // Fetch scene objects.
  3785. SceneObject* pSceneObjectA = proxyTypeA == PhysicsProxy::PHYSIC_PROXY_SCENEOBJECT ? static_cast<SceneObject*>(pPhysicsProxyA) : NULL;
  3786. SceneObject* pSceneObjectB = proxyTypeB == PhysicsProxy::PHYSIC_PROXY_SCENEOBJECT ? static_cast<SceneObject*>(pPhysicsProxyB) : NULL;
  3787. // Populate joint appropriately.
  3788. switch( pBaseJoint->GetType() )
  3789. {
  3790. case e_distanceJoint:
  3791. {
  3792. // Set joint name.
  3793. pJointNode->setNodeName( jointDistanceNodeName );
  3794. // Fetch joint.
  3795. const b2DistanceJoint* pJoint = dynamic_cast<const b2DistanceJoint*>( pBaseJoint );
  3796. // Sanity!
  3797. AssertFatal( pJoint != NULL, "Scene::onTamlCustomWrite() - Invalid distance joint type returned." );
  3798. // Add length.
  3799. pJointNode->addField( jointDistanceLengthName, pJoint->GetLength() );
  3800. // Add frequency.
  3801. if ( mNotZero( pJoint->GetFrequency() ) )
  3802. pJointNode->addField( jointDistanceFrequencyName, pJoint->GetFrequency() );
  3803. // Add damping ratio.
  3804. if ( mNotZero( pJoint->GetDampingRatio() ) )
  3805. pJointNode->addField( jointDistanceDampingRatioName, pJoint->GetDampingRatio() );
  3806. // Add local anchors.
  3807. if ( mNotZero( pJoint->GetLocalAnchorA().LengthSquared() ) )
  3808. pJointNode->addField( jointLocalAnchorAName, pJoint->GetLocalAnchorA() );
  3809. if ( mNotZero( pJoint->GetLocalAnchorB().LengthSquared() ) )
  3810. pJointNode->addField( jointLocalAnchorBName, pJoint->GetLocalAnchorB() );
  3811. // Add scene object bodies.
  3812. if ( pSceneObjectA != NULL )
  3813. pJointNode->addNode( pSceneObjectA );
  3814. if ( pSceneObjectB != NULL )
  3815. pJointNode->addNode( pSceneObjectB );
  3816. }
  3817. break;
  3818. case e_ropeJoint:
  3819. {
  3820. // Set joint name.
  3821. pJointNode->setNodeName( jointRopeNodeName );
  3822. // Fetch joint.
  3823. const b2RopeJoint* pJoint = dynamic_cast<const b2RopeJoint*>( pBaseJoint );
  3824. // Sanity!
  3825. AssertFatal( pJoint != NULL, "Scene::onTamlCustomWrite() - Invalid rope joint type returned." );
  3826. // Add max length.
  3827. if ( mNotZero( pJoint->GetMaxLength() ) )
  3828. pJointNode->addField( jointRopeMaxLengthName, pJoint->GetMaxLength() );
  3829. // Add local anchors.
  3830. if ( mNotZero( pJoint->GetLocalAnchorA().LengthSquared() ) )
  3831. pJointNode->addField( jointLocalAnchorAName, pJoint->GetLocalAnchorA() );
  3832. if ( mNotZero( pJoint->GetLocalAnchorB().LengthSquared() ) )
  3833. pJointNode->addField( jointLocalAnchorBName, pJoint->GetLocalAnchorB() );
  3834. // Add scene object bodies.
  3835. if ( pSceneObjectA != NULL )
  3836. pJointNode->addNode( pSceneObjectA );
  3837. if ( pSceneObjectB != NULL )
  3838. pJointNode->addNode( pSceneObjectB );
  3839. }
  3840. break;
  3841. case e_revoluteJoint:
  3842. {
  3843. // Set join name.
  3844. pJointNode->setNodeName( jointRevoluteNodeName );
  3845. // Fetch joint.
  3846. const b2RevoluteJoint* pJoint = dynamic_cast<const b2RevoluteJoint*>( pBaseJoint );
  3847. // Sanity!
  3848. AssertFatal( pJoint != NULL, "Scene::onTamlCustomWrite() - Invalid revolute joint type returned." );
  3849. // Add limit.
  3850. if ( pJoint->IsLimitEnabled() )
  3851. {
  3852. // Add limits.
  3853. pJointNode->addField( jointRevoluteLimitLowerAngleName, mRadToDeg(pJoint->GetLowerLimit()) );
  3854. pJointNode->addField( jointRevoluteLimitUpperAngleName, mRadToDeg(pJoint->GetUpperLimit()) );
  3855. }
  3856. // Add motor.
  3857. if ( pJoint->IsMotorEnabled() )
  3858. {
  3859. // Add motor.
  3860. pJointNode->addField( jointRevoluteMotorSpeedName, mRadToDeg(pJoint->GetMotorSpeed()) );
  3861. pJointNode->addField( jointRevoluteMotorMaxTorqueName, pJoint->GetMaxMotorTorque() );
  3862. }
  3863. // Add local anchors.
  3864. if ( mNotZero( pJoint->GetLocalAnchorA().LengthSquared() ) )
  3865. pJointNode->addField( jointLocalAnchorAName, pJoint->GetLocalAnchorA() );
  3866. if ( mNotZero( pJoint->GetLocalAnchorB().LengthSquared() ) )
  3867. pJointNode->addField( jointLocalAnchorBName, pJoint->GetLocalAnchorB() );
  3868. // Add scene object bodies.
  3869. if ( pSceneObjectA != NULL )
  3870. pJointNode->addNode( pSceneObjectA );
  3871. if ( pSceneObjectB != NULL )
  3872. pJointNode->addNode( pSceneObjectB );
  3873. }
  3874. break;
  3875. case e_weldJoint:
  3876. {
  3877. // Set joint name.
  3878. pJointNode->setNodeName( jointWeldNodeName );
  3879. // Fetch joint.
  3880. const b2WeldJoint* pJoint = dynamic_cast<const b2WeldJoint*>( pBaseJoint );
  3881. // Sanity!
  3882. AssertFatal( pJoint != NULL, "Scene::onTamlCustomWrite() - Invalid weld joint type returned." );
  3883. // Add frequency.
  3884. if ( mNotZero( pJoint->GetFrequency() ) )
  3885. pJointNode->addField( jointWeldFrequencyName, pJoint->GetFrequency() );
  3886. // Add damping ratio.
  3887. if ( mNotZero( pJoint->GetDampingRatio() ) )
  3888. pJointNode->addField( jointWeldDampingRatioName, pJoint->GetDampingRatio() );
  3889. // Add local anchors.
  3890. if ( mNotZero( pJoint->GetLocalAnchorA().LengthSquared() ) )
  3891. pJointNode->addField( jointLocalAnchorAName, pJoint->GetLocalAnchorA() );
  3892. if ( mNotZero( pJoint->GetLocalAnchorB().LengthSquared() ) )
  3893. pJointNode->addField( jointLocalAnchorBName, pJoint->GetLocalAnchorB() );
  3894. // Add scene object bodies.
  3895. if ( pSceneObjectA != NULL )
  3896. pJointNode->addNode( pSceneObjectA );
  3897. if ( pSceneObjectB != NULL )
  3898. pJointNode->addNode( pSceneObjectB );
  3899. }
  3900. break;
  3901. case e_wheelJoint:
  3902. {
  3903. // Set joint name.
  3904. pJointNode->setNodeName( jointWheelNodeName );
  3905. // Fetch joint.
  3906. b2WheelJoint* pJoint = dynamic_cast<b2WheelJoint*>( pBaseJoint );
  3907. // Sanity!
  3908. AssertFatal( pJoint != NULL, "Scene::onTamlCustomWrite() - Invalid wheel joint type returned." );
  3909. // Add motor.
  3910. if ( pJoint->IsMotorEnabled() )
  3911. {
  3912. // Add motor.
  3913. pJointNode->addField( jointWheelMotorSpeedName, mRadToDeg(pJoint->GetMotorSpeed()) );
  3914. pJointNode->addField( jointWheelMotorMaxTorqueName, pJoint->GetMaxMotorTorque() );
  3915. }
  3916. // Add frequency.
  3917. if ( mNotZero( pJoint->GetSpringFrequencyHz() ) )
  3918. pJointNode->addField( jointWheelFrequencyName, pJoint->GetSpringFrequencyHz() );
  3919. // Add damping ratio.
  3920. if ( mNotZero( pJoint->GetSpringDampingRatio() ) )
  3921. pJointNode->addField( jointWheelDampingRatioName, pJoint->GetSpringDampingRatio() );
  3922. // Add world axis.
  3923. pJointNode->addField( jointWheelWorldAxisName, pJoint->GetBodyA()->GetWorldVector( pJoint->GetLocalAxisA() ) );
  3924. // Add local anchors.
  3925. pJointNode->addField( jointLocalAnchorAName, pJoint->GetLocalAnchorA() );
  3926. pJointNode->addField( jointLocalAnchorBName, pJoint->GetLocalAnchorB() );
  3927. // Add scene object bodies.
  3928. if ( pSceneObjectA != NULL )
  3929. pJointNode->addNode( pSceneObjectA );
  3930. if ( pSceneObjectB != NULL )
  3931. pJointNode->addNode( pSceneObjectB );
  3932. }
  3933. break;
  3934. case e_frictionJoint:
  3935. {
  3936. // Set joint name.
  3937. pJointNode->setNodeName( jointFrictionNodeName );
  3938. // Fetch joint.
  3939. const b2FrictionJoint* pJoint = dynamic_cast<const b2FrictionJoint*>( pBaseJoint );
  3940. // Add max force.
  3941. if ( mNotZero( pJoint->GetMaxForce() ) )
  3942. pJointNode->addField( jointFrictionMaxForceName, pJoint->GetMaxForce() );
  3943. // Add max torque.
  3944. if ( mNotZero( pJoint->GetMaxTorque() ) )
  3945. pJointNode->addField( jointFrictionMaxTorqueName, pJoint->GetMaxTorque() );
  3946. // Sanity!
  3947. AssertFatal( pJoint != NULL, "Scene::onTamlCustomWrite() - Invalid friction joint type returned." );
  3948. // Add local anchors.
  3949. if ( mNotZero( pJoint->GetLocalAnchorA().LengthSquared() ) )
  3950. pJointNode->addField( jointLocalAnchorAName, pJoint->GetLocalAnchorA() );
  3951. if ( mNotZero( pJoint->GetLocalAnchorB().LengthSquared() ) )
  3952. pJointNode->addField( jointLocalAnchorBName, pJoint->GetLocalAnchorB() );
  3953. // Add scene object bodies.
  3954. if ( pSceneObjectA != NULL )
  3955. pJointNode->addNode( pSceneObjectA );
  3956. if ( pSceneObjectB != NULL )
  3957. pJointNode->addNode( pSceneObjectB );
  3958. }
  3959. break;
  3960. case e_prismaticJoint:
  3961. {
  3962. // Set joint name.
  3963. pJointNode->setNodeName( jointPrismaticNodeName );
  3964. // Fetch joint.
  3965. b2PrismaticJoint* pJoint = dynamic_cast<b2PrismaticJoint*>( pBaseJoint );
  3966. // Sanity!
  3967. AssertFatal( pJoint != NULL, "Scene::onTamlCustomWrite() - Invalid prismatic joint type returned." );
  3968. // Add limit.
  3969. if ( pJoint->IsLimitEnabled() )
  3970. {
  3971. // Add limits.
  3972. pJointNode->addField( jointPrismaticLimitLowerTransName, pJoint->GetLowerLimit() );
  3973. pJointNode->addField( jointPrismaticLimitUpperTransName, pJoint->GetUpperLimit() );
  3974. }
  3975. // Add motor.
  3976. if ( pJoint->IsMotorEnabled() )
  3977. {
  3978. // Add motor.
  3979. pJointNode->addField( jointPrismaticMotorSpeedName, mRadToDeg(pJoint->GetMotorSpeed()) );
  3980. pJointNode->addField( jointPrismaticMotorMaxForceName, pJoint->GetMaxMotorForce() );
  3981. }
  3982. // Add world axis.
  3983. pJointNode->addField( jointPrismaticWorldAxisName, pJoint->GetBodyA()->GetWorldVector( pJoint->GetLocalAxisA() ) );
  3984. // Add local anchors.
  3985. pJointNode->addField( jointLocalAnchorAName, pJoint->GetLocalAnchorA() );
  3986. pJointNode->addField( jointLocalAnchorBName, pJoint->GetLocalAnchorB() );
  3987. // Add scene object bodies.
  3988. if ( pSceneObjectA != NULL )
  3989. pJointNode->addNode( pSceneObjectA );
  3990. if ( pSceneObjectB != NULL )
  3991. pJointNode->addNode( pSceneObjectB );
  3992. }
  3993. break;
  3994. case e_pulleyJoint:
  3995. {
  3996. // Set joint name.
  3997. pJointNode->setNodeName( jointPulleyNodeName );
  3998. // Fetch joint.
  3999. b2PulleyJoint* pJoint = dynamic_cast<b2PulleyJoint*>( pBaseJoint );
  4000. // Sanity!
  4001. AssertFatal( pJoint != NULL, "Scene::onTamlCustomWrite() - Invalid pulley joint type returned." );
  4002. // Add lengths.
  4003. pJointNode->addField( jointPulleyLengthAName, pJoint->GetLengthA() );
  4004. pJointNode->addField( jointPulleyLengthBName, pJoint->GetLengthB() );
  4005. // Add ratio,
  4006. pJointNode->addField( jointPulleyRatioName, pJoint->GetRatio() );
  4007. // Add ground anchors.
  4008. pJointNode->addField( jointPulleyGroundAnchorAName, pJoint->GetGroundAnchorA() );
  4009. pJointNode->addField( jointPulleyGroundAnchorBName, pJoint->GetGroundAnchorB() );
  4010. // Add local anchors.
  4011. pJointNode->addField( jointLocalAnchorAName, pJoint->GetBodyA()->GetLocalPoint( pJoint->GetAnchorA() ) );
  4012. pJointNode->addField( jointLocalAnchorBName, pJoint->GetBodyB()->GetLocalPoint( pJoint->GetAnchorB() ) );
  4013. // Add scene object bodies.
  4014. if ( pSceneObjectA != NULL )
  4015. pJointNode->addNode( pSceneObjectA );
  4016. if ( pSceneObjectB != NULL )
  4017. pJointNode->addNode( pSceneObjectB );
  4018. }
  4019. break;
  4020. case e_mouseJoint:
  4021. {
  4022. // Set joint name.
  4023. pJointNode->setNodeName( jointTargetNodeName );
  4024. // Fetch joint.
  4025. const b2MouseJoint* pJoint = dynamic_cast<const b2MouseJoint*>( pBaseJoint );
  4026. // Sanity!
  4027. AssertFatal( pJoint != NULL, "Scene::onTamlCustomWrite() - Invalid target joint type returned." );
  4028. // Add target.
  4029. pJointNode->addField( jointTargetWorldTargetName, pJoint->GetTarget() );
  4030. // Add max force.
  4031. pJointNode->addField( jointTargetMaxForceName, pJoint->GetMaxForce() );
  4032. // Add frequency
  4033. pJointNode->addField( jointTargetFrequencyName, pJoint->GetFrequency() );
  4034. // Add damping ratio.
  4035. pJointNode->addField( jointTargetDampingRatioName, pJoint->GetDampingRatio() );
  4036. // Add body.
  4037. // NOTE: This joint uses BODYB as the object, BODYA is the ground-body however for easy of use
  4038. // we'll refer to this as OBJECTA in the persisted format.
  4039. if ( pSceneObjectB != NULL )
  4040. pJointNode->addNode( pSceneObjectB );
  4041. }
  4042. break;
  4043. case e_motorJoint:
  4044. {
  4045. // Set joint name.
  4046. pJointNode->setNodeName( jointMotorNodeName );
  4047. // Fetch joint.
  4048. const b2MotorJoint* pJoint = dynamic_cast<const b2MotorJoint*>( pBaseJoint );
  4049. // Sanity!
  4050. AssertFatal( pJoint != NULL, "Scene::onTamlCustomWrite() - Invalid motor joint type returned." );
  4051. // Add linear offset.
  4052. if ( mNotZero( pJoint->GetLinearOffset().LengthSquared() ) )
  4053. pJointNode->addField( jointMotorLinearOffsetName, pJoint->GetLinearOffset() );
  4054. // Add angular offset.
  4055. if ( mNotZero( pJoint->GetAngularOffset() ) )
  4056. pJointNode->addField( jointMotorAngularOffsetName, mRadToDeg( pJoint->GetAngularOffset() ) );
  4057. // Add max force.
  4058. pJointNode->addField( jointMotorMaxForceName, pJoint->GetMaxForce() );
  4059. // Add max torque.
  4060. pJointNode->addField( jointMotorMaxTorqueName, pJoint->GetMaxTorque() );
  4061. // Add correction factor.
  4062. pJointNode->addField( jointMotorCorrectionFactorName, pJoint->GetCorrectionFactor() );
  4063. // Add scene object bodies.
  4064. if ( pSceneObjectA != NULL )
  4065. pJointNode->addNode( pSceneObjectA );
  4066. if ( pSceneObjectB != NULL )
  4067. pJointNode->addNode( pSceneObjectB );
  4068. }
  4069. break;
  4070. default:
  4071. // Sanity!
  4072. AssertFatal( false, "Scene::onTamlCustomWrite() - Unknown joint type detected." );
  4073. }
  4074. // Add collide connected flag.
  4075. if ( pBaseJoint->GetCollideConnected() )
  4076. pJointNode->addField( jointCollideConnectedName, pBaseJoint->GetCollideConnected() );
  4077. }
  4078. }
  4079. // Fetch controller count.
  4080. const S32 sceneControllerCount = getControllers() ? getControllers()->size() : 0;
  4081. // Do we have any scene controllers?
  4082. if ( sceneControllerCount > 0 )
  4083. {
  4084. // Yes, so add controller node.
  4085. TamlCustomNode* pControllerCustomNode = customNodes.addNode( controllerCustomNodeName );
  4086. // Fetch the scene controllers.
  4087. SimSet* pControllerSet = getControllers();
  4088. // Iterate scene controllers.
  4089. for( S32 i = 0; i < sceneControllerCount; i++ )
  4090. {
  4091. // Fetch the set object.
  4092. SimObject* pSetObject = pControllerSet->at(i);
  4093. // Skip if not a controller.
  4094. if ( !pSetObject->isType<SceneController*>() )
  4095. continue;
  4096. // Add controller node.
  4097. pControllerCustomNode->addNode( pSetObject );
  4098. }
  4099. }
  4100. // Fetch asset preload count.
  4101. const S32 assetPreloadCount = getAssetPreloadCount();
  4102. // Do we have any asset preloads?
  4103. if ( assetPreloadCount > 0 )
  4104. {
  4105. // Yes, so fetch asset Id type prefix.
  4106. StringTableEntry assetIdTypePrefix = ConsoleBaseType::getType( TypeAssetId )->getTypePrefix();
  4107. // Add asset preload node.
  4108. TamlCustomNode* pAssetPreloadCustomNode = customNodes.addNode( assetPreloadNodeName );
  4109. // Iterate asset preloads.
  4110. for( typeAssetPtrVector::const_iterator assetItr = mAssetPreloads.begin(); assetItr != mAssetPreloads.end(); ++assetItr )
  4111. {
  4112. // Add node.
  4113. TamlCustomNode* pAssetNode = pAssetPreloadCustomNode->addNode( assetNodeName );
  4114. char valueBuffer[1024];
  4115. dSprintf( valueBuffer, sizeof(valueBuffer), "%s%s", assetIdTypePrefix, (*assetItr)->getAssetId() );
  4116. // Add asset Id.
  4117. pAssetNode->addField( "Id", valueBuffer );
  4118. }
  4119. }
  4120. }
  4121. //-----------------------------------------------------------------------------
  4122. void Scene::onTamlCustomRead( const TamlCustomNodes& customNodes )
  4123. {
  4124. // Call parent.
  4125. Parent::onTamlCustomRead( customNodes );
  4126. }
  4127. //-----------------------------------------------------------------------------
  4128. U32 Scene::getGlobalSceneCount( void )
  4129. {
  4130. return sSceneCount;
  4131. }
  4132. //-----------------------------------------------------------------------------
  4133. SceneRenderRequest* Scene::createDefaultRenderRequest( SceneRenderQueue* pSceneRenderQueue, SceneObject* pSceneObject )
  4134. {
  4135. // Create a render request and populate it with the default details.
  4136. SceneRenderRequest* pSceneRenderRequest = pSceneRenderQueue->createRenderRequest()->set(
  4137. pSceneObject,
  4138. pSceneObject->getRenderPosition(),
  4139. pSceneObject->getSceneLayerDepth(),
  4140. pSceneObject->getSortPoint(),
  4141. pSceneObject->getSerialId(),
  4142. pSceneObject->getRenderGroup() );
  4143. // Prepare the blending for it.
  4144. pSceneRenderRequest->mBlendMode = pSceneObject->getBlendMode();
  4145. pSceneRenderRequest->mBlendColor = pSceneObject->getBlendColor();
  4146. pSceneRenderRequest->mSrcBlendFactor = pSceneObject->getSrcBlendFactor();
  4147. pSceneRenderRequest->mDstBlendFactor = pSceneObject->getDstBlendFactor();
  4148. pSceneRenderRequest->mAlphaTest = pSceneObject->getAlphaTest();
  4149. return pSceneRenderRequest;
  4150. }
  4151. //-----------------------------------------------------------------------------
  4152. SimObject* Scene::getTamlChild( const U32 childIndex ) const
  4153. {
  4154. // Sanity!
  4155. AssertFatal( childIndex < (U32)mSceneObjects.size(), "Scene::getTamlChild() - Child index is out of range." );
  4156. // For when the assert is not used.
  4157. if ( childIndex >= (U32)mSceneObjects.size() )
  4158. return NULL;
  4159. return mSceneObjects[ childIndex ];
  4160. }
  4161. //-----------------------------------------------------------------------------
  4162. void Scene::addTamlChild( SimObject* pSimObject )
  4163. {
  4164. // Sanity!
  4165. AssertFatal( pSimObject != NULL, "Scene::addTamlChild() - Cannot add a NULL child object." );
  4166. // Fetch as a scene object.
  4167. SceneObject* pSceneObject = dynamic_cast<SceneObject*>( pSimObject );
  4168. // Is it a scene object?
  4169. if ( pSceneObject == NULL )
  4170. {
  4171. // No, so warn.
  4172. Con::warnf( "Scene::addTamlChild() - Cannot add a child object that isn't a scene object." );
  4173. return;
  4174. }
  4175. // Add the scene object.
  4176. addToScene( pSceneObject );
  4177. }
  4178. //-----------------------------------------------------------------------------
  4179. static EnumTable::Enums DebugOptionsLookup[] =
  4180. {
  4181. { Scene::SCENE_DEBUG_METRICS, "metrics" },
  4182. { Scene::SCENE_DEBUG_FPS_METRICS, "fps" },
  4183. { Scene::SCENE_DEBUG_CONTROLLERS, "controllers" },
  4184. { Scene::SCENE_DEBUG_JOINTS, "joints" },
  4185. { Scene::SCENE_DEBUG_WIREFRAME_RENDER, "wireframe" },
  4186. ///
  4187. { Scene::SCENE_DEBUG_AABB, "aabb" },
  4188. { Scene::SCENE_DEBUG_OOBB, "oobb" },
  4189. { Scene::SCENE_DEBUG_SLEEP, "sleep" },
  4190. { Scene::SCENE_DEBUG_COLLISION_SHAPES, "collision" },
  4191. { Scene::SCENE_DEBUG_POSITION_AND_COM, "position" },
  4192. { Scene::SCENE_DEBUG_SORT_POINTS, "sort" },
  4193. };
  4194. //-----------------------------------------------------------------------------
  4195. Scene::DebugOption Scene::getDebugOptionEnum(const char* label)
  4196. {
  4197. // Search for Mnemonic.
  4198. for(U32 i = 0; i < (sizeof(DebugOptionsLookup) / sizeof(EnumTable::Enums)); i++)
  4199. if( dStricmp(DebugOptionsLookup[i].label, label) == 0)
  4200. return((Scene::DebugOption)DebugOptionsLookup[i].index);
  4201. // Warn.
  4202. Con::warnf( "Scene::getDebugOptionEnum() - Invalid debug option '%s'.", label );
  4203. return Scene::SCENE_DEBUG_INVALID;
  4204. }
  4205. //-----------------------------------------------------------------------------
  4206. const char* Scene::getDebugOptionDescription( Scene::DebugOption debugOption )
  4207. {
  4208. // Search for Mnemonic.
  4209. for (U32 i = 0; i < (sizeof(DebugOptionsLookup) / sizeof(EnumTable::Enums)); i++)
  4210. {
  4211. if( DebugOptionsLookup[i].index == debugOption )
  4212. return DebugOptionsLookup[i].label;
  4213. }
  4214. // Warn.
  4215. Con::warnf( "Scene::getDebugOptionDescription() - Invalid debug option." );
  4216. return StringTable->EmptyString;
  4217. }
  4218. //-----------------------------------------------------------------------------
  4219. static EnumTable::Enums jointTypeLookup[] =
  4220. {
  4221. { e_distanceJoint, "distance" },
  4222. { e_ropeJoint, "rope" },
  4223. { e_revoluteJoint, "revolute" },
  4224. { e_weldJoint, "weld" },
  4225. { e_wheelJoint, "wheel" },
  4226. { e_frictionJoint, "friction" },
  4227. { e_prismaticJoint, "prismatic" },
  4228. { e_pulleyJoint, "pulley" },
  4229. { e_mouseJoint, "target" },
  4230. { e_motorJoint, "motor" },
  4231. };
  4232. EnumTable jointTypeTable(sizeof(jointTypeLookup) / sizeof(EnumTable::Enums), &jointTypeLookup[0]);
  4233. //-----------------------------------------------------------------------------
  4234. const char* Scene::getJointTypeDescription( b2JointType jointType )
  4235. {
  4236. // Search for Mnemonic.
  4237. for (U32 i = 0; i < (sizeof(jointTypeLookup) / sizeof(EnumTable::Enums)); i++)
  4238. {
  4239. if( jointTypeLookup[i].index == jointType )
  4240. return jointTypeLookup[i].label;
  4241. }
  4242. // Warn.
  4243. Con::warnf( "Scene::getJointTypeDescription() - Invalid joint type." );
  4244. return StringTable->EmptyString;
  4245. }
  4246. //-----------------------------------------------------------------------------
  4247. b2JointType Scene::getJointTypeEnum(const char* label)
  4248. {
  4249. // Search for Mnemonic.
  4250. for (U32 i = 0; i < (sizeof(jointTypeLookup) / sizeof(EnumTable::Enums)); i++)
  4251. {
  4252. if( dStricmp(jointTypeLookup[i].label, label) == 0)
  4253. return (b2JointType)jointTypeLookup[i].index;
  4254. }
  4255. // Warn.
  4256. Con::warnf( "Scene::getJointTypeEnum() - Invalid joint of '%s'", label );
  4257. return e_unknownJoint;
  4258. }
  4259. //-----------------------------------------------------------------------------
  4260. static EnumTable::Enums pickModeLookup[] =
  4261. {
  4262. { Scene::PICK_ANY, "Any" },
  4263. { Scene::PICK_AABB, "AABB" },
  4264. { Scene::PICK_OOBB, "OOBB" },
  4265. { Scene::PICK_COLLISION, "Collision" },
  4266. };
  4267. //-----------------------------------------------------------------------------
  4268. Scene::PickMode Scene::getPickModeEnum(const char* label)
  4269. {
  4270. // Search for Mnemonic.
  4271. for(U32 i = 0; i < (sizeof(pickModeLookup) / sizeof(EnumTable::Enums)); i++)
  4272. if( dStricmp(pickModeLookup[i].label, label) == 0)
  4273. return((Scene::PickMode)pickModeLookup[i].index);
  4274. // Warn.
  4275. Con::warnf( "Scene::getPickModeEnum() - Invalid pick mode '%s'.", label );
  4276. return Scene::PICK_INVALID;
  4277. }
  4278. //-----------------------------------------------------------------------------
  4279. const char* Scene::getPickModeDescription( Scene::PickMode pickMode )
  4280. {
  4281. // Search for Mnemonic.
  4282. for (U32 i = 0; i < (sizeof(pickModeLookup) / sizeof(EnumTable::Enums)); i++)
  4283. {
  4284. if( pickModeLookup[i].index == pickMode )
  4285. return pickModeLookup[i].label;
  4286. }
  4287. // Warn.
  4288. Con::warnf( "Scene::getPickModeDescription() - Invalid pick mode.");
  4289. return StringTable->EmptyString;
  4290. }
  4291. //-----------------------------------------------------------------------------
  4292. static void WriteJointsCustomTamlScehema( const AbstractClassRep* pClassRep, TiXmlElement* pParentElement )
  4293. {
  4294. // Sanity!
  4295. AssertFatal( pClassRep != NULL, "Taml::WriteJointsCustomTamlScehema() - ClassRep cannot be NULL." );
  4296. AssertFatal( pParentElement != NULL, "Taml::WriteJointsCustomTamlScehema() - Parent Element cannot be NULL." );
  4297. char buffer[1024];
  4298. // Create joints node element.
  4299. TiXmlElement* pJointsNodeElement = new TiXmlElement( "xs:element" );
  4300. dSprintf( buffer, sizeof(buffer), "%s.%s", pClassRep->getClassName(), jointCustomNodeName );
  4301. pJointsNodeElement->SetAttribute( "name", buffer );
  4302. pJointsNodeElement->SetAttribute( "minOccurs", 0 );
  4303. pJointsNodeElement->SetAttribute( "maxOccurs", 1 );
  4304. pParentElement->LinkEndChild( pJointsNodeElement );
  4305. // Create complex type.
  4306. TiXmlElement* pJointsNodeComplexTypeElement = new TiXmlElement( "xs:complexType" );
  4307. pJointsNodeElement->LinkEndChild( pJointsNodeComplexTypeElement );
  4308. // Create choice element.
  4309. TiXmlElement* pJointsNodeChoiceElement = new TiXmlElement( "xs:choice" );
  4310. pJointsNodeChoiceElement->SetAttribute( "minOccurs", 0 );
  4311. pJointsNodeChoiceElement->SetAttribute( "maxOccurs", "unbounded" );
  4312. pJointsNodeComplexTypeElement->LinkEndChild( pJointsNodeChoiceElement );
  4313. // ********************************************************************************
  4314. // Create Distance Joint Element.
  4315. // ********************************************************************************
  4316. TiXmlElement* pDistanceJointElement = new TiXmlElement( "xs:element" );
  4317. pDistanceJointElement->SetAttribute( "name", jointDistanceNodeName );
  4318. pDistanceJointElement->SetAttribute( "minOccurs", 0 );
  4319. pDistanceJointElement->SetAttribute( "maxOccurs", 1 );
  4320. pJointsNodeChoiceElement->LinkEndChild( pDistanceJointElement );
  4321. // Create complex type Element.
  4322. TiXmlElement* pDistanceJointComplexTypeElement = new TiXmlElement( "xs:complexType" );
  4323. pDistanceJointElement->LinkEndChild( pDistanceJointComplexTypeElement );
  4324. // Create "Length" attribute.
  4325. TiXmlElement* pDistanceJointElementA = new TiXmlElement( "xs:attribute" );
  4326. pDistanceJointElementA->SetAttribute( "name", jointDistanceLengthName );
  4327. pDistanceJointComplexTypeElement->LinkEndChild( pDistanceJointElementA );
  4328. TiXmlElement* pDistanceJointElementB = new TiXmlElement( "xs:simpleType" );
  4329. pDistanceJointElementA->LinkEndChild( pDistanceJointElementB );
  4330. TiXmlElement* pDistanceJointElementC = new TiXmlElement( "xs:restriction" );
  4331. pDistanceJointElementC->SetAttribute( "base", "xs:float" );
  4332. pDistanceJointElementB->LinkEndChild( pDistanceJointElementC );
  4333. TiXmlElement* pDistanceJointElementD = new TiXmlElement( "xs:minInclusive" );
  4334. pDistanceJointElementD->SetAttribute( "value", "0" );
  4335. pDistanceJointElementC->LinkEndChild( pDistanceJointElementD );
  4336. // Create "Frequency" attribute.
  4337. pDistanceJointElementA = new TiXmlElement( "xs:attribute" );
  4338. pDistanceJointElementA->SetAttribute( "name", jointDistanceFrequencyName );
  4339. pDistanceJointComplexTypeElement->LinkEndChild( pDistanceJointElementA );
  4340. pDistanceJointElementB = new TiXmlElement( "xs:simpleType" );
  4341. pDistanceJointElementA->LinkEndChild( pDistanceJointElementB );
  4342. pDistanceJointElementC = new TiXmlElement( "xs:restriction" );
  4343. pDistanceJointElementC->SetAttribute( "base", "xs:float" );
  4344. pDistanceJointElementB->LinkEndChild( pDistanceJointElementC );
  4345. pDistanceJointElementD = new TiXmlElement( "xs:minInclusive" );
  4346. pDistanceJointElementD->SetAttribute( "value", "0" );
  4347. pDistanceJointElementC->LinkEndChild( pDistanceJointElementD );
  4348. // Create "Damping Ratio" attribute.
  4349. pDistanceJointElementA = new TiXmlElement( "xs:attribute" );
  4350. pDistanceJointElementA->SetAttribute( "name", jointDistanceDampingRatioName );
  4351. pDistanceJointComplexTypeElement->LinkEndChild( pDistanceJointElementA );
  4352. pDistanceJointElementB = new TiXmlElement( "xs:simpleType" );
  4353. pDistanceJointElementA->LinkEndChild( pDistanceJointElementB );
  4354. pDistanceJointElementC = new TiXmlElement( "xs:restriction" );
  4355. pDistanceJointElementC->SetAttribute( "base", "xs:float" );
  4356. pDistanceJointElementB->LinkEndChild( pDistanceJointElementC );
  4357. pDistanceJointElementD = new TiXmlElement( "xs:minInclusive" );
  4358. pDistanceJointElementD->SetAttribute( "value", "0" );
  4359. pDistanceJointElementC->LinkEndChild( pDistanceJointElementD );
  4360. }
  4361. //-----------------------------------------------------------------------------
  4362. static void WriteControllersCustomTamlScehema( const AbstractClassRep* pClassRep, TiXmlElement* pParentElement )
  4363. {
  4364. // Sanity!
  4365. AssertFatal( pClassRep != NULL, "Taml::WriteControllersCustomTamlScehema() - ClassRep cannot be NULL." );
  4366. AssertFatal( pParentElement != NULL, "Taml::WriteControllersCustomTamlScehema() - Parent Element cannot be NULL." );
  4367. char buffer[1024];
  4368. // Create controllers node element.
  4369. TiXmlElement* pControllersNodeElement = new TiXmlElement( "xs:element" );
  4370. dSprintf( buffer, sizeof(buffer), "%s.%s", pClassRep->getClassName(), controllerCustomNodeName );
  4371. pControllersNodeElement->SetAttribute( "name", buffer );
  4372. pControllersNodeElement->SetAttribute( "minOccurs", 0 );
  4373. pControllersNodeElement->SetAttribute( "maxOccurs", 1 );
  4374. pParentElement->LinkEndChild( pControllersNodeElement );
  4375. // Create complex type.
  4376. TiXmlElement* pControllersNodeComplexTypeElement = new TiXmlElement( "xs:complexType" );
  4377. pControllersNodeElement->LinkEndChild( pControllersNodeComplexTypeElement );
  4378. // Create choice element.
  4379. TiXmlElement* pControllersNodeChoiceElement = new TiXmlElement( "xs:choice" );
  4380. pControllersNodeChoiceElement->SetAttribute( "minOccurs", 0 );
  4381. pControllersNodeChoiceElement->SetAttribute( "maxOccurs", "unbounded" );
  4382. pControllersNodeComplexTypeElement->LinkEndChild( pControllersNodeChoiceElement );
  4383. // Fetch the scene controller type.
  4384. AbstractClassRep* pPickingSceneControllerType = AbstractClassRep::findClassRep( "PickingSceneController" );
  4385. AbstractClassRep* pGroupedSceneControllerType = AbstractClassRep::findClassRep( "GroupedSceneController" );
  4386. // Sanity!
  4387. AssertFatal( pPickingSceneControllerType != NULL, "Scene::WriteControllersCustomTamlScehema() - Cannot find the PickingSceneController type." );
  4388. AssertFatal( pGroupedSceneControllerType != NULL, "Scene::WriteControllersCustomTamlScehema() - Cannot find the GroupedSceneController type." );
  4389. // Add choice members.
  4390. for ( AbstractClassRep* pChoiceType = AbstractClassRep::getClassList(); pChoiceType != NULL; pChoiceType = pChoiceType->getNextClass() )
  4391. {
  4392. // Skip if not derived from either of the scene controllers.
  4393. if ( !pChoiceType->isClass( pPickingSceneControllerType ) && !pChoiceType->isClass( pGroupedSceneControllerType ) )
  4394. continue;
  4395. // Add choice member.
  4396. TiXmlElement* pChoiceMemberElement = new TiXmlElement( "xs:element" );
  4397. pChoiceMemberElement->SetAttribute( "name", pChoiceType->getClassName() );
  4398. dSprintf( buffer, sizeof(buffer), "%s_Type", pChoiceType->getClassName() );
  4399. pChoiceMemberElement->SetAttribute( "type", buffer );
  4400. pControllersNodeChoiceElement->LinkEndChild( pChoiceMemberElement );
  4401. }
  4402. }
  4403. //-----------------------------------------------------------------------------
  4404. static void WritePreloadsCustomTamlScehema( const AbstractClassRep* pClassRep, TiXmlElement* pParentElement )
  4405. {
  4406. // Sanity!
  4407. AssertFatal( pClassRep != NULL, "Taml::WritePreloadsCustomTamlScehema() - ClassRep cannot be NULL." );
  4408. AssertFatal( pParentElement != NULL, "Taml::WritePreloadsCustomTamlScehema() - Parent Element cannot be NULL." );
  4409. char buffer[1024];
  4410. // Create preloads node element.
  4411. TiXmlElement* pPreloadsNodeElement = new TiXmlElement( "xs:element" );
  4412. dSprintf( buffer, sizeof(buffer), "%s.%s", pClassRep->getClassName(), assetPreloadNodeName );
  4413. pPreloadsNodeElement->SetAttribute( "name", buffer );
  4414. pPreloadsNodeElement->SetAttribute( "minOccurs", 0 );
  4415. pPreloadsNodeElement->SetAttribute( "maxOccurs", 1 );
  4416. pParentElement->LinkEndChild( pPreloadsNodeElement );
  4417. // Create complex type.
  4418. TiXmlElement* pPreloadsNodeComplexTypeElement = new TiXmlElement( "xs:complexType" );
  4419. pPreloadsNodeElement->LinkEndChild( pPreloadsNodeComplexTypeElement );
  4420. // Create choice element.
  4421. TiXmlElement* pPreloadsNodeChoiceElement = new TiXmlElement( "xs:choice" );
  4422. pPreloadsNodeChoiceElement->SetAttribute( "minOccurs", 0 );
  4423. pPreloadsNodeChoiceElement->SetAttribute( "maxOccurs", "unbounded" );
  4424. pPreloadsNodeComplexTypeElement->LinkEndChild( pPreloadsNodeChoiceElement );
  4425. // Add choice member element.
  4426. TiXmlElement* pChoiceMemberElement = new TiXmlElement( "xs:element" );
  4427. pChoiceMemberElement->SetAttribute( "name", assetNodeName );
  4428. pPreloadsNodeChoiceElement->LinkEndChild( pChoiceMemberElement );
  4429. // Add choice member complex type element.
  4430. TiXmlElement* pChoiceMemberComplexTypeElement = new TiXmlElement( "xs:complexType" );
  4431. pChoiceMemberElement->LinkEndChild( pChoiceMemberComplexTypeElement );
  4432. // Add choice member attribute element.
  4433. TiXmlElement* pChoiceAttributeElement = new TiXmlElement( "xs:attribute" );
  4434. pChoiceAttributeElement->SetAttribute( "name", "Id" );
  4435. dSprintf( buffer, sizeof(buffer), "%s", "AssetId_ConsoleType" );
  4436. pChoiceAttributeElement->SetAttribute( "type", buffer );
  4437. pChoiceMemberComplexTypeElement->LinkEndChild( pChoiceAttributeElement );
  4438. }
  4439. //-----------------------------------------------------------------------------
  4440. static void WriteCustomTamlSchema( const AbstractClassRep* pClassRep, TiXmlElement* pParentElement )
  4441. {
  4442. // Sanity!
  4443. AssertFatal( pClassRep != NULL, "Taml::WriteCustomTamlSchema() - ClassRep cannot be NULL." );
  4444. AssertFatal( pParentElement != NULL, "Taml::WriteCustomTamlSchema() - Parent Element cannot be NULL." );
  4445. WriteJointsCustomTamlScehema( pClassRep, pParentElement );
  4446. WriteControllersCustomTamlScehema( pClassRep, pParentElement );
  4447. WritePreloadsCustomTamlScehema( pClassRep, pParentElement );
  4448. }
  4449. //------------------------------------------------------------------------------
  4450. IMPLEMENT_CONOBJECT_CHILDREN_SCHEMA(Scene, WriteCustomTamlSchema);