SceneObject.cc 152 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434
  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_OBJECT_H_
  23. #include "2d/sceneobject/SceneObject.h"
  24. #endif
  25. #ifndef _DGL_H_
  26. #include "graphics/dgl.h"
  27. #endif
  28. #ifndef _COLOR_H_
  29. #include "graphics/color.h"
  30. #endif
  31. #ifndef _BITSTREAM_H_
  32. #include "io/bitStream.h"
  33. #endif
  34. #ifndef _MMATHFN_H_
  35. #include "math/mMathFn.h"
  36. #endif
  37. #ifndef _CONSOLETYPES_H_
  38. #include "console/consoleTypes.h"
  39. #endif
  40. #ifndef _SIMBASE_H_
  41. #include "sim/simBase.h"
  42. #endif
  43. #ifndef _BEHAVIORTEMPLATE_H_
  44. #include "component/behaviors/behaviorTemplate.h"
  45. #endif
  46. #ifndef _SCENE_OBJECT_MOVE_TO_EVENT_H_
  47. #include "2d/sceneobject/SceneObjectMoveToEvent.h"
  48. #endif
  49. #ifndef _SCENE_OBJECT_ROTATE_TO_EVENT_H_
  50. #include "2d/sceneobject/SceneObjectRotateToEvent.h"
  51. #endif
  52. #ifndef _RENDER_PROXY_H_
  53. #include "2d/core/RenderProxy.h"
  54. #endif
  55. #ifndef _STRINGUNIT_H_
  56. #include "string/stringUnit.h"
  57. #endif
  58. // Script bindings.
  59. #include "SceneObject_ScriptBinding.h"
  60. // Debug Profiling.
  61. #include "debug/profiler.h"
  62. //-----------------------------------------------------------------------------
  63. // Scene-Object counter.
  64. static U32 sGlobalSceneObjectCount = 0;
  65. static U32 sSceneObjectMasterSerialId = 0;
  66. // Collision shapes custom node names.
  67. static StringTableEntry shapeCustomNodeName = StringTable->insert( "CollisionShapes" );
  68. static StringTableEntry shapeDensityName = StringTable->insert( "Density" );
  69. static StringTableEntry shapeFrictionName = StringTable->insert( "Friction" );
  70. static StringTableEntry shapeRestitutionName = StringTable->insert( "Restitution" );
  71. static StringTableEntry shapeSensorName = StringTable->insert( "Sensor" );
  72. static StringTableEntry shapePointName = StringTable->insert( "Point" );
  73. static StringTableEntry shapePrevPointName = StringTable->insert( "PreviousPoint" );
  74. static StringTableEntry shapeNextPointName = StringTable->insert( "NextPoint" );
  75. static StringTableEntry circleTypeName = StringTable->insert( "Circle" );
  76. static StringTableEntry circleRadiusName = StringTable->insert( "Radius" );
  77. static StringTableEntry circleOffsetName = StringTable->insert( "Offset" );
  78. static StringTableEntry polygonTypeName = StringTable->insert( "Polygon" );
  79. static StringTableEntry chainTypeName = StringTable->insert( "Chain" );
  80. static StringTableEntry edgeTypeName = StringTable->insert( "Edge" );
  81. //------------------------------------------------------------------------------
  82. // Important: If these defaults are changed then modify the associated "write" field protected methods to ensure
  83. // that the associated field is persisted if not the default.
  84. SceneObject::SceneObject() :
  85. /// Scene.
  86. mpScene(NULL),
  87. mpTargetScene(NULL),
  88. /// Lifetime.
  89. mLifetime(0.0f),
  90. mLifetimeActive(false),
  91. /// Layers.
  92. mSceneLayer(0),
  93. mSceneLayerMask(BIT(mSceneLayer)),
  94. mSceneLayerDepth(0.0f),
  95. /// Scene groups.
  96. mSceneGroup(0),
  97. mSceneGroupMask(BIT(mSceneGroup)),
  98. /// Area.
  99. mWorldProxyId(-1),
  100. /// Position / Angle.
  101. mPreTickPosition( 0.0f, 0.0f ),
  102. mPreTickAngle( 0.0f ),
  103. mRenderPosition( 0.0f, 0.0f ),
  104. mRenderAngle( 0.0f ),
  105. mSpatialDirty( true ),
  106. /// Body.
  107. mpBody(NULL),
  108. mWorldQueryKey(0),
  109. /// Collision control.
  110. mCollisionLayerMask(MASK_ALL),
  111. mCollisionGroupMask(MASK_ALL),
  112. mCollisionSuppress(false),
  113. mGatherContacts(false),
  114. mpCurrentContacts(NULL),
  115. /// Render visibility.
  116. mVisible(true),
  117. /// Render blending.
  118. mBlendMode(true),
  119. mSrcBlendFactor(GL_SRC_ALPHA),
  120. mDstBlendFactor(GL_ONE_MINUS_SRC_ALPHA),
  121. mBlendColor(ColorF(1.0f,1.0f,1.0f,1.0f)),
  122. mAlphaTest(-1.0f),
  123. /// Render sorting.
  124. mSortPoint(0.0f,0.0f),
  125. /// Input events.
  126. mUseInputEvents(false),
  127. /// Script callbacks.
  128. mUpdateCallback(false),
  129. mCollisionCallback(false),
  130. mSleepingCallback(false),
  131. /// Debug mode.
  132. mDebugMask(0X00000000),
  133. /// Camera mounting.
  134. mpAttachedCamera(NULL),
  135. /// GUI attachment.
  136. mAttachedGuiSizeControl(false),
  137. mpAttachedGui(NULL),
  138. mpAttachedGuiSceneWindow(NULL),
  139. /// Safe deletion.
  140. mBeingSafeDeleted(false),
  141. mSafeDeleteReady(true),
  142. /// Miscellaneous.
  143. mBatchIsolated(false),
  144. mSerialiseKey(0),
  145. mEditorTickAllowed(true),
  146. mPickingAllowed(true),
  147. mAlwaysInScope(false),
  148. mMoveToEventId(0),
  149. mRotateToEventId(0),
  150. mSerialId(0),
  151. mRenderGroup( StringTable->EmptyString )
  152. {
  153. // Set Vector Associations.
  154. VECTOR_SET_ASSOCIATION( mDestroyNotifyList );
  155. VECTOR_SET_ASSOCIATION( mCollisionFixtureDefs );
  156. VECTOR_SET_ASSOCIATION( mCollisionFixtures );
  157. // Assign scene-object index.
  158. mSerialId = ++sSceneObjectMasterSerialId;
  159. sGlobalSceneObjectCount++;
  160. // Initialize the body definition.
  161. // Important: If these defaults are changed then modify the associated "write" field protected methods to ensure
  162. // that the associated field is persisted if not the default.
  163. mBodyDefinition.userData = static_cast<PhysicsProxy*>(this);
  164. mBodyDefinition.position.Set(0.0f, 0.0f);
  165. mBodyDefinition.angle = 0.0f;
  166. mBodyDefinition.linearVelocity.Set(0.0f, 0.0f);
  167. mBodyDefinition.angularVelocity = 0.0f;
  168. mBodyDefinition.linearDamping = 0.0f;
  169. mBodyDefinition.angularDamping = 0.0f;
  170. mBodyDefinition.allowSleep = true;
  171. mBodyDefinition.awake = true;
  172. mBodyDefinition.fixedRotation = false;
  173. mBodyDefinition.bullet = false;
  174. mBodyDefinition.type = b2_dynamicBody;
  175. mBodyDefinition.active = true;
  176. mBodyDefinition.gravityScale = 1.0f;
  177. // Initialize the default fixture definition.
  178. // Important: If these defaults are changed then modify the associated "write" field protected methods to ensure
  179. // that the associated field is persisted if not the default.
  180. mDefaultFixture.userData = static_cast<PhysicsProxy*>(this);
  181. mDefaultFixture.density = 1.0f;
  182. mDefaultFixture.friction = 0.2f;
  183. mDefaultFixture.restitution = 0.0f;
  184. mDefaultFixture.isSensor = false;
  185. mDefaultFixture.shape = NULL;
  186. // Set last awake state.
  187. mLastAwakeState = !mBodyDefinition.allowSleep || mBodyDefinition.awake;
  188. // Turn-off auto-sizing.
  189. mAutoSizing = false;
  190. // Set size.
  191. setSize( Vector2::getOne() );
  192. }
  193. //-----------------------------------------------------------------------------
  194. SceneObject::~SceneObject()
  195. {
  196. // Are we in a Scene?
  197. if ( mpScene )
  198. {
  199. // Yes, so remove from Scene.
  200. mpScene->removeFromScene( this );
  201. }
  202. // Decrease scene-object count.
  203. --sGlobalSceneObjectCount;
  204. }
  205. //-----------------------------------------------------------------------------
  206. void SceneObject::initPersistFields()
  207. {
  208. // Call parent.
  209. Parent::initPersistFields();
  210. /// Lifetime.
  211. addProtectedField("Lifetime", TypeF32, Offset(mLifetime, SceneObject), &setLifetime, &defaultProtectedGetFn, &writeLifetime, "");
  212. /// Scene Layers.
  213. addProtectedField("SceneLayer", TypeS32, Offset(mSceneLayer, SceneObject), &setSceneLayer, &defaultProtectedGetFn, &writeSceneLayer, "");
  214. addProtectedField("SceneLayerDepth", TypeF32, Offset(mSceneLayerDepth, SceneObject), &setSceneLayerDepth, &defaultProtectedGetFn, &writeSceneLayerDepth, "" );
  215. // Scene Groups.
  216. addProtectedField("SceneGroup", TypeS32, Offset(mSceneGroup, SceneObject), &setSceneGroup, &defaultProtectedGetFn, &writeSceneGroup, "");
  217. /// Area.
  218. addProtectedField("Size", TypeVector2, Offset( mSize, SceneObject), &setSize, &defaultProtectedGetFn, &writeSize, "");
  219. /// Position / Angle.
  220. addProtectedField("Position", TypeVector2, NULL, &setPosition, &getPosition, &writePosition, "");
  221. addProtectedField("Angle", TypeF32, NULL, &setAngle, &getAngle, &writeAngle, "");
  222. addProtectedField("FixedAngle", TypeBool, NULL, &setFixedAngle, &getFixedAngle, &writeFixedAngle, "");
  223. /// Body.
  224. addProtectedField("BodyType", TypeEnum, NULL, &setBodyType, &getBodyType, &writeBodyType, 1, &bodyTypeTable, "" );
  225. addProtectedField("Active", TypeBool, NULL, &setActive, &getActive, &writeActive, "" );
  226. addProtectedField("Awake", TypeBool, NULL, &setAwake, &getAwake, &writeAwake, "" );
  227. addProtectedField("Bullet", TypeBool, NULL, &setBullet, &getBullet, &writeBullet, "" );
  228. addProtectedField("SleepingAllowed", TypeBool, NULL, &setSleepingAllowed, &getSleepingAllowed, &writeSleepingAllowed, "" );
  229. /// Collision control.
  230. addProtectedField("CollisionGroups", TypeS32, Offset(mCollisionGroupMask, SceneObject), &setCollisionGroups, &defaultProtectedGetFn, &writeCollisionGroups, "");
  231. addProtectedField("CollisionLayers", TypeS32, Offset(mCollisionLayerMask, SceneObject), &setCollisionLayers, &defaultProtectedGetFn, &writeCollisionLayers, "");
  232. addField("CollisionSuppress", TypeBool, Offset(mCollisionSuppress, SceneObject), &writeCollisionSuppress, "");
  233. addProtectedField("GatherContacts", TypeBool, NULL, &setGatherContacts, &defaultProtectedGetFn, &writeGatherContacts, "");
  234. addProtectedField("DefaultDensity", TypeF32, Offset( mDefaultFixture.density, SceneObject), &setDefaultDensity, &defaultProtectedGetFn, &writeDefaultDensity, "");
  235. addProtectedField("DefaultFriction", TypeF32, Offset( mDefaultFixture.friction, SceneObject), &setDefaultFriction, &defaultProtectedGetFn, &writeDefaultFriction, "");
  236. addProtectedField("DefaultRestitution", TypeF32, Offset( mDefaultFixture.restitution, SceneObject), &setDefaultRestitution, &defaultProtectedGetFn, &writeDefaultRestitution, "");
  237. /// Velocities.
  238. addProtectedField("LinearVelocity", TypeVector2, NULL, &setLinearVelocity, &getLinearVelocity, &writeLinearVelocity, "");
  239. addProtectedField("AngularVelocity", TypeF32, NULL, &setAngularVelocity, &getAngularVelocity, &writeAngularVelocity, "");
  240. addProtectedField("LinearDamping", TypeF32, NULL, &setLinearDamping, &getLinearDamping, &writeLinearDamping, "");
  241. addProtectedField("AngularDamping", TypeF32, NULL, &setAngularDamping, &getAngularDamping, &writeAngularDamping, "");
  242. /// Gravity scaling.
  243. addProtectedField("GravityScale", TypeF32, NULL, &setGravityScale, &getGravityScale, &writeGravityScale, "");
  244. /// Render visibility.
  245. addField("Visible", TypeBool, Offset(mVisible, SceneObject), &writeVisible, "");
  246. /// Render blending.
  247. addField("BlendMode", TypeBool, Offset(mBlendMode, SceneObject), &writeBlendMode, "");
  248. addField("SrcBlendFactor", TypeEnum, Offset(mSrcBlendFactor, SceneObject), &writeSrcBlendFactor, 1, &srcBlendFactorTable);
  249. addField("DstBlendFactor", TypeEnum, Offset(mDstBlendFactor, SceneObject), &writeDstBlendFactor, 1, &dstBlendFactorTable);
  250. addField("BlendColor", TypeColorF, Offset(mBlendColor, SceneObject), &writeBlendColor, "");
  251. addField("AlphaTest", TypeF32, Offset(mAlphaTest, SceneObject), &writeAlphaTest, "");
  252. /// Render sorting.
  253. addField("SortPoint", TypeVector2, Offset(mSortPoint, SceneObject), &writeSortPoint, "");
  254. addField("RenderGroup", TypeString, Offset(mRenderGroup, SceneObject), &writeRenderGroup, "");
  255. /// Input events.
  256. addField("UseInputEvents", TypeBool, Offset(mUseInputEvents, SceneObject), &writeUseInputEvents, "");
  257. addField("PickingAllowed", TypeBool, Offset(mPickingAllowed, SceneObject), &writePickingAllowed, "");
  258. // Script callbacks.
  259. addField("UpdateCallback", TypeBool, Offset(mUpdateCallback, SceneObject), &writeUpdateCallback, "");
  260. addField("CollisionCallback", TypeBool, Offset(mCollisionCallback, SceneObject), &writeCollisionCallback, "");
  261. addField("SleepingCallback", TypeBool, Offset(mSleepingCallback, SceneObject), &writeSleepingCallback, "");
  262. /// Scene.
  263. addProtectedField("scene", TypeSimObjectPtr, Offset(mpScene, SceneObject), &setScene, &defaultProtectedGetFn, &writeScene, "");
  264. }
  265. //-----------------------------------------------------------------------------
  266. bool SceneObject::onAdd()
  267. {
  268. // Call Parent.
  269. if(!Parent::onAdd())
  270. return false;
  271. // Add to any target scene.
  272. if ( mpTargetScene )
  273. {
  274. // Add to the target scene.
  275. mpTargetScene->addToScene(this);
  276. mpTargetScene = NULL;
  277. }
  278. // Return Okay.
  279. return true;
  280. }
  281. //-----------------------------------------------------------------------------
  282. void SceneObject::onRemove()
  283. {
  284. // Detach Any GUI Control.
  285. detachGui();
  286. // Remove from Scene.
  287. if ( getScene() )
  288. getScene()->removeFromScene( this );
  289. // Call Parent.
  290. Parent::onRemove();
  291. }
  292. //-----------------------------------------------------------------------------
  293. void SceneObject::onDestroyNotify( SceneObject* pSceneObject )
  294. {
  295. }
  296. //-----------------------------------------------------------------------------
  297. void SceneObject::OnRegisterScene( Scene* pScene )
  298. {
  299. // Sanity!
  300. AssertFatal( mpScene == NULL, "Cannot register to a scene if already registered." );
  301. AssertFatal( mpBody == NULL, "Cannot create a physics body if one already exists." );
  302. // Initialize contact gathering.
  303. initializeContactGathering();
  304. // Set scene.
  305. mpScene = pScene;
  306. // Create the physics body.
  307. mpBody = pScene->getWorld()->CreateBody( &mBodyDefinition );
  308. // Set active status.
  309. if ( !isEnabled() ) mpBody->SetActive( false );
  310. // Create fixtures.
  311. for( typeCollisionFixtureDefVector::iterator itr = mCollisionFixtureDefs.begin(); itr != mCollisionFixtureDefs.end(); itr++ )
  312. {
  313. // Fetch fixture definition.
  314. b2FixtureDef* pFixtureDef = (*itr);
  315. // Create fixture.
  316. b2Fixture* pFixture = mpBody->CreateFixture( pFixtureDef );
  317. // Push fixture.
  318. mCollisionFixtures.push_back( pFixture );
  319. // Destroy fixture shape.
  320. delete pFixtureDef->shape;
  321. // Destroy fixture definition.
  322. delete pFixtureDef;
  323. }
  324. // Clear offline fixture definitions.
  325. mCollisionFixtureDefs.clear();
  326. // Calculate current AABB.
  327. CoreMath::mCalculateAABB( getLocalSizedOOBB(), mpBody->GetTransform(), &mCurrentAABB );
  328. // Create world proxy Id.
  329. mWorldProxyId = pScene->getWorldQuery()->add( this );
  330. // Reset the spatials.
  331. resetTickSpatials();
  332. // Notify components.
  333. notifyComponentsAddToScene();
  334. }
  335. //-----------------------------------------------------------------------------
  336. void SceneObject::OnUnregisterScene( Scene* pScene )
  337. {
  338. // Sanity!
  339. AssertFatal( mpScene == pScene, "Cannot unregister from a scene that is not registered." );
  340. AssertFatal( mpBody != NULL, "Cannot unregister physics body as it does not exist." );
  341. // Notify components.
  342. notifyComponentsRemoveFromScene();
  343. // Copy fixtures to fixture definitions.
  344. for( typeCollisionFixtureVector::iterator itr = mCollisionFixtures.begin(); itr != mCollisionFixtures.end(); itr++ )
  345. {
  346. // Fetch fixture.
  347. b2Fixture* pFixture = (*itr);
  348. // Create fixture definition.
  349. b2FixtureDef* pFixtureDef = new b2FixtureDef();
  350. pFixtureDef->density = pFixture->GetDensity();
  351. pFixtureDef->friction = pFixture->GetFriction();
  352. pFixtureDef->restitution = pFixture->GetRestitution();
  353. pFixtureDef->isSensor = pFixture->IsSensor();
  354. pFixtureDef->userData = this;
  355. pFixtureDef->shape = pFixture->GetShape()->Clone( pScene->getBlockAllocator() );
  356. // Push fixture definition.
  357. mCollisionFixtureDefs.push_back( pFixtureDef );
  358. }
  359. // Clear our fixture references.
  360. // The actual fixtures will get destroyed when the body is destroyed so no need to destroy them here.
  361. mCollisionFixtures.clear();
  362. // Transfer physics body configuration back to definition.
  363. mBodyDefinition.type = getBodyType();
  364. mBodyDefinition.position = getPosition();
  365. mBodyDefinition.angle = getAngle();
  366. mBodyDefinition.linearVelocity = getLinearVelocity();
  367. mBodyDefinition.angularVelocity = getAngularVelocity();
  368. mBodyDefinition.linearDamping = getLinearDamping();
  369. mBodyDefinition.angularDamping = getAngularDamping();
  370. mBodyDefinition.gravityScale = getGravityScale();
  371. mBodyDefinition.allowSleep = getSleepingAllowed();
  372. mBodyDefinition.awake = getAwake();
  373. mBodyDefinition.fixedRotation = getFixedAngle();
  374. mBodyDefinition.bullet = getBullet();
  375. mBodyDefinition.active = getActive();
  376. // Destroy current contacts.
  377. delete mpCurrentContacts;
  378. mpCurrentContacts = NULL;
  379. // Destroy the physics body.
  380. mpScene->getWorld()->DestroyBody( mpBody );
  381. mpBody = NULL;
  382. // Destroy world proxy Id.
  383. if ( mWorldProxyId != -1 )
  384. {
  385. mpScene->getWorldQuery()->remove( this );
  386. mWorldProxyId = -1;
  387. }
  388. // Reset scene.
  389. mpScene = NULL;
  390. }
  391. //-----------------------------------------------------------------------------
  392. void SceneObject::resetTickSpatials( const bool resize )
  393. {
  394. // Set coincident pre-tick, current & render.
  395. mPreTickPosition = mRenderPosition = getPosition();
  396. mPreTickAngle = mRenderAngle = getAngle();
  397. // Fetch body transform.
  398. b2Transform bodyXform = getTransform();
  399. // Calculate current AABB.
  400. CoreMath::mCalculateAABB( getLocalSizedOOBB(), bodyXform, &mCurrentAABB );
  401. // Set coincident AABBs.
  402. mPreTickAABB = mCurrentAABB;
  403. // Calculate render OOBB.
  404. CoreMath::mCalculateOOBB( getLocalSizedOOBB(), bodyXform, mRenderOOBB );
  405. // Update world proxy (if in scene).
  406. if ( mpScene )
  407. {
  408. // Fetch world query.
  409. WorldQuery* pWorldQuery = mpScene->getWorldQuery();
  410. // Are we resizing the object?
  411. if ( resize )
  412. {
  413. // Yes, so we need to recreate a proxy.
  414. pWorldQuery->remove( this );
  415. mWorldProxyId = pWorldQuery->add( this );
  416. }
  417. else
  418. {
  419. // No, so we can simply update the proxy.
  420. pWorldQuery->update( this, mCurrentAABB, b2Vec2( 0.0f, 0.0f ) );
  421. }
  422. }
  423. // Flag spatial changed.
  424. mSpatialDirty = true;
  425. }
  426. //-----------------------------------------------------------------------------
  427. void SceneObject::preIntegrate( const F32 totalTime, const F32 elapsedTime, DebugStats* pDebugStats )
  428. {
  429. // Debug Profiling.
  430. PROFILE_SCOPE(SceneObject_PreIntegrate);
  431. // Finish if nothing is dirty.
  432. if ( !mSpatialDirty )
  433. return;
  434. // Reset spatial changed.
  435. mSpatialDirty = false;
  436. mPreTickPosition = mRenderPosition = getPosition();
  437. mPreTickAngle = mRenderAngle = getAngle();
  438. mPreTickAABB = mCurrentAABB;
  439. // Calculate render OOBB.
  440. CoreMath::mCalculateOOBB( getLocalSizedOOBB(), getTransform(), mRenderOOBB );
  441. }
  442. //-----------------------------------------------------------------------------
  443. void SceneObject::integrateObject( const F32 totalTime, const F32 elapsedTime, DebugStats* pDebugStats )
  444. {
  445. // Debug Profiling.
  446. PROFILE_SCOPE(SceneObject_IntegrateObject);
  447. // Fetch position.
  448. const b2Vec2 position = getPosition();
  449. // Has the angle or position changed?
  450. if ( mPreTickAngle != getAngle() ||
  451. mPreTickPosition.x != position.x ||
  452. mPreTickPosition.y != position.y )
  453. {
  454. // Yes, so flag spatial dirty.
  455. mSpatialDirty = true;
  456. // Calculate current AABB.
  457. CoreMath::mCalculateAABB( getLocalSizedOOBB(), getTransform(), &mCurrentAABB );
  458. // Calculate tick AABB.
  459. b2AABB tickAABB;
  460. tickAABB.Combine( mPreTickAABB, mCurrentAABB );
  461. // Calculate tick displacement.
  462. b2Vec2 tickDisplacement = position - mPreTickPosition;
  463. // Update world proxy.
  464. mpScene->getWorldQuery()->update( this, tickAABB, tickDisplacement );
  465. }
  466. // Update Lifetime.
  467. if ( mLifetimeActive && !getScene()->getIsEditorScene() )
  468. {
  469. updateLifetime( elapsedTime );
  470. }
  471. // Update Any Attached GUI.
  472. if ( mpAttachedGui && mpAttachedGuiSceneWindow )
  473. {
  474. updateAttachedGui();
  475. }
  476. // Are we attached to a camera?
  477. if ( mpAttachedCamera )
  478. {
  479. // Yes, so calculate camera mount.
  480. mpAttachedCamera->calculateCameraMount( elapsedTime );
  481. }
  482. }
  483. //-----------------------------------------------------------------------------
  484. void SceneObject::postIntegrate(const F32 totalTime, const F32 elapsedTime, DebugStats *pDebugStats)
  485. {
  486. // Debug Profiling.
  487. PROFILE_SCOPE(SceneObject_PostIntegrate);
  488. // Finish if no scene.
  489. if( !getScene() )
  490. return;
  491. // Notify components.
  492. notifyComponentsUpdate();
  493. // Script "onUpdate".
  494. if ( mUpdateCallback )
  495. {
  496. PROFILE_SCOPE(SceneObject_onUpdateCallback);
  497. Con::executef(this, 1, "onUpdate");
  498. }
  499. // Are we using the sleeping callback?
  500. if ( mSleepingCallback )
  501. {
  502. PROFILE_SCOPE(SceneObject_onWakeSleepCallback);
  503. // Yes, so fetch the current awake state.
  504. const bool currentAwakeState = getAwake();
  505. // Did we change awake state?
  506. if ( currentAwakeState != mLastAwakeState )
  507. {
  508. // Yes, so update last awake state.
  509. mLastAwakeState = currentAwakeState;
  510. // Perform the appropriate callback.
  511. if ( currentAwakeState )
  512. Con::executef(this, 1, "onWake");
  513. else
  514. Con::executef(this, 1, "onSleep");
  515. }
  516. }
  517. }
  518. //-----------------------------------------------------------------------------
  519. void SceneObject::interpolateObject( const F32 timeDelta )
  520. {
  521. // Debug Profiling.
  522. PROFILE_SCOPE(SceneObject_InterpolateObject);
  523. if ( mSpatialDirty )
  524. {
  525. // Debug Profiling.
  526. PROFILE_SCOPE(SceneObject_InterpolatePositionAngle);
  527. if ( timeDelta < 1.0f )
  528. {
  529. // Calculate render position.
  530. const b2Vec2 position = getPosition();
  531. b2Vec2 positionDelta = position - mPreTickPosition;
  532. positionDelta *= timeDelta;
  533. mRenderPosition = position - positionDelta;
  534. // Calculate render angle.
  535. const F32 angle = getAngle();
  536. F32 relativeAngle = angle - mPreTickAngle;
  537. if ( relativeAngle > b2_pi )
  538. relativeAngle -= b2_pi2;
  539. else if ( relativeAngle < -b2_pi )
  540. relativeAngle += b2_pi2;
  541. mRenderAngle = angle - (relativeAngle * timeDelta);
  542. }
  543. else
  544. {
  545. mRenderPosition = mPreTickPosition;
  546. mRenderAngle = mPreTickAngle;
  547. }
  548. // Calculate render transform.
  549. b2Transform renderXF( mRenderPosition, b2Rot(mRenderAngle) );
  550. // Calculate render OOBB.
  551. CoreMath::mCalculateOOBB( getLocalSizedOOBB(), renderXF, mRenderOOBB );
  552. }
  553. // Update Any Attached GUI.
  554. if ( mpAttachedGui && mpAttachedGuiSceneWindow )
  555. {
  556. updateAttachedGui();
  557. }
  558. // Are we attached to a camera?
  559. if ( mpAttachedCamera )
  560. {
  561. // Yes, so interpolate camera mount.
  562. mpAttachedCamera->interpolateCameraMount( timeDelta );
  563. }
  564. };
  565. //-----------------------------------------------------------------------------
  566. void SceneObject::sceneRenderFallback( const SceneRenderState* pSceneRenderState, const SceneRenderRequest* pSceneRenderRequest, BatchRender* pBatchRenderer )
  567. {
  568. // Debug Profiling.
  569. PROFILE_SCOPE(SceneObject_SceneRenderFallback);
  570. // Fetch the 'cannot render' proxy.
  571. RenderProxy* pNoImageRenderProxy = Sim::findObject<RenderProxy>( CANNOT_RENDER_PROXY_NAME );
  572. // Finish if no render proxy available or it can't render.
  573. if ( pNoImageRenderProxy == NULL || !pNoImageRenderProxy->validRender() )
  574. return;
  575. // Fetch render AABB.
  576. const b2Vec2* pRenderOOBB = getRenderOOBB();
  577. // Render using render proxy.
  578. pNoImageRenderProxy->render(
  579. false, false,
  580. pRenderOOBB[0], pRenderOOBB[1], pRenderOOBB[2], pRenderOOBB[3],
  581. pBatchRenderer );
  582. }
  583. //-----------------------------------------------------------------------------
  584. void SceneObject::sceneRenderOverlay( const SceneRenderState* sceneRenderState )
  585. {
  586. // Debug Profiling.
  587. PROFILE_SCOPE(SceneObject_SceneRenderOverlay);
  588. // Get Scene.
  589. Scene* pScene = getScene();
  590. // Cannot do anything without scene!
  591. if ( !pScene )
  592. return;
  593. // Don't draw debug if not enabled.
  594. if ( !isEnabled() )
  595. return;
  596. // Get merged Local/Scene Debug Mask.
  597. U32 debugMask = getDebugMask() | pScene->getDebugMask();
  598. // Finish here if we're not rendering any debug info or only have scene-rendered debug options.
  599. if ( !debugMask || (debugMask & ~(Scene::SCENE_DEBUG_METRICS | Scene::SCENE_DEBUG_JOINTS)) == 0 )
  600. return;
  601. // Clear blending.
  602. glDisable( GL_BLEND );
  603. glDisable( GL_TEXTURE_2D );
  604. // AABB debug draw.
  605. if ( debugMask & Scene::SCENE_DEBUG_AABB )
  606. {
  607. pScene->mDebugDraw.DrawAABB( mCurrentAABB, ColorF(0.7f, 0.7f, 0.9f) );
  608. }
  609. // OOBB debug draw.
  610. if ( debugMask & Scene::SCENE_DEBUG_OOBB )
  611. {
  612. pScene->mDebugDraw.DrawOOBB( mRenderOOBB, ColorF(0.9f, 0.9f, 1.0f) );
  613. }
  614. // Asleep debug draw.
  615. if ( !getAwake() && debugMask & Scene::SCENE_DEBUG_SLEEP )
  616. {
  617. pScene->mDebugDraw.DrawAsleep( mRenderOOBB, ColorF( 0.0f, 1.0f, 0.0f ) );
  618. }
  619. // Collision Shapes.
  620. if ( debugMask & Scene::SCENE_DEBUG_COLLISION_SHAPES )
  621. {
  622. pScene->mDebugDraw.DrawCollisionShapes( getRenderTransform(), getBody() );
  623. }
  624. // Position and local center of mass.
  625. if ( debugMask & Scene::SCENE_DEBUG_POSITION_AND_COM )
  626. {
  627. const b2Vec2 renderPosition = getRenderPosition();
  628. pScene->mDebugDraw.DrawPoint( renderPosition + getLocalCenter(), 6, ColorF( 0.0f, 1.0f, 0.4f ) );
  629. pScene->mDebugDraw.DrawPoint( renderPosition, 4, ColorF( 0.0f, 0.4f, 1.0f ) );
  630. }
  631. // Sort Points.
  632. if ( debugMask & Scene::SCENE_DEBUG_SORT_POINTS )
  633. {
  634. pScene->mDebugDraw.DrawSortPoint( getRenderPosition(), getSize(), mSortPoint );
  635. }
  636. }
  637. //-----------------------------------------------------------------------------
  638. U32 SceneObject::packUpdate(NetConnection * conn, U32 mask, BitStream *stream)
  639. {
  640. return 0;
  641. }
  642. //-----------------------------------------------------------------------------
  643. void SceneObject::unpackUpdate(NetConnection * conn, BitStream *stream)
  644. {
  645. }
  646. //-----------------------------------------------------------------------------
  647. void SceneObject::setEnabled( const bool enabled )
  648. {
  649. // Call parent.
  650. Parent::setEnabled( enabled );
  651. // If we have a scene, modify active.
  652. if ( mpScene )
  653. {
  654. mpBody->SetActive( enabled );
  655. }
  656. }
  657. //-----------------------------------------------------------------------------
  658. void SceneObject::setLifetime( const F32 lifetime )
  659. {
  660. // Debug Profiling.
  661. PROFILE_SCOPE(SceneObject_setLifetime);
  662. // Usage Flag.
  663. mLifetimeActive = mGreaterThanZero( lifetime );
  664. // Is life active?
  665. if ( mLifetimeActive )
  666. {
  667. // Yes, so set to incoming lifetime.
  668. mLifetime = lifetime;
  669. }
  670. else
  671. {
  672. // No, so reset it to be safe.
  673. mLifetime = 0.0f;
  674. }
  675. }
  676. //-----------------------------------------------------------------------------
  677. void SceneObject::updateLifetime( const F32 elapsedTime )
  678. {
  679. // Update Lifetime.
  680. if ( mLifetimeActive )
  681. {
  682. // Reduce Lifetime.
  683. setLifetime( getLifetime() - elapsedTime );
  684. // Are we now dead?
  685. if ( mLessThanOrEqual( getLifetime(), 0.0f) )
  686. {
  687. // Yes, so reset lifetime.
  688. setLifetime( 0.0f );
  689. // Initiate Death!
  690. safeDelete();
  691. }
  692. }
  693. }
  694. //-----------------------------------------------------------------------------
  695. void SceneObject::setSceneLayer( const U32 sceneLayer )
  696. {
  697. // Check Layer.
  698. if ( sceneLayer > (MAX_LAYERS_SUPPORTED-1) )
  699. {
  700. Con::warnf("SceneObject::setSceneLayer() - Invalid scene layer '%d' (0-31).", sceneLayer);
  701. return;
  702. }
  703. // Set Layer.
  704. mSceneLayer = sceneLayer;
  705. // Set Layer Mask.
  706. mSceneLayerMask = BIT( mSceneLayer );
  707. }
  708. //-----------------------------------------------------------------------------
  709. bool SceneObject::setSceneLayerDepthFront( void )
  710. {
  711. // Fetch the scene.
  712. Scene* pScene = getScene();
  713. // Finish if no scene or only a single object.
  714. if ( pScene == NULL || pScene->getSceneObjectCount() == 1 )
  715. return false;
  716. // Fetch scene objects.
  717. typeSceneObjectVector layerList;
  718. pScene->getSceneObjects( layerList, getSceneLayer() );
  719. // Fetch layer object count.
  720. const U32 layerObjectCount = (U32)layerList.size();
  721. // Sort the layer.
  722. dQsort(layerList.address(), layerObjectCount, sizeof(SceneObject*), sceneObjectLayerDepthSort);
  723. // Reset object index.
  724. U32 objectIndex = 0;
  725. // Find object index.
  726. for ( ; objectIndex < layerObjectCount; ++objectIndex )
  727. {
  728. // Stop if this is the current object index.
  729. if ( layerList[objectIndex] == this )
  730. break;
  731. }
  732. // Finish if already at front of layer.
  733. if ( objectIndex == layerObjectCount-1 )
  734. return false;
  735. // Fetch furthest front depth.
  736. const F32 frontDepth = layerList.last()->getSceneLayerDepth();
  737. // Adjust depth to be in front.
  738. setSceneLayerDepth( frontDepth - 1.0f );
  739. return true;
  740. }
  741. //-----------------------------------------------------------------------------
  742. bool SceneObject::setSceneLayerDepthBack( void )
  743. {
  744. // Fetch the scene.
  745. Scene* pScene = getScene();
  746. // Finish if no scene or only a single object.
  747. if ( pScene == NULL || pScene->getSceneObjectCount() == 1 )
  748. return false;
  749. // Fetch scene objects.
  750. typeSceneObjectVector layerList;
  751. pScene->getSceneObjects( layerList, getSceneLayer() );
  752. // Fetch layer object count.
  753. const U32 layerObjectCount = (U32)layerList.size();
  754. // Sort the layer.
  755. dQsort(layerList.address(), layerObjectCount, sizeof(SceneObject*), sceneObjectLayerDepthSort);
  756. // Reset object index.
  757. U32 objectIndex = 0;
  758. // Find object index.
  759. for ( ; objectIndex < layerObjectCount; ++objectIndex )
  760. {
  761. // Stop if this is the current object index.
  762. if ( layerList[objectIndex] == this )
  763. break;
  764. }
  765. // Finish if already at back of layer.
  766. if ( objectIndex == 0 )
  767. return false;
  768. // Fetch furthest back depth.
  769. const F32 backDepth = layerList.first()->getSceneLayerDepth();
  770. // Adjust depth to be in back.
  771. setSceneLayerDepth( backDepth + 1.0f );
  772. return true;
  773. }
  774. //-----------------------------------------------------------------------------
  775. bool SceneObject::setSceneLayerDepthForward( void )
  776. {
  777. // Fetch the scene.
  778. Scene* pScene = getScene();
  779. // Finish if no scene or only a single object.
  780. if ( pScene == NULL || pScene->getSceneObjectCount() == 1 )
  781. return false;
  782. // Fetch scene objects.
  783. typeSceneObjectVector layerList;
  784. pScene->getSceneObjects( layerList, getSceneLayer() );
  785. // Fetch layer object count.
  786. const U32 layerObjectCount = (U32)layerList.size();
  787. // Sort the layer.
  788. dQsort(layerList.address(), layerObjectCount, sizeof(SceneObject*), sceneObjectLayerDepthSort);
  789. // Reset object index.
  790. U32 objectIndex = 0;
  791. // Find object index.
  792. for ( ; objectIndex < layerObjectCount; ++objectIndex )
  793. {
  794. // Stop if this is the current object index.
  795. if ( layerList[objectIndex] == this )
  796. break;
  797. }
  798. // Finish if already at the front of the layer.
  799. if ( objectIndex == layerObjectCount-1 )
  800. return false;
  801. // Fetch forwards depth.
  802. const F32 forwardDepth = layerList[objectIndex+1]->getSceneLayerDepth();
  803. // Adjust depth to be forward.
  804. setSceneLayerDepth( forwardDepth - 0.5f );
  805. // Finish if we were almost at the front anyway.
  806. if ( objectIndex == layerObjectCount-2 )
  807. return true;
  808. // Adjust next objects forwards.
  809. for( U32 forwardIndex = objectIndex+2; forwardIndex < layerObjectCount; ++forwardIndex )
  810. {
  811. // Fetch scene object.
  812. SceneObject* forwardSceneObject = layerList[forwardIndex];
  813. // Adjust depth to be forwards.
  814. forwardSceneObject->setSceneLayerDepth( forwardSceneObject->getSceneLayerDepth() - 1.0f );
  815. }
  816. return true;
  817. }
  818. //-----------------------------------------------------------------------------
  819. bool SceneObject::setSceneLayerDepthBackward( void )
  820. {
  821. // Fetch the scene.
  822. Scene* pScene = getScene();
  823. // Finish if no scene or only a single object.
  824. if ( pScene == NULL || pScene->getSceneObjectCount() == 1 )
  825. return false;
  826. // Fetch scene objects.
  827. typeSceneObjectVector layerList;
  828. pScene->getSceneObjects( layerList, getSceneLayer() );
  829. // Fetch layer object count.
  830. const U32 layerObjectCount = (U32)layerList.size();
  831. // Sort the layer.
  832. dQsort(layerList.address(), layerObjectCount, sizeof(SceneObject*), sceneObjectLayerDepthSort);
  833. // Reset object index.
  834. U32 objectIndex = 0;
  835. // Find object index.
  836. for ( ; objectIndex < layerObjectCount; ++objectIndex )
  837. {
  838. // Stop if this is the current object index.
  839. if ( layerList[objectIndex] == this )
  840. break;
  841. }
  842. // Finish if already at the back of the layer.
  843. if ( objectIndex == 0 )
  844. return false;
  845. // Fetch backwards depth.
  846. const F32 backDepth = layerList[objectIndex-1]->getSceneLayerDepth();
  847. // Adjust depth to be backwards.
  848. setSceneLayerDepth( backDepth + 0.5f );
  849. // Finish if we were almost at the back anyway.
  850. if ( objectIndex == 1 )
  851. return true;
  852. // Adjust previous objects backwards.
  853. for( U32 backIndex = 0; backIndex < (objectIndex-1); ++backIndex )
  854. {
  855. // Fetch scene object.
  856. SceneObject* backSceneObject = layerList[backIndex];
  857. // Adjust depth to be backwards.
  858. backSceneObject->setSceneLayerDepth( backSceneObject->getSceneLayerDepth() + 1.0f );
  859. }
  860. return true;
  861. }
  862. //-----------------------------------------------------------------------------
  863. void SceneObject::setSceneGroup( const U32 sceneGroup )
  864. {
  865. // Check Group.
  866. if ( sceneGroup > 31 )
  867. {
  868. Con::warnf("SceneObject::setSceneGroup() - Invalid scene group '%d' (0-31).", sceneGroup);
  869. return;
  870. }
  871. // Set Group.
  872. mSceneGroup = sceneGroup;
  873. // Set Group Mask.
  874. mSceneGroupMask = BIT( mSceneGroup );
  875. }
  876. //-----------------------------------------------------------------------------
  877. void SceneObject::setArea( const Vector2& corner1, const Vector2& corner2 )
  878. {
  879. // Debug Profiling.
  880. PROFILE_SCOPE(SceneObject_SetArea);
  881. // Calculate Normalized region.
  882. const Vector2 topLeft((corner1.x <= corner2.x) ? corner1.x : corner2.x, (corner1.y <= corner2.y) ? corner1.y : corner2.y);
  883. const Vector2 bottomRight((corner1.x > corner2.x) ? corner1.x : corner2.x, (corner1.y > corner2.y) ? corner1.y : corner2.y);
  884. // Calculate New Size.
  885. const Vector2 size = bottomRight - topLeft;
  886. // Calculate New Position.
  887. const Vector2 position = topLeft + (size * 0.5f);
  888. // Reset angle.
  889. setAngle( 0.0f );
  890. // Set Position/Size.
  891. setPosition( position );
  892. setSize( size );
  893. }
  894. //-----------------------------------------------------------------------------
  895. void SceneObject::setSize( const Vector2& size )
  896. {
  897. // Debug Profiling.
  898. PROFILE_SCOPE(SceneObject_SetSize);
  899. mSize = size;
  900. // Calculate half size.
  901. const F32 halfWidth = size.x * 0.5f;
  902. const F32 halfHeight = size.y * 0.5f;
  903. // Set local size OOBB.
  904. mLocalSizeOOBB[0].Set( -halfWidth, -halfHeight );
  905. mLocalSizeOOBB[1].Set( +halfWidth, -halfHeight );
  906. mLocalSizeOOBB[2].Set( +halfWidth, +halfHeight );
  907. mLocalSizeOOBB[3].Set( -halfWidth, +halfHeight );
  908. if ( mpScene )
  909. {
  910. // Reset tick spatials.
  911. resetTickSpatials( true );
  912. }
  913. }
  914. //-----------------------------------------------------------------------------
  915. void SceneObject::setPosition( const Vector2& position )
  916. {
  917. // Debug Profiling.
  918. PROFILE_SCOPE(SceneObject_SetPosition);
  919. if ( mpScene )
  920. {
  921. mpBody->SetTransform( position, mpBody->GetAngle() );
  922. // Reset tick spatials.
  923. resetTickSpatials();
  924. }
  925. else
  926. {
  927. mBodyDefinition.position = position;
  928. }
  929. }
  930. //-----------------------------------------------------------------------------
  931. void SceneObject::setAngle( const F32 radians )
  932. {
  933. // Debug Profiling.
  934. PROFILE_SCOPE(SceneObject_SetAngle);
  935. if ( mpScene )
  936. {
  937. mpBody->SetTransform( mpBody->GetPosition(), radians );
  938. // Reset tick spatials.
  939. resetTickSpatials();
  940. }
  941. else
  942. {
  943. mBodyDefinition.angle = radians;
  944. }
  945. }
  946. //-----------------------------------------------------------------------------
  947. Vector2 SceneObject::getLocalPoint( const Vector2 &worldPoint )
  948. {
  949. if ( mpScene )
  950. {
  951. return mpBody->GetLocalPoint( worldPoint );
  952. }
  953. // Calculate body definition transform.
  954. const b2Transform transform( mBodyDefinition.position, b2Rot(mBodyDefinition.angle) );
  955. return b2MulT(transform, worldPoint);
  956. }
  957. //-----------------------------------------------------------------------------
  958. Vector2 SceneObject::getWorldPoint( const Vector2 &localPoint )
  959. {
  960. if ( mpScene )
  961. {
  962. return mpBody->GetWorldPoint( localPoint );
  963. }
  964. // Calculate body definition transform.
  965. const b2Transform transform( mBodyDefinition.position, b2Rot(mBodyDefinition.angle) );
  966. return b2Mul(transform, localPoint);
  967. }
  968. //-----------------------------------------------------------------------------
  969. Vector2 SceneObject::getLocalVector( const Vector2& worldVector )
  970. {
  971. if ( mpScene )
  972. {
  973. return mpBody->GetLocalVector( worldVector );
  974. }
  975. // Calculate body definition transform.
  976. const b2Transform transform( mBodyDefinition.position, b2Rot(mBodyDefinition.angle) );
  977. return b2MulT(transform.q, worldVector);
  978. }
  979. //-----------------------------------------------------------------------------
  980. Vector2 SceneObject::getWorldVector( const Vector2& localVector )
  981. {
  982. if ( mpScene )
  983. {
  984. return mpBody->GetWorldVector( localVector );
  985. }
  986. // Calculate body definition transform.
  987. const b2Transform transform( mBodyDefinition.position, b2Rot(mBodyDefinition.angle) );
  988. return b2Mul(transform.q, localVector);
  989. }
  990. //-----------------------------------------------------------------------------
  991. bool SceneObject::getIsPointInOOBB( const Vector2& worldPoint )
  992. {
  993. // Fetch local point.
  994. Vector2 localPoint = getLocalPoint( worldPoint );
  995. // Turn the local point into an absolute component distance.
  996. localPoint.absolute();
  997. const Vector2 halfSize = getHalfSize();
  998. // Check distance.
  999. return !( localPoint.x > halfSize.x || localPoint.y > halfSize.y );
  1000. }
  1001. //-----------------------------------------------------------------------------
  1002. bool SceneObject::getIsPointInCollisionShape( const U32 shapeIndex, const Vector2& worldPoint )
  1003. {
  1004. // Sanity!
  1005. AssertFatal( shapeIndex < getCollisionShapeCount(), "SceneObject::getIsPointInCollisionShape() - Invalid shape index." );
  1006. // Sanity!
  1007. if ( !mpScene )
  1008. {
  1009. Con::warnf( "SceneObject::getIsPointInCollisionShape() - Cannot test for point, object not in scene." );
  1010. return false;
  1011. }
  1012. // Fetch fixture.
  1013. b2Fixture* pFixture = mCollisionFixtures[shapeIndex];
  1014. // Fetch local point.
  1015. const Vector2 localPoint = getLocalPoint( worldPoint );
  1016. // Test point.
  1017. return pFixture->TestPoint( localPoint );
  1018. }
  1019. //-----------------------------------------------------------------------------
  1020. void SceneObject::setBodyType( const b2BodyType type )
  1021. {
  1022. // Sanity!
  1023. AssertFatal( type == b2_staticBody || type == b2_kinematicBody || type == b2_dynamicBody, "Invalid body type." );
  1024. if ( mpScene )
  1025. {
  1026. mpBody->SetType( type );
  1027. return;
  1028. }
  1029. else
  1030. {
  1031. mBodyDefinition.type = type;
  1032. }
  1033. }
  1034. //-----------------------------------------------------------------------------
  1035. void SceneObject::applyForce( const Vector2& worldForce, const bool wake )
  1036. {
  1037. // Ignore if not in scene.
  1038. if ( !mpScene )
  1039. return;
  1040. applyForce( worldForce, getPosition() + getLocalCenter(), wake );
  1041. }
  1042. //-----------------------------------------------------------------------------
  1043. void SceneObject::applyForce( const Vector2& worldForce, const Vector2& worldPoint, const bool wake )
  1044. {
  1045. // Ignore if not in scene.
  1046. if ( !mpScene )
  1047. return;
  1048. getBody()->ApplyForce( worldForce, worldPoint, wake );
  1049. }
  1050. //-----------------------------------------------------------------------------
  1051. void SceneObject::applyTorque( const F32 torque, const bool wake )
  1052. {
  1053. // Ignore if not in scene.
  1054. if ( !mpScene )
  1055. return;
  1056. getBody()->ApplyTorque( torque, wake );
  1057. }
  1058. //-----------------------------------------------------------------------------
  1059. void SceneObject::applyLinearImpulse( const Vector2& worldImpulse, const bool wake )
  1060. {
  1061. // Ignore if not in scene.
  1062. if ( !mpScene )
  1063. return;
  1064. applyLinearImpulse( worldImpulse, getPosition() + getLocalCenter(), wake );
  1065. }
  1066. //-----------------------------------------------------------------------------
  1067. void SceneObject::applyLinearImpulse( const Vector2& worldImpulse, const Vector2& worldPoint, const bool wake )
  1068. {
  1069. // Ignore if not in scene.
  1070. if ( !mpScene )
  1071. return;
  1072. getBody()->ApplyLinearImpulse( worldImpulse, worldPoint, wake );
  1073. }
  1074. //-----------------------------------------------------------------------------
  1075. void SceneObject::applyAngularImpulse( const F32 impulse, const bool wake )
  1076. {
  1077. // Ignore if not in scene.
  1078. if ( !mpScene )
  1079. return;
  1080. getBody()->ApplyAngularImpulse( impulse, wake );
  1081. }
  1082. //-----------------------------------------------------------------------------
  1083. void SceneObject::setCollisionAgainst( const SceneObject* pSceneObject, const bool clearMasks )
  1084. {
  1085. // Do we need to clear existing masks?
  1086. if ( clearMasks )
  1087. {
  1088. // Yes, so just set the masks to the referenced-objects' masks.
  1089. setCollisionGroupMask( pSceneObject->getSceneGroupMask() );
  1090. setCollisionLayerMask( pSceneObject->getSceneLayerMask() );
  1091. }
  1092. else
  1093. {
  1094. // No, so merge with existing masks.
  1095. setCollisionGroupMask( getCollisionGroupMask() | pSceneObject->getSceneGroupMask() );
  1096. setCollisionLayerMask( getCollisionLayerMask() | pSceneObject->getSceneLayerMask() );
  1097. }
  1098. }
  1099. //-----------------------------------------------------------------------------
  1100. void SceneObject::setDefaultDensity( const F32 density, const bool updateShapes )
  1101. {
  1102. mDefaultFixture.density = density;
  1103. // Early-out if not updating shapes.
  1104. if ( !updateShapes )
  1105. return;
  1106. if ( mpScene )
  1107. {
  1108. // Update live fixtures.
  1109. for( U32 index = 0; index < (U32)mCollisionFixtures.size(); ++index )
  1110. {
  1111. mCollisionFixtures[index]->SetDensity( density );
  1112. }
  1113. // Update the body mass data.
  1114. mpBody->ResetMassData();
  1115. return;
  1116. }
  1117. // Update offline fixture definitions.
  1118. for( U32 index = 0; index < (U32)mCollisionFixtureDefs.size(); ++index )
  1119. {
  1120. mCollisionFixtureDefs[index]->density = density;
  1121. }
  1122. }
  1123. //-----------------------------------------------------------------------------
  1124. void SceneObject::setDefaultFriction( const F32 friction, const bool updateShapes )
  1125. {
  1126. mDefaultFixture.friction = friction;
  1127. // Early-out if not updating shapes.
  1128. if ( !updateShapes )
  1129. return;
  1130. if ( mpScene )
  1131. {
  1132. // Update live fixtures.
  1133. for( U32 index = 0; index < (U32)mCollisionFixtures.size(); ++index )
  1134. {
  1135. mCollisionFixtures[index]->SetFriction( friction );
  1136. }
  1137. return;
  1138. }
  1139. // Update offline fixture definitions.
  1140. for( U32 index = 0; index < (U32)mCollisionFixtureDefs.size(); ++index )
  1141. {
  1142. mCollisionFixtureDefs[index]->friction = friction;
  1143. }
  1144. }
  1145. //-----------------------------------------------------------------------------
  1146. void SceneObject::setDefaultRestitution( const F32 restitution, const bool updateShapes )
  1147. {
  1148. mDefaultFixture.restitution = restitution;
  1149. // Early-out if not updating shapes.
  1150. if ( !updateShapes )
  1151. return;
  1152. if ( mpScene )
  1153. {
  1154. // Update live fixtures.
  1155. for( U32 index = 0; index < (U32)mCollisionFixtures.size(); ++index )
  1156. {
  1157. mCollisionFixtures[index]->SetRestitution( restitution );
  1158. }
  1159. return;
  1160. }
  1161. // Update offline fixture definitions.
  1162. for( U32 index = 0; index < (U32)mCollisionFixtureDefs.size(); ++index )
  1163. {
  1164. mCollisionFixtureDefs[index]->restitution = restitution;
  1165. }
  1166. }
  1167. //-----------------------------------------------------------------------------
  1168. void SceneObject::initializeContactGathering( void )
  1169. {
  1170. // Are we gathering contacts?
  1171. if ( !mGatherContacts )
  1172. {
  1173. // No, so do we have any current contacts?
  1174. if ( mpCurrentContacts )
  1175. {
  1176. // Yes, so destroy them.
  1177. delete mpCurrentContacts;
  1178. mpCurrentContacts = NULL;
  1179. }
  1180. return;
  1181. }
  1182. // Clear current contacts if already present.
  1183. if ( mpCurrentContacts != NULL )
  1184. {
  1185. mpCurrentContacts->clear();
  1186. return;
  1187. }
  1188. // Generate current contacts.
  1189. mpCurrentContacts = new Scene::typeContactVector();
  1190. }
  1191. //-----------------------------------------------------------------------------
  1192. void SceneObject::onBeginCollision( const TickContact& tickContact )
  1193. {
  1194. // Finish if we're not gathering contacts.
  1195. if ( !mGatherContacts )
  1196. return;
  1197. // Sanity!
  1198. AssertFatal( mpCurrentContacts != NULL, "SceneObject::onBeginCollision() - Contacts not initialized correctly." );
  1199. AssertFatal( tickContact.mpSceneObjectA == this || tickContact.mpSceneObjectB == this, "SceneObject::onBeginCollision() - Contact does not involve this scene object." );
  1200. // Keep contact.
  1201. mpCurrentContacts->push_back( tickContact );
  1202. }
  1203. //-----------------------------------------------------------------------------
  1204. void SceneObject::onEndCollision( const TickContact& tickContact )
  1205. {
  1206. // Finish if we're not gathering contacts.
  1207. if ( !mGatherContacts )
  1208. return;
  1209. // Sanity!
  1210. AssertFatal( mpCurrentContacts != NULL, "SceneObject::onBeginCollision() - Contacts not initialized correctly." );
  1211. AssertFatal( tickContact.mpSceneObjectA == this || tickContact.mpSceneObjectB == this, "SceneObject::onEndCollision() - Contact does not involve this scene object." );
  1212. // Remove contact.
  1213. for( Scene::typeContactVector::iterator contactItr = mpCurrentContacts->begin(); contactItr != mpCurrentContacts->end(); ++contactItr )
  1214. {
  1215. // Is this is the correct contact?
  1216. if ( contactItr->mpContact == tickContact.mpContact )
  1217. {
  1218. // Yes, so remove it.
  1219. mpCurrentContacts->erase_fast( contactItr );
  1220. return;
  1221. }
  1222. }
  1223. }
  1224. //-----------------------------------------------------------------------------
  1225. bool SceneObject::moveTo( const Vector2& targetWorldPoint, const F32 speed, const bool autoStop, const bool warpToTarget )
  1226. {
  1227. // Check in a scene.
  1228. if ( !getScene() )
  1229. {
  1230. Con::warnf("SceneObject::moveTo() - Cannot move object (%d) to a point as it is not in a scene.", getId() );
  1231. return false;
  1232. }
  1233. // Check not a static body.
  1234. if ( getBodyType() == b2_staticBody )
  1235. {
  1236. Con::warnf("SceneObject::moveTo() - Cannot move object (%d) to a point as it is a static body.", getId() );
  1237. return false;
  1238. }
  1239. // Check speed.
  1240. if ( speed <= 0.0f )
  1241. {
  1242. Con::warnf("SceneObject::moveTo() - Speed '%f' is invalid.", speed );
  1243. return false;
  1244. }
  1245. // Cancel any previous event.
  1246. if ( mMoveToEventId != 0 )
  1247. {
  1248. Sim::cancelEvent( mMoveToEventId );
  1249. mMoveToEventId = 0;
  1250. }
  1251. // Calculate the linear velocity for the specified speed.
  1252. Vector2 linearVelocity = targetWorldPoint - getPosition();
  1253. const F32 distance = linearVelocity.Normalize( speed );
  1254. // Calculate the time it will take to reach the target.
  1255. const U32 time = (U32)((distance / speed) * 1000.0f);
  1256. // Set the linear velocity.
  1257. setLinearVelocity( linearVelocity );
  1258. // Create and post event.
  1259. SceneObjectMoveToEvent* pEvent = new SceneObjectMoveToEvent( targetWorldPoint, autoStop, warpToTarget );
  1260. mMoveToEventId = Sim::postEvent(this, pEvent, Sim::getCurrentTime() + time );
  1261. return true;
  1262. }
  1263. //-----------------------------------------------------------------------------
  1264. bool SceneObject::rotateTo( const F32 targetAngle, const F32 speed, const bool autoStop, const bool warpToTarget )
  1265. {
  1266. // Check in a scene.
  1267. if ( !getScene() )
  1268. {
  1269. Con::warnf("SceneObject::rotateTo() - Cannot rotate object (%d) to an angle as it is not in a scene.", getId() );
  1270. return false;
  1271. }
  1272. // Check not a static body.
  1273. if ( getBodyType() == b2_staticBody )
  1274. {
  1275. Con::warnf("SceneObject::rotateTo() - Cannot move object (%d) to an angle as it is a static body.", getId() );
  1276. return false;
  1277. }
  1278. // Check speed.
  1279. if ( speed <= 0.0f )
  1280. {
  1281. Con::warnf("SceneObject::rotateTo() - Speed '%f' is invalid.", speed );
  1282. return false;
  1283. }
  1284. // Cancel any previous event.
  1285. if ( mRotateToEventId != 0 )
  1286. {
  1287. Sim::cancelEvent( mRotateToEventId );
  1288. mRotateToEventId = 0;
  1289. }
  1290. // Calculate relative angle.
  1291. const F32 relativeAngle = targetAngle - getAngle();
  1292. // Calculate delta angle.
  1293. const F32 deltaAngle = mAtan( mSin( relativeAngle ), mCos( relativeAngle ) );
  1294. // Set angular velocity.
  1295. setAngularVelocity( deltaAngle > 0.0f ? speed : -speed );
  1296. // Calculate the time it will take to reach the angle.
  1297. const U32 time = (U32)(mFabs(deltaAngle / speed) * 1000.0f);
  1298. // Create and post event.
  1299. SceneObjectRotateToEvent* pEvent = new SceneObjectRotateToEvent( targetAngle, autoStop, warpToTarget );
  1300. mRotateToEventId = Sim::postEvent(this, pEvent, Sim::getCurrentTime() + time );
  1301. return true;
  1302. }
  1303. //-----------------------------------------------------------------------------
  1304. void SceneObject::cancelMoveTo( const bool autoStop )
  1305. {
  1306. // Only cancel an active moveTo event
  1307. if ( mMoveToEventId != 0 )
  1308. {
  1309. Sim::cancelEvent( mMoveToEventId );
  1310. mMoveToEventId = 0;
  1311. // Should we auto stop?
  1312. if ( autoStop )
  1313. {
  1314. // Yes, so remove linear velocity
  1315. setLinearVelocity( Vector2::getZero() );
  1316. }
  1317. }
  1318. }
  1319. //-----------------------------------------------------------------------------
  1320. void SceneObject::cancelRotateTo( const bool autoStop )
  1321. {
  1322. // Only cancel an active rotateTo event
  1323. if ( mRotateToEventId != 0 )
  1324. {
  1325. Sim::cancelEvent( mRotateToEventId );
  1326. mRotateToEventId = 0;
  1327. // Should we auto stop?
  1328. if ( autoStop )
  1329. {
  1330. // Yes, so remove angular velocity
  1331. setAngularVelocity( 0.0f );
  1332. }
  1333. }
  1334. }
  1335. //-----------------------------------------------------------------------------
  1336. b2Shape::Type SceneObject::getCollisionShapeType( U32 shapeIndex ) const
  1337. {
  1338. // Sanity!
  1339. AssertFatal( shapeIndex < getCollisionShapeCount(), "SceneObject::getCollisionShapeType() - Invalid shape index." );
  1340. if ( mpScene )
  1341. {
  1342. return mCollisionFixtures[shapeIndex]->GetType();
  1343. }
  1344. return mCollisionFixtureDefs[shapeIndex]->shape->GetType();
  1345. }
  1346. //-----------------------------------------------------------------------------
  1347. S32 SceneObject::getCollisionShapeIndex( const b2Fixture* pFixture ) const
  1348. {
  1349. // Iterate collision shapes.
  1350. S32 collisionShapeIndex = 0;
  1351. for( typeCollisionFixtureVector::const_iterator collisionShapeItr = mCollisionFixtures.begin(); collisionShapeItr != mCollisionFixtures.end(); ++collisionShapeItr, ++collisionShapeIndex )
  1352. {
  1353. // Return index if this is the collision shape we are searching for.
  1354. if ( pFixture == *collisionShapeItr )
  1355. return collisionShapeIndex;
  1356. }
  1357. // Not found.
  1358. return -1;
  1359. }
  1360. //-----------------------------------------------------------------------------
  1361. void SceneObject::setCollisionShapeDefinition( const U32 shapeIndex, const b2FixtureDef& fixtureDef )
  1362. {
  1363. // We only set specific features of a fixture definition here.
  1364. setCollisionShapeDensity( shapeIndex, fixtureDef.density );
  1365. setCollisionShapeFriction( shapeIndex, fixtureDef.friction );
  1366. setCollisionShapeRestitution( shapeIndex, fixtureDef.restitution );
  1367. setCollisionShapeIsSensor( shapeIndex, fixtureDef.isSensor );
  1368. }
  1369. //-----------------------------------------------------------------------------
  1370. b2FixtureDef SceneObject::getCollisionShapeDefinition( const U32 shapeIndex ) const
  1371. {
  1372. // Sanity!
  1373. AssertFatal( shapeIndex < getCollisionShapeCount(), "SceneObject::getCollisionShapeDefinition() - Invalid shape index." );
  1374. if ( mpScene )
  1375. {
  1376. // Fetch fixture.
  1377. const b2Fixture* pFixture = mCollisionFixtures[shapeIndex];
  1378. b2FixtureDef fixtureDef;
  1379. fixtureDef.density = pFixture->GetDensity();
  1380. fixtureDef.friction = pFixture->GetFriction();
  1381. fixtureDef.restitution = pFixture->GetRestitution();
  1382. fixtureDef.isSensor = pFixture->IsSensor();
  1383. fixtureDef.filter = pFixture->GetFilterData();
  1384. fixtureDef.shape = pFixture->GetShape();
  1385. return fixtureDef;
  1386. }
  1387. return *mCollisionFixtureDefs[shapeIndex];
  1388. }
  1389. //-----------------------------------------------------------------------------
  1390. const b2CircleShape* SceneObject::getCollisionCircleShape( const U32 shapeIndex ) const
  1391. {
  1392. // Sanity!
  1393. AssertFatal( shapeIndex < getCollisionShapeCount(), "SceneObject::getCollisionCircleShape() - Invalid shape index." );
  1394. // Fetch shape definition.
  1395. const b2FixtureDef fixtureDef = getCollisionShapeDefinition( shapeIndex );
  1396. // Sanity!
  1397. AssertFatal( fixtureDef.shape->GetType() == b2Shape::e_circle, "SceneObject::getCollisionCircleShape() - Shape is not a circle shape." );
  1398. // Fetch circle shape.
  1399. const b2CircleShape* pShape = dynamic_cast<const b2CircleShape*>( fixtureDef.shape );
  1400. // Sanity!
  1401. AssertFatal( pShape != NULL, "SceneObject::getCollisionCircleShape() - Invalid circle shape." );
  1402. return pShape;
  1403. }
  1404. //-----------------------------------------------------------------------------
  1405. const b2PolygonShape* SceneObject::getCollisionPolygonShape( const U32 shapeIndex ) const
  1406. {
  1407. // Sanity!
  1408. AssertFatal( shapeIndex < getCollisionShapeCount(), "SceneObject::getCollisionPolygonShape() - Invalid shape index." );
  1409. // Fetch shape definition.
  1410. const b2FixtureDef fixtureDef = getCollisionShapeDefinition( shapeIndex );
  1411. // Sanity!
  1412. AssertFatal( fixtureDef.shape->GetType() == b2Shape::e_polygon, "SceneObject::getCollisionPolygonShape() - Shape is not a polygon shape." );
  1413. // Fetch shape.
  1414. const b2PolygonShape* pShape = dynamic_cast<const b2PolygonShape*>( fixtureDef.shape );
  1415. // Sanity!
  1416. AssertFatal( pShape != NULL, "SceneObject::getCollisionPolygonShape() - Invalid polygon shape." );
  1417. return pShape;
  1418. }
  1419. //-----------------------------------------------------------------------------
  1420. const b2ChainShape* SceneObject::getCollisionChainShape( const U32 shapeIndex ) const
  1421. {
  1422. // Sanity!
  1423. AssertFatal( shapeIndex < getCollisionShapeCount(), "SceneObject::getCollisionChainShape() - Invalid shape index." );
  1424. // Fetch shape definition.
  1425. const b2FixtureDef fixtureDef = getCollisionShapeDefinition( shapeIndex );
  1426. // Sanity!
  1427. AssertFatal( fixtureDef.shape->GetType() == b2Shape::e_chain, "SceneObject::getCollisionChainShape() - Shape is not a chain shape." );
  1428. // Fetch shape.
  1429. const b2ChainShape* pShape = dynamic_cast<const b2ChainShape*>( fixtureDef.shape );
  1430. // Sanity!
  1431. AssertFatal( pShape != NULL, "SceneObject::getCollisionChainShape() - Invalid chain shape." );
  1432. return pShape;
  1433. }
  1434. //-----------------------------------------------------------------------------
  1435. const b2EdgeShape* SceneObject::getCollisionEdgeShape( const U32 shapeIndex ) const
  1436. {
  1437. // Sanity!
  1438. AssertFatal( shapeIndex < getCollisionShapeCount(), "SceneObject::getCollisionEdgeShape() - Invalid shape index." );
  1439. // Fetch shape definition.
  1440. const b2FixtureDef fixtureDef = getCollisionShapeDefinition( shapeIndex );
  1441. // Sanity!
  1442. AssertFatal( fixtureDef.shape->GetType() == b2Shape::e_edge, "SceneObject::getCollisionEdgeShape() - Shape is not a edge shape." );
  1443. // Fetch shape.
  1444. const b2EdgeShape* pShape = dynamic_cast<const b2EdgeShape*>( fixtureDef.shape );
  1445. // Sanity!
  1446. AssertFatal( pShape != NULL, "SceneObject::getCollisionEdgeShape() - Invalid edge shape." );
  1447. return pShape;
  1448. }
  1449. //-----------------------------------------------------------------------------
  1450. void SceneObject::setCollisionShapeDensity( const U32 shapeIndex, const F32 density )
  1451. {
  1452. // Sanity!
  1453. AssertFatal( shapeIndex < getCollisionShapeCount(), "SceneObject::setCollisionShapeDensity() - Invalid shape index." );
  1454. if ( mpScene )
  1455. {
  1456. // Update live fixture.
  1457. mCollisionFixtures[shapeIndex]->SetDensity( density );
  1458. // Update the body mass data.
  1459. mpBody->ResetMassData();
  1460. return;
  1461. }
  1462. // Update offline fixture definition.
  1463. mCollisionFixtureDefs[shapeIndex]->density = density;
  1464. }
  1465. //-----------------------------------------------------------------------------
  1466. F32 SceneObject::getCollisionShapeDensity( const U32 shapeIndex ) const
  1467. {
  1468. // Sanity!
  1469. AssertFatal( shapeIndex < getCollisionShapeCount(), "SceneObject::getCollisionShapeDensity() - Invalid shape index." );
  1470. if ( mpScene )
  1471. {
  1472. // Use live fixture.
  1473. return mCollisionFixtures[shapeIndex]->GetDensity();
  1474. }
  1475. // Use offline fixture definition.
  1476. return mCollisionFixtureDefs[shapeIndex]->density;
  1477. }
  1478. //-----------------------------------------------------------------------------
  1479. void SceneObject::setCollisionShapeFriction( const U32 shapeIndex, const F32 friction )
  1480. {
  1481. // Sanity!
  1482. AssertFatal( shapeIndex < getCollisionShapeCount(), "SceneObject::setCollisionShapeFriction() - Invalid shape index." );
  1483. if ( mpScene )
  1484. {
  1485. // Fetch the fixture.
  1486. b2Fixture* pFixture = mCollisionFixtures[shapeIndex];
  1487. // Update live fixture.
  1488. pFixture->SetFriction( friction );
  1489. // Re-filter fixture.
  1490. pFixture->Refilter();
  1491. return;
  1492. }
  1493. // Update offline fixture definition.
  1494. mCollisionFixtureDefs[shapeIndex]->friction = friction;
  1495. }
  1496. //-----------------------------------------------------------------------------
  1497. F32 SceneObject::getCollisionShapeFriction( const U32 shapeIndex ) const
  1498. {
  1499. // Sanity!
  1500. AssertFatal( shapeIndex < getCollisionShapeCount(), "SceneObject::getCollisionShapeFriction() - Invalid shape index." );
  1501. if ( mpScene )
  1502. {
  1503. // Use live fixture.
  1504. return mCollisionFixtures[shapeIndex]->GetFriction();
  1505. }
  1506. // Use offline fixture definition.
  1507. return mCollisionFixtureDefs[shapeIndex]->friction;
  1508. }
  1509. //-----------------------------------------------------------------------------
  1510. void SceneObject::setCollisionShapeRestitution( const U32 shapeIndex, const F32 restitution )
  1511. {
  1512. // Sanity!
  1513. AssertFatal( shapeIndex < getCollisionShapeCount(), "SceneObject::setCollisionShapeRestitution() - Invalid shape index." );
  1514. if ( mpScene )
  1515. {
  1516. // Fetch the fixture.
  1517. b2Fixture* pFixture = mCollisionFixtures[shapeIndex];
  1518. // Update live fixture.
  1519. pFixture->SetRestitution( restitution );
  1520. // Re-filter fixture.
  1521. pFixture->Refilter();
  1522. return;
  1523. }
  1524. // Update offline fixture definition.
  1525. mCollisionFixtureDefs[shapeIndex]->restitution = restitution;
  1526. }
  1527. //-----------------------------------------------------------------------------
  1528. F32 SceneObject::getCollisionShapeRestitution( const U32 shapeIndex ) const
  1529. {
  1530. // Sanity!
  1531. AssertFatal( shapeIndex < getCollisionShapeCount(), "SceneObject::getCollisionShapeRestitution() - Invalid shape index." );
  1532. if ( mpScene )
  1533. {
  1534. // Use live fixture.
  1535. return mCollisionFixtures[shapeIndex]->GetRestitution();
  1536. }
  1537. // Use offline fixture definition.
  1538. return mCollisionFixtureDefs[shapeIndex]->restitution;
  1539. }
  1540. //-----------------------------------------------------------------------------
  1541. void SceneObject::setCollisionShapeIsSensor( const U32 shapeIndex, const bool isSensor )
  1542. {
  1543. // Sanity!
  1544. AssertFatal( shapeIndex < getCollisionShapeCount(), "SceneObject::setCollisionShapeIsSensor() - Invalid shape index." );
  1545. if ( mpScene )
  1546. {
  1547. // Fetch the fixture.
  1548. b2Fixture* pFixture = mCollisionFixtures[shapeIndex];
  1549. // Update live fixture.
  1550. pFixture->SetSensor( isSensor );
  1551. // Re-filter fixture.
  1552. pFixture->Refilter();
  1553. return;
  1554. }
  1555. // Update offline fixture definition.
  1556. mCollisionFixtureDefs[shapeIndex]->isSensor = isSensor;
  1557. }
  1558. //-----------------------------------------------------------------------------
  1559. bool SceneObject::getCollisionShapeIsSensor( const U32 shapeIndex ) const
  1560. {
  1561. // Sanity!
  1562. AssertFatal( shapeIndex < getCollisionShapeCount(), "SceneObject::getCollisionShapeIsSensor() - Invalid shape index." );
  1563. if ( mpScene )
  1564. {
  1565. // Use live fixture.
  1566. return mCollisionFixtures[shapeIndex]->IsSensor();
  1567. }
  1568. // Use offline fixture definition.
  1569. return mCollisionFixtureDefs[shapeIndex]->isSensor;
  1570. }
  1571. //-----------------------------------------------------------------------------
  1572. void SceneObject::deleteCollisionShape( const U32 shapeIndex )
  1573. {
  1574. // Sanity!
  1575. AssertFatal( shapeIndex < getCollisionShapeCount(), "SceneObject::deleteCollisionShape() - Invalid shape index." );
  1576. if ( mpScene )
  1577. {
  1578. mpBody->DestroyFixture( mCollisionFixtures[ shapeIndex ] );
  1579. mCollisionFixtures.erase_fast( shapeIndex );
  1580. return;
  1581. }
  1582. mCollisionFixtureDefs.erase_fast( shapeIndex );
  1583. }
  1584. //-----------------------------------------------------------------------------
  1585. void SceneObject::clearCollisionShapes( void )
  1586. {
  1587. // Delete all collision shapes.
  1588. while ( getCollisionShapeCount() > 0 )
  1589. {
  1590. deleteCollisionShape( 0 );
  1591. }
  1592. }
  1593. //-----------------------------------------------------------------------------
  1594. S32 SceneObject::createCircleCollisionShape( const F32 radius )
  1595. {
  1596. // Debug Profiling.
  1597. PROFILE_SCOPE(SceneObject_CreateCircleCollisionShape);
  1598. return createCircleCollisionShape( radius, b2Vec2(0.0f, 0.0f) );
  1599. }
  1600. //-----------------------------------------------------------------------------
  1601. S32 SceneObject::createCircleCollisionShape( const F32 radius, const b2Vec2& localPosition )
  1602. {
  1603. // Sanity!
  1604. if ( radius <= 0.0f )
  1605. {
  1606. Con::errorf("SceneObject::createCircleCollisionShape() - Invalid radius of %g.", radius);
  1607. return -1;
  1608. }
  1609. // Configure fixture definition.
  1610. b2FixtureDef* pFixtureDef = new b2FixtureDef( mDefaultFixture );
  1611. b2CircleShape* pShape = new b2CircleShape();
  1612. pShape->m_p = localPosition;
  1613. pShape->m_radius = radius;
  1614. pFixtureDef->shape = pShape;
  1615. if ( mpScene )
  1616. {
  1617. // Create and push fixture.
  1618. mCollisionFixtures.push_back( mpBody->CreateFixture( pFixtureDef ) );
  1619. // Destroy shape and fixture.
  1620. delete pShape;
  1621. delete pFixtureDef;
  1622. return mCollisionFixtures.size()-1;
  1623. }
  1624. // Push fixture definition.
  1625. mCollisionFixtureDefs.push_back( pFixtureDef );
  1626. return mCollisionFixtureDefs.size()-1;
  1627. }
  1628. //-----------------------------------------------------------------------------
  1629. F32 SceneObject::getCircleCollisionShapeRadius( const U32 shapeIndex ) const
  1630. {
  1631. // Fetch shape.
  1632. const b2CircleShape* pShape = getCollisionCircleShape( shapeIndex );
  1633. return pShape->m_radius;
  1634. }
  1635. //-----------------------------------------------------------------------------
  1636. Vector2 SceneObject::getCircleCollisionShapeLocalPosition( const U32 shapeIndex ) const
  1637. {
  1638. // Fetch shape.
  1639. const b2CircleShape* pShape = getCollisionCircleShape( shapeIndex );
  1640. return pShape->m_p;
  1641. }
  1642. //-----------------------------------------------------------------------------
  1643. S32 SceneObject::createPolygonCollisionShape( const U32 pointCount, const b2Vec2* localPoints )
  1644. {
  1645. // Debug Profiling.
  1646. PROFILE_SCOPE(SceneObject_CreatePolygonCollisionShape);
  1647. // Sanity!
  1648. if ( pointCount < 3 || pointCount > b2_maxPolygonVertices )
  1649. {
  1650. Con::errorf("SceneObject::createPolygonCollisionShape() - Invalid point count of %d.", pointCount);
  1651. return -1;
  1652. }
  1653. // Configure fixture definition.
  1654. b2FixtureDef* pFixtureDef = new b2FixtureDef( mDefaultFixture );
  1655. b2PolygonShape* pShape = new b2PolygonShape();
  1656. pShape->Set( localPoints, pointCount );
  1657. pFixtureDef->shape = pShape;
  1658. if ( mpScene )
  1659. {
  1660. // Create and push fixture.
  1661. mCollisionFixtures.push_back( mpBody->CreateFixture( pFixtureDef ) );
  1662. // Destroy shape and fixture.
  1663. delete pShape;
  1664. delete pFixtureDef;
  1665. return mCollisionFixtures.size()-1;
  1666. }
  1667. // Push fixture definition.
  1668. mCollisionFixtureDefs.push_back( pFixtureDef );
  1669. return mCollisionFixtureDefs.size()-1;
  1670. }
  1671. //-----------------------------------------------------------------------------
  1672. S32 SceneObject::createPolygonBoxCollisionShape( const F32 width, const F32 height )
  1673. {
  1674. // Debug Profiling.
  1675. PROFILE_SCOPE(SceneObject_CreatePolygonBoxCollisionShapeWidthHeight);
  1676. // Sanity!
  1677. if ( width <= 0.0f )
  1678. {
  1679. Con::errorf("SceneObject::createPolygonBoxCollisionShape() - Invalid width of %g.", width);
  1680. return -1;
  1681. }
  1682. if ( height <= 0.0f )
  1683. {
  1684. Con::errorf("SceneObject::createPolygonBoxCollisionShape() - Invalid height of %g.", height);
  1685. return -1;
  1686. }
  1687. // Configure fixture definition.
  1688. b2FixtureDef* pFixtureDef = new b2FixtureDef( mDefaultFixture );
  1689. b2PolygonShape* pShape = new b2PolygonShape();
  1690. pShape->SetAsBox( width * 0.5f, height * 0.5f );
  1691. pFixtureDef->shape = pShape;
  1692. if ( mpScene )
  1693. {
  1694. // Create and push fixture.
  1695. mCollisionFixtures.push_back( mpBody->CreateFixture( pFixtureDef ) );
  1696. // Destroy shape and fixture.
  1697. delete pShape;
  1698. delete pFixtureDef;
  1699. return mCollisionFixtures.size()-1;
  1700. }
  1701. // Push fixture definition.
  1702. mCollisionFixtureDefs.push_back( pFixtureDef );
  1703. return mCollisionFixtureDefs.size()-1;
  1704. }
  1705. //-----------------------------------------------------------------------------
  1706. S32 SceneObject::createPolygonBoxCollisionShape( const F32 width, const F32 height, const b2Vec2& localCentroid )
  1707. {
  1708. // Debug Profiling.
  1709. PROFILE_SCOPE(SceneObject_CreatePolygonBoxCollisionShapeWidthHeightCentroid);
  1710. // Sanity!
  1711. if ( width <= 0.0f )
  1712. {
  1713. Con::errorf("SceneObject::createPolygonBoxCollisionShape() - Invalid width of %g.", width);
  1714. return -1;
  1715. }
  1716. if ( height <= 0.0f )
  1717. {
  1718. Con::errorf("SceneObject::createPolygonBoxCollisionShape() - Invalid height of %g.", height);
  1719. return -1;
  1720. }
  1721. // Configure fixture definition.
  1722. b2FixtureDef* pFixtureDef = new b2FixtureDef( mDefaultFixture );
  1723. b2PolygonShape* pShape = new b2PolygonShape();
  1724. pShape->SetAsBox( width * 0.5f, height * 0.5f, localCentroid, 0.0f );
  1725. pFixtureDef->shape = pShape;
  1726. if ( mpScene )
  1727. {
  1728. // Create and push fixture.
  1729. mCollisionFixtures.push_back( mpBody->CreateFixture( pFixtureDef ) );
  1730. // Destroy shape and fixture.
  1731. delete pShape;
  1732. delete pFixtureDef;
  1733. return mCollisionFixtures.size()-1;
  1734. }
  1735. // Push fixture definition.
  1736. mCollisionFixtureDefs.push_back( pFixtureDef );
  1737. return mCollisionFixtureDefs.size()-1;
  1738. }
  1739. //-----------------------------------------------------------------------------
  1740. S32 SceneObject::createPolygonBoxCollisionShape( const F32 width, const F32 height, const b2Vec2& localCentroid, const F32 rotation )
  1741. {
  1742. // Debug Profiling.
  1743. PROFILE_SCOPE(SceneObject_CreatePolygonBoxCollisionShapeWidthHeightCentroidRotation);
  1744. // Sanity!
  1745. if ( width <= 0.0f )
  1746. {
  1747. Con::errorf("SceneObject::createPolygonBoxCollisionShape() - Invalid width of %g.", width);
  1748. return -1;
  1749. }
  1750. if ( height <= 0.0f )
  1751. {
  1752. Con::errorf("SceneObject::createPolygonBoxCollisionShape() - Invalid height of %g.", height);
  1753. return -1;
  1754. }
  1755. // Configure fixture definition.
  1756. b2FixtureDef* pFixtureDef = new b2FixtureDef( mDefaultFixture );
  1757. b2PolygonShape* pShape = new b2PolygonShape();
  1758. pShape->SetAsBox( width * 0.5f, height * 0.5f, localCentroid, rotation );
  1759. pFixtureDef->shape = pShape;
  1760. if ( mpScene )
  1761. {
  1762. // Create and push fixture.
  1763. mCollisionFixtures.push_back( mpBody->CreateFixture( pFixtureDef ) );
  1764. // Destroy shape and fixture.
  1765. delete pShape;
  1766. delete pFixtureDef;
  1767. return mCollisionFixtures.size()-1;
  1768. }
  1769. // Push fixture definition.
  1770. mCollisionFixtureDefs.push_back( pFixtureDef );
  1771. return mCollisionFixtureDefs.size()-1;
  1772. }
  1773. //-----------------------------------------------------------------------------
  1774. U32 SceneObject::getPolygonCollisionShapePointCount( const U32 shapeIndex ) const
  1775. {
  1776. // Fetch shape.
  1777. const b2PolygonShape* pShape = getCollisionPolygonShape( shapeIndex );
  1778. return pShape->GetVertexCount();
  1779. }
  1780. //-----------------------------------------------------------------------------
  1781. Vector2 SceneObject::getPolygonCollisionShapeLocalPoint( const U32 shapeIndex, const U32 pointIndex ) const
  1782. {
  1783. // Fetch shape.
  1784. const b2PolygonShape* pShape = getCollisionPolygonShape( shapeIndex );
  1785. // Sanity!
  1786. AssertFatal( pointIndex < (U32)pShape->GetVertexCount(), "SceneObject::getPolygonCollisionShapeLocalPoint() - Invalid local point index." );
  1787. return pShape->GetVertex( pointIndex );
  1788. }
  1789. //-----------------------------------------------------------------------------
  1790. S32 SceneObject::createChainCollisionShape( const U32 pointCount, const b2Vec2* localPoints )
  1791. {
  1792. // Debug Profiling.
  1793. PROFILE_SCOPE(SceneObject_CreateChainCollisionShape);
  1794. // Sanity!
  1795. if ( pointCount < 2 )
  1796. {
  1797. Con::errorf("SceneObject::createChainCollisionShape() - Invalid point count of %d.", pointCount);
  1798. return -1;
  1799. }
  1800. // Configure fixture definition.
  1801. b2FixtureDef* pFixtureDef = new b2FixtureDef( mDefaultFixture );
  1802. b2ChainShape* pShape = new b2ChainShape();
  1803. pShape->CreateChain( localPoints, pointCount );
  1804. pFixtureDef->shape = pShape;
  1805. if ( mpScene )
  1806. {
  1807. // Create and push fixture.
  1808. mCollisionFixtures.push_back( mpBody->CreateFixture( pFixtureDef ) );
  1809. // Destroy shape and fixture.
  1810. delete pShape;
  1811. delete pFixtureDef;
  1812. return mCollisionFixtures.size()-1;
  1813. }
  1814. // Push fixture definition.
  1815. mCollisionFixtureDefs.push_back( pFixtureDef );
  1816. return mCollisionFixtureDefs.size()-1;
  1817. }
  1818. //-----------------------------------------------------------------------------
  1819. S32 SceneObject::createChainCollisionShape( const U32 pointCount, const b2Vec2* localPoints,
  1820. const bool hasAdjacentLocalPositionStart, const bool hasAdjacentLocalPositionEnd,
  1821. const b2Vec2& adjacentLocalPositionStart, const b2Vec2& adjacentLocalPositionEnd )
  1822. {
  1823. // Debug Profiling.
  1824. PROFILE_SCOPE(SceneObject_CreateChainCollisionShapeAdjacents);
  1825. // Sanity!
  1826. if ( pointCount < 2 )
  1827. {
  1828. Con::errorf("SceneObject::createChainCollisionShape() - Invalid point count of %d.", pointCount);
  1829. return -1;
  1830. }
  1831. // Configure fixture definition.
  1832. b2FixtureDef* pFixtureDef = new b2FixtureDef( mDefaultFixture );
  1833. b2ChainShape* pShape = new b2ChainShape();
  1834. pShape->CreateChain( localPoints, pointCount );
  1835. if ( hasAdjacentLocalPositionStart )
  1836. pShape->SetPrevVertex( adjacentLocalPositionStart );
  1837. if ( hasAdjacentLocalPositionEnd )
  1838. pShape->SetNextVertex( adjacentLocalPositionEnd );
  1839. pFixtureDef->shape = pShape;
  1840. if ( mpScene )
  1841. {
  1842. // Create and push fixture.
  1843. mCollisionFixtures.push_back( mpBody->CreateFixture( pFixtureDef ) );
  1844. // Destroy shape and fixture.
  1845. delete pShape;
  1846. delete pFixtureDef;
  1847. return mCollisionFixtures.size()-1;
  1848. }
  1849. // Push fixture definition.
  1850. mCollisionFixtureDefs.push_back( pFixtureDef );
  1851. return mCollisionFixtureDefs.size()-1;
  1852. }
  1853. //-----------------------------------------------------------------------------
  1854. U32 SceneObject::getChainCollisionShapePointCount( const U32 shapeIndex ) const
  1855. {
  1856. // Fetch shape.
  1857. const b2ChainShape* pShape = getCollisionChainShape( shapeIndex );
  1858. return pShape->m_count;
  1859. }
  1860. //-----------------------------------------------------------------------------
  1861. Vector2 SceneObject::getChainCollisionShapeLocalPoint( const U32 shapeIndex, const U32 pointIndex ) const
  1862. {
  1863. // Fetch shape.
  1864. const b2ChainShape* pShape = getCollisionChainShape( shapeIndex );
  1865. // Sanity!
  1866. AssertFatal( pointIndex < (U32)pShape->m_count, "SceneObject::getChainCollisionShapeLocalPoint() - Invalid local point index." );
  1867. return pShape->m_vertices[ pointIndex ];
  1868. }
  1869. //-----------------------------------------------------------------------------
  1870. bool SceneObject::getChainCollisionShapeHasAdjacentStart( const U32 shapeIndex ) const
  1871. {
  1872. // Fetch shape.
  1873. const b2ChainShape* pShape = getCollisionChainShape( shapeIndex );
  1874. return pShape->m_hasPrevVertex;
  1875. }
  1876. //-----------------------------------------------------------------------------
  1877. bool SceneObject::getChainCollisionShapeHasAdjacentEnd( const U32 shapeIndex ) const
  1878. {
  1879. // Fetch shape.
  1880. const b2ChainShape* pShape = getCollisionChainShape( shapeIndex );
  1881. return pShape->m_hasNextVertex;
  1882. }
  1883. //-----------------------------------------------------------------------------
  1884. Vector2 SceneObject::getChainCollisionShapeAdjacentStart( const U32 shapeIndex ) const
  1885. {
  1886. // Fetch shape.
  1887. const b2ChainShape* pShape = getCollisionChainShape( shapeIndex );
  1888. return pShape->m_prevVertex;
  1889. }
  1890. //-----------------------------------------------------------------------------
  1891. Vector2 SceneObject::getChainCollisionShapeAdjacentEnd( const U32 shapeIndex ) const
  1892. {
  1893. // Fetch shape.
  1894. const b2ChainShape* pShape = getCollisionChainShape( shapeIndex );
  1895. return pShape->m_nextVertex;
  1896. }
  1897. //-----------------------------------------------------------------------------
  1898. S32 SceneObject::createEdgeCollisionShape( const b2Vec2& localPositionStart, const b2Vec2& localPositionEnd )
  1899. {
  1900. // Debug Profiling.
  1901. PROFILE_SCOPE(SceneObject_CreateEdgeCollisionShape);
  1902. // Configure fixture definition.
  1903. b2FixtureDef* pFixtureDef = new b2FixtureDef( mDefaultFixture );
  1904. b2EdgeShape* pShape = new b2EdgeShape();
  1905. pShape->Set( localPositionStart, localPositionEnd );
  1906. pFixtureDef->shape = pShape;
  1907. if ( mpScene )
  1908. {
  1909. // Create and push fixture.
  1910. mCollisionFixtures.push_back( mpBody->CreateFixture( pFixtureDef ) );
  1911. // Destroy shape and fixture.
  1912. delete pShape;
  1913. delete pFixtureDef;
  1914. return mCollisionFixtures.size()-1;
  1915. }
  1916. // Push fixture definition.
  1917. mCollisionFixtureDefs.push_back( pFixtureDef );
  1918. return mCollisionFixtureDefs.size()-1;
  1919. }
  1920. //-----------------------------------------------------------------------------
  1921. S32 SceneObject::createEdgeCollisionShape( const b2Vec2& localPositionStart, const b2Vec2& localPositionEnd,
  1922. const bool hasAdjacentLocalPositionStart, const bool hasAdjacentLocalPositionEnd,
  1923. const b2Vec2& adjacentLocalPositionStart, const b2Vec2& adjacentLocalPositionEnd )
  1924. {
  1925. // Debug Profiling.
  1926. PROFILE_SCOPE(SceneObject_CreateEdgeCollisionShapeAdjacents);
  1927. // Configure fixture definition.
  1928. b2FixtureDef* pFixtureDef = new b2FixtureDef( mDefaultFixture );
  1929. b2EdgeShape* pShape = new b2EdgeShape();
  1930. pShape->Set( localPositionStart, localPositionEnd );
  1931. pShape->m_hasVertex0 = hasAdjacentLocalPositionStart;
  1932. pShape->m_hasVertex3 = hasAdjacentLocalPositionEnd;
  1933. pShape->m_vertex0 = adjacentLocalPositionStart;
  1934. pShape->m_vertex3 = adjacentLocalPositionEnd;
  1935. pFixtureDef->shape = pShape;
  1936. if ( mpScene )
  1937. {
  1938. // Create and push fixture.
  1939. mCollisionFixtures.push_back( mpBody->CreateFixture( pFixtureDef ) );
  1940. // Destroy shape and fixture.
  1941. delete pShape;
  1942. delete pFixtureDef;
  1943. return mCollisionFixtures.size()-1;
  1944. }
  1945. // Push fixture definition.
  1946. mCollisionFixtureDefs.push_back( pFixtureDef );
  1947. return mCollisionFixtureDefs.size()-1;
  1948. }
  1949. //-----------------------------------------------------------------------------
  1950. Vector2 SceneObject::getEdgeCollisionShapeLocalPositionStart( const U32 shapeIndex ) const
  1951. {
  1952. // Fetch shape.
  1953. const b2EdgeShape* pShape = getCollisionEdgeShape( shapeIndex );
  1954. return pShape->m_vertex1;
  1955. }
  1956. //-----------------------------------------------------------------------------
  1957. Vector2 SceneObject::getEdgeCollisionShapeLocalPositionEnd( const U32 shapeIndex ) const
  1958. {
  1959. // Fetch shape.
  1960. const b2EdgeShape* pShape = getCollisionEdgeShape( shapeIndex );
  1961. return pShape->m_vertex2;
  1962. }
  1963. //-----------------------------------------------------------------------------
  1964. bool SceneObject::getEdgeCollisionShapeHasAdjacentStart( const U32 shapeIndex ) const
  1965. {
  1966. // Fetch shape.
  1967. const b2EdgeShape* pShape = getCollisionEdgeShape( shapeIndex );
  1968. return pShape->m_hasVertex0;
  1969. }
  1970. //-----------------------------------------------------------------------------
  1971. bool SceneObject::getEdgeCollisionShapeHasAdjacentEnd( const U32 shapeIndex ) const
  1972. {
  1973. // Fetch shape.
  1974. const b2EdgeShape* pShape = getCollisionEdgeShape( shapeIndex );
  1975. return pShape->m_hasVertex3;
  1976. }
  1977. //-----------------------------------------------------------------------------
  1978. Vector2 SceneObject::getEdgeCollisionShapeAdjacentStart( const U32 shapeIndex ) const
  1979. {
  1980. // Fetch shape.
  1981. const b2EdgeShape* pShape = getCollisionEdgeShape( shapeIndex );
  1982. return pShape->m_vertex0;
  1983. }
  1984. //-----------------------------------------------------------------------------
  1985. Vector2 SceneObject::getEdgeCollisionShapeAdjacentEnd( const U32 shapeIndex ) const
  1986. {
  1987. // Fetch shape.
  1988. const b2EdgeShape* pShape = getCollisionEdgeShape( shapeIndex );
  1989. return pShape->m_vertex3;
  1990. }
  1991. //-----------------------------------------------------------------------------
  1992. void SceneObject::setBlendOptions( void )
  1993. {
  1994. // Set Blend Status.
  1995. if ( mBlendMode )
  1996. {
  1997. // Enable Blending.
  1998. glEnable( GL_BLEND );
  1999. // Set Blend Function.
  2000. glBlendFunc( mSrcBlendFactor, mDstBlendFactor );
  2001. // Set Colour.
  2002. glColor4f(mBlendColor.red,mBlendColor.green,mBlendColor.blue,mBlendColor.alpha );
  2003. }
  2004. else
  2005. {
  2006. // Disable Blending.
  2007. glDisable( GL_BLEND );
  2008. // Reset Colour.
  2009. glColor4f(1,1,1,1);
  2010. }
  2011. // Set Alpha Test.
  2012. if ( mAlphaTest >= 0.0f )
  2013. {
  2014. // Enable Test.
  2015. glEnable( GL_ALPHA_TEST );
  2016. glAlphaFunc( GL_GREATER, mAlphaTest );
  2017. }
  2018. else
  2019. {
  2020. // Disable Test.
  2021. glDisable( GL_ALPHA_TEST );
  2022. }
  2023. }
  2024. //-----------------------------------------------------------------------------
  2025. void SceneObject::resetBlendOptions( void )
  2026. {
  2027. // Disable Blending.
  2028. glDisable( GL_BLEND );
  2029. glDisable( GL_ALPHA_TEST);
  2030. // Reset Colour.
  2031. glColor4f(1,1,1,1);
  2032. }
  2033. //---------------------------------------------------------------------------------------------
  2034. void SceneObject::onInputEvent( StringTableEntry name, const GuiEvent& event, const Vector2& worldMousePosition )
  2035. {
  2036. // Debug Profiling.
  2037. PROFILE_SCOPE(SceneObject_OnInputEvent);
  2038. // Argument Buffers.
  2039. char argBuffer[3][32];
  2040. // ID
  2041. dSprintf(argBuffer[0], 32, "%d", event.eventID);
  2042. // Format Mouse-Position Buffer.
  2043. dSprintf(argBuffer[1], 32, "%g %g", worldMousePosition.x, worldMousePosition.y);
  2044. // Optional double click
  2045. dSprintf(argBuffer[2], 32, "%d", event.mouseClickCount);
  2046. // Call Scripts.
  2047. Con::executef(this, 4, name, argBuffer[0], argBuffer[1], argBuffer[2]);
  2048. }
  2049. //-----------------------------------------------------------------------------
  2050. void SceneObject::attachGui( GuiControl* pGuiControl, SceneWindow* pSceneWindow, const bool sizeControl )
  2051. {
  2052. // Attach Gui Control.
  2053. mpAttachedGui = pGuiControl;
  2054. // Attach SceneWindow.
  2055. mpAttachedGuiSceneWindow = pSceneWindow;
  2056. // Set Size Gui Flag.
  2057. mAttachedGuiSizeControl = sizeControl;
  2058. // Register Gui Control/Window References.
  2059. mpAttachedGui->registerReference( (SimObject**)&mpAttachedGui );
  2060. mpAttachedGuiSceneWindow->registerReference( (SimObject**)&mpAttachedGuiSceneWindow );
  2061. // Check/Adjust Parentage.
  2062. if ( mpAttachedGui->getParent() != mpAttachedGuiSceneWindow )
  2063. {
  2064. // Warn.
  2065. // Remove GuiControl from existing parent (if it has one).
  2066. if ( mpAttachedGui->getParent() )
  2067. {
  2068. mpAttachedGui->getParent()->removeObject( mpAttachedGui );
  2069. }
  2070. // Add it to the scene-window.
  2071. mpAttachedGuiSceneWindow->addObject( mpAttachedGui );
  2072. }
  2073. }
  2074. //-----------------------------------------------------------------------------
  2075. void SceneObject::detachGui( void )
  2076. {
  2077. // Unregister Gui Control Reference.
  2078. if ( mpAttachedGui )
  2079. {
  2080. // [neo, 5/7/2007 - #2997]
  2081. // Changed to UNregisterReference was registerReference which would crash later
  2082. mpAttachedGui->unregisterReference( (SimObject**)&mpAttachedGui );
  2083. mpAttachedGui = NULL;
  2084. }
  2085. // Unregister Gui Control Reference.
  2086. if ( mpAttachedGuiSceneWindow )
  2087. {
  2088. mpAttachedGuiSceneWindow->registerReference( (SimObject**)&mpAttachedGuiSceneWindow );
  2089. mpAttachedGuiSceneWindow = NULL;
  2090. }
  2091. }
  2092. //-----------------------------------------------------------------------------
  2093. void SceneObject::updateAttachedGui( void )
  2094. {
  2095. // Debug Profiling.
  2096. PROFILE_SCOPE(SceneObject_updateAttachedGui);
  2097. // Finish if either Gui Control or Window is invalid.
  2098. if ( !mpAttachedGui || !mpAttachedGuiSceneWindow )
  2099. return;
  2100. // Ignore if we're not in the scene that the scene-window is attached to.
  2101. if ( getScene() != mpAttachedGuiSceneWindow->getScene() )
  2102. {
  2103. // Warn.
  2104. Con::warnf("SceneObject::updateAttachedGui() - SceneWindow is not attached to my Scene!");
  2105. // Detach from GUI Control.
  2106. detachGui();
  2107. // Finish Here.
  2108. return;
  2109. }
  2110. // Calculate the GUI Controls' dimensions.
  2111. Point2I topLeftI, extentI;
  2112. // Size Control?
  2113. if ( mAttachedGuiSizeControl )
  2114. {
  2115. // Yes, so fetch Clip Rectangle; this forms the area we want to fix the Gui-Control to.
  2116. const RectF objAABB = getAABBRectangle();
  2117. // Fetch Top-Left.
  2118. Vector2 upperLeft = Vector2( objAABB.point.x, objAABB.point.y + objAABB.extent.y );
  2119. Vector2 lowerRight = Vector2( objAABB.point.x + objAABB.extent.x, objAABB.point.y );
  2120. // Convert Scene to Window Coordinates.
  2121. mpAttachedGuiSceneWindow->sceneToWindowPoint( upperLeft, upperLeft );
  2122. mpAttachedGuiSceneWindow->sceneToWindowPoint( lowerRight, lowerRight );
  2123. // Convert Control Dimensions.
  2124. topLeftI.set( S32(upperLeft.x), S32(upperLeft.y) );
  2125. extentI.set( S32(lowerRight.x-upperLeft.x), S32(lowerRight.y-upperLeft.y) );
  2126. }
  2127. else
  2128. {
  2129. // No, so center GUI-Control on objects position but don't resize it.
  2130. // Calculate Position from World Clip.
  2131. const RectF clipRectangle = getAABBRectangle();
  2132. // Calculate center position.
  2133. const Vector2 centerPosition = clipRectangle.point + Vector2(clipRectangle.len_x()*0.5f, clipRectangle.len_y()*0.5f);
  2134. // Convert Scene to Window Coordinates.
  2135. Vector2 positionI;
  2136. mpAttachedGuiSceneWindow->sceneToWindowPoint( centerPosition, positionI );
  2137. // Fetch Control Extents (which don't change here).
  2138. extentI = mpAttachedGui->getExtent();
  2139. // Calculate new top-left.
  2140. topLeftI.set( S32(positionI.x-extentI.x/2), S32(positionI.y-extentI.y/2) );
  2141. }
  2142. // Set Control Dimensions.
  2143. mpAttachedGui->resize( topLeftI, extentI );
  2144. }
  2145. //-----------------------------------------------------------------------------
  2146. void SceneObject::copyFrom( SceneObject* pSceneObject, const bool copyDynamicFields )
  2147. {
  2148. // Copy the specified object.
  2149. pSceneObject->copyTo( this );
  2150. // Copy over dynamic fields if requested.
  2151. if ( copyDynamicFields )
  2152. pSceneObject->assignDynamicFieldsFrom( this );
  2153. }
  2154. //-----------------------------------------------------------------------------
  2155. void SceneObject::copyTo( SimObject* obj )
  2156. {
  2157. // Debug Profiling.
  2158. PROFILE_SCOPE(SceneObject_CopyTo);
  2159. // Call parent.
  2160. Parent::copyTo(obj);
  2161. // Fetch object.
  2162. SceneObject* pSceneObject = dynamic_cast<SceneObject*>(obj);
  2163. // Sanity!
  2164. AssertFatal(pSceneObject != NULL, "SceneObject::copyTo() - Object is not the correct type.");
  2165. /// Lifetime.
  2166. pSceneObject->setLifetime( getLifetime() );
  2167. /// Scene Layers.
  2168. pSceneObject->setSceneLayer( getSceneLayer() );
  2169. /// Scene groups.
  2170. pSceneObject->setSceneGroup( getSceneGroup() );
  2171. /// Area.
  2172. pSceneObject->setSize( getSize() );
  2173. /// Position / Angle.
  2174. pSceneObject->setPosition( getPosition() );
  2175. pSceneObject->setAngle( getAngle() );
  2176. pSceneObject->setFixedAngle( getFixedAngle() );
  2177. /// Body.
  2178. pSceneObject->setBodyType( getBodyType() );
  2179. pSceneObject->setActive( getActive() );
  2180. pSceneObject->setAwake( getAwake() );
  2181. pSceneObject->setBullet( getBullet() );
  2182. pSceneObject->setSleepingAllowed( getSleepingAllowed() );
  2183. /// Collision control.
  2184. pSceneObject->setCollisionGroupMask( getCollisionGroupMask() );
  2185. pSceneObject->setCollisionLayerMask( getCollisionLayerMask() );
  2186. pSceneObject->setCollisionSuppress( getCollisionSuppress() );
  2187. pSceneObject->setGatherContacts( getGatherContacts() );
  2188. pSceneObject->setDefaultDensity( getDefaultDensity() );
  2189. pSceneObject->setDefaultFriction( getDefaultFriction() );
  2190. pSceneObject->setDefaultRestitution( getDefaultRestitution() );
  2191. /// Velocities.
  2192. pSceneObject->setLinearVelocity( getLinearVelocity() );
  2193. pSceneObject->setAngularVelocity( getAngularVelocity() );
  2194. pSceneObject->setLinearDamping( getLinearDamping() );
  2195. pSceneObject->setAngularDamping( getAngularDamping() );
  2196. /// Gravity scaling.
  2197. pSceneObject->setGravityScale( getGravityScale() );
  2198. /// Collision shapes.
  2199. copyCollisionShapes( pSceneObject, true );
  2200. /// Render visibility.
  2201. pSceneObject->setVisible( getVisible() );
  2202. /// Render blending.
  2203. pSceneObject->setBlendMode( getBlendMode() );
  2204. pSceneObject->setSrcBlendFactor( getSrcBlendFactor() );
  2205. pSceneObject->setDstBlendFactor( getDstBlendFactor() );
  2206. pSceneObject->setBlendColor( getBlendColor() );
  2207. pSceneObject->setAlphaTest( getAlphaTest() );
  2208. /// Render sorting.
  2209. pSceneObject->setSortPoint( getSortPoint() );
  2210. /// Input events.
  2211. pSceneObject->setUseInputEvents( getUseInputEvents() );
  2212. // Script callbacks.
  2213. pSceneObject->setUpdateCallback( getUpdateCallback() );
  2214. pSceneObject->setCollisionCallback( getCollisionCallback() );
  2215. pSceneObject->setSleepingCallback( getSleepingCallback() );
  2216. /// Misc.
  2217. pSceneObject->setBatchIsolated( getBatchIsolated() );
  2218. /// Debug mode.
  2219. setDebugOn( getDebugMask() );
  2220. }
  2221. //-----------------------------------------------------------------------------
  2222. S32 SceneObject::copyCollisionShapes( SceneObject* pSceneObject, const bool clearTargetShapes, const S32 shapeIndex )
  2223. {
  2224. // Sanity!
  2225. AssertFatal( pSceneObject != NULL, "SceneObject::copyCollisionShapes() - Cannot copy to a NULL scene object." );
  2226. // Clear the collision shapes.
  2227. if ( clearTargetShapes )
  2228. pSceneObject->clearCollisionShapes();
  2229. // Fetch collision shape count.
  2230. const U32 collisionShapeCount = getCollisionShapeCount();
  2231. // If a shape index is specified, is it valid?
  2232. if ( shapeIndex != INVALID_COLLISION_SHAPE_INDEX && shapeIndex >= (S32)collisionShapeCount )
  2233. {
  2234. // No, so warn.
  2235. Con::warnf( "SceneObject::copyCollisionShapes() - Invalid shape index '%d'.", shapeIndex );
  2236. return INVALID_COLLISION_SHAPE_INDEX;
  2237. }
  2238. // Finish if there are no collision shapes.
  2239. if ( collisionShapeCount == 0 )
  2240. return INVALID_COLLISION_SHAPE_INDEX;
  2241. // Calculate shape range.
  2242. const U32 startShapeIndex = shapeIndex >= 0 ? shapeIndex : 0;
  2243. const U32 endShapeIndex = shapeIndex >= 0 ? shapeIndex : collisionShapeCount -1;
  2244. // Iterate collision shapes.
  2245. for ( U32 index = startShapeIndex; index <= endShapeIndex; ++index )
  2246. {
  2247. b2FixtureDef fixtureDef;
  2248. if ( mpScene )
  2249. {
  2250. // Fetch fixture.
  2251. b2Fixture* pFixture = mCollisionFixtures[index];
  2252. // Fetch common details.
  2253. fixtureDef.density = pFixture->GetDensity();
  2254. fixtureDef.friction = pFixture->GetFriction();
  2255. fixtureDef.restitution = pFixture->GetRestitution();
  2256. fixtureDef.isSensor = pFixture->IsSensor();
  2257. fixtureDef.shape = pFixture->GetShape();
  2258. }
  2259. else
  2260. {
  2261. // Fetch fixture def.
  2262. b2FixtureDef* pFixtureDef = mCollisionFixtureDefs[index];
  2263. // Fetch common details.
  2264. fixtureDef = *pFixtureDef;
  2265. }
  2266. S32 newShapeIndex;
  2267. // Fetch shape type.
  2268. const b2Shape::Type shapeType = fixtureDef.shape->GetType();
  2269. // Copy appropriate shape type.
  2270. switch( shapeType )
  2271. {
  2272. case b2Shape::e_circle:
  2273. newShapeIndex = copyCircleCollisionShapeTo( pSceneObject, fixtureDef );
  2274. // Return the new shape if we're copying a specific index.
  2275. if ( shapeIndex >= 0 )
  2276. return newShapeIndex;
  2277. continue;
  2278. case b2Shape::e_polygon:
  2279. newShapeIndex = copyPolygonCollisionShapeTo( pSceneObject, fixtureDef );
  2280. // Return the new shape if we're copying a specific index.
  2281. if ( shapeIndex >= 0 )
  2282. return newShapeIndex;
  2283. continue;
  2284. case b2Shape::e_chain:
  2285. newShapeIndex = copyChainCollisionShapeTo( pSceneObject, fixtureDef );
  2286. // Return the new shape if we're copying a specific index.
  2287. if ( shapeIndex >= 0 )
  2288. return newShapeIndex;
  2289. continue;
  2290. case b2Shape::e_edge:
  2291. newShapeIndex = copyEdgeCollisionShapeTo( pSceneObject, fixtureDef );
  2292. // Return the new shape if we're copying a specific index.
  2293. if ( shapeIndex >= 0 )
  2294. return newShapeIndex;
  2295. continue;
  2296. default:
  2297. AssertFatal( false, "SceneObject::copyCollisionShapes() - Unsupported collision shape type encountered." );
  2298. }
  2299. }
  2300. // Return the first index if we're copying all the shapes.
  2301. if ( shapeIndex < 0 )
  2302. return 0;
  2303. return INVALID_COLLISION_SHAPE_INDEX;
  2304. }
  2305. //-----------------------------------------------------------------------------
  2306. S32 SceneObject::copyCircleCollisionShapeTo( SceneObject* pSceneObject, const b2FixtureDef& fixtureDef ) const
  2307. {
  2308. // Fetch shape.
  2309. const b2CircleShape* pShape = dynamic_cast<const b2CircleShape*>( fixtureDef.shape );
  2310. // Check shape.
  2311. if ( !pShape )
  2312. {
  2313. Con::errorf("SceneObject::copyCircleCollisionShapeTo() - Invalid shape.");
  2314. return INVALID_COLLISION_SHAPE_INDEX;
  2315. }
  2316. // Fetch shape details.
  2317. const F32 radius = pShape->m_radius;
  2318. const b2Vec2 localPosition = pShape->m_p;
  2319. // Copy shape.
  2320. const S32 shapeIndex = pSceneObject->createCircleCollisionShape( radius, localPosition );
  2321. // Was shape created.
  2322. if ( shapeIndex != -1 )
  2323. {
  2324. // Yes, so configure shape.
  2325. pSceneObject->setCollisionShapeDefinition( shapeIndex, fixtureDef );
  2326. }
  2327. return shapeIndex;
  2328. }
  2329. //-----------------------------------------------------------------------------
  2330. S32 SceneObject::copyPolygonCollisionShapeTo( SceneObject* pSceneObject, const b2FixtureDef& fixtureDef ) const
  2331. {
  2332. // Fetch shape.
  2333. const b2PolygonShape* pShape = dynamic_cast<const b2PolygonShape*>( fixtureDef.shape );
  2334. // Check shape.
  2335. if ( !pShape )
  2336. {
  2337. Con::errorf("SceneObject::copyPolygonCollisionShapeTo() - Invalid shape.");
  2338. return INVALID_COLLISION_SHAPE_INDEX;
  2339. }
  2340. // Fetch point count.
  2341. const U32 pointCount = pShape->GetVertexCount();
  2342. // Fetch local points.
  2343. const b2Vec2* plocalPoints = pShape->m_vertices;
  2344. // Copy shape.
  2345. const S32 shapeIndex = pSceneObject->createPolygonCollisionShape( pointCount, plocalPoints );
  2346. // Was shape created.
  2347. if ( shapeIndex != -1 )
  2348. {
  2349. // Yes, so configure shape.
  2350. pSceneObject->setCollisionShapeDefinition( shapeIndex, fixtureDef );
  2351. }
  2352. return shapeIndex;
  2353. }
  2354. //-----------------------------------------------------------------------------
  2355. S32 SceneObject::copyChainCollisionShapeTo( SceneObject* pSceneObject, const b2FixtureDef& fixtureDef ) const
  2356. {
  2357. // Fetch shape.
  2358. const b2ChainShape* pShape = dynamic_cast<const b2ChainShape*>( fixtureDef.shape );
  2359. // Check shape.
  2360. if ( !pShape )
  2361. {
  2362. Con::errorf("SceneObject::copyChainCollisionShapeTo() - Invalid shape.");
  2363. return INVALID_COLLISION_SHAPE_INDEX;
  2364. }
  2365. // Fetch point count.
  2366. const U32 pointCount = pShape->m_count;
  2367. // Fetch local points.
  2368. b2Vec2* localPoints = pShape->m_vertices;
  2369. // Fetch adjacent positions.
  2370. const bool hasAdjacentLocalPositionStart = pShape->m_hasPrevVertex;
  2371. const bool hasAdjacentLocalPositionEnd = pShape->m_hasNextVertex;
  2372. const b2Vec2 adjacentLocalPositionStart = pShape->m_prevVertex;
  2373. const b2Vec2 adjacentLocalPositionEnd = pShape->m_nextVertex;
  2374. // Create shape.
  2375. const S32 shapeIndex = pSceneObject->createChainCollisionShape(
  2376. pointCount, localPoints,
  2377. hasAdjacentLocalPositionStart, hasAdjacentLocalPositionEnd,
  2378. adjacentLocalPositionStart, adjacentLocalPositionEnd);
  2379. // Was shape created.
  2380. if ( shapeIndex != -1 )
  2381. {
  2382. // Yes, so configure shape.
  2383. pSceneObject->setCollisionShapeDefinition( shapeIndex, fixtureDef );
  2384. }
  2385. return shapeIndex;
  2386. }
  2387. //-----------------------------------------------------------------------------
  2388. S32 SceneObject::copyEdgeCollisionShapeTo( SceneObject* pSceneObject, const b2FixtureDef& fixtureDef ) const
  2389. {
  2390. // Fetch shape.
  2391. const b2EdgeShape* pShape = dynamic_cast<const b2EdgeShape*>( fixtureDef.shape );
  2392. // Check shape.
  2393. if ( !pShape )
  2394. {
  2395. Con::errorf("SceneObject::copyEdgeCollisionShapeTo() - Invalid shape.");
  2396. return INVALID_COLLISION_SHAPE_INDEX;
  2397. }
  2398. // Fetch positions.
  2399. const b2Vec2 localPosition1 = pShape->m_vertex1;
  2400. const b2Vec2 localPosition2 = pShape->m_vertex2;
  2401. const bool hasAdjacentLocalPosition1 = pShape->m_hasVertex0;
  2402. const bool hasAdjacentLocalPosition2 = pShape->m_hasVertex3;
  2403. const b2Vec2 adjacentLocalPosition1 = pShape->m_vertex0;
  2404. const b2Vec2 adjacentLocalPosition2 = pShape->m_vertex3;
  2405. // Create shape.
  2406. const S32 shapeIndex = pSceneObject->createEdgeCollisionShape(
  2407. localPosition1, localPosition2,
  2408. hasAdjacentLocalPosition1, hasAdjacentLocalPosition2,
  2409. adjacentLocalPosition1, adjacentLocalPosition2 );
  2410. // Was shape created.
  2411. if ( shapeIndex != -1 )
  2412. {
  2413. // Yes, so configure shape.
  2414. pSceneObject->setCollisionShapeDefinition( shapeIndex, fixtureDef );
  2415. }
  2416. return shapeIndex;
  2417. }
  2418. //-----------------------------------------------------------------------------
  2419. void SceneObject::safeDelete( void )
  2420. {
  2421. // Are we in a scene?
  2422. if ( getScene() )
  2423. {
  2424. // Yes, so add a delete-request to the scene.
  2425. getScene()->addDeleteRequest( this );
  2426. }
  2427. else
  2428. {
  2429. // No, so use standard SimObject helper.
  2430. deleteObject();
  2431. }
  2432. }
  2433. //-----------------------------------------------------------------------------
  2434. void SceneObject::addDestroyNotification( SceneObject* pSceneObject )
  2435. {
  2436. // Search list to see if we're already in it (finish if we are).
  2437. for ( U32 n = 0; n < (U32)mDestroyNotifyList.size(); n++ )
  2438. {
  2439. // In the list already?
  2440. if ( mDestroyNotifyList[n].mpSceneObject == pSceneObject )
  2441. {
  2442. // Yes, so just bump-up the reference count.
  2443. mDestroyNotifyList[n].mRefCount++;
  2444. // Finish here.
  2445. return;
  2446. }
  2447. }
  2448. // Add Destroy Notification.
  2449. tDestroyNotification notification;
  2450. notification.mpSceneObject = pSceneObject;
  2451. notification.mRefCount = 1;
  2452. // Add Notification.
  2453. mDestroyNotifyList.push_back( notification );
  2454. }
  2455. //-----------------------------------------------------------------------------
  2456. void SceneObject::removeDestroyNotification( SceneObject* pSceneObject )
  2457. {
  2458. // Find object in notification list.
  2459. for ( U32 n = 0; n < (U32)mDestroyNotifyList.size(); n++ )
  2460. {
  2461. // Our object?
  2462. if ( mDestroyNotifyList[n].mpSceneObject == pSceneObject )
  2463. {
  2464. // Yes, so reduce reference count.
  2465. mDestroyNotifyList[n].mRefCount--;
  2466. // Finish Here.
  2467. return;
  2468. }
  2469. }
  2470. }
  2471. //-----------------------------------------------------------------------------
  2472. void SceneObject::processDestroyNotifications( void )
  2473. {
  2474. // Find object in notification list.
  2475. while( mDestroyNotifyList.size() )
  2476. {
  2477. // Fetch Notification Item.
  2478. tDestroyNotification notification = mDestroyNotifyList.first();
  2479. // Only action if we've got a reference active.
  2480. if ( notification.mRefCount > 0 )
  2481. // Call Destroy Notification.
  2482. notification.mpSceneObject->onDestroyNotify( this );
  2483. // Remove it.
  2484. mDestroyNotifyList.pop_front();
  2485. }
  2486. // Sanity!
  2487. AssertFatal( mDestroyNotifyList.size() == 0, "SceneObject::processDestroyNotifications() - Notifications still pending!" );
  2488. }
  2489. //-----------------------------------------------------------------------------
  2490. void SceneObject::notifyComponentsAddToScene( void )
  2491. {
  2492. // Debug Profiling.
  2493. PROFILE_SCOPE(SceneObject_NotifyComponentsAddToScene);
  2494. // Notify components.
  2495. VectorPtr<SimComponent*>& componentList = lockComponentList();
  2496. for( SimComponentIterator itr = componentList.begin(); itr != componentList.end(); ++itr )
  2497. {
  2498. SimComponent *pComponent = *itr;
  2499. if( pComponent != NULL )
  2500. pComponent->onAddToScene();
  2501. }
  2502. unlockComponentList();
  2503. }
  2504. //-----------------------------------------------------------------------------
  2505. void SceneObject::notifyComponentsRemoveFromScene( void )
  2506. {
  2507. // Debug Profiling.
  2508. PROFILE_SCOPE(SceneObject_NotifyComponentsRemoveFromScene);
  2509. // Notify components.
  2510. VectorPtr<SimComponent*>& componentList = lockComponentList();
  2511. for( SimComponentIterator itr = componentList.begin(); itr != componentList.end(); ++itr )
  2512. {
  2513. SimComponent *pComponent = *itr;
  2514. if( pComponent != NULL )
  2515. pComponent->onRemoveFromScene();
  2516. }
  2517. unlockComponentList();
  2518. }
  2519. //-----------------------------------------------------------------------------
  2520. void SceneObject::notifyComponentsUpdate( void )
  2521. {
  2522. // Debug Profiling.
  2523. PROFILE_SCOPE(SceneObject_NotifyComponentsUpdate);
  2524. // Notify components.
  2525. VectorPtr<SimComponent*>& componentList = lockComponentList();
  2526. for( SimComponentIterator itr = componentList.begin(); itr != componentList.end(); ++itr )
  2527. {
  2528. SimComponent *pComponent = *itr;
  2529. if( pComponent != NULL )
  2530. pComponent->onUpdate();
  2531. }
  2532. unlockComponentList();
  2533. }
  2534. //-----------------------------------------------------------------------------
  2535. U32 SceneObject::getGlobalSceneObjectCount( void )
  2536. {
  2537. return sGlobalSceneObjectCount;
  2538. }
  2539. //-----------------------------------------------------------------------------
  2540. void SceneObject::onTamlCustomWrite( TamlCustomNodes& customNodes )
  2541. {
  2542. // Debug Profiling.
  2543. PROFILE_SCOPE(SceneObject_OnTamlCustomWrite);
  2544. // Call parent.
  2545. Parent::onTamlCustomWrite( customNodes );
  2546. // Fetch collision shape count.
  2547. const U32 collisionShapeCount = getCollisionShapeCount();
  2548. // Finish if no collision shapes.
  2549. if ( collisionShapeCount == 0 )
  2550. return;
  2551. // Add collision shape node.
  2552. TamlCustomNode* pCustomCollisionShapes = customNodes.addNode( shapeCustomNodeName );
  2553. // Iterate collision shapes.
  2554. for ( U32 shapeIndex = 0; shapeIndex < collisionShapeCount; ++shapeIndex )
  2555. {
  2556. // Fetch collision shape definition.
  2557. b2FixtureDef fixtureDef = getCollisionShapeDefinition( shapeIndex );
  2558. // Add collision shape node.
  2559. // NOTE: The name of the node will get updated shortly.
  2560. TamlCustomNode* pCollisionShapeNode = pCustomCollisionShapes->addNode( StringTable->EmptyString );
  2561. // Add common collision shape fields.
  2562. if ( mNotEqual( getDefaultDensity(), fixtureDef.density ) )
  2563. pCollisionShapeNode->addField( shapeDensityName, fixtureDef.density );
  2564. if ( mNotEqual( getDefaultFriction(), fixtureDef.friction ) )
  2565. pCollisionShapeNode->addField( shapeFrictionName, fixtureDef.friction );
  2566. if ( mNotEqual( getDefaultRestitution(), fixtureDef.restitution ) )
  2567. pCollisionShapeNode->addField( shapeRestitutionName, fixtureDef.restitution );
  2568. if ( fixtureDef.isSensor == true )
  2569. pCollisionShapeNode->addField( shapeSensorName, fixtureDef.isSensor );
  2570. // Populate collision shape appropriately.
  2571. switch( fixtureDef.shape->GetType() )
  2572. {
  2573. case b2Shape::e_circle:
  2574. {
  2575. // Set node name.
  2576. pCollisionShapeNode->setNodeName( circleTypeName );
  2577. // Fetch shape.
  2578. const b2CircleShape* pShape = dynamic_cast<const b2CircleShape*>( fixtureDef.shape );
  2579. // Sanity!
  2580. AssertFatal( pShape != NULL, "SceneObject::onTamlCustomWrite() - Invalid circle shape type returned." );
  2581. // Add radius property.
  2582. pCollisionShapeNode->addField( circleRadiusName, pShape->m_radius );
  2583. // Add offset property (if not zero).
  2584. if ( !Vector2(pShape->m_p).isZero() )
  2585. pCollisionShapeNode->addField( circleOffsetName, pShape->m_p );
  2586. }
  2587. break;
  2588. case b2Shape::e_polygon:
  2589. {
  2590. // Set node name.
  2591. pCollisionShapeNode->setNodeName( polygonTypeName );
  2592. // Fetch shape.
  2593. const b2PolygonShape* pShape = dynamic_cast<const b2PolygonShape*>( fixtureDef.shape );
  2594. // Sanity!
  2595. AssertFatal( pShape != NULL, "SceneObject::onTamlCustomWrite() - Invalid polygon shape type returned." );
  2596. // Fetch point count.
  2597. const U32 pointCount = pShape->GetVertexCount();
  2598. // Add shape properties.
  2599. for ( U32 pointIndex = 0; pointIndex < pointCount; ++pointIndex )
  2600. {
  2601. // Add point node.
  2602. TamlCustomNode* pPointNode = pCollisionShapeNode->addNode( shapePointName );
  2603. // Fetch point.
  2604. const b2Vec2& point = pShape->GetVertex( pointIndex );
  2605. // Add point fields.
  2606. pPointNode->getNodeTextField().setFieldValue( StringTable->EmptyString, point );
  2607. }
  2608. }
  2609. break;
  2610. case b2Shape::e_chain:
  2611. {
  2612. // Set node name.
  2613. pCollisionShapeNode->setNodeName( chainTypeName );
  2614. // Fetch shape.
  2615. const b2ChainShape* pShape = dynamic_cast<const b2ChainShape*>( fixtureDef.shape );
  2616. // Sanity!
  2617. AssertFatal( pShape != NULL, "SceneObject::onTamlCustomWrite() - Invalid chain shape type returned." );
  2618. // Fetch point count.
  2619. const U32 pointCount = pShape->m_count;
  2620. // Add shape properties.
  2621. for ( U32 pointIndex = 0; pointIndex < pointCount; ++pointIndex )
  2622. {
  2623. // Add point node.
  2624. TamlCustomNode* pPointNode = pCollisionShapeNode->addNode( shapePointName );
  2625. // Add point fields.
  2626. pPointNode->getNodeTextField().setFieldValue( StringTable->EmptyString, pShape->m_vertices[pointIndex] );
  2627. }
  2628. // Add adjacent start point (if specified).
  2629. if ( pShape->m_hasPrevVertex )
  2630. {
  2631. TamlCustomNode* pPrevPointNode = pCollisionShapeNode->addNode( shapePrevPointName );
  2632. pPrevPointNode->getNodeTextField().setFieldValue( StringTable->EmptyString, pShape->m_prevVertex );
  2633. }
  2634. // Add adjacent end point (if specified).
  2635. if ( pShape->m_hasNextVertex )
  2636. {
  2637. TamlCustomNode* pNextPointNode = pCollisionShapeNode->addNode( shapeNextPointName );
  2638. pNextPointNode->getNodeTextField().setFieldValue( StringTable->EmptyString, pShape->m_nextVertex );
  2639. }
  2640. }
  2641. break;
  2642. case b2Shape::e_edge:
  2643. {
  2644. // Set node name.
  2645. pCollisionShapeNode->setNodeName( edgeTypeName );
  2646. // Fetch shape.
  2647. const b2EdgeShape* pShape = dynamic_cast<const b2EdgeShape*>( fixtureDef.shape );
  2648. // Sanity!
  2649. AssertFatal( pShape != NULL, "SceneObject::onTamlCustomWrite() - Invalid edge shape type returned." );
  2650. // Add start point.
  2651. TamlCustomNode* pStartPointNode = pCollisionShapeNode->addNode( shapePointName );
  2652. pStartPointNode->getNodeTextField().setFieldValue( StringTable->EmptyString, pShape->m_vertex1 );
  2653. // Add end point.
  2654. TamlCustomNode* pEndPointNode = pCollisionShapeNode->addNode( shapePointName );
  2655. pEndPointNode->getNodeTextField().setFieldValue( StringTable->EmptyString, pShape->m_vertex2 );
  2656. // Add adjacent start point (if specified).
  2657. if ( pShape->m_hasVertex0 )
  2658. {
  2659. TamlCustomNode* pPrevPointNode = pCollisionShapeNode->addNode( shapePrevPointName );
  2660. pPrevPointNode->getNodeTextField().setFieldValue( StringTable->EmptyString, pShape->m_vertex0 );
  2661. }
  2662. // Add adjacent end point (if specified).
  2663. if ( pShape->m_hasVertex3 )
  2664. {
  2665. TamlCustomNode* pNextPointNode = pCollisionShapeNode->addNode( shapeNextPointName );
  2666. pNextPointNode->getNodeTextField().setFieldValue( StringTable->EmptyString, pShape->m_vertex3 );
  2667. }
  2668. }
  2669. break;
  2670. default:
  2671. // Sanity!
  2672. AssertFatal( false, "SceneObject::onTamlCustomWrite() - Unknown shape type detected." );
  2673. }
  2674. }
  2675. }
  2676. //-----------------------------------------------------------------------------
  2677. void SceneObject::onTamlCustomRead( const TamlCustomNodes& customNodes )
  2678. {
  2679. // Debug Profiling.
  2680. PROFILE_SCOPE(SceneObject_OnTamlCustomRead);
  2681. // Call parent.
  2682. Parent::onTamlCustomRead( customNodes );
  2683. // Find collision shape custom node.
  2684. const TamlCustomNode* pCustomCollisionShapes = customNodes.findNode( shapeCustomNodeName );
  2685. // Finish if we don't have collision shapes.
  2686. if ( pCustomCollisionShapes == NULL )
  2687. return;
  2688. // Fetch children shapes.
  2689. const TamlCustomNodeVector& collisionShapeChildren = pCustomCollisionShapes->getChildren();
  2690. // Iterate collision shapes.
  2691. for( TamlCustomNodeVector::const_iterator shapeNodeItr = collisionShapeChildren.begin(); shapeNodeItr != collisionShapeChildren.end(); ++shapeNodeItr )
  2692. {
  2693. // Fetch shape node.
  2694. TamlCustomNode* pShapeNode = *shapeNodeItr;
  2695. // Fetch shape name.
  2696. StringTableEntry shapeName = pShapeNode->getNodeName();
  2697. // Ready common fields.
  2698. F32 shapeDensity = getDefaultDensity();
  2699. F32 shapeFriction = getDefaultFriction();
  2700. F32 shapeRestitution = getDefaultRestitution();
  2701. bool shapeSensor = false;
  2702. S32 shapeIndex;
  2703. // Is this a circle shape?
  2704. if ( shapeName == circleTypeName )
  2705. {
  2706. // Yes, so ready fields.
  2707. F32 radius = 0.0f;
  2708. b2Vec2 offset( 0.0f, 0.0f );
  2709. // Fetch shape children.
  2710. const TamlCustomFieldVector& shapeFields = pShapeNode->getFields();
  2711. // Iterate property fields.
  2712. for ( TamlCustomFieldVector::const_iterator shapeFieldItr = shapeFields.begin(); shapeFieldItr != shapeFields.end(); ++shapeFieldItr )
  2713. {
  2714. // Fetch field.
  2715. const TamlCustomField* pField = *shapeFieldItr;
  2716. // Fetch property field name.
  2717. StringTableEntry fieldName = pField->getFieldName();
  2718. // Check common fields.
  2719. if ( fieldName == shapeDensityName )
  2720. {
  2721. pField->getFieldValue( shapeDensity );
  2722. }
  2723. else if ( fieldName == shapeFrictionName )
  2724. {
  2725. pField->getFieldValue( shapeFriction );
  2726. }
  2727. else if ( fieldName == shapeRestitutionName )
  2728. {
  2729. pField->getFieldValue( shapeRestitution );
  2730. }
  2731. else if ( fieldName == shapeSensorName )
  2732. {
  2733. pField->getFieldValue( shapeSensor );
  2734. }
  2735. // Check circle fields.
  2736. else if ( fieldName == circleRadiusName )
  2737. {
  2738. pField->getFieldValue( radius );
  2739. }
  2740. else if ( fieldName == circleOffsetName )
  2741. {
  2742. pField->getFieldValue( offset );
  2743. }
  2744. }
  2745. // Is radius valid?
  2746. if ( radius <= 0.0f )
  2747. {
  2748. // No, so warn.
  2749. Con::warnf( "SceneObject::onTamlCustomRead() - Invalid radius on circle collision shape '%g'. Using default.", radius );
  2750. // Set default.
  2751. radius = 1.0f;
  2752. }
  2753. // Create shape.
  2754. shapeIndex = createCircleCollisionShape( radius, offset );
  2755. }
  2756. // Is this a polygon shape?
  2757. else if ( shapeName == polygonTypeName )
  2758. {
  2759. // Yes, so fetch shape fields.
  2760. const TamlCustomFieldVector& shapeFields = pShapeNode->getFields();
  2761. // Iterate property fields.
  2762. for ( TamlCustomFieldVector::const_iterator shapeFieldItr = shapeFields.begin(); shapeFieldItr != shapeFields.end(); ++shapeFieldItr )
  2763. {
  2764. // Fetch field.
  2765. const TamlCustomField* pField = *shapeFieldItr;
  2766. // Fetch property field name.
  2767. StringTableEntry fieldName = pField->getFieldName();
  2768. // Check common fields.
  2769. if ( fieldName == shapeDensityName )
  2770. {
  2771. pField->getFieldValue( shapeDensity );
  2772. }
  2773. else if ( fieldName == shapeFrictionName )
  2774. {
  2775. pField->getFieldValue( shapeFriction );
  2776. }
  2777. else if ( fieldName == shapeRestitutionName )
  2778. {
  2779. pField->getFieldValue( shapeRestitution );
  2780. }
  2781. else if ( fieldName == shapeSensorName )
  2782. {
  2783. pField->getFieldValue( shapeSensor );
  2784. }
  2785. }
  2786. // Fetch shape children.
  2787. const TamlCustomNodeVector& shapeChildren = pShapeNode->getChildren();
  2788. // Fetch shape children count.
  2789. const U32 shapeChildrenCount = (U32)shapeChildren.size();
  2790. // Reset points.
  2791. b2Vec2 points[b2_maxPolygonVertices];
  2792. U32 pointCount = 0;
  2793. // Do we have any shape children.
  2794. if ( shapeChildrenCount > 0 )
  2795. {
  2796. // Yes, so iterate them.
  2797. for( TamlCustomNodeVector::const_iterator childItr = shapeChildren.begin(); childItr != shapeChildren.end(); ++childItr )
  2798. {
  2799. TamlCustomNode* pChildNode = *childItr;
  2800. // Skip if it's not a point.
  2801. if ( pChildNode->getNodeName() != shapePointName )
  2802. continue;
  2803. // Skip if it's empty.
  2804. if ( pChildNode->getNodeTextField().isValueEmpty() )
  2805. continue;
  2806. // Read point.
  2807. b2Vec2 point;
  2808. pChildNode->getNodeTextField().getFieldValue( point );
  2809. points[pointCount++] = point;
  2810. }
  2811. }
  2812. // Is point count valid?
  2813. if ( pointCount == 0 )
  2814. {
  2815. // No, so warn.
  2816. Con::warnf( "SceneObject::onTamlCustomRead() - No points on polygon collision shape." );
  2817. continue;
  2818. }
  2819. // Create shape.
  2820. shapeIndex = createPolygonCollisionShape( pointCount, points );
  2821. }
  2822. // Is this a chain shape?
  2823. else if ( shapeName == chainTypeName )
  2824. {
  2825. // Yes, so ready fields.
  2826. Vector<b2Vec2> points;
  2827. bool hasAdjacentStartPoint = false;
  2828. bool hasAdjacentEndPoint = false;
  2829. b2Vec2 adjacentStartPoint;
  2830. b2Vec2 adjacentEndPoint;
  2831. // Fetch shape children.
  2832. const TamlCustomFieldVector& shapeFields = pShapeNode->getFields();
  2833. // Iterate property fields.
  2834. for ( TamlCustomFieldVector::const_iterator shapeFieldItr = shapeFields.begin(); shapeFieldItr != shapeFields.end(); ++shapeFieldItr )
  2835. {
  2836. // Fetch field.
  2837. const TamlCustomField* pField = *shapeFieldItr;
  2838. // Fetch property field name.
  2839. StringTableEntry fieldName = pField->getFieldName();
  2840. // Check common fields.
  2841. if ( fieldName == shapeDensityName )
  2842. {
  2843. pField->getFieldValue( shapeDensity );
  2844. }
  2845. else if ( fieldName == shapeFrictionName )
  2846. {
  2847. pField->getFieldValue( shapeFriction );
  2848. }
  2849. else if ( fieldName == shapeRestitutionName )
  2850. {
  2851. pField->getFieldValue( shapeRestitution );
  2852. }
  2853. else if ( fieldName == shapeSensorName )
  2854. {
  2855. pField->getFieldValue( shapeSensor );
  2856. }
  2857. }
  2858. // Fetch shape children.
  2859. const TamlCustomNodeVector& shapeChildren = pShapeNode->getChildren();
  2860. // Fetch shape children count.
  2861. const U32 shapeChildrenCount = (U32)shapeChildren.size();
  2862. // Do we have any shape children.
  2863. // NOTE: Only do this if the old methods has not been used.
  2864. if ( points.size() == 0 && shapeChildrenCount > 0 )
  2865. {
  2866. // Yes, so iterate them.
  2867. for( TamlCustomNodeVector::const_iterator childItr = shapeChildren.begin(); childItr != shapeChildren.end(); ++childItr )
  2868. {
  2869. TamlCustomNode* pChildNode = *childItr;
  2870. // Fetch the node name.
  2871. StringTableEntry nodeName = pChildNode->getNodeName();
  2872. // Skip if it's not a point.
  2873. if ( !(nodeName == shapePointName || nodeName == shapePrevPointName || nodeName == shapeNextPointName) )
  2874. continue;
  2875. // Skip if it's empty.
  2876. if ( pChildNode->getNodeTextField().isValueEmpty() )
  2877. continue;
  2878. if ( nodeName == shapePointName )
  2879. {
  2880. // Read point.
  2881. b2Vec2 point;
  2882. pChildNode->getNodeTextField().getFieldValue( point );
  2883. points.push_back( point );
  2884. }
  2885. else if ( nodeName == shapePrevPointName )
  2886. {
  2887. // Read adjacent point.
  2888. pChildNode->getNodeTextField().getFieldValue( adjacentStartPoint );
  2889. hasAdjacentStartPoint = true;
  2890. }
  2891. else if ( nodeName == shapeNextPointName )
  2892. {
  2893. // Read adjacent point.
  2894. pChildNode->getNodeTextField().getFieldValue( adjacentEndPoint );
  2895. hasAdjacentEndPoint = true;
  2896. }
  2897. }
  2898. }
  2899. // Is point count valid?
  2900. if ( points.size() == 0 || points.size() < 2 )
  2901. {
  2902. // No, so warn.
  2903. Con::warnf( "SceneObject::onTamlCustomRead() - No points (or less than two) on chain collision shape." );
  2904. continue;
  2905. }
  2906. // Create shape.
  2907. shapeIndex = createChainCollisionShape( points.size(), points.address(), hasAdjacentStartPoint, hasAdjacentEndPoint, adjacentStartPoint, adjacentEndPoint );
  2908. }
  2909. // Is this an edge shape?
  2910. else if ( shapeName == edgeTypeName )
  2911. {
  2912. // Yes, so ready fields.
  2913. b2Vec2 point0;
  2914. b2Vec2 point1;
  2915. U32 pointCount = 0;
  2916. bool hasAdjacentStartPoint = false;
  2917. bool hasAdjacentEndPoint = false;
  2918. b2Vec2 adjacentStartPoint;
  2919. b2Vec2 adjacentEndPoint;
  2920. // Fetch shape children.
  2921. const TamlCustomFieldVector& shapeFields = pShapeNode->getFields();
  2922. // Iterate property fields.
  2923. for ( TamlCustomFieldVector::const_iterator shapeFieldItr = shapeFields.begin(); shapeFieldItr != shapeFields.end(); ++shapeFieldItr )
  2924. {
  2925. // Fetch field.
  2926. const TamlCustomField* pField = *shapeFieldItr;
  2927. // Fetch property field name.
  2928. StringTableEntry fieldName = pField->getFieldName();
  2929. // Check common fields.
  2930. if ( fieldName == shapeDensityName )
  2931. {
  2932. pField->getFieldValue( shapeDensity );
  2933. }
  2934. else if ( fieldName == shapeFrictionName )
  2935. {
  2936. pField->getFieldValue( shapeFriction );
  2937. }
  2938. else if ( fieldName == shapeRestitutionName )
  2939. {
  2940. pField->getFieldValue( shapeRestitution );
  2941. }
  2942. else if ( fieldName == shapeSensorName )
  2943. {
  2944. pField->getFieldValue( shapeSensor );
  2945. }
  2946. }
  2947. // Fetch shape children.
  2948. const TamlCustomNodeVector& shapeChildren = pShapeNode->getChildren();
  2949. // Fetch shape children count.
  2950. const U32 shapeChildrenCount = (U32)shapeChildren.size();
  2951. // Do we have any shape children.
  2952. if ( shapeChildrenCount > 0 )
  2953. {
  2954. // Yes, so iterate them.
  2955. for( TamlCustomNodeVector::const_iterator childItr = shapeChildren.begin(); childItr != shapeChildren.end(); ++childItr )
  2956. {
  2957. TamlCustomNode* pChildNode = *childItr;
  2958. // Fetch the node name.
  2959. StringTableEntry nodeName = pChildNode->getNodeName();
  2960. // Skip if it's not a point.
  2961. if ( !(nodeName == shapePointName || nodeName == shapePrevPointName || nodeName == shapeNextPointName) )
  2962. continue;
  2963. // Skip if it's empty.
  2964. if ( pChildNode->getNodeTextField().isValueEmpty() )
  2965. continue;
  2966. if ( nodeName == shapePointName )
  2967. {
  2968. // Ignore if too many points.
  2969. if ( pointCount >= 2 )
  2970. continue;
  2971. // Read point.
  2972. if ( pointCount == 0 )
  2973. pChildNode->getNodeTextField().getFieldValue( point0 );
  2974. else
  2975. pChildNode->getNodeTextField().getFieldValue( point1 );
  2976. pointCount++;
  2977. }
  2978. else if ( nodeName == shapePrevPointName )
  2979. {
  2980. // Read adjacent point.
  2981. pChildNode->getNodeTextField().getFieldValue( adjacentStartPoint );
  2982. hasAdjacentStartPoint = true;
  2983. }
  2984. else if ( nodeName == shapeNextPointName )
  2985. {
  2986. // Read adjacent point.
  2987. pChildNode->getNodeTextField().getFieldValue( adjacentEndPoint );
  2988. hasAdjacentEndPoint = true;
  2989. }
  2990. }
  2991. }
  2992. // Is point count valid?
  2993. if ( pointCount == 0 || pointCount != 2 )
  2994. {
  2995. // No, so warn.
  2996. Con::warnf( "SceneObject::onTamlCustomRead() - No points (or not two points) on edge collision shape." );
  2997. continue;
  2998. }
  2999. // Create shape.
  3000. shapeIndex = createEdgeCollisionShape( point0, point1, hasAdjacentStartPoint, hasAdjacentEndPoint, adjacentStartPoint, adjacentEndPoint );
  3001. }
  3002. // Unknown shape type!
  3003. else
  3004. {
  3005. // Warn.
  3006. Con::warnf( "Unknown shape type of '%s' encountered.", shapeName );
  3007. // Sanity!
  3008. AssertFatal( false, "SceneObject::onTamlCustomRead() - Unknown shape type detected." );
  3009. continue;
  3010. }
  3011. // Set common properties.
  3012. setCollisionShapeDensity( shapeIndex, shapeDensity );
  3013. setCollisionShapeFriction( shapeIndex, shapeFriction );
  3014. setCollisionShapeRestitution( shapeIndex, shapeRestitution );
  3015. setCollisionShapeIsSensor( shapeIndex, shapeSensor );
  3016. }
  3017. }
  3018. //-----------------------------------------------------------------------------
  3019. bool SceneObject::writeField(StringTableEntry fieldname, const char* value)
  3020. {
  3021. if (!Parent::writeField(fieldname, value))
  3022. return false;
  3023. // Never save the scene field.
  3024. if (dStricmp(fieldname, "scene") == 0)
  3025. return false;
  3026. return true;
  3027. }
  3028. //------------------------------------------------------------------------------
  3029. S32 QSORT_CALLBACK SceneObject::sceneObjectLayerDepthSort(const void* a, const void* b)
  3030. {
  3031. // Fetch scene objects.
  3032. SceneObject* pSceneObjectA = *((SceneObject**)a);
  3033. SceneObject* pSceneObjectB = *((SceneObject**)b);
  3034. // Fetch layers.
  3035. const U32 layerA = pSceneObjectA->getSceneLayer();
  3036. const U32 layerB = pSceneObjectB->getSceneLayer();
  3037. if ( layerA < layerB )
  3038. return -1;
  3039. if ( layerA > layerB )
  3040. return 1;
  3041. // Fetch layer depths.
  3042. const F32 depthA = pSceneObjectA->getSceneLayerDepth();
  3043. const F32 depthB = pSceneObjectB->getSceneLayerDepth();
  3044. return depthA < depthB ? 1 : depthA > depthB ? -1 : pSceneObjectA->getSerialId() - pSceneObjectB->getSerialId();
  3045. }
  3046. //-----------------------------------------------------------------------------
  3047. static EnumTable::Enums bodyTypeLookup[] =
  3048. {
  3049. { b2_staticBody, "Static" },
  3050. { b2_kinematicBody, "Kinematic" },
  3051. { b2_dynamicBody, "Dynamic" },
  3052. };
  3053. EnumTable bodyTypeTable(sizeof(bodyTypeLookup) / sizeof(EnumTable::Enums), &bodyTypeLookup[0]);
  3054. //-----------------------------------------------------------------------------
  3055. static EnumTable::Enums collisionShapeTypeLookup[] =
  3056. {
  3057. { b2Shape::e_circle, "Circle" },
  3058. { b2Shape::e_edge, "Edge" },
  3059. { b2Shape::e_polygon, "Polygon" },
  3060. { b2Shape::e_chain, "Chain" },
  3061. };
  3062. EnumTable collisionShapeTypeTable(sizeof(collisionShapeTypeLookup) / sizeof(EnumTable::Enums), &collisionShapeTypeLookup[0]);
  3063. //-----------------------------------------------------------------------------
  3064. static EnumTable::Enums srcBlendFactorLookup[] =
  3065. {
  3066. { GL_ZERO, "ZERO" },
  3067. { GL_ONE, "ONE" },
  3068. { GL_DST_COLOR, "DST_COLOR" },
  3069. { GL_ONE_MINUS_DST_COLOR, "ONE_MINUS_DST_COLOR" },
  3070. { GL_SRC_ALPHA, "SRC_ALPHA" },
  3071. { GL_ONE_MINUS_SRC_ALPHA, "ONE_MINUS_SRC_ALPHA" },
  3072. { GL_DST_ALPHA, "DST_ALPHA" },
  3073. { GL_ONE_MINUS_DST_ALPHA, "ONE_MINUS_DST_ALPHA" },
  3074. { GL_SRC_ALPHA_SATURATE, "SRC_ALPHA_SATURATE" },
  3075. };
  3076. EnumTable srcBlendFactorTable(sizeof(srcBlendFactorLookup) / sizeof(EnumTable::Enums), &srcBlendFactorLookup[0]);
  3077. //-----------------------------------------------------------------------------
  3078. static EnumTable::Enums dstBlendFactorLookup[] =
  3079. {
  3080. { GL_ZERO, "ZERO" },
  3081. { GL_ONE, "ONE" },
  3082. { GL_SRC_COLOR, "SRC_COLOR" },
  3083. { GL_ONE_MINUS_SRC_COLOR, "ONE_MINUS_SRC_COLOR" },
  3084. { GL_SRC_ALPHA, "SRC_ALPHA" },
  3085. { GL_ONE_MINUS_SRC_ALPHA, "ONE_MINUS_SRC_ALPHA" },
  3086. { GL_DST_ALPHA, "DST_ALPHA" },
  3087. { GL_ONE_MINUS_DST_ALPHA, "ONE_MINUS_DST_ALPHA" },
  3088. };
  3089. EnumTable dstBlendFactorTable(sizeof(dstBlendFactorLookup) / sizeof(EnumTable::Enums), &dstBlendFactorLookup[0]);
  3090. //-----------------------------------------------------------------------------
  3091. b2BodyType SceneObject::getBodyTypeEnum(const char* label)
  3092. {
  3093. // Search for Mnemonic.
  3094. for (U32 i = 0; i < (sizeof(bodyTypeLookup) / sizeof(EnumTable::Enums)); i++)
  3095. {
  3096. if( dStricmp(bodyTypeLookup[i].label, label) == 0)
  3097. return (b2BodyType)bodyTypeLookup[i].index;
  3098. }
  3099. // Warn.
  3100. Con::warnf("SceneObject::getBodyTypeEnum() - Invalid body type of '%s'", label );
  3101. return (b2BodyType)-1;
  3102. }
  3103. //-----------------------------------------------------------------------------
  3104. const char* SceneObject::getBodyTypeDescription(const b2BodyType bodyType)
  3105. {
  3106. // Search for Mnemonic.
  3107. for (U32 i = 0; i < (sizeof(bodyTypeLookup) / sizeof(EnumTable::Enums)); i++)
  3108. {
  3109. if( bodyTypeLookup[i].index == bodyType )
  3110. return bodyTypeLookup[i].label;
  3111. }
  3112. // Warn.
  3113. Con::warnf( "SceneObject::getBodyTypeDescription() - Invalid body type." );
  3114. return StringTable->EmptyString;
  3115. }
  3116. //-----------------------------------------------------------------------------
  3117. b2Shape::Type SceneObject::getCollisionShapeTypeEnum(const char* label)
  3118. {
  3119. // Search for Mnemonic.
  3120. for (U32 i = 0; i < (sizeof(collisionShapeTypeLookup) / sizeof(EnumTable::Enums)); i++)
  3121. {
  3122. if( dStricmp(collisionShapeTypeLookup[i].label, label) == 0)
  3123. return (b2Shape::Type)collisionShapeTypeLookup[i].index;
  3124. }
  3125. // Warn!
  3126. Con::warnf("SceneObject::getCollisionShapeTypeEnum() - Invalid collision shape type of '%s'", label );
  3127. return b2Shape::e_typeCount;
  3128. }
  3129. //-----------------------------------------------------------------------------
  3130. const char* SceneObject::getCollisionShapeTypeDescription(const b2Shape::Type collisionShapeType)
  3131. {
  3132. // Search for Mnemonic.
  3133. for (U32 i = 0; i < (sizeof(collisionShapeTypeLookup) / sizeof(EnumTable::Enums)); i++)
  3134. {
  3135. if( collisionShapeTypeLookup[i].index == collisionShapeType )
  3136. return collisionShapeTypeLookup[i].label;
  3137. }
  3138. // Warn.
  3139. Con::warnf( "SceneObject::getCollisionShapeTypeDescription() - Invalid collision shape type." );
  3140. return StringTable->EmptyString;
  3141. }
  3142. //-----------------------------------------------------------------------------
  3143. S32 SceneObject::getSrcBlendFactorEnum(const char* label)
  3144. {
  3145. // Search for Mnemonic.
  3146. for (U32 i = 0; i < (sizeof(srcBlendFactorLookup) / sizeof(EnumTable::Enums)); i++)
  3147. {
  3148. if( dStricmp(srcBlendFactorLookup[i].label, label) == 0)
  3149. return(srcBlendFactorLookup[i].index);
  3150. }
  3151. // Warn.
  3152. Con::warnf("SceneObject::getSrcBlendFactorEnum() - Invalid source blend factor of '%s'", label );
  3153. return GL_INVALID_BLEND_FACTOR;
  3154. }
  3155. //-----------------------------------------------------------------------------
  3156. const char* SceneObject::getSrcBlendFactorDescription(const GLenum factor)
  3157. {
  3158. // Search for Mnemonic.
  3159. for (U32 i = 0; i < (sizeof(srcBlendFactorLookup) / sizeof(EnumTable::Enums)); i++)
  3160. {
  3161. if( srcBlendFactorLookup[i].index == (S32)factor )
  3162. return srcBlendFactorLookup[i].label;
  3163. }
  3164. // Warn.
  3165. Con::warnf( "SceneObject::getSrcBlendFactorDescription() - Invalid source blend factor." );
  3166. return StringTable->EmptyString;
  3167. }
  3168. //-----------------------------------------------------------------------------
  3169. S32 SceneObject::getDstBlendFactorEnum(const char* label)
  3170. {
  3171. // Search for Mnemonic.
  3172. for (U32 i = 0; i < (sizeof(dstBlendFactorLookup) / sizeof(EnumTable::Enums)); i++)
  3173. {
  3174. if( dStricmp(dstBlendFactorLookup[i].label, label) == 0)
  3175. return(dstBlendFactorLookup[i].index);
  3176. }
  3177. // Warn.
  3178. Con::warnf("SceneObject::getSrcBlendFactorEnum() - Invalid destination blend factor of '%s'", label );
  3179. return GL_INVALID_BLEND_FACTOR;
  3180. }
  3181. //-----------------------------------------------------------------------------
  3182. const char* SceneObject::getDstBlendFactorDescription(const GLenum factor)
  3183. {
  3184. // Search for Mnemonic.
  3185. for(U32 i = 0; i < (sizeof(dstBlendFactorLookup) / sizeof(EnumTable::Enums)); i++)
  3186. {
  3187. if( dstBlendFactorLookup[i].index == (S32)factor )
  3188. return dstBlendFactorLookup[i].label;
  3189. }
  3190. // Warn.
  3191. Con::warnf( "SceneObject::getDstBlendFactorDescription() - Invalid destination blend factor." );
  3192. return StringTable->EmptyString;
  3193. }
  3194. //-----------------------------------------------------------------------------
  3195. static void WriteCustomTamlSchema( const AbstractClassRep* pClassRep, TiXmlElement* pParentElement )
  3196. {
  3197. char buffer[1024];
  3198. // Create shapes node element.
  3199. TiXmlElement* pShapesNodeElement = new TiXmlElement( "xs:element" );
  3200. dSprintf( buffer, sizeof(buffer), "%s.%s", pClassRep->getClassName(), shapeCustomNodeName );
  3201. pShapesNodeElement->SetAttribute( "name", buffer );
  3202. pShapesNodeElement->SetAttribute( "minOccurs", 0 );
  3203. pShapesNodeElement->SetAttribute( "maxOccurs", 1 );
  3204. pParentElement->LinkEndChild( pShapesNodeElement );
  3205. // Create complex type.
  3206. TiXmlElement* pShapesNodeComplexTypeElement = new TiXmlElement( "xs:complexType" );
  3207. pShapesNodeElement->LinkEndChild( pShapesNodeComplexTypeElement );
  3208. // Create choice element.
  3209. TiXmlElement* pShapesNodeChoiceElement = new TiXmlElement( "xs:choice" );
  3210. pShapesNodeChoiceElement->SetAttribute( "minOccurs", 0 );
  3211. pShapesNodeChoiceElement->SetAttribute( "maxOccurs", "unbounded" );
  3212. pShapesNodeComplexTypeElement->LinkEndChild( pShapesNodeChoiceElement );
  3213. // ********************************************************************************
  3214. // Create Circle
  3215. // ********************************************************************************
  3216. TiXmlElement* pCircleElement = new TiXmlElement( "xs:element" );
  3217. pCircleElement->SetAttribute( "name", circleTypeName );
  3218. pCircleElement->SetAttribute( "minOccurs", 0 );
  3219. pCircleElement->SetAttribute( "maxOccurs", 1 );
  3220. pShapesNodeChoiceElement->LinkEndChild( pCircleElement );
  3221. // Create complex type Element.
  3222. TiXmlElement* pCircleComplexTypeElement = new TiXmlElement( "xs:complexType" );
  3223. pCircleElement->LinkEndChild( pCircleComplexTypeElement );
  3224. // Create "Radius" attribute.
  3225. TiXmlElement* pCircleElementA = new TiXmlElement( "xs:attribute" );
  3226. pCircleElementA->SetAttribute( "name", circleRadiusName );
  3227. pCircleComplexTypeElement->LinkEndChild( pCircleElementA );
  3228. TiXmlElement* pCircleElementB = new TiXmlElement( "xs:simpleType" );
  3229. pCircleElementA->LinkEndChild( pCircleElementB );
  3230. TiXmlElement* pCircleElementC = new TiXmlElement( "xs:restriction" );
  3231. pCircleElementC->SetAttribute( "base", "xs:float" );
  3232. pCircleElementB->LinkEndChild( pCircleElementC );
  3233. TiXmlElement* pCircleElementD = new TiXmlElement( "xs:minExclusive" );
  3234. pCircleElementD->SetAttribute( "value", "0" );
  3235. pCircleElementC->LinkEndChild( pCircleElementD );
  3236. // Create "Offset" attribute.
  3237. pCircleElementA = new TiXmlElement( "xs:attribute" );
  3238. pCircleElementA->SetAttribute( "name", circleOffsetName );
  3239. pCircleElementA->SetAttribute( "type", "Vector2_ConsoleType" );
  3240. pCircleComplexTypeElement->LinkEndChild( pCircleElementA );
  3241. // Create "IsSensor" attribute.
  3242. pCircleElementA = new TiXmlElement( "xs:attribute" );
  3243. pCircleElementA->SetAttribute( "name", shapeSensorName );
  3244. pCircleElementA->SetAttribute( "type", "xs:boolean" );
  3245. pCircleComplexTypeElement->LinkEndChild( pCircleElementA );
  3246. // Create "Density" attribute.
  3247. pCircleElementA = new TiXmlElement( "xs:attribute" );
  3248. pCircleElementA->SetAttribute( "name", shapeDensityName );
  3249. pCircleComplexTypeElement->LinkEndChild( pCircleElementA );
  3250. pCircleElementB = new TiXmlElement( "xs:simpleType" );
  3251. pCircleElementA->LinkEndChild( pCircleElementB );
  3252. pCircleElementC = new TiXmlElement( "xs:restriction" );
  3253. pCircleElementC->SetAttribute( "base", "xs:float" );
  3254. pCircleElementB->LinkEndChild( pCircleElementC );
  3255. pCircleElementD = new TiXmlElement( "xs:minInclusive" );
  3256. pCircleElementD->SetAttribute( "value", "0" );
  3257. pCircleElementC->LinkEndChild( pCircleElementD );
  3258. // Create "Friction" attribute.
  3259. pCircleElementA = new TiXmlElement( "xs:attribute" );
  3260. pCircleElementA->SetAttribute( "name", shapeFrictionName );
  3261. pCircleComplexTypeElement->LinkEndChild( pCircleElementA );
  3262. pCircleElementB = new TiXmlElement( "xs:simpleType" );
  3263. pCircleElementA->LinkEndChild( pCircleElementB );
  3264. pCircleElementC = new TiXmlElement( "xs:restriction" );
  3265. pCircleElementC->SetAttribute( "base", "xs:float" );
  3266. pCircleElementB->LinkEndChild( pCircleElementC );
  3267. pCircleElementD = new TiXmlElement( "xs:minInclusive" );
  3268. pCircleElementD->SetAttribute( "value", "0" );
  3269. pCircleElementC->LinkEndChild( pCircleElementD );
  3270. // Create "Restitution" attribute.
  3271. pCircleElementA = new TiXmlElement( "xs:attribute" );
  3272. pCircleElementA->SetAttribute( "name", shapeRestitutionName );
  3273. pCircleComplexTypeElement->LinkEndChild( pCircleElementA );
  3274. pCircleElementB = new TiXmlElement( "xs:simpleType" );
  3275. pCircleElementA->LinkEndChild( pCircleElementB );
  3276. pCircleElementC = new TiXmlElement( "xs:restriction" );
  3277. pCircleElementC->SetAttribute( "base", "xs:float" );
  3278. pCircleElementB->LinkEndChild( pCircleElementC );
  3279. pCircleElementD = new TiXmlElement( "xs:minInclusive" );
  3280. pCircleElementD->SetAttribute( "value", "0" );
  3281. pCircleElementC->LinkEndChild( pCircleElementD );
  3282. // ********************************************************************************
  3283. // Create Polygon
  3284. // ********************************************************************************
  3285. TiXmlElement* pPolygonElement = new TiXmlElement( "xs:element" );
  3286. pPolygonElement->SetAttribute( "name", polygonTypeName );
  3287. pPolygonElement->SetAttribute( "minOccurs", 0 );
  3288. pPolygonElement->SetAttribute( "maxOccurs", 1 );
  3289. pShapesNodeChoiceElement->LinkEndChild( pPolygonElement );
  3290. // Create complex type Element.
  3291. TiXmlElement* pPolygonComplexTypeElement = new TiXmlElement( "xs:complexType" );
  3292. pPolygonElement->LinkEndChild( pPolygonComplexTypeElement );
  3293. // Create "polygon" child.
  3294. TiXmlElement* pPolygonElementA = new TiXmlElement( "xs:choice" );
  3295. pPolygonElementA->SetAttribute( "minOccurs", 0 );
  3296. pPolygonElementA->SetAttribute( "maxOccurs", "unbounded" );
  3297. pPolygonComplexTypeElement->LinkEndChild( pPolygonElementA );
  3298. TiXmlElement* pPolygonElementB = new TiXmlElement( "xs:element" );
  3299. pPolygonElementB->SetAttribute( "name", shapePointName );
  3300. pPolygonElementB->SetAttribute( "type", "Vector2_ConsoleType" );
  3301. pPolygonElementA->LinkEndChild( pPolygonElementB );
  3302. // Create "IsSensor" attribute.
  3303. pPolygonElementA = new TiXmlElement( "xs:attribute" );
  3304. pPolygonElementA->SetAttribute( "name", shapeSensorName );
  3305. pPolygonElementA->SetAttribute( "type", "xs:boolean" );
  3306. pPolygonComplexTypeElement->LinkEndChild( pPolygonElementA );
  3307. // Create "Density" attribute.
  3308. pPolygonElementA = new TiXmlElement( "xs:attribute" );
  3309. pPolygonElementA->SetAttribute( "name", shapeDensityName );
  3310. pPolygonComplexTypeElement->LinkEndChild( pPolygonElementA );
  3311. pPolygonElementB = new TiXmlElement( "xs:simpleType" );
  3312. pPolygonElementA->LinkEndChild( pPolygonElementB );
  3313. TiXmlElement* pPolygonElementC = new TiXmlElement( "xs:restriction" );
  3314. pPolygonElementC->SetAttribute( "base", "xs:float" );
  3315. pPolygonElementB->LinkEndChild( pPolygonElementC );
  3316. TiXmlElement* pPolygonElementD = new TiXmlElement( "xs:minInclusive" );
  3317. pPolygonElementD->SetAttribute( "value", "0" );
  3318. pPolygonElementC->LinkEndChild( pPolygonElementD );
  3319. // Create "Friction" attribute.
  3320. pPolygonElementA = new TiXmlElement( "xs:attribute" );
  3321. pPolygonElementA->SetAttribute( "name", shapeFrictionName );
  3322. pPolygonComplexTypeElement->LinkEndChild( pPolygonElementA );
  3323. pPolygonElementB = new TiXmlElement( "xs:simpleType" );
  3324. pPolygonElementA->LinkEndChild( pPolygonElementB );
  3325. pPolygonElementC = new TiXmlElement( "xs:restriction" );
  3326. pPolygonElementC->SetAttribute( "base", "xs:float" );
  3327. pPolygonElementB->LinkEndChild( pPolygonElementC );
  3328. pPolygonElementD = new TiXmlElement( "xs:minInclusive" );
  3329. pPolygonElementD->SetAttribute( "value", "0" );
  3330. pPolygonElementC->LinkEndChild( pPolygonElementD );
  3331. // Create "Restitution" attribute.
  3332. pPolygonElementA = new TiXmlElement( "xs:attribute" );
  3333. pPolygonElementA->SetAttribute( "name", shapeRestitutionName );
  3334. pPolygonComplexTypeElement->LinkEndChild( pPolygonElementA );
  3335. pPolygonElementB = new TiXmlElement( "xs:simpleType" );
  3336. pPolygonElementA->LinkEndChild( pPolygonElementB );
  3337. pPolygonElementC = new TiXmlElement( "xs:restriction" );
  3338. pPolygonElementC->SetAttribute( "base", "xs:float" );
  3339. pPolygonElementB->LinkEndChild( pPolygonElementC );
  3340. pPolygonElementD = new TiXmlElement( "xs:minInclusive" );
  3341. pPolygonElementD->SetAttribute( "value", "0" );
  3342. pPolygonElementC->LinkEndChild( pPolygonElementD );
  3343. // ********************************************************************************
  3344. // Create Chain
  3345. // ********************************************************************************
  3346. TiXmlElement* pChainElement = new TiXmlElement( "xs:element" );
  3347. pChainElement->SetAttribute( "name", chainTypeName );
  3348. pChainElement->SetAttribute( "minOccurs", 0 );
  3349. pChainElement->SetAttribute( "maxOccurs", 1 );
  3350. pShapesNodeChoiceElement->LinkEndChild( pChainElement );
  3351. // Create complex type Element.
  3352. TiXmlElement* pChainComplexTypeElement = new TiXmlElement( "xs:complexType" );
  3353. pChainElement->LinkEndChild( pChainComplexTypeElement );
  3354. // Create "Chain" child.
  3355. TiXmlElement* pChainElementA = new TiXmlElement( "xs:sequence" );
  3356. pChainComplexTypeElement->LinkEndChild( pChainElementA );
  3357. TiXmlElement* pChainElementB = new TiXmlElement( "xs:choice" );
  3358. pChainElementB->SetAttribute( "minOccurs", 0 );
  3359. pChainElementB->SetAttribute( "maxOccurs", "unbounded" );
  3360. pChainElementA->LinkEndChild( pChainElementB );
  3361. TiXmlElement* pChainElementC = new TiXmlElement( "xs:element" );
  3362. pChainElementC->SetAttribute( "name", shapePointName );
  3363. pChainElementC->SetAttribute( "type", "Vector2_ConsoleType" );
  3364. pChainElementB->LinkEndChild( pChainElementC );
  3365. TiXmlElement* pChainElementD = new TiXmlElement( "xs:element" );
  3366. pChainElementD->SetAttribute( "name", shapePrevPointName );
  3367. pChainElementD->SetAttribute( "type", "Vector2_ConsoleType" );
  3368. pChainElementD->SetAttribute( "minOccurs", 0 );
  3369. pChainElementD->SetAttribute( "maxOccurs", 1 );
  3370. pChainElementA->LinkEndChild( pChainElementD );
  3371. TiXmlElement* pChainElementE = new TiXmlElement( "xs:element" );
  3372. pChainElementE->SetAttribute( "name", shapeNextPointName );
  3373. pChainElementE->SetAttribute( "type", "Vector2_ConsoleType" );
  3374. pChainElementE->SetAttribute( "minOccurs", 0 );
  3375. pChainElementE->SetAttribute( "maxOccurs", 1 );
  3376. pChainElementA->LinkEndChild( pChainElementE );
  3377. // Create "IsSensor" attribute.
  3378. pChainElementA = new TiXmlElement( "xs:attribute" );
  3379. pChainElementA->SetAttribute( "name", shapeSensorName );
  3380. pChainElementA->SetAttribute( "type", "xs:boolean" );
  3381. pChainComplexTypeElement->LinkEndChild( pChainElementA );
  3382. // Create "Density" attribute.
  3383. pChainElementA = new TiXmlElement( "xs:attribute" );
  3384. pChainElementA->SetAttribute( "name", shapeDensityName );
  3385. pChainComplexTypeElement->LinkEndChild( pChainElementA );
  3386. pChainElementB = new TiXmlElement( "xs:simpleType" );
  3387. pChainElementA->LinkEndChild( pChainElementB );
  3388. pChainElementC = new TiXmlElement( "xs:restriction" );
  3389. pChainElementC->SetAttribute( "base", "xs:float" );
  3390. pChainElementB->LinkEndChild( pChainElementC );
  3391. pChainElementD = new TiXmlElement( "xs:minInclusive" );
  3392. pChainElementD->SetAttribute( "value", "0" );
  3393. pChainElementC->LinkEndChild( pChainElementD );
  3394. // Create "Friction" attribute.
  3395. pChainElementA = new TiXmlElement( "xs:attribute" );
  3396. pChainElementA->SetAttribute( "name", shapeFrictionName );
  3397. pChainComplexTypeElement->LinkEndChild( pChainElementA );
  3398. pChainElementB = new TiXmlElement( "xs:simpleType" );
  3399. pChainElementA->LinkEndChild( pChainElementB );
  3400. pChainElementC = new TiXmlElement( "xs:restriction" );
  3401. pChainElementC->SetAttribute( "base", "xs:float" );
  3402. pChainElementB->LinkEndChild( pChainElementC );
  3403. pChainElementD = new TiXmlElement( "xs:minInclusive" );
  3404. pChainElementD->SetAttribute( "value", "0" );
  3405. pChainElementC->LinkEndChild( pChainElementD );
  3406. // Create "Restitution" attribute.
  3407. pChainElementA = new TiXmlElement( "xs:attribute" );
  3408. pChainElementA->SetAttribute( "name", shapeRestitutionName );
  3409. pChainComplexTypeElement->LinkEndChild( pChainElementA );
  3410. pChainElementB = new TiXmlElement( "xs:simpleType" );
  3411. pChainElementA->LinkEndChild( pChainElementB );
  3412. pChainElementC = new TiXmlElement( "xs:restriction" );
  3413. pChainElementC->SetAttribute( "base", "xs:float" );
  3414. pChainElementB->LinkEndChild( pChainElementC );
  3415. pChainElementD = new TiXmlElement( "xs:minInclusive" );
  3416. pChainElementD->SetAttribute( "value", "0" );
  3417. pChainElementC->LinkEndChild( pChainElementD );
  3418. // ********************************************************************************
  3419. // Create Edge
  3420. // ********************************************************************************
  3421. TiXmlElement* pEdgeElement = new TiXmlElement( "xs:element" );
  3422. pEdgeElement->SetAttribute( "name", edgeTypeName );
  3423. pEdgeElement->SetAttribute( "minOccurs", 0 );
  3424. pEdgeElement->SetAttribute( "maxOccurs", 1 );
  3425. pShapesNodeChoiceElement->LinkEndChild( pEdgeElement );
  3426. // Create complex type Element.
  3427. TiXmlElement* pEdgeComplexTypeElement = new TiXmlElement( "xs:complexType" );
  3428. pEdgeElement->LinkEndChild( pEdgeComplexTypeElement );
  3429. // Create "Edge" child.
  3430. TiXmlElement* pEdgeElementA = new TiXmlElement( "xs:sequence" );
  3431. pEdgeComplexTypeElement->LinkEndChild( pEdgeElementA );
  3432. TiXmlElement* pEdgeElementB = new TiXmlElement( "xs:element" );
  3433. pEdgeElementB->SetAttribute( "name", shapePointName );
  3434. pEdgeElementB->SetAttribute( "type", "Vector2_ConsoleType" );
  3435. pEdgeElementB->SetAttribute( "minOccurs", 0 );
  3436. pEdgeElementB->SetAttribute( "maxOccurs", 2 );
  3437. pEdgeElementA->LinkEndChild( pEdgeElementB );
  3438. TiXmlElement* pEdgeElementC = new TiXmlElement( "xs:element" );
  3439. pEdgeElementC->SetAttribute( "name", shapePrevPointName );
  3440. pEdgeElementC->SetAttribute( "type", "Vector2_ConsoleType" );
  3441. pEdgeElementC->SetAttribute( "minOccurs", 0 );
  3442. pEdgeElementC->SetAttribute( "maxOccurs", 1 );
  3443. pEdgeElementA->LinkEndChild( pEdgeElementC );
  3444. TiXmlElement* pEdgeElementD = new TiXmlElement( "xs:element" );
  3445. pEdgeElementD->SetAttribute( "name", shapeNextPointName );
  3446. pEdgeElementD->SetAttribute( "type", "Vector2_ConsoleType" );
  3447. pEdgeElementD->SetAttribute( "minOccurs", 0 );
  3448. pEdgeElementD->SetAttribute( "maxOccurs", 1 );
  3449. pEdgeElementA->LinkEndChild( pEdgeElementD );
  3450. // Create "IsSensor" attribute.
  3451. pEdgeElementA = new TiXmlElement( "xs:attribute" );
  3452. pEdgeElementA->SetAttribute( "name", shapeSensorName );
  3453. pEdgeElementA->SetAttribute( "type", "xs:boolean" );
  3454. pEdgeComplexTypeElement->LinkEndChild( pEdgeElementA );
  3455. // Create "Density" attribute.
  3456. pEdgeElementA = new TiXmlElement( "xs:attribute" );
  3457. pEdgeElementA->SetAttribute( "name", shapeDensityName );
  3458. pEdgeComplexTypeElement->LinkEndChild( pEdgeElementA );
  3459. pEdgeElementB = new TiXmlElement( "xs:simpleType" );
  3460. pEdgeElementA->LinkEndChild( pEdgeElementB );
  3461. pEdgeElementC = new TiXmlElement( "xs:restriction" );
  3462. pEdgeElementC->SetAttribute( "base", "xs:float" );
  3463. pEdgeElementB->LinkEndChild( pEdgeElementC );
  3464. pEdgeElementD = new TiXmlElement( "xs:minInclusive" );
  3465. pEdgeElementD->SetAttribute( "value", "0" );
  3466. pEdgeElementC->LinkEndChild( pEdgeElementD );
  3467. // Create "Friction" attribute.
  3468. pEdgeElementA = new TiXmlElement( "xs:attribute" );
  3469. pEdgeElementA->SetAttribute( "name", shapeFrictionName );
  3470. pEdgeComplexTypeElement->LinkEndChild( pEdgeElementA );
  3471. pEdgeElementB = new TiXmlElement( "xs:simpleType" );
  3472. pEdgeElementA->LinkEndChild( pEdgeElementB );
  3473. pEdgeElementC = new TiXmlElement( "xs:restriction" );
  3474. pEdgeElementC->SetAttribute( "base", "xs:float" );
  3475. pEdgeElementB->LinkEndChild( pEdgeElementC );
  3476. pEdgeElementD = new TiXmlElement( "xs:minInclusive" );
  3477. pEdgeElementD->SetAttribute( "value", "0" );
  3478. pEdgeElementC->LinkEndChild( pEdgeElementD );
  3479. // Create "Restitution" attribute.
  3480. pEdgeElementA = new TiXmlElement( "xs:attribute" );
  3481. pEdgeElementA->SetAttribute( "name", shapeRestitutionName );
  3482. pEdgeComplexTypeElement->LinkEndChild( pEdgeElementA );
  3483. pEdgeElementB = new TiXmlElement( "xs:simpleType" );
  3484. pEdgeElementA->LinkEndChild( pEdgeElementB );
  3485. pEdgeElementC = new TiXmlElement( "xs:restriction" );
  3486. pEdgeElementC->SetAttribute( "base", "xs:float" );
  3487. pEdgeElementB->LinkEndChild( pEdgeElementC );
  3488. pEdgeElementD = new TiXmlElement( "xs:minInclusive" );
  3489. pEdgeElementD->SetAttribute( "value", "0" );
  3490. pEdgeElementC->LinkEndChild( pEdgeElementD );
  3491. }
  3492. //-----------------------------------------------------------------------------
  3493. IMPLEMENT_CONOBJECT_SCHEMA(SceneObject, WriteCustomTamlSchema);